blob: 8085e07c1bedccfc5b555034b9b0c502bb99adaf [file] [log] [blame]
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +00001/*
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
Mike Reedfe4fc142018-10-22 14:24:07 -040013static void test_cachedfont(skiatest::Reporter* reporter,
14 const SkPaint& paint, const SkFont& font) {
reed@google.com49270922014-05-30 17:15:23 +000015 // Currently SkFont resolves null into the default, so only test if paint's is not null
16 if (paint.getTypeface()) {
Mike Reedfe4fc142018-10-22 14:24:07 -040017 REPORTER_ASSERT(reporter, font.getTypeface() == paint.getTypeface());
reed@google.com49270922014-05-30 17:15:23 +000018 }
Mike Reedfe4fc142018-10-22 14:24:07 -040019 REPORTER_ASSERT(reporter, font.getSize() == paint.getTextSize());
20 REPORTER_ASSERT(reporter, font.getScaleX() == paint.getTextScaleX());
21 REPORTER_ASSERT(reporter, font.getSkewX() == paint.getTextSkewX());
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000022
Mike Reedfe4fc142018-10-22 14:24:07 -040023 uint32_t mask = SkPaint::kLinearText_Flag |
24 SkPaint::kSubpixelText_Flag |
25 SkPaint::kFakeBoldText_Flag |
26 SkPaint::kEmbeddedBitmapText_Flag |
27 SkPaint::kAutoHinting_Flag;
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000028
Mike Reedfe4fc142018-10-22 14:24:07 -040029 SkPaint p;
30 font.LEGACY_applyToPaint(&p);
31 uint32_t oldFlags = paint.getFlags() & mask;
32 uint32_t newFlags = p.getFlags() & mask;
33 REPORTER_ASSERT(reporter, oldFlags == newFlags);
34 REPORTER_ASSERT(reporter, paint.getHinting() == p.getHinting());
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000035}
36
Mike Reed02439492018-11-12 12:03:58 +000037static void test_fontmetrics(skiatest::Reporter* reporter,
38 const SkPaint& paint, const SkFont& font) {
39 SkFontMetrics fm0, fm1;
40 SkScalar h0 = paint.getFontMetrics(&fm0);
41 SkScalar h1 = font.getMetrics(&fm1);
42
43 REPORTER_ASSERT(reporter, h0 == h1);
44#define CMP(field) REPORTER_ASSERT(reporter, fm0.field == fm1.field)
45 CMP(fFlags);
46 CMP(fTop);
47 CMP(fAscent);
48 CMP(fDescent);
49 CMP(fBottom);
50 CMP(fLeading);
51#undef CMP
52}
53
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000054static void test_cachedfont(skiatest::Reporter* reporter) {
55 static const char* const faces[] = {
Mike Reedd017e512018-11-17 16:27:41 -050056 nullptr,
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000057 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
58 "Courier New", "Verdana", "monospace",
59 };
60
61 static const struct {
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000062 SkScalar fScaleX;
63 SkScalar fSkewX;
64 } gScaleRec[] = {
65 { SK_Scalar1, 0 },
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000066 { SK_Scalar1/2, -SK_Scalar1/4 },
67 };
68
69 SkPaint paint;
Mike Reedd017e512018-11-17 16:27:41 -050070 char txt[] = "long .text .with .lots .of.dots.";
71
72 unsigned mask = SkPaint::kAntiAlias_Flag |
73 SkPaint::kFakeBoldText_Flag |
74 SkPaint::kLinearText_Flag |
75 SkPaint::kSubpixelText_Flag |
76 SkPaint::kLCDRenderText_Flag |
77 SkPaint::kEmbeddedBitmapText_Flag |
78 SkPaint::kAutoHinting_Flag;
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000079
80 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
mbocee6a9912016-05-31 11:42:36 -070081 paint.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle()));
Mike Reedd017e512018-11-17 16:27:41 -050082 for (unsigned flags = 0; flags <= 0xFFF; ++flags) {
83 if (flags & ~mask) {
84 continue;
85 }
86 paint.setFlags(flags);
87 for (int hint = 0; hint <= 3; ++hint) {
88 paint.setHinting((SkFontHinting)hint);
89 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
90 paint.setTextScaleX(gScaleRec[k].fScaleX);
91 paint.setTextSkewX(gScaleRec[k].fSkewX);
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000092
Mike Reedd017e512018-11-17 16:27:41 -050093 const SkFont font(SkFont::LEGACY_ExtractFromPaint(paint));
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000094
Mike Reedd017e512018-11-17 16:27:41 -050095 test_cachedfont(reporter, paint, font);
96 test_fontmetrics(reporter, paint, font);
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +000097
Mike Reedd017e512018-11-17 16:27:41 -050098 SkRect pbounds, fbounds;
Mike Reedfe4fc142018-10-22 14:24:07 -040099
Mike Reedd017e512018-11-17 16:27:41 -0500100 // Requesting the bounds forces a generateMetrics call.
101 SkScalar pwidth = paint.measureText(txt, strlen(txt), &pbounds);
102 SkScalar fwidth = font.measureText(txt, strlen(txt), kUTF8_SkTextEncoding,
103 &fbounds);
104 REPORTER_ASSERT(reporter, pwidth == fwidth);
105 REPORTER_ASSERT(reporter, pbounds == fbounds);
106 }
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +0000107 }
108 }
109 }
110}
111
Mike Reedfe4fc142018-10-22 14:24:07 -0400112static void test_aa_hinting(skiatest::Reporter* reporter) {
113 SkPaint paint;
114
115 for (bool aa : {false, true}) {
116 paint.setAntiAlias(aa);
117 for (int hint = 0; hint <= 3; ++hint) {
Mike Reed9edbf422018-11-07 19:54:33 -0500118 paint.setHinting((SkFontHinting)hint);
Mike Reedfe4fc142018-10-22 14:24:07 -0400119 SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
120
121 SkPaint p2;
122 font.LEGACY_applyToPaint(&p2);
123 REPORTER_ASSERT(reporter, paint.isAntiAlias() == p2.isAntiAlias());
124 REPORTER_ASSERT(reporter, paint.getHinting() == p2.getHinting());
125 }
126 }
127}
128
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +0000129DEF_TEST(FontObj, reporter) {
130 test_cachedfont(reporter);
Mike Reedfe4fc142018-10-22 14:24:07 -0400131 test_aa_hinting(reporter);
commit-bot@chromium.org7ae034d2014-05-30 16:46:10 +0000132}
133
134// need tests for SkStrSearch