TextBlock.h
1 /* Copyright (C) 2017 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef Pt_Forms_TextBlock_H
30 #define Pt_Forms_TextBlock_H
31 
32 #include <Pt/Forms/Adjustment.h>
33 #include <Pt/Gfx/Font.h>
34 #include <Pt/Gfx/Size.h>
35 #include <Pt/Gfx/TextMetrics.h>
36 #include <Pt/Gfx/FontMetrics.h>
37 #include <Pt/Gfx/PaintSurface.h>
38 #include <Pt/Gfx/Point.h>
39 #include <Pt/String.h>
40 #include <vector>
41 
42 namespace Pt {
43 
44 namespace Forms {
45 
46 class Painter;
47 
57 class PT_FORMS_API TextLine
58 {
59  public:
63 
67 
70  const Gfx::PointF& position() const;
71 
74  void setPosition(const Gfx::PointF& p);
75 
78  void setPosition(double x, double y);
79 
82  double width() const;
83 
86  double height() const;
87 
90  double maxHeight() const;
91 
94  double ascent() const;
95 
98  double descent() const;
99 
102  const Pt::String& text() const;
103 
104  //void setText(const Pt::String& text, const Gfx::Font& font);
105 
108  void setText(const Pt::String& text, const Gfx::TextMetrics& tm,
109  const Gfx::FontMetrics& fm);
110 
113  double cursorToX(const Painter& painter, std::size_t n) const;
114 
117  std::size_t xToCursor(const Painter& painter, double x) const;
118 
119  private:
120  Gfx::PointF _position;
121  Pt::String _text;
122  Gfx::TextMetrics _textMetrics;
123  Gfx::FontMetrics _fontMetrics;
124 };
125 
126 
147 class PT_FORMS_API TextBlock
148 {
149  public:
152  typedef TextLine* Iterator;
153 
156  typedef const TextLine* ConstIterator;
157 
158  public:
165 
169 
172  const Gfx::PointF& position() const;
173 
176  void setPosition(const Gfx::PointF& p);
177 
180  const Gfx::SizeF& size() const;
181 
184  double width() const;
185 
188  double height() const;
189 
192  double maxWidth() const;
193 
196  void setMaxWidth(double w);
197 
201 
205 
208  void setLineSpacing(double v)
209  {
210  _lineSpacing = v;
211  }
212 
215  double lineSpacing() const
216  {
217  return _lineSpacing;
218  }
219 
223 
227 
231 
235 
240  void layout(const Painter& painter, const Pt::String& text);
241 
242  private:
243  void addLine(const Pt::String& line, const Gfx::TextMetrics& tm,
244  const Gfx::FontMetrics& fm);
245 
246  double align(double v) const
247  {
248  return v;
249  }
250 
251  private:
252  Gfx::PointF _position;
253  Gfx::SizeF _size;
254  double _maxWidth;
255  Adjustment _adjustment;
256  std::vector<TextLine> _lines;
257  double _lineSpacing;
258 };
259 
260 } // namespace
261 
262 } // namespace
263 
264 #endif
Core module.
Definition: Allocator.h:33
~TextBlock()
Destructor.
void setText(const Pt::String &text, const Gfx::TextMetrics &tm, const Gfx::FontMetrics &fm)
Assigns text and the metrics used to lay it out.
double maxHeight() const
Returns the sum of ascent and descent.
void layout(const Painter &painter, const Pt::String &text)
Wraps text into lines using painter's font metrics.
double width() const
Returns the laid-out width.
void setAdjustment(Adjustment a)
Sets the horizontal alignment of each line.
TextLine * Iterator
Iterator over the laid-out lines.
Definition: TextBlock.h:152
Adjustment adjustment() const
Returns the horizontal alignment of each line.
const Pt::String & text() const
Returns the line text.
double height() const
Returns the laid-out height.
void setPosition(const Gfx::PointF &p)
Sets the position relative to the text block origin.
Metrics that describe a font face at a given size.
Definition: FontMetrics.h:46
void setPosition(double x, double y)
Sets the position relative to the text block origin.
double height() const
Returns the font height of the line.
Draws on a Forms paint surface or paint context.
Definition: Painter.h:53
Represents wrapped text as positioned lines.
Definition: TextBlock.h:148
~TextLine()
Destructor.
Size with floating-point width and height.
Definition: Size.h:47
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Iterator begin()
Returns a pointer to the first line, or 0 when empty.
const Gfx::PointF & position() const
Returns the position relative to the text block origin.
void setLineSpacing(double v)
Sets extra space between consecutive lines.
Definition: TextBlock.h:208
void setPosition(const Gfx::PointF &p)
Sets the origin of the block.
const Gfx::PointF & position() const
Returns the origin of the block.
Metrics for a measured line of text.
Definition: TextMetrics.h:46
TextLine()
Creates an empty line.
Iterator end()
Returns a pointer past the last line, or 0 when empty.
ConstIterator end() const
Returns a pointer past the last line, or 0 when empty.
double width() const
Returns the advance width of the line text.
void setMaxWidth(double w)
Sets the maximum width used to wrap lines.
Aligns content along one axis.
Definition: Adjustment.h:60
std::size_t xToCursor(const Painter &painter, double x) const
Returns the caret index nearest to x.
double maxWidth() const
Returns the maximum width used to wrap lines.
TextBlock()
Creates an empty text block.
double lineSpacing() const
Returns extra space between consecutive lines.
Definition: TextBlock.h:215
const TextLine * ConstIterator
Const iterator over the laid-out lines.
Definition: TextBlock.h:156
const Gfx::SizeF & size() const
Returns the laid-out width and height.
Unicode capable basic_string.
Definition: Api-String.h:44
double cursorToX(const Painter &painter, std::size_t n) const
Returns the x position of caret index n.
Represents one laid-out line in a TextBlock.
Definition: TextBlock.h:58
double descent() const
Returns the font descent of the line.
ConstIterator begin() const
Returns a pointer to the first line, or 0 when empty.
double ascent() const
Returns the font ascent of the line.