ComplexTextController.h [plain text]
#ifndef ComplexTextController_h
#define ComplexTextController_h
#include "GlyphBuffer.h"
#include <wtf/HashSet.h>
#include <wtf/RefCounted.h>
#include <wtf/RetainPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
#define USE_LAYOUT_SPECIFIC_ADVANCES ((PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000))
typedef unsigned short CGGlyph;
typedef const struct __CTRun * CTRunRef;
typedef const struct __CTLine * CTLineRef;
namespace WTF {
template<> struct VectorTraits<CGPoint> : SimpleClassVectorTraits { };
}
namespace WebCore {
class FontCascade;
class Font;
class TextRun;
enum GlyphIterationStyle { IncludePartialGlyphs, ByWholeGlyphs };
class ComplexTextController {
WTF_MAKE_FAST_ALLOCATED;
public:
ComplexTextController(const FontCascade&, const TextRun&, bool mayUseNaturalWritingDirection = false, HashSet<const Font*>* fallbackFonts = 0, bool forTextEmphasis = false);
void advance(unsigned to, GlyphBuffer* = nullptr, GlyphIterationStyle = IncludePartialGlyphs, HashSet<const Font*>* fallbackFonts = nullptr);
unsigned offsetForPosition(float x, bool includePartialGlyphs);
float runWidthSoFar() const { return m_runWidthSoFar; }
float totalWidth() const { return m_totalWidth; }
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 Ref<ComplexTextRun> create(CTRunRef ctRun, const Font& font, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange)
{
return adoptRef(*new ComplexTextRun(ctRun, font, characters, stringLocation, stringLength, runRange));
}
static Ref<ComplexTextRun> create(const Font& font, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr)
{
return adoptRef(*new ComplexTextRun(font, characters, stringLocation, stringLength, ltr));
}
unsigned glyphCount() const { return m_glyphCount; }
const Font& font() const { return m_font; }
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; }
void growInitialAdvanceHorizontally(CGFloat delta) { m_initialAdvance.width += delta; }
CGSize initialAdvance() const { return m_initialAdvance; }
const CGSize* baseAdvances() const { return m_baseAdvances; }
const CGPoint* glyphOrigins() const { return m_glyphOrigins.size() == glyphCount() ? m_glyphOrigins.data() : nullptr; }
bool isLTR() const { return m_isLTR; }
bool isMonotonic() const { return m_isMonotonic; }
void setIsNonMonotonic();
private:
ComplexTextRun(CTRunRef, const Font&, const UChar* characters, unsigned stringLocation, size_t stringLength, CFRange runRange);
ComplexTextRun(const Font&, const UChar* characters, unsigned stringLocation, size_t stringLength, bool ltr);
Vector<CGSize, 64> m_baseAdvancesVector;
Vector<CGPoint, 64> m_glyphOrigins;
Vector<CGGlyph, 64> m_glyphsVector;
Vector<CFIndex, 64> m_glyphEndOffsets;
Vector<CFIndex, 64> m_coreTextIndicesVector;
CGSize m_initialAdvance;
const Font& m_font;
const UChar* m_characters;
size_t m_stringLength;
const CFIndex* m_coreTextIndices;
const CGGlyph* m_glyphs;
const CGSize* m_baseAdvances;
CFIndex m_indexBegin;
CFIndex m_indexEnd;
unsigned m_glyphCount;
unsigned m_stringLocation;
bool m_isLTR;
bool m_isMonotonic;
};
void computeExpansionOpportunity();
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 Font*);
void adjustGlyphsAndAdvances();
unsigned indexOfCurrentRun(unsigned& leftmostGlyph);
unsigned incrementCurrentRun(unsigned& leftmostGlyph);
float runWidthSoFarFraction(unsigned glyphStartOffset, unsigned glyphEndOffset, unsigned oldCharacterInCurrentGlyph, GlyphIterationStyle) const;
CGPoint glyphOrigin(unsigned index) const { return index < m_glyphOrigins.size() ? m_glyphOrigins[index] : CGPointZero; }
Vector<CGSize, 256> m_adjustedBaseAdvances;
Vector<CGPoint, 256> m_glyphOrigins;
Vector<CGGlyph, 256> m_adjustedGlyphs;
Vector<UChar, 256> m_smallCapsBuffer;
Vector<RefPtr<ComplexTextRun>, 16> m_complexTextRuns;
Vector<unsigned, 16> m_runIndices;
Vector<unsigned, 16> m_glyphCountFromStartToIndex;
Vector<RetainPtr<CTLineRef>> m_coreTextLines;
Vector<String> m_stringsFor8BitRuns;
HashSet<const Font*>* m_fallbackFonts { nullptr };
const FontCascade& m_font;
const TextRun& m_run;
unsigned m_currentCharacter { 0 };
unsigned m_end { 0 };
float m_totalWidth { 0 };
float m_runWidthSoFar { 0 };
unsigned m_numGlyphsSoFar { 0 };
unsigned m_currentRun { 0 };
unsigned m_glyphInCurrentRun { 0 };
unsigned m_characterInCurrentGlyph { 0 };
float m_expansion { 0 };
float m_expansionPerOpportunity { 0 };
float m_minGlyphBoundingBoxX { std::numeric_limits<float>::max() };
float m_maxGlyphBoundingBoxX { std::numeric_limits<float>::min() };
float m_minGlyphBoundingBoxY { std::numeric_limits<float>::max() };
float m_maxGlyphBoundingBoxY { std::numeric_limits<float>::min() };
bool m_isLTROnly { true };
bool m_mayUseNaturalWritingDirection { false };
bool m_forTextEmphasis { false };
};
}
#endif // ComplexTextController_h