1 /*
2  * FTGL - OpenGL font library
3  *
4  * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
5  * Copyright (c) 2008 Sam Hocevar <sam@hocevar.net>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #ifndef __FTSimpleLayoutImpl__
28 #define __FTSimpleLayoutImpl__
29 
30 #include "FTLayoutImpl.h"
31 
32 
33 class FTFont;
34 
35 class FTSimpleLayoutImpl : public FTLayoutImpl
36 {
37     friend class FTSimpleLayout;
38 
39     protected:
40         FTSimpleLayoutImpl();
41 
~FTSimpleLayoutImpl()42         virtual ~FTSimpleLayoutImpl() {};
43 
44         virtual FTBBox BBox(const char* string, const int len,
45                             FTPoint position);
46 
47         virtual FTBBox BBox(const wchar_t* string, const int len,
48                             FTPoint position);
49 
50         virtual void Render(const char *string, const int len,
51                             FTPoint position, int renderMode);
52 
53         virtual void Render(const wchar_t *string, const int len,
54                             FTPoint position, int renderMode);
55 
56         /**
57          * Render a string of characters and distribute extra space amongst
58          * the whitespace regions of the string.
59          *
60          * @param string   A buffer of wchar_t characters to output.
61          * @param len  The length of the string. If < 0 then all characters
62          *             will be displayed until a null character is encountered.
63          * @param position TODO
64          * @param renderMode Render mode to display
65          * @param extraSpace The amount of extra space to distribute amongst
66          *                   the characters.
67          */
68         virtual void RenderSpace(const char *string, const int len,
69                                  FTPoint position, int renderMode,
70                                  const float extraSpace);
71 
72         /**
73          * Render a string of characters and distribute extra space amongst
74          * the whitespace regions of the string.
75          *
76          * @param string   A buffer of wchar_t characters to output.
77          * @param len  The length of the string. If < 0 then all characters
78          *             will be displayed until a null character is encountered.
79          * @param position TODO
80          * @param renderMode Render mode to display
81          * @param extraSpace The amount of extra space to distribute amongst
82          *                   the characters.
83          */
84         virtual void RenderSpace(const wchar_t *string, const int len,
85                                  FTPoint position, int renderMode,
86                                  const float extraSpace);
87 
88     private:
89         /**
90          * Either render a string of characters and wrap lines
91          * longer than a threshold or compute the bounds
92          * of a string of characters when wrapped.  The functionality
93          * of this method is exposed by the BBoxWrapped and
94          * RenderWrapped methods.
95          *
96          * @param buf  A char string to output.
97          * @param len  The length of the string. If < 0 then all characters
98          *             will be displayed until a null character is encountered.
99          * @param position TODO
100          * @param renderMode  Render mode to display
101          * @param bounds      A pointer to a bounds object.  If non null
102          *                    the bounds of the text when laid out
103          *                    will be stored in bounds.  If null the
104          *                    text will be rendered.
105          */
106         virtual void WrapText(const char *buf, const int len,
107                               FTPoint position, int renderMode,
108                               FTBBox *bounds);
109 
110         /**
111          * Either render a string of characters and wrap lines
112          * longer than a threshold or compute the bounds
113          * of a string of characters when wrapped.  The functionality
114          * of this method is exposed by the BBoxWrapped and
115          * RenderWrapped methods.
116          *
117          * @param buf  A wchar_t style string to output.
118          * @param len  The length of the string. If < 0 then all characters
119          *             will be displayed until a null character is encountered.
120          * @param position TODO
121          * @param renderMode  Render mode to display
122          * @param bounds      A pointer to a bounds object.  If non null
123          *                    the bounds of the text when laid out
124          *                    will be stored in bounds.  If null the
125          *                    text will be rendered.
126          */
127         virtual void WrapText(const wchar_t *buf, const int len,
128                               FTPoint position, int renderMode,
129                               FTBBox *bounds);
130 
131         /**
132          * A helper method used by WrapText to either output the text or
133          * compute it's bounds.
134          *
135          * @param buf      A pointer to an array of character data.
136          * @param len  The length of the string. If < 0 then all characters
137          *             will be displayed until a null character is encountered.
138          * @param position TODO
139          * @param renderMode  Render mode to display
140          * @param RemainingWidth The amount of extra space left on the line.
141          * @param bounds     A pointer to a bounds object.  If non null the
142          *                   bounds will be initialized or expanded by the
143          *                   bounds of the line.  If null the text will be
144          *                   rendered.  If the bounds are invalid (lower > upper)
145          *                   they will be initialized.  Otherwise they
146          *                   will be expanded.
147          */
148         void OutputWrapped(const char *buf, const int len,
149                            FTPoint position, int renderMode,
150                            const float RemainingWidth, FTBBox *bounds);
151 
152         /**
153          * A helper method used by WrapText to either output the text or
154          * compute it's bounds.
155          *
156          * @param buf      A pointer to an array of character data.
157          * @param len  The length of the string. If < 0 then all characters
158          *             will be displayed until a null character is encountered.
159          * @param position TODO
160          * @param renderMode  Render mode to display
161          * @param RemainingWidth The amount of extra space left on the line.
162          * @param bounds     A pointer to a bounds object.  If non null the
163          *                   bounds will be initialized or expanded by the
164          *                   bounds of the line.  If null the text will be
165          *                   rendered.  If the bounds are invalid (lower > upper)
166          *                   they will be initialized.  Otherwise they
167          *                   will be expanded.
168          */
169         void OutputWrapped(const wchar_t *buf, const int len,
170                            FTPoint position, int renderMode,
171                            const float RemainingWidth, FTBBox *bounds);
172 
173         /**
174          * The font to use for rendering the text.  The font is
175          * referenced by this but will not be disposed of when this
176          * is deleted.
177          */
178         FTFont *currentFont;
179 
180         /**
181          * The maximum line length for formatting text.
182          */
183         float lineLength;
184 
185         /**
186          * The text alignment mode used to distribute
187          * space within a line or rendered text.
188          */
189         FTGL::TextAlignment alignment;
190 
191         /**
192          * The height of each line of text expressed as
193          * a percentage of the font's line height.
194          */
195         float lineSpacing;
196 
197         /* Internal generic BBox() implementation */
198         template <typename T>
199         inline FTBBox BBoxI(const T* string, const int len, FTPoint position);
200 
201         /* Internal generic Render() implementation */
202         template <typename T>
203         inline void RenderI(const T* string, const int len,
204                             FTPoint position, int renderMode);
205 
206         /* Internal generic RenderSpace() implementation */
207         template <typename T>
208         inline void RenderSpaceI(const T* string, const int len,
209                                  FTPoint position, int renderMode,
210                                  const float extraSpace);
211 
212         /* Internal generic WrapText() implementation */
213         template <typename T>
214         void WrapTextI(const T* buf, const int len, FTPoint position,
215                        int renderMode, FTBBox *bounds);
216 
217         /* Internal generic OutputWrapped() implementation */
218         template <typename T>
219         void OutputWrappedI(const T* buf, const int len, FTPoint position,
220                             int renderMode, const float RemainingWidth,
221                             FTBBox *bounds);
222 };
223 
224 #endif  //  __FTSimpleLayoutImpl__
225 
226