码迷,mamicode.com
首页 > 其他好文 > 详细

TextLayout

时间:2014-12-18 16:32:00      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   os   sp   for   on   

  1 //TextLayout.cpp
  2 
  3 /*
  4 Copyright 2000-2004 The VCF Project.
  5 Please see License.txt in the top level directory
  6 where you installed the VCF.
  7 */
  8 
  9 
 10 //Graphics4.cpp
 11 
 12 
 13 #include "vcf/ApplicationKit/ApplicationKit.h"
 14 
 15 
 16 using namespace VCF;
 17 
 18 /**
 19 Thie will show some of the various text
 20 options that are possible when drawing text
 21 */
 22 
 23 class TextLayoutWindow : public Window {
 24 public:
 25     TextLayoutWindow() {
 26         setCaption( "TextLayout" );
 27 
 28     }
 29 
 30     virtual ~TextLayoutWindow(){};
 31 
 32     virtual void paint( GraphicsContext* ctx ) {
 33         Window::paint( ctx );
 34 
 35         /**
 36         first we‘ll create 3 different fonts
 37         One with Times New Roman, in 16 point bold
 38         Another with Verdana, using the default font size, and bold
 39         And finally the third in Courier at 16 points
 40         */
 41 
 42         Font font1;
 43         font1.setName( "Times New Roman" );
 44         font1.setPointSize( 16 );
 45         font1.setBold( true );
 46 
 47         Font font2;
 48         font2.setName( "Verdana" );
 49         font2.setItalic( true );
 50 
 51         Font font3;
 52         font3.setName( "Courier" );
 53         font3.setPointSize( 16 );
 54 
 55 
 56 
 57         /**
 58         Initial x and y coordinates for the text
 59         */
 60         double x = 20;
 61         double y = 20;
 62 
 63         /**
 64         To change the GraphicsContext‘s font, make a call to
 65         GraphicsContext::setCurrentFont, passing in a pointer to
 66         the font. The passed in font will be copied over, so you
 67         do not have to hold on it.
 68         */
 69         ctx->setCurrentFont( &font1 );
 70 
 71         /**
 72         Set the color of the context for line drawing
 73         */
 74         ctx->setColor( Color::getColor( "black" ) );
 75 
 76         /**
 77         draw a horizontal line, showing where the top of the text will be drawn
 78         */
 79         ctx->moveTo( x, y );
 80         ctx->lineTo( x + 600, y );
 81         ctx->strokePath();
 82 
 83 
 84         /**
 85         Now draw the text using the current font of the GraphicsContext
 86         */
 87         ctx->textAt( x, y, "Text Example - " );
 88 
 89         /**
 90         Determine the text width. The GraphicsContext uses it‘s
 91         current font to determine this
 92         */
 93         double width = ctx->getTextWidth( "Text Example - " );
 94 
 95         /**
 96         Now change the GraphicsContext‘s current to our second font
 97         */
 98         ctx->setCurrentFont( &font2 );
 99 
100         /**
101         and draw our next chunk of text
102         */
103         ctx->textAt( x + width, y, "all on a single line" );
104 
105 
106         x = 20;
107         y = 100;
108 
109         ctx->moveTo( x, y );
110         ctx->lineTo( x + 600, y );
111         ctx->strokePath();
112 
113         ctx->setCurrentFont( &font1 );
114 
115         /**
116         The GraphicsContext::textWithStateAt draws text in either an enabled or disabled
117         format, using the GraphicsContext‘s current font
118         */
119         ctx->textWithStateAt( x, y, "Text Example - ", false );
120 
121         width = ctx->getTextWidth( "Text Example - " );
122 
123         ctx->setCurrentFont( &font2 );
124 
125         ctx->textWithStateAt( x + width, y, "all on a single line", true );
126 
127 
128         y += 100;
129 
130         ctx->setCurrentFont( &font3 );
131 
132         /**
133         Now we‘ll prepare to draw text using
134         a Rect to specify the bounds that the text will be
135         drawn within
136         */
137         String text = "Text within bounds";
138         Rect textBounds(x,y,x,y);
139 
140         /**
141         This will mabe a rectangle that is the size of the text
142         using the GraphicsContext‘s current font
143         */
144         textBounds.right_ += ctx->getTextWidth( text );
145         textBounds.bottom_ += ctx->getTextHeight( text );
146 
147         /**
148         Draw the rectangle that encloses the text
149         */
150         ctx->rectangle( textBounds );
151         ctx->strokePath();
152 
153         /**
154         Draw the text with x,y coordinates
155         */
156         ctx->textAt( x, y, text );
157 
158         y += 100;
159 
160         textBounds.offset( 0, 100 );
161         textBounds.right_ -= 50;
162         textBounds.bottom_ += 100;
163 
164         ctx->rectangle( textBounds );
165         ctx->strokePath();
166 
167         text = "Text within bounds, left aligned and word wrapped";
168 
169         /**
170         Now draw the text using the rect‘s bounds. By default it will be word wrapped
171         */
172         ctx->textBoundedBy( &textBounds, text );
173 
174         int32 drawingOptions = GraphicsContext::tdoCenterHorzAlign;
175 
176         ctx->setCurrentFont( &font2 );
177         textBounds.offset( 350, -100 );
178         textBounds.right_ += 100;
179         textBounds.bottom_ -= 100;
180 
181         text = "Text within bounds, center aligned, single line";
182 
183         /**
184         Now draw the text using the rect‘s bounds
185         using the text drawing options specified in
186         the drawingOptions variable
187         */
188         ctx->textBoundedBy( &textBounds, text, drawingOptions );
189         ctx->rectangle( textBounds );
190         ctx->strokePath();
191 
192         textBounds.offset( 0, 50 );
193         textBounds.right_ -= 50;
194 
195         text = "Clipped text within bounds, center aligned, single line";
196 
197         /**
198         Draw the text but now that teh bounds have been shrunk, you‘ll see that
199         the text becomes clipped
200         */
201         ctx->textBoundedBy( &textBounds, text, drawingOptions );
202         ctx->rectangle( textBounds );
203         ctx->strokePath();
204 
205 
206         textBounds.offset( 0, 50 );
207         textBounds.right_ += 50;
208 
209         text = "Text within bounds, right aligned, single line";
210 
211         drawingOptions = GraphicsContext::tdoRightAlign;
212 
213         /**
214         Draw the text with right alignment
215         */
216         ctx->textBoundedBy( &textBounds, text, drawingOptions );
217         ctx->rectangle( textBounds );
218         ctx->strokePath();
219 
220 
221         textBounds.offset( 0, 50 );
222         textBounds.right_ += 50;
223         textBounds.bottom_ += 100;
224 
225         text = "Text within bounds, left aligned, single line, centered vertically";
226 
227         drawingOptions = GraphicsContext::tdoCenterVertAlign;
228 
229         /**
230         Draw the text and center it vertically
231         */
232         ctx->textBoundedBy( &textBounds, text, drawingOptions );
233         ctx->rectangle( textBounds );
234         ctx->strokePath();
235 
236 
237         x = textBounds.left_;
238         y = textBounds.bottom_ + 50;
239 
240 
241         textBounds.left_ = x;
242         textBounds.top_ = y;
243 
244 
245         ctx->moveTo( x, y );
246         ctx->lineTo( x + 600, y );
247         ctx->strokePath();
248 
249 
250         drawingOptions = GraphicsContext::tdoBottomAlign;
251 
252         ctx->setCurrentFont( &font1 );
253         textBounds.right_ = textBounds.left_ + ctx->getTextWidth( "Text Example - " );
254         textBounds.bottom_ = textBounds.top_ + ctx->getTextHeight( "EM" );
255 
256 
257         /**
258         Draw the text and align it to the bottom of the bounds
259         */
260         ctx->textBoundedBy( &textBounds, "Text Example - ", drawingOptions );
261 
262 
263         ctx->setCurrentFont( &font2 );
264 
265         textBounds.left_ = textBounds.right_;
266         textBounds.right_ = textBounds.left_ + ctx->getTextWidth( "all on a single line - bottom alignment" );
267 
268 
269         ctx->textBoundedBy( &textBounds, "all on a single line - bottom alignment", drawingOptions );
270 
271 
272 
273         /**
274         What follows shows you how to draw text that is aligned along a
275         baseline
276         */
277 
278         x = 20;
279         y = textBounds.top_ + 100;
280 
281         ctx->textAt( x, y -20, "The text below is aligned to the baseline, allowing for multiple fonts" );
282 
283         font1.setPointSize( 32 );
284 
285         ctx->setCurrentFont( &font1 );
286 
287 
288 
289         /**
290         Generate the baseline. The baseline is determined by using the
291         largest ascent in the series of font‘s you intend to use. In
292         our case that‘s font1
293         */
294         double baselineY = y + font1.getAscent() ;
295 
296         /**
297         draw the baseline
298         */
299         ctx->moveTo( x, baselineY );
300         ctx->lineTo( x + 1000, baselineY );
301         ctx->strokePath();
302 
303 
304         /**
305         draw the first part of the text
306         */
307         ctx->textAt( x, y, "Text Example - " );
308 
309         /**
310         nudge our x coordinate over by the width of the text we just drew
311         */
312         x += ctx->getTextWidth( "Text Example - " );
313 
314         /**
315         reset the y coordinate. It‘s new value is the baseline
316         minus the ascent of the next font we are going to use
317         */
318         y = baselineY - (font2.getAscent());
319 
320         /**
321         set the next font
322         */
323         ctx->setCurrentFont( &font2 );
324 
325         /**
326         draw the text
327         */
328         ctx->textAt( x, y, "all on a single line" );
329 
330         /**
331         repeat ad infinitum...
332         */
333         x += ctx->getTextWidth( "all on a single line" );
334 
335         ctx->setCurrentFont( &font3 );
336 
337         y = baselineY - (font3.getAscent());
338 
339         ctx->textAt( x, y, ", with even more Text!" );
340 
341         x += ctx->getTextWidth( ", with even more Text!" );
342 
343         font1.setColor( Color::getColor("red") );
344         font1.setItalic( true );
345 
346         font1.setPointSize( 16 );
347 
348         ctx->setCurrentFont( &font1 );
349         y = baselineY - (font1.getAscent());
350 
351 
352         ctx->textAt( x, y, " And now in Technicolor :)" );
353 
354     }
355 
356 };
357 
358 
359 
360 
361 class TextLayoutApp : public Application {
362 public:
363 
364     TextLayoutApp(int argc, char** argv) : Application(argc,argv){
365 
366     }
367     virtual bool initRunningApplication(){
368         bool result = Application::initRunningApplication();
369 
370         Window* mainWindow = new TextLayoutWindow();
371         setMainWindow(mainWindow);
372         mainWindow->setBounds( 0.0, 10.0, 925.0, 700.0 );
373         mainWindow->show();
374 
375         return result;
376     }
377 
378 };
379 
380 
381 int main(int argc, char *argv[])
382 {
383     Application* app = new TextLayoutApp( argc, argv );
384 
385     Application::main();
386 
387     return 0;
388 }
389 
390 
391 /**
392 $Id: TextLayout.cpp 3124 2007-06-17 00:26:39Z obirsoy $
393 */

 

TextLayout

标签:style   blog   ar   io   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/elitiwin/p/4171822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!