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 PT_FORMS_API TextLine
47 {
48  public:
49  TextLine();
50 
51  ~TextLine();
52 
53  const Gfx::PointF& position() const;
54 
55  void setPosition(const Gfx::PointF& p);
56 
57  void setPosition(double x, double y);
58 
59  double width() const;
60 
61  double height() const;
62 
63  double maxHeight() const;
64 
65  double ascent() const;
66 
67  double descent() const;
68 
69  const Pt::String& text() const;
70 
71  //void setText(const Pt::String& text, const Gfx::Font& font);
72 
73  void setText(const Pt::String& text, const Gfx::TextMetrics& tm,
74  const Gfx::FontMetrics& fm);
75 
76  double cursorToX(const Gfx::Painter& painter, std::size_t n) const;
77 
78  std::size_t xToCursor(const Gfx::Painter& painter, double x) const;
79 
80  private:
81  Gfx::PointF _position;
82  Pt::String _text;
83  Gfx::TextMetrics _textMetrics;
84  Gfx::FontMetrics _fontMetrics;
85 };
86 
87 class PT_FORMS_API TextBlock
88 {
89  public:
90  typedef TextLine* Iterator;
91  typedef const TextLine* ConstIterator;
92 
93  public:
94  TextBlock();
95 
96  ~TextBlock();
97 
98  const Gfx::PointF& position() const;
99 
100  void setPosition(const Gfx::PointF& p);
101 
102  const Gfx::SizeF& size() const;
103 
104  double width() const;
105 
106  double height() const;
107 
108  double maxWidth() const;
109 
110  void setMaxWidth(double w);
111 
112  void setAdjustment(Adjustment a);
113 
114  Adjustment adjustment() const;
115 
116  void setLineSpacing(double v)
117  {
118  _lineSpacing = v;
119  }
120 
121  double lineSpacing() const
122  {
123  return _lineSpacing;
124  }
125 
126  Iterator begin();
127 
128  Iterator end();
129 
130  ConstIterator begin() const;
131 
132  ConstIterator end() const;
133 
134  void layout(const Gfx::Painter& painter, const Pt::String& text);
135 
136  private:
137  void addLine(const Pt::String& line, const Gfx::TextMetrics& tm,
138  const Gfx::FontMetrics& fm);
139 
140  double align(double v) const
141  {
142  return v;
143  }
144 
145  private:
146  Gfx::PointF _position;
147  Gfx::SizeF _size;
148  double _maxWidth;
149  Adjustment _adjustment;
150  std::vector<TextLine> _lines;
151  double _lineSpacing;
152 };
153 
154 } // namespace
155 
156 } // namespace
157 
158 #endif
Unicode capable basic_string.
Definition: String.h:42