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