1 /***********************************************************************
2     created:    Sun Jun 19 2005
3     author:     Paul D Turner <paul@cegui.org.uk>
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2012 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUIFalTextComponent_h_
28 #define _CEGUIFalTextComponent_h_
29 
30 #include "./ComponentBase.h"
31 #include "../RenderedString.h"
32 #include "../RefCounted.h"
33 #include "../FormattedRenderedString.h"
34 #include "CEGUI/falagard/FormattingSetting.h"
35 
36 #if defined(_MSC_VER)
37 #  pragma warning(push)
38 #  pragma warning(disable : 4251)
39 #endif
40 
41 // Start of CEGUI namespace section
42 namespace CEGUI
43 {
44     /*!
45     \brief
46         Class that encapsulates information for a text component.
47     */
48     class CEGUIEXPORT TextComponent : public FalagardComponentBase
49     {
50     public:
51         TextComponent();
52         ~TextComponent();
53         TextComponent(const TextComponent& obj);
54         TextComponent& operator=(const TextComponent& other);
55 
56         /*!
57         \brief
58             Return the text set for this TextComponent.
59 
60         \note
61             This returns the text string set directly to the TextComponent,
62             which may or may not be the actual string that will be used -
63             since the actual string may be sourced from a property or the main
64             text string from a window that the TextComponent is rendered to.
65             To get the actual string, call the getEffectiveText function
66             instead.
67         */
68         const String& getText() const;
69 
70         /*
71         \brief
72             Return a copy of the actual text string that will be used when
73             rendering this TextComponent.
74         */
75         String getEffectiveText(const Window& wnd) const;
76 
77         /*!
78         \brief
79             return text string with \e visual ordering of glyphs.
80 
81         \note
82             This returns the visual text derived from the string set directly to
83             the TextComponent, which may or may not be the actual string that
84             will be used - since the actual string may be sourced from a
85             property or the main text string from a window that the
86             TextComponent is rendered to. To get the actual visual string, call
87             the getEffectiveVisualText function instead.
88         */
89         const String& getTextVisual() const;
90 
91         /*
92         \brief
93             Return a copy of the actual text - with visual ordering - that
94             will be used when rendering this TextComponent.
95         */
96         String getEffectiveVisualText(const Window& wnd) const;
97 
98         /*!
99         \brief
100             Set the text string for this TextComponent.
101 
102         \note
103             Setting this string may not set the actual string that will be used
104             when rendering the TextComponent.  The acutal string used will
105             depend upon whether a text source property is set and whether this
106             string is set to the empty string or not.
107 
108         \param text
109             String containing text to set on the TextComponent.
110         */
111         void setText(const String& text);
112 
113         /*!
114         \brief
115             Return the name of the font set to be used when rendering this
116             TextComponent.
117 
118         \note
119             This returns the name of the font set directly to the TextComponent,
120             which may or may not be the actual font that will be used -
121             since the actual font may be sourced from a property or the main
122             font setting on a window that the TextComponent is rendered to, or
123             the default font. To get the actual font name that will be used,
124             call the getEffectiveFont function instead.
125 
126         \return
127             String object containing the name of a font
128         */
129         const String& getFont() const;
130 
131         /*
132         \brief
133             Return a copy of the name of the font that will actually be used
134             when rendering this TextComponent.
135         */
136         String getEffectiveFont(const Window& wnd) const;
137 
138         /*!
139         \brief
140             Set the name of a font to be used when rendering this TextComponent.
141 
142         \note
143             Setting this may not set the actual font that will be used
144             when rendering the TextComponent.  The acutal font used will
145             depend upon whether a font source property is set and whether the
146             font name set here is set to the empty string or not.
147 
148         \param font
149             String containing name of a font
150         */
151         void setFont(const String& font);
152 
153         /*!
154         \brief
155             Return the current vertical formatting setting for this TextComponent.
156 
157         \return
158             One of the VerticalTextFormatting enumerated values.
159         */
160         VerticalTextFormatting getVerticalFormatting(const Window& wnd) const;
161 
162         /*!
163         \brief
164             Directly returns the vertical formatting which was set for the ImageryComponent.
165 
166         \return
167             The VerticalTextFormatting enum.
168         */
169         VerticalTextFormatting getVerticalFormattingFromComponent() const;
170 
171         /*!
172         \brief
173             Set the vertical formatting setting for this TextComponent.
174 
175         \param fmt
176             One of the VerticalTextFormatting enumerated values.
177 
178         \return
179             Nothing.
180         */
181         void setVerticalFormatting(VerticalTextFormatting fmt);
182 
183         /*!
184         \brief
185             Return the current horizontal formatting setting for this TextComponent.
186 
187         \return
188             One of the HorizontalTextFormatting enumerated values.
189         */
190         HorizontalTextFormatting getHorizontalFormatting(const Window& wnd) const;
191 
192         /*!
193         \brief
194             Directly returns the horizontal formatting which was set for the ImageryComponent.
195 
196         \return
197             The HorizontalTextFormatting enum.
198         */
199         HorizontalTextFormatting getHorizontalFormattingFromComponent() const;
200 
201         /*!
202         \brief
203             Set the horizontal formatting setting for this TextComponent.
204 
205         \param fmt
206             One of the HorizontalTextFormatting enumerated values.
207 
208         \return
209             Nothing.
210         */
211         void setHorizontalFormatting(HorizontalTextFormatting fmt);
212 
213         /*!
214         \brief
215             Returns the name of the property that will be used to obtain the horizontal
216             formatting to use for this ImageryComponent or an empty string if none is set.
217 
218         \return
219             A String containing the name of the property
220         */
221         const String& getHorizontalFormattingPropertySource() const;
222 
223         /*!
224         \brief
225             Set the name of a property that will be used to obtain the horizontal
226             formatting to use for this ImageryComponent.
227         */
228         void setHorizontalFormattingPropertySource(const String& property_name);
229 
230         /*!
231         \brief
232             Returns the name of the property that will be used to obtain the vertical
233             formatting to use for this ImageryComponent or an empty string if none is set.
234 
235         \return
236             A String containing the name of the property
237         */
238         const String& getVerticalFormattingPropertySource() const;
239 
240         /*!
241         \brief
242             Set the name of a property that will be used to obtain the vertical
243             formatting to use for this ImageryComponent.
244         */
245         void setVerticalFormattingPropertySource(const String& property_name);
246 
247         /*!
248         \brief
249             Writes an xml representation of this TextComponent to \a out_stream.
250 
251         \param xml_stream
252             Stream where xml data should be output.
253 
254 
255         \return
256             Nothing.
257         */
258         void writeXMLToStream(XMLSerializer& xml_stream) const;
259 
260         /*!
261         \brief
262             Return whether this TextComponent fetches it's text string via a property on the target window.
263 
264         \return
265             - true if the text string comes via a Propery.
266             - false if the text string is defined explicitly, or will come from the target window.
267         */
268         bool isTextFetchedFromProperty() const;
269 
270         /*!
271         \brief
272             Return the name of the property that will be used to determine the text string to render
273             for this TextComponent.
274 
275         \return
276             String object holding the name of a Propery.
277         */
278         const String& getTextPropertySource() const;
279 
280         /*!
281         \brief
282             Set the name of the property that will be used to determine the text string to render
283             for this TextComponent.
284 
285         \param property
286             String object holding the name of a Propery.  The property can contain any text string to render.
287 
288         \return
289             Nothing.
290         */
291         void setTextPropertySource(const String& property);
292 
293         /*!
294         \brief
295             Return whether this TextComponent fetches it's font via a property on the target window.
296 
297         \return
298             - true if the font comes via a Propery.
299             - false if the font is defined explicitly, or will come from the target window.
300         */
301         bool isFontFetchedFromProperty() const;
302 
303         /*!
304         \brief
305             Return the name of the property that will be used to determine the font to use for rendering
306             the text string for this TextComponent.
307 
308         \return
309             String object holding the name of a Propery.
310         */
311         const String& getFontPropertySource() const;
312 
313         /*!
314         \brief
315             Set the name of the property that will be used to determine the font to use for rendering
316             the text string of this TextComponent.
317 
318         \param property
319             String object holding the name of a Propery.  The property should access a valid font name.
320 
321         \return
322             Nothing.
323         */
324         void setFontPropertySource(const String& property);
325 
326         //! return the horizontal pixel extent of the formatted rendered string.
327         float getHorizontalTextExtent(const Window& window) const;
328 
329         //! return the vertical pixel extent of the formatted rendered string.
330         float getVerticalTextExtent(const Window& window) const;
331 
332         // overridden from ComponentBase.
333         bool handleFontRenderSizeChange(Window& window, const Font* font) const;
334 
335 
336         //! Update string formatting.
337         void updateFormatting(const Window& srcWindow) const;
338 
339         /*!
340         \brief
341             Update string formatting.
342 
343         \param size
344             The pixel size of the component.
345         */
346         void updateFormatting(const Window& srcWindow, const Sizef& size) const;
347 
348     protected:
349         // implemets abstract from base
350         void render_impl(Window& srcWindow, Rectf& destRect, const CEGUI::ColourRect* modColours, const Rectf* clipper, bool clipToDisplay) const;
351         //! helper to set up an appropriate FormattedRenderedString
352         void setupStringFormatter(const Window& window,
353                                   const RenderedString& rendered_string) const;
354         //! helper to get the font object to use
355         const Font* getFontObject(const Window& window) const;
356 
357     private:
358         //! text rendered by this component.
359         String d_textLogical;
360         //! pointer to bidirection support object
361         BidiVisualMapping* d_bidiVisualMapping;
362         //! whether bidi visual mapping has been updated since last text change.
363         mutable bool d_bidiDataValid;
364         //! RenderedString used when not using the one from the target Window.
365         mutable RenderedString d_renderedString;
366         //! FormattedRenderedString object that applies formatting to the string
367         mutable RefCounted<FormattedRenderedString> d_formattedRenderedString;
368         //! Tracks last used horizontal formatting (in order to detect changes)
369         mutable HorizontalTextFormatting d_lastHorzFormatting;
370 
371         String               d_font;            //!< name of font to use.
372         //! Vertical formatting to be applied when rendering the image component.
373         FormattingSetting<VerticalTextFormatting> d_vertFormatting;
374         //! Horizontal formatting to be applied when rendering the image component.
375         FormattingSetting<HorizontalTextFormatting> d_horzFormatting;
376         String  d_textPropertyName;             //!< Name of the property to access to obtain the text string to render.
377         String  d_fontPropertyName;             //!< Name of the property to access to obtain the font to use for rendering.
378     };
379 
380 } // End of  CEGUI namespace section
381 
382 #if defined(_MSC_VER)
383 #  pragma warning(pop)
384 #endif
385 
386 #endif  // end of guard _CEGUIFalTextComponent_h_
387