blob: 453a555a6f6bd40abf430aa1184c017b82456eba [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +00007
8#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
fmalitaeae6a912016-07-28 09:47:24 -070012#include "SkTextBlob.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000016static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reedefdfd512016-04-04 10:02:58 -070017 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
18 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000019 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000020
halcanary96fcdcc2015-08-27 07:41:13 -070021 p.setMaskFilter(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 p.setStyle(SkPaint::kStroke_Style);
23 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000024 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 p.setAlpha(0x11);
27 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -070028 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000029 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000030}
31
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000032static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
33 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 p.setAlpha(0x40);
reed374772b2016-10-05 17:33:02 -070036 p.setBlendMode(SkBlendMode::kSrc);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 p.setStyle(SkPaint::kStroke_Style);
38 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000039 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040}
reed@google.com82065d62011-02-07 15:30:46 +000041
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000042static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 p.setStyle(SkPaint::kStrokeAndFill_Style);
44 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000045 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000046
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 p.setStyle(SkPaint::kStroke_Style);
48 p.setStrokeWidth(SK_Scalar1*3/2);
reed374772b2016-10-05 17:33:02 -070049 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000050 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051}
52
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000053static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 p.setStyle(SkPaint::kStroke_Style);
55 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000056 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 p.setAlpha(0x20);
59 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -070060 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000061 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062}
63
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000064static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000066 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 p.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -070069 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000070 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed374772b2016-10-05 17:33:02 -070072 p.setBlendMode(SkBlendMode::kSrcOver);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000073 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000074}
75
76#include "SkDiscretePathEffect.h"
77
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000078static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
79 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reeda4393342016-03-18 11:22:57 -070081 p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
reed374772b2016-10-05 17:33:02 -070082 p.setBlendMode(SkBlendMode::kSrcOut);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000083 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084}
85
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000086static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
87 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000090 SkLayerRasterizer::Builder rastBuilder2;
91 r5(&rastBuilder2, p);
reed7b380d02016-03-21 13:25:16 -070092 p.setRasterizer(rastBuilder2.detach());
reed374772b2016-10-05 17:33:02 -070093 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000094 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095}
96
97#include "Sk2DPathEffect.h"
98
reeda4393342016-03-18 11:22:57 -070099static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
reed@google.com18dc4772011-08-09 18:47:40 +0000100 SkPath path;
101 path.addCircle(0, 0, radius);
reeda4393342016-03-18 11:22:57 -0700102 return SkPath2DPathEffect::Make(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000103}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000105static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 SkMatrix lattice;
107 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
108 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700109 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000110 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111}
112
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000113static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
114 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 SkMatrix lattice;
117 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
118 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700119 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700120 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000121 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000122
halcanary96fcdcc2015-08-27 07:41:13 -0700123 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700124 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125 p.setStyle(SkPaint::kStroke_Style);
126 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000127 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128}
129
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000130static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
131 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 SkMatrix lattice;
134 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
135 lattice.postRotate(SkIntToScalar(30), 0, 0);
reeda4393342016-03-18 11:22:57 -0700136 p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700137 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000138 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
halcanary96fcdcc2015-08-27 07:41:13 -0700140 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700141 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 p.setStyle(SkPaint::kStroke_Style);
143 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000144 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145}
146
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000147typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148
mtkleindbfd7ab2016-09-01 11:24:54 -0700149constexpr raster_proc gRastProcs[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
151};
152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153#include "SkXfermode.h"
154
reed@google.comf2183392011-04-22 14:10:48 +0000155static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 raster_proc proc = gRastProcs[index];
reed7b380d02016-03-21 13:25:16 -0700157 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000159 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000162 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700163 paint->setRasterizer(rastBuilder.detach());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166#if 0
167 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000168 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169#endif
170 paint->setColor(SK_ColorBLUE);
171}
172
halcanary2a243382015-09-09 08:16:41 -0700173DEF_SIMPLE_GM(texteffects, canvas, 460, 680) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700178 sk_tool_utils::set_portable_typeface(&paint);
reed@android.com0bb6d062010-05-17 14:50:04 +0000179 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
reed@android.com0bb6d062010-05-17 14:50:04 +0000181 SkScalar x = SkIntToScalar(20);
182 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000184 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000186 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
halcanary96fcdcc2015-08-27 07:41:13 -0700189 // paint.setMaskFilter(nullptr);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000190 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 canvas->drawText(str.c_str(), str.size(), x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 y += paint.getFontSpacing();
195 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700198}
caryclarkfb562182015-12-21 08:35:51 -0800199
200DEF_SIMPLE_GM(textunderstrike, canvas, 460, 680) {
201 canvas->clear(SK_ColorYELLOW);
202 SkPaint paint;
203 sk_tool_utils::set_portable_typeface(&paint);
204 paint.setTextSize(50);
205 paint.setStrokeWidth(5);
206 paint.setAntiAlias(true);
207
208 auto drawText = [&]() {
209 paint.setStyle(SkPaint::kFill_Style);
210 canvas->drawText("Hello", 5, 100, 50, paint);
211 paint.setStyle(SkPaint::kStroke_Style);
212 canvas->drawText("Hello", 5, 100, 100, paint);
213 canvas->translate(0, 100);
214 };
215
216 drawText();
217 paint.setUnderlineText(true);
218 drawText();
219 paint.setUnderlineText(false);
220 paint.setStrikeThruText(true);
221 drawText();
222 paint.setUnderlineText(true);
223 drawText();
224 paint.setColor(SK_ColorWHITE);
225 paint.setStyle(SkPaint::kStroke_Style);
226 canvas->drawText("Hello", 5, 100, 50, paint);
227 paint.setColor(SK_ColorBLUE);
228 paint.setStyle(SkPaint::kFill_Style);
229 canvas->drawText("Hello", 5, 100, 50, paint);
230}
caryclark0449bcf2016-02-09 13:25:45 -0800231
232static SkPath create_underline(const SkTDArray<SkScalar>& intersections,
233 SkScalar last, SkScalar finalPos,
234 SkScalar uPos, SkScalar uWidth, SkScalar textSize) {
235 SkPath underline;
236 SkScalar end = last;
237 for (int index = 0; index < intersections.count(); index += 2) {
238 SkScalar start = intersections[index] - uWidth;;
239 end = intersections[index + 1] + uWidth;
240 if (start > last && last + textSize / 12 < start) {
241 underline.moveTo(last, uPos);
242 underline.lineTo(start, uPos);
243 }
244 last = end;
245 }
246 if (end < finalPos) {
247 underline.moveTo(end, uPos);
248 underline.lineTo(finalPos, uPos);
249 }
250 return underline;
251}
252
253static void find_intercepts(const char* test, size_t len, SkScalar x, SkScalar y,
254 const SkPaint& paint, SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
255 SkScalar uPos = y + uWidth;
256 SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
257 int count = paint.getTextIntercepts(test, len, x, y, bounds, nullptr);
258 SkASSERT(!(count % 2));
259 if (count) {
260 intersections->setCount(count);
261 paint.getTextIntercepts(test, len, x, y, bounds, intersections->begin());
262 }
263}
264
265DEF_SIMPLE_GM(fancyunderline, canvas, 900, 1350) {
266 SkPaint paint;
267 paint.setAntiAlias(true);
268 const char* fam[] = { "sans-serif", "serif", "monospace" };
269 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
270 SkPoint textPt = { 10, 80 };
fmalitaeae6a912016-07-28 09:47:24 -0700271 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700272 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800273 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
274 paint.setTextSize(textSize);
275 const SkScalar uWidth = textSize / 15;
276 paint.setStrokeWidth(uWidth);
277 paint.setStyle(SkPaint::kFill_Style);
278 canvas->drawText(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint);
279
280 SkTDArray<SkScalar> intersections;
281 find_intercepts(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint, uWidth,
282 &intersections);
283
284 SkScalar start = textPt.fX;
285 SkScalar end = paint.measureText(test, sizeof(test) - 1) + textPt.fX;
286 SkScalar uPos = textPt.fY + uWidth;
287 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
288 paint.setStyle(SkPaint::kStroke_Style);
289 canvas->drawPath(underline, paint);
290
291 canvas->translate(0, textSize * 1.3f);
292 }
293 canvas->translate(0, 60);
294 }
295}
296
297static void find_intercepts(const char* test, size_t len, const SkPoint* pos, const SkPaint& paint,
298 SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
299 SkScalar uPos = pos[0].fY + uWidth;
300 SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
301 int count = paint.getPosTextIntercepts(test, len, pos, bounds, nullptr);
302 SkASSERT(!(count % 2));
303 if (count) {
304 intersections->setCount(count);
305 paint.getPosTextIntercepts(test, len, pos, bounds, intersections->begin());
306 }
307}
308
309DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) {
310 SkPaint paint;
311 paint.setAntiAlias(true);
312 const char* fam[] = { "sans-serif", "serif", "monospace" };
313 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
314 SkPoint textPt = { 10, 80 };
fmalitaeae6a912016-07-28 09:47:24 -0700315 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700316 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800317 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
318 paint.setTextSize(textSize);
319 const SkScalar uWidth = textSize / 15;
320 paint.setStrokeWidth(uWidth);
321 paint.setStyle(SkPaint::kFill_Style);
322 int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
323 SkTDArray<SkScalar> widths;
324 widths.setCount(widthCount);
325 (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
326 SkTDArray<SkPoint> pos;
327 pos.setCount(widthCount);
328 SkScalar posX = textPt.fX;
329 for (int index = 0; index < widthCount; ++index) {
330 pos[index].fX = posX;
331 posX += widths[index];
332 pos[index].fY = textPt.fY + (textSize / 25) * (index % 4);
333 }
334 canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
335
336 SkTDArray<SkScalar> intersections;
337 find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
338
339 SkScalar start = textPt.fX;
340 SkScalar end = posX;
341 SkScalar uPos = textPt.fY + uWidth;
342 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
343 paint.setStyle(SkPaint::kStroke_Style);
344 canvas->drawPath(underline, paint);
345
346 canvas->translate(0, textSize * 1.3f);
347 }
348 canvas->translate(0, 60);
349 }
350}
351
fmalitaeae6a912016-07-28 09:47:24 -0700352namespace {
353
fmalita37283c22016-09-13 10:00:23 -0700354sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) {
fmalitaeae6a912016-07-28 09:47:24 -0700355 SkPaint blobPaint(paint);
356
357 const size_t textLen = strlen(text);
358 const int glyphCount = blobPaint.textToGlyphs(text, textLen, nullptr);
359 SkAutoTArray<SkGlyphID> glyphs(glyphCount);
360 blobPaint.textToGlyphs(text, textLen, glyphs.get());
361
362 blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
363 const size_t glyphTextBytes = SkTo<uint32_t>(glyphCount) * sizeof(SkGlyphID);
364 const int widthCount = blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, nullptr);
365 SkAssertResult(widthCount == glyphCount);
366
367 SkAutoTArray<SkScalar> widths(glyphCount);
368 blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, widths.get());
369
370 SkTextBlobBuilder blobBuilder;
371 int glyphIndex = 0;
372 SkScalar advance = 0;
373
374 // Default-positioned run.
375 {
376 const int defaultRunLen = glyphCount / 3;
377 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(blobPaint,
378 defaultRunLen,
379 advance, 0);
380 memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID));
381
382 for (int i = 0; i < defaultRunLen; ++i) {
383 advance += widths[glyphIndex++];
384 }
385 }
386
387 // Horizontal-positioned run.
388 {
389 const int horizontalRunLen = glyphCount / 3;
390 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(blobPaint,
391 horizontalRunLen,
392 0);
393 memcpy(buf.glyphs, glyphs.get() + glyphIndex,
394 SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID));
395 for (int i = 0; i < horizontalRunLen; ++i) {
396 buf.pos[i] = advance;
397 advance += widths[glyphIndex++];
398 }
399 }
400
401 // Full-positioned run.
402 {
403 const int fullRunLen = glyphCount - glyphIndex;
404 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(blobPaint, fullRunLen);
405 memcpy(buf.glyphs, glyphs.get() + glyphIndex,
406 SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID));
407 for (int i = 0; i < fullRunLen; ++i) {
408 buf.pos[i * 2 + 0] = advance; // x offset
409 buf.pos[i * 2 + 1] = 0; // y offset
410 advance += widths[glyphIndex++];
411 }
412 }
413
fmalita37283c22016-09-13 10:00:23 -0700414 return blobBuilder.make();
fmalitaeae6a912016-07-28 09:47:24 -0700415}
416
417} // anonymous ns
418
419DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) {
420 SkPaint paint;
421 paint.setAntiAlias(true);
422 const char* fam[] = { "sans-serif", "serif", "monospace" };
423 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
424 const SkPoint blobOffset = { 10, 80 };
425
426 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
427 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
428 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
429 paint.setTextSize(textSize);
430 const SkScalar uWidth = textSize / 15;
431 paint.setStrokeWidth(uWidth);
432 paint.setStyle(SkPaint::kFill_Style);
433
fmalita37283c22016-09-13 10:00:23 -0700434 sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test);
435 canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
fmalitaeae6a912016-07-28 09:47:24 -0700436
437 const SkScalar uPos = uWidth;
438 const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
439 const int interceptCount = paint.getTextBlobIntercepts(blob.get(), bounds, nullptr);
440 SkASSERT(!(interceptCount % 2));
441
442 SkTDArray<SkScalar> intercepts;
443 intercepts.setCount(interceptCount);
444 paint.getTextBlobIntercepts(blob.get(), bounds, intercepts.begin());
445
446 const SkScalar start = blob->bounds().left();
447 const SkScalar end = blob->bounds().right();
448 SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize);
449 underline.offset(blobOffset.x(), blobOffset.y());
450 paint.setStyle(SkPaint::kStroke_Style);
451 canvas->drawPath(underline, paint);
452
453 canvas->translate(0, textSize * 1.3f);
454 }
455
456 canvas->translate(0, 60);
457 }
458}
459
caryclark0449bcf2016-02-09 13:25:45 -0800460DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) {
461 SkPaint paint;
462 paint.setAntiAlias(true);
463 const char test[] = " .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_";
464 SkPoint textPt = { 10, 80 };
465 sk_tool_utils::set_portable_typeface(&paint, "serif");
466 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
467 paint.setTextSize(textSize);
468 SkScalar uWidth = textSize / 15;
469 paint.setStrokeWidth(uWidth);
470 paint.setStyle(SkPaint::kFill_Style);
471 int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
472 SkTDArray<SkScalar> widths;
473 widths.setCount(widthCount);
474 (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
475 SkTDArray<SkPoint> pos;
476 pos.setCount(widthCount);
477 SkScalar posX = textPt.fX;
478 pos[0] = textPt;
479 posX += widths[0];
480 for (int index = 1; index < widthCount; ++index) {
481 pos[index].fX = posX;
482 posX += widths[index];
483 pos[index].fY = textPt.fY - (textSize / 50) * (index / 5) + textSize / 50 * 4;
484 }
485 canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
486
487 SkTDArray<SkScalar> intersections;
488 find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
489
490 SkScalar start = textPt.fX;
491 SkScalar end = posX;
492 SkScalar uPos = pos[0].fY + uWidth;
493 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
494 paint.setStyle(SkPaint::kStroke_Style);
495 canvas->drawPath(underline, paint);
496 canvas->translate(0, textSize * 1.3f);
497 }
498}