New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / workbench / libs / diskfont / af_fontdescr_io.c
blob74d9469506cad877ca255bc239e4b8663fa8e49e
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Functions for readin .font files
6 Lang: English.
7 */
9 /****************************************************************************************/
11 #include <diskfont/diskfonttag.h>
12 #include <aros/macros.h>
14 #include <proto/dos.h>
15 #include <proto/utility.h>
16 #include <proto/arossupport.h>
17 #include <string.h>
18 #include "diskfont_intern.h"
20 #include <aros/debug.h>
22 /****************************************************************************************/
24 /******************/
25 /* ReadFontDescr */
26 /******************/
28 /****************************************************************************************/
30 struct FontDescrHeader *ReadFontDescr(CONST_STRPTR filename, struct DiskfontBase_intern *DiskfontBase)
32 struct FontDescrHeader *fdh = 0;
33 struct TTextAttr *tattr;
34 BPTR fh;
35 UWORD aword, numentries, numtags;
36 UWORD *availsizes = NULL;
37 UWORD numoutlineentries = 0;
38 UBYTE strbuf[MAXFONTPATH + 1];
40 D(bug("ReadFontDescr(filename=%s\n", filename));
42 /* Allocate the FontDescrHeader */
43 if (!(fh = Open(filename, MODE_OLDFILE)))
44 goto failure;
46 if (!(fdh = AllocMem(sizeof (struct FontDescrHeader), MEMF_ANY | MEMF_CLEAR)) )
47 goto failure;
49 /* First read the file id (FCH_ID or TFCH_ID) */
50 if (!ReadWord( &DFB(DiskfontBase)->dsh, &aword, (void *)fh ))
51 goto failure;
53 /* Check that this is a .font file */
55 if ( (aword != FCH_ID) && (aword != TFCH_ID) && (aword != OFCH_ID) )
57 D(bug("ReadFontDescr: unsupported file id\n"));
58 goto failure;
61 fdh->ContentsID = aword;
63 fdh->Tagged = ((aword == TFCH_ID) || (aword == OFCH_ID));
65 /* Read number of (T)FontContents structs in the file */
66 if (!ReadWord( &DFB(DiskfontBase)->dsh, &numentries , (void *)fh ))
68 D(bug("ReadFontDescr: error reading numentries\n"));
69 goto failure;
72 fdh->NumEntries = numentries;
74 if (fdh->ContentsID == OFCH_ID)
76 fdh->OTagList = OTAG_GetFile(filename, DiskfontBase);
77 if (fdh->OTagList)
79 availsizes = (UWORD *)GetTagData(OT_AvailSizes, (IPTR) NULL, fdh->OTagList->tags);
80 if (availsizes)
82 /* OT_AvailSizes points to an UWORD array, where the first UWORD
83 is the number of entries, and the following <numentry> UWORD's
84 are the sizes */
86 numoutlineentries = AROS_BE2WORD(*availsizes);
87 availsizes++;
91 else
92 D(bug("ReadFontDescr: No OTagList for outlined font\n"));
95 if (!(numentries + numoutlineentries)) goto failure;
97 /* Allocate mem for array of TTextAttrs */
98 fdh->TAttrArray = AllocVec((fdh->NumEntries + numoutlineentries) * sizeof(struct TTextAttr), MEMF_ANY|MEMF_CLEAR);
99 if (!fdh->TAttrArray) goto failure;
101 tattr = fdh->TAttrArray;
103 while (numentries--)
106 /* Read the fontname */
108 STRPTR strptr;
109 UWORD len = 0; /* Length of fontname in file including null-termination */
111 strptr = strbuf;
115 if (!ReadByte( &DFB(DiskfontBase)->dsh, strptr, (void *)fh))
116 goto failure;
118 len ++;
120 while (*strptr ++);
122 /* We set tattr->tta_Name to the real font name, for
123 example FONTS:helvetica/15 */
125 if (!(tattr->tta_Name = AllocVec(len + 1, MEMF_ANY)))
126 goto failure;
128 strcpy(tattr->tta_Name, strbuf);
130 D(bug("ReadFontDescr: tta_Name \"%s\"\n", tattr->tta_Name));
132 /* Seek to the end of the fontnamebuffer ( - 2 if tagged) */
133 Flush(fh);
134 Seek
137 MAXFONTPATH - ( len + (fdh->Tagged ? 2 : 0) ),
138 OFFSET_CURRENT
142 /* Do special stuff for files with tags in them */
143 if (fdh->Tagged)
146 /* Next thing up is the tagcount */
147 if (!ReadWord(&DFB(DiskfontBase)->dsh, &numtags, (void *)fh))
148 goto failure;
151 if (numtags)
153 /* Seek back and read the tags. Note, that the TAG_DONE
154 tagitem "goes over" the numtags, ie. it's ti_Data will
155 contain the numtags in the lower WORD:
157 TAGLIST start
159 00000000 80000008 00370000 80000001
160 003F003F 80000002 00640064 00000000
161 00000004 00378022 \TAG_DONE
162 |||| ||||||||
163 TagCount ||||||Flags
164 ||||||
165 ||||Style
166 ||||
167 YSize
170 Flush(fh);
171 Seek
173 fh,
174 -(numtags * 8), /* sizeof (struct TagItem) = 8 */
175 OFFSET_CURRENT
178 if (!(tattr->tta_Tags = ReadTags(fh, numtags, DFB(DiskfontBase) )))
179 goto failure;
181 } /* if (numtags) */
182 else
183 tattr->tta_Tags = NULL;
185 } /* if (fdh->Tagged) */
186 else
187 tattr->tta_Tags = NULL;
189 D(bug("ReadFontDescr: font \"%s\" tags %p\n", tattr->tta_Name, tattr->tta_Tags));
191 /* Read the rest of the info */
192 if (!ReadWord( &DFB(DiskfontBase)->dsh, &(tattr->tta_YSize), (void *)fh ))
193 goto failure;
195 if (!ReadByte(&DFB(DiskfontBase)->dsh, &(tattr->tta_Style), (void *)fh ))
196 goto failure;
198 if (!ReadByte( &DFB(DiskfontBase)->dsh, &(tattr->tta_Flags), (void *)fh ))
199 goto failure;
201 tattr ++;
203 } /* while (numentries--) */
205 Close(fh); fh = 0;
207 if (numoutlineentries)
209 UBYTE flags = OTAG_GetFontFlags(fdh->OTagList, DiskfontBase);
210 UBYTE style = OTAG_GetFontStyle(fdh->OTagList, DiskfontBase);
212 while(numoutlineentries--)
214 UWORD size;
215 UWORD i;
216 BOOL exists = FALSE;
218 size = AROS_BE2WORD(*availsizes);
219 availsizes++;
221 /* Check if this size already exists as bitmap
222 font. If it does, then ignore it */
224 for(i = 0; i < fdh->NumEntries; i++)
226 if (fdh->TAttrArray[i].tta_YSize == size)
228 exists = TRUE;
229 break;
233 if (exists) continue;
235 /* NumEntries contains the total number of entries, ie.
236 bitmap fonts and outline fonts included. NumOutlineEntries
237 tells how many among these (at the end of the array) are
238 outline fonts */
240 fdh->NumEntries++;
241 fdh->NumOutlineEntries++;
243 tattr->tta_Name = fdh->OTagList->filename;
244 tattr->tta_YSize = size;
245 tattr->tta_Flags = flags;
246 tattr->tta_Style = style;
247 tattr->tta_Tags = NULL;
249 D(bug("ReadFontDescr: font \"%s\" tags %p\n", tattr->tta_Name, tattr->tta_Tags));
251 tattr++;
252 } /* while(numoutlineentries--) */
254 } /* if (numoutlineentries) */
256 ReturnPtr ("ReadFontDescr", struct FontDescrHeader *, fdh);
258 failure:
259 /* Free all allocated memory */
261 if (fh) Close(fh);
263 if (fdh)
264 FreeFontDescr(fdh, DFB(DiskfontBase) );
266 ReturnPtr ("ReadFontDescr", struct FontDescrHeader*, FALSE);
270 /****************************************************************************************/
272 /******************/
273 /* FreeFontDescr */
274 /******************/
276 /****************************************************************************************/
278 VOID FreeFontDescr(struct FontDescrHeader *fdh, struct DiskfontBase_intern *DiskfontBase)
280 struct TTextAttr *tattr;
282 UWORD numentries;
284 D(bug("FreeFontDescr(fdh=%p\n", fdh));
286 if (fdh)
288 tattr = fdh->TAttrArray;
289 numentries = fdh->NumEntries - fdh->NumOutlineEntries;
291 if (tattr)
293 /* Go through the whole array of bitmap font entries
294 freeing strings and tags. Outline font entries
295 always have the tta_Name pointing to the otag file
296 and must not get the tta_Name freed. They also have
297 tta_Tags always being NULL. Therefore the loop below
298 goes only through the bitmap font entries */
300 while(numentries--)
302 if (tattr->tta_Name)
303 FreeVec(tattr->tta_Name);
305 if (tattr->tta_Tags)
306 FreeVec(tattr->tta_Tags);
308 tattr ++;
311 FreeVec(fdh->TAttrArray);
314 if (fdh->OTagList) OTAG_KillFile(fdh->OTagList, DiskfontBase);
316 FreeMem(fdh, sizeof (struct FontDescrHeader));
319 ReturnVoid("FreeFontDescr");
322 /****************************************************************************************/