OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2008, 2009, 2012 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include <windows.h> | |
32 #include "config.h" | |
33 #include <vector> | |
34 | |
35 #include "platform/fonts/Character.h" | |
36 #include "platform/fonts/Font.h" | |
37 #include "platform/fonts/GlyphPageTreeNode.h" | |
38 #include "platform/fonts/SimpleFontData.h" | |
39 #include "platform/fonts/win/FontPlatformDataWin.h" | |
40 #include "platform/fonts/win/UniscribeHelperTextRun.h" | |
41 #include "platform/win/HWndDC.h" | |
42 #include "platform/win/SystemInfo.h" | |
43 | |
44 namespace WebCore { | |
45 | |
46 // Fills one page of font data pointers with 0 to indicate that there | |
47 // are no glyphs for the characters. | |
48 static void fillEmptyGlyphs(GlyphPage* page) | |
49 { | |
50 for (int i = 0; i < GlyphPage::size; ++i) | |
51 page->setGlyphDataForIndex(i, 0, 0); | |
52 } | |
53 | |
54 // Convert characters to glyph ids by GetGlyphIndices(), during which, we | |
55 // ensure the font is loaded in memory to make it work in a sandboxed process. | |
56 static bool getGlyphIndices(HFONT font, HDC dc, const UChar* characters, unsigne
d charactersLength, WORD* glyphBuffer, DWORD flag) | |
57 { | |
58 if (GetGlyphIndices(dc, characters, charactersLength, glyphBuffer, flag) !=
GDI_ERROR) | |
59 return true; | |
60 if (FontPlatformData::ensureFontLoaded(font)) { | |
61 if (GetGlyphIndices(dc, characters, charactersLength, glyphBuffer, flag)
!= GDI_ERROR) | |
62 return true; | |
63 // FIXME: Handle gracefully the error if this call also fails. | |
64 // See http://crbug.com/6401 | |
65 WTF_LOG_ERROR("Unable to get the glyph indices after second attempt"); | |
66 } | |
67 return false; | |
68 } | |
69 | |
70 // Initializes space glyph | |
71 static bool initSpaceGlyph(HFONT font, HDC dc, Glyph* spaceGlyph) | |
72 { | |
73 static wchar_t space = ' '; | |
74 return getGlyphIndices(font, dc, &space, 1, spaceGlyph, 0); | |
75 } | |
76 | |
77 // Fills |length| glyphs starting at |offset| in a |page| in the Basic | |
78 // Multilingual Plane (<= U+FFFF). The input buffer size should be the | |
79 // same as |length|. We can use the standard Windows GDI functions here. | |
80 // Returns true if any glyphs were found. | |
81 static bool fillBMPGlyphs(unsigned offset, | |
82 unsigned length, | |
83 UChar* buffer, | |
84 GlyphPage* page, | |
85 const SimpleFontData* fontData) | |
86 { | |
87 HWndDC dc(0); | |
88 HGDIOBJ oldFont = SelectObject(dc, fontData->platformData().hfont()); | |
89 | |
90 TEXTMETRIC tm = {0}; | |
91 if (!GetTextMetrics(dc, &tm)) { | |
92 if (FontPlatformData::ensureFontLoaded(fontData->platformData().hfont())
) { | |
93 if (!GetTextMetrics(dc, &tm)) { | |
94 // FIXME: Handle gracefully the error if this call also fails. | |
95 // See http://crbug.com/6401 | |
96 WTF_LOG_ERROR("Unable to get the text metrics after second attem
pt"); | |
97 | |
98 SelectObject(dc, oldFont); | |
99 fillEmptyGlyphs(page); | |
100 return false; | |
101 } | |
102 } else { | |
103 SelectObject(dc, oldFont); | |
104 fillEmptyGlyphs(page); | |
105 return false; | |
106 } | |
107 } | |
108 | |
109 // FIXME: GetGlyphIndices() sets each item of localGlyphBuffer[] | |
110 // with the one of the values listed below. | |
111 // * With the GGI_MARK_NONEXISTING_GLYPHS flag | |
112 // + If the font has a glyph available for the character, | |
113 // localGlyphBuffer[i] > 0x0. | |
114 // + If the font does not have glyphs available for the character, | |
115 // localGlyphBuffer[i] = 0x1F (TrueType Collection?) or | |
116 // 0xFFFF (OpenType?). | |
117 // * Without the GGI_MARK_NONEXISTING_GLYPHS flag | |
118 // + If the font has a glyph available for the character, | |
119 // localGlyphBuffer[i] > 0x0. | |
120 // + If the font does not have glyphs available for the character, | |
121 // localGlyphBuffer[i] = 0x80. | |
122 // (Windows automatically assigns the glyph for a box character to | |
123 // prevent ExtTextOut() from returning errors.) | |
124 // To avoid from hurting the rendering performance, this code just | |
125 // tells WebKit whether or not the all glyph indices for the given | |
126 // characters are 0x80 (i.e. a possibly-invalid glyph) and let it | |
127 // use alternative fonts for the characters. | |
128 // Although this may cause a problem, it seems to work fine as far as I | |
129 // have tested. (Obviously, I need more tests.) | |
130 WORD localGlyphBuffer[GlyphPage::size]; | |
131 | |
132 // FIXME: I find some Chinese characters can not be correctly displayed | |
133 // when call GetGlyphIndices without flag GGI_MARK_NONEXISTING_GLYPHS, | |
134 // because the corresponding glyph index is set as 0x20 when current font | |
135 // does not have glyphs available for the character. According a blog post | |
136 // http://blogs.msdn.com/michkap/archive/2006/06/28/649791.aspx | |
137 // I think we should switch to the way about calling GetGlyphIndices with | |
138 // flag GGI_MARK_NONEXISTING_GLYPHS, it should be OK according the | |
139 // description of MSDN. | |
140 // Also according to Jungshik and Hironori's suggestion and modification | |
141 // we treat turetype and raster Font as different way when windows version | |
142 // is less than Vista. | |
143 if (!getGlyphIndices(fontData->platformData().hfont(), dc, buffer, length, l
ocalGlyphBuffer, GGI_MARK_NONEXISTING_GLYPHS)) { | |
144 SelectObject(dc, oldFont); | |
145 fillEmptyGlyphs(page); | |
146 return false; | |
147 } | |
148 | |
149 // Copy the output to the GlyphPage | |
150 bool haveGlyphs = false; | |
151 int invalidGlyph = 0xFFFF; | |
152 const DWORD cffTableTag = 0x20464643; // 4-byte identifier for OpenType CFF
table ('CFF '). | |
153 if (!isWindowsVistaOrGreater() && !(tm.tmPitchAndFamily & TMPF_TRUETYPE) &&
(GetFontData(dc, cffTableTag, 0, 0, 0) == GDI_ERROR)) | |
154 invalidGlyph = 0x1F; | |
155 | |
156 Glyph spaceGlyph = 0; // Glyph for a space. Lazily filled. | |
157 bool spaceGlyphInitialized = false; | |
158 | |
159 for (unsigned i = 0; i < length; i++) { | |
160 UChar c = buffer[i]; | |
161 Glyph glyph = localGlyphBuffer[i]; | |
162 const SimpleFontData* glyphFontData = fontData; | |
163 // When this character should be a space, we ignore whatever the font | |
164 // says and use a space. Otherwise, if fonts don't map one of these | |
165 // space or zero width glyphs, we will get a box. | |
166 if (Character::treatAsSpace(c)) { | |
167 // Hard code the glyph indices for characters that should be | |
168 // treated like spaces. | |
169 if (!spaceGlyphInitialized) { | |
170 // If initSpaceGlyph fails, spaceGlyph stays 0 (= glyph is not p
resent). | |
171 initSpaceGlyph(fontData->platformData().hfont(), dc, &spaceGlyph
); | |
172 spaceGlyphInitialized = true; | |
173 if (spaceGlyph) | |
174 haveGlyphs = true; | |
175 } | |
176 glyph = spaceGlyph; | |
177 } else if (glyph == invalidGlyph) { | |
178 // WebKit expects both the glyph index and FontData | |
179 // pointer to be 0 if the glyph is not present | |
180 glyph = 0; | |
181 glyphFontData = 0; | |
182 } else | |
183 haveGlyphs = true; | |
184 page->setGlyphDataForCharacter(offset + i, glyph, glyphFontData); | |
185 } | |
186 | |
187 SelectObject(dc, oldFont); | |
188 return haveGlyphs; | |
189 } | |
190 | |
191 // For non-BMP characters, each is two words (UTF-16) and the input buffer | |
192 // size is 2 * |length|. Since GDI doesn't know how to handle non-BMP | |
193 // characters, we must use Uniscribe to tell us the glyph indices. | |
194 // | |
195 // We don't want to call this in the case of "regular" characters since some | |
196 // fonts may not have the correct combining rules for accents. See the notes | |
197 // at the bottom of ScriptGetCMap. We can't use ScriptGetCMap, though, since | |
198 // it doesn't seem to support UTF-16, despite what this blog post says: | |
199 // http://blogs.msdn.com/michkap/archive/2006/06/29/650680.aspx | |
200 // | |
201 // So we fire up the full Uniscribe doohicky, give it our string, and it will | |
202 // correctly handle the UTF-16 for us. The hard part is taking this and getting | |
203 // the glyph indices back out that correspond to the correct input characters, | |
204 // since they may be missing. | |
205 // | |
206 // Returns true if any glyphs were found. | |
207 static bool fillNonBMPGlyphs(unsigned offset, | |
208 unsigned length, | |
209 UChar* buffer, | |
210 GlyphPage* page, | |
211 const SimpleFontData* fontData) | |
212 { | |
213 bool haveGlyphs = false; | |
214 | |
215 UniscribeHelperTextRun state(buffer, length * 2, false, | |
216 fontData->platformData().hfont(), | |
217 fontData->platformData().scriptCache(), | |
218 fontData->platformData().scriptFontProperties()
); | |
219 state.setInhibitLigate(true); | |
220 state.setDisableFontFallback(true); | |
221 state.init(); | |
222 | |
223 for (unsigned i = 0; i < length; i++) { | |
224 // Each character in this input buffer is a surrogate pair, which | |
225 // consists of two UChars. So, the offset for its i-th character is | |
226 // (i * 2). | |
227 WORD glyph = state.firstGlyphForCharacter(i * 2); | |
228 if (glyph) { | |
229 haveGlyphs = true; | |
230 page->setGlyphDataForIndex(offset + i, glyph, fontData); | |
231 } else | |
232 // Clear both glyph and fontData fields. | |
233 page->setGlyphDataForIndex(offset + i, 0, 0); | |
234 } | |
235 return haveGlyphs; | |
236 } | |
237 | |
238 // We're supposed to return true if there are any glyphs in the range | |
239 // specified by |offset| and |length| in our font, | |
240 // false if there are none. | |
241 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* characterBuffer, | |
242 unsigned bufferLength, const SimpleFontData* fontData) | |
243 { | |
244 // We have to handle BMP and non-BMP characters differently. | |
245 // FIXME: Add assertions to make sure that buffer is entirely in BMP | |
246 // or entirely in non-BMP. | |
247 if (bufferLength == length) | |
248 return fillBMPGlyphs(offset, length, characterBuffer, this, fontData); | |
249 | |
250 if (bufferLength == 2 * length) { | |
251 // A non-BMP input buffer will be twice as long as output glyph buffer | |
252 // because each character in the non-BMP input buffer will be | |
253 // represented by a surrogate pair (two UChar's). | |
254 return fillNonBMPGlyphs(offset, length, characterBuffer, this, fontData)
; | |
255 } | |
256 | |
257 ASSERT_NOT_REACHED(); | |
258 return false; | |
259 } | |
260 | |
261 } // namespace WebCore | |
OLD | NEW |