2 * This file is part of the html renderer for KDE.
4 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
5 * (C) 2000 Antti Koivisto (koivisto@kde.org)
6 * (C) 2000 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2003, 2006, 2007 Apple Computer, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
29 #include "FontDescription.h"
30 #include <wtf/HashMap.h>
33 #include <QtGui/qfont.h>
34 #include <QtGui/qfontmetrics.h>
42 class FontFallbackList;
43 class FontPlatformData;
46 class GlyphPageTreeNode;
47 class GraphicsContext;
54 TextRun(const UChar* c, int len, bool allowTabs = false, int xpos = 0, int padding = 0, bool rtl = false, bool directionalOverride = false,
55 bool applyRunRounding = true, bool applyWordRounding = true)
58 , m_allowTabs(allowTabs)
62 , m_directionalOverride(directionalOverride)
63 , m_applyRunRounding(applyRunRounding)
64 , m_applyWordRounding(applyWordRounding)
65 , m_disableSpacing(false)
69 TextRun(const String& s, bool allowTabs = false, int xpos = 0, int padding = 0, bool rtl = false, bool directionalOverride = false,
70 bool applyRunRounding = true, bool applyWordRounding = true)
71 : m_characters(s.characters())
73 , m_allowTabs(allowTabs)
77 , m_directionalOverride(directionalOverride)
78 , m_applyRunRounding(applyRunRounding)
79 , m_applyWordRounding(applyWordRounding)
80 , m_disableSpacing(false)
84 const UChar operator[](int i) const { return m_characters[i]; }
85 const UChar* data(int i) const { return &m_characters[i]; }
87 const UChar* characters() const { return m_characters; }
88 int length() const { return m_len; }
90 void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
92 bool allowTabs() const { return m_allowTabs; }
93 int xPos() const { return m_xpos; }
94 int padding() const { return m_padding; }
95 bool rtl() const { return m_rtl; }
96 bool ltr() const { return !m_rtl; }
97 bool directionalOverride() const { return m_directionalOverride; }
98 bool applyRunRounding() const { return m_applyRunRounding; }
99 bool applyWordRounding() const { return m_applyWordRounding; }
100 bool spacingDisabled() const { return m_disableSpacing; }
102 void disableSpacing() { m_disableSpacing = true; }
103 void disableRoundingHacks() { m_applyRunRounding = m_applyWordRounding = false; }
104 void setRTL(bool b) { m_rtl = b; }
105 void setDirectionalOverride(bool override) { m_directionalOverride = override; }
108 const UChar* m_characters;
115 bool m_directionalOverride;
116 bool m_applyRunRounding;
117 bool m_applyWordRounding;
118 bool m_disableSpacing;
124 Font(const FontDescription&, short letterSpacing, short wordSpacing);
126 Font(const FontPlatformData&, bool isPrinting); // This constructor is only used if the platform wants to start with a native font.
131 Font& operator=(const Font&);
133 bool operator==(const Font& other) const;
134 bool operator!=(const Font& other) const {
135 return !(*this == other);
138 const FontDescription& fontDescription() const { return m_fontDescription; }
140 int pixelSize() const { return fontDescription().computedPixelSize(); }
141 float size() const { return fontDescription().computedSize(); }
143 void update(PassRefPtr<FontSelector>) const;
145 void drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1) const;
147 int width(const TextRun&) const;
148 float floatWidth(const TextRun&) const;
150 int offsetForPosition(const TextRun&, int position, bool includePartialGlyphs) const;
151 FloatRect selectionRectForText(const TextRun&, const IntPoint&, int h, int from = 0, int to = -1) const;
153 bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
155 short wordSpacing() const { return m_wordSpacing; }
156 short letterSpacing() const { return m_letterSpacing; }
157 void setWordSpacing(short s) { m_wordSpacing = s; }
158 void setLetterSpacing(short s) { m_letterSpacing = s; }
160 bool isFixedPitch() const;
161 bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
163 FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
164 const FontFamily& family() const { return m_fontDescription.family(); }
166 bool italic() const { return m_fontDescription.italic(); }
167 unsigned weight() const { return m_fontDescription.weight(); }
168 bool bold() const { return m_fontDescription.bold(); }
171 bool isPlatformFont() const { return m_isPlatformFont; }
175 inline const QFont &font() const { return m_font; }
176 inline const QFont &scFont() const { return m_scFont; }
179 // Metrics that we query the FontFallbackList for.
182 int height() const { return ascent() + descent(); }
183 int lineSpacing() const;
184 float xHeight() const;
185 unsigned unitsPerEm() const;
186 int spaceWidth() const;
187 int tabWidth() const { return 8 * spaceWidth(); }
190 const FontData* primaryFont() const;
191 const FontData* fontDataAt(unsigned) const;
192 const GlyphData& glyphDataForCharacter(UChar32, bool mirror, bool forceSmallCaps = false) const;
193 // Used for complex text, and does not utilize the glyph map cache.
194 const FontData* fontDataForCharacters(const UChar*, int length) const;
197 bool canUseGlyphCache(const TextRun&) const;
198 void drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
199 void drawGlyphs(GraphicsContext*, const FontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
200 void drawGlyphBuffer(GraphicsContext*, const GlyphBuffer&, const TextRun&, const FloatPoint&) const;
201 void drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
202 float floatWidthForSimpleText(const TextRun&, GlyphBuffer*) const;
203 float floatWidthForComplexText(const TextRun&) const;
204 int offsetForPositionForSimpleText(const TextRun&, int position, bool includePartialGlyphs) const;
205 int offsetForPositionForComplexText(const TextRun&, int position, bool includePartialGlyphs) const;
206 FloatRect selectionRectForSimpleText(const TextRun&, const IntPoint&, int h, int from, int to) const;
207 FloatRect selectionRectForComplexText(const TextRun&, const IntPoint&, int h, int from, int to) const;
209 friend struct WidthIterator;
211 // Useful for debugging the different font rendering code paths.
214 enum CodePath { Auto, Simple, Complex };
215 static void setCodePath(CodePath);
216 static CodePath codePath;
218 static const uint8_t gRoundingHackCharacterTable[256];
219 static bool isRoundingHackCharacter(UChar32 c)
221 return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c]));
224 static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; }
225 static bool treatAsZeroWidthSpace(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == 0x200e || c == 0x200f; }
227 FontDescription m_fontDescription;
229 mutable RefPtr<FontFallbackList> m_fontList;
230 mutable HashMap<int, GlyphPageTreeNode*> m_pages;
231 mutable GlyphPageTreeNode* m_pageZero;
233 short m_letterSpacing;
236 bool m_isPlatformFont;