1/*
2 * Copyright (C) 2007, 2008, 2009, 2010 Apple 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
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "UniscribeController.h"
28#include "Font.h"
29#include "HWndDC.h"
30#include "SimpleFontData.h"
31#include "TextRun.h"
32#include <wtf/MathExtras.h>
33
34using namespace WTF;
35using namespace Unicode;
36using namespace std;
37
38namespace WebCore {
39
40// FIXME: Rearchitect this to be more like WidthIterator in Font.cpp.  Have an advance() method
41// that does stuff in that method instead of doing everything in the constructor.  Have advance()
42// take the GlyphBuffer as an arg so that we don't have to populate the glyph buffer when
43// measuring.
44UniscribeController::UniscribeController(const Font* font, const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts)
45    : m_font(*font)
46    , m_run(run)
47    , m_fallbackFonts(fallbackFonts)
48    , m_minGlyphBoundingBoxX(numeric_limits<float>::max())
49    , m_maxGlyphBoundingBoxX(numeric_limits<float>::min())
50    , m_minGlyphBoundingBoxY(numeric_limits<float>::max())
51    , m_maxGlyphBoundingBoxY(numeric_limits<float>::min())
52    , m_end(run.length())
53    , m_currentCharacter(0)
54    , m_runWidthSoFar(0)
55    , m_padding(run.expansion())
56    , m_computingOffsetPosition(false)
57    , m_includePartialGlyphs(false)
58    , m_offsetX(0)
59    , m_offsetPosition(0)
60{
61    if (!m_padding)
62        m_padPerSpace = 0;
63    else {
64        float numSpaces = 0;
65        for (int s = 0; s < m_run.length(); s++) {
66            if (Font::treatAsSpace(m_run[s]))
67                numSpaces++;
68        }
69
70        if (numSpaces == 0)
71            m_padPerSpace = 0;
72        else
73            m_padPerSpace = m_padding / numSpaces;
74    }
75
76    // Null out our uniscribe structs
77    resetControlAndState();
78}
79
80int UniscribeController::offsetForPosition(int x, bool includePartialGlyphs)
81{
82    m_computingOffsetPosition = true;
83    m_includePartialGlyphs = includePartialGlyphs;
84    m_offsetX = x;
85    m_offsetPosition = 0;
86    advance(m_run.length());
87    if (m_computingOffsetPosition) {
88        // The point is to the left or to the right of the entire run.
89        if (m_offsetX >= m_runWidthSoFar && m_run.ltr() || m_offsetX < 0 && m_run.rtl())
90            m_offsetPosition = m_end;
91    }
92    m_computingOffsetPosition = false;
93    return m_offsetPosition;
94}
95
96void UniscribeController::advance(unsigned offset, GlyphBuffer* glyphBuffer)
97{
98    // FIXME: We really want to be using a newer version of Uniscribe that supports the new OpenType
99    // functions.  Those functions would allow us to turn off kerning and ligatures.  Without being able
100    // to do that, we will have buggy line breaking and metrics when simple and complex text are close
101    // together (the complex code path will narrow the text because of kerning and ligatures and then
102    // when bidi processing splits into multiple runs, the simple portions will get wider and cause us to
103    // spill off the edge of a line).
104    if (static_cast<int>(offset) > m_end)
105        offset = m_end;
106
107    int length = offset - m_currentCharacter;
108    if (length <= 0)
109        return;
110
111    // Itemize the string.
112    const UChar* cp = m_run.data16(m_currentCharacter);
113    unsigned baseCharacter = m_currentCharacter;
114
115    // We break up itemization of the string by fontData and (if needed) the use of small caps.
116
117    // FIXME: It's inconsistent that we use logical order when itemizing, since this
118    // does not match normal RTL.
119
120    // FIXME: This function should decode surrogate pairs. Currently it makes little difference that
121    // it does not because the font cache on Windows does not support non-BMP characters.
122    Vector<UChar, 256> smallCapsBuffer;
123    if (m_font.isSmallCaps())
124        smallCapsBuffer.resize(length);
125
126    unsigned indexOfFontTransition = m_run.rtl() ? length - 1 : 0;
127    const UChar* curr = m_run.rtl() ? cp + length  - 1 : cp;
128    const UChar* end = m_run.rtl() ? cp - 1 : cp + length;
129
130    const SimpleFontData* fontData;
131    const SimpleFontData* nextFontData = m_font.glyphDataForCharacter(*curr, false).fontData;
132
133    UChar newC = 0;
134
135    bool isSmallCaps;
136    bool nextIsSmallCaps = m_font.isSmallCaps() && !(category(*curr) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining)) && (newC = toUpper(*curr)) != *curr;
137
138    if (nextIsSmallCaps)
139        smallCapsBuffer[curr - cp] = newC;
140
141    while (true) {
142        curr = m_run.rtl() ? curr - 1 : curr + 1;
143        if (curr == end)
144            break;
145
146        fontData = nextFontData;
147        isSmallCaps = nextIsSmallCaps;
148        int index = curr - cp;
149        UChar c = *curr;
150
151        bool forceSmallCaps = isSmallCaps && (category(c) & (Mark_NonSpacing | Mark_Enclosing | Mark_SpacingCombining));
152        nextFontData = m_font.glyphDataForCharacter(*curr, false, forceSmallCaps ? SmallCapsVariant : AutoVariant).fontData;
153        if (m_font.isSmallCaps()) {
154            nextIsSmallCaps = forceSmallCaps || (newC = toUpper(c)) != c;
155            if (nextIsSmallCaps)
156                smallCapsBuffer[index] = forceSmallCaps ? c : newC;
157        }
158
159        if (m_fallbackFonts && nextFontData != fontData && fontData != m_font.primaryFont())
160            m_fallbackFonts->add(fontData);
161
162        if (nextFontData != fontData || nextIsSmallCaps != isSmallCaps) {
163            int itemStart = m_run.rtl() ? index + 1 : indexOfFontTransition;
164            int itemLength = m_run.rtl() ? indexOfFontTransition - index : index - indexOfFontTransition;
165            m_currentCharacter = baseCharacter + itemStart;
166            itemizeShapeAndPlace((isSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, fontData, glyphBuffer);
167            indexOfFontTransition = index;
168        }
169    }
170
171    int itemLength = m_run.rtl() ? indexOfFontTransition + 1 : length - indexOfFontTransition;
172    if (itemLength) {
173        if (m_fallbackFonts && nextFontData != m_font.primaryFont())
174            m_fallbackFonts->add(nextFontData);
175
176        int itemStart = m_run.rtl() ? 0 : indexOfFontTransition;
177        m_currentCharacter = baseCharacter + itemStart;
178        itemizeShapeAndPlace((nextIsSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, nextFontData, glyphBuffer);
179    }
180
181    m_currentCharacter = baseCharacter + length;
182}
183
184void UniscribeController::itemizeShapeAndPlace(const UChar* cp, unsigned length, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
185{
186    // ScriptItemize (in Windows XP versions prior to SP2) can overflow by 1.  This is why there is an extra empty item
187    // hanging out at the end of the array
188    m_items.resize(6);
189    int numItems = 0;
190    while (ScriptItemize(cp, length, m_items.size() - 1, &m_control, &m_state, m_items.data(), &numItems) == E_OUTOFMEMORY) {
191        m_items.resize(m_items.size() * 2);
192        resetControlAndState();
193    }
194    m_items.resize(numItems + 1);
195
196    if (m_run.rtl()) {
197        for (int i = m_items.size() - 2; i >= 0; i--) {
198            if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
199                return;
200        }
201    } else {
202        for (unsigned i = 0; i < m_items.size() - 1; i++) {
203            if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
204                return;
205        }
206    }
207}
208
209void UniscribeController::resetControlAndState()
210{
211    memset(&m_control, 0, sizeof(SCRIPT_CONTROL));
212    memset(&m_state, 0, sizeof(SCRIPT_STATE));
213
214    // Set up the correct direction for the run.
215    m_state.uBidiLevel = m_run.rtl();
216
217    // Lock the correct directional override.
218    m_state.fOverrideDirection = m_run.directionalOverride();
219}
220
221bool UniscribeController::shapeAndPlaceItem(const UChar* cp, unsigned i, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
222{
223    // Determine the string for this item.
224    const UChar* str = cp + m_items[i].iCharPos;
225    int len = m_items[i+1].iCharPos - m_items[i].iCharPos;
226    SCRIPT_ITEM item = m_items[i];
227
228    // Set up buffers to hold the results of shaping the item.
229    Vector<WORD> glyphs;
230    Vector<WORD> clusters;
231    Vector<SCRIPT_VISATTR> visualAttributes;
232    clusters.resize(len);
233
234    // Shape the item.
235    // The recommended size for the glyph buffer is 1.5 * the character length + 16 in the uniscribe docs.
236    // Apparently this is a good size to avoid having to make repeated calls to ScriptShape.
237    glyphs.resize(1.5 * len + 16);
238    visualAttributes.resize(glyphs.size());
239
240    if (!shape(str, len, item, fontData, glyphs, clusters, visualAttributes))
241        return true;
242
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());
248    int glyphCount = 0;
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.
254        HWndDC hdc(0);
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);
260    }
261
262    if (FAILED(placeResult) || glyphs.isEmpty())
263        return true;
264
265    // Convert all chars that should be treated as spaces to use the space glyph.
266    // We also create a map that allows us to quickly go from space glyphs back to their corresponding characters.
267    Vector<int> spaceCharacters(glyphs.size());
268    spaceCharacters.fill(-1);
269
270    const float cLogicalScale = fontData->platformData().useGDI() ? 1.0f : 32.0f;
271    float spaceWidth = fontData->spaceWidth() - fontData->syntheticBoldOffset();
272    unsigned logicalSpaceWidth = spaceWidth * cLogicalScale;
273
274    for (int k = 0; k < len; k++) {
275        UChar ch = *(str + k);
276        bool treatAsSpace = Font::treatAsSpace(ch);
277        bool treatAsZeroWidthSpace = Font::treatAsZeroWidthSpace(ch);
278        if (treatAsSpace || treatAsZeroWidthSpace) {
279            // Substitute in the space glyph at the appropriate place in the glyphs
280            // array.
281            glyphs[clusters[k]] = fontData->spaceGlyph();
282            advances[clusters[k]] = treatAsSpace ? logicalSpaceWidth : 0;
283            if (treatAsSpace)
284                spaceCharacters[clusters[k]] = m_currentCharacter + k + item.iCharPos;
285        }
286    }
287
288    // Populate our glyph buffer with this information.
289    bool hasExtraSpacing = m_font.letterSpacing() || m_font.wordSpacing() || m_padding;
290
291    float leftEdge = m_runWidthSoFar;
292
293    for (unsigned k = 0; k < glyphs.size(); k++) {
294        Glyph glyph = glyphs[k];
295        float advance = advances[k] / cLogicalScale;
296        float offsetX = offsets[k].du / cLogicalScale;
297        float offsetY = offsets[k].dv / cLogicalScale;
298
299        // Match AppKit's rules for the integer vs. non-integer rendering modes.
300        float roundedAdvance = roundf(advance);
301        if (!m_font.isPrinterFont() && !fontData->isSystemFont()) {
302            advance = roundedAdvance;
303            offsetX = roundf(offsetX);
304            offsetY = roundf(offsetY);
305        }
306
307        advance += fontData->syntheticBoldOffset();
308
309        if (hasExtraSpacing) {
310            // If we're a glyph with an advance, go ahead and add in letter-spacing.
311            // That way we weed out zero width lurkers.  This behavior matches the fast text code path.
312            if (advance && m_font.letterSpacing())
313                advance += m_font.letterSpacing();
314
315            // Handle justification and word-spacing.
316            int characterIndex = spaceCharacters[k];
317            // characterIndex is left at the initial value of -1 for glyphs that do not map back to treated-as-space characters.
318            if (characterIndex != -1) {
319                // Account for padding. WebCore uses space padding to justify text.
320                // We distribute the specified padding over the available spaces in the run.
321                if (m_padding) {
322                    // Use leftover padding if not evenly divisible by number of spaces.
323                    if (m_padding < m_padPerSpace) {
324                        advance += m_padding;
325                        m_padding = 0;
326                    } else {
327                        m_padding -= m_padPerSpace;
328                        advance += m_padPerSpace;
329                    }
330                }
331
332                // Account for word-spacing.
333                if (characterIndex > 0 && !Font::treatAsSpace(*m_run.data16(characterIndex - 1)) && m_font.wordSpacing())
334                    advance += m_font.wordSpacing();
335            }
336        }
337
338        m_runWidthSoFar += advance;
339
340        // FIXME: We need to take the GOFFSETS for combining glyphs and store them in the glyph buffer
341        // as well, so that when the time comes to draw those glyphs, we can apply the appropriate
342        // translation.
343        if (glyphBuffer) {
344            FloatSize size(offsetX, -offsetY);
345            glyphBuffer->add(glyph, fontData, advance, &size);
346        }
347
348        FloatRect glyphBounds = fontData->boundsForGlyph(glyph);
349        glyphBounds.move(m_glyphOrigin.x(), m_glyphOrigin.y());
350        m_minGlyphBoundingBoxX = min(m_minGlyphBoundingBoxX, glyphBounds.x());
351        m_maxGlyphBoundingBoxX = max(m_maxGlyphBoundingBoxX, glyphBounds.maxX());
352        m_minGlyphBoundingBoxY = min(m_minGlyphBoundingBoxY, glyphBounds.y());
353        m_maxGlyphBoundingBoxY = max(m_maxGlyphBoundingBoxY, glyphBounds.maxY());
354        m_glyphOrigin.move(advance + offsetX, -offsetY);
355
356        // Mutate the glyph array to contain our altered advances.
357        if (m_computingOffsetPosition)
358            advances[k] = advance;
359    }
360
361    while (m_computingOffsetPosition && m_offsetX >= leftEdge && m_offsetX < m_runWidthSoFar) {
362        // The position is somewhere inside this run.
363        int trailing = 0;
364        ScriptXtoCP(m_offsetX - leftEdge, clusters.size(), glyphs.size(), clusters.data(), visualAttributes.data(),
365                    advances.data(), &item.a, &m_offsetPosition, &trailing);
366        if (trailing && m_includePartialGlyphs && m_offsetPosition < len - 1) {
367            m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
368            m_offsetX += m_run.rtl() ? -trailing : trailing;
369        } else {
370            m_computingOffsetPosition = false;
371            m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
372            if (trailing && m_includePartialGlyphs)
373               m_offsetPosition++;
374            return false;
375        }
376    }
377
378    return true;
379}
380
381bool UniscribeController::shape(const UChar* str, int len, SCRIPT_ITEM item, const SimpleFontData* fontData,
382                                Vector<WORD>& glyphs, Vector<WORD>& clusters,
383                                Vector<SCRIPT_VISATTR>& visualAttributes)
384{
385    HWndDC hdc;
386    HFONT oldFont = 0;
387    HRESULT shapeResult = E_PENDING;
388    int glyphCount = 0;
389    do {
390        shapeResult = ScriptShape(hdc, fontData->scriptCache(), str, len, glyphs.size(), &item.a,
391                                  glyphs.data(), clusters.data(), visualAttributes.data(), &glyphCount);
392        if (shapeResult == E_PENDING) {
393            // The script cache isn't primed with enough info yet.  We need to select our HFONT into
394            // a DC and pass the DC in to ScriptShape.
395            ASSERT(!hdc);
396            hdc.setHWnd(0);
397            HFONT hfont = fontData->platformData().hfont();
398            oldFont = (HFONT)SelectObject(hdc, hfont);
399        } else if (shapeResult == E_OUTOFMEMORY) {
400            // Need to resize our buffers.
401            glyphs.resize(glyphs.size() * 2);
402            visualAttributes.resize(glyphs.size());
403        }
404    } while (shapeResult == E_PENDING || shapeResult == E_OUTOFMEMORY);
405
406    if (hdc)
407        SelectObject(hdc, oldFont);
408
409    if (FAILED(shapeResult))
410        return false;
411
412    glyphs.shrink(glyphCount);
413    visualAttributes.shrink(glyphCount);
414
415    return true;
416}
417
418}
419