GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
txt-eng-ft.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2013 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (txt_eng_ft_h)
24 #define txt_eng_ft_h 1
25 
26 #if HAVE_FREETYPE
27 
28 #include <list>
29 #include <vector>
30 
31 #include <ft2build.h>
32 #include FT_FREETYPE_H
33 
34 #include <dMatrix.h>
35 #include <uint8NDArray.h>
36 #include "txt-eng.h"
37 
38 class
41 {
42 public:
43  enum
44  {
45  MODE_BBOX = 0,
46  MODE_RENDER = 1
47  };
48 
49  enum
50  {
51  ROTATION_0 = 0,
52  ROTATION_90 = 1,
53  ROTATION_180 = 2,
54  ROTATION_270 = 3
55  };
56 
57 public:
58  ft_render (void);
59 
60  ~ft_render (void);
61 
62  void visit (text_element_string& e);
63 
64  void visit (text_element_list& e);
65 
66  void visit (text_element_subscript& e);
67 
68  void visit (text_element_superscript& e);
69 
70  void visit (text_element_color& e);
71 
72  void visit (text_element_fontsize& e);
73 
74  void visit (text_element_fontname& e);
75 
76  void visit (text_element_fontstyle& e);
77 
78  void visit (text_element_symbol& e);
79 
80  void visit (text_element_combined& e);
81 
82  void reset (void);
83 
84  uint8NDArray get_pixels (void) const { return pixels; }
85 
86  Matrix get_boundingbox (void) const { return bbox; }
87 
88  uint8NDArray render (text_element* elt, Matrix& box,
89  int rotation = ROTATION_0);
90 
91  Matrix get_extent (text_element *elt, double rotation = 0.0);
92  Matrix get_extent (const std::string& txt, double rotation = 0.0,
93  const caseless_str& interpreter = "tex");
94 
95  void set_font (const std::string& name, const std::string& weight,
96  const std::string& angle, double size);
97 
98  void set_color (Matrix c);
99 
100  void set_mode (int m);
101 
102  void text_to_pixels (const std::string& txt,
103  uint8NDArray& pixels_, Matrix& bbox,
104  int halign, int valign, double rotation,
105  const caseless_str& interpreter = "tex");
106 
107 private:
108  int rotation_to_mode (double rotation) const;
109 
110  // No copying!
111 
112  ft_render (const ft_render&);
113 
114  ft_render& operator = (const ft_render&);
115 
116  // Class to hold information about fonts and a strong
117  // reference to the font objects loaded by freetype.
118  class ft_font
119  {
120  public:
121  ft_font (void)
122  : name (), weight (), angle (), size (0), face (0) { }
123 
124  ft_font (const std::string& nm, const std::string& wt,
125  const std::string& ang, double sz, FT_Face f = 0)
126  : name (nm), weight (wt), angle (ang), size (sz), face (f) { }
127 
128  ft_font (const ft_font& ft);
129 
130  ~ft_font (void)
131  {
132  if (face)
133  FT_Done_Face (face);
134  }
135 
136  ft_font& operator = (const ft_font& ft);
137 
138  bool is_valid (void) const { return get_face (); }
139 
140  std::string get_name (void) const { return name; }
141 
142  std::string get_weight (void) const { return weight; }
143 
144  std::string get_angle (void) const { return angle; }
145 
146  double get_size (void) const { return size; }
147 
148  FT_Face get_face (void) const;
149 
150  private:
151  std::string name;
152  std::string weight;
153  std::string angle;
154  double size;
155  mutable FT_Face face;
156  };
157 
158  void push_new_line (void);
159 
160  void update_line_bbox (void);
161 
162  void compute_bbox (void);
163 
164  int compute_line_xoffset (const Matrix& lb) const;
165 
166  FT_UInt process_character (FT_ULong code, FT_UInt previous = 0);
167 
168 private:
169  // The current font used by the renderer.
171 
172  // Used to stored the bounding box corresponding to the rendered text.
173  // The bounding box has the form [x, y, w, h] where x and y represent the
174  // coordinates of the bottom left corner relative to the anchor point of
175  // the text (== start of text on the baseline). Due to font descent or
176  // multiple lines, the value y is usually negative.
178 
179  // Used to stored the rendered text. It's a 3D matrix with size MxNx4
180  // where M and N are the width and height of the bounding box.
182 
183  // Used to store the bounding box of each line. This is used to layout
184  // multiline text properly.
185  std::list<Matrix> line_bbox;
186 
187  // The current horizontal alignment. This is used to align multi-line text.
188  int halign;
189 
190  // The X offset for the next glyph.
191  int xoffset;
192 
193  // The Y offset of the baseline for the current line.
195 
196  // The Y offset of the baseline for the next glyph. The offset is relative
197  // to line_yoffset. The total Y offset is computed with:
198  // line_yoffset + yoffset.
199  int yoffset;
200 
201  // The current mode of the rendering process (box computing or rendering).
202  int mode;
203 
204  // The base color of the rendered text.
206 };
207 
208 #endif // HAVE_FREETYPE
209 
210 #endif