Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3a79f33

Browse files
reed-at-googleSkia Commit-Bot
authored andcommitted
custom typeface
- only paths implemented at the moment Seems if we want to serialize/deserialize these, we will need to register a factory with skia, so it can sniff the beginning of the font "file", to know how to recreate it. Lots of follow-on things to explore: - do we need to even store/know advance widths? - should we also (optionally) support a CMAP? names? others? Change-Id: If9fa99b7b8f6e265f06eb3ba2ca4fcb073275250 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/287157 Reviewed-by: Ben Wagner <[email protected]> Commit-Queue: Mike Reed <[email protected]>
1 parent 46d9138 commit 3a79f33

File tree

7 files changed

+488
-0
lines changed

7 files changed

+488
-0
lines changed

gm/userfont.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#include "gm/gm.h"
9+
#include "include/core/SkCanvas.h"
10+
#include "include/core/SkFont.h"
11+
#include "include/core/SkPaint.h"
12+
#include "include/core/SkPath.h"
13+
#include "include/core/SkSize.h"
14+
#include "include/core/SkString.h"
15+
#include "include/utils/SkCustomTypeface.h"
16+
#include "tools/Resources.h"
17+
18+
static sk_sp<SkTypeface> make_tf() {
19+
SkCustomTypefaceBuilder builder(128);
20+
SkFont font;
21+
font.setSize(1.0f);
22+
font.setHinting(SkFontHinting::kNone);
23+
24+
// Steal the first 128 chars from the default font
25+
for (SkGlyphID index = 0; index <= 127; ++index) {
26+
SkGlyphID glyph = font.unicharToGlyph(index);
27+
28+
SkScalar width;
29+
font.getWidths(&glyph, 1, &width);
30+
SkPath path;
31+
font.getPath(glyph, &path);
32+
33+
// we use the charcode to be our glyph index, since we have no cmap table
34+
builder.setGlyph(index, width, path);
35+
}
36+
37+
return builder.detach();
38+
}
39+
40+
#include "include/core/SkTextBlob.h"
41+
42+
class UserFontGM : public skiagm::GM {
43+
sk_sp<SkTypeface> fTF;
44+
sk_sp<SkTextBlob> fBlob;
45+
46+
SkPath fPath;
47+
public:
48+
UserFontGM() {}
49+
50+
void onOnceBeforeDraw() override {
51+
fTF = make_tf();
52+
53+
SkFont font(fTF);
54+
font.setSize(100);
55+
font.setEdging(SkFont::Edging::kAntiAlias);
56+
57+
std::vector<SkGlyphID> array;
58+
auto expand8to16 = [&](const char str[]) {
59+
for (int i = 0; str[i]; ++i) {
60+
array.push_back(str[i]);
61+
}
62+
};
63+
64+
expand8to16("User Typeface");
65+
fBlob = SkTextBlob::MakeFromText(array.data(), array.size() * sizeof(SkGlyphID),
66+
font, SkTextEncoding::kGlyphID);
67+
68+
}
69+
70+
bool runAsBench() const override { return true; }
71+
72+
SkString onShortName() override { return SkString("user_typeface"); }
73+
74+
SkISize onISize() override { return {512, 512}; }
75+
76+
void onDraw(SkCanvas* canvas) override {
77+
SkPaint paint;
78+
paint.setColor(SK_ColorRED);
79+
canvas->drawTextBlob(fBlob, 20, 150, paint);
80+
}
81+
};
82+
DEF_GM(return new UserFontGM;)

gn/gm.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ gm_sources = [
378378
"$_gm/trickycubicstrokes.cpp",
379379
"$_gm/typeface.cpp",
380380
"$_gm/unpremul.cpp",
381+
"$_gm/userfont.cpp",
381382
"$_gm/variedtext.cpp",
382383
"$_gm/verifiers/gmverifier.cpp",
383384
"$_gm/vertices.cpp",

gn/utils.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ skia_utils_public = [
1212
"$_include/utils/SkBase64.h",
1313
"$_include/utils/SkCamera.h",
1414
"$_include/utils/SkCanvasStateUtils.h",
15+
"$_include/utils/SkCustomTypeface.h",
1516
"$_include/utils/SkEventTracer.h",
1617
"$_include/utils/SkInterpolator.h",
1718
"$_include/utils/SkNWayCanvas.h",
@@ -40,6 +41,7 @@ skia_utils_sources = [
4041
"$_src/utils/SkCharToGlyphCache.h",
4142
"$_src/utils/SkClipStackUtils.cpp",
4243
"$_src/utils/SkClipStackUtils.h",
44+
"$_src/utils/SkCustomTypeface.cpp",
4345
"$_src/utils/SkDashPath.cpp",
4446
"$_src/utils/SkDashPathPriv.h",
4547
"$_src/utils/SkEventTracer.cpp",

include/utils/SkCustomTypeface.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#ifndef SkCustomTypeface_DEFINED
9+
#define SkCustomTypeface_DEFINED
10+
11+
#include "include/core/SkImage.h"
12+
#include "include/core/SkPaint.h"
13+
#include "include/core/SkPath.h"
14+
#include "include/core/SkPicture.h"
15+
#include "include/core/SkTypeface.h"
16+
17+
#include <vector>
18+
19+
class SkStream;
20+
21+
class SkCustomTypefaceBuilder {
22+
public:
23+
SkCustomTypefaceBuilder(int numGlyphs);
24+
25+
void setGlyph(SkGlyphID, float advance, const SkPath&);
26+
void setGlyph(SkGlyphID, float advance, const SkPath&, const SkPaint&);
27+
void setGlyph(SkGlyphID, float advance, sk_sp<SkImage>, float scale);
28+
void setGlyph(SkGlyphID, float advance, sk_sp<SkPicture>);
29+
30+
sk_sp<SkTypeface> detach();
31+
32+
private:
33+
int fGlyphCount;
34+
std::vector<SkPath> fPaths;
35+
std::vector<float> fAdvances;
36+
37+
static sk_sp<SkTypeface> Deserialize(SkStream*);
38+
39+
friend class SkTypeface;
40+
};
41+
42+
#endif

src/core/SkGlyph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ class SkGlyph {
296296
friend class SkStrikeServer;
297297
friend class SkTestScalerContext;
298298
friend class SkTestSVGScalerContext;
299+
friend class SkUserScalerContext;
299300
friend class TestSVGTypeface;
300301
friend class TestTypeface;
301302

src/core/SkTypeface.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "include/core/SkTypeface.h"
1212
#include "include/private/SkMutex.h"
1313
#include "include/private/SkOnce.h"
14+
#include "include/utils/SkCustomTypeface.h"
1415
#include "src/core/SkAdvancedTypefaceMetrics.h"
1516
#include "src/core/SkEndian.h"
1617
#include "src/core/SkFontDescriptor.h"
@@ -161,6 +162,12 @@ sk_sp<SkTypeface> SkTypeface::MakeFromData(sk_sp<SkData> data, int index) {
161162
}
162163

163164
sk_sp<SkTypeface> SkTypeface::MakeFromFontData(std::unique_ptr<SkFontData> data) {
165+
if (data->hasStream()) {
166+
if (auto tf = SkCustomTypefaceBuilder::Deserialize(data->getStream())) {
167+
return tf;
168+
}
169+
}
170+
164171
return SkFontMgr::RefDefault()->makeFromFontData(std::move(data));
165172
}
166173

0 commit comments

Comments
 (0)