📄 fl_font.cxx
字号:
#ifdef NANO_Xstatic 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_FontSize *f; for (f = s->first; f; f = f->next) { if (f->minsize <= size && f->maxsize >= size) { return f; } } fl_open_display(); // time to create one if (!s->first) { s->first = new Fl_FontSize(s->name, size); return s->first; } for (f = s->first; f->next; f = f->next); f->next = new Fl_FontSize(s->name, size); return f;}#else// 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_FontSize *f; for (f = s->first; f; f = f->next) { if (f->minsize <= size && f->maxsize >= size#ifdef NANOX && !f->isTTF#endif ) { return f; } } fl_open_display();#ifdef NANO_X // time to create one if (!s->first) { s->first = new Fl_FontSize(s->name, size); } // if this is TTF there will only be a first, so resize it if (s->first && s->first->isTTF) { s->first->Resize(size); return s->first; }#endif if (!s->xlist) {#ifndef NANO_X //tanghao s->xlist = XListFonts(fl_display, s->name, 100, &(s->n));#endif //tanghao if (!s->xlist) { // use fixed if no matching font...#ifdef NANO_X// s->first = new Fl_FontSize("HZKFONT"); s->first = new Fl_FontSize(GR_FONT_GUI_VAR); //"/Setup/microwin/src/fonts/simfang.ttf"););//"simfang.ttf");#else s->first = new Fl_FontSize("fixed");#endif s->first->minsize = size; s->first->maxsize = size; return s->first; } } // search for largest <= font size: 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]; 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); 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: 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; s->first = f; return f;}#endif////////////////////////////////////////////////////////////////// Public interface:int fl_font_;int fl_size_;#ifdef NANO_XGR_FONT_ID fl_xfont;#elseXFontStruct *fl_xfont;#endifstatic GC font_gc;voidfl_font(int fnum, int size){ if (fnum == fl_font_ && size == fl_size_) return; fl_font_ = fnum; fl_size_ = size; Fl_FontSize *f = find(fnum, size); if (f != fl_fontsize) { fl_fontsize = f; fl_xfont = f->font; font_gc = 0; }}intfl_height(){#ifdef NANO_X GR_FONT_INFO fi; width_cache_struct *wc = width_cache.check_cache(fl_xfont); if (!wc) { GrGetFontInfo(fl_xfont, &fi); width_cache.add_cache(fl_xfont, fi.widths, &fi); return fi.height; } else { return wc->fi.height; }#else return (fl_xfont->ascent + fl_xfont->descent);#endif}intfl_descent(){#ifdef NANO_X GR_FONT_INFO fi; width_cache_struct *wc = width_cache.check_cache(fl_xfont); if (!wc) { GrGetFontInfo(fl_xfont, &fi); width_cache.add_cache(fl_xfont, fi.widths, &fi); return fi.height - fi.baseline; } else { return wc->fi.height - wc->fi.baseline; }#else return fl_xfont->descent;#endif}/* JHC 0928/00 - We must implement caching for the *//* NanoX functions, because of speed issues with the *//* TrueType fonts */doublefl_width(const char *c){#ifdef NANO_X GR_FONT_INFO fi; double res = 0.0; width_cache_struct *wc = width_cache.check_cache(fl_xfont); unsigned char *fwidths; if (!wc) { GrGetFontInfo(fl_xfont, &fi); width_cache.add_cache(fl_xfont, fi.widths, &fi); fwidths = fi.widths; } else { fwidths = wc->widths; } while (*c) { res += fwidths[*c]; c++; } return res;#else 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) { int x = *(uchar *) c++ - a; if (x >= 0 && x <= b) w += p[x].width; else w += fl_xfont->min_bounds.width; } return w;#endif //tanghao}doublefl_width(const char *c, int n){#ifdef NANO_X double w = 0; GR_FONT_INFO fi; width_cache_struct *wc = width_cache.check_cache(fl_xfont); unsigned char *fwidths; if (!wc) { GrGetFontInfo(fl_xfont, &fi); width_cache.add_cache(fl_xfont, fi.widths, &fi); fwidths = fi.widths; } else { fwidths = wc->widths; } for (int i = 0; i < n; i++) { w += (double) (fwidths[*(c + i)]); } return w;#else 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--) { int x = *(uchar *) c++ - a; if (x >= 0 && x <= b) w += p[x].width; else w += fl_xfont->min_bounds.width; } return w;#endif //tanghao}doublefl_width(uchar c){#ifdef NANO_X GR_FONT_INFO fi; width_cache_struct *wc = width_cache.check_cache(fl_xfont); unsigned char *fwidths; if (!wc) { GrGetFontInfo(fl_xfont, &fi); width_cache.add_cache(fl_xfont, fi.widths, &fi); fwidths = fi.widths; } else { fwidths = wc->widths; } return fwidths[c];#else 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[x].width; } return fl_xfont->min_bounds.width;#endif //tanghao}voidfl_draw(const char *str, int n, int x, int y){ if (font_gc != fl_gc) { if (!fl_xfont) { fl_font(FL_HELVETICA, 14); } font_gc = fl_gc;#if NANO_X GrSetGCFont(fl_gc, fl_xfont);#else XSetFont(fl_display, fl_gc, fl_xfont->fid);#endif }#if NANO_X //FIXME hack becasue nano-X will not draw a true type font // correctly without first setting its background!!! GR_GC_INFO info; GrGetGCInfo(fl_gc, &info); if (info.background == MWRGB(255, 255, 255)) GrSetGCBackground(fl_gc, MWRGB(255, 0, 0)); else GrSetGCBackground(fl_gc, MWRGB(255, 255, 255)); GrSetGCUseBackground(fl_gc, GR_FALSE); GrText(fl_window, fl_gc, x, y, (GR_CHAR *) str, n, GR_TFASCII);#else XDrawString(fl_display, fl_window, fl_gc, x, y, str, n);#endif}voidfl_draw(const char *str, int x, int y){ fl_draw(str, strlen(str), x, y);}#endif//// End of "$Id: fl_font.cxx,v 1.1.1.1 2003/08/07 21:18:41 jasonk Exp $".//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -