forked from google/skia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stub Fontations SkTypeface backend
Preparation for using Rust code to fill the actual functionality. At the moment, this backend can draw a single outline, emulating drawing the capital H from Roboto Regular, and emulating Roboto Regular font metrics. The code is to a large extent taken from TestTypeface code in tools/fonts but is intended to be filled and replaced with Rust logic from src/ports/fontations/* in forthcoming CLs. Define build flag use_fontations for GN and bazel, add a bazel alias --with_fontations for enabling building of this backend. Add a GM test (for the GN build) which directly instantiates this typeface and draws three instances of the H glyphs at different sizes. Bug: skia:14257 Change-Id: I6460e70b2dac8665e4d7f7171bad9b43b8d93c84 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/662076 Reviewed-by: Kevin Lubick <[email protected]> Reviewed-by: Ben Wagner <[email protected]> Commit-Queue: Dominik Röttsches <[email protected]>
- Loading branch information
Showing
10 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include "gm/gm.h" | ||
#include "include/core/SkCanvas.h" | ||
#include "include/core/SkRefCnt.h" | ||
#include "include/core/SkTypeface.h" | ||
#include "src/ports/SkTypeface_fontations.h" | ||
|
||
namespace skiagm { | ||
|
||
namespace { | ||
const SkScalar kTextSizes[] = {12, 18, 30, 120}; | ||
|
||
} // namespace | ||
|
||
class FontationsTypefaceGM : public GM { | ||
public: | ||
FontationsTypefaceGM() { this->setBGColor(SK_ColorWHITE); } | ||
|
||
protected: | ||
void onOnceBeforeDraw() override { fTypeface = sk_sp<SkTypeface>(new SkTypeface_Fontations()); } | ||
|
||
SkString onShortName() override { return SkString("typeface_fontations"); } | ||
|
||
SkISize onISize() override { return SkISize::Make(400, 200); } | ||
|
||
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { | ||
SkPaint paint; | ||
paint.setColor(SK_ColorBLACK); | ||
|
||
if (!fTypeface) { | ||
*errorMsg = "Unable to initialize typeface."; | ||
return DrawResult::kSkip; | ||
} | ||
|
||
SkFont font(fTypeface); | ||
uint16_t glyphs[] = {1, 1, 1}; | ||
SkScalar x = 100; | ||
SkScalar y = 150; | ||
|
||
for (SkScalar textSize : kTextSizes) { | ||
font.setSize(textSize); | ||
y += font.getSpacing(); | ||
|
||
/* Draw origin marker as a green dot. */ | ||
paint.setColor(SK_ColorGREEN); | ||
canvas->drawRect(SkRect::MakeXYWH(x, y, 2, 2), paint); | ||
paint.setColor(SK_ColorBLACK); | ||
|
||
canvas->drawSimpleText(glyphs, | ||
sizeof(uint16_t) * std::size(glyphs), | ||
SkTextEncoding::kGlyphID, | ||
x, | ||
y, | ||
font, | ||
paint); | ||
} | ||
|
||
return DrawResult::kOk; | ||
} | ||
|
||
private: | ||
using INHERITED = GM; | ||
|
||
sk_sp<SkTypeface> fTypeface; | ||
}; | ||
|
||
DEF_GM(return new FontationsTypefaceGM();) | ||
|
||
} // namespace skiagm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#include "include/core/SkFontMetrics.h" | ||
#include "src/core/SkFontPriv.h" | ||
#include "src/ports/SkTypeface_fontations.h" | ||
|
||
namespace { | ||
/* Placeholder glyph example until we extract paths through fontations, representing the capital H | ||
* from Roboto Regular. */ | ||
void drawCapitalH(SkPath* path) { | ||
path->setFillType(SkPathFillType::kWinding); | ||
path->moveTo(1096, -0); | ||
path->lineTo(1096, -673); | ||
path->lineTo(362, -673); | ||
path->lineTo(362, -0); | ||
path->lineTo(169, -0); | ||
path->lineTo(169, -1456); | ||
path->lineTo(362, -1456); | ||
path->lineTo(362, -830); | ||
path->lineTo(1096, -830); | ||
path->lineTo(1096, -1456); | ||
path->lineTo(1288, -1456); | ||
path->lineTo(1288, -0); | ||
path->lineTo(1096, -0); | ||
path->close(); | ||
} | ||
|
||
constexpr SkScalar capitalHAdvance = 1461; | ||
} | ||
|
||
int SkTypeface_Fontations::onGetUPEM() const { return 2048; } | ||
|
||
void SkTypeface_Fontations::onCharsToGlyphs(const SkUnichar* chars, | ||
int count, | ||
SkGlyphID glyphs[]) const { | ||
sk_bzero(glyphs, count * sizeof(glyphs[0])); | ||
} | ||
|
||
void SkTypeface_Fontations::onFilterRec(SkScalerContextRec* rec) const { | ||
rec->setHinting(SkFontHinting::kNone); | ||
} | ||
|
||
class SkFontationsScalerContext : public SkScalerContext { | ||
public: | ||
SkFontationsScalerContext(sk_sp<SkTypeface_Fontations> face, | ||
const SkScalerContextEffects& effects, | ||
const SkDescriptor* desc) | ||
: SkScalerContext(std::move(face), effects, desc) { | ||
fRec.getSingleMatrix(&fMatrix); | ||
this->forceGenerateImageFromPath(); | ||
} | ||
|
||
protected: | ||
|
||
bool generateAdvance(SkGlyph* glyph) override { | ||
const SkVector advance = fMatrix.mapXY(capitalHAdvance / getTypeface()->getUnitsPerEm(), | ||
SkFloatToScalar(0.f)); | ||
glyph->fAdvanceX = SkScalarToFloat(advance.fX); | ||
glyph->fAdvanceY = SkScalarToFloat(advance.fY); | ||
return true; | ||
} | ||
|
||
void generateMetrics(SkGlyph* glyph, SkArenaAlloc*) override { | ||
glyph->fMaskFormat = fRec.fMaskFormat; | ||
glyph->zeroMetrics(); | ||
this->generateAdvance(glyph); | ||
// Always generates from paths, so SkScalerContext::makeGlyph will figure the bounds. | ||
} | ||
|
||
void generateImage(const SkGlyph&) override { SK_ABORT("Should have generated from path."); } | ||
|
||
bool generatePath(const SkGlyph& glyph, SkPath* path) override { | ||
drawCapitalH(path); | ||
SkMatrix scaled = fMatrix.preScale(1.0f / getTypeface()->getUnitsPerEm(), | ||
1.0f / getTypeface()->getUnitsPerEm()); | ||
*path = path->makeTransform(scaled); | ||
return true; | ||
} | ||
|
||
void generateFontMetrics(SkFontMetrics* metrics) override { | ||
/* Hard-coded Roboto Regular metrics, to be replaced with Fontations calls. */ | ||
metrics->fTop = -2163.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fAscent = -2146.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fDescent = 555.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fBottom = 555.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fLeading = 0; | ||
metrics->fAvgCharWidth = 0; | ||
metrics->fMaxCharWidth = 0; | ||
metrics->fXMin = -1825.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fXMax = 4188.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fXHeight = -1082.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fCapHeight = -1456.f / getTypeface()->getUnitsPerEm(); | ||
metrics->fFlags = 0; | ||
|
||
SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY()); | ||
} | ||
|
||
private: | ||
SkMatrix fMatrix; | ||
}; | ||
|
||
std::unique_ptr<SkScalerContext> SkTypeface_Fontations::onCreateScalerContext( | ||
const SkScalerContextEffects& effects, const SkDescriptor* desc) const | ||
{ | ||
return std::make_unique<SkFontationsScalerContext>( | ||
sk_ref_sp(const_cast<SkTypeface_Fontations*>(this)), effects, desc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2023 Google Inc. | ||
* | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
#ifndef SkTypeface_Fontations_DEFINED | ||
#define SkTypeface_Fontations_DEFINED | ||
|
||
#include "include/core/SkStream.h" | ||
#include "include/core/SkTypeface.h" | ||
#include "src/core/SkAdvancedTypefaceMetrics.h" | ||
#include "src/core/SkScalerContext.h" | ||
|
||
/** SkTypeface implementation based on Google Fonts Fontations Rust libraries. */ | ||
class SkTypeface_Fontations : public SkTypeface { | ||
public: | ||
static sk_sp<SkTypeface> Make() { return sk_sp<SkTypeface>(new SkTypeface_Fontations); } | ||
SkTypeface_Fontations() : SkTypeface(SkFontStyle(), true) {} | ||
|
||
protected: | ||
|
||
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; } | ||
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override { | ||
return sk_ref_sp(this); | ||
} | ||
std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects& effects, | ||
const SkDescriptor* desc) const override; | ||
void onFilterRec(SkScalerContextRec*) const override; | ||
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override { | ||
return nullptr; | ||
} | ||
void onGetFontDescriptor(SkFontDescriptor*, bool*) const override {} | ||
void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; | ||
int onCountGlyphs() const override { return 1; } | ||
void getPostScriptGlyphNames(SkString*) const override {} | ||
void getGlyphToUnicodeMap(SkUnichar*) const override {} | ||
int onGetUPEM() const override; | ||
class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings { | ||
public: | ||
bool next(SkTypeface::LocalizedString*) override { return false; } | ||
}; | ||
void onGetFamilyName(SkString* familyName) const override { familyName->reset(); } | ||
bool onGetPostScriptName(SkString*) const override { return false; } | ||
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { | ||
return new EmptyLocalizedStrings; | ||
} | ||
bool onGlyphMaskNeedsCurrentColor() const override { return false; } | ||
int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], | ||
int coordinateCount) const override { | ||
return 0; | ||
} | ||
int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], | ||
int parameterCount) const override { | ||
return 0; | ||
} | ||
int onGetTableTags(SkFontTableTag tags[]) const override { return 0; } | ||
size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override { return 0; } | ||
}; | ||
|
||
#endif // SkTypeface_Fontations_DEFINED |