blob: 8754b4964d61c4454aba9d2a2d15e58289e3a666 [file] [log] [blame]
benjaminwagnerf6bfccd2016-02-25 10:28:11 -08001/*
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 Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
Mike Reedda1b99f2018-12-02 22:21:14 -05009#include "SkFont.h"
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080010#include "SkPaint.h"
11#include "Test.h"
12
Mike Reedda1b99f2018-12-02 22:21:14 -050013static 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
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080026static 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 Reedda1b99f2018-12-02 22:21:14 -050038 const size_t n = break_text(paint, text, length, w, &m, reporter);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080039
Brian Salomon1c80e992018-01-29 09:50:47 -050040 REPORTER_ASSERT(reporter, n <= length, msg);
41 REPORTER_ASSERT(reporter, m <= width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080042
43 if (n == 0) {
Brian Salomon1c80e992018-01-29 09:50:47 -050044 REPORTER_ASSERT(reporter, m == 0, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080045 } else {
46 // now assert that we're monotonic
47 if (n == nn) {
Brian Salomon1c80e992018-01-29 09:50:47 -050048 REPORTER_ASSERT(reporter, m == mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080049 } else {
Brian Salomon1c80e992018-01-29 09:50:47 -050050 REPORTER_ASSERT(reporter, n > nn, msg);
51 REPORTER_ASSERT(reporter, m > mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080052 }
53 }
54 nn = n;
55 mm = m;
56 }
57}
58
59static 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 Reedda1b99f2018-12-02 22:21:14 -050068 const size_t length2 = break_text(paint, text, length, width, &mm, reporter);
Brian Salomon1c80e992018-01-29 09:50:47 -050069 REPORTER_ASSERT(reporter, length2 == length, msg);
70 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080071}
72
73static 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 Reedda1b99f2018-12-02 22:21:14 -050084 const size_t length = break_text(paint, text, kSize, width, &mm, reporter);
Brian Salomon1c80e992018-01-29 09:50:47 -050085 REPORTER_ASSERT(reporter, length == kSize, msg);
86 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080087}
88
89DEF_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}