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 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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
30 #include "FontDescription.h"
31 #include "TextDirection.h"
32 #include "GlyphBuffer.h"
35 // FIXME: Should not be necessary.
36 #include "FontPlatformData.h"
41 class FontFallbackList;
42 class GraphicsContext;
48 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
53 TextRun(const UChar* c, int len)
54 :m_characters(c), m_len(len), m_from(0), m_to(len)
57 TextRun(const UChar* c, int len, int from, int to) // This constructor is only used in one place in Mac-specific code.
58 :m_characters(c), m_len(len), m_from(from), m_to(to)
61 TextRun(const StringImpl* s, int offset = 0, int from = -1, int to = -1)
62 :m_characters(s->characters() + offset), m_len(s->length() - offset), m_from(adjustFrom(from)), m_to(adjustTo(to))
65 const UChar operator[](int i) const { return m_characters[i]; }
66 const UChar* data(int i) const { return &m_characters[i]; }
68 int adjustFrom(int from) const { return from == -1 ? 0 : from; }
69 int adjustTo(int to) const { return to == -1 ? m_len : to; }
71 const UChar* characters() const { return m_characters; }
72 int length() const { return m_len; }
73 int from() const { return m_from; }
74 int to() const { return m_to; }
77 const UChar* m_characters;
86 Font(const FontDescription&, short letterSpacing, short wordSpacing);
90 Font& operator=(const Font&);
92 bool operator==(const Font& other) const {
93 // The renderer pointer doesn't have to be checked, since
94 // checking the font description will be fine.
95 return (m_fontDescription == other.m_fontDescription &&
96 m_letterSpacing == other.m_letterSpacing &&
97 m_wordSpacing == other.m_wordSpacing);
100 bool operator!=(const Font& other) const {
101 return !(*this == other);
104 const FontDescription& fontDescription() const { return m_fontDescription; }
106 int pixelSize() const { return fontDescription().computedPixelSize(); }
107 float size() const { return fontDescription().computedSize(); }
111 void drawText(GraphicsContext*, const TextRun&, const IntPoint&, int tabWidth, int xpos,
112 int toAdd, TextDirection, bool visuallyOrdered) const;
113 void drawLineForText(GraphicsContext*, const IntPoint&, int yOffset, int width) const;
114 void drawLineForMisspelling(GraphicsContext*, const IntPoint&, int width) const;
115 int misspellingLineThickness(GraphicsContext*) const;
117 float floatWidth(const TextRun&, int tabWidth, int xpos, bool runRounding = true) const;
119 int checkSelectionPoint(const TextRun&, int toAdd, int tabWidth, int xpos,
120 int x, TextDirection, bool visuallyOrdered, bool includePartialGlyphs) const;
121 FloatRect selectionRectForText(const TextRun&, const IntPoint&, int h, int tabWidth, int xpos, int width,
122 bool rtl, bool visuallyOrdered = false) const;
124 int width(const TextRun&, int tabWidth = 0, int xpos = 0) const;
126 bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
128 short wordSpacing() const { return m_wordSpacing; }
129 short letterSpacing() const { return m_letterSpacing; }
130 void setWordSpacing(short s) { m_wordSpacing = s; }
131 void setLetterSpacing(short s) { m_letterSpacing = s; }
133 bool isFixedPitch() const;
134 bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
136 FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
137 const FontFamily& family() const { return m_fontDescription.family(); }
139 bool italic() const { return m_fontDescription.italic(); }
140 unsigned weight() const { return m_fontDescription.weight(); }
143 // FIXME: Shouldn't need to access FontPlatformData... should just need NSFont.
144 NSString* getNSFamily() const { return m_fontDescription.family().getNSFamily(); }
145 NSFont* getNSFont() const { return platformFont().font; }
146 const FontPlatformData& platformFont() const;
149 // Metrics that we query the FontFallbackList for.
152 int height() const { return ascent() + descent(); }
153 int lineSpacing() const;
154 float xHeight() const;
156 const FontData* primaryFont() const;
160 // FIXME: This will eventually be cross-platform, but we want to keep Windows compiling for now.
161 bool canUseGlyphCache(const TextRun&) const;
162 void drawSimpleText(GraphicsContext*, const TextRun&, const IntPoint&,
163 int tabWidth, int xpos, int toAdd,
164 TextDirection, bool visuallyOrdered) const;
165 void drawGlyphs(GraphicsContext*, const FontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
166 void drawComplexText(GraphicsContext*, const TextRun&, const IntPoint&,
167 int tabWidth, int xpos, int toAdd, TextDirection, bool visuallyOrdered) const;
168 float floatWidthForSimpleText(const TextRun&,
169 int tabWidth, int xpos, int toAdd,
170 TextDirection, bool visuallyOrdered,
171 bool applyWordRounding, bool applyRunRounding,
172 const FontData* substituteFont,
173 float* startX, GlyphBuffer*) const;
174 float floatWidthForComplexText(const TextRun&, int tabWidth, int xpos, bool runRounding = true) const;
176 friend struct WidthIterator;
178 // Useful for debugging the complex font rendering code path.
180 static void setAlwaysUseComplexPath(bool);
181 static bool gAlwaysUseComplexPath;
183 static const uint8_t gRoundingHackCharacterTable[256];
184 static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; }
185 static bool isRoundingHackCharacter(UChar32 c)
187 return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c]));
192 FontDescription m_fontDescription;
193 mutable RefPtr<FontFallbackList> m_fontList;
194 short m_letterSpacing;