Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / WidthIterator.h
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 "platform/PlatformExport.h"
26 #include "platform/fonts/Font.h"
27 #include "platform/fonts/SVGGlyph.h"
28 #include "platform/text/TextRun.h"
29 #include "wtf/HashSet.h"
30 #include "wtf/unicode/Unicode.h"
31 #include "wtf/Vector.h"
32
33 namespace WebCore {
34
35 class Font;
36 class GlyphBuffer;
37 class SimpleFontData;
38 class TextRun;
39 struct GlyphData;
40
41 struct PLATFORM_EXPORT WidthIterator {
42     WTF_MAKE_FAST_ALLOCATED;
43 public:
44     WidthIterator(const Font*, const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, bool accountForGlyphBounds = false, bool forTextEmphasis = false);
45
46     unsigned advance(int to, GlyphBuffer*);
47     bool advanceOneCharacter(float& width, GlyphBuffer&);
48
49     float maxGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_maxGlyphBoundingBoxY; }
50     float minGlyphBoundingBoxY() const { ASSERT(m_accountForGlyphBounds); return m_minGlyphBoundingBoxY; }
51     float firstGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_firstGlyphOverflow; }
52     float lastGlyphOverflow() const { ASSERT(m_accountForGlyphBounds); return m_lastGlyphOverflow; }
53
54     const TextRun& run() const { return m_run; }
55     float runWidthSoFar() const { return m_runWidthSoFar; }
56
57 #if ENABLE(SVG_FONTS)
58     String lastGlyphName() const { return m_lastGlyphName; }
59     void setLastGlyphName(const String& name) { m_lastGlyphName = name; }
60     Vector<SVGGlyph::ArabicForm>& arabicForms() { return m_arabicForms; }
61 #endif
62
63     static bool supportsTypesettingFeatures(const Font& font)
64     {
65
66         return !font.fontDescription().typesettingFeatures();
67     }
68
69     const Font* m_font;
70
71     const TextRun& m_run;
72
73     unsigned m_currentCharacter;
74     float m_runWidthSoFar;
75     float m_expansion;
76     float m_expansionPerOpportunity;
77     bool m_isAfterExpansion;
78     float m_finalRoundingWidth;
79
80 #if ENABLE(SVG_FONTS)
81     String m_lastGlyphName;
82     Vector<SVGGlyph::ArabicForm> m_arabicForms;
83 #endif
84
85 private:
86     GlyphData glyphDataForCharacter(UChar32, bool mirror, int currentCharacter, unsigned& advanceLength);
87     template <typename TextIterator>
88     inline unsigned advanceInternal(TextIterator&, GlyphBuffer*);
89
90     bool shouldApplyFontTransforms() const { return m_run.length() > 1 && (m_typesettingFeatures & (Kerning | Ligatures)); }
91
92     TypesettingFeatures m_typesettingFeatures;
93     HashSet<const SimpleFontData*>* m_fallbackFonts;
94     bool m_accountForGlyphBounds;
95     float m_maxGlyphBoundingBoxY;
96     float m_minGlyphBoundingBoxY;
97     float m_firstGlyphOverflow;
98     float m_lastGlyphOverflow;
99     bool m_forTextEmphasis;
100 };
101
102 }
103
104 #endif