blob: b98aea714e77e7e7a09683f0e3dc57455f72e11c [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
11#include "SkBlurMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
fmalitaeae6a912016-07-28 09:47:24 -070013#include "SkTextBlob.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000014#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000017static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reedefdfd512016-04-04 10:02:58 -070018 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
19 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000020 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000021
halcanary96fcdcc2015-08-27 07:41:13 -070022 p.setMaskFilter(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 p.setStyle(SkPaint::kStroke_Style);
24 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000025 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 p.setAlpha(0x11);
28 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -070029 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000030 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000031}
32
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000033static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
34 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 p.setAlpha(0x40);
reed374772b2016-10-05 17:33:02 -070037 p.setBlendMode(SkBlendMode::kSrc);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 p.setStyle(SkPaint::kStroke_Style);
39 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000040 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041}
reed@google.com82065d62011-02-07 15:30:46 +000042
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000043static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 p.setStyle(SkPaint::kStrokeAndFill_Style);
45 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000046 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 p.setStyle(SkPaint::kStroke_Style);
49 p.setStrokeWidth(SK_Scalar1*3/2);
reed374772b2016-10-05 17:33:02 -070050 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000051 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000052}
53
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000054static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 p.setStyle(SkPaint::kStroke_Style);
56 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000057 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 p.setAlpha(0x20);
60 p.setStyle(SkPaint::kFill_Style);
reed374772b2016-10-05 17:33:02 -070061 p.setBlendMode(SkBlendMode::kSrc);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000062 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063}
64
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000065static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000067 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 p.setAlpha(0xFF);
reed374772b2016-10-05 17:33:02 -070070 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000071 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed374772b2016-10-05 17:33:02 -070073 p.setBlendMode(SkBlendMode::kSrcOver);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000074 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000075}
76
77#include "SkDiscretePathEffect.h"
78
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000079static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
80 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reeda4393342016-03-18 11:22:57 -070082 p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3));
reed374772b2016-10-05 17:33:02 -070083 p.setBlendMode(SkBlendMode::kSrcOut);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000084 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085}
86
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000087static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
88 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000091 SkLayerRasterizer::Builder rastBuilder2;
92 r5(&rastBuilder2, p);
reed7b380d02016-03-21 13:25:16 -070093 p.setRasterizer(rastBuilder2.detach());
reed374772b2016-10-05 17:33:02 -070094 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000095 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096}
97
98#include "Sk2DPathEffect.h"
99
reeda4393342016-03-18 11:22:57 -0700100static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
reed@google.com18dc4772011-08-09 18:47:40 +0000101 SkPath path;
102 path.addCircle(0, 0, radius);
reeda4393342016-03-18 11:22:57 -0700103 return SkPath2DPathEffect::Make(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000104}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000106static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 SkMatrix lattice;
108 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
109 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700110 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice));
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000111 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112}
113
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000114static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
115 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 SkMatrix lattice;
118 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
119 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reeda4393342016-03-18 11:22:57 -0700120 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700121 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000122 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000123
halcanary96fcdcc2015-08-27 07:41:13 -0700124 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700125 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 p.setStyle(SkPaint::kStroke_Style);
127 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000128 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129}
130
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000131static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
132 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 SkMatrix lattice;
135 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
136 lattice.postRotate(SkIntToScalar(30), 0, 0);
reeda4393342016-03-18 11:22:57 -0700137 p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice));
reed374772b2016-10-05 17:33:02 -0700138 p.setBlendMode(SkBlendMode::kClear);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000139 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
halcanary96fcdcc2015-08-27 07:41:13 -0700141 p.setPathEffect(nullptr);
reed374772b2016-10-05 17:33:02 -0700142 p.setBlendMode(SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 p.setStyle(SkPaint::kStroke_Style);
144 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000145 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146}
147
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000148typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
mtkleindbfd7ab2016-09-01 11:24:54 -0700150constexpr raster_proc gRastProcs[] = {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
152};
153
reed@google.comf2183392011-04-22 14:10:48 +0000154static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 raster_proc proc = gRastProcs[index];
reed7b380d02016-03-21 13:25:16 -0700156 if (proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000158 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000161 proc(&rastBuilder, p);
reed7b380d02016-03-21 13:25:16 -0700162 paint->setRasterizer(rastBuilder.detach());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165#if 0
166 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000167 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168#endif
169 paint->setColor(SK_ColorBLUE);
170}
171
halcanary2a243382015-09-09 08:16:41 -0700172DEF_SIMPLE_GM(texteffects, canvas, 460, 680) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700177 sk_tool_utils::set_portable_typeface(&paint);
reed@android.com0bb6d062010-05-17 14:50:04 +0000178 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@android.com0bb6d062010-05-17 14:50:04 +0000180 SkScalar x = SkIntToScalar(20);
181 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000183 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000185 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187
halcanary96fcdcc2015-08-27 07:41:13 -0700188 // paint.setMaskFilter(nullptr);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000189 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
Cary Clark2a475ea2017-04-28 15:35:12 -0400191 canvas->drawString(str, x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 y += paint.getFontSpacing();
194 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 canvas->restore();
halcanary2a243382015-09-09 08:16:41 -0700197}
caryclarkfb562182015-12-21 08:35:51 -0800198
caryclark0449bcf2016-02-09 13:25:45 -0800199static 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
220static 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
232DEF_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 };
fmalitaeae6a912016-07-28 09:47:24 -0700238 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700239 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800240 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
264static 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
276DEF_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 };
fmalitaeae6a912016-07-28 09:47:24 -0700282 for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) {
mbocee6a9912016-05-31 11:42:36 -0700283 sk_tool_utils::set_portable_typeface(&paint, fam[font]);
caryclark0449bcf2016-02-09 13:25:45 -0800284 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
fmalitaeae6a912016-07-28 09:47:24 -0700319namespace {
320
fmalita37283c22016-09-13 10:00:23 -0700321sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) {
fmalitaeae6a912016-07-28 09:47:24 -0700322 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
fmalita37283c22016-09-13 10:00:23 -0700381 return blobBuilder.make();
fmalitaeae6a912016-07-28 09:47:24 -0700382}
383
384} // anonymous ns
385
386DEF_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
fmalita37283c22016-09-13 10:00:23 -0700401 sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test);
402 canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint);
fmalitaeae6a912016-07-28 09:47:24 -0700403
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
caryclark0449bcf2016-02-09 13:25:45 -0800427DEF_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}