#ifndef ComplexTextController_h
#define ComplexTextController_h
#include "platform/fonts/GlyphBuffer.h"
#include "wtf/HashSet.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RetainPtr.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/Unicode.h"
#include "wtf/Vector.h"
typedef unsigned short CGGlyph;
typedef const struct __CTRun * CTRunRef;
typedef const struct __CTLine * CTLineRef;
namespace WebCore {
class Font;
class SimpleFontData;
class TextRun;
enum GlyphIterationStyle { IncludePartialGlyphs, ByWholeGlyphs };
class ComplexTextController {
public:
ComplexTextController(const Font*, const TextRun&, bool mayUseNaturalWritingDirection = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool forTextEmphasis = false);
void advance(unsigned to, GlyphBuffer* = 0, GlyphIterationStyle = IncludePartialGlyphs, HashSet<const SimpleFontData*>* fallbackFonts = 0);
int offsetForPosition(float x, bool includePartialGlyphs);
float runWidthSoFar() const { return m_runWidthSoFar; }
float totalWidth() const { return m_totalWidth; }
float finalRoundingWidth() const { return m_finalRoundingWidth; }
float minGlyphBoundingBoxX() const { return m_minGlyphBoundingBoxX; }
float maxGlyphBoundingBoxX() const { return m_maxGlyphBoundingBoxX; }
float minGlyphBoundingBoxY() const { return m_minGlyphBoundingBoxY; }
float maxGlyphBoundingBoxY() const { return m_maxGlyphBoundingBoxY; }
private:
class ComplexTextRun : public RefCounted<ComplexTextRun> {
public:
static PassRefPtr<ComplexTextRun> create(CTRunRef ctRun, const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
{
return adoptRef(new ComplexTextRun(ctRun, fontData, characters, stringLocation, stringLength, runRange));
}
static PassRefPtr<ComplexTextRun> create(const SimpleFontData* fontData, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr)
{
return adoptRef(new ComplexTextRun(fontData, characters, stringLocation, stringLength, ltr));
}
unsigned glyphCount() const { return m_glyphCount; }
const SimpleFontData* fontData() const { return m_fontData; }
const UChar* characters() const { return m_characters; }
unsigned stringLocation() const { return m_stringLocation; }
size_t stringLength() const { return m_stringLength; }
ALWAYS_INLINE CFIndex indexAt(size_t i) const;
CFIndex indexBegin() const { return m_indexBegin; }
CFIndex indexEnd() const { return m_indexEnd; }
CFIndex endOffsetAt(size_t i) const { ASSERT(!m_isMonotonic); return m_glyphEndOffsets[i]; }
const CGGlyph* glyphs() const { return m_glyphs; }
const CGSize* advances() const { return m_advances; }
bool isLTR() const { return m_isLTR; }
bool isMonotonic() const { return m_isMonotonic; }
void setIsNonMonotonic();
private:
ComplexTextRun(CTRunRef, const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange);
ComplexTextRun(const SimpleFontData*, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr);
unsigned m_glyphCount;
const SimpleFontData* m_fontData;
const UChar* m_characters;
unsigned m_stringLocation;
size_t m_stringLength;
Vector<CFIndex, 64> m_coreTextIndicesVector;
const CFIndex* m_coreTextIndices;
CFIndex m_indexBegin;
CFIndex m_indexEnd;
Vector<CFIndex, 64> m_glyphEndOffsets;
Vector<CGGlyph, 64> m_glyphsVector;
const CGGlyph* m_glyphs;
Vector<CGSize, 64> m_advancesVector;
const CGSize* m_advances;
bool m_isLTR;
bool m_isMonotonic;
};
static unsigned stringBegin(const ComplexTextRun& run) { return run.stringLocation() + run.indexBegin(); }
static unsigned stringEnd(const ComplexTextRun& run) { return run.stringLocation() + run.indexEnd(); }
void collectComplexTextRuns();
void collectComplexTextRunsForCharacters(const UChar*, unsigned length, unsigned stringLocation, const SimpleFontData*);
void adjustGlyphsAndAdvances();
unsigned findNextRunIndex(unsigned runIndex) const;
unsigned indexOfCurrentRun(unsigned& leftmostGlyph);
unsigned incrementCurrentRun(unsigned& leftmostGlyph);
Vector<unsigned, 16> m_runIndices;
Vector<unsigned, 16> m_glyphCountFromStartToIndex;
const Font& m_font;
const TextRun& m_run;
bool m_isLTROnly;
bool m_mayUseNaturalWritingDirection;
bool m_forTextEmphasis;
Vector<String> m_stringsFor8BitRuns;
Vector<UChar, 256> m_smallCapsBuffer;
Vector<RetainPtr<CTLineRef> > m_coreTextLines;
Vector<RefPtr<ComplexTextRun>, 16> m_complexTextRuns;
Vector<CGSize, 256> m_adjustedAdvances;
Vector<CGGlyph, 256> m_adjustedGlyphs;
unsigned m_currentCharacter;
int m_end;
CGFloat m_totalWidth;
float m_runWidthSoFar;
unsigned m_numGlyphsSoFar;
size_t m_currentRun;
unsigned m_glyphInCurrentRun;
unsigned m_characterInCurrentGlyph;
float m_finalRoundingWidth;
float m_expansion;
float m_expansionPerOpportunity;
float m_leadingExpansion;
bool m_afterExpansion;
HashSet<const SimpleFontData*>* m_fallbackFonts;
float m_minGlyphBoundingBoxX;
float m_maxGlyphBoundingBoxX;
float m_minGlyphBoundingBoxY;
float m_maxGlyphBoundingBoxY;
unsigned m_lastRoundingGlyph;
};
}
#endif