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    String bufferFor16BitData;
112
113    // Itemize the string.
114    const UChar* cp = nullptr;
115    if (m_run.is8Bit()) {
116        // Uniscribe only deals with 16-bit characters. Must generate them now.
117        bufferFor16BitData = String::make16BitFrom8BitSource(m_run.data8(m_currentCharacter), length);
118        cp = bufferFor16BitData.characters16();
119    } else
120        cp = m_run.data16(m_currentCharacter);
121
122    unsigned baseCharacter = m_currentCharacter;
123
124    // We break up itemization of the string by fontData and (if needed) the use of small caps.
125
126    // FIXME: It's inconsistent that we use logical order when itemizing, since this
127    // does not match normal RTL.
128
129    // FIXME: This function should decode surrogate pairs. Currently it makes little difference that
130    // it does not because the font cache on Windows does not support non-BMP characters.
131    Vector<UChar, 256> smallCapsBuffer;
132    if (m_font.isSmallCaps())
133        smallCapsBuffer.resize(length);
134
135    unsigned indexOfFontTransition = m_run.rtl() ? length - 1 : 0;
136    const UChar* curr = m_run.rtl() ? cp + length  - 1 : cp;
137    const UChar* end = m_run.rtl() ? cp - 1 : cp + length;
138
139    const SimpleFontData* fontData;
140    const SimpleFontData* nextFontData = m_font.glyphDataForCharacter(*curr, false).fontData;
141
142    UChar newC = 0;
143
144    bool isSmallCaps;
145    bool nextIsSmallCaps = m_font.isSmallCaps() && !(U_GET_GC_MASK(*curr) & U_GC_M_MASK) && (newC = u_toupper(*curr)) != *curr;
146
147    if (nextIsSmallCaps)
148        smallCapsBuffer[curr - cp] = newC;
149
150    while (true) {
151        curr = m_run.rtl() ? curr - 1 : curr + 1;
152        if (curr == end)
153            break;
154
155        fontData = nextFontData;
156        isSmallCaps = nextIsSmallCaps;
157        int index = curr - cp;
158        UChar c = *curr;
159
160        bool forceSmallCaps = isSmallCaps && (U_GET_GC_MASK(c) & U_GC_M_MASK);
161        nextFontData = m_font.glyphDataForCharacter(*curr, false, forceSmallCaps ? SmallCapsVariant : AutoVariant).fontData;
162        if (m_font.isSmallCaps()) {
163            nextIsSmallCaps = forceSmallCaps || (newC = u_toupper(c)) != c;
164            if (nextIsSmallCaps)
165                smallCapsBuffer[index] = forceSmallCaps ? c : newC;
166        }
167
168        if (m_fallbackFonts && nextFontData != fontData && fontData != m_font.primaryFont())
169            m_fallbackFonts->add(fontData);
170
171        if (nextFontData != fontData || nextIsSmallCaps != isSmallCaps) {
172            int itemStart = m_run.rtl() ? index + 1 : indexOfFontTransition;
173            int itemLength = m_run.rtl() ? indexOfFontTransition - index : index - indexOfFontTransition;
174            m_currentCharacter = baseCharacter + itemStart;
175            itemizeShapeAndPlace((isSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, fontData, glyphBuffer);
176            indexOfFontTransition = index;
177        }
178    }
179
180    int itemLength = m_run.rtl() ? indexOfFontTransition + 1 : length - indexOfFontTransition;
181    if (itemLength) {
182        if (m_fallbackFonts && nextFontData != m_font.primaryFont())
183            m_fallbackFonts->add(nextFontData);
184
185        int itemStart = m_run.rtl() ? 0 : indexOfFontTransition;
186        m_currentCharacter = baseCharacter + itemStart;
187        itemizeShapeAndPlace((nextIsSmallCaps ? smallCapsBuffer.data() : cp) + itemStart, itemLength, nextFontData, glyphBuffer);
188    }
189
190    m_currentCharacter = baseCharacter + length;
191}
192
193void UniscribeController::itemizeShapeAndPlace(const UChar* cp, unsigned length, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
194{
195    // ScriptItemize (in Windows XP versions prior to SP2) can overflow by 1.  This is why there is an extra empty item
196    // hanging out at the end of the array
197    m_items.resize(6);
198    int numItems = 0;
199    while (ScriptItemize(cp, length, m_items.size() - 1, &m_control, &m_state, m_items.data(), &numItems) == E_OUTOFMEMORY) {
200        m_items.resize(m_items.size() * 2);
201        resetControlAndState();
202    }
203    m_items.resize(numItems + 1);
204
205    if (m_run.rtl()) {
206        for (int i = m_items.size() - 2; i >= 0; i--) {
207            if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
208                return;
209        }
210    } else {
211        for (unsigned i = 0; i < m_items.size() - 1; i++) {
212            if (!shapeAndPlaceItem(cp, i, fontData, glyphBuffer))
213                return;
214        }
215    }
216}
217
218void UniscribeController::resetControlAndState()
219{
220    memset(&m_control, 0, sizeof(SCRIPT_CONTROL));
221    memset(&m_state, 0, sizeof(SCRIPT_STATE));
222
223    // Set up the correct direction for the run.
224    m_state.uBidiLevel = m_run.rtl();
225
226    // Lock the correct directional override.
227    m_state.fOverrideDirection = m_run.directionalOverride();
228}
229
230bool UniscribeController::shapeAndPlaceItem(const UChar* cp, unsigned i, const SimpleFontData* fontData, GlyphBuffer* glyphBuffer)
231{
232    // Determine the string for this item.
233    const UChar* str = cp + m_items[i].iCharPos;
234    int len = m_items[i+1].iCharPos - m_items[i].iCharPos;
235    SCRIPT_ITEM item = m_items[i];
236
237    // Set up buffers to hold the results of shaping the item.
238    Vector<WORD> glyphs;
239    Vector<WORD> clusters;
240    Vector<SCRIPT_VISATTR> visualAttributes;
241    clusters.resize(len);
242
243    // Shape the item.
244    // The recommended size for the glyph buffer is 1.5 * the character length + 16 in the uniscribe docs.
245    // Apparently this is a good size to avoid having to make repeated calls to ScriptShape.
246    glyphs.resize(1.5 * len + 16);
247    visualAttributes.resize(glyphs.size());
248
249    if (!shape(str, len, item, fontData, glyphs, clusters, visualAttributes))
250        return true;
251
252    // We now have a collection of glyphs.
253    Vector<GOFFSET> offsets;
254    Vector<int> advances;
255    offsets.resize(glyphs.size());
256    advances.resize(glyphs.size());
257    int glyphCount = 0;
258    HRESULT placeResult = ScriptPlace(0, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
259                                      &item.a, advances.data(), offsets.data(), 0);
260    if (placeResult == E_PENDING) {
261        // The script cache isn't primed with enough info yet.  We need to select our HFONT into
262        // a DC and pass the DC in to ScriptPlace.
263        HWndDC hdc(0);
264        HFONT hfont = fontData->platformData().hfont();
265        HFONT oldFont = (HFONT)SelectObject(hdc, hfont);
266        placeResult = ScriptPlace(hdc, fontData->scriptCache(), glyphs.data(), glyphs.size(), visualAttributes.data(),
267                                  &item.a, advances.data(), offsets.data(), 0);
268        SelectObject(hdc, oldFont);
269    }
270
271    if (FAILED(placeResult) || glyphs.isEmpty())
272        return true;
273
274    // Convert all chars that should be treated as spaces to use the space glyph.
275    // We also create a map that allows us to quickly go from space glyphs back to their corresponding characters.
276    Vector<int> spaceCharacters(glyphs.size());
277    spaceCharacters.fill(-1);
278
279    const float cLogicalScale = fontData->platformData().useGDI() ? 1.0f : 32.0f;
280    float spaceWidth = fontData->spaceWidth() - fontData->syntheticBoldOffset();
281    unsigned logicalSpaceWidth = spaceWidth * cLogicalScale;
282
283    for (int k = 0; k < len; k++) {
284        UChar ch = *(str + k);
285        bool treatAsSpace = Font::treatAsSpace(ch);
286        bool treatAsZeroWidthSpace = Font::treatAsZeroWidthSpace(ch);
287        if (treatAsSpace || treatAsZeroWidthSpace) {
288            // Substitute in the space glyph at the appropriate place in the glyphs
289            // array.
290            glyphs[clusters[k]] = fontData->spaceGlyph();
291            advances[clusters[k]] = treatAsSpace ? logicalSpaceWidth : 0;
292            if (treatAsSpace)
293                spaceCharacters[clusters[k]] = m_currentCharacter + k + item.iCharPos;
294        }
295    }
296
297    // Populate our glyph buffer with this information.
298    bool hasExtraSpacing = m_font.letterSpacing() || m_font.wordSpacing() || m_padding;
299
300    float leftEdge = m_runWidthSoFar;
301
302    for (unsigned k = 0; k < glyphs.size(); k++) {
303        Glyph glyph = glyphs[k];
304        float advance = advances[k] / cLogicalScale;
305        float offsetX = offsets[k].du / cLogicalScale;
306        float offsetY = offsets[k].dv / cLogicalScale;
307
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);
314        }
315
316        advance += fontData->syntheticBoldOffset();
317
318        if (hasExtraSpacing) {
319            // If we're a glyph with an advance, go ahead and add in letter-spacing.
320            // That way we weed out zero width lurkers.  This behavior matches the fast text code path.
321            if (advance && m_font.letterSpacing())
322                advance += m_font.letterSpacing();
323
324            // Handle justification and word-spacing.
325            int characterIndex = spaceCharacters[k];
326            // characterIndex is left at the initial value of -1 for glyphs that do not map back to treated-as-space characters.
327            if (characterIndex != -1) {
328                // Account for padding. WebCore uses space padding to justify text.
329                // We distribute the specified padding over the available spaces in the run.
330                if (m_padding) {
331                    // Use leftover padding if not evenly divisible by number of spaces.
332                    if (m_padding < m_padPerSpace) {
333                        advance += m_padding;
334                        m_padding = 0;
335                    } else {
336                        m_padding -= m_padPerSpace;
337                        advance += m_padPerSpace;
338                    }
339                }
340
341                // Account for word-spacing.
342                if (characterIndex > 0 && m_font.wordSpacing()) {
343                    UChar candidateSpace;
344                    if (m_run.is8Bit())
345                        candidateSpace = *(m_run.data8(characterIndex - 1));
346                    else
347                        candidateSpace = *(m_run.data16(characterIndex - 1));
348
349                    if (!Font::treatAsSpace(candidateSpace))
350                        advance += m_font.wordSpacing();
351                }
352            }
353        }
354
355        m_runWidthSoFar += advance;
356
357        // FIXME: We need to take the GOFFSETS for combining glyphs and store them in the glyph buffer
358        // as well, so that when the time comes to draw those glyphs, we can apply the appropriate
359        // translation.
360        if (glyphBuffer) {
361            FloatSize size(offsetX, -offsetY);
362            glyphBuffer->add(glyph, fontData, advance, GlyphBuffer::kNoOffset, &size);
363        }
364
365        FloatRect glyphBounds = fontData->boundsForGlyph(glyph);
366        glyphBounds.move(m_glyphOrigin.x(), m_glyphOrigin.y());
367        m_minGlyphBoundingBoxX = min(m_minGlyphBoundingBoxX, glyphBounds.x());
368        m_maxGlyphBoundingBoxX = max(m_maxGlyphBoundingBoxX, glyphBounds.maxX());
369        m_minGlyphBoundingBoxY = min(m_minGlyphBoundingBoxY, glyphBounds.y());
370        m_maxGlyphBoundingBoxY = max(m_maxGlyphBoundingBoxY, glyphBounds.maxY());
371        m_glyphOrigin.move(advance + offsetX, -offsetY);
372
373        // Mutate the glyph array to contain our altered advances.
374        if (m_computingOffsetPosition)
375            advances[k] = advance;
376    }
377
378    while (m_computingOffsetPosition && m_offsetX >= leftEdge && m_offsetX < m_runWidthSoFar) {
379        // The position is somewhere inside this run.
380        int trailing = 0;
381        ScriptXtoCP(m_offsetX - leftEdge, clusters.size(), glyphs.size(), clusters.data(), visualAttributes.data(),
382                    advances.data(), &item.a, &m_offsetPosition, &trailing);
383        if (trailing && m_includePartialGlyphs && m_offsetPosition < len - 1) {
384            m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
385            m_offsetX += m_run.rtl() ? -trailing : trailing;
386        } else {
387            m_computingOffsetPosition = false;
388            m_offsetPosition += m_currentCharacter + m_items[i].iCharPos;
389            if (trailing && m_includePartialGlyphs)
390               m_offsetPosition++;
391            return false;
392        }
393    }
394
395    return true;
396}
397
398bool UniscribeController::shape(const UChar* str, int len, SCRIPT_ITEM item, const SimpleFontData* fontData,
399                                Vector<WORD>& glyphs, Vector<WORD>& clusters,
400                                Vector<SCRIPT_VISATTR>& visualAttributes)
401{
402    HWndDC hdc;
403    HFONT oldFont = 0;
404    HRESULT shapeResult = E_PENDING;
405    int glyphCount = 0;
406    do {
407        shapeResult = ScriptShape(hdc, fontData->scriptCache(), str, len, glyphs.size(), &item.a,
408                                  glyphs.data(), clusters.data(), visualAttributes.data(), &glyphCount);
409        if (shapeResult == E_PENDING) {
410            // The script cache isn't primed with enough info yet.  We need to select our HFONT into
411            // a DC and pass the DC in to ScriptShape.
412            ASSERT(!hdc);
413            hdc.setHWnd(0);
414            HFONT hfont = fontData->platformData().hfont();
415            oldFont = (HFONT)SelectObject(hdc, hfont);
416        } else if (shapeResult == E_OUTOFMEMORY) {
417            // Need to resize our buffers.
418            glyphs.resize(glyphs.size() * 2);
419            visualAttributes.resize(glyphs.size());
420        }
421    } while (shapeResult == E_PENDING || shapeResult == E_OUTOFMEMORY);
422
423    if (hdc)
424        SelectObject(hdc, oldFont);
425
426    if (FAILED(shapeResult))
427        return false;
428
429    glyphs.shrink(glyphCount);
430    visualAttributes.shrink(glyphCount);
431
432    return true;
433}
434
435}
436