2 * Copyright (c) 2008, 2009, 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
36 #include "GlyphPageTreeNode.h"
38 #include "PlatformSupport.h"
39 #include "SimpleFontData.h"
40 #include "SystemInfo.h"
41 #include "UniscribeHelperTextRun.h"
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)
49 for (int i = 0; i < GlyphPage::size; ++i)
50 page->setGlyphDataForIndex(i, 0, 0);
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, unsigned charactersLength, WORD* glyphBuffer, DWORD flag)
57 if (GetGlyphIndices(dc, characters, charactersLength, glyphBuffer, flag) != GDI_ERROR)
59 if (PlatformSupport::ensureFontLoaded(font)) {
60 if (GetGlyphIndices(dc, characters, charactersLength, glyphBuffer, flag) != GDI_ERROR)
62 // FIXME: Handle gracefully the error if this call also fails.
63 // See http://crbug.com/6401
64 LOG_ERROR("Unable to get the glyph indices after second attempt");
69 // Initializes space glyph
70 static bool initSpaceGlyph(HFONT font, HDC dc, Glyph* spaceGlyph)
72 static wchar_t space = ' ';
73 return getGlyphIndices(font, dc, &space, 1, spaceGlyph, 0);
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,
84 const SimpleFontData* fontData)
87 HGDIOBJ oldFont = SelectObject(dc, fontData->platformData().hfont());
90 if (!GetTextMetrics(dc, &tm)) {
91 if (PlatformSupport::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 LOG_ERROR("Unable to get the text metrics after second attempt");
97 SelectObject(dc, oldFont);
98 fillEmptyGlyphs(page);
102 SelectObject(dc, oldFont);
103 fillEmptyGlyphs(page);
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];
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, localGlyphBuffer, GGI_MARK_NONEXISTING_GLYPHS)) {
143 SelectObject(dc, oldFont);
144 fillEmptyGlyphs(page);
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 ((windowsVersion() < WindowsVista) && !(tm.tmPitchAndFamily & TMPF_TRUETYPE) && (GetFontData(dc, cffTableTag, 0, 0, 0) == GDI_ERROR))
155 Glyph spaceGlyph = 0; // Glyph for a space. Lazily filled.
156 bool spaceGlyphInitialized = false;
158 for (unsigned i = 0; i < length; 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 present).
170 initSpaceGlyph(fontData->platformData().hfont(), dc, &spaceGlyph);
171 spaceGlyphInitialized = true;
174 } else if (glyph == invalidGlyph) {
175 // WebKit expects both the glyph index and FontData
176 // pointer to be 0 if the glyph is not present
181 page->setGlyphDataForCharacter(offset + i, glyph, glyphFontData);
184 SelectObject(dc, oldFont);
188 // For non-BMP characters, each is two words (UTF-16) and the input buffer
189 // size is 2 * |length|. Since GDI doesn't know how to handle non-BMP
190 // characters, we must use Uniscribe to tell us the glyph indices.
192 // We don't want to call this in the case of "regular" characters since some
193 // fonts may not have the correct combining rules for accents. See the notes
194 // at the bottom of ScriptGetCMap. We can't use ScriptGetCMap, though, since
195 // it doesn't seem to support UTF-16, despite what this blog post says:
196 // http://blogs.msdn.com/michkap/archive/2006/06/29/650680.aspx
198 // So we fire up the full Uniscribe doohicky, give it our string, and it will
199 // correctly handle the UTF-16 for us. The hard part is taking this and getting
200 // the glyph indices back out that correspond to the correct input characters,
201 // since they may be missing.
203 // Returns true if any glyphs were found.
204 static bool fillNonBMPGlyphs(unsigned offset,
208 const SimpleFontData* fontData)
210 bool haveGlyphs = false;
212 UniscribeHelperTextRun state(buffer, length * 2, false,
213 fontData->platformData().hfont(),
214 fontData->platformData().scriptCache(),
215 fontData->platformData().scriptFontProperties());
216 state.setInhibitLigate(true);
217 state.setDisableFontFallback(true);
220 for (unsigned i = 0; i < length; i++) {
221 // Each character in this input buffer is a surrogate pair, which
222 // consists of two UChars. So, the offset for its i-th character is
224 WORD glyph = state.firstGlyphForCharacter(i * 2);
227 page->setGlyphDataForIndex(offset + i, glyph, fontData);
229 // Clear both glyph and fontData fields.
230 page->setGlyphDataForIndex(offset + i, 0, 0);
235 // We're supposed to return true if there are any glyphs in the range
236 // specified by |offset| and |length| in our font,
237 // false if there are none.
238 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* characterBuffer,
239 unsigned bufferLength, const SimpleFontData* fontData)
241 // We have to handle BMP and non-BMP characters differently.
242 // FIXME: Add assertions to make sure that buffer is entirely in BMP
243 // or entirely in non-BMP.
244 if (bufferLength == length)
245 return fillBMPGlyphs(offset, length, characterBuffer, this, fontData);
247 if (bufferLength == 2 * length) {
248 // A non-BMP input buffer will be twice as long as output glyph buffer
249 // because each character in the non-BMP input buffer will be
250 // represented by a surrogate pair (two UChar's).
251 return fillNonBMPGlyphs(offset, length, characterBuffer, this, fontData);
254 ASSERT_NOT_REACHED();
258 } // namespace WebCore