benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011-2016 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 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 8 | #include "SkAutoMalloc.h" |
Mike Reed | da1b99f | 2018-12-02 22:21:14 -0500 | [diff] [blame^] | 9 | #include "SkFont.h" |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 10 | #include "SkPaint.h" |
| 11 | #include "Test.h" |
| 12 | |
Mike Reed | da1b99f | 2018-12-02 22:21:14 -0500 | [diff] [blame^] | 13 | static size_t break_text(const SkPaint& paint, const void* text, size_t length, SkScalar maxw, |
| 14 | SkScalar* measuredw, skiatest::Reporter* reporter) { |
| 15 | size_t n = paint.breakText(text, length, maxw, measuredw); |
| 16 | SkScalar measuredw2; |
| 17 | size_t n2 = SkFont::LEGACY_ExtractFromPaint(paint).breakText(text, length, |
| 18 | (SkTextEncoding)paint.getTextEncoding(), maxw, &measuredw2); |
| 19 | REPORTER_ASSERT(reporter, n == n2); |
| 20 | if (measuredw) { |
| 21 | REPORTER_ASSERT(reporter, *measuredw == measuredw2); |
| 22 | } |
| 23 | return n; |
| 24 | } |
| 25 | |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 26 | static void test_monotonic(skiatest::Reporter* reporter, |
| 27 | const SkPaint& paint, |
| 28 | const char* msg) { |
| 29 | const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj"; |
| 30 | const size_t length = strlen(text); |
| 31 | const SkScalar width = paint.measureText(text, length); |
| 32 | |
| 33 | SkScalar mm = 0; |
| 34 | size_t nn = 0; |
| 35 | const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1); |
| 36 | for (SkScalar w = 0; w <= width; w += step) { |
| 37 | SkScalar m; |
Mike Reed | da1b99f | 2018-12-02 22:21:14 -0500 | [diff] [blame^] | 38 | const size_t n = break_text(paint, text, length, w, &m, reporter); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 39 | |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, n <= length, msg); |
| 41 | REPORTER_ASSERT(reporter, m <= width, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 42 | |
| 43 | if (n == 0) { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, m == 0, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 45 | } else { |
| 46 | // now assert that we're monotonic |
| 47 | if (n == nn) { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, m == mm, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 49 | } else { |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 50 | REPORTER_ASSERT(reporter, n > nn, msg); |
| 51 | REPORTER_ASSERT(reporter, m > mm, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | nn = n; |
| 55 | mm = m; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void test_eq_measure_text(skiatest::Reporter* reporter, |
| 60 | const SkPaint& paint, |
| 61 | const char* msg) { |
| 62 | const char* text = "The ultimate measure of a man is not where he stands in moments of comfort " |
| 63 | "and convenience, but where he stands at times of challenge and controversy."; |
| 64 | const size_t length = strlen(text); |
| 65 | const SkScalar width = paint.measureText(text, length); |
| 66 | |
| 67 | SkScalar mm; |
Mike Reed | da1b99f | 2018-12-02 22:21:14 -0500 | [diff] [blame^] | 68 | const size_t length2 = break_text(paint, text, length, width, &mm, reporter); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, length2 == length, msg); |
| 70 | REPORTER_ASSERT(reporter, mm == width, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void test_long_text(skiatest::Reporter* reporter, |
| 74 | const SkPaint& paint, |
| 75 | const char* msg) { |
| 76 | static const int kSize = 16 * 1024; |
| 77 | SkAutoMalloc block(kSize); |
| 78 | memset(block.get(), 'a', kSize - 1); |
| 79 | char* text = static_cast<char*>(block.get()); |
| 80 | text[kSize - 1] = '\0'; |
| 81 | const SkScalar width = paint.measureText(text, kSize); |
| 82 | |
| 83 | SkScalar mm; |
Mike Reed | da1b99f | 2018-12-02 22:21:14 -0500 | [diff] [blame^] | 84 | const size_t length = break_text(paint, text, kSize, width, &mm, reporter); |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, length == kSize, msg); |
| 86 | REPORTER_ASSERT(reporter, mm == width, msg); |
benjaminwagner | f6bfccd | 2016-02-25 10:28:11 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | DEF_TEST(PaintBreakText, reporter) { |
| 90 | SkPaint paint; |
| 91 | test_monotonic(reporter, paint, "default"); |
| 92 | test_eq_measure_text(reporter, paint, "default"); |
| 93 | test_long_text(reporter, paint, "default"); |
| 94 | paint.setTextSize(SkIntToScalar(1 << 17)); |
| 95 | test_monotonic(reporter, paint, "huge text size"); |
| 96 | test_eq_measure_text(reporter, paint, "huge text size"); |
| 97 | paint.setTextSize(0); |
| 98 | test_monotonic(reporter, paint, "zero text size"); |
| 99 | } |