1 // -*- C++ -*-
2 // --------------------------------------------------------------------
3 // The text object.
4 // --------------------------------------------------------------------
5 /*
6 
7     This file is part of the extensible drawing editor Ipe.
8     Copyright (c) 1993-2020 Otfried Cheong
9 
10     Ipe is free software; you can redistribute it and/or modify it
11     under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 3 of the License, or
13     (at your option) any later version.
14 
15     As a special exception, you have permission to link Ipe with the
16     CGAL library and distribute executables, as long as you follow the
17     requirements of the Gnu General Public License in regard to all of
18     the software in the executable aside from CGAL.
19 
20     Ipe is distributed in the hope that it will be useful, but WITHOUT
21     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
23     License for more details.
24 
25     You should have received a copy of the GNU General Public License
26     along with Ipe; if not, you can find it at
27     "http://www.gnu.org/copyleft/gpl.html", or write to the Free
28     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 
30 */
31 
32 #ifndef IPETEXT_H
33 #define IPETEXT_H
34 
35 #include "ipeobject.h"
36 
37 // --------------------------------------------------------------------
38 
39 namespace ipe {
40 
41   class Text : public Object {
42   public:
43     enum TextType { ELabel, EMinipage };
44 
45     explicit Text();
46     explicit Text(const AllAttributes &attr, String data,
47 		  const Vector &pos, TextType type, double width = 10.0);
48     Text(const Text &rhs);
49     ~Text();
50 
51     explicit Text(const XmlAttributes &attr, String data);
52 
53     virtual Object *clone() const;
54 
55     virtual Text *asText();
56 
57     virtual Type type() const;
58 
59     virtual void saveAsXml(Stream &stream, String layer) const;
60     virtual void draw(Painter &painter) const;
61     virtual void drawSimple(Painter &painter) const;
62 
63     virtual void accept(Visitor &visitor) const;
64 
65     virtual void addToBBox(Rect &box, const Matrix &m, bool) const;
66     virtual double distance(const Vector &v, const Matrix &m,
67 			    double bound) const;
68     virtual void snapCtl(const Vector &mouse, const Matrix &m,
69 			 Vector &pos, double &bound) const;
70 
71     virtual void checkStyle(const Cascade *sheet, AttributeSeq &seq) const;
72 
73     virtual bool setAttribute(Property prop, Attribute value);
74     virtual Attribute getAttribute(Property prop) const noexcept;
75 
76   private:
77     void quadrilateral(const Matrix &m, Vector v[4]) const;
78   public:
79     Vector align() const;
80 
81     TextType textType() const;
82     inline Vector position() const;
83     inline String text() const;
84     void setStroke(Attribute stroke);
85     void setOpacity(Attribute opaq);
86 
87     inline Attribute stroke() const;
88     inline Attribute size() const;
89     inline Attribute style() const;
90     //! Return opacity of the opject.
opacity()91     inline Attribute opacity() const { return iOpacity; }
92 
93     inline bool isMinipage() const;
94     void setTextType(TextType type);
95 
96     inline THorizontalAlignment horizontalAlignment() const;
97     inline TVerticalAlignment verticalAlignment() const;
98     void setHorizontalAlignment(THorizontalAlignment align);
99     void setVerticalAlignment(TVerticalAlignment align);
100 
101     static TVerticalAlignment makeVAlign(String str,
102 					 TVerticalAlignment def);
103     static THorizontalAlignment makeHAlign(String str,
104 					   THorizontalAlignment def);
105     static void saveAlignment(Stream &stream, THorizontalAlignment h,
106 			      TVerticalAlignment v);
107 
108     inline double width() const;
109     inline double height() const;
110     inline double depth() const;
111     inline double totalHeight() const;
112 
113     void setSize(Attribute size);
114     void setStyle(Attribute style);
115     void setWidth(double width);
116     void setText(String text);
117 
118     struct XForm {
119       int iRefCount;
120       Rect iBBox;
121       int iDepth;
122       float iStretch;
123       String iName;
124       Vector iTranslation;
125     };
126 
127     void setXForm(XForm *xform) const;
128     inline const XForm *getXForm() const;
129 
130   private:
131     Vector iPos;
132     String iText;
133     Attribute iStroke;
134     Attribute iSize;
135     Attribute iStyle;
136     Attribute iOpacity;
137     mutable double iWidth;
138     mutable double iHeight;
139     mutable double iDepth;
140     TextType iType;
141     THorizontalAlignment iHorizontalAlignment;
142     TVerticalAlignment iVerticalAlignment;
143     mutable XForm *iXForm; // reference counted
144   };
145 
146   // --------------------------------------------------------------------
147 
148   //! Return text position.
position()149   inline Vector Text::position() const
150   {
151     return iPos;
152   }
153 
154   //! Return text source.
text()155   inline String Text::text() const
156   {
157     return iText;
158   }
159 
160   //! Return stroke color.
stroke()161   inline Attribute Text::stroke() const
162   {
163     return iStroke;
164   }
165 
166   //! Return font size.
size()167   inline Attribute Text::size() const
168   {
169     return iSize;
170   }
171 
172   //! Return Latex style of text object.
style()173   inline Attribute Text::style() const
174   {
175     return iStyle;
176   }
177 
178   //! Return width of text object.
width()179   inline double Text::width() const
180   {
181     return iWidth;
182   }
183 
184   //! Return height of text object (from baseline to top).
height()185   inline double Text::height() const
186   {
187     return iHeight;
188   }
189 
190   //! Return depth of text object.
depth()191   inline double Text::depth() const
192   {
193     return iDepth;
194   }
195 
196   //! Return height + depth of text object.
totalHeight()197   inline double Text::totalHeight() const
198   {
199     return iHeight + iDepth;
200   }
201 
202   //! Return true if text object is formatted as a minipage.
203   /*! Equivalent to type being EMinipage. */
isMinipage()204   inline bool Text::isMinipage() const
205   {
206     return (iType == EMinipage);
207   }
208 
209   //! Return horizontal alignment of text object.
horizontalAlignment()210   inline THorizontalAlignment Text::horizontalAlignment() const
211   {
212     return iHorizontalAlignment;
213   }
214 
215   //! Return vertical alignment of text object.
verticalAlignment()216   inline TVerticalAlignment Text::verticalAlignment() const
217   {
218     return iVerticalAlignment;
219   }
220 
221   //! Return the PDF representation of this text object.
222   /*! If Pdflatex has not been run yet, returns 0. */
getXForm()223   inline const Text::XForm *Text::getXForm() const
224   {
225     return iXForm;
226   }
227 
228 } // namespace
229 
230 // --------------------------------------------------------------------
231 #endif
232