diff options
| author | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-12-02 15:51:38 +0000 |
|---|---|---|
| committer | Michael R Sweet <michael.r.sweet@gmail.com> | 1998-12-02 15:51:38 +0000 |
| commit | beb210d4d4ea2550fcc9de5a610b8fe6d9afc631 (patch) | |
| tree | f792a964cfdc82e9de4fd838be0c58282eefbbd5 /src/fl_font.cxx | |
| parent | c061230dc7ba0d14dcfa96dedb59f34f6a16d378 (diff) | |
Font changes from Bill.
git-svn-id: file:///fltk/svn/fltk/trunk@109 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/fl_font.cxx')
| -rw-r--r-- | src/fl_font.cxx | 134 |
1 files changed, 63 insertions, 71 deletions
diff --git a/src/fl_font.cxx b/src/fl_font.cxx index d8b0a6ac3..8fa805f8f 100644 --- a/src/fl_font.cxx +++ b/src/fl_font.cxx @@ -1,5 +1,5 @@ // -// "$Id: fl_font.cxx,v 1.4 1998/12/02 15:39:36 mike Exp $" +// "$Id: fl_font.cxx,v 1.5 1998/12/02 15:51:35 mike Exp $" // // Font selection code for the Fast Light Tool Kit (FLTK). // @@ -39,41 +39,20 @@ #include <stdlib.h> #include <string.h> -Fl_XFont::Fl_XFont(const char *name, int num) - : number(num) -{ +Fl_FontSize::Fl_FontSize(const char* name) { font = XLoadQueryFont(fl_display, name); if (!font) { Fl::warning("bad font: %s", name); font = XLoadQueryFont(fl_display, "fixed"); // if fixed fails we crash } - XCharStruct *p = font->per_char; - if (!p) { - free_this = per_char = 0; - } else if (font->min_char_or_byte2>0x20 || font->max_char_or_byte2<0xff) { - // fill in short fonts so fl_width does not crash: - XCharStruct *q = free_this = new XCharStruct[0xff-0x20+1]; - per_char = q - 0x20; - unsigned int i = 0x20; - for (; i<font->min_char_or_byte2; i++, q++) - q->width = font->min_bounds.width; - for (; i<=font->max_char_or_byte2; i++) - *q++ = *p++; - for (; i<=0xff; i++) - q->width = font->min_bounds.width; - } else { - free_this = 0; - per_char = p - font->min_char_or_byte2; - } #if HAVE_GL listbase = 0; #endif } -Fl_XFont *fl_current_xfont; -Fl_XFont *fl_fixed_xfont; +Fl_FontSize* fl_current_xfont; -Fl_XFont::~Fl_XFont() { +Fl_FontSize::~Fl_FontSize() { #if HAVE_GL // Delete list created by gl_draw(). This is not done by this code // as it will link in GL unnecessarily. There should be some kind @@ -86,7 +65,6 @@ Fl_XFont::~Fl_XFont() { // } #endif if (this == fl_current_xfont) fl_current_xfont = 0; - delete[] free_this; XFreeFont(fl_display, font); } @@ -113,28 +91,28 @@ static Fl_Fontdesc built_in_table[] = { {"-*-*zapf dingbats-*"} }; -Fl_Fontdesc *fl_fonts = built_in_table; +Fl_Fontdesc* fl_fonts = built_in_table; #define MAXSIZE 32767 // return dash number N, or pointer to ending null if none: -const char* fl_font_word(const char *p, int n) { +const char* fl_font_word(const char* p, int n) { while (*p) {if (*p=='-') {if (!--n) break;} p++;} return p; } // return a pointer to a number we think is "point size": -char *fl_find_fontsize(char* name) { - char *c = name; +char* fl_find_fontsize(char* name) { + char* c = name; // for standard x font names, try after 7th dash: if (*c == '-') { c = (char*)fl_font_word(c,7); if (*c++ && isdigit(*c)) return c; return 0; // malformed x font name? } - char *r = 0; + char* r = 0; // find last set of digits: - for (c++; *c; c++) + for (c++;* c; c++) if (isdigit(*c) && !isdigit(*(c-1))) r = c; return r; } @@ -148,42 +126,39 @@ int fl_correct_encoding(const char* name) { return (*c++ && !strcmp(c,fl_encoding)); } -// locate or create an Fl_XFont for a given Fl_Fontdesc and size: -static Fl_XFont *find(int fnum, int size) { - Fl_Fontdesc *s = fl_fonts+fnum; +// locate or create an Fl_FontSize for a given Fl_Fontdesc and size: +static Fl_FontSize* find(int fnum, int size) { + Fl_Fontdesc* s = fl_fonts+fnum; if (!s->name) s = fl_fonts; // use font 0 if still undefined - Fl_XFont *f; + Fl_FontSize* f; for (f = s->first; f; f = f->next) if (f->minsize <= size && f->maxsize >= size) return f; fl_open_display(); if (!s->xlist) { s->xlist = XListFonts(fl_display, s->name, 100, &(s->n)); if (!s->xlist) { // use fixed if no matching font... - if (!fl_fixed_xfont) { - fl_fixed_xfont = new Fl_XFont("fixed",fnum); - fl_fixed_xfont->minsize = 0; - fl_fixed_xfont->maxsize = 32767; - } - s->first = fl_fixed_xfont; - return fl_fixed_xfont; + s->first = new Fl_FontSize("fixed"); + s->first->minsize = 0; + s->first->maxsize = 32767; + return s->first; } } // search for largest <= font size: - char *name = s->xlist[0]; int ptsize = 0; // best one found so far + char* name = s->xlist[0]; int ptsize = 0; // best one found so far int matchedlength = 32767; char namebuffer[1024]; // holds scalable font name int found_encoding = 0; int m = s->n; if (m<0) m = -m; for (int n=0; n < m; n++) { - char *thisname = s->xlist[n]; + char* thisname = s->xlist[n]; if (fl_correct_encoding(thisname)) { if (!found_encoding) ptsize = 0; // force it to choose this found_encoding = 1; } else { if (found_encoding) continue; } - char *c = fl_find_fontsize(thisname); + char* c = fl_find_fontsize(thisname); int thissize = c ? atoi(c) : MAXSIZE; int thislength = strlen(thisname); if (thissize == size && thislength < matchedlength) { @@ -227,7 +202,7 @@ static Fl_XFont *find(int fnum, int size) { } // okay, we definately have some name, make the font: - f = new Fl_XFont(name,fnum); + f = new Fl_FontSize(name); if (ptsize < size) {f->minsize = ptsize; f->maxsize = size;} else {f->minsize = size; f->maxsize = ptsize;} f->next = s->first; @@ -241,64 +216,81 @@ static Fl_XFont *find(int fnum, int size) { int fl_font_; int fl_size_; +XFontStruct* fl_xfont; static GC font_gc; void fl_font(int fnum, int size) { if (fnum == fl_font_ && size == fl_size_) return; fl_font_ = fnum; fl_size_ = size; - Fl_XFont *f = find(fnum, size); - if (f != fl_current_xfont) {fl_current_xfont = f; font_gc = 0;} + Fl_FontSize* f = find(fnum, size); + if (f != fl_current_xfont) { + fl_current_xfont = f; + fl_xfont = f->font; + font_gc = 0; + } } int fl_height() { - return (fl_current_xfont->font->ascent + fl_current_xfont->font->descent); + return (fl_xfont->ascent + fl_xfont->descent); } int fl_descent() { - return fl_current_xfont->font->descent; + return fl_xfont->descent; } -double fl_width(const char *c) { - XCharStruct *p = fl_current_xfont->per_char; - if (!p) return strlen(c)*fl_current_xfont->font->min_bounds.width; +double fl_width(const char* c) { + XCharStruct* p = fl_xfont->per_char; + if (!p) return strlen(c)*fl_xfont->min_bounds.width; + int a = fl_xfont->min_char_or_byte2; + int b = fl_xfont->max_char_or_byte2 - a; int w = 0; - while (*c) - if (*c >= ' ') - w += p[(uchar)(*c++)].width; - else - c ++; - + while (*c) { + int x = *(uchar*)c++ - a; + if (x >= 0 && x <= b) w += p[x].width; + else w += fl_xfont->min_bounds.width; + } return w; } -double fl_width(const char *c, int n) { - XCharStruct *p = fl_current_xfont->per_char; - if (!p) return n*fl_current_xfont->font->min_bounds.width; +double fl_width(const char* c, int n) { + XCharStruct* p = fl_xfont->per_char; + if (!p) return n*fl_xfont->min_bounds.width; + int a = fl_xfont->min_char_or_byte2; + int b = fl_xfont->max_char_or_byte2 - a; int w = 0; - while (n--) w += p[(uchar)(*c++)].width; + while (n--) { + int x = *(uchar*)c++ - a; + if (x >= 0 && x <= b) w += p[x].width; + else w += fl_xfont->min_bounds.width; + } return w; } double fl_width(uchar c) { - XCharStruct *p = fl_current_xfont->per_char; - if (!p) return fl_current_xfont->font->min_bounds.width; - return p[c].width; + XCharStruct* p = fl_xfont->per_char; + if (p) { + int a = fl_xfont->min_char_or_byte2; + int b = fl_xfont->max_char_or_byte2 - a; + int x = c-a; + if (x >= 0 && x <= b) return p[c].width; + } + return fl_xfont->min_bounds.width; } -void fl_draw(const char *str, int n, int x, int y) { +void fl_draw(const char* str, int n, int x, int y) { if (font_gc != fl_gc) { font_gc = fl_gc; - XSetFont(fl_display, fl_gc, fl_current_xfont->font->fid); + XSetFont(fl_display, fl_gc, fl_xfont->fid); } XDrawString(fl_display, fl_window, fl_gc, x, y, str, n); } -void fl_draw(const char *str, int x, int y) { +void fl_draw(const char* str, int x, int y) { fl_draw(str, strlen(str), x, y); } #endif // -// End of "$Id: fl_font.cxx,v 1.4 1998/12/02 15:39:36 mike Exp $". +// End of "$Id: fl_font.cxx,v 1.5 1998/12/02 15:51:35 mike Exp $". // |
