1 /*
2 * Copyright 2014 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 "SkFont.h"
9 #include "SkPaint.h"
10 #include "SkTypeface.h"
11 #include "Test.h"
12
is_use_nonlinear_metrics(const SkPaint & paint)13 static bool is_use_nonlinear_metrics(const SkPaint& paint) {
14 return !paint.isSubpixelText() && !paint.isLinearText();
15 }
16
is_enable_auto_hints(const SkPaint & paint)17 static bool is_enable_auto_hints(const SkPaint& paint) {
18 return paint.isAutohinted();
19 }
20
is_enable_bytecode_hints(const SkPaint & paint)21 static bool is_enable_bytecode_hints(const SkPaint& paint) {
22 return paint.getHinting() >= SkPaint::kFull_Hinting;
23 }
24
test_cachedfont(skiatest::Reporter * reporter,const SkPaint & paint)25 static void test_cachedfont(skiatest::Reporter* reporter, const SkPaint& paint) {
26 SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
27
28 // Currently SkFont resolves null into the default, so only test if paint's is not null
29 if (paint.getTypeface()) {
30 REPORTER_ASSERT(reporter, font->getTypeface() == paint.getTypeface());
31 }
32 REPORTER_ASSERT(reporter, font->getSize() == paint.getTextSize());
33 REPORTER_ASSERT(reporter, font->getScaleX() == paint.getTextScaleX());
34 REPORTER_ASSERT(reporter, font->getSkewX() == paint.getTextSkewX());
35
36 REPORTER_ASSERT(reporter, font->isVertical() == paint.isVerticalText());
37 REPORTER_ASSERT(reporter, font->isEmbolden() == paint.isFakeBoldText());
38
39 REPORTER_ASSERT(reporter, font->isUseNonLinearMetrics() == is_use_nonlinear_metrics(paint));
40 REPORTER_ASSERT(reporter, font->isEnableAutoHints() == is_enable_auto_hints(paint));
41 REPORTER_ASSERT(reporter, font->isEnableByteCodeHints() == is_enable_bytecode_hints(paint));
42 }
43
test_cachedfont(skiatest::Reporter * reporter)44 static void test_cachedfont(skiatest::Reporter* reporter) {
45 static const char* const faces[] = {
46 nullptr, // default font
47 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
48 "Courier New", "Verdana", "monospace",
49 };
50
51 static const struct {
52 SkPaint::Hinting hinting;
53 unsigned flags;
54 } settings[] = {
55 { SkPaint::kNo_Hinting, 0 },
56 { SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
57 { SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
58 { SkPaint::kSlight_Hinting, 0 },
59 { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
60 { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
61 { SkPaint::kNormal_Hinting, 0 },
62 { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
63 { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
64 };
65
66 static const struct {
67 SkScalar fScaleX;
68 SkScalar fSkewX;
69 } gScaleRec[] = {
70 { SK_Scalar1, 0 },
71 { SK_Scalar1/2, 0 },
72 // these two exercise obliquing (skew)
73 { SK_Scalar1, -SK_Scalar1/4 },
74 { SK_Scalar1/2, -SK_Scalar1/4 },
75 };
76
77 SkPaint paint;
78 char txt[] = "long.text.with.lots.of.dots.";
79
80 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
81 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
82 paint.setTypeface(face);
83
84 for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
85 paint.setHinting(settings[j].hinting);
86 paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
87 paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
88
89 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
90 paint.setTextScaleX(gScaleRec[k].fScaleX);
91 paint.setTextSkewX(gScaleRec[k].fSkewX);
92
93 test_cachedfont(reporter, paint);
94
95 SkRect bounds;
96
97 // For no hinting and light hinting this should take the
98 // optimized generateAdvance path.
99 SkScalar width1 = paint.measureText(txt, strlen(txt));
100
101 // Requesting the bounds forces a generateMetrics call.
102 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
103
104 REPORTER_ASSERT(reporter, width1 == width2);
105
106 SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
107 SkScalar font_width1 = font->measureText(txt, strlen(txt), kUTF8_SkTextEncoding);
108 // measureText not yet implemented...
109 REPORTER_ASSERT(reporter, font_width1 == -1);
110 // REPORTER_ASSERT(reporter, width1 == font_width1);
111 }
112 }
113 }
114 }
115
DEF_TEST(FontObj,reporter)116 DEF_TEST(FontObj, reporter) {
117 test_cachedfont(reporter);
118 }
119
120 // need tests for SkStrSearch
121