File: font_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 8,881 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/font.h"

#include "cc/paint/paint_flags.h"
#include "third_party/blink/renderer/platform/fonts/font_variant_emoji.h"
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_shaper.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_view.h"
#include "third_party/blink/renderer/platform/fonts/text_fragment_paint_info.h"
#include "third_party/blink/renderer/platform/testing/font_test_base.h"
#include "third_party/blink/renderer/platform/testing/font_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/runtime_enabled_features_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/text/tab_size.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"

using blink::test::CreateTestFont;

namespace blink {

namespace {

Font* CreateVerticalUprightTestFont(const AtomicString& family_name,
                                    const String& font_path,
                                    float size) {
  return CreateTestFont(
      family_name, font_path, size, /* ligatures */ nullptr,
      kNormalVariantEmoji, [](FontDescription* font_description) {
        font_description->SetOrientation(FontOrientation::kVerticalUpright);
      });
}

}  // namespace

class FontTest : public FontTestBase {
 public:
  Font* CreateFontWithOrientation(const Font* base_font,
                                  FontOrientation orientation) {
    FontDescription font_description = base_font->GetFontDescription();
    font_description.SetOrientation(orientation);
    return MakeGarbageCollected<Font>(font_description);
  }
};

TEST_F(FontTest, FonteMetricsCapHeight) {
  const auto cap_height_of = [](const char* font_path, float size) {
    Font* font = CreateTestFont(AtomicString("test"),
                                test::PlatformTestDataPath(font_path), size);
    const SimpleFontData* const font_data = font->PrimaryFont();
    return font_data->GetFontMetrics().CapHeight();
  };

  EXPECT_FLOAT_EQ(80.0f, cap_height_of("Ahem.woff", 100));
  EXPECT_FLOAT_EQ(160.0f, cap_height_of("Ahem.woff", 200));

#if BUILDFLAG(IS_WIN)
  EXPECT_FLOAT_EQ(
      70.9961f, cap_height_of("third_party/Roboto/roboto-regular.woff2", 100));
  EXPECT_FLOAT_EQ(
      141.99219f,
      cap_height_of("third_party/Roboto/roboto-regular.woff2", 200));
#else
  EXPECT_FLOAT_EQ(
      71.09375f, cap_height_of("third_party/Roboto/roboto-regular.woff2", 100));
  EXPECT_FLOAT_EQ(
      142.1875f, cap_height_of("third_party/Roboto/roboto-regular.woff2", 200));
#endif
}

TEST_F(FontTest, ConvertBaseline) {
  Font* font = test::CreateAhemFont(100);
  const SimpleFontData* font_data = font->PrimaryFont();
  const FontMetrics& metrics = font_data->GetFontMetrics();
  EXPECT_EQ(metrics.FixedAscent(), 80);
  EXPECT_EQ(metrics.FixedDescent(), 20);
  EXPECT_EQ(metrics.FixedAlphabetic(FontBaseline::kAlphabeticBaseline), 0);
  EXPECT_EQ(metrics.FixedAlphabetic(FontBaseline::kCentralBaseline), -30);
  EXPECT_EQ(metrics.FixedCapHeight(FontBaseline::kAlphabeticBaseline), 80);
  EXPECT_EQ(metrics.FixedCapHeight(FontBaseline::kCentralBaseline), 50);
}

TEST_F(FontTest, IdeographicFullWidthAhem) {
  Font* font = CreateTestFont(AtomicString("Ahem"),
                              test::PlatformTestDataPath("Ahem.woff"), 16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  EXPECT_FALSE(font_data->IdeographicInlineSize().has_value());
}

TEST_F(FontTest, IdeographicFullWidthCjkFull) {
  Font* font = CreateTestFont(
      AtomicString("M PLUS 1p"),
      blink::test::BlinkWebTestsFontsTestDataPath("mplus-1p-regular.woff"), 16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  EXPECT_TRUE(font_data->IdeographicInlineSize().has_value());
  EXPECT_EQ(*font_data->IdeographicInlineSize(), 16);
}

TEST_F(FontTest, IdeographicFullWidthCjkNarrow) {
  ScopedNoFontAntialiasingForTest disable_no_font_antialiasing_for_test(false);

  Font* font = CreateTestFont(AtomicString("CSSHWOrientationTest"),
                              blink::test::BlinkWebTestsFontsTestDataPath(
                                  "adobe-fonts/CSSHWOrientationTest.otf"),
                              16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  EXPECT_TRUE(font_data->IdeographicInlineSize().has_value());
  EXPECT_FLOAT_EQ(*font_data->IdeographicInlineSize(), 8.0f);
}

// A font that does not have the CJK "water" glyph.
TEST_F(FontTest, IdeographicFullWidthUprightAhem) {
  Font* font = CreateTestFont(AtomicString("Ahem"),
                              test::PlatformTestDataPath("Ahem.woff"), 16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  EXPECT_FALSE(font_data->IdeographicInlineSize().has_value());
}

// A Japanese font, with the "water" glyph, but the `vmtx` table is missing.
TEST_F(FontTest, IdeographicFullWidthUprightCjkNoVmtx) {
  Font* font = CreateVerticalUprightTestFont(
      AtomicString("M PLUS 1p"),
      blink::test::BlinkWebTestsFontsTestDataPath("mplus-1p-regular.woff"), 16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  // If the `vmtx` table is missing, the vertical advance should be synthesized.
  ASSERT_TRUE(font_data->IdeographicInlineSize().has_value());
  EXPECT_EQ(*font_data->IdeographicInlineSize(),
            font_data->GetFontMetrics().Height());
}

// A Japanese font, with the "water" glyph, with the `vmtx` table.
TEST_F(FontTest, IdeographicFullWidthUprightCjkVmtx) {
  Font* font =
      CreateVerticalUprightTestFont(AtomicString("CSSHWOrientationTest"),
                                    blink::test::BlinkWebTestsFontsTestDataPath(
                                        "adobe-fonts/CSSHWOrientationTest.otf"),
                                    16);
  const SimpleFontData* font_data = font->PrimaryFont();
  ASSERT_TRUE(font_data);
  ASSERT_TRUE(font_data->IdeographicInlineSize().has_value());
  EXPECT_EQ(*font_data->IdeographicInlineSize(), 16);
}

TEST_F(FontTest, TextIntercepts) {
  Font* font = CreateTestFont(AtomicString("Ahem"),
                              test::PlatformTestDataPath("Ahem.woff"), 16);
  // A sequence of LATIN CAPITAL LETTER E WITH ACUTE and LATIN SMALL LETTER P
  // characters. E ACUTES are squares above the baseline in Ahem, while p's
  // are rectangles below the baseline.
  UChar ahem_above_below_baseline_string[] = {0xc9, 0x70, 0xc9, 0x70, 0xc9,
                                              0x70, 0xc9, 0x70, 0xc9};
  String ahem_above_below_baseline{
      base::span(ahem_above_below_baseline_string)};
  ShapeResult* shape_result = HarfBuzzShaper(ahem_above_below_baseline)
                                  .Shape(font, TextDirection::kLtr);
  TextFragmentPaintInfo text_paint_info{ahem_above_below_baseline, 0,
                                        ahem_above_below_baseline.length(),
                                        ShapeResultView::Create(shape_result)};
  cc::PaintFlags default_paint;

  std::tuple<float, float> below_baseline_bounds = std::make_tuple(2, 4);
  Vector<Font::TextIntercept> text_intercepts;
  // 4 intercept ranges for below baseline p glyphs in the test string
  font->GetTextIntercepts(text_paint_info, default_paint, below_baseline_bounds,
                          text_intercepts);
  EXPECT_EQ(text_intercepts.size(), 4u);
  for (auto text_intercept : text_intercepts) {
    EXPECT_GT(text_intercept.end_, text_intercept.begin_);
  }

  std::tuple<float, float> above_baseline_bounds = std::make_tuple(-4, -2);
  // 5 intercept ranges for the above baseline E ACUTE glyphs
  font->GetTextIntercepts(text_paint_info, default_paint, above_baseline_bounds,
                          text_intercepts);
  EXPECT_EQ(text_intercepts.size(), 5u);
  for (auto text_intercept : text_intercepts) {
    EXPECT_GT(text_intercept.end_, text_intercept.begin_);
  }
}

TEST_F(FontTest, TabWidthZero) {
  Font* font = CreateTestFont(AtomicString("Ahem"),
                              test::PlatformTestDataPath("Ahem.woff"), 0);
  TabSize tab_size(8);
  EXPECT_EQ(font->TabWidth(tab_size, .0f), .0f);
  EXPECT_EQ(font->TabWidth(tab_size, LayoutUnit()), LayoutUnit());
}

TEST_F(FontTest, NullifyPrimaryFontForTesting) {
  Font* font = CreateTestFont(AtomicString("Ahem"),
                              test::PlatformTestDataPath("Ahem.woff"), 0);
  EXPECT_TRUE(font->PrimaryFont());
  font->NullifyPrimaryFontForTesting();
  EXPECT_FALSE(font->PrimaryFont());
}

}  // namespace blink