2 * Copyright (C) 2007, 2008, 2009, 2010 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "UniscribeController.h"
29 #include "SimpleFontData.h"
31 #include <wtf/MathExtras.h>
37 // FIXME: Rearchitect this to be more like WidthIterator in Font.cpp. Have an advance() method
38 // that does stuff in that method instead of doing everything in the constructor. Have advance()
39 // take the GlyphBuffer as an arg so that we don't have to populate the glyph buffer when
41 UniscribeController::UniscribeController(const Font* font, const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts)
44 , m_fallbackFonts(fallbackFonts)
45 , m_minGlyphBoundingBoxX(numeric_limits<float>::max())
46 , m_maxGlyphBoundingBoxX(numeric_limits<float>::min())
47 , m_minGlyphBoundingBoxY(numeric_limits<float>::max())
48 , m_maxGlyphBoundingBoxY(numeric_limits<float>::min())
50 , m_currentCharacter(0)
52 , m_padding(run.expansion())
53 , m_computingOffsetPosition(false)
54 , m_includePartialGlyphs(false)
62 for (int s = 0; s < m_run.length(); s++) {
63 if (Font::treatAsSpace(m_run[s]))
70 m_padPerSpace = m_padding / numSpaces;
73 // Null out our uniscribe structs
74 resetControlAndState();
77 int UniscribeController::offsetForPosition(int x, bool includePartialGlyphs)
79 m_computingOffsetPosition = true;
80 m_includePartialGlyphs = includePartialGlyphs;
83 advance(m_run.length());
84 if (m_computingOffsetPosition) {
85 // The point is to the left or to the right of the entire run.
86 if (m_offsetX >= m_runWidthSoFar && m_run.ltr() || m_offsetX < 0 && m_run.rtl())
87 m_offsetPosition = m_end;
89 m_computingOffsetPosition = false;
90 return m_offsetPosition;
93 void UniscribeController::advance(unsigned offset, GlyphBuffer* glyphBuffer)
95 // FIXME: We really want to be using a newer version of Uniscribe that supports the new OpenType
96 // functions. Those functions would allow us to turn off kerning and ligatures. Without being able
97 // to do that, we will have buggy line breaking and metrics when simple and complex text are close
98 // together (the complex code path will narrow the text because of kerning and ligatures and then
99 // when bidi processing splits into multiple runs, the simple portions will get wider and cause us to
100 // spill off the edge of a line).
101 if (static_cast<int>(offset) > m_end)
104 int length = offset - m_currentCharacter;
108 // Itemize the string.
109 const UChar* cp = m_run.data(m_currentCharacter);
110 unsigned baseCharacter = m_currentCharacter;
112 // We break up itemization of the string by fontData and (if needed) the use of small caps.
114 // FIXME: It's inconsistent that we use logical order when itemizing, since this
115 // does not match normal RTL.
117 // FIXME: This function should decode surrogate pairs. Currently it makes little difference that
118 // it does not because the font cache on Windows does not support non-BMP characters.
119 Vector<UChar, 256> smallCapsBuffer;
120 if (m_font.isSmallCaps())
121 smallCapsBuffer.resize(length);
123 unsigned indexOfFontTransition = m_run.rtl() ? length - 1 : 0;
124 const UChar* curr = m_run.rtl() ? cp + length - 1 : cp;
125 const UChar* end = m_run.rtl() ? cp - 1 : cp + length;
127 const SimpleFontData* fontData;
128 const SimpleFontData* nextFontData = m_font.glyphDataForCharacter(*curr, false).fontData;
133 bool nextIsSmallCaps = m_font.isSmallCaps() && !(U_GET_GC_MASK(*curr) & U_GC_M_MASK) && (newC = u_toupper(*curr)) != *curr;
136 smallCapsBuffer[curr - cp] = newC;
139 curr = m_run.rtl() ? curr - 1 : curr + 1;
143 fontData = nextFontData;
144 isSmallCaps = nextIsSmallCaps;
145 int index = curr - cp;
148 bool forceSmallCaps = isSmallCaps && (U_GET_GC_MASK(c) & U_GC_M_MASK);
149 nextFontData = m_font.glyphDataForCharacter(*curr, false, forceSmallCaps ? SmallCapsVariant : AutoVariant).fontData;
150 if (m_font.isSmallCaps()) {
151 nextIsSmallCaps = forceSmallCaps || (newC = u_toupper(c)) != c;
153 smallCapsBuffer[index] = forceSmallCaps ? c : newC;
156 if (m_fallbackFonts && nextFontData != fontData && fontData != m_font.primaryFont())
157 m_fallbackFonts->add(fontData);
159 if (nextFontData != fontData || nextIsSmallCaps != isSmallCaps) {
160 int itemStart = m_run.rtl() ? index + 1 : indexOfFontTransition;
161 int itemLength = m_run.rtl() ? indexOfFontTransition - index : index - indexOfFontTransition;
162 m_currentCharacter = baseCharacter + itemStart;
163 itemizeShapeAndPlace((isSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, fontData, glyphBuffer);
164 indexOfFontTransition = index;
168 int itemLength = m_run.rtl() ? indexOfFontTransition + 1 : length - indexOfFontTransition;
170 if (m_fallbackFonts && nextFontData != m_font.primaryFont())
171 m_fallbackFonts->add(nextFontData);
173 int itemStart = m_run.rtl() ? 0 : indexOfFontTransition;
174 m_currentCharacter = baseCharacter + itemStart;
175 itemizeShapeAndPlace((nextIsSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, nextFontData, glyphBuffer);
178 m_currentCharacter = baseCharacter + length;
181 void UniscribeController::itemizeShapeAndPlace(const UChar* cp, unsigned length, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
183 // ScriptItemize (in Windows XP versions prior to SP2) can overflow by 1. This is why there is an extra empty item
184 // hanging out at the end of the array
187 while (ScriptItemize(cp, length, m_items.size() - 1, &m_control, &m_state, m_items.data(), &numItems) == E_OUTOFMEMORY) {
188 m_items.resize(m_items.size() * 2);
189 resetControlAndState();
191 m_items.resize(numItems + 1);
194 for (int i = m_items.size() - 2; i >= 0; i--) {
195 if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
199 for (unsigned i = 0; i < m_items.size() - 1; i++) {
200 if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
206 void UniscribeController::resetControlAndState()
208 memset(&m_control, 0, sizeof(SCRIPT_CONTROL));
209 memset(&m_state, 0, sizeof(SCRIPT_STATE));
211 // Set up the correct direction for the run.
212 m_state.uBidiLevel = m_run.rtl();
214 // Lock the correct directional override.
215 m_state.fOverrideDirection = m_run.directionalOverride();
218 bool UniscribeController::shapeAndPlaceItem(const UChar* cp, unsigned i, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
220 // Determine the string for this item.
221 const UChar* str = cp + m_items[i].iCharPos;
222 int len = m_items[i+1].iCharPos - m_items[i].iCharPos;
223 SCRIPT_ITEM item = m_items[i];
225 // Set up buffers to hold the results of shaping the item.
227 Vector<WORD> clusters;
228 Vector<SCRIPT_VISATTR> visualAttributes;
229 clusters.resize(len);
232 // The recommended size for the glyph buffer is 1.5 * the character length + 16 in the uniscribe docs.
233 // Apparently this is a good size to avoid having to make repeated calls to ScriptShape.
234 glyphs.resize(1.5 * len + 16);
235 visualAttributes.resize(glyphs.size());
237 if (!shape(str, len, item, fontData, glyphs, clusters, visualAttributes))
240 // We now have a collection of glyphs.
241 Vector<GOFFSET> offsets;
242 Vector<int> advances;
243 offsets.resize(glyphs.size());
244 advances.resize(glyphs.size());
246 HRESULT placeResult = ScriptPlace(0, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
247 &item.a, advances.data(), offsets.data(), 0);
248 if (placeResult == E_PENDING) {
249 // The script cache isn't primed with enough info yet. We need to select our HFONT into
250 // a DC and pass the DC in to ScriptPlace.
252 HFONT hfont = fontData->platformData().hfont();
253 HFONT oldFont = (HFONT)SelectObject(hdc, hfont);
254 placeResult = ScriptPlace(hdc, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
255 &item.a, advances.data(), offsets.data(), 0);
256 SelectObject(hdc, oldFont);
260 if (FAILED(placeResult) || glyphs.isEmpty())
263 // Convert all chars that should be treated as spaces to use the space glyph.
264 // We also create a map that allows us to quickly go from space glyphs back to their corresponding characters.
265 Vector<int> spaceCharacters(glyphs.size());
266 spaceCharacters.fill(-1);
268 const float cLogicalScale = fontData->platformData().useGDI() ? 1.0f : 32.0f;
269 float spaceWidth = fontData->spaceWidth() - fontData->syntheticBoldOffset();
270 unsigned logicalSpaceWidth = spaceWidth * cLogicalScale;
272 for (int k = 0; k < len; k++) {
273 UChar ch = *(str + k);
274 bool treatAsSpace = Font::treatAsSpace(ch);
275 bool treatAsZeroWidthSpace = ch == zeroWidthSpace || Font::treatAsZeroWidthSpace(ch);
276 if (treatAsSpace || treatAsZeroWidthSpace) {
277 // Substitute in the space glyph at the appropriate place in the glyphs
279 glyphs[clusters[k]] = fontData->spaceGlyph();
280 advances[clusters[k]] = treatAsSpace ? logicalSpaceWidth : 0;
282 spaceCharacters[clusters[k]] = m_currentCharacter + k + item.iCharPos;
286 // Populate our glyph buffer with this information.
287 bool hasExtraSpacing = m_font.letterSpacing() || m_font.wordSpacing() || m_padding;
289 float leftEdge = m_runWidthSoFar;
291 for (unsigned k = 0; k < glyphs.size(); k++) {
292 Glyph glyph = glyphs[k];
293 float advance = advances[k] / cLogicalScale;
294 float offsetX = offsets[k].du / cLogicalScale;
295 float offsetY = offsets[k].dv / cLogicalScale;
297 // Match AppKit's rules for the integer vs. non-integer rendering modes.
298 float roundedAdvance = roundf(advance);
299 if (!m_font.isPrinterFont() && !fontData->isSystemFont()) {
300 advance = roundedAdvance;
301 offsetX = roundf(offsetX);
302 offsetY = roundf(offsetY);
305 advance += fontData->syntheticBoldOffset();
307 if (hasExtraSpacing) {
308 // If we're a glyph with an advance, go ahead and add in letter-spacing.
309 // That way we weed out zero width lurkers. This behavior matches the fast text code path.
310 if (advance && m_font.letterSpacing())
311 advance += m_font.letterSpacing();
313 // Handle justification and word-spacing.
314 int characterIndex = spaceCharacters[k];
315 // characterIndex is left at the initial value of -1 for glyphs that do not map back to treated-as-space characters.
316 if (characterIndex != -1) {
317 // Account for padding. WebCore uses space padding to justify text.
318 // We distribute the specified padding over the available spaces in the run.
320 // Use leftover padding if not evenly divisible by number of spaces.
321 if (m_padding < m_padPerSpace) {
322 advance += m_padding;
325 m_padding -= m_padPerSpace;
326 advance += m_padPerSpace;
330 // Account for word-spacing.
331 if (characterIndex > 0 && !Font::treatAsSpace(*m_run.data(characterIndex - 1)) && m_font.wordSpacing())
332 advance += m_font.wordSpacing();
336 m_runWidthSoFar += advance;
338 // FIXME: We need to take the GOFFSETS for combining glyphs and store them in the glyph buffer
339 // as well, so that when the time comes to draw those glyphs, we can apply the appropriate
342 FloatSize size(offsetX, -offsetY);
343 glyphBuffer->add(glyph, fontData, advance, &size);
346 FloatRect glyphBounds = fontData->boundsForGlyph(glyph);
347 glyphBounds.move(m_glyphOrigin.x(), m_glyphOrigin.y());
348 m_minGlyphBoundingBoxX = min(m_minGlyphBoundingBoxX, glyphBounds.x());
349 m_maxGlyphBoundingBoxX = max(m_maxGlyphBoundingBoxX, glyphBounds.maxX());
350 m_minGlyphBoundingBoxY = min(m_minGlyphBoundingBoxY, glyphBounds.y());
351 m_maxGlyphBoundingBoxY = max(m_maxGlyphBoundingBoxY, glyphBounds.maxY());
352 m_glyphOrigin.move(advance + offsetX, -offsetY);
354 // Mutate the glyph array to contain our altered advances.
355 if (m_computingOffsetPosition)
356 advances[k] = advance;
359 while (m_computingOffsetPosition && m_offsetX >= leftEdge && m_offsetX < m_runWidthSoFar) {
360 // The position is somewhere inside this run.
362 ScriptXtoCP(m_offsetX - leftEdge, clusters.size(), glyphs.size(), clusters.data(), visualAttributes.data(),
363 advances.data(), &item.a, &m_offsetPosition, &trailing);
364 if (trailing && m_includePartialGlyphs && m_offsetPosition < len - 1) {
365 m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
366 m_offsetX += m_run.rtl() ? -trailing : trailing;
368 m_computingOffsetPosition = false;
369 m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
370 if (trailing && m_includePartialGlyphs)
379 bool UniscribeController::shape(const UChar* str, int len, SCRIPT_ITEM item, const SimpleFontData* fontData,
380 Vector<WORD>& glyphs, Vector<WORD>& clusters,
381 Vector<SCRIPT_VISATTR>& visualAttributes)
385 HRESULT shapeResult = E_PENDING;
388 shapeResult = ScriptShape(hdc, fontData->scriptCache(), str, len, glyphs.size(), &item.a,
389 glyphs.data(), clusters.data(), visualAttributes.data(), &glyphCount);
390 if (shapeResult == E_PENDING) {
391 // The script cache isn't primed with enough info yet. We need to select our HFONT into
392 // a DC and pass the DC in to ScriptShape.
395 HFONT hfont = fontData->platformData().hfont();
396 oldFont = (HFONT)SelectObject(hdc, hfont);
397 } else if (shapeResult == E_OUTOFMEMORY) {
398 // Need to resize our buffers.
399 glyphs.resize(glyphs.size() * 2);
400 visualAttributes.resize(glyphs.size());
402 } while (shapeResult == E_PENDING || shapeResult == E_OUTOFMEMORY);
405 SelectObject(hdc, oldFont);
409 if (FAILED(shapeResult))
412 glyphs.shrink(glyphCount);
413 visualAttributes.shrink(glyphCount);