You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.6KB

  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. #include <FL/Fl_Box.H>
  28. #include <FL/fl_draw.H>
  29. //
  30. // --- fl_text_extents() tests -----------------------------------------------
  31. //
  32. class TextExtentsTest : public Fl_Widget
  33. {
  34. void DrawTextAndBoxes(const char *txt, int X, int Y) {
  35. int wo = 0, ho = 0;
  36. int dx, dy;
  37. // First, we draw the bounding boxes (fl_measure and fl_text_extents)
  38. // draw fl_measure() typographical bounding box
  39. fl_measure(txt, wo, ho, 0);
  40. int desc = fl_descent();
  41. fl_color(FL_RED);
  42. fl_rect(X, Y-ho+desc, wo, ho);
  43. // draw fl_text_extents() glyph bounding box
  44. fl_text_extents(txt, dx, dy, wo, ho);
  45. fl_color(FL_GREEN);
  46. fl_rect(X+dx, Y+dy, wo, ho);
  47. // Then we draw the text to show how it fits insode each of the two boxes
  48. fl_color(FL_BLACK);
  49. fl_draw(txt, X, Y);
  50. }
  51. public:
  52. static Fl_Widget *create() {
  53. return new TextExtentsTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
  54. }
  55. TextExtentsTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {}
  56. void draw(void) {
  57. int x0 = x(); // origin is current window position for Fl_Box
  58. int y0 = y();
  59. int w0 = w();
  60. int h0 = h();
  61. fl_push_clip(x0, y0, w0, h0); // reset local clipping
  62. {
  63. // set the background colour - slightly off-white to enhance the green bounding box
  64. fl_color(fl_gray_ramp(FL_NUM_GRAY - 3));
  65. fl_rectf(x0, y0, w0, h0);
  66. fl_font(FL_HELVETICA, 30);
  67. int xx = x0+55;
  68. int yy = y0+40;
  69. DrawTextAndBoxes("!abcdeABCDE\"#A", xx, yy); yy += 50; // mixed string
  70. DrawTextAndBoxes("oacs", xx, yy); xx += 100; // small glyphs
  71. DrawTextAndBoxes("qjgIPT", xx, yy); yy += 50; xx -= 100; // glyphs with descenders
  72. DrawTextAndBoxes("````````", xx, yy); yy += 50; // high small glyphs
  73. DrawTextAndBoxes("--------", xx, yy); yy += 50; // mid small glyphs
  74. DrawTextAndBoxes("________", xx, yy); yy += 50; // low small glyphs
  75. fl_font(FL_HELVETICA, 14);
  76. fl_color(FL_RED); fl_draw("fl_measure bounding box in RED", xx, yy); yy += 20;
  77. fl_color(FL_GREEN); fl_draw("fl_text_extents bounding box in GREEN", xx, yy);
  78. fl_color(FL_BLACK);
  79. xx = x0 + 10; yy += 30;
  80. fl_draw("NOTE: On systems with text anti-aliasing (e.g. OSX Quartz)", xx, yy);
  81. w0 = h0 = 0; fl_measure("NOTE: ", w0, h0, 0);
  82. xx += w0; yy += h0;
  83. fl_draw("text may leak slightly outside the fl_text_extents()", xx, yy);
  84. }
  85. fl_pop_clip(); // remove the local clip
  86. }
  87. };
  88. UnitTest textExtents("rendering text", TextExtentsTest::create);
  89. //
  90. // End of "$Id: unittest_text.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
  91. //