blob: 38370e3eac9590909bd214461cfcedfd283caa0e [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 "Test.h"
11
Mike Reed70fbc362018-12-05 05:34:58 -050012static void test_monotonic(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080013 const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
14 const size_t length = strlen(text);
Mike Reed70fbc362018-12-05 05:34:58 -050015 const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080016
17 SkScalar mm = 0;
18 size_t nn = 0;
19 const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1);
20 for (SkScalar w = 0; w <= width; w += step) {
21 SkScalar m;
Mike Reed70fbc362018-12-05 05:34:58 -050022 const size_t n = font.breakText(text, length, kUTF8_SkTextEncoding, w, &m);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080023
Brian Salomon1c80e992018-01-29 09:50:47 -050024 REPORTER_ASSERT(reporter, n <= length, msg);
25 REPORTER_ASSERT(reporter, m <= width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080026
27 if (n == 0) {
Brian Salomon1c80e992018-01-29 09:50:47 -050028 REPORTER_ASSERT(reporter, m == 0, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080029 } else {
30 // now assert that we're monotonic
31 if (n == nn) {
Brian Salomon1c80e992018-01-29 09:50:47 -050032 REPORTER_ASSERT(reporter, m == mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080033 } else {
Brian Salomon1c80e992018-01-29 09:50:47 -050034 REPORTER_ASSERT(reporter, n > nn, msg);
35 REPORTER_ASSERT(reporter, m > mm, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080036 }
37 }
38 nn = n;
39 mm = m;
40 }
41}
42
Mike Reed70fbc362018-12-05 05:34:58 -050043static void test_eq_measure_text(skiatest::Reporter* reporter, const SkFont& font,
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080044 const char* msg) {
45 const char* text = "The ultimate measure of a man is not where he stands in moments of comfort "
46 "and convenience, but where he stands at times of challenge and controversy.";
47 const size_t length = strlen(text);
Mike Reed70fbc362018-12-05 05:34:58 -050048 const SkScalar width = font.measureText(text, length, kUTF8_SkTextEncoding);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080049
50 SkScalar mm;
Mike Reed70fbc362018-12-05 05:34:58 -050051 const size_t length2 = font.breakText(text, length, kUTF8_SkTextEncoding, width, &mm);
Brian Salomon1c80e992018-01-29 09:50:47 -050052 REPORTER_ASSERT(reporter, length2 == length, msg);
53 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080054}
55
Mike Reed70fbc362018-12-05 05:34:58 -050056static void test_long_text(skiatest::Reporter* reporter, const SkFont& font, const char* msg) {
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080057 static const int kSize = 16 * 1024;
58 SkAutoMalloc block(kSize);
59 memset(block.get(), 'a', kSize - 1);
60 char* text = static_cast<char*>(block.get());
61 text[kSize - 1] = '\0';
Mike Reed70fbc362018-12-05 05:34:58 -050062 const SkScalar width = font.measureText(text, kSize, kUTF8_SkTextEncoding);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080063
64 SkScalar mm;
Mike Reed70fbc362018-12-05 05:34:58 -050065 const size_t length = font.breakText(text, kSize, kUTF8_SkTextEncoding, width, &mm);
Brian Salomon1c80e992018-01-29 09:50:47 -050066 REPORTER_ASSERT(reporter, length == kSize, msg);
67 REPORTER_ASSERT(reporter, mm == width, msg);
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080068}
69
70DEF_TEST(PaintBreakText, reporter) {
Mike Reed70fbc362018-12-05 05:34:58 -050071 SkFont font;
72 test_monotonic(reporter, font, "default");
73 test_eq_measure_text(reporter, font, "default");
74 test_long_text(reporter, font, "default");
75 font.setSize(SkIntToScalar(1 << 17));
76 test_monotonic(reporter, font, "huge text size");
77 test_eq_measure_text(reporter, font, "huge text size");
78 font.setSize(0);
79 test_monotonic(reporter, font, "zero text size");
benjaminwagnerf6bfccd2016-02-25 10:28:11 -080080}