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