FLTK logo

[master] fca63db - Minor update to unittest_text to add support for showing the text baseline in testing.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] fca63db - Minor update to unittest_text to add support for showing the text baseline in testing. "ian" Dec 07, 2022  
 
commit fca63db4ab3c813c4c5fe36411055ac431ced78d
Author:     ian <ian@ian-XPS-13-9360>
AuthorDate: Wed Dec 7 11:17:55 2022 +0000
Commit:     ian <ian@ian-XPS-13-9360>
CommitDate: Wed Dec 7 11:17:55 2022 +0000

    Minor update to unittest_text to add support for showing the text baseline in testing.

 test/unittest_text.cxx | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git test/unittest_text.cxx test/unittest_text.cxx
index 67c46f3..c1f5a9a 100644
--- test/unittest_text.cxx
+++ test/unittest_text.cxx
@@ -17,27 +17,39 @@
 #include "unittests.h"
 
 #include <FL/Fl_Box.H>
+#include <FL/Fl_Check_Button.H>
 #include <FL/fl_draw.H>
 
 //
 // --- fl_text_extents() tests -----------------------------------------------
+static void cb_base_bt(Fl_Widget *bt, void*) {
+  bt->parent()->redraw();
+}
 //
-class TextExtentsTest : public Fl_Widget
+class TextExtentsTest : public Fl_Group
 {
+  Fl_Check_Button *base_bt;
+
   void DrawTextAndBoxes(const char *txt, int X, int Y) {
-    int wo = 0, ho = 0;
+    int wm = 0, hm = 0, wt = 0, ht = 0;
     int dx, dy;
-    // First, we draw the bounding boxes (fl_measure and fl_text_extents)
+    // measure text so we can draw the baseline first
+    fl_measure(txt, wm, hm, 0);
+    fl_text_extents(txt, dx, dy, wt, ht);
+    // Draw a baseline before the boxes
+    if (base_bt->value()) {
+      fl_color(FL_BLUE);
+      fl_line((X - 20), Y, (X + wt + 20), Y);
+    }
+    // Then we draw the bounding boxes (fl_measure and fl_text_extents)
     // draw fl_measure() typographical bounding box
-    fl_measure(txt, wo, ho, 0);
     int desc = fl_descent();
     fl_color(FL_RED);
-    fl_rect(X, Y-ho+desc, wo, ho);
+    fl_rect(X, Y-hm+desc, wm, hm);
     // draw fl_text_extents() glyph bounding box
-    fl_text_extents(txt, dx, dy, wo, ho);
     fl_color(FL_GREEN);
-    fl_rect(X+dx, Y+dy, wo, ho);
-    // Then we draw the text to show how it fits insode each of the two boxes
+    fl_rect(X+dx, Y+dy, wt, ht);
+    // Then we draw the text to show how it fits inside each of the two boxes
     fl_color(FL_BLACK);
     fl_draw(txt, X, Y);
   }
@@ -45,7 +57,16 @@ public:
   static Fl_Widget *create() {
     return new TextExtentsTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
   }
-  TextExtentsTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {}
+  TextExtentsTest(int x, int y, int w, int h) : Fl_Group(x, y, w, h) {
+      base_bt = new Fl_Check_Button(x + w - 150, 50, 130, 20, "Show Baseline");
+      base_bt->box(FL_FLAT_BOX);
+      base_bt->down_box(FL_DOWN_BOX);
+      base_bt->callback(cb_base_bt);
+
+      Fl_Box *dummy = new Fl_Box ((x + w - 4), (y + h - 4), 2, 2);
+      resizable(dummy);
+      end();
+  }
   void draw(void) {
     int x0 = x(); // origin is current window position for Fl_Box
     int y0 = y();
@@ -57,6 +78,8 @@ public:
       fl_color(fl_gray_ramp(FL_NUM_GRAY - 3));
       fl_rectf(x0, y0, w0, h0);
 
+      Fl_Group::draw();
+
       fl_font(FL_HELVETICA, 30);
       int xx = x0+55;
       int yy = y0+40;
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.