1/*
2 * Copyright (C) 2003, 2006, 2008, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Holger Hans Peter Freyther
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WidthIterator_h
23#define WidthIterator_h
24
25#include "Font.h"
26#include "SVGGlyph.h"
27#include "TextRun.h"
28#include <wtf/HashSet.h>
29#include <wtf/Vector.h>
30
31namespace WebCore {
32
33class Font;
34class GlyphBuffer;
35class SimpleFontData;
36class TextRun;
37struct GlyphData;
38
39struct WidthIterator {
40    WTF_MAKE_FAST_ALLOCATED;
41public:
42    WidthIterator(const Font*, const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool accountForGlyphBounds = false, bool forTextEmphasis = false);
43
44    unsigned advance(int to, GlyphBuffer*);
45    bool advanceOneCharacter(float& width, GlyphBuffer&);
46
47    float maxGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_maxGlyphBoundingBoxY; }
48    float minGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_minGlyphBoundingBoxY; }
49    float firstGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_firstGlyphOverflow; }
50    float lastGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_lastGlyphOverflow; }
51
52    const TextRun& run() const { return m_run; }
53    float runWidthSoFar() const { return m_runWidthSoFar; }
54
55#if ENABLE(SVG_FONTS)
56    String lastGlyphName() const { return m_lastGlyphName; }
57    void setLastGlyphName(const String& name) { m_lastGlyphName = name; }
58    Vector<SVGGlyph::ArabicForm>& arabicForms() { return m_arabicForms; }
59#endif
60
61    static bool supportsTypesettingFeatures(const Font& font)
62    {
63#if PLATFORM(IOS) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED > 1080)
64        if (!font.isPrinterFont())
65            return !font.typesettingFeatures();
66
67        return !(font.typesettingFeatures() & ~(Kerning | Ligatures));
68#else
69        return !font.typesettingFeatures();
70#endif
71    }
72
73    const Font* m_font;
74
75    const TextRun& m_run;
76
77    unsigned m_currentCharacter;
78    float m_runWidthSoFar;
79    float m_expansion;
80    float m_expansionPerOpportunity;
81    bool m_isAfterExpansion;
82    float m_finalRoundingWidth;
83
84#if ENABLE(SVG_FONTS)
85    String m_lastGlyphName;
86    Vector<SVGGlyph::ArabicForm> m_arabicForms;
87#endif
88
89private:
90    GlyphData glyphDataForCharacter(UChar32, bool mirror, int currentCharacter, unsigned& advanceLength);
91    template <typename TextIterator>
92    inline unsigned advanceInternal(TextIterator&, GlyphBuffer*);
93
94    bool shouldApplyFontTransforms() const { return m_run.length() > 1 && (m_typesettingFeatures & (Kerning | Ligatures)); }
95
96    TypesettingFeatures m_typesettingFeatures;
97    HashSet<const SimpleFontData*>* m_fallbackFonts;
98    bool m_accountForGlyphBounds;
99    float m_maxGlyphBoundingBoxY;
100    float m_minGlyphBoundingBoxY;
101    float m_firstGlyphOverflow;
102    float m_lastGlyphOverflow;
103    bool m_forTextEmphasis;
104};
105
106}
107
108#endif
109