added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / libs / diskfont / af_fontdescr_io.c
blob95cb15b5fb56ff24b224a0edb99103cbd9b4adfa
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
93 D(bug("ReadFontDescr: No OTagList for outlined font\n"));
95 #warning "stegerg: CHECKME!! Prevent OTAG_ functions from being called on NULL/non existing otag / taglist"
96 fdh->ContentsID = TFCH_ID;
100 if (!(numentries + numoutlineentries)) goto failure;
102 /* Allocate mem for array of TTextAttrs */
103 fdh->TAttrArray = AllocVec((fdh->NumEntries + numoutlineentries) * sizeof(struct TTextAttr), MEMF_ANY|MEMF_CLEAR);
104 if (!fdh->TAttrArray) goto failure;
106 tattr = fdh->TAttrArray;
108 while (numentries--)
111 /* Read the fontname */
113 STRPTR strptr;
114 UWORD len = 0; /* Length of fontname in file including null-termination */
116 strptr = strbuf;
120 if (!ReadByte( &DFB(DiskfontBase)->dsh, strptr, (void *)fh))
121 goto failure;
123 len ++;
125 while (*strptr ++);
127 /* We set tattr->tta_Name to the real font name, for
128 example FONTS:helvetica/15 */
130 if (!(tattr->tta_Name = AllocVec(len + 1, MEMF_ANY)))
131 goto failure;
133 strcpy(tattr->tta_Name, strbuf);
135 D(bug("ReadFontDescr: tta_Name \"%s\"\n", tattr->tta_Name));
137 /* Seek to the end of the fontnamebuffer ( - 2 if tagged) */
138 Flush(fh);
139 Seek
142 MAXFONTPATH - ( len + (fdh->Tagged ? 2 : 0) ),
143 OFFSET_CURRENT
147 /* Do special stuff for files with tags in them */
148 if (fdh->Tagged)
151 /* Next thing up is the tagcount */
152 if (!ReadWord(&DFB(DiskfontBase)->dsh, &numtags, (void *)fh))
153 goto failure;
156 if (numtags)
158 /* Seek back and read the tags. Note, that the TAG_DONE
159 tagitem "goes over" the numtags, ie. it's ti_Data will
160 contain the numtags in the lower WORD:
162 TAGLIST start
164 00000000 80000008 00370000 80000001
165 003F003F 80000002 00640064 00000000
166 00000004 00378022 \TAG_DONE
167 |||| ||||||||
168 TagCount ||||||Flags
169 ||||||
170 ||||Style
171 ||||
172 YSize
175 Flush(fh);
176 Seek
178 fh,
179 -(numtags * 8), /* sizeof (struct TagItem) = 8 */
180 OFFSET_CURRENT
183 if (!(tattr->tta_Tags = ReadTags(fh, numtags, DFB(DiskfontBase) )))
184 goto failure;
186 } /* if (numtags) */
187 else
188 tattr->tta_Tags = NULL;
190 } /* if (fdh->Tagged) */
191 else
192 tattr->tta_Tags = NULL;
194 D(bug("ReadFontDescr: font \"%s\" tags %p\n", tattr->tta_Name, tattr->tta_Tags));
196 /* Read the rest of the info */
197 if (!ReadWord( &DFB(DiskfontBase)->dsh, &(tattr->tta_YSize), (void *)fh ))
198 goto failure;
200 if (!ReadByte(&DFB(DiskfontBase)->dsh, &(tattr->tta_Style), (void *)fh ))
201 goto failure;
203 if (!ReadByte( &DFB(DiskfontBase)->dsh, &(tattr->tta_Flags), (void *)fh ))
204 goto failure;
206 tattr ++;
208 } /* while (numentries--) */
210 Close(fh); fh = 0;
212 if (numoutlineentries)
214 UBYTE flags = OTAG_GetFontFlags(fdh->OTagList, DiskfontBase);
215 UBYTE style = OTAG_GetFontStyle(fdh->OTagList, DiskfontBase);
217 while(numoutlineentries--)
219 UWORD size;
220 UWORD i;
221 BOOL exists = FALSE;
223 size = AROS_BE2WORD(*availsizes);
224 availsizes++;
226 /* Check if this size already exists as bitmap
227 font. If it does, then ignore it */
229 for(i = 0; i < fdh->NumEntries; i++)
231 if (fdh->TAttrArray[i].tta_YSize == size)
233 exists = TRUE;
234 break;
238 if (exists) continue;
240 /* NumEntries contains the total number of entries, ie.
241 bitmap fonts and outline fonts included. NumOutlineEntries
242 tells how many among these (at the end of the array) are
243 outline fonts */
245 fdh->NumEntries++;
246 fdh->NumOutlineEntries++;
248 tattr->tta_Name = fdh->OTagList->filename;
249 tattr->tta_YSize = size;
250 tattr->tta_Flags = flags;
251 tattr->tta_Style = style;
252 tattr->tta_Tags = NULL;
254 D(bug("ReadFontDescr: font \"%s\" tags %p\n", tattr->tta_Name, tattr->tta_Tags));
256 tattr++;
257 } /* while(numoutlineentries--) */
259 } /* if (numoutlineentries) */
261 ReturnPtr ("ReadFontDescr", struct FontDescrHeader *, fdh);
263 failure:
264 /* Free all allocated memory */
266 if (fh) Close(fh);
268 if (fdh)
269 FreeFontDescr(fdh, DFB(DiskfontBase) );
271 ReturnPtr ("ReadFontDescr", struct FontDescrHeader*, FALSE);
275 /****************************************************************************************/
277 /******************/
278 /* FreeFontDescr */
279 /******************/
281 /****************************************************************************************/
283 VOID FreeFontDescr(struct FontDescrHeader *fdh, struct DiskfontBase_intern *DiskfontBase)
285 struct TTextAttr *tattr;
287 UWORD numentries;
289 D(bug("FreeFontDescr(fdh=%p\n", fdh));
291 if (fdh)
293 tattr = fdh->TAttrArray;
294 numentries = fdh->NumEntries - fdh->NumOutlineEntries;
296 if (tattr)
298 /* Go through the whole array of bitmap font entries
299 freeing strings and tags. Outline font entries
300 always have the tta_Name pointing to the otag file
301 and must not get the tta_Name freed. They also have
302 tta_Tags always being NULL. Therefore the loop below
303 goes only through the bitmap font entries */
305 while(numentries--)
307 if (tattr->tta_Name)
308 FreeVec(tattr->tta_Name);
310 if (tattr->tta_Tags)
311 FreeVec(tattr->tta_Tags);
313 tattr ++;
316 FreeVec(fdh->TAttrArray);
319 if (fdh->OTagList) OTAG_KillFile(fdh->OTagList, DiskfontBase);
321 FreeMem(fdh, sizeof (struct FontDescrHeader));
324 ReturnVoid("FreeFontDescr");
327 /****************************************************************************************/