📄 font.c
字号:
/* font.h - multiple font handling Copyright (C) 1996-2000 Paul Sheer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */#include "coolwidget.h"#include "coollocal.h"#include "aafont.h"#include "mad.h"#define TEST_FONT_STRING "The Quick Brown Fox Jumps Over The Lazy Dog"int option_no_font_set = 0;static struct font_stack { struct font_object *f; struct font_stack *next;} *font_stack = 0;/* only ImageStrings can bee anti-aliased */int CImageTextWidth (const char *s, int l){ if (FONT_USE_FONT_SET) return XmbTextEscapement (current_font->font_set, s, l); if (FONT_ANTIALIASING) return XAaTextWidth (current_font->font_struct, (char *) s, l); return XTextWidth (current_font->font_struct, s, l);}static XChar2b *wchar_t_to_XChar2b (wchar_t * swc, int l){ XChar2b *p, *s; int r = l; p = s = malloc (l * sizeof (XChar2b)); while (r--) { p->byte1 = (unsigned char) (*swc >> 8); (p++)->byte2 = (unsigned char) (*(swc++) & 0xFF); } return s;}int CImageTextWidthWC (XChar2b * s, wchar_t * swc, int l){ if (FONT_USE_FONT_SET) return XwcTextEscapement (current_font->font_set, swc, l); if (!s) { int r; if (FONT_ANTIALIASING) r = XAaTextWidth16 (current_font->font_struct, s = wchar_t_to_XChar2b (swc, l), l); else r = XTextWidth16 (current_font->font_struct, s = wchar_t_to_XChar2b (swc, l), l); free (s); return r; } if (FONT_ANTIALIASING) return XAaTextWidth16 (current_font->font_struct, s, l); return XTextWidth16 (current_font->font_struct, s, l);}int CImageStringWidth (const char *s){ return CImageTextWidth (s, strlen (s));}int CImageText (Window w, int x, int y, const char *s, int l){ if (FONT_USE_FONT_SET) { XmbDrawImageString (CDisplay, w, current_font->font_set, CGC, x, y, s, l); return XmbTextEscapement (current_font->font_set, s, l); } if (FONT_ANTIALIASING) return XAaDrawImageString (CDisplay, w, CGC, x, y, (char *) s, l); return XDrawImageString (CDisplay, w, CGC, x, y, s, l);}int CText (Window w, int x, int y, const char *s, int l){ if (FONT_USE_FONT_SET) { XmbDrawString (CDisplay, w, current_font->font_set, CGC, x, y, s, l); return XmbTextEscapement (current_font->font_set, s, l); } return XDrawString (CDisplay, w, CGC, x, y, s, l);}int CImageTextWC (Window w, int x, int y, XChar2b * s, wchar_t * swc, int l){ if (FONT_USE_FONT_SET) { XwcDrawImageString (CDisplay, w, current_font->font_set, CGC, x, y, swc, l); return XwcTextEscapement (current_font->font_set, swc, l); } if (!s) { int r; if (FONT_ANTIALIASING) r = XAaDrawImageString16 (CDisplay, w, CGC, x, y, s = wchar_t_to_XChar2b (swc, l), l); else r = XDrawImageString16 (CDisplay, w, CGC, x, y, s = wchar_t_to_XChar2b (swc, l), l); free (s); return r; } if (FONT_ANTIALIASING) return XAaDrawImageString16 (CDisplay, w, CGC, x, y, s, l); return XDrawImageString16 (CDisplay, w, CGC, x, y, s, l);}int CImageString (Window w, int x, int y, const char *s){ return CImageText (w, x, y, s, strlen (s));}static struct font_object *find_font (char *name){ struct font_object *n; if (current_font) if (!strcmp (current_font->name, name)) return current_font; for (n = all_fonts; n; n = n->next) if (!strcmp (n->name, name)) return n; return 0;}/* * ● If the min_byte1 and max_byte1 members are both zero, * min_char_or_byte2 specifies the linear character * index corresponding to the first element of the * per_char array, and max_char_or_byte2 specifies the * linear character index of the last element. * * If either min_byte1 or max_byte1 are nonzero, both * min_char_or_byte2 and max_char_or_byte2 are less than * 256, and the 2-byte character index values corre * sponding to the per_char array element N (counting * from 0) are: * * byte1 = N/D + min_byte1 * byte2 = N%D + min_char_or_byte2 * * where: * * D = max_char_or_byte2 - min_char_or_byte2 + 1 * / = integer division * % = integer modulus *//* returns width. the descent is only ever used for drawing an underbar. the ascent is only ever used in calculating FONT_PIX_PER_LINE */static int get_wchar_dimension (wchar_t ch, int *height, int *ascent, int *ink_descent){ int width, direction; XRectangle ink; XRectangle logical; if (FONT_USE_FONT_SET) { width = XwcTextExtents (current_font->font_set, &ch, 1, &ink, &logical); if (height) *height = logical.height; if (ascent) *ascent = (-logical.y); if (ink_descent) *ink_descent = ink.height + ink.y; } else { XCharStruct c; XChar2b s; int ascent_, descent, w; s.byte2 = ch & 0xFF; s.byte1 = (ch >> 8) & 0xFF; XTextExtents16 (current_font->font_struct, &s, 1, &direction, &ascent_, &descent, &c); width = c.width; if (FONT_ANTIALIASING) { width = SHRINK_WIDTH (width); if (ascent) *ascent = ascent_ / 3; if (height) *height = SHRINK_HEIGHT (ascent_ + descent); } else { if (ascent) *ascent = ascent_; if (height) *height = ascent_ + descent; } w = current_font->font_struct->max_char_or_byte2 - current_font->font_struct->min_char_or_byte2 + 1; if (w == 1) w = 0; if (ink_descent) { if (s.byte2 >= current_font->font_struct->min_char_or_byte2 && s.byte2 <= current_font->font_struct->max_char_or_byte2 && s.byte1 >= current_font->font_struct->min_byte1 && s.byte1 <= current_font->font_struct->max_byte1) { if (current_font->font_struct->per_char) { *ink_descent = current_font->font_struct->per_char[ (s.byte2 - current_font->font_struct-> min_char_or_byte2) + w * (s.byte1 - current_font-> font_struct-> min_byte1)]. descent; } else {/* this happens when you try to scale a non-scalable font */ *ink_descent = current_font->font_struct->max_bounds.descent; } } else { *ink_descent = 0; } if (FONT_ANTIALIASING) *ink_descent = (*ink_descent + 3) / 3; } } return width;}/* returns width. the descent is only ever used for drawing an underbar. the ascent is only ever used in calculating FONT_PIX_PER_LINE */static int get_string_dimensions (char *s, int n, int *height, int *ascent, int *ink_descent){ int width, direction; XRectangle ink; XRectangle logical; if (FONT_USE_FONT_SET) { width = XmbTextExtents (current_font->font_set, s, n, &ink, &logical); if (height) *height = logical.height; if (ascent) *ascent = (-logical.y); if (ink_descent) *ink_descent = ink.height + ink.y; } else { XCharStruct c; int ascent_, descent; XTextExtents (current_font->font_struct, s, n, &direction, &ascent_, &descent, &c); if (FONT_ANTIALIASING) { width = SHRINK_WIDTH (c.width); if (ascent) *ascent = ascent_ / 3; if (height) *height = SHRINK_HEIGHT (ascent_ + descent); } else { width = c.width; if (ascent) *ascent = ascent_; if (height) *height = ascent_ + descent; } if (ink_descent) { if (n == 1) { int i; i = (unsigned char) *s; if (i >= current_font->font_struct->min_char_or_byte2 && i <= current_font->font_struct->max_char_or_byte2) *ink_descent = current_font->font_struct->per_char[i - current_font-> font_struct->min_char_or_byte2].descent; else *ink_descent = 0; } else *ink_descent = descent; if (FONT_ANTIALIASING) *ink_descent = (*ink_descent + 3) / 3; }#if 0 width = CTextWidth (s, n);#endif }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -