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  QRectF getCharacterRect(const QString& fontName, const QChar& ch) const;
116  QRectF getCharacterRect(const QFont& font, const QChar& ch) const;
117 
118  QString getRichTextForBlock(const QString& blockText,
119  const QList<QTextLayout::FormatRange>& formats);
120 
121  void setBlockFont(const QString& f) {
122  if (!blockFont.isEmpty()) {
123  blockFont.top() = f;
124  }
125  }
126 
127  QString getBlockFont() const {
128  if (!blockFont.isEmpty()) {
129  return blockFont.top();
130  }
131  return "";
132  }
133 
134  void setBlockFontFile(const QString& f) {
135  if (!blockFontFile.isEmpty()) {
136  blockFontFile.top() = f;
137  }
138  }
139 
140  QString getBlockFontFile() const {
141  if (!blockFontFile.isEmpty()) {
142  return blockFontFile.top();
143  }
144  return "";
145  }
146 
147  void setUseCadFont(bool on) {
148  if (!useCadFont.isEmpty()) {
149  useCadFont.top() = on;
150  }
151  }
152 
153  bool getUseCadFont() const {
154  if (!useCadFont.isEmpty()) {
155  return useCadFont.top();
156  }
157  return false;
158  }
159 
160  void setBlockBold(bool on) {
161  if (!blockBold.isEmpty()) {
162  blockBold.top() = on;
163  }
164  }
165 
166  bool getBlockBold() const {
167  if (!blockBold.isEmpty()) {
168  return blockBold.top();
169  }
170  return false;
171  }
172 
173  void setBlockItalic(bool on) {
174  if (!blockItalic.isEmpty()) {
175  blockItalic.top() = on;
176  }
177  }
178 
179  bool getBlockItalic() const {
180  if (!blockItalic.isEmpty()) {
181  return blockItalic.top();
182  }
183  return false;
184  }
185 
186  void setBlockHeight(double h) {
187  if (!blockHeight.isEmpty()) {
188  blockHeight.top() = h;
189  }
190  }
191 
192  double getBlockHeight() const {
193  if (!blockHeight.isEmpty()) {
194  return blockHeight.top();
195  }
196  return 1.0;
197  }
198 
199  void setCurrentFormat(QTextCharFormat f) {
200  if (!currentFormat.isEmpty()) {
201  currentFormat.top() = f;
202  }
203  }
204 
205  QTextCharFormat getCurrentFormat() const {
206  if (!currentFormat.isEmpty()) {
207  return currentFormat.top();
208  }
209  return QTextCharFormat();
210  }
211 
212 public:
213  static QString rxLineFeed;
214  static QString rxAlignmentLeft;
215  static QString rxAlignmentCenter;
216  static QString rxAlignmentRight;
217  static QString rxParagraphFeed;
218  static QString rxXFeed;
219  static QString rxHeightChange;
220  //static QString rxRelativeHeightChange;
221  static QString rxStackedText;
222  static QString rxColorChangeIndex;
223  static QString rxColorChangeCustom;
224  static QString rxNonBreakingSpace;
225  static QString rxOverlineOn;
226  static QString rxOverlineOff;
227  static QString rxUnderlineOn;
228  static QString rxUnderlineOff;
229  static QString rxWidthChange;
230  static QString rxObliqueAngleChange;
231  static QString rxTrackChange;
232  static QString rxAlignmentChange;
233  static QString rxFontChangeCad;
234  static QString rxFontChangeTtf;
235  static QString rxBeginBlock;
236  static QString rxEndBlock;
237  static QString rxBackslash;
238  static QString rxCurlyOpen;
239  static QString rxCurlyClose;
240  static QString rxDegree;
241  static QString escDegree;
242  static QString rxPlusMinus;
243  static QString escPlusMinus;
244  static QString rxDiameter;
245  static QString escDiameter;
246  static QString rxUnderlined;
247  static QString rxUnicode;
248 
249  static QString rxAll;
250 
251  static QChar chDegree;
252  static QChar chPlusMinus;
253  static QChar chDiameter;
254 
255 private:
257 
259 
260  // painter paths used to draw text geometry as painter path:
261  QList<RPainterPath> painterPaths;
262 
263  // text layouts and transformations used to draw text as QTextLayout instead of painter paths:
264  QList<RTextLayout> textLayouts;
265  // list of block transforms of all QTextLayout blocks of the current line:
266  QList<QTransform> lineBlockTransforms;
267 
269  double height;
270  double width;
271  bool draft;
272  QString richText;
274 
275  QStack<bool> useCadFont;
276  QStack<QTextCharFormat> currentFormat;
277  QStack<double> blockHeight;
278  QStack<QString> blockFont;
279  QStack<QString> blockFontFile;
280  QStack<bool> blockBold;
281  QStack<bool> blockItalic;
282  QStack<QStringList> openTags;
283 };
284 
285 
289 
290 #endif
Represents a box e.g.
Definition: RBox.h:43
static QString rxDiameter
Definition: RTextRenderer.h:244
QList< RTextLayout > getTextLayouts() const
Definition: RTextRenderer.h:61
QString getRichText() const
Definition: RTextRenderer.h:81
static QString rxAlignmentCenter
Definition: RTextRenderer.h:215
bool getUseCadFont() const
Definition: RTextRenderer.h:153
static QString rxAll
Definition: RTextRenderer.h:249
static QString rxCurlyOpen
Definition: RTextRenderer.h:238
static QString rxBeginBlock
Definition: RTextRenderer.h:235
static QString rxLineFeed
Definition: RTextRenderer.h:213
QStack< QString > blockFont
Definition: RTextRenderer.h:278
static QChar chDiameter
Definition: RTextRenderer.h:253
static QString escDegree
Definition: RTextRenderer.h:241
QStack< QStringList > openTags
Definition: RTextRenderer.h:282
static QString rxColorChangeCustom
Definition: RTextRenderer.h:223
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:218
double getWidth() const
Definition: RTextRenderer.h:77
static QString rxObliqueAngleChange
Definition: RTextRenderer.h:230
static QString rxEndBlock
Definition: RTextRenderer.h:236
RBox getBoundingBox() const
Definition: RTextRenderer.h:69
void setCurrentFormat(QTextCharFormat f)
Definition: RTextRenderer.h:199
double fontHeightFactor
Definition: RTextRenderer.h:273
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RTextRenderer.h:43
void setBlockFontFile(const QString &f)
Definition: RTextRenderer.h:134
bool draft
Definition: RTextRenderer.h:271
static QString rxUnderlined
Definition: RTextRenderer.h:246
QStack< QTextCharFormat > currentFormat
Definition: RTextRenderer.h:276
Stores and manages all data that defines the geometry and appearance of a text based entity (text...
Definition: RTextBasedData.h:43
Color.
Definition: RColor.h:42
static QString rxTrackChange
Definition: RTextRenderer.h:231
static QChar chDegree
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RTextRenderer.h:251
static QString rxStackedText
Definition: RTextRenderer.h:221
void setBlockHeight(double h)
Definition: RTextRenderer.h:186
const RTextBasedData & textData
Definition: RTextRenderer.h:256
static QString rxParagraphFeed
Definition: RTextRenderer.h:217
void setBlockFont(const QString &f)
Definition: RTextRenderer.h:121
static QString escPlusMinus
Definition: RTextRenderer.h:243
static QString rxAlignmentRight
Definition: RTextRenderer.h:216
RBox boundingBox
Definition: RTextRenderer.h:268
Q_DECLARE_METATYPE(RMath *)
static QString rxHeightChange
Definition: RTextRenderer.h:219
QString richText
Definition: RTextRenderer.h:272
QList< RPainterPath > painterPaths
Definition: RTextRenderer.h:261
static QString rxDegree
Definition: RTextRenderer.h:240
static QString rxOverlineOff
Definition: RTextRenderer.h:226
static QString rxBackslash
Definition: RTextRenderer.h:237
Extended painter path with a z-level and a pen.
Definition: RPainterPath.h:46
static QChar chPlusMinus
Definition: RTextRenderer.h:252
static QString rxUnderlineOff
Definition: RTextRenderer.h:228
static QString rxColorChangeIndex
Definition: RTextRenderer.h:222
QStack< bool > blockItalic
Definition: RTextRenderer.h:281
void setUseCadFont(bool on)
Definition: RTextRenderer.h:147
QStack< bool > useCadFont
Definition: RTextRenderer.h:275
QStack< bool > blockBold
Definition: RTextRenderer.h:280
QStack< double > blockHeight
Definition: RTextRenderer.h:277
static QString rxUnderlineOn
Definition: RTextRenderer.h:227
QString getBlockFont() const
Definition: RTextRenderer.h:127
static QString rxWidthChange
Definition: RTextRenderer.h:229
QStack< QString > blockFontFile
Definition: RTextRenderer.h:279
QTextCharFormat getCurrentFormat() const
Definition: RTextRenderer.h:205
static QString rxFontChangeCad
Definition: RTextRenderer.h:233
QString getBlockFontFile() const
Definition: RTextRenderer.h:140
static QString rxFontChangeTtf
Definition: RTextRenderer.h:234
static QString rxAlignmentLeft
Definition: RTextRenderer.h:214
QList< QTransform > lineBlockTransforms
Definition: RTextRenderer.h:266
double height
Definition: RTextRenderer.h:269
static QString rxOverlineOn
Definition: RTextRenderer.h:225
double getBlockHeight() const
Definition: RTextRenderer.h:192
static QString rxCurlyClose
Definition: RTextRenderer.h:239
void setBlockBold(bool on)
Definition: RTextRenderer.h:160
static QString rxPlusMinus
Definition: RTextRenderer.h:242
static QString rxUnicode
Definition: RTextRenderer.h:247
Target target
Definition: RTextRenderer.h:258
static QString rxAlignmentChange
Definition: RTextRenderer.h:232
double getHeight() const
Definition: RTextRenderer.h:73
static QString escDiameter
Definition: RTextRenderer.h:245
bool getBlockBold() const
Definition: RTextRenderer.h:166
double width
Definition: RTextRenderer.h:270
bool getBlockItalic() const
Definition: RTextRenderer.h:179
QList< RTextLayout > textLayouts
Definition: RTextRenderer.h:264
static QString rxNonBreakingSpace
Definition: RTextRenderer.h:224
void setBlockItalic(bool on)
Definition: RTextRenderer.h:173