revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / diskfont / availfonts.c
blobe00731e125c628977e100a0361e1f438b9df10e0
1 /*
2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Main fileof diskfont function AvailFonts()
6 Lang: english
7 */
10 #include "diskfont_intern.h"
11 #include <string.h>
13 #include <aros/debug.h>
15 /****************************************************************************/
17 struct BufferInfo
19 struct AvailFontsHeader *afh;
20 LONG space;
21 union {
22 struct TAvailFonts *taf;
23 struct AvailFonts *af;
24 } u;
25 UBYTE *endptr;
29 STATIC struct BufferInfo *BufferInfoCreate(STRPTR buffer, LONG bufBytes, BOOL tagged, struct DiskfontBase *DiskfontBase);
30 STATIC VOID BufferInfoAdd(struct BufferInfo *bi, UWORD type, struct TTextAttr *tattr, BOOL tagged, struct DiskfontBase *DiskfontBase);
31 STATIC VOID BufferInfoFree(struct BufferInfo *bi, struct DiskfontBase *DiskfontBase);
33 /*****************************************************************************
35 NAME */
36 #include <clib/diskfont_protos.h>
38 AROS_LH3(LONG, AvailFonts,
40 /* SYNOPSIS */
41 AROS_LHA(STRPTR, buffer, A0),
42 AROS_LHA(LONG , bufBytes, D0),
43 AROS_LHA(LONG , flags, D1),
45 /* LOCATION */
46 struct Library *, DiskfontBase, 6, Diskfont)
48 /* FUNCTION
49 Fill the supplied buffer with info about the available fonts.
50 The buffer will after function execution first contains a
51 struct AvailFontsHeader, and then an array of struct AvailFonts
52 element (or TAvailFonts elements if AFF_TAGGED is specified in the
53 flags parameter). If the buffer is not big enough for the
54 descriptions than the additional length needed will be returned.
56 INPUTS
57 buffer - pointer to a buffer in which the font descriptions
58 should be placed.
60 bufBytes - size of the supplied buffer.
62 flags - flags telling what kind of fonts to load,
63 for example AFF_TAGGED for tagged fonts also,
64 AFF_MEMORY for fonts in memory, AFF_DISK for fonts
65 on disk.
67 RESULT
68 shortage - 0 if buffer was big enough or a number telling
69 how much additional place is needed.
71 NOTES
72 If the routine failes, then the afh_Numentries field
73 in the AvailFontsHeader will be 0.
75 EXAMPLE
77 BUGS
79 SEE ALSO
80 OpenDiskfont(), <diskfont/diskfont.h>
82 INTERNALS
84 HISTORY
85 27-11-96 digulla automatically created from
86 diskfont_lib.fd and clib/diskfont_protos.h
88 *****************************************************************************/
90 AROS_LIBFUNC_INIT
92 LONG retval = 0;
93 APTR iterator;
94 struct TTextAttr *attr;
95 BOOL tagged = (flags & AFF_TAGGED) ? TRUE : FALSE;
96 struct BufferInfo *bi;
98 D(bug("AvailFonts(buffer=%p, bufbytes=%d,flags=%d)\n", buffer, bufBytes, flags));
100 bi = BufferInfoCreate(buffer, bufBytes, tagged, DFB(DiskfontBase));
102 if (flags & AFF_MEMORY)
104 iterator = MF_IteratorInit(DFB(DiskfontBase));
105 while((attr = MF_IteratorGetNext(iterator, DFB(DiskfontBase)))!=NULL)
107 if ((!IS_SCALED_FONT(attr) || (flags & AFF_SCALED))
108 && !(IS_OUTLINE_FONT(attr) && (flags & AFF_BITMAP)))
110 /* TODO: CHECKME */
112 /* taf_Type only ever seems to contain one of AFF_MEMORY/AFF_DISK/AFF_SCALED,
113 but not a combination of these. */
114 UWORD type = IS_SCALED_FONT(attr) ? AFF_SCALED : AFF_MEMORY;
116 BufferInfoAdd(bi, type, attr, tagged, DFB(DiskfontBase));
119 MF_IteratorFree(iterator, DFB(DiskfontBase));
122 if (flags & AFF_DISK)
124 iterator = DF_IteratorInit(NULL, DFB(DiskfontBase));
125 while((attr = DF_IteratorGetNext(iterator, DFB(DiskfontBase)))!=NULL)
127 if ((!IS_SCALED_FONT(attr) || (flags & AFF_SCALED))
128 && !(IS_OUTLINE_FONT(attr) && (flags & AFF_BITMAP)))
130 /* TODO: CHECKME */
131 /* For disk fonts the type is always AFF_DISK ??? */
132 BufferInfoAdd(bi, AFF_DISK, attr, tagged, DFB(DiskfontBase));
135 DF_IteratorFree(iterator, DFB(DiskfontBase));
138 retval = bi->space>=0 ? 0 : -bi->space;
140 BufferInfoFree(bi, DFB(DiskfontBase));
142 ReturnInt ("AvailFonts", ULONG, retval);
144 AROS_LIBFUNC_EXIT
146 } /* AvailFonts */
149 /*****************************************************************************/
151 STATIC struct BufferInfo *BufferInfoCreate(STRPTR buffer, LONG bufBytes, BOOL tagged, struct DiskfontBase *DiskfontBase)
153 struct BufferInfo *retval;
155 retval = (struct BufferInfo *)AllocMem(sizeof(struct BufferInfo), MEMF_ANY | MEMF_CLEAR);
156 if (retval != NULL)
158 retval->afh = (struct AvailFontsHeader *)buffer;
159 /* FIXME: what if bufBytes < sizeof (struct AvailFontsHeader) ? */
160 if (buffer != NULL)
162 retval->afh->afh_NumEntries = 0;
164 retval->space = bufBytes-sizeof(struct AvailFontsHeader);
165 if (tagged)
166 retval->u.taf = (struct TAvailFonts *)(retval->afh+1);
167 else
168 retval->u.af = (struct AvailFonts *)(retval->afh+1);
169 retval->endptr = (UBYTE *)buffer + bufBytes;
172 return retval;
176 STATIC VOID BufferInfoAdd(struct BufferInfo *bi, UWORD type, struct TTextAttr *tattr, BOOL tagged, struct DiskfontBase *DiskfontBase)
178 if (tagged && tattr->tta_Tags!=NULL)
179 bi->space -= sizeof(struct TAvailFonts) + strlen(tattr->tta_Name)+1 + NumTags(tattr->tta_Tags, DiskfontBase)*sizeof(struct TagItem);
180 else
181 bi->space -= sizeof(struct AvailFonts) + strlen(tattr->tta_Name)+1;
183 if (bi->space >= 0)
185 bi->endptr -= strlen(tattr->tta_Name)+1;
186 strcpy(bi->endptr, tattr->tta_Name);
188 if (tagged)
190 LONG size;
192 bi->u.taf->taf_Type = type;
193 bi->u.taf->taf_Attr.tta_Name = bi->endptr;
194 bi->u.taf->taf_Attr.tta_YSize = tattr->tta_YSize;
195 bi->u.taf->taf_Attr.tta_Style = tattr->tta_Style;
196 bi->u.taf->taf_Attr.tta_Flags = tattr->tta_Flags;
198 if (tattr->tta_Tags!=NULL)
200 size = NumTags(tattr->tta_Tags, DiskfontBase)*sizeof(struct TagItem);
201 bi->endptr -= size;
202 memcpy(bi->endptr, tattr->tta_Tags, size);
203 bi->u.taf->taf_Attr.tta_Tags = (struct TagItem *)bi->endptr;
205 else
206 bi->u.taf->taf_Attr.tta_Tags = NULL;
208 bi->u.taf++;
210 else
212 bi->u.af->af_Type = type;
213 bi->u.af->af_Attr.ta_Name = bi->endptr;
214 bi->u.af->af_Attr.ta_YSize = tattr->tta_YSize;
215 bi->u.af->af_Attr.ta_Style = tattr->tta_Style;
216 bi->u.af->af_Attr.ta_Flags = tattr->tta_Flags;
218 bi->u.af++;
221 bi->afh->afh_NumEntries++;
226 STATIC VOID BufferInfoFree(struct BufferInfo *bi, struct DiskfontBase *DiskfontBase)
228 FreeMem(bi, sizeof(struct BufferInfo));
231 /*****************************************************************************/