~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/lib/engine/rs_mtext.h

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
class RS_MTextData {
 
37
public:
 
38
    /**
 
39
     * Vertical alignments.
 
40
     */
 
41
    enum VAlign {
 
42
        VATop,      /**< Top. */
 
43
        VAMiddle,   /**< Middle */
 
44
        VABottom    /**< Bottom */
 
45
    };
 
46
 
 
47
    /**
 
48
     * Horizontal alignments.
 
49
     */
 
50
    enum HAlign {
 
51
        HALeft,     /**< Left */
 
52
        HACenter,   /**< Centered */
 
53
        HARight     /**< Right */
 
54
    };
 
55
 
 
56
    /**
 
57
     * MText drawing direction.
 
58
     */
 
59
    enum MTextDrawingDirection {
 
60
        LeftToRight,     /**< Left to right */
 
61
        TopToBottom,     /**< Top to bottom */
 
62
        ByStyle          /**< Inherited from associated text style */
 
63
    };
 
64
 
 
65
    /**
 
66
     * Line spacing style for MTexts.
 
67
     */
 
68
    enum MTextLineSpacingStyle {
 
69
        AtLeast,        /**< Taller characters will override */
 
70
        Exact           /**< Taller characters will not override */
 
71
    };
 
72
 
 
73
    /**
 
74
     * Default constructor. Leaves the data object uninitialized.
 
75
     */
 
76
    RS_MTextData() {}
 
77
 
 
78
    /**
 
79
     * Constructor with initialisation.
 
80
     *
 
81
     * @param insertionPoint Insertion point
 
82
     * @param height Nominal (initial) text height
 
83
     * @param width Reference rectangle width
 
84
     * @param valign Vertical alignment
 
85
     * @param halign Horizontal alignment
 
86
     * @param drawingDirection Drawing direction
 
87
     * @param lineSpacingStyle Line spacing style
 
88
     * @param lineSpacingFactor Line spacing factor
 
89
     * @param text Text string
 
90
     * @param style Text style name
 
91
     * @param angle Rotation angle
 
92
     * @param updateMode RS2::Update will update the text entity instantly
 
93
     *    RS2::NoUpdate will not update the entity. You can update
 
94
     *    it later manually using the update() method. This is
 
95
     *    often the case since you might want to adjust attributes
 
96
     *    after creating a text entity.
 
97
     */
 
98
    RS_MTextData(const RS_Vector& insertionPoint,
 
99
                double height,
 
100
                double width,
 
101
                VAlign valign,
 
102
                HAlign halign,
 
103
                MTextDrawingDirection drawingDirection,
 
104
                MTextLineSpacingStyle lineSpacingStyle,
 
105
                double lineSpacingFactor,
 
106
                const QString& text,
 
107
                const QString& style,
 
108
                double angle,
 
109
                RS2::UpdateMode updateMode = RS2::Update) {
 
110
        this->insertionPoint = insertionPoint;
 
111
        this->height = height;
 
112
        this->width = width;
 
113
        this->valign = valign;
 
114
        this->halign = halign;
 
115
        this->drawingDirection = drawingDirection;
 
116
        this->lineSpacingStyle = lineSpacingStyle;
 
117
        this->lineSpacingFactor = lineSpacingFactor;
 
118
        this->style = style;
 
119
        this->angle = angle;
 
120
        this->text = text;
 
121
        this->updateMode = updateMode;
 
122
    }
 
123
 
 
124
    friend class RS_MText;
 
125
 
 
126
    friend std::ostream& operator << (std::ostream& os, const RS_MTextData& td) {
 
127
        os << "(" << td.text.toLatin1().data() << ")";
 
128
        return os;
 
129
    }
 
130
 
 
131
public:
 
132
    /** Insertion point */
 
133
    RS_Vector insertionPoint;
 
134
    /** Nominal (initial) text height */
 
135
    double height;
 
136
    /** Reference rectangle width */
 
137
    double width;
 
138
    /** Vertical alignment */
 
139
    VAlign valign;
 
140
    /** Horizontal alignment */
 
141
    HAlign halign;
 
142
    /** Drawing direction */
 
143
    MTextDrawingDirection drawingDirection;
 
144
    /** Line spacing style */
 
145
    MTextLineSpacingStyle lineSpacingStyle;
 
146
    /** Line spacing factor */
 
147
    double lineSpacingFactor;
 
148
    /** Text string */
 
149
    QString text;
 
150
    /** Text style name */
 
151
    QString style;
 
152
    /** Rotation angle */
 
153
    double angle;
 
154
    /** Update mode */
 
155
    RS2::UpdateMode updateMode;
 
156
};
 
157
 
 
158
 
 
159
 
 
160
/**
 
161
 * Class for a text entity.
 
162
 * Please note that text strings can contain special
 
163
 * characters such as %%c for a diameter sign as well as unicode
 
164
 * characters. Line feeds are stored as real line feeds in the string.
 
165
 *
 
166
 * @author Andrew Mustun
 
167
 */
 
168
class RS_MText : public RS_EntityContainer {
 
169
public:
 
170
    RS_MText(RS_EntityContainer* parent,
 
171
            const RS_MTextData& d);
 
172
    virtual ~RS_MText() {}
 
173
 
 
174
    virtual RS_Entity* clone() {
 
175
        RS_MText* t = new RS_MText(*this);
 
176
        t->setOwner(isOwner());
 
177
        t->initId();
 
178
        t->detach();
 
179
        return t;
 
180
    }
 
181
 
 
182
    /** @return RS2::EntityText */
 
183
    virtual RS2::EntityType rtti() const {
 
184
        return RS2::EntityMText;
 
185
    }
 
186
 
 
187
    /** @return Copy of data that defines the text. */
 
188
    RS_MTextData getData() const {
 
189
        return data;
 
190
    }
 
191
 
 
192
    void update();
 
193
 
 
194
    int getNumberOfLines();
 
195
 
 
196
 
 
197
    RS_Vector getInsertionPoint() {
 
198
        return data.insertionPoint;
 
199
    }
 
200
    double getHeight() {
 
201
        return data.height;
 
202
    }
 
203
    void setHeight(double h) {
 
204
        data.height = h;
 
205
    }
 
206
    double getWidth() {
 
207
        return data.width;
 
208
    }
 
209
    void setAlignment(int a);
 
210
    int getAlignment();
 
211
    RS_MTextData::VAlign getVAlign() {
 
212
        return data.valign;
 
213
    }
 
214
    void setVAlign(RS_MTextData::VAlign va) {
 
215
        data.valign = va;
 
216
    }
 
217
    RS_MTextData::HAlign getHAlign() {
 
218
        return data.halign;
 
219
    }
 
220
    void setHAlign(RS_MTextData::HAlign ha) {
 
221
        data.halign = ha;
 
222
    }
 
223
    RS_MTextData::MTextDrawingDirection getDrawingDirection() {
 
224
        return data.drawingDirection;
 
225
    }
 
226
    RS_MTextData::MTextLineSpacingStyle getLineSpacingStyle() {
 
227
        return data.lineSpacingStyle;
 
228
    }
 
229
    void setLineSpacingFactor(double f) {
 
230
        data.lineSpacingFactor = f;
 
231
    }
 
232
    double getLineSpacingFactor() {
 
233
        return data.lineSpacingFactor;
 
234
    }
 
235
    void setText(const QString& t);
 
236
    QString getText() {
 
237
        return data.text;
 
238
    }
 
239
    void setStyle(const QString& s) {
 
240
        data.style = s;
 
241
    }
 
242
    QString getStyle() {
 
243
        return data.style;
 
244
    }
 
245
    void setAngle(double a) {
 
246
        data.angle = a;
 
247
    }
 
248
    double getAngle() {
 
249
        return data.angle;
 
250
    }
 
251
    double getUsedTextWidth() {
 
252
        return usedTextWidth;
 
253
    }
 
254
    double getUsedTextHeight() {
 
255
        return usedTextHeight;
 
256
    }
 
257
 
 
258
//      virtual double getLength() const {
 
259
//              return -1.0;
 
260
//      }
 
261
 
 
262
    /**
 
263
     * @return The insertion point as endpoint.
 
264
     */
 
265
    virtual RS_Vector getNearestEndpoint(const RS_Vector& coord,
 
266
                                         double* dist = NULL)const;
 
267
    virtual RS_VectorSolutions getRefPoints();
 
268
    virtual RS_Vector getNearestRef(const RS_Vector& coord,
 
269
                                    double* dist = NULL);
 
270
 
 
271
    virtual void move(const RS_Vector& offset);
 
272
    virtual void rotate(const RS_Vector& center, const double& angle);
 
273
    virtual void rotate(const RS_Vector& center, const RS_Vector& angleVector);
 
274
    virtual void scale(const RS_Vector& center, const RS_Vector& factor);
 
275
    virtual void mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2);
 
276
    virtual bool hasEndpointsWithinWindow(const RS_Vector& v1, const RS_Vector& v2);
 
277
    virtual void stretch(const RS_Vector& firstCorner,
 
278
                         const RS_Vector& secondCorner,
 
279
                         const RS_Vector& offset);
 
280
 
 
281
    friend std::ostream& operator << (std::ostream& os, const RS_Text& p);
 
282
 
 
283
private:
 
284
    double updateAddLine(RS_EntityContainer* textLine, int lineCounter);
 
285
 
 
286
protected:
 
287
    RS_MTextData data;
 
288
 
 
289
    /**
 
290
     * Text width used by the current contents of this text entity.
 
291
     * This property is updated by the update method.
 
292
     * @see update
 
293
     */
 
294
    double usedTextWidth;
 
295
    /**
 
296
     * Text height used by the current contents of this text entity.
 
297
     * This property is updated by the update method.
 
298
     * @see update
 
299
     */
 
300
    double usedTextHeight;
 
301
};
 
302
 
 
303
#endif