2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Functions for reading disk font files.
8 /****************************************************************************************/
11 #include <exec/execbase.h>
12 #include <exec/initializers.h>
13 #include <dos/doshunks.h>
14 #include <dos/dosasl.h>
15 #include <dos/dosextens.h>
18 #include <proto/dos.h>
19 #include <proto/graphics.h>
21 #include "diskfont_intern.h"
23 /****************************************************************************************/
26 #include <aros/debug.h>
28 /****************************************************************************************/
30 #define SKIPLONG(ptr) ptr += sizeof (LONG);
32 #define SKIPWORD(ptr) ptr += sizeof (WORD)
34 #define SKIPBYTE(ptr) ptr ++;
36 #define SKIPPTR(ptr) ptr += sizeof(LONG)
38 #define CONVLONG(ptr, destlong) \
39 destlong = ptr[0] << 24 |ptr[1] << 16 | ptr[2] << 8 | ptr[3]; \
42 #define CONVWORD(ptr, destword) \
43 destword = ptr[0] << 8 | ptr[1]; \
47 #define CONVBYTE(ptr, destbyte) \
52 We don't need endian conversion of pointers since this is done inside LoadSeg_AOS()
53 #define CONVPTR(ptr, destptr) \
54 ((APTR)destptr) = (APTR)(ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3]); \
59 #define COPYPTR(ptr, destptr) \
60 ((APTR)destptr) = (APTR)(ptr[0] | ptr[1] << 8 | ptr [2] << 16 | ptr[3] << 24); \
64 #define COPYPTR(ptr,destptr) \
65 (destptr) = (APTR)(*((ULONG *)(ptr))); \
68 /****************************************************************************************/
74 /****************************************************************************************/
76 struct DiskFontHeader
*ConvDiskFont(BPTR seglist
, CONST_STRPTR fontname
, BOOL doextend
,
77 struct DiskfontBase_intern
*DiskfontBase
)
79 UWORD count
, numchars
;
87 struct DiskFontHeader tmp_dfh
, *dfh
= 0;
88 struct TextFont
*tf
= 0;
90 APTR ctf_chardata_ptrs
[8] = {0};
91 struct ColorFontColors
*cfc_ptr
= 0;
92 UWORD
*colortable_ptr
= NULL
;
94 APTR chardata_ptr
= NULL
,
100 BPTR fontsegment
= NULL
;
101 BOOL fontextended
= FALSE
;
103 CONST_STRPTR filepart
;
105 EnterFunc(bug("ConvDiskFont(seglist=%p, fontname=%s)\n", seglist
, fontname
));
107 /* Clear temporary diskfontheader struct */
108 memset(&tmp_dfh
, 0, sizeof (struct DiskFontHeader
));
110 /* Get start of diskfontheader. (Go past the dummy exe header) */
111 ptr
= UB(BADDR(seglist
)) + sizeof (ULONG
) + sizeof(BPTR
);
113 /* Skip the whole DiskFontHeader */
114 SKIPPTR(ptr
); /* dfh_TF.ln_Succ */
115 SKIPPTR(ptr
); /* dfh_TF.ln_Pred */
116 CONVBYTE(ptr
, tmp_dfh
.dfh_DF
.ln_Type
); /* dfh_TF.ln_Type */
117 CONVBYTE(ptr
, tmp_dfh
.dfh_DF
.ln_Pri
); /* dfh_TF.ln_Pri */
118 SKIPPTR(ptr
); /* dfh_TF.ln_Name */
119 CONVWORD(ptr
, tmp_dfh
.dfh_FileID
); /* dfh_FileID */
121 if (tmp_dfh
.dfh_FileID
!= DFH_ID
)
124 CONVWORD(ptr
, tmp_dfh
.dfh_Revision
); /* dfh_Revision */
126 COPYPTR(ptr
, taglist_ptr
); /* dfh_Segment */
132 /* Skip nodes successor and predecessor field */
136 /* skip type and pri */
139 tmp_dfh
.dfh_TF
.tf_Message
.mn_Node
.ln_Type
= NT_FONT
;
141 /* Skip name pointer, replyport and msg length */
146 CONVWORD(ptr
, tmp_dfh
.dfh_TF
.tf_YSize
);
147 CONVBYTE(ptr
, tmp_dfh
.dfh_TF
.tf_Style
);
148 CONVBYTE(ptr
, tmp_dfh
.dfh_TF
.tf_Flags
);
149 CONVWORD(ptr
, tmp_dfh
.dfh_TF
.tf_XSize
);
150 CONVWORD(ptr
, tmp_dfh
.dfh_TF
.tf_Baseline
);
151 CONVWORD(ptr
, tmp_dfh
.dfh_TF
.tf_BoldSmear
);
152 SKIPWORD(ptr
); /* tf_Accessors */
153 CONVBYTE(ptr
, tmp_dfh
.dfh_TF
.tf_LoChar
);
154 CONVBYTE(ptr
, tmp_dfh
.dfh_TF
.tf_HiChar
);
155 COPYPTR(ptr
, chardata_ptr
); /* tf_CharData */
156 CONVWORD(ptr
, tmp_dfh
.dfh_TF
.tf_Modulo
);
157 COPYPTR(ptr
, charloc_ptr
); /* tf_CharLoc */
158 COPYPTR(ptr
, charspace_ptr
); /* tf_CharSpace */
159 COPYPTR(ptr
, charkern_ptr
); /* tf_CharKern */
161 D(bug("Textfont struct converted\n"));
162 D(bug("YSize: %d\n", tmp_dfh
.dfh_TF
.tf_YSize
));
163 D(bug("Style: %d\n", tmp_dfh
.dfh_TF
.tf_Style
));
164 D(bug("Flags: %d\n", tmp_dfh
.dfh_TF
.tf_Flags
));
165 D(bug("XSize: %d\n", tmp_dfh
.dfh_TF
.tf_XSize
));
166 D(bug("Baseline: %d\n", tmp_dfh
.dfh_TF
.tf_Baseline
));
167 D(bug("Boldsmear: %d\n", tmp_dfh
.dfh_TF
.tf_BoldSmear
));
168 D(bug("LoChar: %d\n", tmp_dfh
.dfh_TF
.tf_LoChar
));
169 D(bug("HiChar: %d\n", tmp_dfh
.dfh_TF
.tf_HiChar
));
170 D(bug("chardara: %p\n", chardata_ptr
));
171 D(bug("Modulo: %d\n", tmp_dfh
.dfh_TF
.tf_Modulo
));
172 D(bug("charloc: %p\n", charloc_ptr
));
173 D(bug("charspace: %p\n", charspace_ptr
));
174 D(bug("charkern: %p\n", charkern_ptr
));
176 /* Allocate memory for font */
178 i
= sizeof(struct DiskFontHeader
);
179 if (tmp_dfh
.dfh_TF
.tf_Style
& FSF_COLORFONT
)
181 /* +8 should this calc. not work 100 % because of alignments */
182 i
+= (sizeof(struct ColorTextFont
) - sizeof(struct TextFont
) + 8);
185 dfh
= prevsegment
= AllocSegment(prevsegment
, i
, MEMF_ANY
| MEMF_CLEAR
, DiskfontBase
);
186 if (!dfh
) goto failure
;
188 fontsegment
= (BPTR
)MAKE_REAL_SEGMENT(dfh
);
191 /* Don't do this in case of doextend = FALSE (called by NewFontContents),
192 because then code expects taglist to be in there!!!! (dfh_Segment == dfh_TagList) */
194 tmp_dfh
.dfh_Segment
= fontsegment
;
199 D(bug("charkern in temp: %p\n", tmp_dfh
.dfh_TF
.tf_CharKern
));
201 /* Copy already converted stuff into allocated mem */
202 CopyMem(&tmp_dfh
, dfh
, sizeof (struct DiskFontHeader
));
204 D(bug("tmp_tf copied, charkern=%p\n", tf
->tf_CharKern
));
206 /* Calculate size of one character data bitmap */
207 chardatasize
= tf
->tf_YSize
* tf
->tf_Modulo
;
209 numchars
= (tf
->tf_HiChar
- tf
->tf_LoChar
) + 2; /* + 2 because of default character (255) */
211 if (tf
->tf_Style
& FSF_COLORFONT
)
214 UBYTE num_planes_data
= 0;
217 #define CTF(tf) ((struct ColorTextFont *)tf)
219 D(bug("Colorfont found\n"));
221 /* Convert extended colortextfont info */
222 CONVWORD(ptr
, CTF(tf
)->ctf_Flags
);
223 CONVBYTE(ptr
, CTF(tf
)->ctf_Depth
);
224 CONVBYTE(ptr
, CTF(tf
)->ctf_FgColor
);
225 CONVBYTE(ptr
, CTF(tf
)->ctf_Low
);
226 CONVBYTE(ptr
, CTF(tf
)->ctf_High
);
227 CONVBYTE(ptr
, CTF(tf
)->ctf_PlanePick
);
228 CONVBYTE(ptr
, CTF(tf
)->ctf_PlaneOnOff
);
229 COPYPTR(ptr
, cfc_ptr
);
231 for (i
= 0; i
< 8; i
++ )
233 COPYPTR(ptr
, ctf_chardata_ptrs
[i
]);
235 if ((CTF(tf
)->ctf_Depth
> i
) && (CTF(tf
)->ctf_PlanePick
& (1L << i
)))
242 /* ------------------------------- */
243 /* Handle ColorFontColors structure */
245 #define CFC(p) ((struct ColorFontColors*)p)
247 CTF(tf
)->ctf_ColorFontColors
= prevsegment
= AllocSegment(prevsegment
,
248 sizeof(struct ColorFontColors
),
249 MEMF_ANY
| MEMF_CLEAR
,
252 if (!CTF(tf
)->ctf_ColorFontColors
) goto failure
;
254 temp_ptr
= CTF(tf
)->ctf_ColorFontColors
;
256 ptr
= (UBYTE
*)cfc_ptr
;
258 CONVWORD(ptr
, CFC(temp_ptr
)->cfc_Count
);
259 COPYPTR (ptr
, colortable_ptr
);
261 /* ------------------------------- */
262 /* Handle colortable */
263 count
= CFC(temp_ptr
)->cfc_Count
;
264 ptr
= (UBYTE
*)colortable_ptr
;
266 CFC(temp_ptr
)->cfc_ColorTable
= prevsegment
= AllocSegment(prevsegment
,
267 count
* sizeof (UWORD
),
271 if (!CFC(temp_ptr
)->cfc_ColorTable
) goto failure
;
273 for (i
= 0; i
< count
; i
++)
275 CONVWORD(ptr
, CFC(temp_ptr
)->cfc_ColorTable
[i
]);
278 /* ------------------------------- */
279 /* Handle character bitmap data for colorfonts */
280 for (i
= 0; i
< num_planes_data
; i
++)
282 if (!ctf_chardata_ptrs
[i
]) continue;
284 CTF(tf
)->ctf_CharData
[i
] = prevsegment
= AllocSegment(prevsegment
,
289 if (!CTF(tf
)->ctf_CharData
[i
]) goto failure
;
291 CopyMem(ctf_chardata_ptrs
[i
], CTF(tf
)->ctf_CharData
[i
], chardatasize
);
297 if (chardata_ptr
) /* CHECKME: check necessary? Do also colorfonts always have this? */
299 D(bug("B&W font\t Chardatasize: %d\n", chardatasize
));
301 /* Character data for B/W fonts */
302 tf
->tf_CharData
= prevsegment
= AllocSegment(prevsegment
,
306 if (!tf
->tf_CharData
) goto failure
;
309 D(bug("chardataptr=%p\n",chardata_ptr
));
310 CopyMem(chardata_ptr
, tf
->tf_CharData
, chardatasize
);
311 D(bug("Chardata copied\n"));
314 /* ----------------------- */
316 filepart
= FilePart(fontname
);
317 i
= strlen(filepart
) + 1;
318 if (i
>= sizeof(dfh
->dfh_Name
)) i
= sizeof(dfh
->dfh_Name
) - 1;
320 CopyMem((STRPTR
) filepart
, dfh
->dfh_Name
, i
);
322 tf
->tf_Message
.mn_Node
.ln_Name
= dfh
->dfh_Name
;
323 dfh
->dfh_DF
.ln_Name
= dfh
->dfh_Name
;
325 /* ----------------------- */
326 /* Allocate memory for charloc */
328 D(bug("Doing charloc\n"));
330 tf
->tf_CharLoc
= prevsegment
= AllocSegment(prevsegment
,
331 numchars
* sizeof (ULONG
),
335 if (!tf
->tf_CharLoc
) goto failure
;
337 /* Convert charloc data */
339 destlptr
= (ULONG
*) tf
->tf_CharLoc
;
340 for (i
= 0; i
< numchars
; i
++ )
342 CONVLONG(ptr
, *destlptr
++);
343 D(bug("charloc[%d]: %x\n", i
, destlptr
[-1]));
345 D(bug("Charloc OK\n"));
350 D(bug("Proportional font\n"));
352 tf
->tf_CharSpace
= prevsegment
= AllocSegment(prevsegment
,
353 numchars
* sizeof (UWORD
) ,
357 if (!tf
->tf_CharSpace
) goto failure
;
359 /* Convert charspace data */
361 destptr
= tf
->tf_CharSpace
;
362 for (i
= numchars
; i
--;)
364 CONVWORD(ptr
, *destptr
++ );
367 D(bug("Charspace OK\n"));
370 /* ----------------------- */
371 /* Allocate memory for charkern */
373 D(bug("Doing Charkern, ptr =%p\n", charkern_ptr
));
376 tf
->tf_CharKern
= prevsegment
= AllocSegment(prevsegment
,
377 numchars
* sizeof (UWORD
),
380 if (!tf
->tf_CharKern
) goto failure
;
382 /* Convert charkern data */
384 destptr
= tf
->tf_CharKern
;
385 for (i
= numchars
; i
--;)
387 CONVWORD(ptr
, *destptr
++);
388 D(bug("Setting to %d\n", destptr
[-1]));
390 D(bug("Charkern OK\n"));
393 D(bug("Charkern, ptr =%p\n", tf
->tf_CharKern
));
396 /* ----------------------- */
399 if ((tf
->tf_Style
& FSF_TAGGED
) && taglist_ptr
)
403 struct TagItem
*taglist
;
405 D(bug("Tagged font\n"));
407 /* Convert the tags */
410 /* We assume that tags are placed in one single array, and not
411 spread around the whole file with TAG_NEXT
414 /* Count number of tags w/TAG_DONE */
421 while (tag
!= TAG_DONE
);
423 /* Allocate memory for taglist */
424 prevsegment
= taglist
= (struct TagItem
*)AllocSegment(prevsegment
,
425 numtags
* sizeof(struct TagItem
),
431 /* Copy tags into allocated mem */
433 for (i
= 0; i
< numtags
; i
++ )
435 CONVLONG(ptr
, taglist
[i
].ti_Tag
);
436 CONVLONG(ptr
, taglist
[i
].ti_Data
);
441 if (ExtendFont(tf
, taglist
))
452 dfh
->dfh_TagList
= taglist
;
457 D(bug("No tags, extending it\n"));
459 if (ExtendFont(tf
, NULL
))
469 /* ----------------------- */
471 ReturnPtr("ConvTextFont", struct DiskFontHeader
*, dfh
);
479 if (fontextended
) StripFont(tf
);
481 UnLoadSeg(fontsegment
);
484 ReturnPtr("ConvTextFont", struct DiskFontHeader
*, 0);
487 void DisposeConvDiskFont(struct DiskFontHeader
*dfh
,
488 struct DiskfontBase_intern
*DiskfontBase
)
492 StripFont(&dfh
->dfh_TF
);
493 UnLoadSeg(MKBADDR(((BPTR
*)dfh
)-1));
497 /****************************************************************************************/
503 /****************************************************************************************/
505 struct TextFont
*ReadDiskFont(
506 struct TTextAttr
*reqattr
,
507 CONST_STRPTR realfontname
,
508 struct DiskfontBase_intern
*DiskfontBase
)
510 struct DiskFontHeader
*dfh
= NULL
;
514 EnterFunc(bug("ReadDiskFont(reqattr=%p, name=%s, ysize=%d)\n",
515 reqattr
, reqattr
->tta_Name
, reqattr
->tta_YSize
));
517 filename
= reqattr
->tta_Name
;
519 if ((seglist
= LoadSeg(filename
)) != 0)
521 dfh
= ConvDiskFont(seglist
, realfontname
, TRUE
, DiskfontBase
);
526 D(bug("ReadDiskFont: dfh=0x%lx\n"));
527 ReturnPtr("ReadDiskFont", struct TextFont
*, &dfh
->dfh_TF
);
531 D(bug("ReadDiskFont: error converting seglist\n"));
532 ReturnPtr("ReadDiskFont", struct TextFont
*, NULL
);
537 D(bug("Could not load segment list\n"));
538 ReturnPtr("ReadDiskFont", struct TextFont
*, NULL
);
542 /****************************************************************************************/