From b90f593ae6c6769a1244b5efebbcad3f469027ae Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 9 Sep 2004 00:55:41 +0000 Subject: [PATCH] News from FLTK1.1 and Quartz - listing fonts using ATS instead of QD - made bold and italic fonts work - returning correct font metrics One problem came up though: Quartz renders fonts at subpixel positions, which is great for the overall look, but unfortunatly 'fl_draw' supports only integer coordinates for printing. As a result, marking a line of text makes the rest of the line jump by a fractional pixel. Any suggestions to solve this problem are greatly appreciated! git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@3810 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/mac.H | 6 ++-- src/Fl_Font.H | 5 ++- src/fl_font_mac.cxx | 66 +++++++++++++++++++++++++++++++++++----- src/fl_set_fonts_mac.cxx | 53 +++++++++++++++++++++++++++----- test/fonts.cxx | 8 +++-- 5 files changed, 116 insertions(+), 22 deletions(-) diff --git a/FL/mac.H b/FL/mac.H index 1322088d6..647dc5aef 100644 --- a/FL/mac.H +++ b/FL/mac.H @@ -1,5 +1,5 @@ // -// "$Id: mac.H,v 1.1.2.15 2004/08/31 22:00:44 matthiaswm Exp $" +// "$Id: mac.H,v 1.1.2.16 2004/09/09 00:55:41 matthiaswm Exp $" // // Mac header file for the Fast Light Tool Kit (FLTK). // @@ -79,6 +79,8 @@ public: void flush(); // Quartz additions: CGContextRef gc; // graphics context (NULL when using QD) + static ATSUTextLayout atsu_layout; // windows share a global font + static ATSUStyle atsu_style; static void q_fill_context(); // fill a Quartz context with current FLTK state static void q_clear_clipping(); // remove all clipping from a Quartz context static void q_release_context(Fl_X *x=0); // free all resources associated with fl_gc @@ -126,6 +128,6 @@ extern void fl_open_callback(void (*cb)(const char *)); extern FL_EXPORT int fl_parse_color(const char* p, uchar& r, uchar& g, uchar& b); // -// End of "$Id: mac.H,v 1.1.2.15 2004/08/31 22:00:44 matthiaswm Exp $". +// End of "$Id: mac.H,v 1.1.2.16 2004/09/09 00:55:41 matthiaswm Exp $". // diff --git a/src/Fl_Font.H b/src/Fl_Font.H index bca85a1ea..4918f9f3f 100644 --- a/src/Fl_Font.H +++ b/src/Fl_Font.H @@ -1,5 +1,5 @@ // -// "$Id: Fl_Font.H,v 1.6.2.3.2.7 2004/08/26 00:18:42 matthiaswm Exp $" +// "$Id: Fl_Font.H,v 1.6.2.3.2.8 2004/09/09 00:55:41 matthiaswm Exp $" // // Font definitions for the Fast Light Tool Kit (FLTK). // @@ -57,7 +57,6 @@ public: short width[256]; bool knowMetrics; # elif defined(__APPLE_QUARTZ__) -#warning : minimal quartz, use ATS instead! FL_EXPORT Fl_FontSize(const char* fontname, int size); char *q_name; int size; @@ -102,5 +101,5 @@ FL_EXPORT char *fl_find_fontsize(char *name); #endif // -// End of "$Id: Fl_Font.H,v 1.6.2.3.2.7 2004/08/26 00:18:42 matthiaswm Exp $". +// End of "$Id: Fl_Font.H,v 1.6.2.3.2.8 2004/09/09 00:55:41 matthiaswm Exp $". // diff --git a/src/fl_font_mac.cxx b/src/fl_font_mac.cxx index 0fc0f3a8e..5ddf479f0 100644 --- a/src/fl_font_mac.cxx +++ b/src/fl_font_mac.cxx @@ -1,5 +1,5 @@ // -// "$Id: fl_font_mac.cxx,v 1.1.2.20 2004/09/07 20:59:17 easysw Exp $" +// "$Id: fl_font_mac.cxx,v 1.1.2.21 2004/09/09 00:55:41 matthiaswm Exp $" // // MacOS font selection routines for the Fast Light Tool Kit (FLTK). // @@ -25,10 +25,6 @@ #include -#ifdef __APPLE_QUARTZ__ -#warning quartz -#endif - //: MeasureText, FontMetrics, WidthTabHandle, GetSysFont, SysFontSize //: TextSize, TextFont //: GetFNum (theName: Str255; VAR familyID: Integer); @@ -58,12 +54,24 @@ Fl_FontSize::Fl_FontSize(const char* name, int Size) { #endif minsize = maxsize = size; #elif defined(__APPLE_QUARTZ__) - q_name = strdup(name+1); + q_name = strdup(name); size = Size; ascent = Size*3/4; descent = Size-ascent; q_width = Size*2/3; minsize = maxsize = Size; + // Using ATS to get the genral Glyph size information + CFStringRef cfname = CFStringCreateWithCString(0L, q_name, kCFStringEncodingASCII); + ATSFontRef font = ATSFontFindFromName(cfname, kATSOptionFlagsDefault); + if (font) { + ATSFontMetrics m = { 0 }; + ATSFontGetHorizontalMetrics(font, kATSOptionFlagsDefault, &m); + if (m.avgAdvanceWidth) q_width = int(m.avgAdvanceWidth*size); + // playing with the offsets a little to make standard sizes fit + if (m.ascent) ascent = int(m.ascent*size-0.5f); + if (m.descent) descent = -int(m.descent*size-1.5f); + } + CFRelease(cfname); #endif } @@ -93,6 +101,7 @@ Fl_FontSize::~Fl_FontSize() { //////////////////////////////////////////////////////////////// static Fl_Fontdesc built_in_table[] = { +#ifdef __APPLE_QD__ {" Arial"}, {"BArial"}, {"IArial"}, @@ -109,6 +118,24 @@ static Fl_Fontdesc built_in_table[] = { {" Chicago"}, {"BChicago"}, {" Webdings"}, +#elif defined(__APPLE_QUARTZ__) +{"Arial"}, +{"Arial Bold"}, +{"Arial Italic"}, +{"Arial Bold Italic"}, +{"Courier New"}, +{"Courier New Bold"}, +{"Courier New Italic"}, +{"Courier New Bold Italic"}, +{"Times New Roman"}, +{"Times New Roman Bold"}, +{"Times New Roman Italic"}, +{"Times New Roman Bold Italic"}, +{"Symbol"}, +{"Monaco"}, +{"Andale Mono"}, // there is no bold Monaco font on standard Mac +{"Webdings"}, +#endif }; Fl_Fontdesc* fl_fonts = built_in_table; @@ -120,7 +147,8 @@ void fl_font(Fl_FontSize* s) { TextFont(fl_fontsize->font); //: select font into current QuickDraw GC TextFace(fl_fontsize->face); TextSize(fl_fontsize->size); - if (!fl_fontsize->knowMetrics) { //: get the true metrics for the currnet GC (fails on multiple monitors with different dpi's!) + if (!fl_fontsize->knowMetrics) { //: get the true metrics for the currnet GC + //: (fails on multiple monitors with different dpi's!) FontInfo fi; GetFontInfo(&fi); fl_fontsize->ascent = fi.ascent; fl_fontsize->descent = fi.descent; @@ -172,13 +200,35 @@ int fl_descent() { else return -1; } +// TODO: the text has to be translated according to the macroman_lut to give +// the correct result! double fl_width(const char* c, int n) { +#ifdef __APPLE_QD__ return (double)TextWidth( c, 0, n ); +#else + if (!fl_gc) { + Fl_Window *w = Fl::first_window(); + if (w) w->make_current(); + if (!fl_gc) return -1; + } + // according to the Apple developer docs, this is the correct way to + // find the length of a rendered text... + CGContextSetTextPosition(fl_gc, 0, 0); + CGContextSetTextDrawingMode(fl_gc, kCGTextInvisible); + CGContextShowText(fl_gc, c, n); + CGContextSetTextDrawingMode(fl_gc, kCGTextFill); + CGPoint p = CGContextGetTextPosition(fl_gc); + return p.x; +#endif } // todo : fl_width returns wrong results for OS X double fl_width(uchar c) { +#ifdef __APPLE_QD__ return (double)TextWidth( &c, 0, 1 ); +#else + return fl_width((const char*)&c, 1); +#endif } // MRS: The default character set is MacRoman, which is different from @@ -228,5 +278,5 @@ void fl_draw(const char* str, int n, int x, int y) { // -// End of "$Id: fl_font_mac.cxx,v 1.1.2.20 2004/09/07 20:59:17 easysw Exp $". +// End of "$Id: fl_font_mac.cxx,v 1.1.2.21 2004/09/09 00:55:41 matthiaswm Exp $". // diff --git a/src/fl_set_fonts_mac.cxx b/src/fl_set_fonts_mac.cxx index ee03c7d9f..8b49c2efe 100644 --- a/src/fl_set_fonts_mac.cxx +++ b/src/fl_set_fonts_mac.cxx @@ -1,5 +1,5 @@ // -// "$Id: fl_set_fonts_mac.cxx,v 1.1.2.11 2004/08/25 00:20:27 matthiaswm Exp $" +// "$Id: fl_set_fonts_mac.cxx,v 1.1.2.12 2004/09/09 00:55:41 matthiaswm Exp $" // // MacOS font utilities for the Fast Light Tool Kit (FLTK). // @@ -25,10 +25,6 @@ #include -#ifdef __APPLE_QUARTZ__ -#warning quartz -#endif - // This function fills in the fltk font table with all the fonts that // are found on the X server. It tries to place the fonts into families // and to sort them so the first 4 in a family are normal, bold, italic, @@ -42,6 +38,7 @@ // turn a stored font name into a pretty name: const char* Fl::get_font_name(Fl_Font fnum, int* ap) { +#ifdef __APPLE_QD__ Fl_Fontdesc *f = fl_fonts + fnum; if (!f->fontname[0]) { const char* p = f->name; @@ -60,12 +57,27 @@ const char* Fl::get_font_name(Fl_Font fnum, int* ap) { } if (ap) *ap = f->fontname[ENDOFBUFFER]; return f->fontname; +#elif defined(__APPLE_QUARTZ__) + Fl_Fontdesc *f = fl_fonts + fnum; + if (!f->fontname[0]) { + const char* p = f->name; + if (!p || !*p) {if (ap) *ap = 0; return "";} + strlcpy(f->fontname, p, ENDOFBUFFER); + int type = 0; + if (strstr(f->name, "Bold")) type |= FL_BOLD; + if (strstr(f->name, "Italic")) type |= FL_ITALIC; + f->fontname[ENDOFBUFFER] = (char)type; + } + if (ap) *ap = f->fontname[ENDOFBUFFER]; + return f->fontname; +#endif } static int fl_free_font = FL_FREE_FONT; Fl_Font Fl::set_fonts(const char* xstarname) { #pragma unused ( xstarname ) +#ifdef __APPLE_QD__ if (fl_free_font != FL_FREE_FONT) return (Fl_Font)fl_free_font; static char styleLU[] = " BIP"; @@ -113,13 +125,35 @@ Fl_Font Fl::set_fonts(const char* xstarname) { } FMDisposeFontFamilyIterator( &ffIterator ); return (Fl_Font)fl_free_font; +#elif defined(__APPLE_QUARTZ__) + ATSFontIterator it; + ATSFontIteratorCreate(kATSFontContextGlobal, 0L, 0L, kATSOptionFlagsRestrictedScope, &it); + for (;;) { + ATSFontRef font; + CFStringRef fname = 0; + OSStatus err = ATSFontIteratorNext(it, &font); + if (err!=noErr) break; + ATSFontGetName(font, kATSOptionFlagsDefault, &fname); + char buf[1024]; + CFStringGetCString(fname, buf, 1024, kCFStringEncodingASCII); + int i; + for (i=0; iname) s = fl_fonts; // empty slot in table, use entry 0 + int cnt = 0; +#ifdef __APPLE_QD__ Str255 name; int len = strlen( s->name ); memcpy(((char*)name)+1, s->name+1, len ); @@ -147,7 +181,6 @@ int Fl::get_font_sizes(Fl_Font fnum, int*& sizep) { FMFontSize size, pSize = -1; FMFontFamilyInstanceIterator ffiIterator; FMCreateFontFamilyInstanceIterator( family, &ffiIterator ); - int cnt = 0; OSStatus listInstances; for (;;) { @@ -163,10 +196,16 @@ int Fl::get_font_sizes(Fl_Font fnum, int*& sizep) { } } FMDisposeFontFamilyInstanceIterator( &ffiIterator ); +#elif defined(__APPLE_QUARTZ__) + // ATS supports all font size + array[0] = 0; + sizep = array; + cnt = 1; +#endif return cnt; } // -// End of "$Id: fl_set_fonts_mac.cxx,v 1.1.2.11 2004/08/25 00:20:27 matthiaswm Exp $". +// End of "$Id: fl_set_fonts_mac.cxx,v 1.1.2.12 2004/09/09 00:55:41 matthiaswm Exp $". // diff --git a/test/fonts.cxx b/test/fonts.cxx index 4443f90f6..951460196 100644 --- a/test/fonts.cxx +++ b/test/fonts.cxx @@ -1,5 +1,5 @@ // -// "$Id: fonts.cxx,v 1.5.2.3.2.4 2004/04/11 04:39:01 easysw Exp $" +// "$Id: fonts.cxx,v 1.5.2.3.2.5 2004/09/09 00:55:41 matthiaswm Exp $" // // Font demo program for the Fast Light Tool Kit (FLTK). // @@ -133,7 +133,11 @@ void create_the_forms() { int main(int argc, char **argv) { Fl::scheme(NULL); create_the_forms(); +#ifdef __APPLE__ + int i = 0; +#else int i = fl_choice("Which fonts:","-*","iso8859","All"); +#endif int k = Fl::set_fonts(i ? (i>1 ? "*" : 0) : "-*"); for (i = 0; i < k; i++) { int t; const char *name = Fl::get_font_name((Fl_Font)i,&t); @@ -165,5 +169,5 @@ int main(int argc, char **argv) { } // -// End of "$Id: fonts.cxx,v 1.5.2.3.2.4 2004/04/11 04:39:01 easysw Exp $". +// End of "$Id: fonts.cxx,v 1.5.2.3.2.5 2004/09/09 00:55:41 matthiaswm Exp $". //