Update with current status
[gnash.git] / libcore / FreetypeGlyphsProvider.h
bloba6f047b99cdd5a9cfadfcf70159ee53c0e93f4ab
1 // FreetypeGlyphsProvider.h: Freetype glyphs manager
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
20 #ifndef GNASH_FREETYPE_H
21 #define GNASH_FREETYPE_H
23 #include <string>
24 #include <memory> // for unique_ptr
25 #include <mutex>
26 #include <cstdint>
28 #ifdef USE_FREETYPE
29 # include <ft2build.h>
30 # include FT_FREETYPE_H
31 # include FT_GLYPH_H
32 #endif
34 // Forward declarations
35 namespace gnash {
36 namespace SWF {
37 class ShapeRecord;
42 namespace gnash {
44 /// Truetype font rasterizer/converter based on freetype library
46 /// Instances of this class provide rasterized or vectorial glyphs
47 /// for a given truetype font face.
48 ///
49 /// The rasterized glyphs have a max size of 96 (TODO: make parametrizable)
50 /// but I think the actual size could change between glyphs (see the 'box'
51 /// parameter of getRenderedGlyph() method).
52 ///
53 /// Vectorial glyphs are instances of a SWF::ShapeRecord, same class
54 /// resulting from parsing of embedded fonts.
55 ///
56 class FreetypeGlyphsProvider
59 public:
61 /// Named constructor for a face-bound rasterizer.
63 /// @param name
64 /// Name of the font to get glyphs info from
65 ///
66 /// @param bold
67 /// Whether to use a bold version of the font
68 ///
69 /// @param italic
70 /// Whether to use an italic version of the font
71 ///
72 /// @return a rasterizer bound to the given font name,
73 /// or a NULL unique_ptr if the given truetype font
74 /// could not be found.
75 ///
76 static std::unique_ptr<FreetypeGlyphsProvider> createFace(
77 const std::string& name, bool bold, bool italic);
79 /// Destructor
81 /// Release face resources
82 ///
83 ~FreetypeGlyphsProvider();
86 /// \brief
87 /// Return the given DisplayObject glyph as a shape DisplayObject definition
88 /// in unitsPerEM() coordinates.
90 ///
91 /// TODO: allow using a custom EM square ?
92 ///
93 /// @param code
94 /// Character code.
95 ///
96 /// @param advance
97 /// Output parameter... units to advance horizontally from this
98 /// glyph to the next, in unitsPerEM() units.
99 ///
100 /// @return A DefineShapeTag in unitsPerEM() coordinates,
101 /// or a NULL pointer if the given DisplayObject code
102 /// doesn't exist in this font.
104 std::unique_ptr<SWF::ShapeRecord> getGlyph(std::uint16_t code,
105 float& advance);
107 /// Return the font's ascender in terms of its EM own square.
108 float ascent() const;
110 /// Return the font's descender in terms of its own EM square.
111 float descent() const;
113 /// Return the number of units of glyphs EM
115 /// This is currently hard-coded to 1024, but could in future depend
116 /// on actual font file being used.
118 unsigned short unitsPerEM() const;
120 private:
122 /// Use the named constructor to create an instance
124 /// throw a GnashException on error (unkonwn font name or similar).
126 FreetypeGlyphsProvider(const std::string& fontname, bool bold, bool italic);
128 #ifdef USE_FREETYPE
130 /// Scale factor to make the freetype glyph metrix match our unitsPerEM()
131 /// coordinate space. Not all font faces have am EM square of unitsPerEM(), so we
132 /// use this value to scale both coordinates and advance values
133 /// The value is computed by the costructor, as soon as a face is initialized.
134 float scale;
136 /// Get filename containing given font
138 /// @param name
139 /// Font name
141 /// @param bold
142 /// Want bold version
144 /// @param italic
145 /// Want italic version
147 /// @param filename
148 /// Where to return the filename to
150 /// @return true if the font was found, false otherwise.
151 /// Actually, this function should return a default
152 /// filename in any case, so false should only be
153 /// returned if not even a default font was found.
155 bool getFontFilename(const std::string& name, bool bold, bool italic,
156 std::string& filename);
158 /// Initialize the FreeType library if not done so yet
159 static void init();
161 static void close();
163 /// Mutex protecting FreeType library (for initialization basically)
164 static std::mutex m_lib_mutex;
166 /// FreeType library
167 static FT_Library m_lib;
169 FT_Face _face;
171 #endif // USE_FREETYPE
175 } // namespace gnash
178 #endif // GNASH_FREETYPE_H