1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-12-02 18:51:38 +03:00
|
|
|
// "$Id: fl_font.cxx,v 1.5 1998/12/02 15:51:35 mike Exp $"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// Font selection code for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
|
|
|
// Copyright 1998 by Bill Spitzak and others.
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
|
|
|
// Please report all bugs and problems to "fltk-bugs@easysw.com".
|
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
// Select fonts from the fltk font table.
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include "fl_font_win32.C"
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/x.H>
|
|
|
|
#include "Fl_Font.H"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_FontSize::Fl_FontSize(const char* name) {
|
1998-10-06 22:21:25 +04:00
|
|
|
font = XLoadQueryFont(fl_display, name);
|
|
|
|
if (!font) {
|
|
|
|
Fl::warning("bad font: %s", name);
|
|
|
|
font = XLoadQueryFont(fl_display, "fixed"); // if fixed fails we crash
|
|
|
|
}
|
|
|
|
#if HAVE_GL
|
|
|
|
listbase = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_FontSize* fl_current_xfont;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_FontSize::~Fl_FontSize() {
|
1998-10-06 22:21:25 +04:00
|
|
|
#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
|
|
|
|
// of "free" routine pointer, or a subclass?
|
|
|
|
// if (listbase) {
|
|
|
|
// int base = font->min_char_or_byte2;
|
|
|
|
// int size = font->max_char_or_byte2-base+1;
|
|
|
|
// int base = 0; int size = 256;
|
|
|
|
// glDeleteLists(listbase+base,size);
|
|
|
|
// }
|
|
|
|
#endif
|
|
|
|
if (this == fl_current_xfont) fl_current_xfont = 0;
|
|
|
|
XFreeFont(fl_display, font);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// WARNING: if you add to this table, you must redefine FL_FREE_FONT
|
|
|
|
// in Enumerations.H & recompile!!
|
|
|
|
static Fl_Fontdesc built_in_table[] = {
|
|
|
|
{"-*-helvetica-medium-r-normal--*"},
|
|
|
|
{"-*-helvetica-bold-r-normal--*"},
|
|
|
|
{"-*-helvetica-medium-o-normal--*"},
|
|
|
|
{"-*-helvetica-bold-o-normal--*"},
|
|
|
|
{"-*-courier-medium-r-normal--*"},
|
|
|
|
{"-*-courier-bold-r-normal--*"},
|
|
|
|
{"-*-courier-medium-o-normal--*"},
|
|
|
|
{"-*-courier-bold-o-normal--*"},
|
|
|
|
{"-*-times-medium-r-normal--*"},
|
|
|
|
{"-*-times-bold-r-normal--*"},
|
|
|
|
{"-*-times-medium-i-normal--*"},
|
|
|
|
{"-*-times-bold-i-normal--*"},
|
|
|
|
{"-*-symbol-*"},
|
|
|
|
{"-*-lucidatypewriter-medium-r-normal-sans-*"},
|
|
|
|
{"-*-lucidatypewriter-bold-r-normal-sans-*"},
|
|
|
|
{"-*-*zapf dingbats-*"}
|
|
|
|
};
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_Fontdesc* fl_fonts = built_in_table;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
#define MAXSIZE 32767
|
|
|
|
|
|
|
|
// return dash number N, or pointer to ending null if none:
|
1998-12-02 18:51:38 +03:00
|
|
|
const char* fl_font_word(const char* p, int n) {
|
1998-10-06 22:21:25 +04:00
|
|
|
while (*p) {if (*p=='-') {if (!--n) break;} p++;}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a pointer to a number we think is "point size":
|
1998-12-02 18:51:38 +03:00
|
|
|
char* fl_find_fontsize(char* name) {
|
|
|
|
char* c = name;
|
1998-10-06 22:21:25 +04:00
|
|
|
// 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?
|
|
|
|
}
|
1998-12-02 18:51:38 +03:00
|
|
|
char* r = 0;
|
1998-10-06 22:21:25 +04:00
|
|
|
// find last set of digits:
|
1998-12-02 18:51:38 +03:00
|
|
|
for (c++;* c; c++)
|
1998-10-06 22:21:25 +04:00
|
|
|
if (isdigit(*c) && !isdigit(*(c-1))) r = c;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* fl_encoding = "iso8859-1";
|
|
|
|
|
|
|
|
// return true if this matches fl_encoding:
|
|
|
|
int fl_correct_encoding(const char* name) {
|
|
|
|
if (*name != '-') return 0;
|
|
|
|
const char* c = fl_font_word(name,13);
|
|
|
|
return (*c++ && !strcmp(c,fl_encoding));
|
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
// 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;
|
1998-10-06 22:21:25 +04:00
|
|
|
if (!s->name) s = fl_fonts; // use font 0 if still undefined
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_FontSize* f;
|
1998-10-06 22:21:25 +04:00
|
|
|
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...
|
1998-12-02 18:51:38 +03:00
|
|
|
s->first = new Fl_FontSize("fixed");
|
|
|
|
s->first->minsize = 0;
|
|
|
|
s->first->maxsize = 32767;
|
|
|
|
return s->first;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// search for largest <= font size:
|
1998-12-02 18:51:38 +03:00
|
|
|
char* name = s->xlist[0]; int ptsize = 0; // best one found so far
|
1998-10-06 22:21:25 +04:00
|
|
|
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++) {
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
char* thisname = s->xlist[n];
|
1998-10-06 22:21:25 +04:00
|
|
|
if (fl_correct_encoding(thisname)) {
|
|
|
|
if (!found_encoding) ptsize = 0; // force it to choose this
|
|
|
|
found_encoding = 1;
|
|
|
|
} else {
|
|
|
|
if (found_encoding) continue;
|
|
|
|
}
|
1998-12-02 18:51:38 +03:00
|
|
|
char* c = fl_find_fontsize(thisname);
|
1998-10-06 22:21:25 +04:00
|
|
|
int thissize = c ? atoi(c) : MAXSIZE;
|
|
|
|
int thislength = strlen(thisname);
|
|
|
|
if (thissize == size && thislength < matchedlength) {
|
|
|
|
// exact match, use it:
|
|
|
|
name = thisname;
|
|
|
|
ptsize = size;
|
|
|
|
matchedlength = thislength;
|
|
|
|
} else if (!thissize && ptsize!=size) {
|
|
|
|
// whoa! A scalable font! Use unless exact match found:
|
|
|
|
int l = c-thisname;
|
|
|
|
memcpy(namebuffer,thisname,l);
|
|
|
|
#if 1 // this works if you don't want stdio
|
|
|
|
if (size>=100) namebuffer[l++] = size/100+'0';
|
|
|
|
if (size>=10) namebuffer[l++] = (size/10)%10+'0';
|
|
|
|
namebuffer[l++] = (size%10)+'0';
|
|
|
|
#else
|
|
|
|
//for some reason, sprintf fails to return the right value under Solaris.
|
|
|
|
l += sprintf(namebuffer+l,"%d",size);
|
|
|
|
#endif
|
|
|
|
while (*c == '0') c++;
|
|
|
|
strcpy(namebuffer+l,c);
|
|
|
|
name = namebuffer;
|
|
|
|
ptsize = size;
|
|
|
|
} else if (!ptsize || // no fonts yet
|
|
|
|
thissize < ptsize && ptsize > size || // current font too big
|
|
|
|
thissize > ptsize && thissize <= size // current too small
|
|
|
|
) {
|
|
|
|
name = thisname; ptsize = thissize;
|
|
|
|
matchedlength = thislength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptsize != size) { // see if we already found this unscalable font:
|
|
|
|
for (f = s->first; f; f = f->next) {
|
|
|
|
if (f->minsize <= ptsize && f->maxsize >= ptsize) {
|
|
|
|
if (f->minsize > size) f->minsize = size;
|
|
|
|
if (f->maxsize < size) f->maxsize = size;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, we definately have some name, make the font:
|
1998-12-02 18:51:38 +03:00
|
|
|
f = new Fl_FontSize(name);
|
1998-10-06 22:21:25 +04:00
|
|
|
if (ptsize < size) {f->minsize = ptsize; f->maxsize = size;}
|
|
|
|
else {f->minsize = size; f->maxsize = ptsize;}
|
|
|
|
f->next = s->first;
|
|
|
|
s->first = f;
|
|
|
|
return f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
// Public interface:
|
|
|
|
|
|
|
|
int fl_font_;
|
|
|
|
int fl_size_;
|
1998-12-02 18:51:38 +03:00
|
|
|
XFontStruct* fl_xfont;
|
1998-10-06 22:21:25 +04:00
|
|
|
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;
|
1998-12-02 18:51:38 +03:00
|
|
|
Fl_FontSize* f = find(fnum, size);
|
|
|
|
if (f != fl_current_xfont) {
|
|
|
|
fl_current_xfont = f;
|
|
|
|
fl_xfont = f->font;
|
|
|
|
font_gc = 0;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fl_height() {
|
1998-12-02 18:51:38 +03:00
|
|
|
return (fl_xfont->ascent + fl_xfont->descent);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fl_descent() {
|
1998-12-02 18:51:38 +03:00
|
|
|
return fl_xfont->descent;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
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;
|
1998-10-06 22:21:25 +04:00
|
|
|
int w = 0;
|
1998-12-02 18:51:38 +03:00
|
|
|
while (*c) {
|
|
|
|
int x = *(uchar*)c++ - a;
|
|
|
|
if (x >= 0 && x <= b) w += p[x].width;
|
|
|
|
else w += fl_xfont->min_bounds.width;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
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;
|
1998-10-06 22:21:25 +04:00
|
|
|
int w = 0;
|
1998-12-02 18:51:38 +03:00
|
|
|
while (n--) {
|
|
|
|
int x = *(uchar*)c++ - a;
|
|
|
|
if (x >= 0 && x <= b) w += p[x].width;
|
|
|
|
else w += fl_xfont->min_bounds.width;
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
double fl_width(uchar c) {
|
1998-12-02 18:51:38 +03:00
|
|
|
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;
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
void fl_draw(const char* str, int n, int x, int y) {
|
1998-10-06 22:21:25 +04:00
|
|
|
if (font_gc != fl_gc) {
|
|
|
|
font_gc = fl_gc;
|
1998-12-02 18:51:38 +03:00
|
|
|
XSetFont(fl_display, fl_gc, fl_xfont->fid);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
XDrawString(fl_display, fl_window, fl_gc, x, y, str, n);
|
|
|
|
}
|
|
|
|
|
1998-12-02 18:51:38 +03:00
|
|
|
void fl_draw(const char* str, int x, int y) {
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_draw(str, strlen(str), x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
1998-10-20 00:46:58 +04:00
|
|
|
|
|
|
|
//
|
1998-12-02 18:51:38 +03:00
|
|
|
// End of "$Id: fl_font.cxx,v 1.5 1998/12/02 15:51:35 mike Exp $".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|