1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 
28 #ifndef RS_MTEXT_H
29 #define RS_MTEXT_H
30 
31 #include "rs_entitycontainer.h"
32 
33 /**
34  * Holds the data that defines a text entity.
35  */
36 struct RS_MTextData {
37 	/**
38 	 * Vertical alignments.
39 	 */
40 	enum VAlign {
41 		VATop,      /**< Top. */
42 		VAMiddle,   /**< Middle */
43 		VABottom    /**< Bottom */
44 	};
45 
46 	/**
47 	 * Horizontal alignments.
48 	 */
49 	enum HAlign {
50 		HALeft,     /**< Left */
51 		HACenter,   /**< Centered */
52 		HARight     /**< Right */
53 	};
54 
55 	/**
56 	 * MText drawing direction.
57 	 */
58 	enum MTextDrawingDirection {
59 		LeftToRight,     /**< Left to right */
60 		TopToBottom,     /**< Top to bottom */
61 		ByStyle          /**< Inherited from associated text style */
62 	};
63 
64 	/**
65 	 * Line spacing style for MTexts.
66 	 */
67 	enum MTextLineSpacingStyle {
68 		AtLeast,        /**< Taller characters will override */
69 		Exact           /**< Taller characters will not override */
70 	};
71 
72 	/**
73 	 * Default constructor. Leaves the data object uninitialized.
74 	 */
75 	RS_MTextData() = default;
76 
77 	/**
78 	 * Constructor with initialisation.
79 	 *
80 	 * @param insertionPoint Insertion point
81 	 * @param height Nominal (initial) text height
82 	 * @param width Reference rectangle width
83 	 * @param valign Vertical alignment
84 	 * @param halign Horizontal alignment
85 	 * @param drawingDirection Drawing direction
86 	 * @param lineSpacingStyle Line spacing style
87 	 * @param lineSpacingFactor Line spacing factor
88 	 * @param text Text string
89 	 * @param style Text style name
90 	 * @param angle Rotation angle
91 	 * @param updateMode RS2::Update will update the text entity instantly
92 	 *    RS2::NoUpdate will not update the entity. You can update
93 	 *    it later manually using the update() method. This is
94 	 *    often the case since you might want to adjust attributes
95 	 *    after creating a text entity.
96 	 */
97 	RS_MTextData(const RS_Vector& insertionPoint,
98 				 double height,
99 				 double width,
100 				 VAlign valign,
101 				 HAlign halign,
102 				 MTextDrawingDirection drawingDirection,
103 				 MTextLineSpacingStyle lineSpacingStyle,
104 				 double lineSpacingFactor,
105 				 const QString& text,
106 				 const QString& style,
107 				 double angle,
108 				 RS2::UpdateMode updateMode = RS2::Update);
109 
110 	/** Insertion point */
111 	RS_Vector insertionPoint;
112 	/** Nominal (initial) text height */
113 	double height;
114 	/** Reference rectangle width */
115 	double width;
116 	/** Vertical alignment */
117 	VAlign valign;
118 	/** Horizontal alignment */
119 	HAlign halign;
120 	/** Drawing direction */
121 	MTextDrawingDirection drawingDirection;
122 	/** Line spacing style */
123 	MTextLineSpacingStyle lineSpacingStyle;
124 	/** Line spacing factor */
125 	double lineSpacingFactor;
126 	/** Text string */
127 	QString text;
128 	/** Text style name */
129 	QString style;
130 	/** Rotation angle */
131 	double angle;
132 	/** Update mode */
133 	RS2::UpdateMode updateMode;
134 };
135 
136 std::ostream& operator << (std::ostream& os, const RS_MTextData& td);
137 
138 
139 /**
140  * Class for a text entity.
141  * Please note that text strings can contain special
142  * characters such as %%c for a diameter sign as well as unicode
143  * characters. Line feeds are stored as real line feeds in the string.
144  *
145  * @author Andrew Mustun
146  */
147 class RS_MText : public RS_EntityContainer {
148 public:
149     RS_MText(RS_EntityContainer* parent,
150             const RS_MTextData& d);
151 	virtual ~RS_MText() = default;
152 
153     virtual RS_Entity* clone() const override;
154 
155     /**	@return RS2::EntityText */
rtti()156     virtual RS2::EntityType rtti() const override{
157         return RS2::EntityMText;
158     }
159 
160     /** @return Copy of data that defines the text. */
getData()161     RS_MTextData getData() const {
162         return data;
163     }
164 
165     void update() override;
166 
167     int getNumberOfLines();
168 
169 
getInsertionPoint()170     RS_Vector getInsertionPoint() {
171         return data.insertionPoint;
172     }
getHeight()173     double getHeight() {
174         return data.height;
175     }
setHeight(double h)176     void setHeight(double h) {
177         data.height = h;
178     }
getWidth()179     double getWidth() {
180         return data.width;
181     }
182     void setAlignment(int a);
183     int getAlignment();
getVAlign()184     RS_MTextData::VAlign getVAlign() {
185         return data.valign;
186     }
setVAlign(RS_MTextData::VAlign va)187     void setVAlign(RS_MTextData::VAlign va) {
188         data.valign = va;
189     }
getHAlign()190     RS_MTextData::HAlign getHAlign() {
191         return data.halign;
192     }
setHAlign(RS_MTextData::HAlign ha)193     void setHAlign(RS_MTextData::HAlign ha) {
194         data.halign = ha;
195     }
getDrawingDirection()196     RS_MTextData::MTextDrawingDirection getDrawingDirection() {
197         return data.drawingDirection;
198     }
getLineSpacingStyle()199     RS_MTextData::MTextLineSpacingStyle getLineSpacingStyle() {
200         return data.lineSpacingStyle;
201     }
setLineSpacingFactor(double f)202     void setLineSpacingFactor(double f) {
203         data.lineSpacingFactor = f;
204     }
getLineSpacingFactor()205     double getLineSpacingFactor() {
206         return data.lineSpacingFactor;
207     }
208     void setText(const QString& t);
getText()209     QString getText() {
210         return data.text;
211     }
setStyle(const QString & s)212     void setStyle(const QString& s) {
213         data.style = s;
214     }
getStyle()215     QString getStyle() {
216         return data.style;
217     }
setAngle(double a)218     void setAngle(double a) {
219         data.angle = a;
220     }
getAngle()221     double getAngle() {
222         return data.angle;
223     }
getUsedTextWidth()224     double getUsedTextWidth() {
225         return usedTextWidth;
226     }
getUsedTextHeight()227     double getUsedTextHeight() {
228         return usedTextHeight;
229     }
230 
231 //	virtual double getLength() const {
232 //		return -1.0;
233 //	}
234 
235     /**
236      * @return The insertion point as endpoint.
237      */
238     virtual RS_Vector getNearestEndpoint(const RS_Vector& coord,
239                                          double* dist = NULL)const override;
240     virtual RS_VectorSolutions getRefPoints() const override;
241 
242     virtual void move(const RS_Vector& offset) override;
243     virtual void rotate(const RS_Vector& center, const double& angle) override;
244     virtual void rotate(const RS_Vector& center, const RS_Vector& angleVector) override;
245     virtual void scale(const RS_Vector& center, const RS_Vector& factor) override;
246     virtual void mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) override;
247     virtual bool hasEndpointsWithinWindow(const RS_Vector& v1, const RS_Vector& v2) override;
248     virtual void stretch(const RS_Vector& firstCorner,
249                          const RS_Vector& secondCorner,
250                          const RS_Vector& offset) override;
251 
252     friend std::ostream& operator << (std::ostream& os, const RS_Text& p);
253 
254     void draw(RS_Painter* painter, RS_GraphicView* view, double& patternOffset) override;
255 
256 private:
257     double updateAddLine(RS_EntityContainer* textLine, int lineCounter);
258 
259 protected:
260     RS_MTextData data;
261 
262     /**
263      * Text width used by the current contents of this text entity.
264      * This property is updated by the update method.
265      * @see update
266      */
267     double usedTextWidth;
268     /**
269      * Text height used by the current contents of this text entity.
270      * This property is updated by the update method.
271      * @see update
272      */
273     double usedTextHeight;
274 };
275 
276 #endif
277