blob: d0b9c0827d173a999deea29f7adf53c52f6bddb3 [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"
benjaminwagnerf6bfccd2016-02-25 10:28:11 -08009#include "SkPaint.h"
10#include "Test.h"
11
12static void test_monotonic(skiatest::Reporter* reporter,
13 const SkPaint& paint,
14 const char* msg) {
15 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
16 const size_t length = strlen(text);
17 const SkScalar width = paint.measureText(text, length);
18
19 SkScalar mm = 0;
20 size_t nn = 0;
21 const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1);
22 for (SkScalar w = 0; w <= width; w += step) {
23 SkScalar m;
24 const size_t n = paint.breakText(text, length, w, &m);
25
Brian Salomon1c80e992018-01-29 09:50:47 -050026 REPORTER_ASSERT(reporter, n <= length, msg);
27 REPORTER_ASSERT(reporter, m <= width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080028
29 if (n == 0) {
Brian Salomon1c80e992018-01-29 09:50:47 -050030 REPORTER_ASSERT(reporter, m == 0, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080031 } else {
32 // now assert that we're monotonic
33 if (n == nn) {
Brian Salomon1c80e992018-01-29 09:50:47 -050034 REPORTER_ASSERT(reporter, m == mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080035 } else {
Brian Salomon1c80e992018-01-29 09:50:47 -050036 REPORTER_ASSERT(reporter, n > nn, msg);
37 REPORTER_ASSERT(reporter, m > mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080038 }
39 }
40 nn = n;
41 mm = m;
42 }
43}
44
45static void test_eq_measure_text(skiatest::Reporter* reporter,
46 const SkPaint& paint,
47 const char* msg) {
48 const char* text = "The ultimate measure of a man is not where he stands in moments of comfort "
49 "and convenience, but where he stands at times of challenge and controversy.";
50 const size_t length = strlen(text);
51 const SkScalar width = paint.measureText(text, length);
52
53 SkScalar mm;
54 const size_t length2 = paint.breakText(text, length, width, &mm);
Brian Salomon1c80e992018-01-29 09:50:47 -050055 REPORTER_ASSERT(reporter, length2 == length, msg);
56 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080057}
58
59static void test_long_text(skiatest::Reporter* reporter,
60 const SkPaint& paint,
61 const char* msg) {
62 static const int kSize = 16 * 1024;
63 SkAutoMalloc block(kSize);
64 memset(block.get(), 'a', kSize - 1);
65 char* text = static_cast<char*>(block.get());
66 text[kSize - 1] = '\0';
67 const SkScalar width = paint.measureText(text, kSize);
68
69 SkScalar mm;
70 const size_t length = paint.breakText(text, kSize, width, &mm);
Brian Salomon1c80e992018-01-29 09:50:47 -050071 REPORTER_ASSERT(reporter, length == kSize, msg);
72 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080073}
74
75DEF_TEST(PaintBreakText, reporter) {
76 SkPaint paint;
77 test_monotonic(reporter, paint, "default");
78 test_eq_measure_text(reporter, paint, "default");
79 test_long_text(reporter, paint, "default");
80 paint.setTextSize(SkIntToScalar(1 << 17));
81 test_monotonic(reporter, paint, "huge text size");
82 test_eq_measure_text(reporter, paint, "huge text size");
83 paint.setTextSize(0);
84 test_monotonic(reporter, paint, "zero text size");
85}