2 * Copyright (C) 2007 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "UniscribeController.h"
33 #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)
45 , m_currentCharacter(0)
47 , m_computingOffsetPosition(false)
48 , m_includePartialGlyphs(false)
52 m_padding = m_run.padding();
57 for (int s = 0; s < m_run.length(); s++)
58 if (Font::treatAsSpace(m_run[s]))
64 m_padPerSpace = ceilf(m_run.padding() / numSpaces);
67 // Null out our uniscribe structs
68 resetControlAndState();
71 int UniscribeController::offsetForPosition(int x, bool includePartialGlyphs)
73 m_computingOffsetPosition = true;
74 m_includePartialGlyphs = includePartialGlyphs;
77 advance(m_run.length());
78 if (m_computingOffsetPosition) {
79 // The point is to the left or to the right of the entire run.
80 if (m_offsetX >= m_runWidthSoFar && m_run.ltr() || m_offsetX < 0 && m_run.rtl())
81 m_offsetPosition = m_end;
83 m_computingOffsetPosition = false;
84 return m_offsetPosition;
87 void UniscribeController::advance(unsigned offset, GlyphBuffer* glyphBuffer)
89 // FIXME: We really want to be using a newer version of Uniscribe that supports the new OpenType
90 // functions. Those functions would allow us to turn off kerning and ligatures. Without being able
91 // to do that, we will have buggy line breaking and metrics when simple and complex text are close
92 // together (the complex code path will narrow the text because of kerning and ligatures and then
93 // when bidi processing splits into multiple runs, the simple portions will get wider and cause us to
94 // spill off the edge of a line).
95 if (static_cast<int>(offset) > m_end)
98 // Itemize the string.
99 const UChar* cp = m_run.data(m_currentCharacter);
100 int length = offset - m_currentCharacter;
104 // We break up itemization of the string if small caps is involved.
105 // Adjust the characters to account for small caps if it is set.
106 if (m_font.isSmallCaps()) {
107 // FIXME: It's inconsistent that we use logical order when itemizing, since this
108 // does not match normal RTL.
109 Vector<UChar> smallCapsBuffer(length);
110 memcpy(smallCapsBuffer.data(), cp, length * sizeof(UChar));
111 bool isSmallCaps = false;
112 unsigned indexOfCaseShift = m_run.rtl() ? length - 1 : 0;
113 const UChar* curr = m_run.rtl() ? cp + length - 1: cp;
114 const UChar* end = m_run.rtl() ? cp - 1: cp + length;
115 while (curr != end) {
116 int index = curr - cp;
117 UChar c = smallCapsBuffer[index];
119 curr = m_run.rtl() ? curr - 1 : curr + 1;
120 if (U_GET_GC_MASK(c) & U_GC_M_MASK)
122 if (!u_isUUppercase(c) && (newC = u_toupper(c)) != c) {
123 smallCapsBuffer[index] = newC;
126 int itemStart = m_run.rtl() ? index : indexOfCaseShift;
127 int itemLength = m_run.rtl() ? indexOfCaseShift - index : index - indexOfCaseShift;
128 itemizeShapeAndPlace(smallCapsBuffer.data() + itemStart, itemLength, false, glyphBuffer);
129 indexOfCaseShift = index;
131 } else if (isSmallCaps) {
133 int itemStart = m_run.rtl() ? index : indexOfCaseShift;
134 int itemLength = m_run.rtl() ? indexOfCaseShift - index : index - indexOfCaseShift;
135 itemizeShapeAndPlace(smallCapsBuffer.data() + itemStart, itemLength, true, glyphBuffer);
136 indexOfCaseShift = index;
140 int itemLength = m_run.rtl() ? indexOfCaseShift + 1 : length - indexOfCaseShift;
142 int itemStart = m_run.rtl() ? 0 : indexOfCaseShift;
143 itemizeShapeAndPlace(smallCapsBuffer.data() + itemStart, itemLength, isSmallCaps, glyphBuffer);
146 itemizeShapeAndPlace(cp, length, false, glyphBuffer);
149 void UniscribeController::itemizeShapeAndPlace(const UChar* cp, unsigned length, bool smallCaps, GlyphBuffer* glyphBuffer)
151 // ScriptItemize (in Windows XP versions prior to SP2) can overflow by 1. This is why there is an extra empty item
152 // hanging out at the end of the array
155 while (ScriptItemize(cp, length, m_items.size() - 1, &m_control, &m_state, m_items.data(), &numItems) == E_OUTOFMEMORY) {
156 m_items.resize(m_items.size() * 2);
157 resetControlAndState();
159 m_items.resize(numItems + 1);
162 for (int i = m_items.size() - 2; i >= 0; i--) {
163 if (!shapeAndPlaceItem(cp, i, smallCaps, glyphBuffer))
167 for (unsigned i = 0; i < m_items.size() - 1; i++) {
168 if (!shapeAndPlaceItem(cp, i, smallCaps, glyphBuffer))
173 m_currentCharacter += length;
176 void UniscribeController::resetControlAndState()
178 memset(&m_control, 0, sizeof(SCRIPT_CONTROL));
179 memset(&m_state, 0, sizeof(SCRIPT_STATE));
181 // Set up the correct direction for the run.
182 m_state.uBidiLevel = m_run.rtl();
184 // Lock the correct directional override.
185 m_state.fOverrideDirection = m_run.directionalOverride();
188 bool UniscribeController::shapeAndPlaceItem(const UChar* cp, unsigned i, bool smallCaps, GlyphBuffer* glyphBuffer)
190 // Determine the string for this item.
191 const UChar* str = cp + m_items[i].iCharPos;
192 int len = m_items[i+1].iCharPos - m_items[i].iCharPos;
193 SCRIPT_ITEM item = m_items[i];
195 // Get our current FontData that we are using.
196 unsigned dataIndex = 0;
197 const FontData* fontData = m_font.fontDataAt(dataIndex);
199 fontData = fontData->smallCapsFontData(m_font.fontDescription());
201 // Set up buffers to hold the results of shaping the item.
203 Vector<WORD> clusters;
204 Vector<SCRIPT_VISATTR> visualAttributes;
205 clusters.resize(len);
207 // Shape the item. This will provide us with glyphs for the item. We will
208 // attempt to shape using the first available FontData. If the shaping produces a result with missing
209 // glyphs, then we will fall back to the next FontData.
210 // FIXME: This isn't as good as per-glyph fallback, but in practice it should be pretty good, since
211 // items are broken up by "shaping engine", meaning unique scripts will be broken up into
213 bool lastResortFontTried = false;
215 // The recommended size for the glyph buffer is 1.5 * the character length + 16 in the uniscribe docs.
216 // Apparently this is a good size to avoid having to make repeated calls to ScriptShape.
217 glyphs.resize(1.5 * len + 16);
218 visualAttributes.resize(glyphs.size());
220 if (shape(str, len, item, fontData, glyphs, clusters, visualAttributes))
223 // Try again with the next font in the list.
224 if (lastResortFontTried) {
229 fontData = m_font.fontDataAt(++dataIndex);
231 // Out of fonts. Get a font data based on the actual characters.
232 fontData = m_font.fontDataForCharacters(str, len);
233 lastResortFontTried = true;
236 fontData = fontData->smallCapsFontData(m_font.fontDescription());
239 // Just give up. We were unable to shape.
243 // We now have a collection of glyphs.
244 Vector<GOFFSET> offsets;
245 Vector<int> advances;
246 offsets.resize(glyphs.size());
247 advances.resize(glyphs.size());
249 HRESULT placeResult = ScriptPlace(0, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
250 &item.a, advances.data(), offsets.data(), 0);
251 if (placeResult == E_PENDING) {
252 // The script cache isn't primed with enough info yet. We need to select our HFONT into
253 // a DC and pass the DC in to ScriptPlace.
255 HFONT hfont = fontData->platformData().hfont();
256 HFONT oldFont = (HFONT)SelectObject(hdc, hfont);
257 placeResult = ScriptPlace(hdc, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
258 &item.a, advances.data(), offsets.data(), 0);
259 SelectObject(hdc, oldFont);
263 if (FAILED(placeResult) || glyphs.isEmpty())
266 // Convert all chars that should be treated as spaces to use the space glyph.
267 // We also create a map that allows us to quickly go from space glyphs or rounding
268 // hack glyphs back to their corresponding characters.
269 Vector<int> spaceCharacters(glyphs.size());
270 spaceCharacters.fill(-1);
271 Vector<int> roundingHackCharacters(glyphs.size());
272 roundingHackCharacters.fill(-1);
273 Vector<int> roundingHackWordBoundaries(glyphs.size());
274 roundingHackWordBoundaries.fill(-1);
275 unsigned logicalSpaceWidth = fontData->m_spaceWidth * 32.0f;
276 float roundedSpaceWidth = roundf(fontData->m_spaceWidth);
278 for (int k = 0; k < len; k++) {
279 UChar ch = *(str + k);
280 if (Font::treatAsSpace(ch)) {
281 // Substitute in the space glyph at the appropriate place in the glyphs
283 glyphs[clusters[k]] = fontData->m_spaceGlyph;
284 advances[clusters[k]] = logicalSpaceWidth;
285 spaceCharacters[clusters[k]] = m_currentCharacter + k + m_items[i].iCharPos;
288 if (Font::isRoundingHackCharacter(ch))
289 roundingHackCharacters[clusters[k]] = m_currentCharacter + k + m_items[i].iCharPos;
291 int boundary = k + m_currentCharacter + m_items[i].iCharPos;
292 if (boundary < m_run.length() &&
293 Font::isRoundingHackCharacter(*(str + k + 1)))
294 roundingHackWordBoundaries[clusters[k]] = boundary;
297 // Populate our glyph buffer with this information.
298 bool hasExtraSpacing = m_font.letterSpacing() || m_font.wordSpacing() || m_padding;
300 float leftEdge = m_runWidthSoFar;
302 for (unsigned k = 0; k < glyphs.size(); k++) {
303 Glyph glyph = glyphs[k];
304 float advance = advances[k] / 32.0f;
305 float offsetX = offsets[k].du / 32.0f;
306 float offsetY = offsets[k].dv / 32.0f;
308 // Match AppKit's rules for the integer vs. non-integer rendering modes.
309 float roundedAdvance = roundf(advance);
310 if (!m_font.isPrinterFont() && !fontData->isSystemFont()) {
311 advance = roundedAdvance;
312 offsetX = roundf(offsetX);
313 offsetY = roundf(offsetY);
316 // We special case spaces in two ways when applying word rounding.
317 // First, we round spaces to an adjusted width in all fonts.
318 // Second, in fixed-pitch fonts we ensure that all glyphs that
319 // match the width of the space glyph have the same width as the space glyph.
320 if (roundedAdvance == roundedSpaceWidth && (fontData->m_treatAsFixedPitch || glyph == fontData->m_spaceGlyph) &&
321 m_run.applyWordRounding())
322 advance = fontData->m_adjustedSpaceWidth;
324 if (hasExtraSpacing) {
325 // If we're a glyph with an advance, go ahead and add in letter-spacing.
326 // That way we weed out zero width lurkers. This behavior matches the fast text code path.
327 if (advance && m_font.letterSpacing())
328 advance += m_font.letterSpacing();
330 // Handle justification and word-spacing.
331 if (glyph == fontData->m_spaceGlyph) {
332 // Account for padding. WebCore uses space padding to justify text.
333 // We distribute the specified padding over the available spaces in the run.
335 // Use leftover padding if not evenly divisible by number of spaces.
336 if (m_padding < m_padPerSpace) {
337 advance += m_padding;
340 advance += m_padPerSpace;
341 m_padding -= m_padPerSpace;
345 // Account for word-spacing.
346 int characterIndex = spaceCharacters[k];
347 if (characterIndex > 0 && !Font::treatAsSpace(*m_run.data(characterIndex-1)) && m_font.wordSpacing())
348 advance += m_font.wordSpacing();
352 // Deal with the float/integer impedance mismatch between CG and WebCore. "Words" (characters
353 // followed by a character defined by isRoundingHackCharacter()) are always an integer width.
354 // We adjust the width of the last character of a "word" to ensure an integer width.
355 // Force characters that are used to determine word boundaries for the rounding hack
356 // to be integer width, so the following words will start on an integer boundary.
357 int roundingHackIndex = roundingHackCharacters[k];
358 if (m_run.applyWordRounding() && roundingHackIndex != -1)
359 advance = ceilf(advance);
361 // Check to see if the next character is a "rounding hack character", if so, adjust the
362 // width so that the total run width will be on an integer boundary.
363 int position = m_currentCharacter + len;
364 bool lastGlyph = (k == glyphs.size() - 1) && (m_run.rtl() ? i == 0 : i == m_items.size() - 2) && (position >= m_end);
365 if ((m_run.applyWordRounding() && roundingHackWordBoundaries[k] != -1) ||
366 (m_run.applyRunRounding() && lastGlyph)) {
367 float totalWidth = m_runWidthSoFar + advance;
368 advance += ceilf(totalWidth) - totalWidth;
371 m_runWidthSoFar += advance;
373 // FIXME: We need to take the GOFFSETS for combining glyphs and store them in the glyph buffer
374 // as well, so that when the time comes to draw those glyphs, we can apply the appropriate
377 FloatSize size(offsetX, offsetY);
378 glyphBuffer->add(glyph, fontData, advance, &size);
381 // Mutate the glyph array to contain our altered advances.
382 if (m_computingOffsetPosition)
383 advances[k] = advance;
386 while (m_computingOffsetPosition && m_offsetX >= leftEdge && m_offsetX < m_runWidthSoFar) {
387 // The position is somewhere inside this run.
389 ScriptXtoCP(m_offsetX - leftEdge, clusters.size(), glyphs.size(), clusters.data(), visualAttributes.data(),
390 advances.data(), &item.a, &m_offsetPosition, &trailing);
391 if (trailing && m_includePartialGlyphs && m_offsetPosition < len - 1) {
392 m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
393 m_offsetX += m_run.rtl() ? -trailing : trailing;
395 m_computingOffsetPosition = false;
396 m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
397 if (trailing && m_includePartialGlyphs)
406 bool UniscribeController::shape(const UChar* str, int len, SCRIPT_ITEM item, const FontData* fontData,
407 Vector<WORD>& glyphs, Vector<WORD>& clusters,
408 Vector<SCRIPT_VISATTR>& visualAttributes)
412 HRESULT shapeResult = E_PENDING;
415 shapeResult = ScriptShape(hdc, fontData->scriptCache(), str, len, glyphs.size(), &item.a,
416 glyphs.data(), clusters.data(), visualAttributes.data(), &glyphCount);
417 if (shapeResult == E_PENDING) {
418 // The script cache isn't primed with enough info yet. We need to select our HFONT into
419 // a DC and pass the DC in to ScriptShape.
422 HFONT hfont = fontData->platformData().hfont();
423 oldFont = (HFONT)SelectObject(hdc, hfont);
424 } else if (shapeResult == E_OUTOFMEMORY) {
425 // Need to resize our buffers.
426 glyphs.resize(glyphs.size() * 2);
427 visualAttributes.resize(glyphs.size());
429 } while (shapeResult == E_PENDING || shapeResult == E_OUTOFMEMORY);
432 SelectObject(hdc, oldFont);
436 if (FAILED(shapeResult))
439 // FIXME: We need to do better than this. Falling back on the entire item is not good enough.
440 // We may still have missing glyphs even if we succeeded. We need to treat missing glyphs as
441 // a failure so that we will fall back to another font.
442 bool containsMissingGlyphs = false;
443 SCRIPT_FONTPROPERTIES* fontProperties = fontData->scriptFontProperties();
444 for (int i = 0; i < glyphCount; i++) {
445 WORD glyph = glyphs[i];
446 if (glyph == fontProperties->wgDefault) {
447 containsMissingGlyphs = true;
452 if (containsMissingGlyphs)
455 glyphs.resize(glyphCount);
456 visualAttributes.resize(glyphCount);