PaintBreakTextTest.cpp revision 7ea5cb18389db11a94175de95c9db2b44972630c
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
8#include "SkPaint.h"
9#include "Test.h"
10
11static void test_monotonic(skiatest::Reporter* reporter,
12                           const SkPaint& paint,
13                           const char* msg) {
14    const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
15    const size_t length = strlen(text);
16    const SkScalar width = paint.measureText(text, length);
17
18    SkScalar mm = 0;
19    size_t nn = 0;
20    const SkScalar step = SkMaxScalar(width / 10, SK_Scalar1);
21    for (SkScalar w = 0; w <= width; w += step) {
22        SkScalar m;
23        const size_t n = paint.breakText(text, length, w, &m);
24
25        REPORTER_ASSERT_MESSAGE(reporter, n <= length, msg);
26        REPORTER_ASSERT_MESSAGE(reporter, m <= width, msg);
27
28        if (n == 0) {
29            REPORTER_ASSERT_MESSAGE(reporter, m == 0, msg);
30        } else {
31            // now assert that we're monotonic
32            if (n == nn) {
33                REPORTER_ASSERT_MESSAGE(reporter, m == mm, msg);
34            } else {
35                REPORTER_ASSERT_MESSAGE(reporter, n > nn, msg);
36                REPORTER_ASSERT_MESSAGE(reporter, m > mm, msg);
37            }
38        }
39        nn = n;
40        mm = m;
41    }
42}
43
44static void test_eq_measure_text(skiatest::Reporter* reporter,
45                                 const SkPaint& paint,
46                                 const char* msg) {
47    const char* text = "The ultimate measure of a man is not where he stands in moments of comfort "
48        "and convenience, but where he stands at times of challenge and controversy.";
49    const size_t length = strlen(text);
50    const SkScalar width = paint.measureText(text, length);
51
52    SkScalar mm;
53    const size_t length2 = paint.breakText(text, length, width, &mm);
54    REPORTER_ASSERT_MESSAGE(reporter, length2 == length, msg);
55    REPORTER_ASSERT_MESSAGE(reporter, mm == width, msg);
56}
57
58static void test_long_text(skiatest::Reporter* reporter,
59                           const SkPaint& paint,
60                           const char* msg) {
61    static const int kSize = 16 * 1024;
62    SkAutoMalloc block(kSize);
63    memset(block.get(), 'a', kSize - 1);
64    char* text = static_cast<char*>(block.get());
65    text[kSize - 1] = '\0';
66    const SkScalar width = paint.measureText(text, kSize);
67
68    SkScalar mm;
69    const size_t length = paint.breakText(text, kSize, width, &mm);
70    REPORTER_ASSERT_MESSAGE(reporter, length == kSize, msg);
71    REPORTER_ASSERT_MESSAGE(reporter, mm == width, msg);
72}
73
74DEF_TEST(PaintBreakText, reporter) {
75    SkPaint paint;
76    test_monotonic(reporter, paint, "default");
77    test_eq_measure_text(reporter, paint, "default");
78    test_long_text(reporter, paint, "default");
79    paint.setTextSize(SkIntToScalar(1 << 17));
80    test_monotonic(reporter, paint, "huge text size");
81    test_eq_measure_text(reporter, paint, "huge text size");
82    paint.setTextSize(0);
83    test_monotonic(reporter, paint, "zero text size");
84    paint.setVerticalText(true);
85    paint.setTextSize(SkIntToScalar(100));
86    test_monotonic(reporter, paint, "vertical");
87    test_eq_measure_text(reporter, paint, "vertical");
88}
89