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