2 * Copyright (c) 2008, 2009 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.
35 #include "ChromiumBridge.h"
37 #include "GlyphPageTreeNode.h"
38 #include "SimpleFontData.h"
39 #include "UniscribeHelperTextRun.h"
40 #include "WindowsVersion.h"
44 // Fills one page of font data pointers with 0 to indicate that there
45 // are no glyphs for the characters.
46 static void fillEmptyGlyphs(GlyphPage* page)
48 for (int i = 0; i < GlyphPage::size; ++i)
49 page->setGlyphDataForIndex(i, 0, 0);
52 // Lazily initializes space glyph
53 static Glyph initSpaceGlyph(HDC dc, Glyph* spaceGlyph)
58 static wchar_t space = ' ';
59 GetGlyphIndices(dc, &space, 1, spaceGlyph, 0);
63 // Fills |length| glyphs starting at |offset| in a |page| in the Basic
64 // Multilingual Plane (<= U+FFFF). The input buffer size should be the
65 // same as |length|. We can use the standard Windows GDI functions here.
66 // Returns true if any glyphs were found.
67 static bool fillBMPGlyphs(unsigned offset,
71 const SimpleFontData* fontData,
74 HDC dc = GetDC((HWND)0);
75 HGDIOBJ oldFont = SelectObject(dc, fontData->m_font.hfont());
78 if (!GetTextMetrics(dc, &tm)) {
79 SelectObject(dc, oldFont);
83 if (ChromiumBridge::ensureFontLoaded(fontData->m_font.hfont()))
84 return fillBMPGlyphs(offset, length, buffer, page, fontData, false);
86 fillEmptyGlyphs(page);
90 // FIXME: This should never happen. We want to crash the
91 // process and receive a crash dump. We should revisit this code later.
92 // See http://crbug.com/6401
94 fillEmptyGlyphs(page);
99 // FIXME: GetGlyphIndices() sets each item of localGlyphBuffer[]
100 // with the one of the values listed below.
101 // * With the GGI_MARK_NONEXISTING_GLYPHS flag
102 // + If the font has a glyph available for the character,
103 // localGlyphBuffer[i] > 0x0.
104 // + If the font does not have glyphs available for the character,
105 // localGlyphBuffer[i] = 0x1F (TrueType Collection?) or
106 // 0xFFFF (OpenType?).
107 // * Without the GGI_MARK_NONEXISTING_GLYPHS flag
108 // + If the font has a glyph available for the character,
109 // localGlyphBuffer[i] > 0x0.
110 // + If the font does not have glyphs available for the character,
111 // localGlyphBuffer[i] = 0x80.
112 // (Windows automatically assigns the glyph for a box character to
113 // prevent ExtTextOut() from returning errors.)
114 // To avoid from hurting the rendering performance, this code just
115 // tells WebKit whether or not the all glyph indices for the given
116 // characters are 0x80 (i.e. a possibly-invalid glyph) and let it
117 // use alternative fonts for the characters.
118 // Although this may cause a problem, it seems to work fine as far as I
119 // have tested. (Obviously, I need more tests.)
120 WORD localGlyphBuffer[GlyphPage::size];
122 // FIXME: I find some Chinese characters can not be correctly displayed
123 // when call GetGlyphIndices without flag GGI_MARK_NONEXISTING_GLYPHS,
124 // because the corresponding glyph index is set as 0x20 when current font
125 // does not have glyphs available for the character. According a blog post
126 // http://blogs.msdn.com/michkap/archive/2006/06/28/649791.aspx
127 // I think we should switch to the way about calling GetGlyphIndices with
128 // flag GGI_MARK_NONEXISTING_GLYPHS, it should be OK according the
129 // description of MSDN.
130 // Also according to Jungshik and Hironori's suggestion and modification
131 // we treat turetype and raster Font as different way when windows version
132 // is less than Vista.
133 GetGlyphIndices(dc, buffer, length, localGlyphBuffer, GGI_MARK_NONEXISTING_GLYPHS);
135 // Copy the output to the GlyphPage
136 bool haveGlyphs = false;
137 int invalidGlyph = 0xFFFF;
138 if (!isVistaOrNewer() && !(tm.tmPitchAndFamily & TMPF_TRUETYPE))
141 Glyph spaceGlyph = 0; // Glyph for a space. Lazily filled.
143 for (unsigned i = 0; i < length; i++) {
145 Glyph glyph = localGlyphBuffer[i];
146 const SimpleFontData* glyphFontData = fontData;
147 // When this character should be a space, we ignore whatever the font
148 // says and use a space. Otherwise, if fonts don't map one of these
149 // space or zero width glyphs, we will get a box.
150 if (Font::treatAsSpace(c))
151 // Hard code the glyph indices for characters that should be
152 // treated like spaces.
153 glyph = initSpaceGlyph(dc, &spaceGlyph);
154 else if (Font::treatAsZeroWidthSpace(c) || c == 0x200B) {
155 // FIXME: change Font::treatAsZeroWidthSpace to use
156 // u_hasBinaryProperty, per jungshik's comment here:
157 // https://bugs.webkit.org/show_bug.cgi?id=20237#c6.
158 // Then the additional OR above won't be necessary.
159 glyph = initSpaceGlyph(dc, &spaceGlyph);
160 glyphFontData = fontData->zeroWidthFontData();
161 } else if (glyph == invalidGlyph) {
162 // WebKit expects both the glyph index and FontData
163 // pointer to be 0 if the glyph is not present
167 if (SimpleFontData::isCJKCodePoint(c))
168 glyphFontData = fontData->cjkWidthFontData();
171 page->setGlyphDataForCharacter(offset + i, glyph, glyphFontData);
174 SelectObject(dc, oldFont);
179 // For non-BMP characters, each is two words (UTF-16) and the input buffer
180 // size is 2 * |length|. Since GDI doesn't know how to handle non-BMP
181 // characters, we must use Uniscribe to tell us the glyph indices.
183 // We don't want to call this in the case of "regular" characters since some
184 // fonts may not have the correct combining rules for accents. See the notes
185 // at the bottom of ScriptGetCMap. We can't use ScriptGetCMap, though, since
186 // it doesn't seem to support UTF-16, despite what this blog post says:
187 // http://blogs.msdn.com/michkap/archive/2006/06/29/650680.aspx
189 // So we fire up the full Uniscribe doohicky, give it our string, and it will
190 // correctly handle the UTF-16 for us. The hard part is taking this and getting
191 // the glyph indices back out that correspond to the correct input characters,
192 // since they may be missing.
194 // Returns true if any glyphs were found.
195 static bool fillNonBMPGlyphs(unsigned offset,
199 const SimpleFontData* fontData)
201 bool haveGlyphs = false;
203 UniscribeHelperTextRun state(buffer, length * 2, false,
204 fontData->m_font.hfont(),
205 fontData->m_font.scriptCache(),
206 fontData->m_font.scriptFontProperties());
207 state.setInhibitLigate(true);
210 for (unsigned i = 0; i < length; i++) {
211 // Each character in this input buffer is a surrogate pair, which
212 // consists of two UChars. So, the offset for its i-th character is
214 WORD glyph = state.firstGlyphForCharacter(i * 2);
217 page->setGlyphDataForIndex(offset + i, glyph, fontData);
219 // Clear both glyph and fontData fields.
220 page->setGlyphDataForIndex(offset + i, 0, 0);
225 // We're supposed to return true if there are any glyphs in the range
226 // specified by |offset| and |length| in our font,
227 // false if there are none.
228 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* characterBuffer,
229 unsigned bufferLength, const SimpleFontData* fontData)
231 // We have to handle BMP and non-BMP characters differently.
232 // FIXME: Add assertions to make sure that buffer is entirely in BMP
233 // or entirely in non-BMP.
234 if (bufferLength == length)
235 return fillBMPGlyphs(offset, length, characterBuffer, this, fontData, true);
237 if (bufferLength == 2 * length) {
238 // A non-BMP input buffer will be twice as long as output glyph buffer
239 // because each character in the non-BMP input buffer will be
240 // represented by a surrogate pair (two UChar's).
241 return fillNonBMPGlyphs(offset, length, characterBuffer, this, fontData);
244 ASSERT_NOT_REACHED();
248 } // namespace WebCore