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" |
| 10 | #include "SkTextBlob.h" |
| 11 | |
| 12 | #include "Test.h" |
| 13 | |
| 14 | |
| 15 | class TextBlobTester { |
| 16 | public: |
| 17 | static void test_builder(skiatest::Reporter* reporter) { |
| 18 | SkPaint font; |
| 19 | font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 20 | |
| 21 | SkTextBlobBuilder builder; |
| 22 | |
| 23 | // empty run set |
| 24 | runBuilderTest(reporter, font, builder, NULL, 0, NULL, 0); |
| 25 | |
| 26 | RunDef SET1[] = { |
| 27 | { 128, SkTextBlob::kDefault_Positioning, 100, 100 }, |
| 28 | }; |
| 29 | runBuilderTest(reporter, font, builder, SET1, SK_ARRAY_COUNT(SET1), SET1, |
| 30 | SK_ARRAY_COUNT(SET1)); |
| 31 | |
| 32 | RunDef SET2[] = { |
| 33 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 100 }, |
| 34 | }; |
| 35 | runBuilderTest(reporter, font, builder, SET2, SK_ARRAY_COUNT(SET2), SET2, |
| 36 | SK_ARRAY_COUNT(SET2)); |
| 37 | |
| 38 | RunDef SET3[] = { |
| 39 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 40 | }; |
| 41 | runBuilderTest(reporter, font, builder, SET3, SK_ARRAY_COUNT(SET3), SET3, |
| 42 | SK_ARRAY_COUNT(SET3)); |
| 43 | |
| 44 | RunDef SET4[] = { |
| 45 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 46 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 47 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 48 | }; |
| 49 | runBuilderTest(reporter, font, builder, SET4, SK_ARRAY_COUNT(SET4), SET4, |
| 50 | SK_ARRAY_COUNT(SET4)); |
| 51 | |
| 52 | RunDef SET5[] = { |
| 53 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 54 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 55 | { 128, SkTextBlob::kHorizontal_Positioning, 300, 250 }, |
| 56 | }; |
| 57 | RunDef SET5_MERGED[] = { |
| 58 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 59 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 250 }, |
| 60 | }; |
| 61 | runBuilderTest(reporter, font, builder, SET5, SK_ARRAY_COUNT(SET5), SET5_MERGED, |
| 62 | SK_ARRAY_COUNT(SET5_MERGED)); |
| 63 | |
| 64 | RunDef SET6[] = { |
| 65 | { 128, SkTextBlob::kFull_Positioning, 100, 100 }, |
| 66 | { 128, SkTextBlob::kFull_Positioning, 200, 200 }, |
| 67 | { 128, SkTextBlob::kFull_Positioning, 300, 300 }, |
| 68 | }; |
| 69 | RunDef SET6_MERGED[] = { |
| 70 | { 384, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 71 | }; |
| 72 | runBuilderTest(reporter, font, builder, SET6, SK_ARRAY_COUNT(SET6), SET6_MERGED, |
| 73 | SK_ARRAY_COUNT(SET6_MERGED)); |
| 74 | |
| 75 | RunDef SET7[] = { |
| 76 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 77 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 78 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 150 }, |
| 79 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 150 }, |
| 80 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 81 | { 128, SkTextBlob::kFull_Positioning, 400, 350 }, |
| 82 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 83 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 84 | { 128, SkTextBlob::kHorizontal_Positioning, 100, 550 }, |
| 85 | { 128, SkTextBlob::kHorizontal_Positioning, 200, 650 }, |
| 86 | { 128, SkTextBlob::kFull_Positioning, 400, 750 }, |
| 87 | { 128, SkTextBlob::kFull_Positioning, 400, 850 }, |
| 88 | }; |
| 89 | RunDef SET7_MERGED[] = { |
| 90 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 91 | { 128, SkTextBlob::kDefault_Positioning, 100, 150 }, |
| 92 | { 256, SkTextBlob::kHorizontal_Positioning, 0, 150 }, |
| 93 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 94 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 95 | { 128, SkTextBlob::kDefault_Positioning, 100, 450 }, |
| 96 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 550 }, |
| 97 | { 128, SkTextBlob::kHorizontal_Positioning, 0, 650 }, |
| 98 | { 256, SkTextBlob::kFull_Positioning, 0, 0 }, |
| 99 | }; |
| 100 | runBuilderTest(reporter, font, builder, SET7, SK_ARRAY_COUNT(SET7), SET7_MERGED, |
| 101 | SK_ARRAY_COUNT(SET7_MERGED)); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | struct RunDef { |
| 106 | unsigned count; |
| 107 | SkTextBlob::GlyphPositioning pos; |
| 108 | SkScalar x, y; |
| 109 | }; |
| 110 | |
| 111 | static void runBuilderTest(skiatest::Reporter* reporter, const SkPaint& font, |
| 112 | SkTextBlobBuilder& builder, |
| 113 | const RunDef in[], unsigned inCount, |
| 114 | const RunDef out[], unsigned outCount) { |
| 115 | unsigned glyphCount = 0; |
| 116 | unsigned posCount = 0; |
| 117 | |
| 118 | for (unsigned i = 0; i < inCount; ++i) { |
| 119 | addRun(font, in[i].count, in[i].pos, SkPoint::Make(in[i].x, in[i].y), builder); |
| 120 | glyphCount += in[i].count; |
| 121 | posCount += in[i].count * in[i].pos; |
| 122 | } |
| 123 | |
| 124 | SkAutoTUnref<const SkTextBlob> blob(builder.build()); |
| 125 | REPORTER_ASSERT(reporter, (NULL != blob->fGlyphBuffer) == (glyphCount > 0)); |
| 126 | REPORTER_ASSERT(reporter, (NULL != blob->fPosBuffer) == (posCount > 0)); |
| 127 | REPORTER_ASSERT(reporter, (NULL != blob->fRuns.get()) == (inCount > 0)); |
| 128 | |
| 129 | SkTextBlob::RunIterator it(blob); |
| 130 | for (unsigned i = 0; i < outCount; ++i) { |
| 131 | REPORTER_ASSERT(reporter, !it.done()); |
| 132 | REPORTER_ASSERT(reporter, out[i].pos == it.positioning()); |
| 133 | REPORTER_ASSERT(reporter, out[i].count == it.glyphCount()); |
| 134 | if (SkTextBlob::kDefault_Positioning == out[i].pos) { |
| 135 | REPORTER_ASSERT(reporter, out[i].x == it.offset().x()); |
| 136 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 137 | } else if (SkTextBlob::kHorizontal_Positioning == out[i].pos) { |
| 138 | REPORTER_ASSERT(reporter, out[i].y == it.offset().y()); |
| 139 | } |
| 140 | |
| 141 | for (unsigned k = 0; k < it.glyphCount(); ++k) { |
| 142 | REPORTER_ASSERT(reporter, k % 128 == it.glyphs()[k]); |
| 143 | if (SkTextBlob::kHorizontal_Positioning == it.positioning()) { |
| 144 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k]); |
| 145 | } else if (SkTextBlob::kFull_Positioning == it.positioning()) { |
| 146 | REPORTER_ASSERT(reporter, SkIntToScalar(k % 128) == it.pos()[k * 2]); |
| 147 | REPORTER_ASSERT(reporter, -SkIntToScalar(k % 128) == it.pos()[k * 2 + 1]); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | it.next(); |
| 152 | } |
| 153 | |
| 154 | REPORTER_ASSERT(reporter, it.done()); |
| 155 | } |
| 156 | |
| 157 | static void addRun(const SkPaint& font, int count, SkTextBlob::GlyphPositioning pos, |
| 158 | const SkPoint& offset, SkTextBlobBuilder& builder, |
| 159 | const SkRect* bounds = NULL) { |
| 160 | switch (pos) { |
| 161 | case SkTextBlob::kDefault_Positioning: { |
| 162 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRun(font, count, offset.x(), |
| 163 | offset.y(), bounds); |
| 164 | for (int i = 0; i < count; ++i) { |
| 165 | rb.glyphs[i] = i; |
| 166 | } |
| 167 | } break; |
| 168 | case SkTextBlob::kHorizontal_Positioning: { |
| 169 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPosH(font, count, offset.y(), |
| 170 | bounds); |
| 171 | for (int i = 0; i < count; ++i) { |
| 172 | rb.glyphs[i] = i; |
| 173 | rb.pos[i] = SkIntToScalar(i); |
| 174 | } |
| 175 | } break; |
| 176 | case SkTextBlob::kFull_Positioning: { |
| 177 | const SkTextBlobBuilder::RunBuffer& rb = builder.allocRunPos(font, count, bounds); |
| 178 | for (int i = 0; i < count; ++i) { |
| 179 | rb.glyphs[i] = i; |
| 180 | rb.pos[i * 2] = SkIntToScalar(i); |
| 181 | rb.pos[i * 2 + 1] = -SkIntToScalar(i); |
| 182 | } |
| 183 | } break; |
| 184 | default: |
| 185 | SkFAIL("unhandled positioning value"); |
| 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | DEF_TEST(TextBlob_builder, reporter) { |
| 191 | TextBlobTester::test_builder(reporter); |
| 192 | } |