• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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 
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkBlurMask.h"
11 #include "SkBlurMaskFilter.h"
12 #include "SkReadBuffer.h"
13 #include "SkTextBlob.h"
14 #include "SkWriteBuffer.h"
15 #include "SkLayerRasterizer.h"
16 
r0(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)17 static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
18     p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
19                                            SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
20     rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
21 
22     p.setMaskFilter(nullptr);
23     p.setStyle(SkPaint::kStroke_Style);
24     p.setStrokeWidth(SK_Scalar1);
25     rastBuilder->addLayer(p);
26 
27     p.setAlpha(0x11);
28     p.setStyle(SkPaint::kFill_Style);
29     p.setBlendMode(SkBlendMode::kSrc);
30     rastBuilder->addLayer(p);
31 }
32 
r1(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)33 static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
34     rastBuilder->addLayer(p);
35 
36     p.setAlpha(0x40);
37     p.setBlendMode(SkBlendMode::kSrc);
38     p.setStyle(SkPaint::kStroke_Style);
39     p.setStrokeWidth(SK_Scalar1*2);
40     rastBuilder->addLayer(p);
41 }
42 
r2(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)43 static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
44     p.setStyle(SkPaint::kStrokeAndFill_Style);
45     p.setStrokeWidth(SK_Scalar1*4);
46     rastBuilder->addLayer(p);
47 
48     p.setStyle(SkPaint::kStroke_Style);
49     p.setStrokeWidth(SK_Scalar1*3/2);
50     p.setBlendMode(SkBlendMode::kClear);
51     rastBuilder->addLayer(p);
52 }
53 
r3(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)54 static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
55     p.setStyle(SkPaint::kStroke_Style);
56     p.setStrokeWidth(SK_Scalar1*3);
57     rastBuilder->addLayer(p);
58 
59     p.setAlpha(0x20);
60     p.setStyle(SkPaint::kFill_Style);
61     p.setBlendMode(SkBlendMode::kSrc);
62     rastBuilder->addLayer(p);
63 }
64 
r4(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)65 static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
66     p.setAlpha(0x60);
67     rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
68 
69     p.setAlpha(0xFF);
70     p.setBlendMode(SkBlendMode::kClear);
71     rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
72 
73     p.setBlendMode(SkBlendMode::kSrcOver);
74     rastBuilder->addLayer(p);
75 }
76 
77 #include "SkDiscretePathEffect.h"
78 
r5(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)79 static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
80     rastBuilder->addLayer(p);
81 
82     p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
83     p.setBlendMode(SkBlendMode::kSrcOut);
84     rastBuilder->addLayer(p);
85 }
86 
r6(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)87 static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
88     rastBuilder->addLayer(p);
89 
90     p.setAntiAlias(false);
91     SkLayerRasterizer::Builder rastBuilder2;
92     r5(&rastBuilder2, p);
93     p.setRasterizer(rastBuilder2.detach());
94     p.setBlendMode(SkBlendMode::kClear);
95     rastBuilder->addLayer(p);
96 }
97 
98 #include "Sk2DPathEffect.h"
99 
MakeDotEffect(SkScalar radius,const SkMatrix & matrix)100 static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
101     SkPath path;
102     path.addCircle(0, 0, radius);
103     return SkPath2DPathEffect::Make(matrix, path);
104 }
105 
r7(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)106 static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
107     SkMatrix    lattice;
108     lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
109     lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
110     p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
111     rastBuilder->addLayer(p);
112 }
113 
r8(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)114 static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
115     rastBuilder->addLayer(p);
116 
117     SkMatrix    lattice;
118     lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
119     lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
120     p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
121     p.setBlendMode(SkBlendMode::kClear);
122     rastBuilder->addLayer(p);
123 
124     p.setPathEffect(nullptr);
125     p.setBlendMode(SkBlendMode::kSrcOver);
126     p.setStyle(SkPaint::kStroke_Style);
127     p.setStrokeWidth(SK_Scalar1);
128     rastBuilder->addLayer(p);
129 }
130 
r9(SkLayerRasterizer::Builder * rastBuilder,SkPaint & p)131 static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
132     rastBuilder->addLayer(p);
133 
134     SkMatrix    lattice;
135     lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
136     lattice.postRotate(SkIntToScalar(30), 0, 0);
137     p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
138     p.setBlendMode(SkBlendMode::kClear);
139     rastBuilder->addLayer(p);
140 
141     p.setPathEffect(nullptr);
142     p.setBlendMode(SkBlendMode::kSrcOver);
143     p.setStyle(SkPaint::kStroke_Style);
144     p.setStrokeWidth(SK_Scalar1);
145     rastBuilder->addLayer(p);
146 }
147 
148 typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
149 
150 constexpr raster_proc gRastProcs[] = {
151     r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
152 };
153 
apply_shader(SkPaint * paint,int index)154 static void apply_shader(SkPaint* paint, int index) {
155     raster_proc proc = gRastProcs[index];
156     if (proc) {
157         SkPaint p;
158         SkLayerRasterizer::Builder rastBuilder;
159 
160         p.setAntiAlias(true);
161         proc(&rastBuilder, p);
162         paint->setRasterizer(rastBuilder.detach());
163     }
164 
165 #if 0
166     SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
167     paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
168 #endif
169     paint->setColor(SK_ColorBLUE);
170 }
171 
172 DEF_SIMPLE_GM(texteffects, canvas, 460, 680) {
173         canvas->save();
174 
175         SkPaint     paint;
176         paint.setAntiAlias(true);
177         sk_tool_utils::set_portable_typeface(&paint);
178         paint.setTextSize(SkIntToScalar(56));
179 
180         SkScalar    x = SkIntToScalar(20);
181         SkScalar    y = paint.getTextSize();
182 
183         SkString str("Hamburgefons");
184 
185         for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
186             apply_shader(&paint, i);
187 
188             //  paint.setMaskFilter(nullptr);
189             //  paint.setColor(SK_ColorBLACK);
190 
191             canvas->drawString(str, x, y, paint);
192 
193             y += paint.getFontSpacing();
194         }
195 
196         canvas->restore();
197 }
198 
create_underline(const SkTDArray<SkScalar> & intersections,SkScalar last,SkScalar finalPos,SkScalar uPos,SkScalar uWidth,SkScalar textSize)199 static SkPath create_underline(const SkTDArray<SkScalar>& intersections,
200         SkScalar last, SkScalar finalPos,
201         SkScalar uPos, SkScalar uWidth, SkScalar textSize) {
202     SkPath underline;
203     SkScalar end = last;
204     for (int index = 0; index < intersections.count(); index += 2) {
205         SkScalar start = intersections[index] - uWidth;;
206         end = intersections[index + 1] + uWidth;
207         if (start > last && last + textSize / 12 < start) {
208             underline.moveTo(last, uPos);
209             underline.lineTo(start, uPos);
210         }
211         last = end;
212     }
213     if (end < finalPos) {
214         underline.moveTo(end, uPos);
215         underline.lineTo(finalPos, uPos);
216     }
217     return underline;
218 }
219 
find_intercepts(const char * test,size_t len,SkScalar x,SkScalar y,const SkPaint & paint,SkScalar uWidth,SkTDArray<SkScalar> * intersections)220 static void find_intercepts(const char* test, size_t len, SkScalar x, SkScalar y,
221         const SkPaint& paint, SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
222     SkScalar uPos = y + uWidth;
223     SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
224     int count = paint.getTextIntercepts(test, len, x, y, bounds, nullptr);
225     SkASSERT(!(count % 2));
226     if (count) {
227         intersections->setCount(count);
228         paint.getTextIntercepts(test, len, x, y, bounds, intersections->begin());
229     }
230 }
231 
232 DEF_SIMPLE_GM(fancyunderline, canvas, 900, 1350) {
233     SkPaint paint;
234     paint.setAntiAlias(true);
235     const char* fam[] = { "sans-serif", "serif", "monospace" };
236     const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
237     SkPoint textPt = { 10, 80 };
238     for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
239         sk_tool_utils::set_portable_typeface(&paint, fam[font]);
240         for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
241             paint.setTextSize(textSize);
242             const SkScalar uWidth = textSize / 15;
243             paint.setStrokeWidth(uWidth);
244             paint.setStyle(SkPaint::kFill_Style);
245             canvas->drawText(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint);
246 
247             SkTDArray<SkScalar> intersections;
248             find_intercepts(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint, uWidth,
249                 &intersections);
250 
251             SkScalar start = textPt.fX;
252             SkScalar end = paint.measureText(test, sizeof(test) - 1) + textPt.fX;
253             SkScalar uPos = textPt.fY + uWidth;
254             SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
255             paint.setStyle(SkPaint::kStroke_Style);
256             canvas->drawPath(underline, paint);
257 
258             canvas->translate(0, textSize * 1.3f);
259         }
260         canvas->translate(0, 60);
261     }
262 }
263 
find_intercepts(const char * test,size_t len,const SkPoint * pos,const SkPaint & paint,SkScalar uWidth,SkTDArray<SkScalar> * intersections)264 static void find_intercepts(const char* test, size_t len, const SkPoint* pos, const SkPaint& paint,
265         SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
266     SkScalar uPos = pos[0].fY + uWidth;
267     SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
268     int count = paint.getPosTextIntercepts(test, len, pos, bounds, nullptr);
269     SkASSERT(!(count % 2));
270     if (count) {
271         intersections->setCount(count);
272         paint.getPosTextIntercepts(test, len, pos, bounds, intersections->begin());
273     }
274 }
275 
276 DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) {
277     SkPaint paint;
278     paint.setAntiAlias(true);
279     const char* fam[] = { "sans-serif", "serif", "monospace" };
280     const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
281     SkPoint textPt = { 10, 80 };
282     for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
283         sk_tool_utils::set_portable_typeface(&paint, fam[font]);
284         for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
285             paint.setTextSize(textSize);
286             const SkScalar uWidth = textSize / 15;
287             paint.setStrokeWidth(uWidth);
288             paint.setStyle(SkPaint::kFill_Style);
289             int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
290             SkTDArray<SkScalar> widths;
291             widths.setCount(widthCount);
292             (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
293             SkTDArray<SkPoint> pos;
294             pos.setCount(widthCount);
295             SkScalar posX = textPt.fX;
296             for (int index = 0; index < widthCount; ++index) {
297                 pos[index].fX = posX;
298                 posX += widths[index];
299                 pos[index].fY = textPt.fY + (textSize / 25) * (index % 4);
300             }
301             canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
302 
303             SkTDArray<SkScalar> intersections;
304             find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
305 
306             SkScalar start = textPt.fX;
307             SkScalar end = posX;
308             SkScalar uPos = textPt.fY + uWidth;
309             SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
310             paint.setStyle(SkPaint::kStroke_Style);
311             canvas->drawPath(underline, paint);
312 
313             canvas->translate(0, textSize * 1.3f);
314         }
315         canvas->translate(0, 60);
316     }
317 }
318 
319 namespace {
320 
MakeFancyBlob(const SkPaint & paint,const char * text)321 sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) {
322     SkPaint blobPaint(paint);
323 
324     const size_t textLen = strlen(text);
325     const int glyphCount = blobPaint.textToGlyphs(text, textLen, nullptr);
326     SkAutoTArray<SkGlyphID> glyphs(glyphCount);
327     blobPaint.textToGlyphs(text, textLen, glyphs.get());
328 
329     blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
330     const size_t glyphTextBytes = SkTo<uint32_t>(glyphCount) * sizeof(SkGlyphID);
331     const int widthCount = blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, nullptr);
332     SkAssertResult(widthCount == glyphCount);
333 
334     SkAutoTArray<SkScalar> widths(glyphCount);
335     blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, widths.get());
336 
337     SkTextBlobBuilder blobBuilder;
338     int glyphIndex = 0;
339     SkScalar advance = 0;
340 
341     // Default-positioned run.
342     {
343         const int defaultRunLen = glyphCount / 3;
344         const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(blobPaint,
345                                                                        defaultRunLen,
346                                                                        advance, 0);
347         memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID));
348 
349         for (int i = 0; i < defaultRunLen; ++i) {
350             advance += widths[glyphIndex++];
351         }
352     }
353 
354     // Horizontal-positioned run.
355     {
356         const int horizontalRunLen = glyphCount / 3;
357         const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(blobPaint,
358                                                                            horizontalRunLen,
359                                                                            0);
360         memcpy(buf.glyphs, glyphs.get() + glyphIndex,
361                SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID));
362         for (int i = 0; i < horizontalRunLen; ++i) {
363             buf.pos[i] = advance;
364             advance += widths[glyphIndex++];
365         }
366     }
367 
368     // Full-positioned run.
369     {
370         const int fullRunLen = glyphCount - glyphIndex;
371         const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(blobPaint, fullRunLen);
372         memcpy(buf.glyphs, glyphs.get() + glyphIndex,
373                SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID));
374         for (int i = 0; i < fullRunLen; ++i) {
375             buf.pos[i * 2 + 0] = advance; // x offset
376             buf.pos[i * 2 + 1] = 0;       // y offset
377             advance += widths[glyphIndex++];
378         }
379     }
380 
381     return blobBuilder.make();
382 }
383 
384 } // anonymous ns
385 
386 DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) {
387     SkPaint paint;
388     paint.setAntiAlias(true);
389     const char* fam[] = { "sans-serif", "serif", "monospace" };
390     const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
391     const SkPoint blobOffset = { 10, 80 };
392 
393     for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
394         sk_tool_utils::set_portable_typeface(&paint, fam[font]);
395         for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
396             paint.setTextSize(textSize);
397             const SkScalar uWidth = textSize / 15;
398             paint.setStrokeWidth(uWidth);
399             paint.setStyle(SkPaint::kFill_Style);
400 
401             sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test);
402             canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
403 
404             const SkScalar uPos = uWidth;
405             const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
406             const int interceptCount = paint.getTextBlobIntercepts(blob.get(), bounds, nullptr);
407             SkASSERT(!(interceptCount % 2));
408 
409             SkTDArray<SkScalar> intercepts;
410             intercepts.setCount(interceptCount);
411             paint.getTextBlobIntercepts(blob.get(), bounds, intercepts.begin());
412 
413             const SkScalar start = blob->bounds().left();
414             const SkScalar end = blob->bounds().right();
415             SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize);
416             underline.offset(blobOffset.x(), blobOffset.y());
417             paint.setStyle(SkPaint::kStroke_Style);
418             canvas->drawPath(underline, paint);
419 
420             canvas->translate(0, textSize * 1.3f);
421         }
422 
423         canvas->translate(0, 60);
424     }
425 }
426 
427 DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) {
428     SkPaint paint;
429     paint.setAntiAlias(true);
430     const char test[] = " .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_";
431     SkPoint textPt = { 10, 80 };
432     sk_tool_utils::set_portable_typeface(&paint, "serif");
433     for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
434         paint.setTextSize(textSize);
435         SkScalar uWidth = textSize / 15;
436         paint.setStrokeWidth(uWidth);
437         paint.setStyle(SkPaint::kFill_Style);
438         int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
439         SkTDArray<SkScalar> widths;
440         widths.setCount(widthCount);
441         (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
442         SkTDArray<SkPoint> pos;
443         pos.setCount(widthCount);
444         SkScalar posX = textPt.fX;
445         pos[0] = textPt;
446         posX += widths[0];
447         for (int index = 1; index < widthCount; ++index) {
448             pos[index].fX = posX;
449             posX += widths[index];
450             pos[index].fY = textPt.fY - (textSize / 50) * (index / 5) + textSize / 50 * 4;
451         }
452         canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
453 
454         SkTDArray<SkScalar> intersections;
455         find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
456 
457         SkScalar start = textPt.fX;
458         SkScalar end = posX;
459         SkScalar uPos = pos[0].fY + uWidth;
460         SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
461         paint.setStyle(SkPaint::kStroke_Style);
462         canvas->drawPath(underline, paint);
463         canvas->translate(0, textSize * 1.3f);
464     }
465 }
466