fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkPaint.h" |
| 9 | #include "SkPoint.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 10 | #include "SkTextBlobRunIterator.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 12 | #include "SkTypeface.h" |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 13 | |
| 14 | #include "Test.h" |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 15 | #include "sk_tool_utils.h" |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 16 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 17 | class TextBlobTester { |
| 18 | public: |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 19 | // This unit test feeds an SkTextBlobBuilder various runs then checks to see if |
| 20 | // the result contains the provided data and merges runs when appropriate. |
| 21 | static void TestBuilder(skiatest::Reporter* reporter) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 22 | SkTextBlobBuilder builder; |
| 23 | |
| 24 | // empty run set |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 26 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 27 | RunDef set1[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 28 | { 128, SkTextBlob::kDefault_Positioning, 100, 100 }, |
| 29 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 30 | RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 31 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 32 | RunDef set2[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 33 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 }, |
| 34 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 35 | RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 36 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 37 | RunDef set3[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 38 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 39 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 40 | RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 41 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 42 | RunDef set4[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 43 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 44 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 45 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 46 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 47 | RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 48 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 49 | RunDef set5[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 50 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 51 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 52 | { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 }, |
| 53 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 54 | RunDef mergedSet5[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 55 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 56 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 }, |
| 57 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 58 | RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5, |
| 59 | SK_ARRAY_COUNT(mergedSet5)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 60 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 61 | RunDef set6[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 62 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 63 | { 128, SkTextBlob::kFull_Positioning, 200, 200 }, |
| 64 | { 128, SkTextBlob::kFull_Positioning, 300, 300 }, |
| 65 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 66 | RunDef mergedSet6[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 67 | { 384, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 68 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 69 | RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6, |
| 70 | SK_ARRAY_COUNT(mergedSet6)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 71 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 72 | RunDef set7[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 73 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 74 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 75 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 76 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 77 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 78 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 79 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 80 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 81 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 }, |
| 82 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 }, |
| 83 | { 128, SkTextBlob::kFull_Positioning, 400, 750 }, |
| 84 | { 128, SkTextBlob::kFull_Positioning, 400, 850 }, |
| 85 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 86 | RunDef mergedSet7[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 87 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 88 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 89 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 90 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 91 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 92 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 93 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 }, |
| 94 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 }, |
| 95 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 96 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 97 | RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7, |
| 98 | SK_ARRAY_COUNT(mergedSet7)); |
| 99 | } |
| 100 | |
| 101 | // This unit test verifies blob bounds computation. |
| 102 | static void TestBounds(skiatest::Reporter* reporter) { |
| 103 | SkTextBlobBuilder builder; |
| 104 | SkPaint font; |
| 105 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 106 | |
| 107 | // Explicit bounds. |
| 108 | { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 109 | sk_sp<SkTextBlob> blob(builder.make()); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 110 | REPORTER_ASSERT(reporter, !blob); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | { |
| 114 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 115 | builder.allocRun(font, 16, 0, 0, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 116 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 118 | } |
| 119 | |
| 120 | { |
| 121 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 122 | builder.allocRunPosH(font, 16, 0, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 123 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 125 | } |
| 126 | |
| 127 | { |
| 128 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 129 | builder.allocRunPos(font, 16, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 130 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 131 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 132 | } |
| 133 | |
| 134 | { |
| 135 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 136 | SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50); |
| 137 | SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5); |
| 138 | |
| 139 | builder.allocRun(font, 16, 0, 0, &r1); |
| 140 | builder.allocRunPosH(font, 16, 0, &r2); |
| 141 | builder.allocRunPos(font, 16, &r3); |
| 142 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 143 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65)); |
| 145 | } |
| 146 | |
| 147 | { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 148 | sk_sp<SkTextBlob> blob(builder.make()); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 149 | REPORTER_ASSERT(reporter, !blob); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Implicit bounds |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 153 | |
| 154 | { |
| 155 | // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers |
| 156 | // don't trigger asserts (http://crbug.com/542643). |
| 157 | SkPaint p; |
| 158 | p.setTextSize(0); |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 159 | p.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 160 | |
| 161 | const char* txt = "BOOO"; |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 162 | const size_t txtLen = strlen(txt); |
| 163 | const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr); |
| 164 | |
| 165 | p.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 166 | const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount); |
| 167 | |
| 168 | p.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 169 | p.textToGlyphs(txt, txtLen, buffer.glyphs); |
| 170 | |
| 171 | memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 172 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); |
| 174 | } |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 175 | } |
| 176 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 177 | // Verify that text-related properties are captured in run paints. |
| 178 | static void TestPaintProps(skiatest::Reporter* reporter) { |
| 179 | SkPaint font; |
| 180 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 181 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 182 | // Kitchen sink font. |
| 183 | font.setTextSize(42); |
| 184 | font.setTextScaleX(4.2f); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 185 | font.setTypeface(SkTypeface::MakeDefault()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 186 | font.setTextSkewX(0.42f); |
| 187 | font.setTextAlign(SkPaint::kCenter_Align); |
| 188 | font.setHinting(SkPaint::kFull_Hinting); |
| 189 | font.setAntiAlias(true); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 190 | font.setFakeBoldText(true); |
| 191 | font.setLinearText(true); |
| 192 | font.setSubpixelText(true); |
| 193 | font.setDevKernText(true); |
| 194 | font.setLCDRenderText(true); |
| 195 | font.setEmbeddedBitmapText(true); |
| 196 | font.setAutohinted(true); |
| 197 | font.setVerticalText(true); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 198 | |
| 199 | // Ensure we didn't pick default values by mistake. |
| 200 | SkPaint defaultPaint; |
| 201 | REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize()); |
| 202 | REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX()); |
| 203 | REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface()); |
| 204 | REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX()); |
| 205 | REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign()); |
| 206 | REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting()); |
| 207 | REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 208 | REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText()); |
| 209 | REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText()); |
| 210 | REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText()); |
| 212 | REPORTER_ASSERT(reporter, defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText()); |
| 213 | REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted()); |
| 214 | REPORTER_ASSERT(reporter, defaultPaint.isVerticalText() != font.isVerticalText()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 215 | |
| 216 | SkTextBlobBuilder builder; |
| 217 | AddRun(font, 1, SkTextBlob::kDefault_Positioning, SkPoint::Make(0, 0), builder); |
| 218 | AddRun(font, 1, SkTextBlob::kHorizontal_Positioning, SkPoint::Make(0, 0), builder); |
| 219 | AddRun(font, 1, SkTextBlob::kFull_Positioning, SkPoint::Make(0, 0), builder); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 220 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 221 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 222 | SkTextBlobRunIterator it(blob.get()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 223 | while (!it.done()) { |
| 224 | SkPaint paint; |
| 225 | it.applyFontToPaint(&paint); |
| 226 | |
| 227 | REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize()); |
| 228 | REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX()); |
| 229 | REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface()); |
| 230 | REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX()); |
| 231 | REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign()); |
| 232 | REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting()); |
| 233 | REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText()); |
| 235 | REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText()); |
| 236 | REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 237 | REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText()); |
| 238 | REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText()); |
| 239 | REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted()); |
| 240 | REPORTER_ASSERT(reporter, paint.isVerticalText() == font.isVerticalText()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 241 | |
| 242 | it.next(); |
| 243 | } |
| 244 | |
| 245 | } |
| 246 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 247 | private: |
| 248 | struct RunDef { |
| 249 | unsigned count; |
| 250 | SkTextBlob::GlyphPositioning pos; |
| 251 | SkScalar x, y; |
| 252 | }; |
| 253 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 254 | static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 255 | const RunDef in[], unsigned inCount, |
| 256 | const RunDef out[], unsigned outCount) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 257 | SkPaint font; |
| 258 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 259 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 260 | unsigned glyphCount = 0; |
| 261 | unsigned posCount = 0; |
| 262 | |
| 263 | for (unsigned i = 0; i < inCount; ++i) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 264 | AddRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 265 | glyphCount += in[i].count; |
| 266 | posCount += in[i].count * in[i].pos; |
| 267 | } |
| 268 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 269 | sk_sp<SkTextBlob> blob(builder.make()); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 270 | REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob)); |
| 271 | if (!blob) { |
| 272 | return; |
| 273 | } |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 274 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 275 | SkTextBlobRunIterator it(blob.get()); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 276 | for (unsigned i = 0; i < outCount; ++i) { |
| 277 | REPORTER_ASSERT(reporter, !it.done()); |
| 278 | REPORTER_ASSERT(reporter, out[i].pos == it.positioning()); |
| 279 | REPORTER_ASSERT(reporter, out[i].count == it.glyphCount()); |
| 280 | if (SkTextBlob::kDefault_Positioning == out[i].pos) { |
| 281 | REPORTER_ASSERT(reporter, out[i].x == it.offset().x()); |
| 282 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 283 | } else if (SkTextBlob::kHorizontal_Positioning == out[i].pos) { |
| 284 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 285 | } |
| 286 | |
| 287 | for (unsigned k = 0; k < it.glyphCount(); ++k) { |
| 288 | REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]); |
| 289 | if (SkTextBlob::kHorizontal_Positioning == it.positioning()) { |
| 290 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]); |
| 291 | } else if (SkTextBlob::kFull_Positioning == it.positioning()) { |
| 292 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]); |
| 293 | REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | it.next(); |
| 298 | } |
| 299 | |
| 300 | REPORTER_ASSERT(reporter, it.done()); |
| 301 | } |
| 302 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 303 | static void AddRun(const SkPaint& font, int count, SkTextBlob::GlyphPositioning pos, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 304 | const SkPoint& offset, SkTextBlobBuilder& builder, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 305 | const SkRect* bounds = nullptr) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 306 | switch (pos) { |
| 307 | case SkTextBlob::kDefault_Positioning: { |
| 308 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(), |
| 309 | offset.y(), bounds); |
| 310 | for (int i = 0; i < count; ++i) { |
| 311 | rb.glyphs[i] = i; |
| 312 | } |
| 313 | } break; |
| 314 | case SkTextBlob::kHorizontal_Positioning: { |
| 315 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(), |
| 316 | bounds); |
| 317 | for (int i = 0; i < count; ++i) { |
| 318 | rb.glyphs[i] = i; |
| 319 | rb.pos[i] = SkIntToScalar(i); |
| 320 | } |
| 321 | } break; |
| 322 | case SkTextBlob::kFull_Positioning: { |
| 323 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds); |
| 324 | for (int i = 0; i < count; ++i) { |
| 325 | rb.glyphs[i] = i; |
| 326 | rb.pos[i * 2] = SkIntToScalar(i); |
| 327 | rb.pos[i * 2 + 1] = -SkIntToScalar(i); |
| 328 | } |
| 329 | } break; |
| 330 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 331 | SK_ABORT("unhandled positioning value"); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | DEF_TEST(TextBlob_builder, reporter) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 337 | TextBlobTester::TestBuilder(reporter); |
| 338 | TextBlobTester::TestBounds(reporter); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 339 | } |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 340 | |
| 341 | DEF_TEST(TextBlob_paint, reporter) { |
| 342 | TextBlobTester::TestPaintProps(reporter); |
| 343 | } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 344 | |
| 345 | DEF_TEST(TextBlob_extended, reporter) { |
| 346 | SkTextBlobBuilder textBlobBuilder; |
| 347 | SkPaint paint; |
| 348 | const char text1[] = "Foo"; |
| 349 | const char text2[] = "Bar"; |
| 350 | |
| 351 | int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr); |
| 352 | SkAutoTMalloc<uint16_t> glyphs(glyphCount); |
| 353 | (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get()); |
| 354 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 355 | |
| 356 | auto run = textBlobBuilder.allocRunText( |
| 357 | paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr); |
| 358 | memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount); |
| 359 | memcpy(run.utf8text, text2, strlen(text2)); |
| 360 | for (int i = 0; i < glyphCount; ++i) { |
| 361 | run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2))); |
| 362 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 363 | sk_sp<SkTextBlob> blob(textBlobBuilder.make()); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 364 | REPORTER_ASSERT(reporter, blob); |
| 365 | |
| 366 | for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 367 | REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount); |
| 368 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 369 | REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]); |
| 370 | } |
| 371 | REPORTER_ASSERT(reporter, SkTextBlob::kDefault_Positioning == it.positioning()); |
| 372 | REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset()); |
| 373 | REPORTER_ASSERT(reporter, it.textSize() > 0); |
| 374 | REPORTER_ASSERT(reporter, it.clusters()); |
| 375 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 376 | REPORTER_ASSERT(reporter, i == it.clusters()[i]); |
| 377 | } |
| 378 | REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize())); |
| 379 | } |
| 380 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 381 | |
| 382 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 383 | #include "SkCanvas.h" |
| 384 | #include "SkSurface.h" |
| 385 | #include "SkTDArray.h" |
| 386 | |
| 387 | static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y, |
| 388 | sk_sp<SkTypeface> tf) { |
| 389 | SkPaint paint; |
| 390 | paint.setAntiAlias(true); |
| 391 | paint.setSubpixelText(true); |
| 392 | paint.setTextSize(16); |
| 393 | paint.setTypeface(tf); |
| 394 | |
| 395 | int glyphCount = paint.textToGlyphs(text, strlen(text), nullptr); |
| 396 | |
| 397 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 398 | SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(paint, glyphCount, x, y); |
| 399 | |
| 400 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 401 | (void)paint.textToGlyphs(text, strlen(text), buffer.glyphs); |
| 402 | } |
| 403 | |
| 404 | static sk_sp<SkImage> render(const SkTextBlob* blob) { |
| 405 | auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()), |
| 406 | SkScalarRoundToInt(blob->bounds().height())); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 407 | if (!surf) { |
| 408 | return nullptr; // bounds are empty? |
| 409 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 410 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 411 | surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint()); |
| 412 | return surf->makeImageSnapshot(); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * Build a blob with more than one typeface. |
| 417 | * Draw it into an offscreen, |
| 418 | * then serialize and deserialize, |
| 419 | * Then draw the new instance and assert it draws the same as the original. |
| 420 | */ |
| 421 | DEF_TEST(TextBlob_serialize, reporter) { |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 422 | sk_sp<SkTextBlob> blob0 = []() { |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 423 | sk_sp<SkTypeface> tf = SkTypeface::MakeDefault(); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 424 | |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 425 | SkTextBlobBuilder builder; |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 426 | add_run(&builder, "Hello", 10, 20, nullptr); // we don't flatten this in the paint |
| 427 | add_run(&builder, "World", 10, 40, tf); // we will flatten this in the paint |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 428 | return builder.make(); |
| 429 | }(); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 430 | |
| 431 | SkTDArray<SkTypeface*> array; |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 432 | sk_sp<SkData> data = blob0->serialize([](SkTypeface* tf, void* ctx) { |
| 433 | auto array = (SkTDArray<SkTypeface*>*)ctx; |
| 434 | if (array->find(tf) < 0) { |
| 435 | *array->append() = tf; |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 436 | } |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 437 | }, &array); |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 438 | // we only expect 1, since null would not have been serialized, but the default would |
| 439 | REPORTER_ASSERT(reporter, array.count() == 1); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 440 | |
| 441 | sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 442 | [](uint32_t uniqueID, void* ctx) { |
| 443 | auto array = (SkTDArray<SkTypeface*>*)ctx; |
| 444 | for (int i = 0; i < array->count(); ++i) { |
| 445 | if ((*array)[i]->uniqueID() == uniqueID) { |
| 446 | return sk_ref_sp((*array)[i]); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 447 | } |
| 448 | } |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 449 | SkASSERT(false); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 450 | return sk_sp<SkTypeface>(nullptr); |
Mike Reed | aaa3056 | 2017-07-21 11:53:23 -0400 | [diff] [blame] | 451 | }, &array); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 452 | |
| 453 | sk_sp<SkImage> img0 = render(blob0.get()); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 454 | sk_sp<SkImage> img1 = render(blob1.get()); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 455 | if (img0 && img1) { |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 456 | REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get())); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 457 | } |
| 458 | } |