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; |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 105 | SkFont font; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 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). |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 157 | SkFont font; |
| 158 | font.setSize(0); |
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); |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 162 | const int glyphCount = font.countText(txt, txtLen, kUTF8_SkTextEncoding); |
| 163 | const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, glyphCount); |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 164 | |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 165 | font.textToGlyphs(txt, txtLen, kUTF8_SkTextEncoding, buffer.glyphs, glyphCount); |
fmalita | 0b01da7 | 2015-10-14 08:11:40 -0700 | [diff] [blame] | 166 | |
| 167 | memset(buffer.pos, 0, sizeof(SkScalar) * glyphCount * 2); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 168 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 9ae8fe1 | 2015-10-13 08:59:23 -0700 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, blob->bounds().isEmpty()); |
| 170 | } |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 171 | } |
| 172 | |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 173 | // Verify that text-related properties are captured in run paints. |
| 174 | static void TestPaintProps(skiatest::Reporter* reporter) { |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 175 | SkFont font; |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 176 | // Kitchen sink font. |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 177 | font.setSize(42); |
| 178 | font.setScaleX(4.2f); |
| 179 | font.setTypeface(sk_tool_utils::create_portable_typeface()); |
| 180 | font.setSkewX(0.42f); |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame] | 181 | font.setHinting(kFull_SkFontHinting); |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 182 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 183 | font.setEmbolden(true); |
| 184 | font.setLinearMetrics(true); |
| 185 | font.setSubpixel(true); |
| 186 | font.setEmbeddedBitmaps(true); |
| 187 | font.setForceAutoHinting(true); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 188 | |
| 189 | // Ensure we didn't pick default values by mistake. |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 190 | SkFont defaultFont; |
| 191 | REPORTER_ASSERT(reporter, defaultFont.getSize() != font.getSize()); |
| 192 | REPORTER_ASSERT(reporter, defaultFont.getScaleX() != font.getScaleX()); |
| 193 | REPORTER_ASSERT(reporter, defaultFont.getTypeface() != font.getTypeface()); |
| 194 | REPORTER_ASSERT(reporter, defaultFont.getSkewX() != font.getSkewX()); |
| 195 | REPORTER_ASSERT(reporter, defaultFont.getHinting() != font.getHinting()); |
| 196 | REPORTER_ASSERT(reporter, defaultFont.getEdging() != font.getEdging()); |
| 197 | REPORTER_ASSERT(reporter, defaultFont.isEmbolden() != font.isEmbolden()); |
| 198 | REPORTER_ASSERT(reporter, defaultFont.isLinearMetrics() != font.isLinearMetrics()); |
| 199 | REPORTER_ASSERT(reporter, defaultFont.isSubpixel() != font.isSubpixel()); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 200 | REPORTER_ASSERT(reporter, |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 201 | defaultFont.isEmbeddedBitmaps() != font.isEmbeddedBitmaps()); |
| 202 | REPORTER_ASSERT(reporter, defaultFont.isForceAutoHinting() != font.isForceAutoHinting()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 203 | |
| 204 | SkTextBlobBuilder builder; |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 205 | AddRun(font, 1, SkTextBlobRunIterator::kDefault_Positioning, SkPoint::Make(0, 0), builder); |
| 206 | AddRun(font, 1, SkTextBlobRunIterator::kHorizontal_Positioning, SkPoint::Make(0, 0), |
| 207 | builder); |
| 208 | AddRun(font, 1, SkTextBlobRunIterator::kFull_Positioning, SkPoint::Make(0, 0), builder); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 209 | sk_sp<SkTextBlob> blob(builder.make()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 210 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 211 | SkTextBlobRunIterator it(blob.get()); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 212 | while (!it.done()) { |
Mike Reed | 60a0e87 | 2019-01-04 09:41:49 -0500 | [diff] [blame] | 213 | REPORTER_ASSERT(reporter, it.font() == font); |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 214 | it.next(); |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 219 | private: |
| 220 | struct RunDef { |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 221 | unsigned count; |
| 222 | SkTextBlobRunIterator::GlyphPositioning pos; |
| 223 | SkScalar x, y; |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 226 | static void RunBuilderTest(skiatest::Reporter* reporter, SkTextBlobBuilder& builder, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 227 | const RunDef in[], unsigned inCount, |
| 228 | const RunDef out[], unsigned outCount) { |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 229 | SkFont font; |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 230 | |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 231 | unsigned glyphCount = 0; |
| 232 | unsigned posCount = 0; |
| 233 | |
| 234 | for (unsigned i = 0; i < inCount; ++i) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 235 | 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] | 236 | glyphCount += in[i].count; |
| 237 | posCount += in[i].count * in[i].pos; |
| 238 | } |
| 239 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 240 | sk_sp<SkTextBlob> blob(builder.make()); |
Florin Malita | 3a9a7a3 | 2017-03-13 09:03:24 -0400 | [diff] [blame] | 241 | REPORTER_ASSERT(reporter, (inCount > 0) == SkToBool(blob)); |
| 242 | if (!blob) { |
| 243 | return; |
| 244 | } |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 245 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 246 | SkTextBlobRunIterator it(blob.get()); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 247 | for (unsigned i = 0; i < outCount; ++i) { |
| 248 | REPORTER_ASSERT(reporter, !it.done()); |
| 249 | REPORTER_ASSERT(reporter, out[i].pos == it.positioning()); |
| 250 | REPORTER_ASSERT(reporter, out[i].count == it.glyphCount()); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 251 | if (SkTextBlobRunIterator::kDefault_Positioning == out[i].pos) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 252 | REPORTER_ASSERT(reporter, out[i].x == it.offset().x()); |
| 253 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 254 | } else if (SkTextBlobRunIterator::kHorizontal_Positioning == out[i].pos) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 255 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 256 | } |
| 257 | |
| 258 | for (unsigned k = 0; k < it.glyphCount(); ++k) { |
| 259 | REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 260 | if (SkTextBlobRunIterator::kHorizontal_Positioning == it.positioning()) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 261 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 262 | } else if (SkTextBlobRunIterator::kFull_Positioning == it.positioning()) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]); |
| 264 | REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | it.next(); |
| 269 | } |
| 270 | |
| 271 | REPORTER_ASSERT(reporter, it.done()); |
| 272 | } |
| 273 | |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 274 | static void AddRun(const SkFont& font, int count, SkTextBlobRunIterator::GlyphPositioning pos, |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 275 | const SkPoint& offset, SkTextBlobBuilder& builder, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 276 | const SkRect* bounds = nullptr) { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 277 | switch (pos) { |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 278 | case SkTextBlobRunIterator::kDefault_Positioning: { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 279 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(), |
| 280 | offset.y(), bounds); |
| 281 | for (int i = 0; i < count; ++i) { |
| 282 | rb.glyphs[i] = i; |
| 283 | } |
| 284 | } break; |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 285 | case SkTextBlobRunIterator::kHorizontal_Positioning: { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 286 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(), |
| 287 | bounds); |
| 288 | for (int i = 0; i < count; ++i) { |
| 289 | rb.glyphs[i] = i; |
| 290 | rb.pos[i] = SkIntToScalar(i); |
| 291 | } |
| 292 | } break; |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 293 | case SkTextBlobRunIterator::kFull_Positioning: { |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 294 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds); |
| 295 | for (int i = 0; i < count; ++i) { |
| 296 | rb.glyphs[i] = i; |
| 297 | rb.pos[i * 2] = SkIntToScalar(i); |
| 298 | rb.pos[i * 2 + 1] = -SkIntToScalar(i); |
| 299 | } |
| 300 | } break; |
| 301 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 302 | SK_ABORT("unhandled positioning value"); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | DEF_TEST(TextBlob_builder, reporter) { |
fmalita | 51bf957 | 2014-08-22 07:50:45 -0700 | [diff] [blame] | 308 | TextBlobTester::TestBuilder(reporter); |
| 309 | TextBlobTester::TestBounds(reporter); |
fmalita | c6765d6 | 2014-08-21 15:03:04 -0700 | [diff] [blame] | 310 | } |
fmalita | 32d6b87 | 2015-11-17 11:39:32 -0800 | [diff] [blame] | 311 | |
| 312 | DEF_TEST(TextBlob_paint, reporter) { |
| 313 | TextBlobTester::TestPaintProps(reporter); |
| 314 | } |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 315 | |
| 316 | DEF_TEST(TextBlob_extended, reporter) { |
| 317 | SkTextBlobBuilder textBlobBuilder; |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 318 | SkFont font; |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 319 | const char text1[] = "Foo"; |
| 320 | const char text2[] = "Bar"; |
| 321 | |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 322 | int glyphCount = font.countText(text1, strlen(text1), kUTF8_SkTextEncoding); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 323 | SkAutoTMalloc<uint16_t> glyphs(glyphCount); |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 324 | (void)font.textToGlyphs(text1, strlen(text1), kUTF8_SkTextEncoding, glyphs.get(), glyphCount); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 325 | |
Cary Clark | e12a090 | 2018-08-09 10:07:33 -0400 | [diff] [blame] | 326 | auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder, |
Mike Reed | 6d59568 | 2018-12-05 17:28:14 -0500 | [diff] [blame] | 327 | font, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 328 | memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount); |
| 329 | memcpy(run.utf8text, text2, strlen(text2)); |
| 330 | for (int i = 0; i < glyphCount; ++i) { |
| 331 | run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2))); |
| 332 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 333 | sk_sp<SkTextBlob> blob(textBlobBuilder.make()); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 334 | REPORTER_ASSERT(reporter, blob); |
| 335 | |
| 336 | for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 337 | REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount); |
| 338 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 339 | REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]); |
| 340 | } |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 341 | REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning()); |
halcanary | 4f0a23a | 2016-08-30 11:58:33 -0700 | [diff] [blame] | 342 | REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset()); |
| 343 | REPORTER_ASSERT(reporter, it.textSize() > 0); |
| 344 | REPORTER_ASSERT(reporter, it.clusters()); |
| 345 | for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 346 | REPORTER_ASSERT(reporter, i == it.clusters()[i]); |
| 347 | } |
| 348 | REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize())); |
| 349 | } |
| 350 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 351 | |
| 352 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 353 | #include "SkCanvas.h" |
| 354 | #include "SkSurface.h" |
| 355 | #include "SkTDArray.h" |
| 356 | |
| 357 | static void add_run(SkTextBlobBuilder* builder, const char text[], SkScalar x, SkScalar y, |
| 358 | sk_sp<SkTypeface> tf) { |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 359 | SkFont font; |
| 360 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 361 | font.setSubpixel(true); |
| 362 | font.setSize(16); |
| 363 | font.setTypeface(tf); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 364 | |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 365 | int glyphCount = font.countText(text, strlen(text), kUTF8_SkTextEncoding); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 366 | |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 367 | SkTextBlobBuilder::RunBuffer buffer = builder->allocRun(font, glyphCount, x, y); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 368 | |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 369 | (void)font.textToGlyphs(text, strlen(text), kUTF8_SkTextEncoding, buffer.glyphs, glyphCount); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static sk_sp<SkImage> render(const SkTextBlob* blob) { |
| 373 | auto surf = SkSurface::MakeRasterN32Premul(SkScalarRoundToInt(blob->bounds().width()), |
| 374 | SkScalarRoundToInt(blob->bounds().height())); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 375 | if (!surf) { |
| 376 | return nullptr; // bounds are empty? |
| 377 | } |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 378 | surf->getCanvas()->clear(SK_ColorWHITE); |
| 379 | surf->getCanvas()->drawTextBlob(blob, -blob->bounds().left(), -blob->bounds().top(), SkPaint()); |
| 380 | return surf->makeImageSnapshot(); |
| 381 | } |
| 382 | |
Cary Clark | 785586a | 2018-07-19 10:07:01 -0400 | [diff] [blame] | 383 | static sk_sp<SkData> SerializeTypeface(SkTypeface* tf, void* ctx) { |
| 384 | auto array = (SkTDArray<SkTypeface*>*)ctx; |
| 385 | *array->append() = tf; |
| 386 | return sk_sp<SkData>(nullptr); |
| 387 | } |
| 388 | |
| 389 | static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx) { |
| 390 | auto array = (SkTDArray<SkTypeface*>*)ctx; |
| 391 | for (int i = 0; i < array->count(); ++i) { |
| 392 | auto result = (*array)[i]; |
| 393 | if (result) { |
| 394 | (*array)[i] = nullptr; |
| 395 | return sk_ref_sp(result); |
| 396 | } |
| 397 | } |
| 398 | SkASSERT(false); |
| 399 | return sk_sp<SkTypeface>(nullptr); |
| 400 | } |
| 401 | |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 402 | /* |
| 403 | * Build a blob with more than one typeface. |
| 404 | * Draw it into an offscreen, |
| 405 | * then serialize and deserialize, |
| 406 | * Then draw the new instance and assert it draws the same as the original. |
| 407 | */ |
| 408 | DEF_TEST(TextBlob_serialize, reporter) { |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 409 | sk_sp<SkTextBlob> blob0 = []() { |
Mike Reed | 8e74cbc | 2017-12-08 13:20:01 -0500 | [diff] [blame] | 410 | sk_sp<SkTypeface> tf = SkTypeface::MakeDefault(); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 411 | |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 412 | SkTextBlobBuilder builder; |
Mike Reed | 0951cc2 | 2018-12-21 16:19:24 -0500 | [diff] [blame] | 413 | add_run(&builder, "Hello", 10, 20, nullptr); // don't flatten a typeface |
| 414 | add_run(&builder, "World", 10, 40, tf); // do flatten this typeface |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 415 | return builder.make(); |
| 416 | }(); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 417 | |
| 418 | SkTDArray<SkTypeface*> array; |
Cary Clark | 785586a | 2018-07-19 10:07:01 -0400 | [diff] [blame] | 419 | SkSerialProcs serializeProcs; |
| 420 | serializeProcs.fTypefaceProc = &SerializeTypeface; |
| 421 | serializeProcs.fTypefaceCtx = (void*) &array; |
| 422 | sk_sp<SkData> data = blob0->serialize(serializeProcs); |
Mike Reed | 0951cc2 | 2018-12-21 16:19:24 -0500 | [diff] [blame] | 423 | REPORTER_ASSERT(reporter, array.count() == 1); |
Cary Clark | 785586a | 2018-07-19 10:07:01 -0400 | [diff] [blame] | 424 | SkDeserialProcs deserializeProcs; |
| 425 | deserializeProcs.fTypefaceProc = &DeserializeTypeface; |
| 426 | deserializeProcs.fTypefaceCtx = (void*) &array; |
| 427 | sk_sp<SkTextBlob> blob1 = SkTextBlob::Deserialize(data->data(), data->size(), deserializeProcs); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 428 | |
| 429 | sk_sp<SkImage> img0 = render(blob0.get()); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 430 | sk_sp<SkImage> img1 = render(blob1.get()); |
Mike Reed | 7e49d53 | 2017-07-11 15:07:08 -0400 | [diff] [blame] | 431 | if (img0 && img1) { |
Mike Reed | 4bcef3c | 2017-12-08 11:46:09 -0500 | [diff] [blame] | 432 | REPORTER_ASSERT(reporter, sk_tool_utils::equal_pixels(img0.get(), img1.get())); |
Mike Reed | b99bedd | 2017-07-11 10:27:40 -0400 | [diff] [blame] | 433 | } |
| 434 | } |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 435 | |
| 436 | DEF_TEST(TextBlob_MakeAsDrawText, reporter) { |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 437 | const char text[] = "Hello"; |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 438 | auto blob = SkTextBlob::MakeFromString(text, SkFont(), kUTF8_SkTextEncoding); |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 439 | |
| 440 | int runs = 0; |
| 441 | for(SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 442 | REPORTER_ASSERT(reporter, it.glyphCount() == strlen(text)); |
Florin Malita | ab54e73 | 2018-07-27 09:47:15 -0400 | [diff] [blame] | 443 | REPORTER_ASSERT(reporter, it.positioning() == SkTextBlobRunIterator::kFull_Positioning); |
Herb Derby | 4b3a515 | 2018-07-17 16:10:30 -0400 | [diff] [blame] | 444 | runs += 1; |
| 445 | } |
| 446 | REPORTER_ASSERT(reporter, runs == 1); |
| 447 | |
| 448 | } |