Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / diskfont / opendiskfont.c
blob3dadc622fc91fd6067fd9d9d2c386dc7fe8c2086
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Diskfont function OpenDiskFont()
6 Lang: english
7 */
9 #include <string.h>
10 #include <graphics/text.h>
11 #include <proto/dos.h>
12 #include <proto/utility.h>
13 #include <proto/graphics.h>
14 #include "diskfont_intern.h"
16 #include <aros/debug.h>
19 /*****************************************************************************
21 NAME */
22 #include <clib/diskfont_protos.h>
24 AROS_LH1(struct TextFont *, OpenDiskFont,
26 /* SYNOPSIS */
27 AROS_LHA(struct TextAttr *, textAttr, A0),
29 /* LOCATION */
30 struct Library *, DiskfontBase, 5, Diskfont)
32 /* FUNCTION
33 Tries to open the font specified by textAttr. If the font has allready
34 been loaded into memory, it will be opened with OpenFont(). Otherwise
35 OpenDiskFont() will try to load it from disk.
37 INPUTS
38 textAttr - Description of the font to load. If the textAttr->ta_Style
39 FSF_TAGGED bit is set, it will be treated as a struct TTextAttr.
42 RESULT
43 Pointer to a struct TextFont on success, 0 on failure.
45 NOTES
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 AvailFonts()
54 INTERNALS
55 Internally now the WeighTAMatch function is used to find the best
56 font match. In OS 3.0 and above this graphics function is made
57 private to the library. So in the future maybe this function can
58 be replaced by a more flexible internal mechanism.
60 *****************************************************************************/
62 AROS_LIBFUNC_INIT
64 WORD match_weight = 0, new_match_weight;
65 APTR iterator;
66 BOOL bestinmemory = TRUE;
67 struct TTextAttr *ttait;
68 struct TextFont *tf = NULL;
69 STRPTR filepart = FilePart(textAttr->ta_Name), wholename = textAttr->ta_Name;
70 ULONG len = strlen(filepart);
72 if (len>5 && strcasecmp(filepart+len-5,".font")==0)
73 len -= 5;
75 D(bug("OpenDiskFont(textAttr=%p)\n", textAttr));
76 D(bug("Name %s YSize %ld Style 0x%lx Flags 0x%lx\n",
77 textAttr->ta_Name,
78 textAttr->ta_YSize,
79 textAttr->ta_Style,
80 textAttr->ta_Flags));
82 /* Check if font is in memory, ignore path */
83 textAttr->ta_Name = filepart;
85 tf = OpenFont(textAttr);
86 if (tf!=NULL)
88 struct TTextAttr tattr;
90 tattr.tta_Name = tf->tf_Message.mn_Node.ln_Name;
91 tattr.tta_YSize = tf->tf_YSize;
92 tattr.tta_Style = tf->tf_Style;
93 tattr.tta_Flags = tf->tf_Flags;
94 if (ExtendFont(tf, NULL))
96 tattr.tta_Tags = TFE(tf->tf_Extension)->tfe_Tags;
97 if (tattr.tta_Tags) tattr.tta_Style |= FSF_TAGGED;
99 else
100 tattr.tta_Tags = NULL;
102 match_weight = WeighTAMatch(textAttr,
103 (struct TextAttr *)&tattr,
104 tattr.tta_Tags);
106 D(bug("OpenDiskFont: Found font in memory weight(%d)\n", match_weight));
108 else
109 D(bug("OpenDiskFont: No font found in memory\n"));
111 textAttr->ta_Name = wholename;
113 if (match_weight!=MAXFONTMATCHWEIGHT)
115 iterator = DF_IteratorInit((struct TTextAttr *)textAttr, DFB(DiskfontBase));
116 if (iterator == NULL)
117 D(bug("OpenDiskFont: Error initializing Diskfont Iterator\n"));
118 else
120 while ((ttait = DF_IteratorGetNext(iterator, DFB(DiskfontBase)))!=NULL)
122 ULONG len2 = strlen(ttait->tta_Name) - 5;
124 D(bug("OpenDiskFont: Checking font: %s(%d)\n", ttait->tta_Name, ttait->tta_YSize));
126 D(bug("len: %d len2: %d\n", len, len2));
128 if ((len == len2) && (strncasecmp(ttait->tta_Name, filepart, len) == 0))
130 new_match_weight = WeighTAMatch(textAttr,
131 (struct TextAttr *)ttait,
132 ttait->tta_Tags);
134 if (new_match_weight > match_weight)
136 match_weight = new_match_weight;
137 DF_IteratorRemember(iterator, DFB(DiskfontBase));
138 bestinmemory = FALSE;
139 D(bug("Better weight (%d)\n", match_weight));
141 else
142 D(bug("No better weight (%d)\n", new_match_weight));
144 if (match_weight==MAXFONTMATCHWEIGHT)
145 break;
149 /* Best still in memory then open this font, otherwise load from disk */
150 if (!bestinmemory)
152 if (tf!=NULL)
153 CloseFont(tf);
154 tf = DF_IteratorRememberOpen(iterator, DFB(DiskfontBase));
156 DF_IteratorFree(iterator, DFB(DiskfontBase));
160 if (tf)
162 D(bug("extend font\n"));
163 if (ExtendFont(tf, NULL))
165 #warning CHECKME
166 TFE(tf->tf_Extension)->tfe_Flags0 |= TE0F_NOREMFONT;
170 ReturnPtr("OpenDiskFont", struct TextFont *, tf);
172 AROS_LIBFUNC_EXIT
174 } /* OpenDiskFont */