OLD | NEW |
1 /* libs/graphics/ports/SkFontHost_fontconfig_direct.cpp | 1 /* libs/graphics/ports/SkFontHost_fontconfig_direct.cpp |
2 ** | 2 ** |
3 ** Copyright 2009, Google Inc. | 3 ** Copyright 2009, Google Inc. |
4 ** | 4 ** |
5 ** Licensed under the Apache License, Version 2.0 (the "License"); | 5 ** Licensed under the Apache License, Version 2.0 (the "License"); |
6 ** you may not use this file except in compliance with the License. | 6 ** you may not use this file except in compliance with the License. |
7 ** You may obtain a copy of the License at | 7 ** You may obtain a copy of the License at |
8 ** | 8 ** |
9 ** http://www.apache.org/licenses/LICENSE-2.0 | 9 ** http://www.apache.org/licenses/LICENSE-2.0 |
10 ** | 10 ** |
11 ** Unless required by applicable law or agreed to in writing, software | 11 ** Unless required by applicable law or agreed to in writing, software |
12 ** distributed under the License is distributed on an "AS IS" BASIS, | 12 ** distributed under the License is distributed on an "AS IS" BASIS, |
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 ** See the License for the specific language governing permissions and | 14 ** See the License for the specific language governing permissions and |
15 ** limitations under the License. | 15 ** limitations under the License. |
16 */ | 16 */ |
17 | 17 |
18 #include "SkFontHost_fontconfig_direct.h" | 18 #include "SkFontHost_fontconfig_direct.h" |
19 | 19 |
20 #include <unistd.h> | 20 #include <unistd.h> |
21 #include <fcntl.h> | 21 #include <fcntl.h> |
22 | 22 |
23 #include <fontconfig/fontconfig.h> | 23 #include <fontconfig/fontconfig.h> |
24 | 24 |
25 #include "unicode/utf16.h" | 25 #include "unicode/utf16.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Equivalence classes, used to match the Liberation and Ascender fonts | 29 // Equivalence classes, used to match the Liberation and other fonts |
30 // with their metric-compatible replacements. See the discussion in | 30 // with their metric-compatible replacements. See the discussion in |
31 // GetFontEquivClass(). | 31 // GetFontEquivClass(). |
32 enum FontEquivClass | 32 enum FontEquivClass |
33 { | 33 { |
34 OTHER, | 34 OTHER, |
35 SANS, | 35 SANS, |
36 SERIF, | 36 SERIF, |
37 MONO | 37 MONO, |
| 38 PMINCHO, |
| 39 MINCHO, |
| 40 PGOTHIC, |
| 41 GOTHIC, |
| 42 SIMSUN, |
| 43 NSIMSUN, |
38 }; | 44 }; |
39 | 45 |
40 // Match the font name against a whilelist of fonts, returning the equivalence | 46 // Match the font name against a whilelist of fonts, returning the equivalence |
41 // class. | 47 // class. |
42 FontEquivClass GetFontEquivClass(const char* fontname) | 48 FontEquivClass GetFontEquivClass(const char* fontname) |
43 { | 49 { |
44 // It would be nice for fontconfig to tell us whether a given suggested | 50 // It would be nice for fontconfig to tell us whether a given suggested |
45 // replacement is a "strong" match (that is, an equivalent font) or | 51 // replacement is a "strong" match (that is, an equivalent font) or |
46 // a "weak" match (that is, fontconfig's next-best attempt at finding a | 52 // a "weak" match (that is, fontconfig's next-best attempt at finding a |
47 // substitute). However, I played around with the fontconfig API for | 53 // substitute). However, I played around with the fontconfig API for |
48 // a good few hours and could not make it reveal this information. | 54 // a good few hours and could not make it reveal this information. |
49 // | 55 // |
50 // So instead, we hardcode. Initially this function emulated | 56 // So instead, we hardcode. Initially this function emulated |
51 // /etc/fonts/conf.d/30-metric-aliases.conf | 57 // /etc/fonts/conf.d/30-metric-aliases.conf |
52 // from my Ubuntu system, but we're better off being very conservative. | 58 // from my Ubuntu system, but we're better off being very conservative. |
53 | 59 |
54 // "Ascender Sans", "Ascender Serif" and "Ascender Sans Mono" are the | 60 // Arimo, Tinos and Cousine are a set of fonts metric-compatible with |
55 // tentative names of another set of fonts metric-compatible with | |
56 // Arial, Times New Roman and Courier New with a character repertoire | 61 // Arial, Times New Roman and Courier New with a character repertoire |
57 // much larger than Liberation. Note that Ascender Sans Mono | 62 // much larger than Liberation. Note that Cousine is metrically |
58 // is metrically compatible with Courier New, but the former | 63 // compatible with Courier New, but the former is sans-serif while |
59 // is sans-serif while ther latter is serif. | 64 // the latter is serif. |
60 // Arimo, Tinos and Cousine are the names of new fonts derived from and | 65 |
61 // expanded upon Ascender Sans, Ascender Serif and Ascender Sans Mono. | 66 |
62 if (strcasecmp(fontname, "Arial") == 0 || | 67 struct FontEquivMap { |
63 strcasecmp(fontname, "Liberation Sans") == 0 || | 68 FontEquivClass clazz; |
64 strcasecmp(fontname, "Arimo") == 0 || | 69 const char name[40]; |
65 strcasecmp(fontname, "Ascender Sans") == 0) { | 70 }; |
66 return SANS; | 71 |
67 } else if (strcasecmp(fontname, "Times New Roman") == 0 || | 72 static const FontEquivMap kFontEquivMap[] = { |
68 strcasecmp(fontname, "Liberation Serif") == 0 || | 73 { SANS, "Arial" }, |
69 strcasecmp(fontname, "Tinos") == 0 || | 74 { SANS, "Arimo" }, |
70 strcasecmp(fontname, "Ascender Serif") == 0) { | 75 { SANS, "Liberation Sans" }, |
71 return SERIF; | 76 |
72 } else if (strcasecmp(fontname, "Courier New") == 0 || | 77 { SERIF, "Times New Roman" }, |
73 strcasecmp(fontname, "Cousine") == 0 || | 78 { SERIF, "Tinos" }, |
74 strcasecmp(fontname, "Ascender Sans Mono") == 0) { | 79 { SERIF, "Liberation Serif" }, |
75 return MONO; | 80 |
| 81 { MONO, "Courier New" }, |
| 82 { MONO, "Cousine" }, |
| 83 { MONO, "Liberation Mono" }, |
| 84 |
| 85 // MS Pゴシック |
| 86 { PGOTHIC, "MS PGothic" }, |
| 87 { PGOTHIC, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0" |
| 88 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" }, |
| 89 { PGOTHIC, "IPAPGothic" }, |
| 90 |
| 91 // 宋体 |
| 92 { SIMSUN, "Simsun" }, |
| 93 { SIMSUN, "\xe5\xae\x8b\xe4\xbd\x93" }, |
| 94 { SIMSUN, "Song ASC" }, |
| 95 |
| 96 // MS P明朝 |
| 97 { PMINCHO, "MS PMincho" }, |
| 98 { PMINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xef\xbc\xb0" |
| 99 "\xe6\x98\x8e\xe6\x9c\x9d"}, |
| 100 { PMINCHO, "IPAPMincho" }, |
| 101 |
| 102 // MS ゴシック |
| 103 { GOTHIC, "MS Gothic" }, |
| 104 { GOTHIC, "\xef\xbc\xad\xef\xbc\xb3 " |
| 105 "\xe3\x82\xb4\xe3\x82\xb7\xe3\x83\x83\xe3\x82\xaf" }, |
| 106 { GOTHIC, "IPAGothic" }, |
| 107 |
| 108 // MS 明朝 |
| 109 { MINCHO, "MS Mincho" }, |
| 110 { MINCHO, "\xef\xbc\xad\xef\xbc\xb3 \xe6\x98\x8e\xe6\x9c\x9d" }, |
| 111 { MINCHO, "IPAMincho" }, |
| 112 |
| 113 // 新宋体 |
| 114 { NSIMSUN, "NSimsun" }, |
| 115 { NSIMSUN, "\xe6\x96\xb0\xe5\xae\x8b\xe4\xbd\x93" }, |
| 116 { NSIMSUN, "N Song ASC" }, |
| 117 }; |
| 118 |
| 119 static const size_t kFontCount = |
| 120 sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]); |
| 121 |
| 122 // TODO(jungshik): If this loop turns out to be hot, turn |
| 123 // the array to a static (hash)map to speed it up. |
| 124 for (size_t i = 0; i < kFontCount; ++i) { |
| 125 if (strcasecmp(kFontEquivMap[i].name, fontname) == 0) |
| 126 return kFontEquivMap[i].clazz; |
76 } | 127 } |
77 return OTHER; | 128 return OTHER; |
78 } | 129 } |
79 | 130 |
80 | 131 |
81 // Return true if |font_a| and |font_b| are visually and at the metrics | 132 // Return true if |font_a| and |font_b| are visually and at the metrics |
82 // level interchangeable. | 133 // level interchangeable. |
83 bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b) | 134 bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b) |
84 { | 135 { |
85 FontEquivClass class_a = GetFontEquivClass(font_a); | 136 FontEquivClass class_a = GetFontEquivClass(font_a); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 415 |
365 int FontConfigDirect::Open(unsigned filefaceid) { | 416 int FontConfigDirect::Open(unsigned filefaceid) { |
366 SkAutoMutexAcquire ac(mutex_); | 417 SkAutoMutexAcquire ac(mutex_); |
367 const std::map<unsigned, std::string>::const_iterator | 418 const std::map<unsigned, std::string>::const_iterator |
368 i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid)); | 419 i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid)); |
369 if (i == fileid_to_filename_.end()) | 420 if (i == fileid_to_filename_.end()) |
370 return -1; | 421 return -1; |
371 | 422 |
372 return open(i->second.c_str(), O_RDONLY); | 423 return open(i->second.c_str(), O_RDONLY); |
373 } | 424 } |
OLD | NEW |