update copyright date
[gnash.git] / libcore / swf / TextRecord.h
blob33e95e15aad28f1ec6fc980ae5fcf6220100d37b
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 // 2011 Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_SWF_TEXTRECORD_H
20 #define GNASH_SWF_TEXTRECORD_H
22 #include "RGBA.h"
23 #include "SWF.h"
24 #include <string>
25 #include <vector>
27 namespace gnash {
28 class movie_definition;
29 class SWFStream;
30 class Font;
31 class Renderer;
32 class Transform;
35 namespace gnash {
36 namespace SWF {
38 /// Store a TextRecord.
40 /// This consists of style information and a number of glyphs.
41 /// This may be parsed from a SWFStream, or it can be constructed
42 /// dynamically by TextField. A static TextField has fewer possible
43 /// properties than a dynamic one.
44 class TextRecord
46 public:
48 typedef std::vector<TextRecord> TextRecords;
50 struct GlyphEntry
52 int index;
53 float advance;
56 TextRecord()
58 _color(0, 0, 0, 0),
59 _textHeight(0),
60 _hasXOffset(false),
61 _hasYOffset(false),
62 _xOffset(0.0f),
63 _yOffset(0.0f),
64 _font(0),
65 _underline(false)
68 typedef std::vector<GlyphEntry> Glyphs;
70 /// Accumulate the number of glyphs in a TextRecord.
71 struct RecordCounter
73 size_t operator()(size_t c, const TextRecord& t) {
74 const Glyphs& glyphs = t.glyphs();
75 size_t ret = c + glyphs.size();
76 return ret;
80 /// Read a TextRecord from the stream
82 /// @param in The SWFStream to read from.
83 /// @param m The movie_definition containing this TextRecord.
84 /// @param glyphBits The number of bits per glyph
85 /// @param advanceBits The number of bits per advance
86 /// @param tag The tag type of this TextRecord. This must be
87 /// DefineText or DefineText2
88 /// @return False if we have reached the end of the
89 /// TextRecords, true if there are more to parse.
90 bool read(SWFStream& in, movie_definition& m, int glyphBits,
91 int advanceBits, TagType tag);
93 static void displayRecords(Renderer& renderer, const Transform& xform,
94 const TextRecords& records, bool embedded = true);
96 const Glyphs& glyphs() const {
97 return _glyphs;
100 void addGlyph(const GlyphEntry& ge, Glyphs::size_type num = 1) {
101 _glyphs.insert(_glyphs.end(), num, ge);
104 void clearGlyphs(Glyphs::size_type num = 0) {
105 if (!num) _glyphs.clear();
106 else _glyphs.resize(_glyphs.size() - num);
109 // TODO: check font properly.
110 void setFont(const Font* f) {
111 _font = f;
114 void setURL(std::string url) {
115 _htmlURL = url;
118 const std::string& getURL() const {
119 return _htmlURL;
122 void setTarget(std::string target) {
123 _htmlTarget = target;
126 const std::string& getTarget() const {
127 return _htmlTarget;
130 const Font* getFont() const {
131 return _font;
134 void setTextHeight(boost::uint16_t height) {
135 _textHeight = height;
138 float recordWidth() const {
139 float width = 0.0f;
140 for (size_t i = 0; i < glyphs().size(); ++i)
142 width += glyphs()[i].advance;
144 return width;
147 boost::uint16_t textHeight() const {
148 return _textHeight;
151 bool hasXOffset() const {
152 return _hasXOffset;
155 void setXOffset(float x) {
156 _hasXOffset = true;
157 _xOffset = x;
160 float xOffset() const {
161 return _xOffset;
164 bool hasYOffset() const {
165 return _hasYOffset;
168 void setYOffset(float y) {
169 _hasYOffset = true;
170 _yOffset = y;
173 float yOffset() const {
174 return _yOffset;
177 void setColor(const rgba& color) {
178 _color = color;
181 const rgba& color() const {
182 return _color;
185 bool underline() const {
186 return _underline;
189 void setUnderline(bool b) {
190 _underline = b;
193 private:
195 Glyphs _glyphs;
197 /// The text color.
198 rgba _color;
200 /// The height of the text in TWIPS.
201 boost::uint16_t _textHeight;
203 /// Whether the TextRecord has an x offset.
204 bool _hasXOffset;
206 /// Whether the TextRecord has a y offset.
207 bool _hasYOffset;
209 /// The x offset of the text, by default 0.0
210 float _xOffset;
212 /// The y offset of the text, by default 0.0
213 float _yOffset;
215 /// The font associated with the TextRecord. Can be NULL.
216 const Font* _font;
218 std::string _htmlURL;
219 std::string _htmlTarget;
220 /// Whether the text should be underlined.
221 bool _underline;
224 } // namespace SWF
225 } // namespace gnash
228 #endif