Home | History | Annotate | Download | only in mac
      1 /*
      2  * Copyright (C) 2007, 2008, 2009, 2011 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'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  */
     24 
     25 #ifndef ComplexTextController_h
     26 #define ComplexTextController_h
     27 
     28 #include <ApplicationServices/ApplicationServices.h>
     29 #include "GlyphBuffer.h"
     30 #include <wtf/HashSet.h>
     31 #include <wtf/PassRefPtr.h>
     32 #include <wtf/RefCounted.h>
     33 #include <wtf/RetainPtr.h>
     34 #include <wtf/Vector.h>
     35 #include <wtf/unicode/Unicode.h>
     36 
     37 namespace WebCore {
     38 
     39 class Font;
     40 class SimpleFontData;
     41 class TextRun;
     42 
     43 // ComplexTextController is responsible for rendering and measuring glyphs for
     44 // complex scripts on OS X.
     45 // The underlying API can be selected at compile time based on USE(ATSUI) and
     46 // USE(CORE_TEXT).  If both are defined then the Core Text APIs are used for
     47 // OS Versions >= 10.6, ATSUI is used otherwise.
     48 class ComplexTextController {
     49 public:
     50     ComplexTextController(const Font*, const TextRun&, bool mayUseNaturalWritingDirection = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool forTextEmphasis = false);
     51 
     52     // Advance and emit glyphs up to the specified character.
     53     void advance(unsigned to, GlyphBuffer* = 0);
     54 
     55     // Compute the character offset for a given x coordinate.
     56     int offsetForPosition(float x, bool includePartialGlyphs);
     57 
     58     // Returns the width of everything we've consumed so far.
     59     float runWidthSoFar() const { return m_runWidthSoFar; }
     60 
     61     float totalWidth() const { return m_totalWidth; }
     62 
     63     float minGlyphBoundingBoxX() const { return m_minGlyphBoundingBoxX; }
     64     float maxGlyphBoundingBoxX() const { return m_maxGlyphBoundingBoxX; }
     65     float minGlyphBoundingBoxY() const { return m_minGlyphBoundingBoxY; }
     66     float maxGlyphBoundingBoxY() const { return m_maxGlyphBoundingBoxY; }
     67 
     68 private:
     69     class ComplexTextRun : public RefCounted<ComplexTextRun> {
     70     public:
     71 #if USE(CORE_TEXT)
     72         static PassRefPtr<ComplexTextRun> create(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
     73         {
     74             return adoptRef(new ComplexTextRun(ctRun, fontData, characters, stringLocation, stringLength, runRange));
     75         }
     76 #endif
     77 #if USE(ATSUI)
     78         static PassRefPtr<ComplexTextRun> create(ATSUTextLayout atsuTextLayout, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr, bool directionalOverride)
     79         {
     80             return adoptRef(new ComplexTextRun(atsuTextLayout, fontData, characters, stringLocation, stringLength, ltr, directionalOverride));
     81         }
     82 #endif
     83         static PassRefPtr<ComplexTextRun> create(const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr)
     84         {
     85             return adoptRef(new ComplexTextRun(fontData, characters, stringLocation, stringLength, ltr));
     86         }
     87 
     88         unsigned glyphCount() const { return m_glyphCount; }
     89         const SimpleFontData* fontData() const { return m_fontData; }
     90         const UChar* characters() const { return m_characters; }
     91         unsigned stringLocation() const { return m_stringLocation; }
     92         size_t stringLength() const { return m_stringLength; }
     93         ALWAYS_INLINE CFIndex indexAt(size_t i) const;
     94         CFIndex indexEnd() const { return m_indexEnd; }
     95         CFIndex endOffsetAt(size_t i) const { ASSERT(!m_isMonotonic); return m_glyphEndOffsets[i]; }
     96         const CGGlyph* glyphs() const { return m_glyphs; }
     97         const CGSize* advances() const { return m_advances; }
     98         bool isMonotonic() const { return m_isMonotonic; }
     99         void setIsNonMonotonic();
    100 
    101     private:
    102 #if USE(CORE_TEXT)
    103         ComplexTextRun(CTRunRef, const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange);
    104         void createTextRunFromFontDataCoreText(bool ltr);
    105 #endif
    106 #if USE(ATSUI)
    107         ComplexTextRun(ATSUTextLayout, const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr, bool directionalOverride);
    108         void createTextRunFromFontDataATSUI(bool ltr);
    109 #endif
    110         ComplexTextRun(const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr);
    111 
    112 #if USE(ATSUI)
    113 #ifdef BUILDING_ON_TIGER
    114         typedef UInt32 URefCon;
    115 #endif
    116         static OSStatus overrideLayoutOperation(ATSULayoutOperationSelector, ATSULineRef, URefCon, void*, ATSULayoutOperationCallbackStatus*);
    117 #endif
    118 
    119         unsigned m_glyphCount;
    120         const SimpleFontData* m_fontData;
    121         const UChar* m_characters;
    122         unsigned m_stringLocation;
    123         size_t m_stringLength;
    124 #if USE(CORE_TEXT)
    125         Vector<CFIndex, 64> m_coreTextIndicesVector;
    126         const CFIndex* m_coreTextIndices;
    127 #endif
    128 #if USE(ATSUI)
    129         Vector<CFIndex, 64> m_atsuiIndices;
    130 #endif
    131         CFIndex m_indexEnd;
    132         Vector<CFIndex, 64> m_glyphEndOffsets;
    133         Vector<CGGlyph, 64> m_glyphsVector;
    134         const CGGlyph* m_glyphs;
    135         Vector<CGSize, 64> m_advancesVector;
    136         const CGSize* m_advances;
    137 #if USE(ATSUI)
    138         bool m_directionalOverride;
    139 #endif
    140         bool m_isMonotonic;
    141     };
    142 
    143     void collectComplexTextRuns();
    144 
    145     // collectComplexTextRunsForCharacters() is a stub function that calls through to the ATSUI or Core Text variants based
    146     // on the API in use.
    147     void collectComplexTextRunsForCharacters(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*);
    148     void collectComplexTextRunsForCharactersATSUI(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*);
    149     void collectComplexTextRunsForCharactersCoreText(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*);
    150     void adjustGlyphsAndAdvances();
    151 
    152     const Font& m_font;
    153     const TextRun& m_run;
    154     bool m_mayUseNaturalWritingDirection;
    155     bool m_forTextEmphasis;
    156 
    157     Vector<UChar, 256> m_smallCapsBuffer;
    158 
    159 #if USE(CORE_TEXT)
    160     // Retain lines rather than their runs for better performance.
    161     Vector<RetainPtr<CTLineRef> > m_coreTextLines;
    162 #endif
    163     Vector<RefPtr<ComplexTextRun>, 16> m_complexTextRuns;
    164     Vector<CGSize, 256> m_adjustedAdvances;
    165     Vector<CGGlyph, 256> m_adjustedGlyphs;
    166 
    167     unsigned m_currentCharacter;
    168     int m_end;
    169 
    170     CGFloat m_totalWidth;
    171 
    172     float m_runWidthSoFar;
    173     unsigned m_numGlyphsSoFar;
    174     size_t m_currentRun;
    175     unsigned m_glyphInCurrentRun;
    176     unsigned m_characterInCurrentGlyph;
    177     float m_expansion;
    178     float m_expansionPerOpportunity;
    179     float m_leadingExpansion;
    180     bool m_afterExpansion;
    181 
    182     HashSet<const SimpleFontData*>* m_fallbackFonts;
    183 
    184     float m_minGlyphBoundingBoxX;
    185     float m_maxGlyphBoundingBoxX;
    186     float m_minGlyphBoundingBoxY;
    187     float m_maxGlyphBoundingBoxY;
    188 };
    189 
    190 } // namespace WebCore
    191 
    192 #endif // ComplexTextController_h
    193