blob: e74d7a97f5354271c53cfaf24023585066484c82 [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@google.comf2183392011-04-22 14:10:48 +0000153static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 raster_proc proc = gRastProcs[index];
reed7b380d02016-03-21 13:25:16 -0700155 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000157 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000160 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700161 paint->setRasterizer(rastBuilder.detach());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164#if 0
165 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000166 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167#endif
168 paint->setColor(SK_ColorBLUE);
169}
170
halcanary2a243382015-09-09 08:16:41 -0700171DEF_SIMPLE_GM(texteffects, canvas, 460, 680) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700176 sk_tool_utils::set_portable_typeface(&paint);
reed@android.com0bb6d062010-05-17 14:50:04 +0000177 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@android.com0bb6d062010-05-17 14:50:04 +0000179 SkScalar x = SkIntToScalar(20);
180 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000182 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000184 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
halcanary96fcdcc2015-08-27 07:41:13 -0700187 // paint.setMaskFilter(nullptr);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000188 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 canvas->drawText(str.c_str(), str.size(), x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 y += paint.getFontSpacing();
193 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700196}
caryclarkfb562182015-12-21 08:35:51 -0800197
caryclark0449bcf2016-02-09 13:25:45 -0800198static SkPath create_underline(const SkTDArray<SkScalar>& intersections,
199 SkScalar last, SkScalar finalPos,
200 SkScalar uPos, SkScalar uWidth, SkScalar textSize) {
201 SkPath underline;
202 SkScalar end = last;
203 for (int index = 0; index < intersections.count(); index += 2) {
204 SkScalar start = intersections[index] - uWidth;;
205 end = intersections[index + 1] + uWidth;
206 if (start > last && last + textSize / 12 < start) {
207 underline.moveTo(last, uPos);
208 underline.lineTo(start, uPos);
209 }
210 last = end;
211 }
212 if (end < finalPos) {
213 underline.moveTo(end, uPos);
214 underline.lineTo(finalPos, uPos);
215 }
216 return underline;
217}
218
219static void find_intercepts(const char* test, size_t len, SkScalar x, SkScalar y,
220 const SkPaint& paint, SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
221 SkScalar uPos = y + uWidth;
222 SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
223 int count = paint.getTextIntercepts(test, len, x, y, bounds, nullptr);
224 SkASSERT(!(count % 2));
225 if (count) {
226 intersections->setCount(count);
227 paint.getTextIntercepts(test, len, x, y, bounds, intersections->begin());
228 }
229}
230
231DEF_SIMPLE_GM(fancyunderline, canvas, 900, 1350) {
232 SkPaint paint;
233 paint.setAntiAlias(true);
234 const char* fam[] = { "sans-serif", "serif", "monospace" };
235 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
236 SkPoint textPt = { 10, 80 };
fmalitaeae6a912016-07-28 09:47:24 -0700237 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700238 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800239 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
240 paint.setTextSize(textSize);
241 const SkScalar uWidth = textSize / 15;
242 paint.setStrokeWidth(uWidth);
243 paint.setStyle(SkPaint::kFill_Style);
244 canvas->drawText(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint);
245
246 SkTDArray<SkScalar> intersections;
247 find_intercepts(test, sizeof(test) - 1, textPt.fX, textPt.fY, paint, uWidth,
248 &intersections);
249
250 SkScalar start = textPt.fX;
251 SkScalar end = paint.measureText(test, sizeof(test) - 1) + textPt.fX;
252 SkScalar uPos = textPt.fY + uWidth;
253 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
254 paint.setStyle(SkPaint::kStroke_Style);
255 canvas->drawPath(underline, paint);
256
257 canvas->translate(0, textSize * 1.3f);
258 }
259 canvas->translate(0, 60);
260 }
261}
262
263static void find_intercepts(const char* test, size_t len, const SkPoint* pos, const SkPaint& paint,
264 SkScalar uWidth, SkTDArray<SkScalar>* intersections) {
265 SkScalar uPos = pos[0].fY + uWidth;
266 SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
267 int count = paint.getPosTextIntercepts(test, len, pos, bounds, nullptr);
268 SkASSERT(!(count % 2));
269 if (count) {
270 intersections->setCount(count);
271 paint.getPosTextIntercepts(test, len, pos, bounds, intersections->begin());
272 }
273}
274
275DEF_SIMPLE_GM(fancyposunderline, canvas, 900, 1350) {
276 SkPaint paint;
277 paint.setAntiAlias(true);
278 const char* fam[] = { "sans-serif", "serif", "monospace" };
279 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
280 SkPoint textPt = { 10, 80 };
fmalitaeae6a912016-07-28 09:47:24 -0700281 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700282 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800283 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
284 paint.setTextSize(textSize);
285 const SkScalar uWidth = textSize / 15;
286 paint.setStrokeWidth(uWidth);
287 paint.setStyle(SkPaint::kFill_Style);
288 int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
289 SkTDArray<SkScalar> widths;
290 widths.setCount(widthCount);
291 (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
292 SkTDArray<SkPoint> pos;
293 pos.setCount(widthCount);
294 SkScalar posX = textPt.fX;
295 for (int index = 0; index < widthCount; ++index) {
296 pos[index].fX = posX;
297 posX += widths[index];
298 pos[index].fY = textPt.fY + (textSize / 25) * (index % 4);
299 }
300 canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
301
302 SkTDArray<SkScalar> intersections;
303 find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
304
305 SkScalar start = textPt.fX;
306 SkScalar end = posX;
307 SkScalar uPos = textPt.fY + uWidth;
308 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
309 paint.setStyle(SkPaint::kStroke_Style);
310 canvas->drawPath(underline, paint);
311
312 canvas->translate(0, textSize * 1.3f);
313 }
314 canvas->translate(0, 60);
315 }
316}
317
fmalitaeae6a912016-07-28 09:47:24 -0700318namespace {
319
fmalita37283c22016-09-13 10:00:23 -0700320sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) {
fmalitaeae6a912016-07-28 09:47:24 -0700321 SkPaint blobPaint(paint);
322
323 const size_t textLen = strlen(text);
324 const int glyphCount = blobPaint.textToGlyphs(text, textLen, nullptr);
325 SkAutoTArray<SkGlyphID> glyphs(glyphCount);
326 blobPaint.textToGlyphs(text, textLen, glyphs.get());
327
328 blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
329 const size_t glyphTextBytes = SkTo<uint32_t>(glyphCount) * sizeof(SkGlyphID);
330 const int widthCount = blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, nullptr);
331 SkAssertResult(widthCount == glyphCount);
332
333 SkAutoTArray<SkScalar> widths(glyphCount);
334 blobPaint.getTextWidths(glyphs.get(), glyphTextBytes, widths.get());
335
336 SkTextBlobBuilder blobBuilder;
337 int glyphIndex = 0;
338 SkScalar advance = 0;
339
340 // Default-positioned run.
341 {
342 const int defaultRunLen = glyphCount / 3;
343 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRun(blobPaint,
344 defaultRunLen,
345 advance, 0);
346 memcpy(buf.glyphs, glyphs.get(), SkTo<uint32_t>(defaultRunLen) * sizeof(SkGlyphID));
347
348 for (int i = 0; i < defaultRunLen; ++i) {
349 advance += widths[glyphIndex++];
350 }
351 }
352
353 // Horizontal-positioned run.
354 {
355 const int horizontalRunLen = glyphCount / 3;
356 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPosH(blobPaint,
357 horizontalRunLen,
358 0);
359 memcpy(buf.glyphs, glyphs.get() + glyphIndex,
360 SkTo<uint32_t>(horizontalRunLen) * sizeof(SkGlyphID));
361 for (int i = 0; i < horizontalRunLen; ++i) {
362 buf.pos[i] = advance;
363 advance += widths[glyphIndex++];
364 }
365 }
366
367 // Full-positioned run.
368 {
369 const int fullRunLen = glyphCount - glyphIndex;
370 const SkTextBlobBuilder::RunBuffer& buf = blobBuilder.allocRunPos(blobPaint, fullRunLen);
371 memcpy(buf.glyphs, glyphs.get() + glyphIndex,
372 SkTo<uint32_t>(fullRunLen) * sizeof(SkGlyphID));
373 for (int i = 0; i < fullRunLen; ++i) {
374 buf.pos[i * 2 + 0] = advance; // x offset
375 buf.pos[i * 2 + 1] = 0; // y offset
376 advance += widths[glyphIndex++];
377 }
378 }
379
fmalita37283c22016-09-13 10:00:23 -0700380 return blobBuilder.make();
fmalitaeae6a912016-07-28 09:47:24 -0700381}
382
383} // anonymous ns
384
385DEF_SIMPLE_GM(fancyblobunderline, canvas, 1480, 1380) {
386 SkPaint paint;
387 paint.setAntiAlias(true);
388 const char* fam[] = { "sans-serif", "serif", "monospace" };
389 const char test[] = "aAjJgGyY_|{-(~[,]qQ}pP}zZ";
390 const SkPoint blobOffset = { 10, 80 };
391
392 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
393 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
394 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
395 paint.setTextSize(textSize);
396 const SkScalar uWidth = textSize / 15;
397 paint.setStrokeWidth(uWidth);
398 paint.setStyle(SkPaint::kFill_Style);
399
fmalita37283c22016-09-13 10:00:23 -0700400 sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test);
401 canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
fmalitaeae6a912016-07-28 09:47:24 -0700402
403 const SkScalar uPos = uWidth;
404 const SkScalar bounds[2] = { uPos - uWidth / 2, uPos + uWidth / 2 };
405 const int interceptCount = paint.getTextBlobIntercepts(blob.get(), bounds, nullptr);
406 SkASSERT(!(interceptCount % 2));
407
408 SkTDArray<SkScalar> intercepts;
409 intercepts.setCount(interceptCount);
410 paint.getTextBlobIntercepts(blob.get(), bounds, intercepts.begin());
411
412 const SkScalar start = blob->bounds().left();
413 const SkScalar end = blob->bounds().right();
414 SkPath underline = create_underline(intercepts, start, end, uPos, uWidth, textSize);
415 underline.offset(blobOffset.x(), blobOffset.y());
416 paint.setStyle(SkPaint::kStroke_Style);
417 canvas->drawPath(underline, paint);
418
419 canvas->translate(0, textSize * 1.3f);
420 }
421
422 canvas->translate(0, 60);
423 }
424}
425
caryclark0449bcf2016-02-09 13:25:45 -0800426DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) {
427 SkPaint paint;
428 paint.setAntiAlias(true);
429 const char test[] = " .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_";
430 SkPoint textPt = { 10, 80 };
431 sk_tool_utils::set_portable_typeface(&paint, "serif");
432 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
433 paint.setTextSize(textSize);
434 SkScalar uWidth = textSize / 15;
435 paint.setStrokeWidth(uWidth);
436 paint.setStyle(SkPaint::kFill_Style);
437 int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
438 SkTDArray<SkScalar> widths;
439 widths.setCount(widthCount);
440 (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
441 SkTDArray<SkPoint> pos;
442 pos.setCount(widthCount);
443 SkScalar posX = textPt.fX;
444 pos[0] = textPt;
445 posX += widths[0];
446 for (int index = 1; index < widthCount; ++index) {
447 pos[index].fX = posX;
448 posX += widths[index];
449 pos[index].fY = textPt.fY - (textSize / 50) * (index / 5) + textSize / 50 * 4;
450 }
451 canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
452
453 SkTDArray<SkScalar> intersections;
454 find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &intersections);
455
456 SkScalar start = textPt.fX;
457 SkScalar end = posX;
458 SkScalar uPos = pos[0].fY + uWidth;
459 SkPath underline = create_underline(intersections, start, end, uPos, uWidth, textSize);
460 paint.setStyle(SkPaint::kStroke_Style);
461 canvas->drawPath(underline, paint);
462 canvas->translate(0, textSize * 1.3f);
463 }
464}