Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
layout.h
Go to the documentation of this file.
1 
18 #ifndef ION_TEXT_LAYOUT_H_
19 #define ION_TEXT_LAYOUT_H_
20 
25 
26 #include <vector>
27 
28 #ifdef ION_PLATFORM_NACL
29 #include <sys/types.h>
35 #undef quad
36 #endif
37 
38 #include "ion/base/invalid.h"
40 #include "ion/math/range.h"
41 #include "ion/math/vector.h"
42 
43 namespace ion {
44 namespace text {
45 
48 
50 
53 
55  kAlignLeft, // Put the left edge of the text at the point.
56  kAlignHCenter, // Put the horizontal center of the text at the point.
57  kAlignRight, // Put the right edge of the text at the point.
58 };
59 
61  kAlignTop, // Put the top edge of the text at the point.
62  kAlignVCenter, // Put the vertical center of the text at the point.
63  kAlignBaseline, // Put the text baseline at the point.
64  kAlignBottom, // Put the bottom edge of the text at the point.
65 };
66 
68 
103  : target_point(0.0f, 0.0f),
104  target_size(0.0f, 1.0f),
107  line_spacing(1.0f) {}
108 
110  math::Point2f target_point;
112  math::Vector2f target_size;
120 };
121 
123 
127 class ION_API Layout {
128  public:
132  struct Quad {
134  Quad() {
135  points[0] = points[1] = points[2] = points[3] = math::Point3f::Zero();
136  }
137 
140  Quad(const math::Point3f& lower_left, const math::Point3f& lower_right,
141  const math::Point3f& upper_right, const math::Point3f& upper_left) {
142  points[0] = lower_left;
143  points[1] = lower_right;
144  points[2] = upper_right;
145  points[3] = upper_left;
146  }
147 
149  explicit Quad(const math::Point3f points_in[4]) {
150  for (int i = 0; i < 4; ++i)
151  points[i] = points_in[i];
152  }
153 
154  math::Point3f points[4]; // Quadrilateral points.
155  };
156 
160  struct Glyph {
163  Glyph() : glyph_index(0) {}
164 
166  Glyph(GlyphIndex glyph_index_in,
167  const Quad& quad_in,
168  const math::Range2f& bounds_in,
169  const math::Vector2f& offset_in)
170  : glyph_index(glyph_index_in), quad(quad_in),
171  bounds(bounds_in), offset(offset_in) {}
172 
173  GlyphIndex glyph_index; // Index of the glyph in the font.
174  Quad quad; // Quadrilateral points to use for rendering.
175  math::Range2f bounds; // Tight bounds of the glyph.
180  math::Vector2f offset;
181  };
182 
183  Layout() : line_advance_height_(0.f) {}
184  ~Layout() {}
185 
188  bool AddGlyph(const Glyph& glyph);
189 
191  size_t GetGlyphCount() const;
192 
194  void Reserve(size_t s);
195 
198  const Glyph& GetGlyph(size_t i) const;
199 
203  bool ReplaceGlyph(size_t i, const Glyph& new_glyph);
204 
206  void GetGlyphSet(GlyphSet* glyphs) const;
207 
210  float GetLineAdvanceHeight() const;
212  void SetLineAdvanceHeight(float line_advance);
213 
214  private:
215  std::vector<Glyph> glyphs_;
216  float line_advance_height_;
217 };
218 
220 std::ostream& operator<<(std::ostream& os, const Layout& layout);
221 std::ostream& operator<<(std::ostream& out, const Layout::Glyph& g);
222 std::ostream& operator<<(std::ostream& out, const Layout::Quad& q);
223 
224 } // namespace text
225 } // namespace ion
226 
227 #endif // ION_TEXT_LAYOUT_H_
std::ostream & operator<<(std::ostream &out, const Layout::Quad &q)
Helpers for logging Layouts/Glyphs/Quads.
Definition: layout.cc:61
VerticalAlignment
Definition: layout.h:60
std::string text
math::Vector2f offset
Offset from text insertion point to glyph bounds' lower left corner.
Definition: layout.h:180
Glyph(GlyphIndex glyph_index_in, const Quad &quad_in, const math::Range2f &bounds_in, const math::Vector2f &offset_in)
Constructor taking specifics.
Definition: layout.h:166
A Quad represents a 3D quadrilateral onto which a character glyph in the layout will be drawn...
Definition: layout.h:132
math::Point2f target_point
Location of the text rectangle. (Default: origin)
Definition: layout.h:110
uint32 offset
Range< 2, float > Range2f
Definition: range.h:373
This struct defines parameters affecting layout of a single text string when passed to BuildLayout()...
Definition: layout.h:101
Glyph()
The default constructor sets an invalid index (0, the NUL character) and sets all quadrilateral point...
Definition: layout.h:163
base::AllocSet< GlyphIndex > GlyphSet
Definition: layout.h:47
Quad()
The default constructor sets all points to the origin.
Definition: layout.h:134
Quad(const math::Point3f points_in[4])
Constructor taking all four quadrilateral points as an array.
Definition: layout.h:149
math::Vector2f target_size
Target width and height of the text rectangle. (Default: 0 in x, 1 in y)
Definition: layout.h:112
A Layout instance specifies how glyphs are arranged to form text.
Definition: layout.h:127
HorizontalAlignment horizontal_alignment
Text alignment in the horizontal direction. (Default: kAlignLeft)
Definition: layout.h:114
VerticalAlignment vertical_alignment
Text alignment in the vertical direction. (Default: kAlignBaseline)
Definition: layout.h:116
Quad(const math::Point3f &lower_left, const math::Point3f &lower_right, const math::Point3f &upper_right, const math::Point3f &upper_left)
Constructor taking all four individual quadrilateral points in the correct order. ...
Definition: layout.h:140
GlyphIndex glyph_index
Definition: layout.h:173
HorizontalAlignment
Alignment enums.
Definition: layout.h:54
float line_spacing
Spacing between baselines of lines of multi-line text, expressed as a fraction of the font's FontMetr...
Definition: layout.h:119
A Glyph represents one character glyph in the layout.
Definition: layout.h:160
uint64 GlyphIndex
Definition: layout.h:46
math::Range2f bounds
Definition: layout.h:175