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 { PMINCHO, "MS PMincho" }, | |
86 { PMINCHO, "MS P明朝" }, | |
Evan Martin
2010/12/14 18:04:21
I worry that people's editors will eat these UTF-8
| |
87 { PMINCHO, "IPAPMincho" }, | |
88 | |
89 { MINCHO, "MS Mincho" }, | |
90 { MINCHO, "MS 明朝" }, | |
91 { MINCHO, "IPAMincho" }, | |
92 | |
93 { PGOTHIC, "MS PGothic" }, | |
94 { PGOTHIC, "MS Pゴシック" }, | |
95 { PGOTHIC, "IPAPGothic" }, | |
96 | |
97 { GOTHIC, "MS Gothic" }, | |
98 { GOTHIC, "MS ゴシック" }, | |
99 { GOTHIC, "IPAGothic" }, | |
100 | |
101 { SIMSUN, "Simsun" }, | |
102 { SIMSUN, "宋体" }, | |
103 { SIMSUN, "Song ASC" }, | |
104 | |
105 { NSIMSUN, "NSimsun" }, | |
106 { NSIMSUN, "新宋体" }, | |
107 { NSIMSUN, "N Song ASC" }, | |
108 }; | |
109 | |
110 static const size_t kFontCount = | |
111 sizeof(kFontEquivMap)/sizeof(kFontEquivMap[0]); | |
112 | |
113 for (size_t i = 0; i < kFontCount; ++i) { | |
jungshik at Google
2010/12/11 01:10:50
Evan, do you think I'd better make a (hash)map onc
Evan Martin
2010/12/14 18:04:21
I think whichever implementation is simpler would
| |
114 if (strcasecmp(kFontEquivMap[i].name, fontname) == 0) | |
115 return kFontEquivMap[i].clazz; | |
76 } | 116 } |
77 return OTHER; | 117 return OTHER; |
78 } | 118 } |
79 | 119 |
80 | 120 |
81 // Return true if |font_a| and |font_b| are visually and at the metrics | 121 // Return true if |font_a| and |font_b| are visually and at the metrics |
82 // level interchangeable. | 122 // level interchangeable. |
83 bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b) | 123 bool IsMetricCompatibleReplacement(const char* font_a, const char* font_b) |
84 { | 124 { |
85 FontEquivClass class_a = GetFontEquivClass(font_a); | 125 FontEquivClass class_a = GetFontEquivClass(font_a); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 | 404 |
365 int FontConfigDirect::Open(unsigned filefaceid) { | 405 int FontConfigDirect::Open(unsigned filefaceid) { |
366 SkAutoMutexAcquire ac(mutex_); | 406 SkAutoMutexAcquire ac(mutex_); |
367 const std::map<unsigned, std::string>::const_iterator | 407 const std::map<unsigned, std::string>::const_iterator |
368 i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid)); | 408 i = fileid_to_filename_.find(FileFaceIdToFileId(filefaceid)); |
369 if (i == fileid_to_filename_.end()) | 409 if (i == fileid_to_filename_.end()) |
370 return -1; | 410 return -1; |
371 | 411 |
372 return open(i->second.c_str(), O_RDONLY); | 412 return open(i->second.c_str(), O_RDONLY); |
373 } | 413 } |
OLD | NEW |