• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkAutoMalloc.h"
9 #include "SkPaint.h"
10 #include "Test.h"
11 
test_monotonic(skiatest::Reporter * reporter,const SkPaint & paint,const char * msg)12 static 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 
26         REPORTER_ASSERT_MESSAGE(reporter, n <= length, msg);
27         REPORTER_ASSERT_MESSAGE(reporter, m <= width, msg);
28 
29         if (n == 0) {
30             REPORTER_ASSERT_MESSAGE(reporter, m == 0, msg);
31         } else {
32             // now assert that we're monotonic
33             if (n == nn) {
34                 REPORTER_ASSERT_MESSAGE(reporter, m == mm, msg);
35             } else {
36                 REPORTER_ASSERT_MESSAGE(reporter, n > nn, msg);
37                 REPORTER_ASSERT_MESSAGE(reporter, m > mm, msg);
38             }
39         }
40         nn = n;
41         mm = m;
42     }
43 }
44 
test_eq_measure_text(skiatest::Reporter * reporter,const SkPaint & paint,const char * msg)45 static 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);
55     REPORTER_ASSERT_MESSAGE(reporter, length2 == length, msg);
56     REPORTER_ASSERT_MESSAGE(reporter, mm == width, msg);
57 }
58 
test_long_text(skiatest::Reporter * reporter,const SkPaint & paint,const char * msg)59 static 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);
71     REPORTER_ASSERT_MESSAGE(reporter, length == kSize, msg);
72     REPORTER_ASSERT_MESSAGE(reporter, mm == width, msg);
73 }
74 
DEF_TEST(PaintBreakText,reporter)75 DEF_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 }
86