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