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 "SkBlurMask.h" 10 #include "SkBlurMaskFilter.h" 11 #include "SkReadBuffer.h" 12 #include "SkWriteBuffer.h" 13 #include "SkLayerRasterizer.h" 14 15 static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 16 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 17 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))))->unref(); 18 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); 19 20 p.setMaskFilter(nullptr); 21 p.setStyle(SkPaint::kStroke_Style); 22 p.setStrokeWidth(SK_Scalar1); 23 rastBuilder->addLayer(p); 24 25 p.setAlpha(0x11); 26 p.setStyle(SkPaint::kFill_Style); 27 p.setXfermodeMode(SkXfermode::kSrc_Mode); 28 rastBuilder->addLayer(p); 29 } 30 31 static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 32 rastBuilder->addLayer(p); 33 34 p.setAlpha(0x40); 35 p.setXfermodeMode(SkXfermode::kSrc_Mode); 36 p.setStyle(SkPaint::kStroke_Style); 37 p.setStrokeWidth(SK_Scalar1*2); 38 rastBuilder->addLayer(p); 39 } 40 41 static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 42 p.setStyle(SkPaint::kStrokeAndFill_Style); 43 p.setStrokeWidth(SK_Scalar1*4); 44 rastBuilder->addLayer(p); 45 46 p.setStyle(SkPaint::kStroke_Style); 47 p.setStrokeWidth(SK_Scalar1*3/2); 48 p.setXfermodeMode(SkXfermode::kClear_Mode); 49 rastBuilder->addLayer(p); 50 } 51 52 static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 53 p.setStyle(SkPaint::kStroke_Style); 54 p.setStrokeWidth(SK_Scalar1*3); 55 rastBuilder->addLayer(p); 56 57 p.setAlpha(0x20); 58 p.setStyle(SkPaint::kFill_Style); 59 p.setXfermodeMode(SkXfermode::kSrc_Mode); 60 rastBuilder->addLayer(p); 61 } 62 63 static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 64 p.setAlpha(0x60); 65 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); 66 67 p.setAlpha(0xFF); 68 p.setXfermodeMode(SkXfermode::kClear_Mode); 69 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); 70 71 p.setXfermode(nullptr); 72 rastBuilder->addLayer(p); 73 } 74 75 #include "SkDiscretePathEffect.h" 76 77 static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 78 rastBuilder->addLayer(p); 79 80 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref(); 81 p.setXfermodeMode(SkXfermode::kSrcOut_Mode); 82 rastBuilder->addLayer(p); 83 } 84 85 static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 86 rastBuilder->addLayer(p); 87 88 p.setAntiAlias(false); 89 SkLayerRasterizer::Builder rastBuilder2; 90 r5(&rastBuilder2, p); 91 p.setRasterizer(rastBuilder2.detachRasterizer())->unref(); 92 p.setXfermodeMode(SkXfermode::kClear_Mode); 93 rastBuilder->addLayer(p); 94 } 95 96 #include "Sk2DPathEffect.h" 97 98 static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) { 99 SkPath path; 100 path.addCircle(0, 0, radius); 101 return SkPath2DPathEffect::Create(matrix, path); 102 } 103 104 static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 105 SkMatrix lattice; 106 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); 107 lattice.postSkew(SK_Scalar1/3, 0, 0, 0); 108 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref(); 109 rastBuilder->addLayer(p); 110 } 111 112 static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 113 rastBuilder->addLayer(p); 114 115 SkMatrix lattice; 116 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); 117 lattice.postSkew(SK_Scalar1/3, 0, 0, 0); 118 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref(); 119 p.setXfermodeMode(SkXfermode::kClear_Mode); 120 rastBuilder->addLayer(p); 121 122 p.setPathEffect(nullptr); 123 p.setXfermode(nullptr); 124 p.setStyle(SkPaint::kStroke_Style); 125 p.setStrokeWidth(SK_Scalar1); 126 rastBuilder->addLayer(p); 127 } 128 129 static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { 130 rastBuilder->addLayer(p); 131 132 SkMatrix lattice; 133 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); 134 lattice.postRotate(SkIntToScalar(30), 0, 0); 135 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref(); 136 p.setXfermodeMode(SkXfermode::kClear_Mode); 137 rastBuilder->addLayer(p); 138 139 p.setPathEffect(nullptr); 140 p.setXfermode(nullptr); 141 p.setStyle(SkPaint::kStroke_Style); 142 p.setStrokeWidth(SK_Scalar1); 143 rastBuilder->addLayer(p); 144 } 145 146 typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&); 147 148 static const raster_proc gRastProcs[] = { 149 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 150 }; 151 152 #include "SkXfermode.h" 153 154 static void apply_shader(SkPaint* paint, int index) { 155 raster_proc proc = gRastProcs[index]; 156 if (proc) 157 { 158 SkPaint p; 159 SkLayerRasterizer::Builder rastBuilder; 160 161 p.setAntiAlias(true); 162 proc(&rastBuilder, p); 163 paint->setRasterizer(rastBuilder.detachRasterizer())->unref(); 164 } 165 166 #if 0 167 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; 168 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref(); 169 #endif 170 paint->setColor(SK_ColorBLUE); 171 } 172 173 DEF_SIMPLE_GM(texteffects, canvas, 460, 680) { 174 canvas->save(); 175 176 SkPaint paint; 177 paint.setAntiAlias(true); 178 sk_tool_utils::set_portable_typeface(&paint); 179 paint.setTextSize(SkIntToScalar(56)); 180 181 SkScalar x = SkIntToScalar(20); 182 SkScalar y = paint.getTextSize(); 183 184 SkString str("Hamburgefons"); 185 186 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) { 187 apply_shader(&paint, i); 188 189 // paint.setMaskFilter(nullptr); 190 // paint.setColor(SK_ColorBLACK); 191 192 canvas->drawText(str.c_str(), str.size(), x, y, paint); 193 194 y += paint.getFontSpacing(); 195 } 196 197 canvas->restore(); 198 } 199 200 DEF_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 } 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>* 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 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::kNormal); 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, 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 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::kNormal); 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, &intersections); 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, uWidth, textSize); 386 paint.setStyle(SkPaint::kStroke_Style); 387 canvas->drawPath(underline, paint); 388 canvas->translate(0, textSize * 1.3f); 389 } 390 } 391