Gnash  0.8.11dev
Font.h
Go to the documentation of this file.
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.
15 //
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
19 //
20 
21 // Based on the public domain work of Thatcher Ulrich <tu@tulrich.com> 2003
22 
23 
24 #ifndef GNASH_FONT_H
25 #define GNASH_FONT_H
26 
27 #include <string>
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/cstdint.hpp>
31 #include <memory>
32 #include <vector>
33 #include <map>
34 
35 #include "ref_counted.h"
36 
37 namespace gnash {
38  class FreetypeGlyphsProvider;
39  namespace SWF {
40  class ShapeRecord;
41  class DefineFontTag;
42  }
43 }
44 
45 namespace gnash {
46 
47 
48 
49 // @@ replace this with a flat hash, or else a sorted array
50 // (binary search)
52 {
53 public:
54  boost::uint16_t m_char0;
55  boost::uint16_t m_char1;
56 
57  bool operator==(const kerning_pair& k) const
58  {
59  return m_char0 == k.m_char0 && m_char1 == k.m_char1;
60  }
61 };
62 
63 // for use in standard algorithms
64 inline bool
65 operator< (const kerning_pair& p1, const kerning_pair& p2)
66 {
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;
70  }
71 
72  return false;
73 }
74 
75 
77 //
83 //
88 //
90 class Font : public ref_counted
91 {
92 public:
93 
94  // This table maps from Unicode DisplayObject number to glyph index.
95  typedef std::map<boost::uint16_t, int> CodeTable;
96 
97  Font(std::auto_ptr<SWF::DefineFontTag> ft);
98 
100  //
109  Font(const std::string& name, bool bold = false, bool italic = false);
110 
111  ~Font();
112 
113  boost::uint16_t codeTableLookup(int glyph, bool embedded) const;
114 
116  //
125  bool matches(const std::string& name, bool bold, bool italic) const;
126 
128  //
140  SWF::ShapeRecord* get_glyph(int glyph_index, bool embedded) const;
141 
143  const std::string& name() const { return _name; }
144 
146  //
162  int get_glyph_index(boost::uint16_t code, bool embedded) const;
163 
165  //
167  //
173  float get_advance(int glyph_index, bool embedded) const;
174 
177  //
184  float get_kerning_adjustment(int last_code, int this_code) const;
185 
187  //
191  size_t unitsPerEM(bool embedded) const;
192 
194  //
196  float ascent(bool embedded) const;
197 
199  //
201  float descent(bool embedded) const;
202 
204  //
206  float leading() const;
207 
209  bool isBold() const {
210  return _bold;
211  }
212 
214  bool isItalic() const {
215  return _italic;
216  }
217 
219  //
222  {
223  std::string displayName;
224  std::string copyrightName;
225  };
226 
228  struct GlyphInfo
229  {
230  // no glyph, default textured glyph, 0 advance
231  GlyphInfo();
232 
234  //
236  GlyphInfo(std::auto_ptr<SWF::ShapeRecord> glyph, float advance);
237 
238  GlyphInfo(const GlyphInfo& o);
239 
240  boost::shared_ptr<SWF::ShapeRecord> glyph;
241 
242  float advance;
243  };
244 
245  typedef std::vector<GlyphInfo> GlyphInfoRecords;
246 
248  //
252  void addFontNameInfo(const FontNameInfo& fontName);
253 
255  //
257  void setName(const std::string& name);
258 
260  //
262  void setFlags(boost::uint8_t flags);
263 
265  //
267  void setCodeTable(std::auto_ptr<CodeTable> table);
268 
270  GlyphInfoRecords::size_type glyphCount() const;
271 
273  //
277 
278 private:
279 
281  //
288  int add_os_glyph(boost::uint16_t code);
289 
291  boost::scoped_ptr<SWF::DefineFontTag> _fontTag;
292 
293  // Device glyphs
294  GlyphInfoRecords _deviceGlyphTable;
295 
296  std::string _name;
297  std::string _displayName;
298  std::string _copyrightName;
299 
300  bool _unicodeChars;
301  bool _shiftJISChars;
302  bool _ansiChars;
303  bool _italic;
304  bool _bold;
305 
307  //
317  boost::shared_ptr<const CodeTable> _embeddedCodeTable;
318 
320  CodeTable _deviceCodeTable;
321 
322  typedef std::map<kerning_pair, float> kernings_table;
323  kernings_table m_kerning_pairs;
324 
325  mutable std::auto_ptr<FreetypeGlyphsProvider> _ftProvider;
326 
327 };
328 
329 
330 } // end namespace gnash
331 
332 
333 
334 #endif // GNASH_FONT_H
335 
336 // Local Variables:
337 // mode: C++
338 // c-basic-offset: 8
339 // tab-width: 8
340 // indent-tabs-mode: t
341 // End:
~Font()
Definition: Font.cpp:104
Font(std::auto_ptr< SWF::DefineFontTag > ft)
Definition: Font.cpp:76
bool isBold() const
Return true if the font is bold.
Definition: Font.h:209
Glyph info structure.
Definition: Font.h:228
std::string copyrightName
Definition: Font.h:224
A pair of strings describing the font.
Definition: Font.h:221
float descent(bool embedded) const
Return the descent value of the font in EM units.
Definition: Font.cpp:357
bool matches(const std::string &name, bool bold, bool italic) const
Return true if this font matches given name and flags.
Definition: Font.cpp:316
A Font resource.
Definition: Font.h:90
For stuff that&#39;s tricky to keep track of w/r/t ownership &amp; cleanup. The only use for this class seems...
Definition: ref_counted.h:34
boost::uint16_t m_char1
Definition: Font.h:55
FreetypeGlyphsProvider * ftProvider() const
Retrieve the FreetypeGlyphsProvider, initializing it if necessary.
Definition: Font.cpp:327
boost::uint16_t codeTableLookup(int glyph, bool embedded) const
Definition: Font.cpp:188
Definition: GnashKey.h:157
SWF::ShapeRecord * get_glyph(int glyph_index, bool embedded) const
Get glyph by index.
Definition: Font.cpp:109
void setName(const std::string &name)
Set the name of the font.
Definition: Font.cpp:181
Definition: GnashKey.h:161
code
Definition: GnashKey.h:43
void setFlags(boost::uint8_t flags)
Set the language and encoding flags of the font.
Definition: Font.cpp:152
bool operator==(const kerning_pair &k) const
Definition: Font.h:57
float leading() const
Return the leading value of the font.
Definition: Font.cpp:322
float advance
Definition: Font.h:242
void setCodeTable(std::auto_ptr< CodeTable > table)
Add a CodeTable to the font.
Definition: Font.cpp:163
std::string displayName
Definition: Font.h:223
boost::shared_ptr< SWF::ShapeRecord > glyph
Definition: Font.h:240
std::map< boost::uint16_t, int > CodeTable
Definition: Font.h:95
float get_advance(int glyph_index, bool embedded) const
Return the advance value for the given glyph index.
Definition: Font.cpp:229
bool operator<(const event_id &a, const event_id &b)
Comparator for use in stdlib containers.
Definition: event_id.h:170
std::vector< GlyphInfo > GlyphInfoRecords
Definition: Font.h:245
size_t unitsPerEM(bool embedded) const
Return height of the EM square used for glyphs definition.
Definition: Font.cpp:265
Definition: Font.h:51
Truetype font rasterizer/converter based on freetype library.
Definition: FreetypeGlyphsProvider.h:56
float get_kerning_adjustment(int last_code, int this_code) const
Definition: Font.cpp:251
boost::uint16_t m_char0
Definition: Font.h:54
GlyphInfoRecords::size_type glyphCount() const
Retrieve the number of embedded glyphs in this font.
Definition: Font.cpp:144
bool isItalic() const
Return true if the font is italic.
Definition: Font.h:214
void addFontNameInfo(const FontNameInfo &fontName)
Add display name and copyright name for an embedded font.
Definition: Font.cpp:125
Holds information needed to draw a shape.
Definition: ShapeRecord.h:126
float ascent(bool embedded) const
Return the ascent value of the font.
Definition: Font.cpp:348
Definition: GnashKey.h:331
GlyphInfo()
Definition: Font.cpp:57
int get_glyph_index(boost::uint16_t code, bool embedded) const
Return the glyph index for a given character code.
Definition: Font.cpp:209
const std::string & name() const
Get name of this font.
Definition: Font.h:143