Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(495)

Side by Side Diff: gm/texteffects.cpp

Issue 1654883003: add helper to create fancy underlines (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: correct comment to multiply by two Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/core/SkPaint.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkBlurMask.h" 9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h" 10 #include "SkBlurMaskFilter.h"
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 drawText(); 221 drawText();
222 paint.setUnderlineText(true); 222 paint.setUnderlineText(true);
223 drawText(); 223 drawText();
224 paint.setColor(SK_ColorWHITE); 224 paint.setColor(SK_ColorWHITE);
225 paint.setStyle(SkPaint::kStroke_Style); 225 paint.setStyle(SkPaint::kStroke_Style);
226 canvas->drawText("Hello", 5, 100, 50, paint); 226 canvas->drawText("Hello", 5, 100, 50, paint);
227 paint.setColor(SK_ColorBLUE); 227 paint.setColor(SK_ColorBLUE);
228 paint.setStyle(SkPaint::kFill_Style); 228 paint.setStyle(SkPaint::kFill_Style);
229 canvas->drawText("Hello", 5, 100, 50, paint); 229 canvas->drawText("Hello", 5, 100, 50, paint);
230 } 230 }
231
232 static 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
253 static void find_intercepts(const char* test, size_t len, SkScalar x, SkScalar y ,
254 const SkPaint& paint, SkScalar uWidth, SkTDArray<SkScalar>* intersection s) {
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
265 DEF_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 };
271 for (int font = 0; font < 3; ++font) {
272 sk_tool_utils::set_portable_typeface(&paint, fam[font], SkTypeface::kNor mal);
273 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
297 static void find_intercepts(const char* test, size_t len, const SkPoint* pos, co nst 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
309 DEF_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 };
315 for (int font = 0; font < 3; ++font) {
316 sk_tool_utils::set_portable_typeface(&paint, fam[font], SkTypeface::kNor mal);
317 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
352 DEF_SIMPLE_GM(fancyunderlinebars, canvas, 1500, 460) {
353 SkPaint paint;
354 paint.setAntiAlias(true);
355 const char test[] = " .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_ .}]_";
356 SkPoint textPt = { 10, 80 };
357 sk_tool_utils::set_portable_typeface(&paint, "serif");
358 for (SkScalar textSize = 100; textSize > 10; textSize -= 20) {
359 paint.setTextSize(textSize);
360 SkScalar uWidth = textSize / 15;
361 paint.setStrokeWidth(uWidth);
362 paint.setStyle(SkPaint::kFill_Style);
363 int widthCount = paint.getTextWidths(test, sizeof(test) - 1, nullptr);
364 SkTDArray<SkScalar> widths;
365 widths.setCount(widthCount);
366 (void) paint.getTextWidths(test, sizeof(test) - 1, widths.begin());
367 SkTDArray<SkPoint> pos;
368 pos.setCount(widthCount);
369 SkScalar posX = textPt.fX;
370 pos[0] = textPt;
371 posX += widths[0];
372 for (int index = 1; index < widthCount; ++index) {
373 pos[index].fX = posX;
374 posX += widths[index];
375 pos[index].fY = textPt.fY - (textSize / 50) * (index / 5) + textSize / 50 * 4;
376 }
377 canvas->drawPosText(test, sizeof(test) - 1, pos.begin(), paint);
378
379 SkTDArray<SkScalar> intersections;
380 find_intercepts(test, sizeof(test) - 1, pos.begin(), paint, uWidth, &int ersections);
381
382 SkScalar start = textPt.fX;
383 SkScalar end = posX;
384 SkScalar uPos = pos[0].fY + uWidth;
385 SkPath underline = create_underline(intersections, start, end, uPos, uWi dth, textSize);
386 paint.setStyle(SkPaint::kStroke_Style);
387 canvas->drawPath(underline, paint);
388 canvas->translate(0, textSize * 1.3f);
389 }
390 }
OLDNEW
« no previous file with comments | « no previous file | include/core/SkPaint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698