2 * Copyright (C) 2007, 2008, 2009, 2011 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
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.
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.
25 #ifndef ComplexTextController_h
26 #define ComplexTextController_h
28 #include "wtf/HashSet.h"
29 #include "wtf/PassRefPtr.h"
30 #include "wtf/RefCounted.h"
31 #include "wtf/RetainPtr.h"
32 #include "wtf/text/WTFString.h"
33 #include "wtf/unicode/Unicode.h"
34 #include "wtf/Vector.h"
35 #include <ApplicationServices/ApplicationServices.h>
37 typedef unsigned short CGGlyph;
39 typedef const struct __CTRun * CTRunRef;
40 typedef const struct __CTLine * CTLineRef;
49 enum GlyphIterationStyle { IncludePartialGlyphs, ByWholeGlyphs };
51 // ComplexTextController is responsible for rendering and measuring glyphs for
52 // complex scripts on OS X.
53 class ComplexTextController {
55 ComplexTextController(const Font*, const TextRun&, bool mayUseNaturalWritingDirection = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool forTextEmphasis = false);
57 // Advance and emit glyphs up to the specified character.
58 void advance(unsigned to, GlyphBuffer* = 0, GlyphIterationStyle = IncludePartialGlyphs, HashSet<const SimpleFontData*>* fallbackFonts = 0);
60 // Compute the character offset for a given x coordinate.
61 int offsetForPosition(float x, bool includePartialGlyphs);
63 // Returns the width of everything we've consumed so far.
64 float runWidthSoFar() const { return m_runWidthSoFar; }
66 float totalWidth() const { return m_totalWidth; }
68 float minGlyphBoundingBoxX() const { return m_minGlyphBoundingBoxX; }
69 float maxGlyphBoundingBoxX() const { return m_maxGlyphBoundingBoxX; }
70 float minGlyphBoundingBoxY() const { return m_minGlyphBoundingBoxY; }
71 float maxGlyphBoundingBoxY() const { return m_maxGlyphBoundingBoxY; }
74 class ComplexTextRun : public RefCounted<ComplexTextRun> {
76 static PassRefPtr<ComplexTextRun> create(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
78 return adoptRef(new ComplexTextRun(ctRun, fontData, characters, stringLocation, stringLength, runRange));
81 static PassRefPtr<ComplexTextRun> create(const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr)
83 return adoptRef(new ComplexTextRun(fontData, characters, stringLocation, stringLength, ltr));
86 unsigned glyphCount() const { return m_glyphCount; }
87 const SimpleFontData* fontData() const { return m_fontData; }
88 const UChar* characters() const { return m_characters; }
89 unsigned stringLocation() const { return m_stringLocation; }
90 size_t stringLength() const { return m_stringLength; }
91 ALWAYS_INLINE CFIndex indexAt(size_t i) const;
92 CFIndex indexBegin() const { return m_indexBegin; }
93 CFIndex indexEnd() const { return m_indexEnd; }
94 CFIndex endOffsetAt(size_t i) const { ASSERT(!m_isMonotonic); return m_glyphEndOffsets[i]; }
95 const CGGlyph* glyphs() const { return m_glyphs; }
96 const CGSize* advances() const { return m_advances; }
97 bool isLTR() const { return m_isLTR; }
98 bool isMonotonic() const { return m_isMonotonic; }
99 void setIsNonMonotonic();
102 ComplexTextRun(CTRunRef, const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange);
103 ComplexTextRun(const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr);
105 unsigned m_glyphCount;
106 const SimpleFontData* m_fontData;
107 const UChar* m_characters;
108 unsigned m_stringLocation;
109 size_t m_stringLength;
110 Vector<CFIndex, 64> m_coreTextIndicesVector;
111 const CFIndex* m_coreTextIndices;
112 CFIndex m_indexBegin;
114 Vector<CFIndex, 64> m_glyphEndOffsets;
115 Vector<CGGlyph, 64> m_glyphsVector;
116 const CGGlyph* m_glyphs;
117 Vector<CGSize, 64> m_advancesVector;
118 const CGSize* m_advances;
123 static unsigned stringBegin(const ComplexTextRun& run) { return run.stringLocation() + run.indexBegin(); }
124 static unsigned stringEnd(const ComplexTextRun& run) { return run.stringLocation() + run.indexEnd(); }
126 void collectComplexTextRuns();
128 void collectComplexTextRunsForCharacters(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*);
129 void adjustGlyphsAndAdvances();
131 unsigned findNextRunIndex(unsigned runIndex) const;
132 unsigned indexOfCurrentRun(unsigned& leftmostGlyph);
133 unsigned incrementCurrentRun(unsigned& leftmostGlyph);
135 // The initial capacity of these vectors was selected as being the smallest power of two greater than
136 // the average (3.5) plus one standard deviation (7.5) of nonzero sizes used on Arabic Wikipedia.
137 Vector<unsigned, 16> m_runIndices;
138 Vector<unsigned, 16> m_glyphCountFromStartToIndex;
141 const TextRun& m_run;
143 bool m_mayUseNaturalWritingDirection;
144 bool m_forTextEmphasis;
146 Vector<String> m_stringsFor8BitRuns;
147 Vector<UChar, 256> m_smallCapsBuffer;
149 // Retain lines rather than their runs for better performance.
150 Vector<RetainPtr<CTLineRef> > m_coreTextLines;
151 Vector<RefPtr<ComplexTextRun>, 16> m_complexTextRuns;
152 Vector<CGSize, 256> m_adjustedAdvances;
153 Vector<CGGlyph, 256> m_adjustedGlyphs;
155 unsigned m_currentCharacter;
158 CGFloat m_totalWidth;
160 float m_runWidthSoFar;
161 unsigned m_numGlyphsSoFar;
163 unsigned m_glyphInCurrentRun;
164 unsigned m_characterInCurrentGlyph;
166 float m_expansionPerOpportunity;
167 float m_leadingExpansion;
168 bool m_afterExpansion;
170 HashSet<const SimpleFontData*>* m_fallbackFonts;
172 float m_minGlyphBoundingBoxX;
173 float m_maxGlyphBoundingBoxX;
174 float m_minGlyphBoundingBoxY;
175 float m_maxGlyphBoundingBoxY;
180 #endif // ComplexTextController_h