Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 "src/core/SkEnumerate.h" |
| 9 | #include "src/core/SkGlyphBuffer.h" |
| 10 | #include "src/core/SkGlyphRunPainter.h" |
| 11 | #include "src/core/SkScalerContext.h" |
| 12 | #include "tests/Test.h" |
| 13 | |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 14 | DEF_TEST(SkPackedGlyphIDCtor, reporter) { |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 15 | using PG = SkPackedGlyphID; |
| 16 | // x and y are in one quarter the sub-pixel sampling frequency. |
| 17 | // Number of steps on the interval [0, 1) |
| 18 | const int perUnit = 1u << (PG::kSubPixelPosLen + 2); |
| 19 | const float step = 1.f / perUnit; |
| 20 | const int testLimit = 2 * perUnit; |
| 21 | auto freqRound = [](uint32_t x) -> uint32_t { |
| 22 | return ((x + 2) >> 2) & PG::kSubPixelPosMask; |
| 23 | }; |
| 24 | |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 25 | { |
| 26 | // Normal sub-pixel with y-axis snapping. |
| 27 | auto roundingSpec = SkGlyphPositionRoundingSpec(true, kX_SkAxisAlignment); |
| 28 | SkIPoint mask = roundingSpec.ignorePositionFieldMask; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 29 | for (int x = -testLimit; x < testLimit; x++) { |
| 30 | float fx = x * step; |
| 31 | SkPoint roundedPos = SkPoint{fx, 0} + roundingSpec.halfAxisSampleFreq; |
| 32 | SkPackedGlyphID packedID{3, roundedPos, mask}; |
| 33 | uint32_t subX = freqRound(x); |
| 34 | uint32_t subY = 0; |
| 35 | SkPackedGlyphID correctID(3, subX, subY); |
| 36 | SkASSERT(packedID == correctID); |
| 37 | REPORTER_ASSERT(reporter, packedID == correctID); |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | { |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 42 | // No subpixel positioning. |
| 43 | auto roundingSpec = SkGlyphPositionRoundingSpec(false, kNone_SkAxisAlignment); |
| 44 | SkIPoint mask = roundingSpec.ignorePositionFieldMask; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 45 | for (int y = -testLimit; y < testLimit; y++) { |
| 46 | for (int x = -testLimit; x < testLimit; x++) { |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 47 | float fx = x * step, fy = y * step; |
| 48 | SkPoint roundedPos = SkPoint{fx, fy} + roundingSpec.halfAxisSampleFreq; |
| 49 | SkPackedGlyphID packedID{3, roundedPos, mask}; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 50 | uint32_t subX = 0; |
| 51 | uint32_t subY = 0; |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 52 | SkPackedGlyphID correctID(3, subX, subY); |
| 53 | REPORTER_ASSERT(reporter, packedID == correctID); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | { |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 59 | // Subpixel with no axis snapping. |
| 60 | auto roundingSpec = SkGlyphPositionRoundingSpec(true, kNone_SkAxisAlignment); |
| 61 | SkIPoint mask = roundingSpec.ignorePositionFieldMask; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 62 | for (int y = -testLimit; y < testLimit; y++) { |
| 63 | for (int x = -testLimit; x < testLimit; x++) { |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 64 | float fx = x * step, fy = y * step; |
| 65 | SkPoint roundedPos = SkPoint{fx, fy} + roundingSpec.halfAxisSampleFreq; |
| 66 | SkPackedGlyphID packedID{3, roundedPos, mask}; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 67 | uint32_t subX = freqRound(x); |
| 68 | uint32_t subY = freqRound(y); |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 69 | SkPackedGlyphID correctID(3, subX, subY); |
| 70 | REPORTER_ASSERT(reporter, packedID == correctID); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | { |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 76 | // Test dynamic range by transposing a large distance. |
| 77 | // Floating point numbers have 24 bits of precision. The largest distance is 24 - 2 (for |
| 78 | // sub-pixel) - 1 (for truncation to floor trick in the code). This leaves 21 bits. Large |
| 79 | // Distance is 2^21 - 2 (because the test is on the interval [-2, 2). |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 80 | const uint32_t kLogLargeDistance = 24 - PG::kSubPixelPosLen - 1; |
| 81 | const int64_t kLargeDistance = (1ull << kLogLargeDistance) - 2; |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 82 | auto roundingSpec = SkGlyphPositionRoundingSpec(true, kNone_SkAxisAlignment); |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 83 | SkIPoint mask = roundingSpec.ignorePositionFieldMask; |
| 84 | for (int y = -32; y < 33; y++) { |
| 85 | for (int x = -32; x < 33; x++) { |
Herb Derby | f7ce19e | 2019-11-06 13:04:54 -0500 | [diff] [blame] | 86 | float fx = x * step + kLargeDistance, fy = y * step + kLargeDistance; |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 87 | SkPoint roundedPos = SkPoint{fx, fy} + roundingSpec.halfAxisSampleFreq; |
| 88 | SkPackedGlyphID packedID{3, roundedPos, mask}; |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 89 | uint32_t subX = freqRound(x); |
| 90 | uint32_t subY = freqRound(y); |
Herb Derby | f5ad3f4 | 2019-10-30 16:33:16 -0400 | [diff] [blame] | 91 | SkPackedGlyphID correctID(3, subX, subY); |
| 92 | REPORTER_ASSERT(reporter, packedID == correctID); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 97 | |
| 98 | DEF_TEST(SkSourceGlyphBufferBasic, reporter) { |
| 99 | SkSourceGlyphBuffer rejects; |
| 100 | // Positions are picked to avoid precision problems. |
| 101 | const SkPoint positions[] = {{10.25,10.25}, {20.5,10.25}, {30.75,10.25}, {40,10.25}}; |
| 102 | const SkGlyphID glyphIDs[] = {1, 2, 3, 4}; |
| 103 | auto source = SkMakeZip(glyphIDs, positions); |
| 104 | |
| 105 | rejects.setSource(source); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 106 | for (auto [i, glyphID, pos] : SkMakeEnumerate(rejects.source())) { |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, glyphID == std::get<0>(source[i])); |
| 108 | REPORTER_ASSERT(reporter, pos == std::get<1>(source[i])); |
| 109 | } |
| 110 | // Reject a couple of glyphs. |
| 111 | rejects.reject(1); |
| 112 | rejects.reject(2, 100); |
| 113 | rejects.flipRejectsToSource(); |
| 114 | REPORTER_ASSERT(reporter, rejects.rejectedMaxDimension() == 100); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 115 | for (auto [i, glyphID, pos] : SkMakeEnumerate(rejects.source())) { |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 116 | // This will index 1 and 2 from the original source. |
| 117 | size_t j = i + 1; |
| 118 | REPORTER_ASSERT(reporter, glyphID == std::get<0>(source[j])); |
| 119 | REPORTER_ASSERT(reporter, pos == std::get<1>(source[j])); |
| 120 | } |
| 121 | |
| 122 | // Reject an additional glyph |
| 123 | rejects.reject(0, 10); |
| 124 | rejects.flipRejectsToSource(); |
| 125 | REPORTER_ASSERT(reporter, rejects.rejectedMaxDimension() == 10); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 126 | for (auto [i, glyphID, pos] : SkMakeEnumerate(rejects.source())) { |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 127 | // This will index 1 from the original source. |
| 128 | size_t j = i + 1; |
| 129 | REPORTER_ASSERT(reporter, glyphID == std::get<0>(source[j])); |
| 130 | REPORTER_ASSERT(reporter, pos == std::get<1>(source[j])); |
| 131 | } |
| 132 | |
| 133 | // Start all over |
| 134 | rejects.setSource(source); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 135 | for (auto [i, glyphID, pos] : SkMakeEnumerate(rejects.source())) { |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, glyphID == std::get<0>(source[i])); |
| 137 | REPORTER_ASSERT(reporter, pos == std::get<1>(source[i])); |
| 138 | } |
| 139 | |
| 140 | // Check that everything is working after calling setSource. |
| 141 | rejects.reject(1); |
| 142 | rejects.reject(2, 100); |
| 143 | rejects.flipRejectsToSource(); |
| 144 | REPORTER_ASSERT(reporter, rejects.rejectedMaxDimension() == 100); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 145 | for (auto [i, glyphID, pos] : SkMakeEnumerate(rejects.source())) { |
Herbert Derby | d7689d4 | 2019-10-11 10:56:31 -0400 | [diff] [blame] | 146 | // This will index 1 and 2 from the original source. |
| 147 | size_t j = i + 1; |
| 148 | REPORTER_ASSERT(reporter, glyphID == std::get<0>(source[j])); |
| 149 | REPORTER_ASSERT(reporter, pos == std::get<1>(source[j])); |
| 150 | } |
| 151 | } |
| 152 | |
Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 153 | DEF_TEST(SkDrawableGlyphBufferBasic, reporter) { |
| 154 | // Positions are picked to avoid precision problems. |
| 155 | const SkPoint positions[] = {{10.25,10.25}, {20.5,10.25}, {30.75,10.25}, {40,10.25}}; |
| 156 | const SkGlyphID glyphIDs[] = {1, 2, 3, 4}; |
| 157 | SkGlyph glyphs[100]; |
| 158 | auto source = SkMakeZip(glyphIDs, positions); |
| 159 | |
| 160 | { |
| 161 | SkDrawableGlyphBuffer drawable; |
| 162 | drawable.ensureSize(100); |
Herb Derby | 79c5674 | 2020-05-14 12:26:25 -0400 | [diff] [blame] | 163 | drawable.startSource(source); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 164 | for (auto [i, packedID, pos] : SkMakeEnumerate(drawable.input())) { |
Herb Derby | c7a8df8 | 2019-10-30 11:58:18 -0400 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, packedID.packedID().glyphID() == glyphIDs[i]); |
Herb Derby | 79c5674 | 2020-05-14 12:26:25 -0400 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, pos == positions[i]); |
Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | SkDrawableGlyphBuffer drawable; |
| 172 | drawable.ensureSize(100); |
| 173 | SkMatrix matrix = SkMatrix::MakeScale(0.5); |
| 174 | SkGlyphPositionRoundingSpec rounding{true, kX_SkAxisAlignment}; |
Herb Derby | e7825ff | 2020-05-15 10:08:49 -0400 | [diff] [blame] | 175 | drawable.startBitmapDevice(source, {100, 100}, matrix, rounding); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 176 | for (auto [i, packedID, pos] : SkMakeEnumerate(drawable.input())) { |
Herb Derby | 95ea4c4 | 2019-09-30 16:26:52 -0400 | [diff] [blame] | 177 | REPORTER_ASSERT(reporter, glyphIDs[i] == packedID.packedID().glyphID()); |
Herb Derby | 2959749 | 2019-11-06 15:47:43 -0500 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, |
| 179 | pos.x() == positions[i].x() * 0.5 + 50 + SkPackedGlyphID::kSubpixelRound); |
Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 180 | REPORTER_ASSERT(reporter, pos.y() == positions[i].y() * 0.5 + 50 + 0.5); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | { |
| 185 | SkDrawableGlyphBuffer drawable; |
| 186 | drawable.ensureSize(100); |
Herb Derby | 79c5674 | 2020-05-14 12:26:25 -0400 | [diff] [blame] | 187 | drawable.startSource(source); |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 188 | for (auto [i, packedID, pos] : SkMakeEnumerate(drawable.input())) { |
Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 189 | drawable.push_back(&glyphs[i], i); |
| 190 | } |
Herb Derby | 81777ac | 2019-11-14 13:51:43 -0500 | [diff] [blame] | 191 | for (auto [i, glyph, pos] : SkMakeEnumerate(drawable.drawable())) { |
Herbert Derby | 0be4c2c | 2019-10-09 12:26:56 -0400 | [diff] [blame] | 192 | REPORTER_ASSERT(reporter, glyph.glyph() == &glyphs[i]); |
| 193 | } |
| 194 | } |
| 195 | } |