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