3 * Copyright (C) 1999-2002 A.J. van Os; Released under GPL
6 * Functions to deal with fonts (RiscOs version)
11 #include "DeskLib:Font.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
24 pOpenFontTableFile(void)
26 FILE *pFileR
, *pFileW
;
27 char *szFontNamesFile
;
32 pFileR
= fopen("<AntiWord$FontNamesFile>", "r");
34 /* The font table is already in the right directory */
38 szFontNamesFile
= getenv("AntiWord$FontNamesSave");
39 if (szFontNamesFile
== NULL
) {
40 werr(0, "Warning: Name of the FontNames file not found");
43 DBG_MSG(szFontNamesFile
);
45 pFileR
= fopen("<AntiWord$Dir>.Resources.Default", "r");
47 werr(0, "I can't find 'Resources.Default'");
50 /* Here the default font translation table is known to exist */
52 if (!bMakeDirectory(szFontNamesFile
)) {
54 "I can't make a directory for the FontNames file");
57 /* Here the proper directory is known to exist */
59 pFileW
= fopen(szFontNamesFile
, "w");
62 werr(0, "I can't create a default FontNames file");
65 /* Here the proper directory is known to be writeable */
67 /* Copy the default FontNames file */
69 while (!feof(pFileR
)) {
70 tSize
= fread(acBuffer
, 1, sizeof(acBuffer
), pFileR
);
72 DBG_MSG("Read error");
76 if (fwrite(acBuffer
, 1, tSize
, pFileW
) != tSize
) {
77 DBG_MSG("Write error");
85 DBG_MSG("Copying the FontNames file failed");
86 (void)remove(szFontNamesFile
);
89 return fopen(szFontNamesFile
, "r");
90 } /* end of pOpenFontTableFile */
93 * vCloseFont - close the current font, if any
100 NO_DBG_MSG("vCloseFont");
102 if (tFontCurr
== (font_handle
)-1) {
105 e
= Font_LoseFont(tFontCurr
);
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
118 tOpenFont(UCHAR ucWordFontNumber
, USHORT usFontStyle
, USHORT usWordFontSize
)
121 const char *szOurFontname
;
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;
140 NO_DBG_MSG(szOurFontname
);
141 e
= Font_FindFont(&tFont
, (char *)szOurFontname
,
142 (int)usWordFontSize
* 8, (int)usWordFontSize
* 8,
147 werr(0, "%s", e
->errmess
);
150 werr(0, "Open font error %d: %s",
151 e
->errnum
, e
->errmess
);
154 tFontCurr
= (font_handle
)-1;
155 return (drawfile_fontref
)0;
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
168 tOpenTableFont(USHORT usWordFontSize
)
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
190 lComputeStringWidth(const char *szString
, size_t tStringLength
,
191 drawfile_fontref tFontRef
, USHORT usFontSize
)
196 fail(szString
== NULL
);
197 fail(usFontSize
< MIN_FONT_SIZE
|| usFontSize
> MAX_FONT_SIZE
);
199 if (szString
[0] == '\0' || tStringLength
== 0) {
203 if (tStringLength
== 1 && szString
[0] == TABLE_SEPARATOR
) {
204 /* Font_strwidth doesn't like control characters */
207 if (tFontCurr
== (font_handle
)-1) {
208 /* No current font, use systemfont */
209 return lChar2MilliPoints(tStringLength
);
211 tStr
.s
= (char *)szString
;
215 tStr
.term
= tStringLength
;
216 e
= Font_StringWidth(&tStr
);
222 DBG_DEC(tStringLength
);
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
234 tCountColumns(const char *szString
, size_t tLength
)
236 fail(szString
== NULL
);
238 /* One byte, one character, one column */
240 } /* end of tCountColumns */
243 * tGetCharacterLength - the length of the specified character in bytes
245 * Returns the length in bytes
248 tGetCharacterLength(const char *szString
)
251 } /* end of tGetCharacterLength */