Debian Codegraph @

tools/viewer/TypefaceSlide.cpp

raw
1 /*
2 * Copyright 2024 Google Inc.
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 #include "include/core/SkCanvas.h"
8 #include "include/core/SkContourMeasure.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkFontMetrics.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathBuilder.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/utils/SkNoDrawCanvas.h"
18 #include "src/base/SkRandom.h"
19 #include "tools/SkMetaData.h"
20 #include "tools/ToolUtils.h"
21 #include "tools/fonts/FontToolUtils.h"
22 #include "tools/viewer/Slide.h"
23
24 namespace {
25
26 class TypefaceSlide : public Slide {
27 public:
28 TypefaceSlide() { fName = "Typeface Viewer"; }
29
30 void load(SkScalar w, SkScalar h) override {
31 fWindowSize = {w, h};
32
33 fBaseTypeface = ToolUtils::CreateTypefaceFromResource("fonts/Variable.ttf");
34 fCurrentTypeface = fBaseTypeface;
35 fVariationSliders = ToolUtils::VariationSliders(fCurrentTypeface.get(), fVariationPosition);
36
37 fPathDirectionIndicator = SkPathBuilder()
38 .moveTo(0, -3)
39 .lineTo(3, 0)
40 .lineTo(0, 3)
41 .close()
42 .detach();
43
44 fPathDirectionIndicatorPaint.setColor(SK_ColorRED);
45 fPathDirectionIndicatorPaint.setStroke(true);
46 fPathDirectionIndicatorPaint.setStrokeWidth(0);
47 }
48
49 void resize(SkScalar w, SkScalar h) override { fWindowSize = {w, h}; }
50
51 void unload() override {
52 fBaseTypeface = nullptr;
53 fCurrentTypeface = nullptr;
54 }
55
56 SkISize getDimensions() const override {
57 if (fDrawArea.isEmpty()) {
58 TypefaceSlide& self = *const_cast<TypefaceSlide*>(this);
59 SkNoDrawCanvas noDraw(fWindowSize.width(), fWindowSize.height());
60 self.draw(&noDraw);
61 }
62 return fDrawArea;
63 }
64
65 void updateCurrentTypeface() {
66 if (!fBaseTypeface) {
67 return;
68 }
69 SkSpan<const SkFontArguments::VariationPosition::Coordinate> coords =
70 fVariationSliders.getCoordinates();
71 SkFontArguments::VariationPosition varPos = {coords.data(),
72 static_cast<int>(coords.size())};
73 SkFontArguments args;
74 args.setVariationDesignPosition(varPos);
75 fCurrentTypeface = fBaseTypeface->makeClone(args);
76 fCurrentTypefaceDirty = false;
77 }
78
79 void draw(SkCanvas* canvas) override {
80 SkPaint notationPaint;
81 notationPaint.setColor(SK_ColorRED);
82 SkFont notationFont(ToolUtils::DefaultTypeface(), 8.0);
83
84 SkPaint paint;
85 if (fOutline) {
86 paint.setStroke(true);
87 paint.setStrokeWidth(0);
88 }
89 if (fCurrentTypefaceDirty) {
90 this->updateCurrentTypeface();
91 }
92 SkFont font(fCurrentTypeface, fFontSize);
93 SkFontMetrics metrics;
94 font.getMetrics(&metrics);
95
96 SkPoint origin{0, 0};
97 SkPoint position{0, 0};
98 SkRect drawBounds = SkRect::MakeXYWH(0, 0, 1, 1);
99 struct Line {
100 SkRect bounds;
101 SkGlyphID firstGlyph; // inclusive
102 SkGlyphID lastGlyph; // inclusive
103 int number;
104 } line{{}, fCurrentGlyphID, fCurrentGlyphID, 0};
105
106 int numGlyphs = fCurrentTypeface->countGlyphs();
107 if (numGlyphs == 0) {
108 fDrawArea.setEmpty();
109 return;
110 }
111 SkGlyphID lastGlyph = numGlyphs - 1;
112 for (SkGlyphID glyph = line.firstGlyph; line.number < 2; ++glyph) {
113 // measure a line
114 SkRect beginLineGlyphBounds;
115 {
116 SkRect newLineBounds = line.bounds;
117 if (glyph != line.firstGlyph) {
118 newLineBounds.fRight += 10;
119 }
120 SkScalar advance;
121 SkRect glyphBounds;
122 font.getWidthsBounds({&glyph, 1}, {&advance, 1}, {&glyphBounds, 1}, nullptr);
123 SkRect advanceBounds = SkRect::MakeWH(advance, 1);
124 SkRect glyphAndAdvanceBounds = glyphBounds;
125 glyphAndAdvanceBounds.join(advanceBounds);
126 beginLineGlyphBounds = glyphAndAdvanceBounds;
127 beginLineGlyphBounds.offset(-beginLineGlyphBounds.fLeft, 0);
128
129 SkRect glyphDrawBounds = beginLineGlyphBounds.makeOffset(newLineBounds.right(), 0);
130 if (line.number == -1) {
131 SkPaint p;
132 p.setColor(SK_ColorBLUE);
133 canvas->drawRect(glyphDrawBounds, p);
134 }
135 newLineBounds.join(glyphDrawBounds);
136 if (newLineBounds.width() < fWindowSize.width()) {
137 line.lastGlyph = glyph;
138 line.bounds = newLineBounds;
139 if (glyph != lastGlyph) {
140 continue;
141 }
142 }
143 }
144
145 // draw the line
146 position.fY -= line.bounds.top();
147 for (SkGlyphID gid = line.firstGlyph; gid <= line.lastGlyph; ++gid) {
148 if (gid != line.firstGlyph) {
149 position.fX += 10;
150 }
151 SkScalar advance;
152 SkRect glyphBounds;
153 font.getWidthsBounds({&gid, 1}, {&advance, 1}, {&glyphBounds, 1}, nullptr);
154 SkRect advanceBounds = SkRect::MakeWH(advance, 1);
155 SkRect glyphAndAdvanceBounds = glyphBounds;
156 glyphAndAdvanceBounds.join(advanceBounds);
157
158 position.fX -= glyphAndAdvanceBounds.left();
159
160 if (fDrawGlyphMetrics) {
161 SkPaint metricPaint;
162 metricPaint.setColor(SK_ColorRED);
163 canvas->drawRect(glyphBounds.makeOffset(position), metricPaint);
164 metricPaint.setColor(SK_ColorGREEN);
165 canvas->drawRect(advanceBounds.makeOffset(position), metricPaint);
166 }
167
168 canvas->drawGlyphs({&gid, 1}, {&position, 1}, origin, font, paint);
169
170 if (fGlyphNumbers) {
171 SkString gidStr;
172 gidStr.appendS32(gid);
173 canvas->drawSimpleText(gidStr.c_str(), gidStr.size(), SkTextEncoding::kUTF8,
174 position.x(), position.y(), notationFont, notationPaint);
175 }
176 // TODO: also handle drawable by using a paint override canvas?
177 if (fOutline) {
178 SkContourMeasureIter iter(font.getPath(gid).value_or(SkPath()), false);
179 sk_sp<SkContourMeasure> contour;
180 int contourIndex = 0;
181 while ((contour = iter.next())) {
182 SkPoint contourStart;
183 SkVector tangent;
184 if (contour->getPosTan(0, &contourStart, &tangent)) {
185 contourStart += position;
186 SkAutoCanvasRestore acr(canvas, true);
187 SkMatrix matrix;
188 matrix.setSinCos(tangent.y(), tangent.x(), 0, 0);
189 matrix.postTranslate(contourStart.x(), contourStart.y());
190 canvas->concat(matrix);
191
192 if (fOutlineContourNumbers) {
193 SkString contourStr;
194 contourStr.appendS32(contourIndex);
195 canvas->drawSimpleText(contourStr.c_str(), contourStr.size(),
196 SkTextEncoding::kUTF8, 0, 0,
197 notationFont, notationPaint);
198 } else {
199 canvas->drawPath(fPathDirectionIndicator, fPathDirectionIndicatorPaint);
200 }
201
202 ++contourIndex;
203 }
204 }
205 }
206
207 position.fX += glyphAndAdvanceBounds.right();
208 }
209 if (line.lastGlyph == lastGlyph) {
210 break;
211 }
212 drawBounds.join(line.bounds.makeOffset(-line.bounds.fLeft, position.fY));
213 position.fX = 0;
214 position.fY += line.bounds.bottom() + 10;
215 line.bounds = beginLineGlyphBounds;
216 line.firstGlyph = glyph;
217 line.lastGlyph = glyph;
218 ++line.number;
219 }
220
221 fDrawArea = drawBounds.roundOut().size();
222 }
223
224 bool onGetControls(SkMetaData* controls) override {
225 // requested font size
226 SkScalar size[3] = {fFontSize, 0, 256};
227 controls->setScalars("Size", 3, size);
228
229 // the first glyph on the first line
230 SkScalar glyph[3] = {SkScalar(fCurrentGlyphID), 0, SkScalar(this->fCurrentTypeface->countGlyphs())};
231 controls->setScalars("Glyph", 3, glyph);
232
233 // TODO: limit number of glyphs
234 // TODO: choose typeface factory
235 // TODO: choose between typefaces
236 // TODO: font metrics like underline, strikeout, x-height, cap-height, etc.
237
238 // show glyph metrics like advances and bounds
239 controls->setBool("Glyph Metrics", fDrawGlyphMetrics);
240
241 // Show glyph numbers at origin
242 controls->setBool("Glyph numbers", fGlyphNumbers);
243
244 // hairline contours with initial direction mark
245 controls->setBool("Outline", fOutline);
246
247 // draw contour numbers instead of initial contour direction mark
248 controls->setBool("Outline contour numbers", fOutlineContourNumbers);
249
250 return fVariationSliders.writeControls(controls);
251 }
252
253 void onSetControls(const SkMetaData& controls) override {
254 SkScalar size[3] = {0};
255 int numReturnedScalars = 0;
256 SkASSERT_RELEASE(controls.findScalars("Size", &numReturnedScalars, size));
257 SkASSERT_RELEASE(numReturnedScalars == 3);
258 if (fFontSize != size[0]) {
259 fFontSize = size[0];
260 fDrawArea.setEmpty();
261 }
262
263 SkScalar glyph[3] = {0};
264 numReturnedScalars = 0;
265 SkASSERT_RELEASE(controls.findScalars("Glyph", &numReturnedScalars, glyph));
266 SkASSERT_RELEASE(numReturnedScalars == 3);
267 if (fCurrentGlyphID != SkScalarRoundToInt(glyph[0])) {
268 fCurrentGlyphID = SkScalarRoundToInt(glyph[0]);
269 fDrawArea.setEmpty();
270 }
271
272 controls.findBool("Glyph Metrics", &fDrawGlyphMetrics);
273 controls.findBool("Glyph numbers", &fGlyphNumbers);
274 controls.findBool("Outline", &fOutline);
275 controls.findBool("Outline contour numbers", &fOutlineContourNumbers);
276
277 fVariationSliders.readControls(controls, &fCurrentTypefaceDirty);
278 }
279
280 private:
281 sk_sp<SkTypeface> fBaseTypeface;
282 sk_sp<SkTypeface> fCurrentTypeface;
283
284 std::unique_ptr<SkFontArguments::VariationPosition::Coordinate[]> fCoordinates;
285 SkFontArguments::VariationPosition fVariationPosition;
286 ToolUtils::VariationSliders fVariationSliders;
287 bool fCurrentTypefaceDirty = true;
288 SkScalar fFontSize = 80;
289 bool fOutline = false;
290 bool fOutlineContourNumbers = false;
291 bool fGlyphNumbers = false;
292 bool fDrawGlyphMetrics = false;
293 SkGlyphID fCurrentGlyphID = 0;
294
295 SkSize fWindowSize;
296 SkISize fDrawArea;
297
298 SkPath fPathDirectionIndicator;
299 SkPaint fPathDirectionIndicatorPaint;
300 };
301
302 } //namespace
303
304 //////////////////////////////////////////////////////////////////////////////
305
306 DEF_SLIDE( return new TypefaceSlide(); )