1 // Font.h -- Font class, for Gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
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.
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
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/cstdint.hpp>
35 #include "ref_counted.h"
38 class FreetypeGlyphsProvider
;
49 // @@ replace this with a flat hash, or else a sorted array
54 boost::uint16_t m_char0
;
55 boost::uint16_t m_char1
;
57 bool operator==(const kerning_pair
& k
) const
59 return m_char0
== k
.m_char0
&& m_char1
== k
.m_char1
;
63 // for use in standard algorithms
65 operator< (const kerning_pair
& p1
, const kerning_pair
& p2
)
67 if (p1
.m_char0
< p2
.m_char0
) return true;
68 if (p1
.m_char0
== p2
.m_char0
) {
69 if (p1
.m_char1
< p2
.m_char1
) return true;
78 /// All fonts used in the course of rendering a SWF are represented by this
79 /// class. There are two types of Font object: device fonts and glyph fonts
80 /// (also called embedded fonts). Device fonts contain no embedded glyphs,
81 /// but glyph fonts may be rendered using device fonts if requested during
84 /// The fact that one Font object may represent an embedded and a device
85 /// font simultaneously means that callers must themselves ensure they
86 /// specify which font they require. Failure to do this consistently may mean
87 /// callers end up with the wrong information about a font.
89 /// TODO: check whether it really needs to be ref_counted.
90 class Font
: public ref_counted
94 // This table maps from Unicode DisplayObject number to glyph index.
95 typedef std::map
<boost::uint16_t, int> CodeTable
;
97 Font(std::auto_ptr
<SWF::DefineFontTag
> ft
);
99 /// Create a device-font only font, using the given name to find it
102 /// Name of the font face to look for.
105 /// Whether to use the bold variant of the font.
108 /// Whether to use the italic variant of the font.
109 Font(const std::string
& name
, bool bold
= false, bool italic
= false);
113 boost::uint16_t codeTableLookup(int glyph
, bool embedded
) const;
115 /// Return true if this font matches given name and flags
125 bool matches(const std::string
& name
, bool bold
, bool italic
) const;
127 /// Get glyph by index.
129 /// @param glyph_index
130 /// Index of the glyph. See get_glyph_index() to obtain by character code.
133 /// If true, queries the 'embedded' glyphs table,
134 /// otherwise, looks in the 'device' font table.
137 /// The glyph outline, or NULL if out of range. (would be a
138 /// programming error most likely). The ShapeRecord is owned by
140 SWF::ShapeRecord
* get_glyph(int glyph_index
, bool embedded
) const;
142 /// Get name of this font.
143 const std::string
& name() const { return _name
; }
145 /// Return the glyph index for a given character code
148 /// Character code to fetch the corresponding glyph index of.
151 /// If true, queries the 'embedded' glyphs table,
152 /// otherwise, looks in the 'device' font table.
154 /// Note, when querying device fonts, glyphs are created on demand,
155 /// this never happens for embedded fonts, in which case an unexistent
156 /// glyph results in a return of -1
158 /// @return -1 if there is no glyph for the specified code or a valid
159 /// positive index to use in subsequent calls to other
160 /// glyph-index-based methods.
162 int get_glyph_index(boost::uint16_t code
, bool embedded
) const;
164 /// Return the advance value for the given glyph index
166 /// Note: use unitsPerEM() to get the EM square.
168 /// @param glyph_index Index of the glyph. See get_glyph_index()
169 /// to obtain by character code.
171 /// @param embedded If true, queries the 'embedded' glyphs table,
172 /// otherwise, looks in the 'device' font table.
173 float get_advance(int glyph_index
, bool embedded
) const;
175 /// Return the adjustment in advance between the given two
176 /// DisplayObjects (makes sense for embedded glyphs only)
178 /// Normally this will be 0
180 /// NOTE: don't call this method when willing to work with device
181 /// fonts, or you'll end up mixing information from device fonts
182 /// with information from embedded fonts.
184 float get_kerning_adjustment(int last_code
, int this_code
) const;
186 /// Return height of the EM square used for glyphs definition
188 /// @param embedded If true, return is based on the SWF tag the font
189 /// was read from, otherwise will query the
190 /// FreeTypeGlyphsProvider
191 size_t unitsPerEM(bool embedded
) const;
193 /// Return the ascent value of the font.
195 /// Note: use unitsPerEM() to get the EM square.
196 float ascent(bool embedded
) const;
198 /// Return the descent value of the font in EM units.
200 /// Note: use unitsPerEM() to get the EM square.
201 float descent(bool embedded
) const;
203 /// Return the leading value of the font.
205 /// Note: use unitsPerEM() to get the EM square.
206 float leading() const;
208 /// Return true if the font is bold.
209 bool isBold() const {
213 /// Return true if the font is italic.
214 bool isItalic() const {
218 /// A pair of strings describing the font.
220 /// Used by the DefineFontName tag, usefulness unclear.
223 std::string displayName
;
224 std::string copyrightName
;
227 /// Glyph info structure
230 // no glyph, default textured glyph, 0 advance
233 /// Construct default textured glyph
235 /// Takes ownership of the SWF::ShapeRecord.
236 GlyphInfo(std::auto_ptr
<SWF::ShapeRecord
> glyph
, float advance
);
238 GlyphInfo(const GlyphInfo
& o
);
240 boost::shared_ptr
<SWF::ShapeRecord
> glyph
;
245 typedef std::vector
<GlyphInfo
> GlyphInfoRecords
;
247 /// Add display name and copyright name for an embedded font.
249 /// It's a string copy, but a decent standard library implementation
250 /// should be able to avoid actually copying. Since it's only two
251 /// strings, it doesn't seem worth the effort to avoid the copy.
252 void addFontNameInfo(const FontNameInfo
& fontName
);
254 /// Set the name of the font.
256 /// This is used by SWF::DefineFontInfoTag
257 void setName(const std::string
& name
);
259 /// Set the language and encoding flags of the font.
261 /// This is used by SWF::DefineFontInfoTag
262 void setFlags(boost::uint8_t flags
);
264 /// Add a CodeTable to the font.
266 /// This is used by SWF::DefineFontInfoTag
267 void setCodeTable(std::auto_ptr
<CodeTable
> table
);
269 /// Retrieve the number of embedded glyphs in this font.
270 GlyphInfoRecords::size_type
glyphCount() const;
272 /// Retrieve the FreetypeGlyphsProvider, initializing it if necessary.
274 /// Always use this method rather than directly accessing the _ftProvider
275 /// member to ensure that the provider is initialized. May return null.
276 FreetypeGlyphsProvider
* ftProvider() const;
280 /// Add a glyph from the os font into the device glyphs table
282 /// It is assumed that the glyph tables do NOT contain
283 /// an entry for the given code.
284 /// Initializes the rasterizer if not already done so.
286 /// @return index of the newly added glyph, or -1 on error.
288 int add_os_glyph(boost::uint16_t code
);
290 /// If we were constructed from a definition, this is not NULL.
291 boost::scoped_ptr
<SWF::DefineFontTag
> _fontTag
;
294 GlyphInfoRecords _deviceGlyphTable
;
297 std::string _displayName
;
298 std::string _copyrightName
;
306 /// Code to index table for embedded glyphs
308 /// This can be NULL if an embedded font should not be
309 /// substituted by a device font. This can arise with
310 /// a) a DefineFont tag without a corresponding DefineFontInfo
311 /// or DefineFontInfo2, or
312 /// b) a DefineFont2 or DefineFont3 tag with no CodeTable.
314 /// It is a shared_ptr to avoid changing an original
315 /// DefineFont2Tag, while allowing this class to take ownership
316 /// of CodeTables from a DefineFontInfo tag.
317 boost::shared_ptr
<const CodeTable
> _embeddedCodeTable
;
319 /// Code to index table for device glyphs
320 CodeTable _deviceCodeTable
;
322 typedef std::map
<kerning_pair
, float> kernings_table
;
323 kernings_table m_kerning_pairs
;
325 mutable std::auto_ptr
<FreetypeGlyphsProvider
> _ftProvider
;
330 } // end namespace gnash
334 #endif // GNASH_FONT_H
340 // indent-tabs-mode: t