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" |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 11 | #include "SkTypeface.h" |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 12 | |
| 13 | #include "Test.h" |
| 14 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 15 | class TextBlobTester { |
| 16 | public: |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 17 | // This unit test feeds an SkTextBlobBuilder various runs then checks to see if |
| 18 | // the result contains the provided data and merges runs when appropriate. |
| 19 | static void TestBuilder(skiatest::Reporter* reporter) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 20 | SkTextBlobBuilder builder; |
| 21 | |
| 22 | // empty run set |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | RunBuilderTest(reporter, builder, nullptr, 0, nullptr, 0); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 24 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 25 | RunDef set1[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 26 | { 128, SkTextBlob::kDefault_Positioning, 100, 100 }, |
| 27 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 28 | RunBuilderTest(reporter, builder, set1, SK_ARRAY_COUNT(set1), set1, SK_ARRAY_COUNT(set1)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 29 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 30 | RunDef set2[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 31 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 }, |
| 32 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 33 | RunBuilderTest(reporter, builder, set2, SK_ARRAY_COUNT(set2), set2, SK_ARRAY_COUNT(set2)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 34 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 35 | RunDef set3[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 36 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 37 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 38 | RunBuilderTest(reporter, builder, set3, SK_ARRAY_COUNT(set3), set3, SK_ARRAY_COUNT(set3)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 39 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 40 | RunDef set4[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 41 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 42 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 43 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 44 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 45 | RunBuilderTest(reporter, builder, set4, SK_ARRAY_COUNT(set4), set4, SK_ARRAY_COUNT(set4)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 46 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 47 | RunDef set5[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 48 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 49 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 50 | { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 }, |
| 51 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 52 | RunDef mergedSet5[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 53 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 54 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 }, |
| 55 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 56 | RunBuilderTest(reporter, builder, set5, SK_ARRAY_COUNT(set5), mergedSet5, |
| 57 | SK_ARRAY_COUNT(mergedSet5)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 58 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 59 | RunDef set6[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 60 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 61 | { 128, SkTextBlob::kFull_Positioning, 200, 200 }, |
| 62 | { 128, SkTextBlob::kFull_Positioning, 300, 300 }, |
| 63 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 64 | RunDef mergedSet6[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 65 | { 384, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 66 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 67 | RunBuilderTest(reporter, builder, set6, SK_ARRAY_COUNT(set6), mergedSet6, |
| 68 | SK_ARRAY_COUNT(mergedSet6)); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 69 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 70 | RunDef set7[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 71 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 72 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 73 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 74 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 75 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 76 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 77 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 78 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 79 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 }, |
| 80 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 }, |
| 81 | { 128, SkTextBlob::kFull_Positioning, 400, 750 }, |
| 82 | { 128, SkTextBlob::kFull_Positioning, 400, 850 }, |
| 83 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 84 | RunDef mergedSet7[] = { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 85 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 86 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 87 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 88 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 89 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 90 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 91 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 }, |
| 92 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 }, |
| 93 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 94 | }; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 95 | RunBuilderTest(reporter, builder, set7, SK_ARRAY_COUNT(set7), mergedSet7, |
| 96 | SK_ARRAY_COUNT(mergedSet7)); |
| 97 | } |
| 98 | |
| 99 | // This unit test verifies blob bounds computation. |
| 100 | static void TestBounds(skiatest::Reporter* reporter) { |
| 101 | SkTextBlobBuilder builder; |
| 102 | SkPaint font; |
| 103 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 104 | |
| 105 | // Explicit bounds. |
| 106 | { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 107 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 113 | builder.allocRun(font, 16, 0, 0, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 114 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 115 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 116 | } |
| 117 | |
| 118 | { |
| 119 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 120 | builder.allocRunPosH(font, 16, 0, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 121 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 123 | } |
| 124 | |
| 125 | { |
| 126 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 127 | builder.allocRunPos(font, 16, &r1); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 128 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 129 | REPORTER_ASSERT(reporter, blob->bounds() == r1); |
| 130 | } |
| 131 | |
| 132 | { |
| 133 | SkRect r1 = SkRect::MakeXYWH(10, 10, 20, 20); |
| 134 | SkRect r2 = SkRect::MakeXYWH(15, 20, 50, 50); |
| 135 | SkRect r3 = SkRect::MakeXYWH(0, 5, 10, 5); |
| 136 | |
| 137 | builder.allocRun(font, 16, 0, 0, &r1); |
| 138 | builder.allocRunPosH(font, 16, 0, &r2); |
| 139 | builder.allocRunPos(font, 16, &r3); |
| 140 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 141 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 142 | REPORTER_ASSERT(reporter, blob->bounds() == SkRect::MakeXYWH(0, 5, 65, 65)); |
| 143 | } |
| 144 | |
| 145 | { |
| 146 | // Verify empty blob bounds after building some non-empty blobs. |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 147 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 148 | REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); |
| 149 | } |
| 150 | |
| 151 | // Implicit bounds |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 152 | |
| 153 | { |
| 154 | // Exercise the empty bounds path, and ensure that RunRecord-aligned pos buffers |
| 155 | // don't trigger asserts (http://crbug.com/542643). |
| 156 | SkPaint p; |
| 157 | p.setTextSize(0); |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 158 | p.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 159 | |
| 160 | const char* txt = "BOOO"; |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 161 | const size_t txtLen = strlen(txt); |
| 162 | const int glyphCount = p.textToGlyphs(txt, txtLen, nullptr); |
| 163 | |
| 164 | p.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 165 | const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(p, glyphCount); |
| 166 | |
| 167 | p.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 168 | p.textToGlyphs(txt, txtLen, buffer.glyphs); |
| 169 | |
| 170 | memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 171 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); |
| 173 | } |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 176 | // Verify that text-related properties are captured in run paints. |
| 177 | static void TestPaintProps(skiatest::Reporter* reporter) { |
| 178 | SkPaint font; |
| 179 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 180 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 181 | // Kitchen sink font. |
| 182 | font.setTextSize(42); |
| 183 | font.setTextScaleX(4.2f); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 184 | font.setTypeface(SkTypeface::MakeDefault()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 185 | font.setTextSkewX(0.42f); |
| 186 | font.setTextAlign(SkPaint::kCenter_Align); |
| 187 | font.setHinting(SkPaint::kFull_Hinting); |
| 188 | font.setAntiAlias(true); |
| 189 | font.setUnderlineText(true); |
| 190 | font.setStrikeThruText(true); |
| 191 | font.setFakeBoldText(true); |
| 192 | font.setLinearText(true); |
| 193 | font.setSubpixelText(true); |
| 194 | font.setDevKernText(true); |
| 195 | font.setLCDRenderText(true); |
| 196 | font.setEmbeddedBitmapText(true); |
| 197 | font.setAutohinted(true); |
| 198 | font.setVerticalText(true); |
| 199 | font.setFlags(font.getFlags() | SkPaint::kGenA8FromLCD_Flag); |
| 200 | |
| 201 | // Ensure we didn't pick default values by mistake. |
| 202 | SkPaint defaultPaint; |
| 203 | REPORTER_ASSERT(reporter, defaultPaint.getTextSize() != font.getTextSize()); |
| 204 | REPORTER_ASSERT(reporter, defaultPaint.getTextScaleX() != font.getTextScaleX()); |
| 205 | REPORTER_ASSERT(reporter, defaultPaint.getTypeface() != font.getTypeface()); |
| 206 | REPORTER_ASSERT(reporter, defaultPaint.getTextSkewX() != font.getTextSkewX()); |
| 207 | REPORTER_ASSERT(reporter, defaultPaint.getTextAlign() != font.getTextAlign()); |
| 208 | REPORTER_ASSERT(reporter, defaultPaint.getHinting() != font.getHinting()); |
| 209 | REPORTER_ASSERT(reporter, defaultPaint.isAntiAlias() != font.isAntiAlias()); |
| 210 | REPORTER_ASSERT(reporter, defaultPaint.isUnderlineText() != font.isUnderlineText()); |
| 211 | REPORTER_ASSERT(reporter, defaultPaint.isStrikeThruText() != font.isStrikeThruText()); |
| 212 | REPORTER_ASSERT(reporter, defaultPaint.isFakeBoldText() != font.isFakeBoldText()); |
| 213 | REPORTER_ASSERT(reporter, defaultPaint.isLinearText() != font.isLinearText()); |
| 214 | REPORTER_ASSERT(reporter, defaultPaint.isSubpixelText() != font.isSubpixelText()); |
| 215 | REPORTER_ASSERT(reporter, defaultPaint.isDevKernText() != font.isDevKernText()); |
| 216 | REPORTER_ASSERT(reporter, defaultPaint.isLCDRenderText() != font.isLCDRenderText()); |
| 217 | REPORTER_ASSERT(reporter, defaultPaint.isEmbeddedBitmapText() != font.isEmbeddedBitmapText()); |
| 218 | REPORTER_ASSERT(reporter, defaultPaint.isAutohinted() != font.isAutohinted()); |
| 219 | REPORTER_ASSERT(reporter, defaultPaint.isVerticalText() != font.isVerticalText()); |
| 220 | REPORTER_ASSERT(reporter, (defaultPaint.getFlags() & SkPaint::kGenA8FromLCD_Flag) != |
| 221 | (font.getFlags() & SkPaint::kGenA8FromLCD_Flag)); |
| 222 | |
| 223 | SkTextBlobBuilder builder; |
| 224 | AddRun(font, 1, SkTextBlob::kDefault_Positioning, SkPoint::Make(0, 0), builder); |
| 225 | AddRun(font, 1, SkTextBlob::kHorizontal_Positioning, SkPoint::Make(0, 0), builder); |
| 226 | AddRun(font, 1, SkTextBlob::kFull_Positioning, SkPoint::Make(0, 0), builder); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 227 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 228 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 229 | SkTextBlobRunIterator it(blob.get()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 230 | while (!it.done()) { |
| 231 | SkPaint paint; |
| 232 | it.applyFontToPaint(&paint); |
| 233 | |
| 234 | REPORTER_ASSERT(reporter, paint.getTextSize() == font.getTextSize()); |
| 235 | REPORTER_ASSERT(reporter, paint.getTextScaleX() == font.getTextScaleX()); |
| 236 | REPORTER_ASSERT(reporter, paint.getTypeface() == font.getTypeface()); |
| 237 | REPORTER_ASSERT(reporter, paint.getTextSkewX() == font.getTextSkewX()); |
| 238 | REPORTER_ASSERT(reporter, paint.getTextAlign() == font.getTextAlign()); |
| 239 | REPORTER_ASSERT(reporter, paint.getHinting() == font.getHinting()); |
| 240 | REPORTER_ASSERT(reporter, paint.isAntiAlias() == font.isAntiAlias()); |
| 241 | REPORTER_ASSERT(reporter, paint.isUnderlineText() == font.isUnderlineText()); |
| 242 | REPORTER_ASSERT(reporter, paint.isStrikeThruText() == font.isStrikeThruText()); |
| 243 | REPORTER_ASSERT(reporter, paint.isFakeBoldText() == font.isFakeBoldText()); |
| 244 | REPORTER_ASSERT(reporter, paint.isLinearText() == font.isLinearText()); |
| 245 | REPORTER_ASSERT(reporter, paint.isSubpixelText() == font.isSubpixelText()); |
| 246 | REPORTER_ASSERT(reporter, paint.isDevKernText() == font.isDevKernText()); |
| 247 | REPORTER_ASSERT(reporter, paint.isLCDRenderText() == font.isLCDRenderText()); |
| 248 | REPORTER_ASSERT(reporter, paint.isEmbeddedBitmapText() == font.isEmbeddedBitmapText()); |
| 249 | REPORTER_ASSERT(reporter, paint.isAutohinted() == font.isAutohinted()); |
| 250 | REPORTER_ASSERT(reporter, paint.isVerticalText() == font.isVerticalText()); |
| 251 | REPORTER_ASSERT(reporter, (paint.getFlags() & SkPaint::kGenA8FromLCD_Flag) == |
| 252 | (font.getFlags() & SkPaint::kGenA8FromLCD_Flag)); |
| 253 | |
| 254 | it.next(); |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 259 | private: |
| 260 | struct RunDef { |
| 261 | unsigned count; |
| 262 | SkTextBlob::GlyphPositioning pos; |
| 263 | SkScalar x, y; |
| 264 | }; |
| 265 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 266 | static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 267 | const RunDef in[], unsigned inCount, |
| 268 | const RunDef out[], unsigned outCount) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 269 | SkPaint font; |
| 270 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 271 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 272 | unsigned glyphCount = 0; |
| 273 | unsigned posCount = 0; |
| 274 | |
| 275 | for (unsigned i = 0; i < inCount; ++i) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 276 | 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] | 277 | glyphCount += in[i].count; |
| 278 | posCount += in[i].count * in[i].pos; |
| 279 | } |
| 280 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 281 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 282 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 283 | SkTextBlobRunIterator it(blob.get()); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 284 | for (unsigned i = 0; i < outCount; ++i) { |
| 285 | REPORTER_ASSERT(reporter, !it.done()); |
| 286 | REPORTER_ASSERT(reporter, out[i].pos == it.positioning()); |
| 287 | REPORTER_ASSERT(reporter, out[i].count == it.glyphCount()); |
| 288 | if (SkTextBlob::kDefault_Positioning == out[i].pos) { |
| 289 | REPORTER_ASSERT(reporter, out[i].x == it.offset().x()); |
| 290 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 291 | } else if (SkTextBlob::kHorizontal_Positioning == out[i].pos) { |
| 292 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 293 | } |
| 294 | |
| 295 | for (unsigned k = 0; k < it.glyphCount(); ++k) { |
| 296 | REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]); |
| 297 | if (SkTextBlob::kHorizontal_Positioning == it.positioning()) { |
| 298 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]); |
| 299 | } else if (SkTextBlob::kFull_Positioning == it.positioning()) { |
| 300 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]); |
| 301 | REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | it.next(); |
| 306 | } |
| 307 | |
| 308 | REPORTER_ASSERT(reporter, it.done()); |
| 309 | } |
| 310 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 311 | static void AddRun(const SkPaint& font, int count, SkTextBlob::GlyphPositioning pos, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 312 | const SkPoint& offset, SkTextBlobBuilder& builder, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 313 | const SkRect* bounds = nullptr) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 314 | switch (pos) { |
| 315 | case SkTextBlob::kDefault_Positioning: { |
| 316 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(), |
| 317 | offset.y(), bounds); |
| 318 | for (int i = 0; i < count; ++i) { |
| 319 | rb.glyphs[i] = i; |
| 320 | } |
| 321 | } break; |
| 322 | case SkTextBlob::kHorizontal_Positioning: { |
| 323 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(), |
| 324 | bounds); |
| 325 | for (int i = 0; i < count; ++i) { |
| 326 | rb.glyphs[i] = i; |
| 327 | rb.pos[i] = SkIntToScalar(i); |
| 328 | } |
| 329 | } break; |
| 330 | case SkTextBlob::kFull_Positioning: { |
| 331 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds); |
| 332 | for (int i = 0; i < count; ++i) { |
| 333 | rb.glyphs[i] = i; |
| 334 | rb.pos[i * 2] = SkIntToScalar(i); |
| 335 | rb.pos[i * 2 + 1] = -SkIntToScalar(i); |
| 336 | } |
| 337 | } break; |
| 338 | default: |
| 339 | SkFAIL("unhandled positioning value"); |
| 340 | } |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | DEF_TEST(TextBlob_builder, reporter) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 345 | TextBlobTester::TestBuilder(reporter); |
| 346 | TextBlobTester::TestBounds(reporter); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 347 | } |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 348 | |
| 349 | DEF_TEST(TextBlob_paint, reporter) { |
| 350 | TextBlobTester::TestPaintProps(reporter); |
| 351 | } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 352 | |
| 353 | DEF_TEST(TextBlob_extended, reporter) { |
| 354 | SkTextBlobBuilder textBlobBuilder; |
| 355 | SkPaint paint; |
| 356 | const char text1[] = "Foo"; |
| 357 | const char text2[] = "Bar"; |
| 358 | |
| 359 | int glyphCount = paint.textToGlyphs(text1, strlen(text1), nullptr); |
| 360 | SkAutoTMalloc<uint16_t> glyphs(glyphCount); |
| 361 | (void)paint.textToGlyphs(text1, strlen(text1), glyphs.get()); |
| 362 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 363 | |
| 364 | auto run = textBlobBuilder.allocRunText( |
| 365 | paint, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr); |
| 366 | memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount); |
| 367 | memcpy(run.utf8text, text2, strlen(text2)); |
| 368 | for (int i = 0; i < glyphCount; ++i) { |
| 369 | run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2))); |
| 370 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 371 | sk_sp<SkTextBlob> blob(textBlobBuilder.make()); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 372 | REPORTER_ASSERT(reporter, blob); |
| 373 | |
| 374 | for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 375 | REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount); |
| 376 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 377 | REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]); |
| 378 | } |
| 379 | REPORTER_ASSERT(reporter, SkTextBlob::kDefault_Positioning == it.positioning()); |
| 380 | REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset()); |
| 381 | REPORTER_ASSERT(reporter, it.textSize() > 0); |
| 382 | REPORTER_ASSERT(reporter, it.clusters()); |
| 383 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 384 | REPORTER_ASSERT(reporter, i == it.clusters()[i]); |
| 385 | } |
| 386 | REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize())); |
| 387 | } |
| 388 | } |