summaryrefslogtreecommitdiffstats
path: root/ui/gfx/font_render_params_linux_unittest.cc
blob: 46a4fcfaac27f036693448205271b265cc92db1e (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/font_render_params.h"

#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"
#include "ui/gfx/linux_font_delegate.h"
#include "ui/gfx/pango_util.h"
#include "ui/gfx/test/fontconfig_util_linux.h"

namespace gfx {

namespace {

// Implementation of LinuxFontDelegate that returns a canned FontRenderParams
// struct. This is used to isolate tests from the system's local configuration.
class TestFontDelegate : public LinuxFontDelegate {
 public:
  TestFontDelegate() {}
  ~TestFontDelegate() override {}

  void set_params(const FontRenderParams& params) { params_ = params; }

  FontRenderParams GetDefaultFontRenderParams() const override {
    return params_;
  }
  scoped_ptr<ScopedPangoFontDescription> GetDefaultPangoFontDescription()
      const override {
    NOTIMPLEMENTED();
    return nullptr;
  }
  double GetFontDPI() const override {
    NOTIMPLEMENTED();
    return 96.0;
  }

 private:
  FontRenderParams params_;

  DISALLOW_COPY_AND_ASSIGN(TestFontDelegate);
};

// Loads the first system font defined by fontconfig_util_linux.h with a base
// filename of |basename|. Case is ignored.
bool LoadSystemFont(const std::string& basename) {
  for (size_t i = 0; i < kNumSystemFontsForFontconfig; ++i) {
    base::FilePath path(gfx::kSystemFontsForFontconfig[i]);
    if (strcasecmp(path.BaseName().value().c_str(), basename.c_str()) == 0)
      return LoadFontIntoFontconfig(path);
  }
  LOG(ERROR) << "Unable to find system font named " << basename;
  return false;
}

}  // namespace

class FontRenderParamsTest : public testing::Test {
 public:
  FontRenderParamsTest() {
    SetUpFontconfig();
    CHECK(temp_dir_.CreateUniqueTempDir());
    original_font_delegate_ = LinuxFontDelegate::instance();
    LinuxFontDelegate::SetInstance(&test_font_delegate_);
    ClearFontRenderParamsCacheForTest();
  }

  ~FontRenderParamsTest() override {
    LinuxFontDelegate::SetInstance(
        const_cast<LinuxFontDelegate*>(original_font_delegate_));
    TearDownFontconfig();
  }

 protected:
  base::ScopedTempDir temp_dir_;
  const LinuxFontDelegate* original_font_delegate_;
  TestFontDelegate test_font_delegate_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FontRenderParamsTest);
};

TEST_F(FontRenderParamsTest, Default) {
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      CreateFontconfigEditStanza("autohint", "bool", "true") +
      CreateFontconfigEditStanza("hinting", "bool", "true") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintslight") +
      CreateFontconfigEditStanza("rgba", "const", "rgb") +
      kFontconfigMatchFooter +
      // Add a font match for Arial; it shouldn't be used when querying for
      // default settings: http://crbug.com/421247
      kFontconfigMatchFontHeader +
      CreateFontconfigTestStanza("family", "eq", "string", "Arial") +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      CreateFontconfigEditStanza("autohint", "bool", "false") +
      CreateFontconfigEditStanza("hinting", "bool", "true") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintfull") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  FontRenderParams params = GetFontRenderParams(
      FontRenderParamsQuery(true), NULL);
  EXPECT_TRUE(params.antialiasing);
  EXPECT_TRUE(params.autohinter);
  EXPECT_TRUE(params.use_bitmaps);
  EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting);
  EXPECT_FALSE(params.subpixel_positioning);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB,
            params.subpixel_rendering);
}

TEST_F(FontRenderParamsTest, Size) {
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      CreateFontconfigEditStanza("hinting", "bool", "true") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintfull") +
      CreateFontconfigEditStanza("rgba", "const", "none") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("pixelsize", "less_eq", "double", "10") +
      CreateFontconfigEditStanza("antialias", "bool", "false") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("size", "more_eq", "double", "20") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintslight") +
      CreateFontconfigEditStanza("rgba", "const", "rgb") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  // The defaults should be used when the supplied size isn't matched by the
  // second or third blocks.
  FontRenderParamsQuery query(false);
  query.pixel_size = 12;
  FontRenderParams params = GetFontRenderParams(query, NULL);
  EXPECT_TRUE(params.antialiasing);
  EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE,
            params.subpixel_rendering);

  query.pixel_size = 10;
  params = GetFontRenderParams(query, NULL);
  EXPECT_FALSE(params.antialiasing);
  EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE,
            params.subpixel_rendering);

  query.pixel_size = 0;
  query.point_size = 20;
  params = GetFontRenderParams(query, NULL);
  EXPECT_TRUE(params.antialiasing);
  EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB,
            params.subpixel_rendering);
}

TEST_F(FontRenderParamsTest, Style) {
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  // Load a config that disables subpixel rendering for bold text and disables
  // hinting for italic text.
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      CreateFontconfigEditStanza("hinting", "bool", "true") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintslight") +
      CreateFontconfigEditStanza("rgba", "const", "rgb") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("weight", "eq", "const", "bold") +
      CreateFontconfigEditStanza("rgba", "const", "none") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("slant", "eq", "const", "italic") +
      CreateFontconfigEditStanza("hinting", "bool", "false") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  FontRenderParamsQuery query(false);
  query.style = Font::NORMAL;
  FontRenderParams params = GetFontRenderParams(query, NULL);
  EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB,
            params.subpixel_rendering);

  query.style = Font::BOLD;
  params = GetFontRenderParams(query, NULL);
  EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE,
            params.subpixel_rendering);

  query.style = Font::ITALIC;
  params = GetFontRenderParams(query, NULL);
  EXPECT_EQ(FontRenderParams::HINTING_NONE, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB,
            params.subpixel_rendering);

  query.style = Font::BOLD | Font::ITALIC;
  params = GetFontRenderParams(query, NULL);
  EXPECT_EQ(FontRenderParams::HINTING_NONE, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE,
            params.subpixel_rendering);
}

TEST_F(FontRenderParamsTest, Scalable) {
  // Load a config that only enables antialiasing for scalable fonts.
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "false") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("scalable", "eq", "bool", "true") +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  // Check that we specifically ask how scalable fonts should be rendered.
  FontRenderParams params = GetFontRenderParams(
      FontRenderParamsQuery(false), NULL);
  EXPECT_TRUE(params.antialiasing);
}

TEST_F(FontRenderParamsTest, UseBitmaps) {
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  // Load a config that enables embedded bitmaps for fonts <= 10 pixels.
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("embeddedbitmap", "bool", "false") +
      kFontconfigMatchFooter +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("pixelsize", "less_eq", "double", "10") +
      CreateFontconfigEditStanza("embeddedbitmap", "bool", "true") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  FontRenderParamsQuery query(false);
  FontRenderParams params = GetFontRenderParams(query, NULL);
  EXPECT_FALSE(params.use_bitmaps);

  query.pixel_size = 5;
  params = GetFontRenderParams(query, NULL);
  EXPECT_TRUE(params.use_bitmaps);
}

TEST_F(FontRenderParamsTest, ForceFullHintingWhenAntialiasingIsDisabled) {
  // Load a config that disables antialiasing and hinting while requesting
  // subpixel rendering.
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "false") +
      CreateFontconfigEditStanza("hinting", "bool", "false") +
      CreateFontconfigEditStanza("hintstyle", "const", "hintnone") +
      CreateFontconfigEditStanza("rgba", "const", "rgb") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  // Full hinting should be forced. See the comment in GetFontRenderParams() for
  // more information.
  FontRenderParams params = GetFontRenderParams(
      FontRenderParamsQuery(false), NULL);
  EXPECT_FALSE(params.antialiasing);
  EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting);
  EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE,
            params.subpixel_rendering);
  EXPECT_FALSE(params.subpixel_positioning);
}

#if defined(OS_CHROMEOS)
TEST_F(FontRenderParamsTest, ForceSubpixelPositioning) {
  {
    FontRenderParams params =
        GetFontRenderParams(FontRenderParamsQuery(false), NULL);
    EXPECT_TRUE(params.antialiasing);
    EXPECT_FALSE(params.subpixel_positioning);
    SetFontRenderParamsDeviceScaleFactor(1.0f);
  }
  ClearFontRenderParamsCacheForTest();
  SetFontRenderParamsDeviceScaleFactor(1.25f);
  // Subpixel positioning should be forced.
  {
    FontRenderParams params =
        GetFontRenderParams(FontRenderParamsQuery(false), NULL);
    EXPECT_TRUE(params.antialiasing);
    EXPECT_TRUE(params.subpixel_positioning);
    SetFontRenderParamsDeviceScaleFactor(1.0f);
  }
}
#endif

TEST_F(FontRenderParamsTest, OnlySetConfiguredValues) {
  // Configure the LinuxFontDelegate (which queries GtkSettings on desktop
  // Linux) to request subpixel rendering.
  FontRenderParams system_params;
  system_params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
  test_font_delegate_.set_params(system_params);

  // Load a Fontconfig config that enables antialiasing but doesn't say anything
  // about subpixel rendering.
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      kFontconfigMatchPatternHeader +
      CreateFontconfigEditStanza("antialias", "bool", "true") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  // The subpixel rendering setting from the delegate should make it through.
  FontRenderParams params = GetFontRenderParams(
      FontRenderParamsQuery(false), NULL);
  EXPECT_EQ(system_params.subpixel_rendering, params.subpixel_rendering);
}

TEST_F(FontRenderParamsTest, NoFontconfigMatch) {
  // Don't load a Fontconfig configuration.
  FontRenderParams system_params;
  system_params.antialiasing = true;
  system_params.hinting = FontRenderParams::HINTING_MEDIUM;
  system_params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
  test_font_delegate_.set_params(system_params);

  FontRenderParamsQuery query(false);
  query.families.push_back("Arial");
  query.families.push_back("Times New Roman");
  query.pixel_size = 10;
  std::string suggested_family;
  FontRenderParams params = GetFontRenderParams(query, &suggested_family);

  // The system params and the first requested family should be returned.
  EXPECT_EQ(system_params.antialiasing, params.antialiasing);
  EXPECT_EQ(system_params.hinting, params.hinting);
  EXPECT_EQ(system_params.subpixel_rendering, params.subpixel_rendering);
  EXPECT_EQ(query.families[0], suggested_family);
}

TEST_F(FontRenderParamsTest, MissingFamily) {
  // With Arial and Verdana installed, request (in order) Helvetica, Arial, and
  // Verdana and check that Arial is returned.
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  ASSERT_TRUE(LoadSystemFont("verdana.ttf"));
  FontRenderParamsQuery query(false);
  query.families.push_back("Helvetica");
  query.families.push_back("Arial");
  query.families.push_back("Verdana");
  std::string suggested_family;
  GetFontRenderParams(query, &suggested_family);
  EXPECT_EQ("Arial", suggested_family);
}

TEST_F(FontRenderParamsTest, SubstituteFamily) {
  // Configure Fontconfig to use Verdana for both Helvetica and Arial.
  ASSERT_TRUE(LoadSystemFont("arial.ttf"));
  ASSERT_TRUE(LoadSystemFont("verdana.ttf"));
  ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(),
      std::string(kFontconfigFileHeader) +
      CreateFontconfigAliasStanza("Helvetica", "Verdana") +
      kFontconfigMatchPatternHeader +
      CreateFontconfigTestStanza("family", "eq", "string", "Arial") +
      CreateFontconfigEditStanza("family", "string", "Verdana") +
      kFontconfigMatchFooter +
      kFontconfigFileFooter));

  FontRenderParamsQuery query(false);
  query.families.push_back("Helvetica");
  std::string suggested_family;
  GetFontRenderParams(query, &suggested_family);
  EXPECT_EQ("Verdana", suggested_family);

  query.families.clear();
  query.families.push_back("Arial");
  suggested_family.clear();
  GetFontRenderParams(query, &suggested_family);
  EXPECT_EQ("Verdana", suggested_family);
}

}  // namespace gfx