2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2006, 2007 Apple Computer, Inc.
6 * Copyright (C) 2008 Holger Hans Peter Freyther
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
29 #include "FontDescription.h"
30 #include "SimpleFontData.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/MathExtras.h>
43 class FontFallbackList
;
44 class FontPlatformData
;
47 class GlyphPageTreeNode
;
48 class GraphicsContext
;
54 const unsigned defaultUnitsPerEm
= 1000;
59 Font(const FontDescription
&, float letterSpacing
, float wordSpacing
);
60 // This constructor is only used if the platform wants to start with a native font.
61 Font(const FontPlatformData
&, bool isPrinting
);
65 Font
& operator=(const Font
&);
67 bool operator==(const Font
& other
) const;
68 bool operator!=(const Font
& other
) const {
69 return !(*this == other
);
72 const FontDescription
& fontDescription() const { return m_fontDescription
; }
74 int pixelSize() const { return fontDescription().computedPixelSize(); }
75 float size() const { return fontDescription().computedSize(); }
77 void update(PassRefPtr
<FontSelector
>) const;
79 float drawText(GraphicsContext
*, const TextRun
&, const FloatPoint
&, int from
= 0, int to
= -1) const;
81 int width(const TextRun
& run
) const { return lroundf(floatWidth(run
)); }
82 float floatWidth(const TextRun
&) const;
83 float floatWidth(const TextRun
& run
, int extraCharsAvailable
, int& charsConsumed
, String
& glyphName
) const;
85 int offsetForPosition(const TextRun
&, int position
, bool includePartialGlyphs
) const;
86 FloatRect
selectionRectForText(const TextRun
&, const IntPoint
&, int h
, int from
= 0, int to
= -1) const;
88 bool isSmallCaps() const { return m_fontDescription
.smallCaps(); }
90 float wordSpacing() const { return m_wordSpacing
; }
91 float letterSpacing() const { return m_letterSpacing
; }
92 void setWordSpacing(float s
) { m_wordSpacing
= s
; }
93 void setLetterSpacing(float s
) { m_letterSpacing
= s
; }
94 bool isFixedPitch() const;
95 bool isPrinterFont() const { return m_fontDescription
.usePrinterFont(); }
97 FontRenderingMode
renderingMode() const { return m_fontDescription
.renderingMode(); }
99 FontFamily
& firstFamily() { return m_fontDescription
.firstFamily(); }
100 const FontFamily
& family() const { return m_fontDescription
.family(); }
102 bool italic() const { return m_fontDescription
.italic(); }
103 FontWeight
weight() const { return m_fontDescription
.weight(); }
105 bool isPlatformFont() const { return m_isPlatformFont
; }
107 // Metrics that we query the FontFallbackList for.
108 int ascent() const { return primaryFont()->ascent(); }
109 int descent() const { return primaryFont()->descent(); }
110 int height() const { return ascent() + descent(); }
111 int lineSpacing() const { return primaryFont()->lineSpacing(); }
112 int lineGap() const { return primaryFont()->lineGap(); }
113 float xHeight() const { return primaryFont()->xHeight(); }
114 unsigned unitsPerEm() const { return primaryFont()->unitsPerEm(); }
115 int spaceWidth() const { return (int)ceilf(primaryFont()->m_adjustedSpaceWidth
+ m_letterSpacing
); }
116 int tabWidth() const { return 8 * spaceWidth(); }
118 const SimpleFontData
* primaryFont() const {
119 if (!m_cachedPrimaryFont
)
121 return m_cachedPrimaryFont
;
124 const FontData
* fontDataAt(unsigned) const;
125 const GlyphData
& glyphDataForCharacter(UChar32
, bool mirror
, bool forceSmallCaps
= false) const;
126 // Used for complex text, and does not utilize the glyph map cache.
127 const FontData
* fontDataForCharacters(const UChar
*, int length
) const;
134 #if ENABLE(SVG_FONTS)
135 void drawTextUsingSVGFont(GraphicsContext
*, const TextRun
&, const FloatPoint
&, int from
, int to
) const;
136 float floatWidthUsingSVGFont(const TextRun
&) const;
137 float floatWidthUsingSVGFont(const TextRun
&, int extraCharsAvailable
, int& charsConsumed
, String
& glyphName
) const;
138 FloatRect
selectionRectForTextUsingSVGFont(const TextRun
&, const IntPoint
&, int h
, int from
, int to
) const;
139 int offsetForPositionForTextUsingSVGFont(const TextRun
&, int position
, bool includePartialGlyphs
) const;
142 #if USE(FONT_FAST_PATH)
143 bool canUseGlyphCache(const TextRun
&) const;
144 float drawSimpleText(GraphicsContext
*, const TextRun
&, const FloatPoint
&, int from
, int to
) const;
145 void drawGlyphs(GraphicsContext
*, const SimpleFontData
*, const GlyphBuffer
&, int from
, int to
, const FloatPoint
&, bool setColor
= true) const;
146 void drawGlyphBuffer(GraphicsContext
*, const GlyphBuffer
&, const TextRun
&, FloatPoint
&) const;
147 float floatWidthForSimpleText(const TextRun
&, GlyphBuffer
*) const;
148 int offsetForPositionForSimpleText(const TextRun
&, int position
, bool includePartialGlyphs
) const;
149 FloatRect
selectionRectForSimpleText(const TextRun
&, const IntPoint
&, int h
, int from
, int to
) const;
152 float drawComplexText(GraphicsContext
*, const TextRun
&, const FloatPoint
&, int from
, int to
) const;
153 float floatWidthForComplexText(const TextRun
&) const;
154 int offsetForPositionForComplexText(const TextRun
&, int position
, bool includePartialGlyphs
) const;
155 FloatRect
selectionRectForComplexText(const TextRun
&, const IntPoint
&, int h
, int from
, int to
) const;
156 void cachePrimaryFont() const;
158 friend struct WidthIterator
;
161 bool equalForTextAutoSizing (const Font
&other
) const {
162 return (m_fontDescription
.equalForTextAutoSizing(other
.m_fontDescription
) &&
163 m_letterSpacing
== other
.m_letterSpacing
&&
164 m_wordSpacing
== other
.m_wordSpacing
);
167 // Useful for debugging the different font rendering code paths.
168 #if USE(FONT_FAST_PATH)
169 enum CodePath
{ Auto
, Simple
, Complex
};
170 static void setCodePath(CodePath
);
171 static CodePath
codePath();
172 static CodePath s_codePath
;
174 static const uint8_t gRoundingHackCharacterTable
[256];
175 static bool isRoundingHackCharacter(UChar32 c
)
177 return (((c
& ~0xFF) == 0 && gRoundingHackCharacterTable
[c
]) || c
== 0x200e || c
== 0x200f);
181 FontSelector
* fontSelector() const;
182 static bool treatAsSpace(UChar c
) { return c
== ' ' || c
== '\t' || c
== '\n' || c
== 0x00A0; }
183 static bool treatAsZeroWidthSpace(UChar c
) { return c
< 0x20 || (c
>= 0x7F && c
< 0xA0) || c
== 0x200e || c
== 0x200f || (c
>= 0x202a && c
<= 0x202e) || c
== 0xFFFC; }
185 #if ENABLE(SVG_FONTS)
186 bool isSVGFont() const;
187 SVGFontElement
* svgFont() const;
191 FontDescription m_fontDescription
;
192 mutable RefPtr
<FontFallbackList
> m_fontList
;
193 mutable HashMap
<int, GlyphPageTreeNode
*> m_pages
;
194 mutable GlyphPageTreeNode
* m_pageZero
;
195 mutable const SimpleFontData
* m_cachedPrimaryFont
;
196 float m_letterSpacing
;
198 bool m_isPlatformFont
;