Update with current status
[gnash.git] / libcore / Font.h
blobf69bd4b430b044253b9ba52e1c7e41ad6adc5965
1 // Font.h -- Font class, for Gnash
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // Based on the public domain work of Thatcher Ulrich <tu@tulrich.com> 2003
24 #ifndef GNASH_FONT_H
25 #define GNASH_FONT_H
27 #include <string>
28 #include <memory>
29 #include <cstdint>
30 #include <memory>
31 #include <vector>
32 #include <map>
34 #include "ref_counted.h"
36 namespace gnash {
37 class FreetypeGlyphsProvider;
38 namespace SWF {
39 class ShapeRecord;
40 class DefineFontTag;
44 namespace gnash {
48 // @@ replace this with a flat hash, or else a sorted array
49 // (binary search)
50 class kerning_pair
52 public:
53 std::uint16_t m_char0;
54 std::uint16_t m_char1;
56 bool operator==(const kerning_pair& k) const
58 return m_char0 == k.m_char0 && m_char1 == k.m_char1;
62 // for use in standard algorithms
63 inline bool
64 operator< (const kerning_pair& p1, const kerning_pair& p2)
66 if (p1.m_char0 < p2.m_char0) return true;
67 if (p1.m_char0 == p2.m_char0) {
68 if (p1.m_char1 < p2.m_char1) return true;
71 return false;
75 /// A Font resource.
77 /// All fonts used in the course of rendering a SWF are represented by this
78 /// class. There are two types of Font object: device fonts and glyph fonts
79 /// (also called embedded fonts). Device fonts contain no embedded glyphs,
80 /// but glyph fonts may be rendered using device fonts if requested during
81 /// runtime.
83 /// The fact that one Font object may represent an embedded and a device
84 /// font simultaneously means that callers must themselves ensure they
85 /// specify which font they require. Failure to do this consistently may mean
86 /// callers end up with the wrong information about a font.
88 /// TODO: check whether it really needs to be ref_counted.
89 class Font : public ref_counted
91 public:
93 // This table maps from Unicode DisplayObject number to glyph index.
94 typedef std::map<std::uint16_t, int> CodeTable;
96 Font(std::unique_ptr<SWF::DefineFontTag> ft);
98 /// Create a device-font only font, using the given name to find it
100 /// @param name
101 /// Name of the font face to look for.
103 /// @param bold
104 /// Whether to use the bold variant of the font.
106 /// @param italic
107 /// Whether to use the italic variant of the font.
108 Font(std::string name, bool bold = false, bool italic = false);
110 ~Font();
112 std::uint16_t codeTableLookup(int glyph, bool embedded) const;
114 /// Return true if this font matches given name and flags
116 /// @param name
117 /// Font name
119 /// @param bold
120 /// Bold flag
122 /// @param italic
123 /// Italic flag
124 bool matches(const std::string& name, bool bold, bool italic) const;
126 /// Get glyph by index.
128 /// @param glyph_index
129 /// Index of the glyph. See get_glyph_index() to obtain by character code.
131 /// @param embedded
132 /// If true, queries the 'embedded' glyphs table,
133 /// otherwise, looks in the 'device' font table.
135 /// @return
136 /// The glyph outline, or NULL if out of range. (would be a
137 /// programming error most likely). The ShapeRecord is owned by
138 /// the Font class.
139 SWF::ShapeRecord* get_glyph(int glyph_index, bool embedded) const;
141 /// Get name of this font.
142 const std::string& name() const { return _name; }
144 /// Return the glyph index for a given character code
146 /// @param code
147 /// Character code to fetch the corresponding glyph index of.
149 /// @param embedded
150 /// If true, queries the 'embedded' glyphs table,
151 /// otherwise, looks in the 'device' font table.
153 /// Note, when querying device fonts, glyphs are created on demand,
154 /// this never happens for embedded fonts, in which case an unexistent
155 /// glyph results in a return of -1
157 /// @return -1 if there is no glyph for the specified code or a valid
158 /// positive index to use in subsequent calls to other
159 /// glyph-index-based methods.
161 int get_glyph_index(std::uint16_t code, bool embedded) const;
163 /// Return the advance value for the given glyph index
165 /// Note: use unitsPerEM() to get the EM square.
167 /// @param glyph_index Index of the glyph. See get_glyph_index()
168 /// to obtain by character code.
170 /// @param embedded If true, queries the 'embedded' glyphs table,
171 /// otherwise, looks in the 'device' font table.
172 float get_advance(int glyph_index, bool embedded) const;
174 /// Return the adjustment in advance between the given two
175 /// DisplayObjects (makes sense for embedded glyphs only)
177 /// Normally this will be 0
179 /// NOTE: don't call this method when willing to work with device
180 /// fonts, or you'll end up mixing information from device fonts
181 /// with information from embedded fonts.
183 float get_kerning_adjustment(int last_code, int this_code) const;
185 /// Return height of the EM square used for glyphs definition
187 /// @param embedded If true, return is based on the SWF tag the font
188 /// was read from, otherwise will query the
189 /// FreeTypeGlyphsProvider
190 size_t unitsPerEM(bool embedded) const;
192 /// Return the ascent value of the font.
194 /// Note: use unitsPerEM() to get the EM square.
195 float ascent(bool embedded) const;
197 /// Return the descent value of the font in EM units.
199 /// Note: use unitsPerEM() to get the EM square.
200 float descent(bool embedded) const;
202 /// Return the leading value of the font.
204 /// Note: use unitsPerEM() to get the EM square.
205 float leading() const;
207 /// Return true if the font is bold.
208 bool isBold() const {
209 return _bold;
212 /// Return true if the font is italic.
213 bool isItalic() const {
214 return _italic;
217 /// A pair of strings describing the font.
219 /// Used by the DefineFontName tag, usefulness unclear.
220 struct FontNameInfo
222 std::string displayName;
223 std::string copyrightName;
226 /// Glyph info structure
227 struct GlyphInfo
229 // no glyph, default textured glyph, 0 advance
230 GlyphInfo();
232 /// Construct default textured glyph
234 /// Takes ownership of the SWF::ShapeRecord.
235 GlyphInfo(std::unique_ptr<SWF::ShapeRecord> glyph, float advance);
237 std::unique_ptr<SWF::ShapeRecord> glyph;
239 float advance;
242 typedef std::vector<GlyphInfo> GlyphInfoRecords;
244 /// Add display name and copyright name for an embedded font.
246 /// It's a string copy, but a decent standard library implementation
247 /// should be able to avoid actually copying. Since it's only two
248 /// strings, it doesn't seem worth the effort to avoid the copy.
249 void addFontNameInfo(const FontNameInfo& fontName);
251 /// Set the name of the font.
253 /// This is used by SWF::DefineFontInfoTag
254 void setName(const std::string& name);
256 /// Set the language and encoding flags of the font.
258 /// This is used by SWF::DefineFontInfoTag
259 void setFlags(std::uint8_t flags);
261 /// Add a CodeTable to the font.
263 /// This is used by SWF::DefineFontInfoTag
264 void setCodeTable(std::unique_ptr<CodeTable> table);
266 /// Retrieve the number of embedded glyphs in this font.
267 GlyphInfoRecords::size_type glyphCount() const;
269 /// Retrieve the FreetypeGlyphsProvider, initializing it if necessary.
271 /// Always use this method rather than directly accessing the _ftProvider
272 /// member to ensure that the provider is initialized. May return null.
273 FreetypeGlyphsProvider* ftProvider() const;
275 private:
277 /// Add a glyph from the os font into the device glyphs table
279 /// It is assumed that the glyph tables do NOT contain
280 /// an entry for the given code.
281 /// Initializes the rasterizer if not already done so.
283 /// @return index of the newly added glyph, or -1 on error.
285 int add_os_glyph(std::uint16_t code);
287 /// If we were constructed from a definition, this is not NULL.
288 std::unique_ptr<SWF::DefineFontTag> _fontTag;
290 // Device glyphs
291 GlyphInfoRecords _deviceGlyphTable;
293 std::string _name;
294 std::string _displayName;
295 std::string _copyrightName;
297 bool _unicodeChars;
298 bool _shiftJISChars;
299 bool _ansiChars;
300 bool _italic;
301 bool _bold;
303 /// Code to index table for embedded glyphs
305 /// This can be NULL if an embedded font should not be
306 /// substituted by a device font. This can arise with
307 /// a) a DefineFont tag without a corresponding DefineFontInfo
308 /// or DefineFontInfo2, or
309 /// b) a DefineFont2 or DefineFont3 tag with no CodeTable.
311 /// It is a shared_ptr to avoid changing an original
312 /// DefineFont2Tag, while allowing this class to take ownership
313 /// of CodeTables from a DefineFontInfo tag.
314 std::shared_ptr<const CodeTable> _embeddedCodeTable;
316 /// Code to index table for device glyphs
317 CodeTable _deviceCodeTable;
319 typedef std::map<kerning_pair, float> kernings_table;
320 kernings_table m_kerning_pairs;
322 mutable std::unique_ptr<FreetypeGlyphsProvider> _ftProvider;
327 } // end namespace gnash
331 #endif // GNASH_FONT_H
333 // Local Variables:
334 // mode: C++
335 // c-basic-offset: 8
336 // tab-width: 8
337 // indent-tabs-mode: t
338 // End: