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;
47 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
52 TextRun(const UChar* c, int len)
53 :m_characters(c), m_len(len), m_from(0), m_to(len)
56 TextRun(const UChar* c, int len, int from, int to) // This constructor is only used in one place in Mac-specific code.
57 :m_characters(c), m_len(len), m_from(from), m_to(to)
60 TextRun(const StringImpl* s, int offset = 0, int from = -1, int to = -1)
61 :m_characters(s->characters() + offset), m_len(s->length() - offset), m_from(adjustFrom(from)), m_to(adjustTo(to))
64 const UChar operator[](int i) const { return m_characters[i]; }
65 const UChar* data(int i) const { return &m_characters[i]; }
67 int adjustFrom(int from) const { return from == -1 ? 0 : from; }
68 int adjustTo(int to) const { return to == -1 ? m_len : to; }
70 const UChar* characters() const { return m_characters; }
71 int length() const { return m_len; }
72 int from() const { return m_from; }
73 int to() const { return m_to; }
76 const UChar* m_characters;
85 Font(const FontDescription&, short letterSpacing, short wordSpacing);
89 Font& operator=(const Font&);
91 bool operator==(const Font& other) const {
92 // The renderer pointer doesn't have to be checked, since
93 // checking the font description will be fine.
94 return (m_fontDescription == other.m_fontDescription &&
95 m_letterSpacing == other.m_letterSpacing &&
96 m_wordSpacing == other.m_wordSpacing);
99 bool operator!=(const Font& other) const {
100 return !(*this == other);
103 const FontDescription& fontDescription() const { return m_fontDescription; }
105 int pixelSize() const { return fontDescription().computedPixelSize(); }
106 float size() const { return fontDescription().computedSize(); }
110 void drawText(GraphicsContext*, const TextRun&, const IntPoint&, int tabWidth, int xpos,
111 int toAdd, TextDirection, bool visuallyOrdered) const;
112 void drawHighlightForText(GraphicsContext*, const TextRun&, const IntPoint&, int h,
113 int tabWidth, int xpos, int toAdd,
114 TextDirection d, bool visuallyOrdered, const Color& backgroundColor) const;
115 void drawLineForText(GraphicsContext*, const IntPoint&, int yOffset, int width) const;
116 void drawLineForMisspelling(GraphicsContext*, const IntPoint&, int width) const;
117 int misspellingLineThickness(GraphicsContext*) const;
119 float floatWidth(const TextRun&, int tabWidth, int xpos, bool runRounding = true) const;
121 int checkSelectionPoint(const TextRun&, int toAdd, int tabWidth, int xpos,
122 int x, TextDirection, bool visuallyOrdered, bool includePartialGlyphs) const;
123 IntRect selectionRectForText(const TextRun&, const IntPoint&, int h, int tabWidth, int xpos, int width,
124 bool rtl, bool visuallyOrdered = false) const;
126 int width(const TextRun&, int tabWidth = 0, int xpos = 0) const;
128 bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
130 short wordSpacing() const { return m_wordSpacing; }
131 short letterSpacing() const { return m_letterSpacing; }
132 void setWordSpacing(short s) { m_wordSpacing = s; }
133 void setLetterSpacing(short s) { m_letterSpacing = s; }
135 bool isFixedPitch() const;
136 bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
138 FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
139 const FontFamily& family() const { return m_fontDescription.family(); }
141 bool italic() const { return m_fontDescription.italic(); }
142 unsigned weight() const { return m_fontDescription.weight(); }
145 // FIXME: Shouldn't need to access FontPlatformData... should just need NSFont.
146 NSString* getNSFamily() const { return m_fontDescription.family().getNSFamily(); }
147 NSFont* getNSFont() const { return platformFont().font; }
148 const FontPlatformData& platformFont() const;
151 // Metrics that we query the FontFallbackList for.
154 int height() const { return ascent() + descent(); }
155 int lineSpacing() const;
156 float xHeight() const;
158 const FontData* primaryFont() const;
162 // FIXME: This will eventually be cross-platform, but we want to keep Windows compiling for now.
163 bool canUseGlyphCache(const TextRun&) const;
164 void drawSimpleText(GraphicsContext*, const TextRun&, const IntPoint&,
165 int tabWidth, int xpos, int toAdd,
166 TextDirection, bool visuallyOrdered) const;
167 void drawGlyphs(GraphicsContext*, const FontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
168 void drawComplexText(GraphicsContext*, const TextRun&, const IntPoint&,
169 int tabWidth, int xpos, int toAdd, TextDirection, bool visuallyOrdered) const;
170 float floatWidthForSimpleText(const TextRun&,
171 int tabWidth, int xpos, int toAdd,
172 TextDirection, bool visuallyOrdered,
173 bool applyWordRounding, bool applyRunRounding,
174 const FontData* substituteFont,
175 float* startX, GlyphBuffer*) const;
176 float floatWidthForComplexText(const TextRun&, int tabWidth, int xpos, bool runRounding = true) const;
178 friend struct WidthIterator;
180 // Useful for debugging the complex font rendering code path.
182 static void setAlwaysUseComplexPath(bool);
183 static bool gAlwaysUseComplexPath;
185 static const uint8_t gRoundingHackCharacterTable[256];
186 static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; }
187 static bool isRoundingHackCharacter(UChar32 c)
189 return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c]));
194 FontDescription m_fontDescription;
195 mutable RefPtr<FontFallbackList> m_fontList;
196 short m_letterSpacing;