1 /****************************************************************************
2 Copyright (C) 1987-2015 by Jeffery P. Hansen
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 Last edit by hansen on Thu Feb 12 14:48:59 2009
19 ****************************************************************************/
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <sys/time.h>
23 #include <stdarg.h>
24 #include "tkgate.h"
25
26 /*
27 * Table of all the fonts we have loaded. The indexes represent:
28 * [FF_MAX] Font family
29 * [FP_MAX] Point sizes
30 * [FS_MAX] Font styles
31 * [ZOOM_MAX] Zoom settings
32 */
33 static XFontStruct *xfonts[FF_MAX][FP_MAX][FS_MAX][ZOOM_MAX+1] = {{{{0}}}};
34
35 /*
36 * Names of the font families we support
37 */
38 static char *font_family_names[FF_MAX] = {
39 "courier",
40 "helvetica",
41 "times",
42 "symbol",
43 "kanji",
44 };
45
46 /*
47 * Font sizes we support. These correspond to the 7 font sizes levels in html.
48 */
49 static int font_sizes[FS_MAX] = {6, 8, 10, 12, 14, 18, 24};
50 static int kanji_font_sizes[FS_MAX] = {9, 9, 11, 13, 15, 18, 25};
51
52 /*
53 * Translate a font size code to a point size
54 */
getFontSize(int fs)55 int getFontSize(int fs)
56 {
57 return font_sizes[fs];
58 }
59
60 /*
61 * Translate a kanji font size code to a point size
62 */
getKanjiFontSize(int fs)63 int getKanjiFontSize(int fs)
64 {
65 return kanji_font_sizes[fs];
66 }
67
68 /*
69 * Translate a font family code to a font family name
70 */
getFontFamilyName(fontfamily_t ff)71 const char *getFontFamilyName(fontfamily_t ff)
72 {
73 return font_family_names[ff];
74 }
75
76 /*
77 * Get the x11 font name for the font with the requested properties and zoom-level
78 */
getFontName(char * fullName,fontfamily_t ff,fontprop_t fp,fontsize_t fs,int zoom)79 void getFontName(char *fullName,fontfamily_t ff,fontprop_t fp,fontsize_t fs,int zoom)
80 {
81 char *font_weight = (fp & FP_BOLD) ? "bold" : "medium";
82 char *font_posture;
83
84 if (ff == FF_TIMES)
85 font_posture = (fp & FP_ITALIC) ? "i" : "r";
86 else
87 font_posture = (fp & FP_ITALIC) ? "o" : "r";
88
89
90 switch (ff) {
91 case FF_KANJI :
92 sprintf(fullName,
93 "-misc-fixed-medium-r-normal--*-%d-75-75-c-*-jisx0208.1983-0",
94 kanji_font_sizes[fs]*10*zoom);
95 break;
96 case FF_SYMBOL :
97 sprintf(fullName,
98 "-adobe-symbol-medium-r-normal--%d-*-*-*-*-*-*",
99 font_sizes[fs]*zoom);
100 break;
101 default :
102 sprintf(fullName,
103 "-*-%s-%s-%s-normal--%d-*-*-*-*-*-%s",
104 font_family_names[ff],
105 font_weight,
106 font_posture,
107 font_sizes[fs]*zoom,
108 TkGate.locale->l_encFont);
109 break;
110 }
111 }
112
GetXFont(GateFont font,int zoom)113 XFontStruct *GetXFont(GateFont font,int zoom)
114 {
115 char fontName[STRMAX];
116 XFontStruct *xfs;
117
118 getFontName(fontName,font.family,font.prop,font.size,zoom);
119
120 xfs = XLoadQueryFont(TkGate.D,fontName);
121 if (!xfs) xfs = XLoadQueryFont(TkGate.D,"fixed");
122 if (!xfs) xfs = XLoadQueryFont(TkGate.D,"*");
123
124 return xfs;
125 }
126
GetXFonts(fontfamily_t ff,fontprop_t fp,fontsize_t fs)127 XFontStruct **GetXFonts(fontfamily_t ff,fontprop_t fp,fontsize_t fs)
128 {
129 int z;
130
131 GateFont font = { .family = ff, .prop = fp, .size = fs };
132
133 for (z = 1;z <= ZOOM_MAX;z++) {
134 xfonts[ff][fp][fs][z] = GetXFont(font,z);
135 }
136
137 return xfonts[ff][fp][fs];
138 }
139
UnloadAllFonts()140 void UnloadAllFonts()
141 {
142 fontfamily_t ff;
143 fontprop_t fp;
144 fontsize_t fs;
145 int z;
146
147 for (ff = 0;ff < FF_MAX; ff++) {
148 for (fp = 0;fp < FP_MAX; fp++) {
149 for (fs = 0;fs < FS_MAX; fs++) {
150 for (z = 1;z <= ZOOM_MAX; z++) {
151 if (xfonts[ff][fp][fs][z]) {
152 /* Tk_FreeFont(tkfonts[ff][fp][fs][z]);*/
153 }
154 }
155 }
156 }
157 }
158 }
159
160