📄 fl_font.cxx
字号:
//// "$Id: fl_font.cxx,v 1.1.1.1 2003/08/07 21:18:41 jasonk Exp $"//// Font selection code for the Fast Light Tool Kit (FLTK).//// Copyright 1998-1999 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 GNULibrary 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".//// Select fonts from the fltk font table.#ifdef WIN32#include "fl_font_win32.cxx"#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>#include <stdio.h>#ifdef NANO_X/* Define a class here for the font cache */#define WIDTH_CACHE_SIZE 20typedef struct{ GR_FONT_ID fontid; GR_FONT_INFO fi; int age; unsigned char widths[256];}width_cache_struct;class font_cache{ private: width_cache_struct * cache[WIDTH_CACHE_SIZE]; public: width_cache_struct * check_cache(GR_FONT_ID curfont) { /* For the moment, we index the cache with the font ID */ int n; for (n = 0; n < WIDTH_CACHE_SIZE; n++) { if (cache[n]) { if (cache[n]->fontid == curfont) { cache[n]->age = 0; return (cache[n]); } cache[n]->age++; } } return ((width_cache_struct *) 0); } void add_cache(GR_FONT_ID curfont, unsigned char *widths, GR_FONT_INFO * fi) { int n; int oldest = -1; for (n = 0; n < WIDTH_CACHE_SIZE; n++) { if (!cache[n]) { cache[n] = (width_cache_struct *) malloc(sizeof(width_cache_struct)); if (!cache[n]) return; cache[n]->fontid = curfont; cache[n]->age = 0; bcopy(widths, cache[n]->widths, 256); memcpy(&cache[n]->fi, fi, sizeof(GR_FONT_INFO)); return; } else { if (oldest != -1) { if (cache[n]->age > cache[oldest]->age) oldest = n; } else oldest = n; } } if (oldest == -1) return; bzero(cache[oldest], sizeof(width_cache_struct)); cache[oldest]->fontid = curfont; cache[oldest]->age = 0; bcopy(widths, cache[oldest]->widths, 256); return; } font_cache() { int n; for (n = 0; n < WIDTH_CACHE_SIZE; n++) cache[n] = (width_cache_struct *) 0; } ~font_cache() { int n; for (n = 0; n < WIDTH_CACHE_SIZE; n++) if (cache[n]) free(cache[n]); }};font_cache width_cache;#endif// size is not used unless we're nano-X and then it size defaults to 16// if TTF we'll resize it laterFl_FontSize::Fl_FontSize(const char *name, int size){#ifdef NANO_X isTTF = false; next = 0; // it can't be in a list yet font = GrCreateFont((GR_CHAR *) name, size, NULL); // need to determine whether the font is TrueType // GrGetFontInfo() needs a way to indicate that // so this is a bit of a hack; are all TTF fonts in .ttf files? if (font) { if (strstr(name, ".ttf")) { minsize = size; maxsize = size; // TTF fonts always scale isTTF = true; } else { minsize = size; maxsize = size; // not true type always scale } } else { Fl::warning("bad font: %s, using a builtin", name);// font = GrCreateFont("HZKFONT", 16, NULL); font = GrCreateFont((GR_CHAR *) "fonts/simfang.ttf", 16, NULL); if (!font) font = GrCreateFont((GR_CHAR *) GR_FONT_GUI_VAR, 0, NULL); }#else font = XLoadQueryFont(fl_display, name); if (!font) { Fl::warning("bad font: %s", name); font = XLoadQueryFont(fl_display, "fixed"); // if fixed fails we crash }#endif#if HAVE_GL listbase = 0;#endif}Fl_FontSize *fl_fontsize;#ifdef NANOX// attempts to resize the font in microwindowsvoidFl_FontSize::Resize(int size){ GrSetFontSize(font, size);}#endifFl_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// 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_fontsize) fl_fontsize = 0;#ifdef NANO_X if (font) GrDestroyFont(font);#else XFreeFont(fl_display, font);#endif}////////////////////////////////////////////////////////////////#ifdef NANO_X/* JHC - Major hack! */#ifdef PDAstatic Fl_Fontdesc built_in_table[] = { {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_SYSTEM_FIXED}, {GR_FONT_SYSTEM_FIXED}, {GR_FONT_SYSTEM_FIXED}, {GR_FONT_SYSTEM_FIXED}, {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_GUI_VAR}, {GR_FONT_OEM_FIXED}, {GR_FONT_SYSTEM_FIXED}, {GR_FONT_SYSTEM_FIXED}, {"pda3x6.fnt"}};#else// WARNING: if you add to this table, you must redefine FL_FREE_FONT// in Enumerations.H & recompile!!static Fl_Fontdesc built_in_table[] = { {"/usr/local/microwin/fonts/arial.ttf"}, {"/usr/local/microwin/fonts/arialb.ttf"}, {"/usr/local/microwin/fonts/ariali.ttf"}, {"/usr/local/microwin/fonts/arialz.ttf"}, {"/usr/local/microwin/fonts/cour.ttf"}, {"/usr/local/microwin/fonts/courb.ttf"}, {"/usr/local/microwin/fonts/couri.ttf"}, {"/usr/local/microwin/fonts/courz.ttf"}, {"/usr/local/microwin/fonts/times.ttf"}, {"/usr/local/microwin/fonts/timesb.ttf"}, {"/usr/local/microwin/fonts/timesi.ttf"}, {"/usr/local/microwin/fonts/timesz.ttf"}, {"/usr/local/microwin/fonts/impact.ttf"}, {"/usr/local/microwin/fonts/comic.ttf"}, {"/usr/local/microwin/fonts/comicbd.ttf"}, {"Terminal"} /* {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"}, {"HZKFONT"} */};#endif /* PDA */#else// 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-*"}};#endifFl_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){ 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; // 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; // find last set of digits: for (c++; *c; c++) if (isdigit(*c) && !isdigit(*(c - 1))) r = c; return r;}const char *fl_encoding = "iso8859-1";// return true if this matches fl_encoding:intfl_correct_encoding(const char *name){ if (*name != '-') return 0; const char *c = fl_font_word(name, 13); return (*c++ && !strcmp(c, fl_encoding));}#ifdef NANO_X// font choosers - if the fonts change in built_in_table, // these and the enum in Fl/Enumerations.H will have to changeinline boolIsBold(int fnum){ switch (fnum) { case FL_HELVETICA_BOLD: case FL_HELVETICA_BOLD_ITALIC: case FL_COURIER_BOLD: case FL_COURIER_BOLD_ITALIC: return true; default: return false; }}inline boolIsItalic(int fnum){ switch (fnum) { case FL_HELVETICA_ITALIC: case FL_HELVETICA_BOLD_ITALIC: case FL_COURIER_ITALIC: case FL_COURIER_BOLD_ITALIC: return true; default: return false; }}#endif // NANO-X
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -