1 //
2 // "$Id: unittest_text.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
3 //
4 // Unit tests for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
22 //
23 // Please report all bugs and problems on the following page:
24 //
25 //     http://www.fltk.org/str.php
26 //
27 
28 #include <FL/Fl_Box.H>
29 #include <FL/fl_draw.H>
30 
31 //
32 // --- fl_text_extents() tests -----------------------------------------------
33 //
34 class TextExtentsTest : public Fl_Widget
35 {
DrawTextAndBoxes(const char * txt,int X,int Y)36   void DrawTextAndBoxes(const char *txt, int X, int Y) {
37     int wo = 0, ho = 0;
38     int dx, dy;
39     // First, we draw the bounding boxes (fl_measure and fl_text_extents)
40     // draw fl_measure() typographical bounding box
41     fl_measure(txt, wo, ho, 0);
42     int desc = fl_descent();
43     fl_color(FL_RED);
44     fl_rect(X, Y-ho+desc, wo, ho);
45     // draw fl_text_extents() glyph bounding box
46     fl_text_extents(txt, dx, dy, wo, ho);
47     fl_color(FL_GREEN);
48     fl_rect(X+dx, Y+dy, wo, ho);
49     // Then we draw the text to show how it fits insode each of the two boxes
50     fl_color(FL_BLACK);
51     fl_draw(txt, X, Y);
52   }
53 public:
create()54   static Fl_Widget *create() {
55     return new TextExtentsTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
56   }
TextExtentsTest(int x,int y,int w,int h)57   TextExtentsTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {}
draw(void)58   void draw(void) {
59     int x0 = x(); // origin is current window position for Fl_Box
60     int y0 = y();
61     int w0 = w();
62     int h0 = h();
63     fl_push_clip(x0, y0, w0, h0); // reset local clipping
64     {
65       // set the background colour - slightly off-white to enhance the green bounding box
66       fl_color(fl_gray_ramp(FL_NUM_GRAY - 3));
67       fl_rectf(x0, y0, w0, h0);
68 
69       fl_font(FL_HELVETICA, 30);
70       int xx = x0+55;
71       int yy = y0+40;
72       DrawTextAndBoxes("!abcdeABCDE\"#A", xx, yy); yy += 50;	// mixed string
73       DrawTextAndBoxes("oacs",     xx, yy); xx += 100;		// small glyphs
74       DrawTextAndBoxes("qjgIPT",   xx, yy); yy += 50; xx -= 100;	// glyphs with descenders
75       DrawTextAndBoxes("````````", xx, yy); yy += 50;		// high small glyphs
76       DrawTextAndBoxes("--------", xx, yy); yy += 50;		// mid small glyphs
77       DrawTextAndBoxes("________", xx, yy); yy += 50;		// low small glyphs
78 
79       fl_font(FL_HELVETICA, 14);
80       fl_color(FL_RED);  fl_draw("fl_measure bounding box in RED",       xx, yy); yy += 20;
81       fl_color(FL_GREEN); fl_draw("fl_text_extents bounding box in GREEN", xx, yy);
82       fl_color(FL_BLACK);
83       xx = x0 + 10;  yy += 30;
84       fl_draw("NOTE: On systems with text anti-aliasing (e.g. OSX Quartz)", xx, yy);
85       w0 = h0 = 0; fl_measure("NOTE: ", w0, h0, 0);
86       xx += w0; yy += h0;
87       fl_draw("text may leak slightly outside the fl_text_extents()", xx, yy);
88     }
89     fl_pop_clip(); // remove the local clip
90   }
91 };
92 
93 UnitTest textExtents("rendering text", TextExtentsTest::create);
94 
95 //
96 // End of "$Id: unittest_text.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
97 //
98