diff --git a/CHANGES b/CHANGES index d5ba2787c..ebcfa11a2 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,7 @@ CHANGES IN FLTK 1.1.5rc3 + - added test/unittests to verify pixel drawing and + alignment across platforms - Fl_Menu_::find_item() didn't determine the menu path properly (STR #481) - The build system now creates image libraries named diff --git a/src/Fl_mac.cxx b/src/Fl_mac.cxx index e6b088ec0..c70a649f8 100644 --- a/src/Fl_mac.cxx +++ b/src/Fl_mac.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_mac.cxx,v 1.1.2.59 2004/08/26 06:18:12 matthiaswm Exp $" +// "$Id: Fl_mac.cxx,v 1.1.2.60 2004/08/26 22:24:23 matthiaswm Exp $" // // MacOS specific code for the Fast Light Tool Kit (FLTK). // @@ -1863,6 +1863,8 @@ void Fl_Window::make_current() if (!i->gc) { //CreateCGContextForPort(GetWindowPort(i->xid), &i->gc); QDBeginCGContext(GetWindowPort(i->xid), &i->gc); +#warning : line capping should not be set. Check AA settings to make this work + CGContextSetLineCap(i->gc, kCGLineCapSquare); // save the unclipped state for later CGContextSaveGState(i->gc); // translate coordinate system to coorespond with fltk's. @@ -1948,6 +1950,6 @@ void Fl::paste(Fl_Widget &receiver, int clipboard) { // -// End of "$Id: Fl_mac.cxx,v 1.1.2.59 2004/08/26 06:18:12 matthiaswm Exp $". +// End of "$Id: Fl_mac.cxx,v 1.1.2.60 2004/08/26 22:24:23 matthiaswm Exp $". // diff --git a/src/fl_rect.cxx b/src/fl_rect.cxx index 5510bb648..d7b2549e2 100644 --- a/src/fl_rect.cxx +++ b/src/fl_rect.cxx @@ -1,5 +1,5 @@ // -// "$Id: fl_rect.cxx,v 1.10.2.4.2.14 2004/08/26 06:18:12 matthiaswm Exp $" +// "$Id: fl_rect.cxx,v 1.10.2.4.2.15 2004/08/26 22:24:24 matthiaswm Exp $" // // Rectangle drawing routines for the Fast Light Tool Kit (FLTK). // @@ -46,8 +46,10 @@ void fl_rect(int x, int y, int w, int h) { SetRect(&rect, x, y, x+w, y+h); FrameRect(&rect); #elif defined(__APPLE_QUARTZ__) - CGRect rect = CGRectMake(x-1.0f, y-1.0f, w, h); + CGContextSetShouldAntialias(fl_gc, false); + CGRect rect = CGRectMake(x, y, w-1, h-1); CGContextStrokeRect(fl_gc, rect); + CGContextSetShouldAntialias(fl_gc, true); #else XDrawRectangle(fl_display, fl_window, fl_gc, x, y, w-1, h-1); #endif @@ -65,8 +67,10 @@ void fl_rectf(int x, int y, int w, int h) { SetRect(&rect, x, y, x+w, y+h); PaintRect(&rect); #elif defined(__APPLE_QUARTZ__) - CGRect rect = CGRectMake(x-1.0f, y-1.0f, w, h); + CGContextSetShouldAntialias(fl_gc, false); + CGRect rect = CGRectMake(x, y, w-1, h-1); CGContextFillRect(fl_gc, rect); + CGContextSetShouldAntialias(fl_gc, true); #else if (w && h) XFillRectangle(fl_display, fl_window, fl_gc, x, y, w, h); #endif @@ -78,9 +82,11 @@ void fl_xyline(int x, int y, int x1) { #elif defined(__APPLE_QD__) MoveTo(x, y); LineTo(x1, y); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x1, y); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XDrawLine(fl_display, fl_window, fl_gc, x, y, x1, y); #endif @@ -98,10 +104,12 @@ void fl_xyline(int x, int y, int x1, int y2) { LineTo(x1, y); LineTo(x1, y2); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x1, y); CGContextAddLineToPoint(fl_gc, x1, y2); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XPoint p[3]; p[0].x = x; p[0].y = p[1].y = y; @@ -124,11 +132,13 @@ void fl_xyline(int x, int y, int x1, int y2, int x3) { LineTo(x1, y2); LineTo(x3, y2); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x1, y); CGContextAddLineToPoint(fl_gc, x1, y2); CGContextAddLineToPoint(fl_gc, x3, y2); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XPoint p[4]; p[0].x = x; p[0].y = p[1].y = y; @@ -146,9 +156,11 @@ void fl_yxline(int x, int y, int y1) { #elif defined(__APPLE_QD__) MoveTo(x, y); LineTo(x, y1); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x, y1); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XDrawLine(fl_display, fl_window, fl_gc, x, y, x, y1); #endif @@ -166,10 +178,12 @@ void fl_yxline(int x, int y, int y1, int x2) { LineTo(x, y1); LineTo(x2, y1); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x, y1); CGContextAddLineToPoint(fl_gc, x2, y1); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XPoint p[3]; p[0].x = p[1].x = x; p[0].y = y; @@ -192,11 +206,13 @@ void fl_yxline(int x, int y, int y1, int x2, int y3) { LineTo(x2, y1); LineTo(x2, y3); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x, y1); CGContextAddLineToPoint(fl_gc, x2, y1); CGContextAddLineToPoint(fl_gc, x2, y3); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XPoint p[4]; p[0].x = p[1].x = x; p[0].y = y; @@ -217,9 +233,11 @@ void fl_line(int x, int y, int x1, int y1) { MoveTo(x, y); LineTo(x1, y1); #elif defined(__APPLE_QUARTZ__) + if ( x==x1 || y==y1 ) CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x1, y1); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XDrawLine(fl_display, fl_window, fl_gc, x, y, x1, y1); #endif @@ -376,9 +394,11 @@ void fl_point(int x, int y) { #elif defined(__APPLE_QD__) MoveTo(x, y); Line(0, 0); #elif defined(__APPLE_QUARTZ__) + CGContextSetShouldAntialias(fl_gc, false); CGContextMoveToPoint(fl_gc, x, y); CGContextAddLineToPoint(fl_gc, x, y); CGContextStrokePath(fl_gc); + CGContextSetShouldAntialias(fl_gc, true); #else XDrawPoint(fl_display, fl_window, fl_gc, x, y); #endif @@ -642,5 +662,5 @@ int fl_clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H){ } // -// End of "$Id: fl_rect.cxx,v 1.10.2.4.2.14 2004/08/26 06:18:12 matthiaswm Exp $". +// End of "$Id: fl_rect.cxx,v 1.10.2.4.2.15 2004/08/26 22:24:24 matthiaswm Exp $". // diff --git a/test/Makefile b/test/Makefile index 8e85bd3b2..3c7d74bb4 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ # -# "$Id: Makefile,v 1.19.2.7.2.41 2004/04/11 04:39:00 easysw Exp $" +# "$Id: Makefile,v 1.19.2.7.2.42 2004/08/26 22:24:24 matthiaswm Exp $" # # Test/example program makefile for the Fast Light Tool Kit (FLTK). # @@ -26,6 +26,7 @@ include ../makeinclude CPPFILES =\ + unittests.cxx \ adjuster.cxx \ arc.cxx \ ask.cxx \ @@ -89,6 +90,7 @@ CPPFILES =\ valuators.cxx ALL = \ + unittests$(EXEEXT) \ adjuster$(EXEEXT) \ arc$(EXEEXT) \ ask$(EXEEXT) \ @@ -181,6 +183,8 @@ uninstall: $(ALL): ../lib/$(LIBNAME) # General demos... +unittests$(EXEEXT): unittests.o + adjuster$(EXEEXT): adjuster.o arc$(EXEEXT): arc.o @@ -372,5 +376,5 @@ shape$(EXEEXT): shape.o # -# End of "$Id: Makefile,v 1.19.2.7.2.41 2004/04/11 04:39:00 easysw Exp $". +# End of "$Id: Makefile,v 1.19.2.7.2.42 2004/08/26 22:24:24 matthiaswm Exp $". # diff --git a/test/unittests.cxx b/test/unittests.cxx new file mode 100644 index 000000000..2ea086a67 --- /dev/null +++ b/test/unittests.cxx @@ -0,0 +1,208 @@ + + + + +#include +#include +#include +#include +#include +#include + +Fl_Window *win = 0; + +int point_test_ix, line_test_ix, rect_test_ix, viewport_test_ix; + +void changePageCB(Fl_Widget*, void *ixvp) { + int ix = (int)ixvp; + int i = 0, n = win->children(); + for ( ; ichild(i)->hide(); + if (ix>=n || ix<0) ix = n-1; + win->child(ix)->show(); +} + +void newButton(int x, int y, int w, int h, const char *l, int ix, const char *tt) { + Fl_Button *b = new Fl_Button(x, y, w, h, l); + b->tooltip(tt); + b->callback(changePageCB, (void*)ix); +} + +void createMenuPage() { + Fl_Group *page, *g; + page = new Fl_Group(0, 0, 600, 600); + g = new Fl_Group(100, 20, 460, 26, "drawing:"); + g->align(FL_ALIGN_LEFT); + newButton(100+2, 22, 22, 22, "1", point_test_ix, "Testing pixel drawing"); + newButton(125+2, 22, 22, 22, "2", line_test_ix, "Testing fl_line"); + newButton(150+2, 22, 22, 22, "3", rect_test_ix, "Testing fl_rect"); + newButton(175+2, 22, 22, 22, "4", viewport_test_ix, "Testing viewport alignment"); + g->end(); + page->end(); +} + +Fl_Group *beginTestPage(const char *l) { + int ix = win->children(); + Fl_Group *g = new Fl_Group(0, 0, win->w(), win->h()); + g->box(FL_FLAT_BOX); + g->hide(); + newButton(20, 20, 20, 20, "M", -1, "Return to main menu"); + newButton(20, 40, 20, 20, "@<", ix-1, "previous test"); + newButton(20, 60, 20, 20, "@>", ix+1, "next test"); + Fl_Box *bx = new Fl_Box(60, 20, win->w()-80, 100, l); + bx->box(FL_THIN_DOWN_BOX); + bx->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP); + return g; +} + +//------- test the point drawing capabilities of this implementation ---------- +class PointTest : Fl_Widget { +public: PointTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {} + void draw() { + int a = x(), b = y(); + fl_color(FL_BLACK); + fl_rect(x(), y(), w(), h()); + fl_point(a+10, b+10); fl_point(a+20, b+20); + fl_point(a+10, b+20); fl_point(a+20, b+10); + fl_color(FL_RED); a = x()+70; + fl_point(a+10, b+10); fl_point(a+20, b+20); + fl_point(a+10, b+20); fl_point(a+20, b+10); + fl_color(FL_GREEN); a = x(); b = y()+70; + fl_point(a+10, b+10); fl_point(a+20, b+20); + fl_point(a+10, b+20); fl_point(a+20, b+10); + fl_color(FL_BLUE); a = x()+70; + fl_point(a+10, b+10); fl_point(a+20, b+20); + fl_point(a+10, b+20); fl_point(a+20, b+10); + } +}; +void fl_point_test() { + point_test_ix = win->children(); + Fl_Group *page = beginTestPage( + "testing the fl_point call\n" + "You should see four pixels each in black, red, green and blue. " + "Make sure that pixels are not anti-aliased (blured across multiple pixels)!" + ); + new PointTest(20, 140, 100, 100); + page->end(); +} + +//------- test the line drawing capabilities of this implementation ---------- +class LineTest : Fl_Widget { +public: LineTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {} + void draw() { + int a = x(), b = y(); fl_color(FL_BLACK); fl_rect(a, b, w(), h()); + // testing fl_xyline(x, y, x1) + fl_color(FL_RED); fl_point(a+10, b+10); fl_point(a+20, b+10); + fl_color(FL_BLACK); fl_xyline(a+10, b+10, a+20); + // testing fl_xyline(x, y, x1, y2); + fl_color(FL_RED); fl_point(a+10, b+20); fl_point(a+20, b+20); + fl_point(a+20, b+30); + fl_color(FL_BLACK); fl_xyline(a+10, b+20, a+20, b+30); + // testing fl_xyline(x, y, x1, y2, x3); + fl_color(FL_RED); fl_point(a+10, b+40); fl_point(a+20, b+40); + fl_point(a+20, b+50); fl_point(a+30, b+50); + fl_color(FL_BLACK); fl_xyline(a+10, b+40, a+20, b+50, a+30); + //+++ add testing for the fl_yxline commands! + // testing fl_loop(x,y, x,y, x,y, x, y) + fl_color(FL_RED); fl_point(a+60, b+60); fl_point(a+90, b+60); + fl_point(a+60, b+90); fl_point(a+90, b+90); + fl_color(FL_BLACK); + fl_loop(a+60, b+60, a+90, b+60, a+90, b+90, a+60, b+90); + } +}; +void fl_line_test() { + line_test_ix = win->children(); + Fl_Group *page = beginTestPage( + "testing the integer based fl_line calls\n" + "No red pixels should be visible. " + "If you see bright red pixels, the line drawing alignment is off, " + "or the last pixel in a line does not get drawn. " + "If you see dark red pixels, anti-aliasing must be switched off." + ); + new LineTest(20, 140, 100, 100); + page->end(); +} + +//------- test the line drawing capabilities of this implementation ---------- +class RectTest : Fl_Widget { +public: RectTest(int x, int y, int w, int h) : Fl_Widget(x, y, w, h) {} + void draw() { + int a = x(), b = y(); fl_color(FL_BLACK); fl_rect(a, b, w(), h()); + // testing fl_rect() with positive size + fl_color(FL_RED); fl_loop(a+10, b+10, a+40, b+10, a+40, b+40, a+10, b+40); + fl_color(FL_GREEN); fl_loop(a+ 9, b+ 9, a+41, b+ 9, a+41, b+41, a+ 9, b+41); + fl_color(FL_GREEN); fl_loop(a+11, b+11, a+39, b+11, a+39, b+39, a+11, b+39); + fl_color(FL_BLACK); fl_rect(a+10, b+10, 31, 31); + // testing fl_rect() with positive size + fl_color(FL_RED); fl_loop(a+60, b+60, a+90, b+60, a+90, b+90, a+60, b+90); + fl_color(FL_GREEN); fl_loop(a+59, b+59, a+91, b+59, a+91, b+91, a+59, b+91); + fl_color(FL_BLACK); fl_rectf(a+60, b+60, 31, 31); + } +}; +void fl_rect_test() { + rect_test_ix = win->children(); + Fl_Group *page = beginTestPage( + "testing the fl_rect call\n" + "No red pixels should be visible. " + "If you see bright red lines, or if parts of the green frames are hidden, " + "the rect drawing alignment is off. " + ); + new RectTest(20, 140, 100, 100); + page->end(); +} + +//------- test the line drawing capabilities of this implementation ---------- +class ViewportTest : Fl_Widget { + int pos; +public: ViewportTest(int x, int y, int w, int h, int p) : Fl_Widget(x, y, w, h), + pos(p) {} + void draw() { + if (pos&1) { + fl_color(FL_RED); fl_yxline(x()+w(), y(), y()+h()); + fl_color(FL_GREEN); fl_yxline(x()+w()-1, y(), y()+h()); + } else { + fl_color(FL_RED); fl_yxline(x()-1, y(), y()+h()); + fl_color(FL_GREEN); fl_yxline(x(), y(), y()+h()); + } + if (pos&2) { + fl_color(FL_RED); fl_xyline(x(), y()+h(), x()+w()); + fl_color(FL_GREEN); fl_xyline(x(), y()+h()-1, x()+w()); + } else { + fl_color(FL_RED); fl_xyline(x(), y()-1, x()+w()); + fl_color(FL_GREEN); fl_xyline(x(), y(), x()+w()); + } + fl_color(FL_BLACK); + fl_loop(x()+3, y()+3, x()+w()-4, y()+3, x()+w()-4, y()+h()-4, x()+3, y()+h()-4); + } +}; +void fl_viewport_test() { + viewport_test_ix = win->children(); + Fl_Group *page = beginTestPage( + "testing viewport alignment\n" + "Only green lines should be visible. " + "If red lines are visible in the corners of this window, " + "your viewport alignment and clipping is off. " + "If there is a space between the green lines and the window border, " + "the viewport is off, but some clipping may be working. " + "Also, your window size may be off to begin with." + ); + new ViewportTest(0, 0, 20, 20, 0); + new ViewportTest(page->w()-20, 0, 20, 20, 1); + new ViewportTest(0, page->h()-20, 20, 20, 2); + new ViewportTest(page->w()-20,page->h()-20, 20, 20, 3); + page->end(); +} + +int main(int argc, char **argv) { + win = new Fl_Window(600, 600, "Unit Tests for FLTK"); + fl_point_test(); + fl_line_test(); + fl_rect_test(); + fl_viewport_test(); + createMenuPage(); + win->end(); + win->show(argc, argv); + Fl::run(); +} + +