QCAD
Open Source 2D CAD
RTextRenderer.h
Go to the documentation of this file.
1 
20 #ifndef RTEXTRENDERER_H
21 #define RTEXTRENDERER_H
22 
23 #include "core_global.h"
24 
25 #include <QTextLayout>
26 
27 #include "RDocument.h"
28 #include "REntityData.h"
29 #include "RPainterPath.h"
30 #include "RPainterPathSource.h"
31 #include "RTextBasedData.h"
32 #include "RTextLayout.h"
33 #include "RVector.h"
34 
35 //class QTextDocument;
36 
44 public:
45  enum Target {
46  PainterPaths = 0x01,
47  RichText = 0x02
48  // TODO: TextLayout = 0x03
49  };
50 
51 public:
52  RTextRenderer(const RTextBasedData& textData, bool draft, RTextRenderer::Target target, double fontHeightFactor = 1.0);
53 
54  void renderSimple();
55  void render();
56 
57  QList<RPainterPath> getPainterPaths() const {
58  return painterPaths;
59  }
60 
61  QList<RTextLayout> getTextLayouts() const {
62  return textLayouts;
63  }
64 
65 // QList<QTransform> getTextTransforms() const {
66 // return textTransforms;
67 // }
68 
69  RBox getBoundingBox() const {
70  return boundingBox;
71  }
72 
73  double getHeight() const {
74  return height;
75  }
76 
77  double getWidth() const {
78  return width;
79  }
80 
81  QString getRichText() const {
82  return richText;
83  }
84 
85 private:
86  QList<RPainterPath> getPainterPathsForBlock(
87  const QString& blockText,
88  const QList<QTextLayout::FormatRange>& formats,
89  double& horizontalAdvance,
90  double& horizontalAdvanceNoSpacing,
91  double& ascent,
92  double& descent);
93 
94  QList<RPainterPath> getPainterPathsForBlockTtf(
95  const QString& blockText,
96  const QList<QTextLayout::FormatRange>& formats,
97  double& horizontalAdvance,
98  double& horizontalAdvanceNoSpacing,
99  double& ascent,
100  double& descent);
101 
102  QList<RPainterPath> getPainterPathsForBlockCad(
103  const QString& blockText,
104  const QList<QTextLayout::FormatRange>& formats,
105  double& horizontalAdvance,
106  double& horizontalAdvanceNoSpacing,
107  double& ascent,
108  double& descent);
109 
110  void preparePath(RPainterPath& path,
111  const RColor& color,
112  double cursor,
113  double cxfScale);
114 
115  void preparePathColor(RPainterPath& path, const RColor& color);
116  void preparePathTransform(RPainterPath& path, double cursor, double cxfScale);
117 
118  QRectF getCharacterRect(const QString& fontName, const QChar& ch) const;
119  QRectF getCharacterRect(const QFont& font, const QChar& ch) const;
120 
121  QString getRichTextForBlock(const QString& blockText,
122  const QList<QTextLayout::FormatRange>& formats);
123 
124  void setBlockFont(const QString& f) {
125  if (!blockFont.isEmpty()) {
126  blockFont.top() = f;
127  }
128  }
129 
130  QString getBlockFont() const {
131  if (!blockFont.isEmpty()) {
132  return blockFont.top();
133  }
134  return "";
135  }
136 
137  void setBlockFontFile(const QString& f) {
138  if (!blockFontFile.isEmpty()) {
139  blockFontFile.top() = f;
140  }
141  }
142 
143  QString getBlockFontFile() const {
144  if (!blockFontFile.isEmpty()) {
145  return blockFontFile.top();
146  }
147  return "";
148  }
149 
150  void setUseCadFont(bool on) {
151  if (!useCadFont.isEmpty()) {
152  useCadFont.top() = on;
153  }
154  }
155 
156  bool getUseCadFont() const {
157  if (!useCadFont.isEmpty()) {
158  return useCadFont.top();
159  }
160  return false;
161  }
162 
163  void setBlockBold(bool on) {
164  if (!blockBold.isEmpty()) {
165  blockBold.top() = on;
166  }
167  }
168 
169  bool getBlockBold() const {
170  if (!blockBold.isEmpty()) {
171  return blockBold.top();
172  }
173  return false;
174  }
175 
176  void setBlockItalic(bool on) {
177  if (!blockItalic.isEmpty()) {
178  blockItalic.top() = on;
179  }
180  }
181 
182  bool getBlockItalic() const {
183  if (!blockItalic.isEmpty()) {
184  return blockItalic.top();
185  }
186  return false;
187  }
188 
189  void setBlockHeight(double h) {
190  if (!blockHeight.isEmpty()) {
191  blockHeight.top() = h;
192  }
193  }
194 
195  double getBlockHeight() const {
196  if (!blockHeight.isEmpty()) {
197  return blockHeight.top();
198  }
199  return 1.0;
200  }
201 
202  void setCurrentFormat(QTextCharFormat f) {
203  if (!currentFormat.isEmpty()) {
204  currentFormat.top() = f;
205  }
206  }
207 
208  QTextCharFormat getCurrentFormat() const {
209  if (!currentFormat.isEmpty()) {
210  return currentFormat.top();
211  }
212  return QTextCharFormat();
213  }
214 
215 public:
216  static QString rxLineFeed;
217  static QString rxAlignmentLeft;
218  static QString rxAlignmentCenter;
219  static QString rxAlignmentRight;
220  static QString rxXAlignmentLeft;
221  static QString rxXAlignmentCenter;
222  static QString rxXAlignmentRight;
223  static QString rxParagraphFeed;
224  static QString rxXFeed;
225  static QString rxHeightChange;
226  //static QString rxRelativeHeightChange;
227  static QString rxStackedText;
228  static QString rxColorChangeIndex;
229  static QString rxColorChangeCustom;
230  static QString rxNonBreakingSpace;
231  static QString rxOverlineOn;
232  static QString rxOverlineOff;
233  static QString rxUnderlineOn;
234  static QString rxUnderlineOff;
235  static QString rxStrikethroughOn;
236  static QString rxStrikethroughOff;
237  static QString rxWidthChange;
238  static QString rxObliqueAngleChange;
239  static QString rxTrackChange;
240  static QString rxAlignmentChange;
241  static QString rxFontChangeCad;
242  static QString rxFontChangeTtf;
243  static QString rxBeginBlock;
244  static QString rxEndBlock;
245  static QString rxBackslash;
246  static QString rxCurlyOpen;
247  static QString rxCurlyClose;
248  static QString rxDegree;
249  static QString escDegree;
250  static QString rxPlusMinus;
251  static QString escPlusMinus;
252  static QString rxDiameter;
253  static QString escDiameter;
254  static QString rxUnderlined;
255  static QString rxUnicode;
256 
257  static QString rxAll;
258 
259  static QChar chDegree;
260  static QChar chPlusMinus;
261  static QChar chDiameter;
262 
263 private:
265 
267 
268  // painter paths used to draw text geometry as painter path:
269  QList<RPainterPath> painterPaths;
270 
271  // text layouts and transformations used to draw text as QTextLayout instead of painter paths:
272  QList<RTextLayout> textLayouts;
273  // list of block transforms of all QTextLayout blocks of the current line:
274  QList<QTransform> lineBlockTransforms;
275 
277  double height;
278  double width;
279  bool draft;
280  QString richText;
282 
283  QStack<bool> useCadFont;
284  QStack<QTextCharFormat> currentFormat;
285  QStack<double> blockHeight;
286  QStack<QString> blockFont;
287  QStack<QString> blockFontFile;
288  QStack<bool> blockBold;
289  QStack<bool> blockItalic;
290  QStack<QStringList> openTags;
291 };
292 
293 
297 
298 #endif
Represents a box e.g.
Definition: RBox.h:43
static QString rxDiameter
Definition: RTextRenderer.h:252
QList< RTextLayout > getTextLayouts() const
Definition: RTextRenderer.h:61
static QString rxXAlignmentLeft
Definition: RTextRenderer.h:220
QString getRichText() const
Definition: RTextRenderer.h:81
static QString rxAlignmentCenter
Definition: RTextRenderer.h:218
bool getUseCadFont() const
Definition: RTextRenderer.h:156
static QString rxAll
Definition: RTextRenderer.h:257
static QString rxCurlyOpen
Definition: RTextRenderer.h:246
static QString rxBeginBlock
Definition: RTextRenderer.h:243
static QString rxLineFeed
Definition: RTextRenderer.h:216
QStack< QString > blockFont
Definition: RTextRenderer.h:286
static QChar chDiameter
Definition: RTextRenderer.h:261
static QString escDegree
Definition: RTextRenderer.h:249
QStack< QStringList > openTags
Definition: RTextRenderer.h:290
static QString rxStrikethroughOff
Definition: RTextRenderer.h:236
static QString rxColorChangeCustom
Definition: RTextRenderer.h:229
Target
Definition: RTextRenderer.h:45
QList< RPainterPath > getPainterPaths() const
Definition: RTextRenderer.h:57
#define QCADCORE_EXPORT
Definition: core_global.h:10
static QString rxXFeed
Definition: RTextRenderer.h:224
double getWidth() const
Definition: RTextRenderer.h:77
static QString rxObliqueAngleChange
Definition: RTextRenderer.h:238
static QString rxEndBlock
Definition: RTextRenderer.h:244
RBox getBoundingBox() const
Definition: RTextRenderer.h:69
void setCurrentFormat(QTextCharFormat f)
Definition: RTextRenderer.h:202
double fontHeightFactor
Definition: RTextRenderer.h:281
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RTextRenderer.h:43
void setBlockFontFile(const QString &f)
Definition: RTextRenderer.h:137
static QString rxXAlignmentRight
Definition: RTextRenderer.h:222
bool draft
Definition: RTextRenderer.h:279
static QString rxUnderlined
Definition: RTextRenderer.h:254
QStack< QTextCharFormat > currentFormat
Definition: RTextRenderer.h:284
Stores and manages all data that defines the geometry and appearance of a text based entity (text...
Definition: RTextBasedData.h:44
Color.
Definition: RColor.h:42
static QString rxTrackChange
Definition: RTextRenderer.h:239
static QChar chDegree
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RTextRenderer.h:259
static QString rxStackedText
Definition: RTextRenderer.h:227
void setBlockHeight(double h)
Definition: RTextRenderer.h:189
const RTextBasedData & textData
Definition: RTextRenderer.h:264
static QString rxParagraphFeed
Definition: RTextRenderer.h:223
void setBlockFont(const QString &f)
Definition: RTextRenderer.h:124
static QString escPlusMinus
Definition: RTextRenderer.h:251
static QString rxAlignmentRight
Definition: RTextRenderer.h:219
RBox boundingBox
Definition: RTextRenderer.h:276
Q_DECLARE_METATYPE(RMath *)
static QString rxHeightChange
Definition: RTextRenderer.h:225
QString richText
Definition: RTextRenderer.h:280
QList< RPainterPath > painterPaths
Definition: RTextRenderer.h:269
static QString rxDegree
Definition: RTextRenderer.h:248
static QString rxOverlineOff
Definition: RTextRenderer.h:232
static QString rxBackslash
Definition: RTextRenderer.h:245
Extended painter path with a z-level and a pen.
Definition: RPainterPath.h:46
static QChar chPlusMinus
Definition: RTextRenderer.h:260
static QString rxUnderlineOff
Definition: RTextRenderer.h:234
static QString rxColorChangeIndex
Definition: RTextRenderer.h:228
QStack< bool > blockItalic
Definition: RTextRenderer.h:289
void setUseCadFont(bool on)
Definition: RTextRenderer.h:150
QStack< bool > useCadFont
Definition: RTextRenderer.h:283
QStack< bool > blockBold
Definition: RTextRenderer.h:288
QStack< double > blockHeight
Definition: RTextRenderer.h:285
static QString rxUnderlineOn
Definition: RTextRenderer.h:233
QString getBlockFont() const
Definition: RTextRenderer.h:130
static QString rxWidthChange
Definition: RTextRenderer.h:237
QStack< QString > blockFontFile
Definition: RTextRenderer.h:287
QTextCharFormat getCurrentFormat() const
Definition: RTextRenderer.h:208
static QString rxFontChangeCad
Definition: RTextRenderer.h:241
static QString rxXAlignmentCenter
Definition: RTextRenderer.h:221
static QString rxStrikethroughOn
Definition: RTextRenderer.h:235
QString getBlockFontFile() const
Definition: RTextRenderer.h:143
static QString rxFontChangeTtf
Definition: RTextRenderer.h:242
static QString rxAlignmentLeft
Definition: RTextRenderer.h:217
QList< QTransform > lineBlockTransforms
Definition: RTextRenderer.h:274
double height
Definition: RTextRenderer.h:277
static QString rxOverlineOn
Definition: RTextRenderer.h:231
double getBlockHeight() const
Definition: RTextRenderer.h:195
static QString rxCurlyClose
Definition: RTextRenderer.h:247
void setBlockBold(bool on)
Definition: RTextRenderer.h:163
static QString rxPlusMinus
Definition: RTextRenderer.h:250
static QString rxUnicode
Definition: RTextRenderer.h:255
Target target
Definition: RTextRenderer.h:266
static QString rxAlignmentChange
Definition: RTextRenderer.h:240
double getHeight() const
Definition: RTextRenderer.h:73
static QString escDiameter
Definition: RTextRenderer.h:253
bool getBlockBold() const
Definition: RTextRenderer.h:169
double width
Definition: RTextRenderer.h:278
bool getBlockItalic() const
Definition: RTextRenderer.h:182
QList< RTextLayout > textLayouts
Definition: RTextRenderer.h:272
static QString rxNonBreakingSpace
Definition: RTextRenderer.h:230
void setBlockItalic(bool on)
Definition: RTextRenderer.h:176