2009-04-12 17:48:03 +04:00
|
|
|
//
|
|
|
|
// Unit tests for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2022-02-19 14:55:06 +03:00
|
|
|
// Copyright 1998-2022 by Bill Spitzak and others.
|
2009-04-12 17:48:03 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2009-04-12 17:48:03 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2009-04-12 17:48:03 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2009-04-12 17:48:03 +04:00
|
|
|
//
|
|
|
|
|
2022-02-06 17:22:24 +03:00
|
|
|
#include "unittests.h"
|
|
|
|
|
2009-04-12 17:48:03 +04:00
|
|
|
#include <FL/Fl_Box.H>
|
2022-12-07 14:17:55 +03:00
|
|
|
#include <FL/Fl_Check_Button.H>
|
2009-04-12 17:48:03 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
|
|
|
|
//
|
|
|
|
// --- fl_text_extents() tests -----------------------------------------------
|
2022-12-07 14:17:55 +03:00
|
|
|
static void cb_base_bt(Fl_Widget *bt, void*) {
|
|
|
|
bt->parent()->redraw();
|
|
|
|
}
|
2009-04-12 17:48:03 +04:00
|
|
|
//
|
2022-12-07 14:17:55 +03:00
|
|
|
class TextExtentsTest : public Fl_Group
|
2009-04-12 17:48:03 +04:00
|
|
|
{
|
2022-12-07 14:17:55 +03:00
|
|
|
Fl_Check_Button *base_bt;
|
|
|
|
|
2009-04-12 17:48:03 +04:00
|
|
|
void DrawTextAndBoxes(const char *txt, int X, int Y) {
|
2022-12-07 14:17:55 +03:00
|
|
|
int wm = 0, hm = 0, wt = 0, ht = 0;
|
2009-04-12 17:48:03 +04:00
|
|
|
int dx, dy;
|
2022-12-07 14:17:55 +03:00
|
|
|
// 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)
|
2009-04-12 17:48:03 +04:00
|
|
|
// draw fl_measure() typographical bounding box
|
|
|
|
int desc = fl_descent();
|
|
|
|
fl_color(FL_RED);
|
2022-12-07 14:17:55 +03:00
|
|
|
fl_rect(X, Y-hm+desc, wm, hm);
|
2009-04-12 17:48:03 +04:00
|
|
|
// draw fl_text_extents() glyph bounding box
|
|
|
|
fl_color(FL_GREEN);
|
2022-12-07 14:17:55 +03:00
|
|
|
fl_rect(X+dx, Y+dy, wt, ht);
|
|
|
|
// Then we draw the text to show how it fits inside each of the two boxes
|
2009-04-12 17:48:03 +04:00
|
|
|
fl_color(FL_BLACK);
|
|
|
|
fl_draw(txt, X, Y);
|
|
|
|
}
|
2020-07-01 19:03:10 +03:00
|
|
|
public:
|
2009-04-12 17:48:03 +04:00
|
|
|
static Fl_Widget *create() {
|
|
|
|
return new TextExtentsTest(TESTAREA_X, TESTAREA_Y, TESTAREA_W, TESTAREA_H);
|
|
|
|
}
|
2022-12-07 14:17:55 +03:00
|
|
|
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();
|
|
|
|
}
|
2009-04-12 17:48:03 +04:00
|
|
|
void draw(void) {
|
|
|
|
int x0 = x(); // origin is current window position for Fl_Box
|
|
|
|
int y0 = y();
|
|
|
|
int w0 = w();
|
|
|
|
int h0 = h();
|
|
|
|
fl_push_clip(x0, y0, w0, h0); // reset local clipping
|
|
|
|
{
|
|
|
|
// set the background colour - slightly off-white to enhance the green bounding box
|
|
|
|
fl_color(fl_gray_ramp(FL_NUM_GRAY - 3));
|
|
|
|
fl_rectf(x0, y0, w0, h0);
|
|
|
|
|
2022-12-07 14:17:55 +03:00
|
|
|
Fl_Group::draw();
|
|
|
|
|
2009-04-12 17:48:03 +04:00
|
|
|
fl_font(FL_HELVETICA, 30);
|
|
|
|
int xx = x0+55;
|
|
|
|
int yy = y0+40;
|
2020-07-01 19:03:10 +03:00
|
|
|
DrawTextAndBoxes("!abcdeABCDE\"#A", xx, yy); yy += 50; // mixed string
|
|
|
|
DrawTextAndBoxes("oacs", xx, yy); xx += 100; // small glyphs
|
|
|
|
DrawTextAndBoxes("qjgIPT", xx, yy); yy += 50; xx -= 100; // glyphs with descenders
|
|
|
|
DrawTextAndBoxes("````````", xx, yy); yy += 50; // high small glyphs
|
|
|
|
DrawTextAndBoxes("--------", xx, yy); yy += 50; // mid small glyphs
|
|
|
|
DrawTextAndBoxes("________", xx, yy); yy += 50; // low small glyphs
|
2009-04-12 17:48:03 +04:00
|
|
|
|
|
|
|
fl_font(FL_HELVETICA, 14);
|
|
|
|
fl_color(FL_RED); fl_draw("fl_measure bounding box in RED", xx, yy); yy += 20;
|
|
|
|
fl_color(FL_GREEN); fl_draw("fl_text_extents bounding box in GREEN", xx, yy);
|
|
|
|
fl_color(FL_BLACK);
|
|
|
|
xx = x0 + 10; yy += 30;
|
2022-02-19 14:55:06 +03:00
|
|
|
fl_draw("NOTE: On systems with text anti-aliasing (e.g. macOS Quartz)", xx, yy);
|
2009-04-12 17:48:03 +04:00
|
|
|
w0 = h0 = 0; fl_measure("NOTE: ", w0, h0, 0);
|
|
|
|
xx += w0; yy += h0;
|
|
|
|
fl_draw("text may leak slightly outside the fl_text_extents()", xx, yy);
|
|
|
|
}
|
|
|
|
fl_pop_clip(); // remove the local clip
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-11 21:18:06 +03:00
|
|
|
UnitTest textExtents(kTestText, "Rendering Text", TextExtentsTest::create);
|