epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 7 | |
| 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
| 10 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 12 | #include "SkTextBlob.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkLayerRasterizer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 16 | static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 17 | p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 18 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)))); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 19 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 20 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | p.setMaskFilter(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | p.setStyle(SkPaint::kStroke_Style); |
| 23 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 24 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | p.setAlpha(0x11); |
| 27 | p.setStyle(SkPaint::kFill_Style); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 28 | p.setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 29 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | } |
| 31 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 32 | static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 33 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | p.setAlpha(0x40); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 36 | p.setBlendMode(SkBlendMode::kSrc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | p.setStyle(SkPaint::kStroke_Style); |
| 38 | p.setStrokeWidth(SK_Scalar1*2); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 39 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 42 | static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 44 | p.setStrokeWidth(SK_Scalar1*4); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 45 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | p.setStyle(SkPaint::kStroke_Style); |
| 48 | p.setStrokeWidth(SK_Scalar1*3/2); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 49 | p.setBlendMode(SkBlendMode::kClear); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 50 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 53 | static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | p.setStyle(SkPaint::kStroke_Style); |
| 55 | p.setStrokeWidth(SK_Scalar1*3); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 56 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | p.setAlpha(0x20); |
| 59 | p.setStyle(SkPaint::kFill_Style); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 60 | p.setBlendMode(SkBlendMode::kSrc); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 61 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 64 | static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | p.setAlpha(0x60); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 66 | rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | p.setAlpha(0xFF); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 69 | p.setBlendMode(SkBlendMode::kClear); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 70 | rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 72 | p.setBlendMode(SkBlendMode::kSrcOver); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 73 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | #include "SkDiscretePathEffect.h" |
| 77 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 78 | static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 79 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 81 | p.setPathEffect(SkDiscretePathEffect::Make(SK_Scalar1*4, SK_Scalar1*3)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 82 | p.setBlendMode(SkBlendMode::kSrcOut); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 83 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | } |
| 85 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 86 | static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 87 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | p.setAntiAlias(false); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 90 | SkLayerRasterizer::Builder rastBuilder2; |
| 91 | r5(&rastBuilder2, p); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 92 | p.setRasterizer(rastBuilder2.detach()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 93 | p.setBlendMode(SkBlendMode::kClear); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 94 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | #include "Sk2DPathEffect.h" |
| 98 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 99 | static sk_sp<SkPathEffect> MakeDotEffect(SkScalar radius, const SkMatrix& matrix) { |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 100 | SkPath path; |
| 101 | path.addCircle(0, 0, radius); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 102 | return SkPath2DPathEffect::Make(matrix, path); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 103 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 105 | static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | SkMatrix lattice; |
| 107 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 108 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 109 | p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice)); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 110 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | } |
| 112 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 113 | static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 114 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | SkMatrix lattice; |
| 117 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 118 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 119 | p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 120 | p.setBlendMode(SkBlendMode::kClear); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 121 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | p.setPathEffect(nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 124 | p.setBlendMode(SkBlendMode::kSrcOver); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | p.setStyle(SkPaint::kStroke_Style); |
| 126 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 127 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 130 | static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) { |
| 131 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | SkMatrix lattice; |
| 134 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 135 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 136 | p.setPathEffect(SkLine2DPathEffect::Make(SK_Scalar1*2, lattice)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 137 | p.setBlendMode(SkBlendMode::kClear); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 138 | rastBuilder->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | p.setPathEffect(nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 141 | p.setBlendMode(SkBlendMode::kSrcOver); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | p.setStyle(SkPaint::kStroke_Style); |
| 143 | p.setStrokeWidth(SK_Scalar1); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 144 | rastBuilder->addLayer(p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 147 | typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 149 | constexpr raster_proc gRastProcs[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 151 | }; |
| 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | #include "SkXfermode.h" |
| 154 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 155 | static void apply_shader(SkPaint* paint, int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | raster_proc proc = gRastProcs[index]; |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 157 | if (proc) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | SkPaint p; |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 159 | SkLayerRasterizer::Builder rastBuilder; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | p.setAntiAlias(true); |
commit-bot@chromium.org | f792a1b | 2014-02-26 13:27:37 +0000 | [diff] [blame] | 162 | proc(&rastBuilder, p); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 163 | paint->setRasterizer(rastBuilder.detach()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | #if 0 |
| 167 | SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 168 | paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | #endif |
| 170 | paint->setColor(SK_ColorBLUE); |
| 171 | } |
| 172 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 173 | DEF_SIMPLE_GM(texteffects, canvas, 460, 680) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 175 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 178 | sk_tool_utils::set_portable_typeface(&paint); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 179 | paint.setTextSize(SkIntToScalar(56)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 181 | SkScalar x = SkIntToScalar(20); |
| 182 | SkScalar y = paint.getTextSize(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 184 | SkString str("Hamburgefons"); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 185 | |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 186 | for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | apply_shader(&paint, i); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 188 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | // paint.setMaskFilter(nullptr); |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 190 | // paint.setColor(SK_ColorBLACK); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 191 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | y += paint.getFontSpacing(); |
| 195 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 198 | } |
caryclark | fb56218 | 2015-12-21 08:35:51 -0800 | [diff] [blame] | 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 | } |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 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 }; |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 271 | for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 272 | sk_tool_utils::set_portable_typeface(&paint, fam[font]); |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 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 }; |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 315 | for (size_t font = 0; font < SK_ARRAY_COUNT(fam); ++font) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 316 | sk_tool_utils::set_portable_typeface(&paint, fam[font]); |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 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 | |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 352 | namespace { |
| 353 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 354 | sk_sp<SkTextBlob> MakeFancyBlob(const SkPaint& paint, const char* text) { |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 355 | 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 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 414 | return blobBuilder.make(); |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | } // anonymous ns |
| 418 | |
| 419 | DEF_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 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 434 | sk_sp<SkTextBlob> blob = MakeFancyBlob(paint, test); |
| 435 | canvas->drawTextBlob(blob, blobOffset.x(), blobOffset.y(), paint); |
fmalita | eae6a91 | 2016-07-28 09:47:24 -0700 | [diff] [blame] | 436 | |
| 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 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 460 | DEF_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 | } |