Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/font_render_params.h" | |
| 6 | |
| 7 #include <fontconfig/fontconfig.h> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "net/base/escape.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace gfx { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Strings appearing at the beginning and end of Fontconfig XML files. | |
| 22 const char kFileHeader[] = | |
| 23 "<?xml version=\"1.0\"?>\n" | |
| 24 "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n" | |
| 25 "<fontconfig>\n"; | |
| 26 const char kFileFooter[] = "</fontconfig>"; | |
| 27 | |
| 28 // Strings appearing at the beginning and end of Fontconfig <match> stanzas. | |
| 29 const char kMatchHeader[] = " <match>\n"; | |
| 30 const char kMatchFooter[] = " </match>\n"; | |
| 31 | |
| 32 // Returns a Fontconfig <edit> stanza. | |
| 33 std::string CreateEditStanza(const std::string& name, | |
|
Daniel Erat
2014/07/15 22:09:45
an alternative would be creating a testdata direct
msw
2014/07/16 00:53:31
Test .conf files might be clearer (especially if w
| |
| 34 const std::string& type, | |
| 35 const std::string& value) { | |
| 36 return base::StringPrintf( | |
| 37 " <edit name=\"%s\" mode=\"assign\">\n" | |
| 38 " <%s>%s</%s>\n" | |
| 39 " </edit>\n", | |
| 40 name.c_str(), type.c_str(), value.c_str(), type.c_str()); | |
| 41 } | |
| 42 | |
| 43 #if !defined(OS_CHROMEOS) | |
| 44 // Returns a Fontconfig <test> stanza. | |
| 45 std::string CreateTestStanza(const std::string& name, | |
| 46 const std::string& op, | |
| 47 const std::string& type, | |
| 48 const std::string& value) { | |
| 49 return base::StringPrintf( | |
| 50 " <test name=\"%s\" compare=\"%s\" qual=\"any\">\n" | |
| 51 " <%s>%s</%s>\n" | |
| 52 " </test>\n", | |
| 53 name.c_str(), op.c_str(), type.c_str(), value.c_str(), type.c_str()); | |
| 54 } | |
| 55 #endif // !defined(OS_CHROMEOS) | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 class FontRenderParamsTest : public testing::Test { | |
| 60 public: | |
| 61 FontRenderParamsTest() { | |
|
msw
2014/07/16 00:53:31
Should this be done on SetUp instead of the ctor?
Daniel Erat
2014/07/16 03:19:47
these are mostly equivalent, but i believe that c'
| |
| 62 CHECK(FcInit()); | |
| 63 CHECK(temp_dir_.CreateUniqueTempDir()); | |
| 64 | |
| 65 // Add the original config's fonts to the test config. | |
|
Daniel Erat
2014/07/15 22:09:45
the other approach here would be creating a testda
msw
2014/07/16 00:53:31
It looks like content/shell/app/webkit_test_platfo
Daniel Erat
2014/07/16 03:19:47
thanks for pointing that out. i'll switch to that
| |
| 66 std::string config_data = kFileHeader; | |
| 67 | |
| 68 FcStrList* font_dirs = FcConfigGetFontDirs(NULL); | |
| 69 const FcChar8* font_dir = NULL; | |
| 70 while ((font_dir = FcStrListNext(font_dirs))) { | |
| 71 const std::string path = net::EscapeForHTML( | |
|
Daniel Erat
2014/07/15 22:09:45
ui/base appears to use this for escaping, so i hop
msw
2014/07/16 00:53:31
Maybe just make a base::FilePath and get its ::val
willchan no longer on Chromium
2014/07/16 01:35:38
That'd be nice if possible :) Let me know if I nee
Daniel Erat
2014/07/16 03:19:47
this is going into an XML file, so i need to amper
| |
| 72 reinterpret_cast<const char*>(font_dir)); | |
| 73 config_data += base::StringPrintf(" <dir>%s</dir>\n", path.c_str()); | |
| 74 } | |
| 75 FcStrListDone(font_dirs); | |
| 76 | |
| 77 FcStrList* cache_dirs = FcConfigGetCacheDirs(NULL); | |
|
msw
2014/07/16 00:53:31
The <cachedir>s are used to *store* or read the ca
Daniel Erat
2014/07/16 03:19:47
the already-built caches are needed to prevent fon
| |
| 78 const FcChar8* cache_dir = NULL; | |
| 79 while ((cache_dir = FcStrListNext(cache_dirs))) { | |
| 80 const std::string path = net::EscapeForHTML( | |
| 81 reinterpret_cast<const char*>(cache_dir)); | |
| 82 config_data += base::StringPrintf( | |
| 83 " <cachedir>%s</cachedir>\n", path.c_str()); | |
| 84 } | |
| 85 FcStrListDone(cache_dirs); | |
| 86 | |
| 87 config_data += kFileFooter; | |
| 88 config_ = FcConfigCreate(); | |
| 89 LoadFontconfigData(config_data); | |
|
msw
2014/07/16 00:53:31
Could this class instead offer any needed font/dir
Daniel Erat
2014/07/16 03:19:47
it could, but i'm not sure if there's much benefit
| |
| 90 CHECK(FcConfigBuildFonts(config_)); | |
| 91 | |
| 92 // Swap in the new config. A primer on undocumented FcConfig | |
|
msw
2014/07/16 00:53:31
Ha, this is good to include!
| |
| 93 // reference-counting: | |
| 94 // | |
| 95 // - FcConfigCreate() creates a config with a refcount of 1. | |
| 96 // - FcConfigReference() increments a config's refcount. | |
| 97 // - FcConfigDestroy() decrements a config's refcount, deallocating the | |
| 98 // config when the count reaches 0. | |
| 99 // - FcConfigSetCurrent() calls FcConfigDestroy() on the old config, but | |
| 100 // interestingly does not call FcConfigReference() on the new config. | |
| 101 CHECK(FcConfigSetCurrent(config_)); | |
| 102 } | |
| 103 | |
| 104 virtual ~FontRenderParamsTest() { | |
|
msw
2014/07/16 00:53:31
Should this be done on TearDown instead of the dto
Daniel Erat
2014/07/16 03:19:47
see c'tor comment
| |
| 105 FcFini(); | |
| 106 } | |
| 107 | |
| 108 protected: | |
| 109 // Writes Fontconfig configuration data to a temporary file and loads it into | |
| 110 // |config_|. | |
| 111 void LoadFontconfigData(const std::string& data) { | |
| 112 VLOG(1) << "Loading config:\n" << data; | |
| 113 base::FilePath path; | |
| 114 ASSERT_TRUE(CreateTemporaryFileInDir(temp_dir_.path(), &path)); | |
| 115 ASSERT_EQ(static_cast<int>(data.size()), | |
| 116 base::WriteFile(path, data.data(), data.size())); | |
| 117 ASSERT_TRUE(FcConfigParseAndLoad( | |
| 118 config_, reinterpret_cast<const FcChar8*>(path.value().c_str()), | |
| 119 FcTrue)); | |
| 120 } | |
| 121 | |
| 122 private: | |
| 123 FcConfig* config_; | |
| 124 | |
| 125 base::ScopedTempDir temp_dir_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(FontRenderParamsTest); | |
| 128 }; | |
| 129 | |
| 130 TEST_F(FontRenderParamsTest, Default) { | |
| 131 LoadFontconfigData( | |
|
msw
2014/07/16 00:53:31
Both tests don't modify the same FontRenderParamsT
Daniel Erat
2014/07/16 03:19:47
see the googletest faq that i linked to -- each TE
| |
| 132 std::string(kFileHeader) + | |
| 133 kMatchHeader + | |
| 134 CreateEditStanza("antialias", "bool", "true") + | |
| 135 CreateEditStanza("autohint", "bool", "false") + | |
| 136 CreateEditStanza("hinting", "bool", "true") + | |
| 137 CreateEditStanza("hintstyle", "const", "hintfull") + | |
| 138 CreateEditStanza("rgba", "const", "rgb") + | |
| 139 kMatchFooter + | |
| 140 kFileFooter); | |
| 141 | |
| 142 FontRenderParams params = GetDefaultFontRenderParams(); | |
| 143 #if defined(OS_CHROMEOS) | |
| 144 // Chrome OS uses its own defaults for everything except subpixel rendering, | |
| 145 // which comes from Fontconfig. | |
| 146 EXPECT_TRUE(params.antialiasing); | |
| 147 EXPECT_TRUE(params.autohinter); | |
| 148 EXPECT_TRUE(params.use_bitmaps); | |
| 149 EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting); | |
| 150 #else | |
| 151 // Desktop Linux gets all settings from fontconfig. | |
| 152 EXPECT_TRUE(params.antialiasing); | |
| 153 EXPECT_FALSE(params.autohinter); | |
| 154 EXPECT_TRUE(params.use_bitmaps); | |
| 155 EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting); | |
| 156 #endif | |
| 157 EXPECT_FALSE(params.subpixel_positioning); | |
| 158 EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB, | |
| 159 params.subpixel_rendering); | |
| 160 } | |
| 161 | |
| 162 // Chrome OS ignores most Fontconfig settings. | |
| 163 #if !defined(OS_CHROMEOS) | |
| 164 TEST_F(FontRenderParamsTest, Size) { | |
| 165 LoadFontconfigData( | |
| 166 std::string(kFileHeader) + | |
| 167 kMatchHeader + | |
| 168 CreateEditStanza("antialias", "bool", "true") + | |
| 169 CreateEditStanza("hinting", "bool", "true") + | |
| 170 CreateEditStanza("hintstyle", "const", "hintfull") + | |
| 171 CreateEditStanza("rgba", "const", "none") + | |
| 172 kMatchFooter + | |
| 173 kMatchHeader + | |
| 174 CreateTestStanza("pixelsize", "less_eq", "double", "10") + | |
| 175 CreateEditStanza("antialias", "bool", "false") + | |
| 176 kMatchFooter + | |
| 177 kMatchHeader + | |
| 178 CreateTestStanza("size", "more_eq", "double", "20") + | |
| 179 CreateEditStanza("hintstyle", "const", "hintslight") + | |
| 180 CreateEditStanza("rgba", "const", "rgb") + | |
| 181 kMatchFooter + | |
| 182 kFileFooter); | |
| 183 | |
| 184 // The defaults should be used when the supplied size isn't matched by the | |
| 185 // second or third blocks. | |
| 186 int pixel_size = 12; | |
| 187 FontRenderParams params = GetCustomFontRenderParams( | |
| 188 false, NULL, &pixel_size, NULL, NULL); | |
| 189 EXPECT_TRUE(params.antialiasing); | |
| 190 EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting); | |
| 191 EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE, | |
| 192 params.subpixel_rendering); | |
| 193 | |
| 194 pixel_size = 10; | |
| 195 params = GetCustomFontRenderParams(false, NULL, &pixel_size, NULL, NULL); | |
| 196 EXPECT_FALSE(params.antialiasing); | |
| 197 EXPECT_EQ(FontRenderParams::HINTING_FULL, params.hinting); | |
| 198 EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_NONE, | |
| 199 params.subpixel_rendering); | |
| 200 | |
| 201 int point_size = 20; | |
| 202 params = GetCustomFontRenderParams(false, NULL, NULL, &point_size, NULL); | |
| 203 EXPECT_TRUE(params.antialiasing); | |
| 204 EXPECT_EQ(FontRenderParams::HINTING_SLIGHT, params.hinting); | |
| 205 EXPECT_EQ(FontRenderParams::SUBPIXEL_RENDERING_RGB, | |
| 206 params.subpixel_rendering); | |
| 207 } | |
| 208 #endif // !defined(OS_CHROMEOS) | |
| 209 | |
| 210 } // namespace gfx | |
| OLD | NEW |