Imported from antiword-0.37.tar.gz.
[antiword.git] / fonts_r.c
blob0898d4f37da3ffa5f7417ff5abca22d7467acbae
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 "DeskLib:Font.h"
12 #include "drawfile.h"
13 #include "antiword.h"
15 static font_handle tFontCurr = (font_handle)-1;
18 * pOpenFontTableFile - open the Font translation file
19 * Copy the file to the proper place if necessary.
21 * Returns the file pointer or NULL
23 FILE *
24 pOpenFontTableFile(void)
26 FILE *pFileR, *pFileW;
27 char *szFontNamesFile;
28 size_t tSize;
29 BOOL bFailed;
30 char acBuffer[256];
32 pFileR = fopen("<AntiWord$FontNamesFile>", "r");
33 if (pFileR != NULL) {
34 /* The font table is already in the right directory */
35 return pFileR;
38 szFontNamesFile = getenv("AntiWord$FontNamesSave");
39 if (szFontNamesFile == NULL) {
40 werr(0, "Warning: Name of the FontNames file not found");
41 return NULL;
43 DBG_MSG(szFontNamesFile);
45 pFileR = fopen("<AntiWord$Dir>.Resources.Default", "r");
46 if (pFileR == NULL) {
47 werr(0, "I can't find 'Resources.Default'");
48 return NULL;
50 /* Here the default font translation table is known to exist */
52 if (!bMakeDirectory(szFontNamesFile)) {
53 werr(0,
54 "I can't make a directory for the FontNames file");
55 return NULL;
57 /* Here the proper directory is known to exist */
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;
65 /* Here the proper directory is known to be writeable */
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;
76 if (fwrite(acBuffer, 1, tSize, pFileW) != tSize) {
77 DBG_MSG("Write error");
78 bFailed = TRUE;
79 break;
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;
89 return fopen(szFontNamesFile, "r");
90 } /* end of pOpenFontTableFile */
93 * vCloseFont - close the current font, if any
95 void
96 vCloseFont(void)
98 os_error *e;
100 NO_DBG_MSG("vCloseFont");
102 if (tFontCurr == (font_handle)-1) {
103 return;
105 e = Font_LoseFont(tFontCurr);
106 if (e != NULL) {
107 werr(0, "Close font error %d: %s", e->errnum, e->errmess);
109 tFontCurr = (font_handle)-1;
110 } /* end of vCloseFont */
113 * tOpenFont - make the specified font the current font
115 * Returns the font reference number for use in a draw file
117 drawfile_fontref
118 tOpenFont(UCHAR ucWordFontNumber, USHORT usFontStyle, USHORT usWordFontSize)
120 os_error *e;
121 const char *szOurFontname;
122 font_handle tFont;
123 int iFontnumber;
125 NO_DBG_MSG("tOpenFont");
126 NO_DBG_DEC(ucWordFontNumber);
127 NO_DBG_HEX(usFontStyle);
128 NO_DBG_DEC(usWordFontSize);
130 /* Keep the relevant bits */
131 usFontStyle &= FONT_BOLD|FONT_ITALIC;
132 NO_DBG_HEX(usFontStyle);
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;
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;
154 tFontCurr = (font_handle)-1;
155 return (drawfile_fontref)0;
157 tFontCurr = tFont;
158 NO_DBG_DEC(tFontCurr);
159 return (drawfile_fontref)(iFontnumber + 1);
160 } /* end of tOpenFont */
163 * tOpenTableFont - make the table font the current font
165 * Returns the font reference number for use in a draw file
167 drawfile_fontref
168 tOpenTableFont(USHORT usWordFontSize)
170 int iWordFontnumber;
172 NO_DBG_MSG("tOpenTableFont");
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;
181 return tOpenFont((UCHAR)iWordFontnumber, FONT_REGULAR, usWordFontSize);
182 } /* end of tOpenTableFont */
185 * lComputeStringWidth - compute the string width
187 * Returns the string width in millipoints
189 long
190 lComputeStringWidth(const char *szString, size_t tStringLength,
191 drawfile_fontref tFontRef, USHORT usFontSize)
193 font_string tStr;
194 os_error *e;
196 fail(szString == NULL);
197 fail(usFontSize < MIN_FONT_SIZE || usFontSize > MAX_FONT_SIZE);
199 if (szString[0] == '\0' || tStringLength == 0) {
200 /* Empty string */
201 return 0;
203 if (tStringLength == 1 && szString[0] == TABLE_SEPARATOR) {
204 /* Font_strwidth doesn't like control characters */
205 return 0;
207 if (tFontCurr == (font_handle)-1) {
208 /* No current font, use systemfont */
209 return lChar2MilliPoints(tStringLength);
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;
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 */
229 * tCountColumns - count the number of columns in a string
231 * Returns the number of columns
233 size_t
234 tCountColumns(const char *szString, size_t tLength)
236 fail(szString == NULL);
238 /* One byte, one character, one column */
239 return tLength;
240 } /* end of tCountColumns */
243 * tGetCharacterLength - the length of the specified character in bytes
245 * Returns the length in bytes
247 size_t
248 tGetCharacterLength(const char *szString)
250 return 1;
251 } /* end of tGetCharacterLength */