xref: /plan9/sys/src/cmd/aux/antiword/fonts_r.c (revision 25b329d522281a8cdd35da0dcc08c3fc621059a9)
1 /*
2  * fonts_r.c
3  * Copyright (C) 1999-2002 A.J. van Os; Released under GPL
4  *
5  * Description:
6  * Functions to deal with fonts (RiscOs version)
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include "DeskLib:Font.h"
12 #include "drawfile.h"
13 #include "antiword.h"
14 
15 static font_handle	tFontCurr = (font_handle)-1;
16 
17 /*
18  * pOpenFontTableFile - open the Font translation file
19  * Copy the file to the proper place if necessary.
20  *
21  * Returns the file pointer or NULL
22  */
23 FILE *
pOpenFontTableFile(void)24 pOpenFontTableFile(void)
25 {
26 	FILE	*pFileR, *pFileW;
27 	char	*szFontNamesFile;
28 	size_t	tSize;
29 	BOOL	bFailed;
30 	char	acBuffer[256];
31 
32 	pFileR = fopen("<AntiWord$FontNamesFile>", "r");
33 	if (pFileR != NULL) {
34 		/* The font table is already in the right directory */
35 		return pFileR;
36 	}
37 
38 	szFontNamesFile = getenv("AntiWord$FontNamesSave");
39 	if (szFontNamesFile == NULL) {
40 		werr(0, "Warning: Name of the FontNames file not found");
41 		return NULL;
42 	}
43 	DBG_MSG(szFontNamesFile);
44 
45 	pFileR = fopen("<AntiWord$Dir>.Resources.Default", "r");
46 	if (pFileR == NULL) {
47 		werr(0, "I can't find 'Resources.Default'");
48 		return NULL;
49 	}
50 	/* Here the default font translation table is known to exist */
51 
52 	if (!bMakeDirectory(szFontNamesFile)) {
53 		werr(0,
54 		"I can't make a directory for the FontNames file");
55 		return NULL;
56 	}
57 	/* Here the proper directory is known to exist */
58 
59 	pFileW = fopen(szFontNamesFile, "w");
60 	if (pFileW == NULL) {
61 		(void)fclose(pFileR);
62 		werr(0, "I can't create a default FontNames file");
63 		return NULL;
64 	}
65 	/* Here the proper directory is known to be writeable */
66 
67 	/* Copy the default FontNames file */
68 	bFailed = FALSE;
69 	while (!feof(pFileR)) {
70 		tSize = fread(acBuffer, 1, sizeof(acBuffer), pFileR);
71 		if (ferror(pFileR)) {
72 			DBG_MSG("Read error");
73 			bFailed = TRUE;
74 			break;
75 		}
76 		if (fwrite(acBuffer, 1, tSize, pFileW) != tSize) {
77 			DBG_MSG("Write error");
78 			bFailed = TRUE;
79 			break;
80 		}
81 	}
82 	(void)fclose(pFileW);
83 	(void)fclose(pFileR);
84 	if (bFailed) {
85 		DBG_MSG("Copying the FontNames file failed");
86 		(void)remove(szFontNamesFile);
87 		return NULL;
88 	}
89 	return fopen(szFontNamesFile, "r");
90 } /* end of pOpenFontTableFile */
91 
92 /*
93  * vCloseFont - close the current font, if any
94  */
95 void
vCloseFont(void)96 vCloseFont(void)
97 {
98 	os_error	*e;
99 
100 	NO_DBG_MSG("vCloseFont");
101 
102 	if (tFontCurr == (font_handle)-1) {
103 		return;
104 	}
105 	e = Font_LoseFont(tFontCurr);
106 	if (e != NULL) {
107 		werr(0, "Close font error %d: %s", e->errnum, e->errmess);
108 	}
109 	tFontCurr = (font_handle)-1;
110 } /* end of vCloseFont */
111 
112 /*
113  * tOpenFont - make the specified font the current font
114  *
115  * Returns the font reference number for use in a draw file
116  */
117 drawfile_fontref
tOpenFont(UCHAR ucWordFontNumber,USHORT usFontStyle,USHORT usWordFontSize)118 tOpenFont(UCHAR ucWordFontNumber, USHORT usFontStyle, USHORT usWordFontSize)
119 {
120 	os_error	*e;
121 	const char	*szOurFontname;
122 	font_handle	tFont;
123 	int	iFontnumber;
124 
125 	NO_DBG_MSG("tOpenFont");
126 	NO_DBG_DEC(ucWordFontNumber);
127 	NO_DBG_HEX(usFontStyle);
128 	NO_DBG_DEC(usWordFontSize);
129 
130 	/* Keep the relevant bits */
131 	usFontStyle &= FONT_BOLD|FONT_ITALIC;
132 	NO_DBG_HEX(usFontStyle);
133 
134 	iFontnumber = iGetFontByNumber(ucWordFontNumber, usFontStyle);
135 	szOurFontname = szGetOurFontname(iFontnumber);
136 	if (szOurFontname == NULL || szOurFontname[0] == '\0') {
137 		tFontCurr = (font_handle)-1;
138 		return (byte)0;
139 	}
140 	NO_DBG_MSG(szOurFontname);
141 	e = Font_FindFont(&tFont, (char *)szOurFontname,
142 			(int)usWordFontSize * 8, (int)usWordFontSize * 8,
143 			0, 0);
144 	if (e != NULL) {
145 		switch (e->errnum) {
146 		case 523:
147 			werr(0, "%s", e->errmess);
148 			break;
149 		default:
150 			werr(0, "Open font error %d: %s",
151 				e->errnum, e->errmess);
152 			break;
153 		}
154 		tFontCurr = (font_handle)-1;
155 		return (drawfile_fontref)0;
156 	}
157 	tFontCurr = tFont;
158 	NO_DBG_DEC(tFontCurr);
159 	return (drawfile_fontref)(iFontnumber + 1);
160 } /* end of tOpenFont */
161 
162 /*
163  * tOpenTableFont - make the table font the current font
164  *
165  * Returns the font reference number for use in a draw file
166  */
167 drawfile_fontref
tOpenTableFont(USHORT usWordFontSize)168 tOpenTableFont(USHORT usWordFontSize)
169 {
170 	int	iWordFontnumber;
171 
172 	NO_DBG_MSG("tOpenTableFont");
173 
174 	iWordFontnumber = iFontname2Fontnumber(TABLE_FONT, FONT_REGULAR);
175 	if (iWordFontnumber < 0 || iWordFontnumber > (int)UCHAR_MAX) {
176 		DBG_DEC(iWordFontnumber);
177 		tFontCurr = (font_handle)-1;
178 		return (drawfile_fontref)0;
179 	}
180 
181 	return tOpenFont((UCHAR)iWordFontnumber, FONT_REGULAR, usWordFontSize);
182 } /* end of tOpenTableFont */
183 
184 /*
185  * lComputeStringWidth - compute the string width
186  *
187  * Returns the string width in millipoints
188  */
189 long
lComputeStringWidth(const char * szString,size_t tStringLength,drawfile_fontref tFontRef,USHORT usFontSize)190 lComputeStringWidth(const char *szString, size_t tStringLength,
191 	drawfile_fontref tFontRef, USHORT usFontSize)
192 {
193 	font_string	tStr;
194 	os_error	*e;
195 
196 	fail(szString == NULL);
197 	fail(usFontSize < MIN_FONT_SIZE || usFontSize > MAX_FONT_SIZE);
198 
199 	if (szString[0] == '\0' || tStringLength == 0) {
200 		/* Empty string */
201 		return 0;
202 	}
203 	if (tStringLength == 1 && szString[0] == TABLE_SEPARATOR) {
204 		/* Font_strwidth doesn't like control characters */
205 		return 0;
206 	}
207 	if (tFontCurr == (font_handle)-1) {
208 		/* No current font, use systemfont */
209 		return lChar2MilliPoints(tStringLength);
210 	}
211 	tStr.s = (char *)szString;
212 	tStr.x = INT_MAX;
213 	tStr.y = INT_MAX;
214 	tStr.split = -1;
215 	tStr.term = tStringLength;
216 	e = Font_StringWidth(&tStr);
217 	if (e == NULL) {
218 		return (long)tStr.x;
219 	}
220 	DBG_DEC(e->errnum);
221 	DBG_MSG(e->errmess);
222 	DBG_DEC(tStringLength);
223 	DBG_MSG(szString);
224 	werr(0, "String width error %d: %s", e->errnum, e->errmess);
225 	return lChar2MilliPoints(tStringLength);
226 } /* end of lComputeStringWidth */
227 
228 /*
229  * tCountColumns - count the number of columns in a string
230  *
231  * Returns the number of columns
232  */
233 size_t
tCountColumns(const char * szString,size_t tLength)234 tCountColumns(const char *szString, size_t tLength)
235 {
236 	fail(szString == NULL);
237 
238 	/* One byte, one character, one column */
239 	return tLength;
240 } /* end of tCountColumns */
241 
242 /*
243  * tGetCharacterLength - the length of the specified character in bytes
244  *
245  * Returns the length in bytes
246  */
247 size_t
tGetCharacterLength(const char * szString)248 tGetCharacterLength(const char *szString)
249 {
250 	return 1;
251 } /* end of tGetCharacterLength */
252