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 "core/platform/graphics/GlyphBuffer.h" 29 #include "wtf/HashSet.h" 30 #include "wtf/PassRefPtr.h" 31 #include "wtf/RefCounted.h" 32 #include "wtf/RetainPtr.h" 33 #include "wtf/text/WTFString.h" 34 #include "wtf/unicode/Unicode.h" 35 #include "wtf/Vector.h" 36 37 typedef unsigned short CGGlyph; 38 39 typedef const struct __CTRun * CTRunRef; 40 typedef const struct __CTLine * CTLineRef; 41 42 namespace WebCore { 43 44 class Font; 45 class SimpleFontData; 46 class TextRun; 47 48 enum GlyphIterationStyle { IncludePartialGlyphs, ByWholeGlyphs }; 49 50 // ComplexTextController is responsible for rendering and measuring glyphs for 51 // complex scripts on OS X. 52 class ComplexTextController { 53 public: 54 ComplexTextController(const Font*, const TextRun&, bool mayUseNaturalWritingDirection = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool forTextEmphasis = false); 55 56 // Advance and emit glyphs up to the specified character. 57 void advance(unsigned to, GlyphBuffer* = 0, GlyphIterationStyle = IncludePartialGlyphs, HashSet<const SimpleFontData*>* fallbackFonts = 0); 58 59 // Compute the character offset for a given x coordinate. 60 int offsetForPosition(float x, bool includePartialGlyphs); 61 62 // Returns the width of everything we've consumed so far. 63 float runWidthSoFar() const { return m_runWidthSoFar; } 64 65 float totalWidth() const { return m_totalWidth; } 66 67 float finalRoundingWidth() const { return m_finalRoundingWidth; } 68 69 float minGlyphBoundingBoxX() const { return m_minGlyphBoundingBoxX; } 70 float maxGlyphBoundingBoxX() const { return m_maxGlyphBoundingBoxX; } 71 float minGlyphBoundingBoxY() const { return m_minGlyphBoundingBoxY; } 72 float maxGlyphBoundingBoxY() const { return m_maxGlyphBoundingBoxY; } 73 74 private: 75 class ComplexTextRun : public RefCounted<ComplexTextRun> { 76 public: 77 static PassRefPtr<ComplexTextRun> create(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange) 78 { 79 return adoptRef(new ComplexTextRun(ctRun, fontData, characters, stringLocation, stringLength, runRange)); 80 } 81 82 static PassRefPtr<ComplexTextRun> create(const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr) 83 { 84 return adoptRef(new ComplexTextRun(fontData, characters, stringLocation, stringLength, ltr)); 85 } 86 87 unsigned glyphCount() const { return m_glyphCount; } 88 const SimpleFontData* fontData() const { return m_fontData; } 89 const UChar* characters() const { return m_characters; } 90 unsigned stringLocation() const { return m_stringLocation; } 91 size_t stringLength() const { return m_stringLength; } 92 ALWAYS_INLINE CFIndex indexAt(size_t i) const; 93 CFIndex indexBegin() const { return m_indexBegin; } 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 isLTR() const { return m_isLTR; } 99 bool isMonotonic() const { return m_isMonotonic; } 100 void setIsNonMonotonic(); 101 102 private: 103 ComplexTextRun(CTRunRef, const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange); 104 ComplexTextRun(const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr); 105 106 unsigned m_glyphCount; 107 const SimpleFontData* m_fontData; 108 const UChar* m_characters; 109 unsigned m_stringLocation; 110 size_t m_stringLength; 111 Vector<CFIndex, 64> m_coreTextIndicesVector; 112 const CFIndex* m_coreTextIndices; 113 CFIndex m_indexBegin; 114 CFIndex m_indexEnd; 115 Vector<CFIndex, 64> m_glyphEndOffsets; 116 Vector<CGGlyph, 64> m_glyphsVector; 117 const CGGlyph* m_glyphs; 118 Vector<CGSize, 64> m_advancesVector; 119 const CGSize* m_advances; 120 bool m_isLTR; 121 bool m_isMonotonic; 122 }; 123 124 static unsigned stringBegin(const ComplexTextRun& run) { return run.stringLocation() + run.indexBegin(); } 125 static unsigned stringEnd(const ComplexTextRun& run) { return run.stringLocation() + run.indexEnd(); } 126 127 void collectComplexTextRuns(); 128 129 void collectComplexTextRunsForCharacters(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*); 130 void adjustGlyphsAndAdvances(); 131 132 unsigned findNextRunIndex(unsigned runIndex) const; 133 unsigned indexOfCurrentRun(unsigned& leftmostGlyph); 134 unsigned incrementCurrentRun(unsigned& leftmostGlyph); 135 136 // The initial capacity of these vectors was selected as being the smallest power of two greater than 137 // the average (3.5) plus one standard deviation (7.5) of nonzero sizes used on Arabic Wikipedia. 138 Vector<unsigned, 16> m_runIndices; 139 Vector<unsigned, 16> m_glyphCountFromStartToIndex; 140 141 const Font& m_font; 142 const TextRun& m_run; 143 bool m_isLTROnly; 144 bool m_mayUseNaturalWritingDirection; 145 bool m_forTextEmphasis; 146 147 Vector<String> m_stringsFor8BitRuns; 148 Vector<UChar, 256> m_smallCapsBuffer; 149 150 // Retain lines rather than their runs for better performance. 151 Vector<RetainPtr<CTLineRef> > m_coreTextLines; 152 Vector<RefPtr<ComplexTextRun>, 16> m_complexTextRuns; 153 Vector<CGSize, 256> m_adjustedAdvances; 154 Vector<CGGlyph, 256> m_adjustedGlyphs; 155 156 unsigned m_currentCharacter; 157 int m_end; 158 159 CGFloat m_totalWidth; 160 161 float m_runWidthSoFar; 162 unsigned m_numGlyphsSoFar; 163 size_t m_currentRun; 164 unsigned m_glyphInCurrentRun; 165 unsigned m_characterInCurrentGlyph; 166 float m_finalRoundingWidth; 167 float m_expansion; 168 float m_expansionPerOpportunity; 169 float m_leadingExpansion; 170 bool m_afterExpansion; 171 172 HashSet<const SimpleFontData*>* m_fallbackFonts; 173 174 float m_minGlyphBoundingBoxX; 175 float m_maxGlyphBoundingBoxX; 176 float m_minGlyphBoundingBoxY; 177 float m_maxGlyphBoundingBoxY; 178 179 unsigned m_lastRoundingGlyph; 180 }; 181 182 } // namespace WebCore 183 184 #endif // ComplexTextController_h 185