using kprintf to output debug, which may be redirected to serial, is not a good idea...
[AROS-Contrib.git] / MultiMedia / AMP2 / amigaos / font.c
blob4703004a65eb63ee75e7bb5b00df3167879050a3
1 /*
3 * font.c
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
12 #include <exec/types.h>
13 #include <exec/nodes.h>
14 #include <exec/lists.h>
15 #include <exec/memory.h>
17 #include <proto/diskfont.h>
19 #if !defined(__AROS__)
20 #include <powerup/ppcproto/intuition.h>
21 #include <powerup/ppclib/interface.h>
22 #include <powerup/ppclib/time.h>
23 #include <powerup/gcclib/powerup_protos.h>
24 #include <powerup/ppcproto/exec.h>
25 #include <powerup/ppcinline/graphics.h>
26 #include <powerup/ppcproto/dos.h>
27 #include <powerup/ppcproto/asl.h>
28 #include <powerup/ppcproto/gadtools.h>
29 #else
31 #include "aros-inc.h"
33 #endif
35 #include <graphics/displayinfo.h>
37 #include "../refresh/osd.h"
39 #if !defined(__AROS__)
40 extern struct Library *GfxBase;
41 #endif
42 extern struct Library *DiskfontBase;
44 struct charDef
46 WORD charOffset,charBitWidth;
49 struct Character
51 UWORD chr_Width,chr_Height;
52 UBYTE *chr_Data;
55 #define GETBIT(buffer,offset) \
56 ((((*((UBYTE *)(buffer)+((offset)>>3)))&(128>>((offset)&7))) != 0) \
57 ? 1 : 0)
59 static ULONG Proportional, Normal, Bold, Italic, ULine, Extended, Reversed;
60 static ULONG Height, Width, Baseline, Smear, Aspect;
61 static WORD SpaceTable[257], KernTable[257];
62 static ULONG FirstChar, LastChar, UseTable;
63 static struct TextFont *NewFont = NULL;
64 static struct Character CharBuffer[257];
65 static struct FontRequester *FontReq = NULL;
66 static struct TextAttr topaz8 = {"topaz.font", 8, FS_NORMAL, FPF_ROMFONT};
68 BOOL UnpackChar(struct Character *chr,struct TextFont *font,ULONG i)
70 struct charDef *def;
71 ULONG j,k,mod,off,width,modj,widthj;
72 UBYTE *data;
74 mod = font->tf_Modulo;
75 data = (UBYTE *)font->tf_CharData;
76 def = ((struct charDef *)(font->tf_CharLoc))+i;
77 off = def->charOffset;
78 chr->chr_Width = def->charBitWidth;
79 if ((width = chr->chr_Width) > 0)
81 if ((chr->chr_Data = AllocVec(chr->chr_Width*chr->chr_Height,MEMF_ANY))
82 == NULL) return (FALSE);
83 for (j = 0; j < chr->chr_Height; j++)
85 modj = mod*j;
86 widthj = width*j;
87 for (k = 0; k < width; k++)
88 *(chr->chr_Data+widthj+k) = GETBIT(data+modj,off+k);
91 return (TRUE);
94 void KernTables(ULONG to, ULONG from)
96 if (NewFont->tf_CharSpace)
97 SpaceTable[to] = *((WORD *)(NewFont->tf_CharSpace)+from);
98 if (NewFont->tf_CharKern)
99 KernTable[to] = *((WORD *)(NewFont->tf_CharKern)+from);
100 if ((NewFont->tf_CharSpace) || (NewFont->tf_CharKern))
101 UseTable = TRUE;
102 else
103 UseTable = FALSE;
106 void ClearCurrentFont(void)
108 struct Character *chr;
110 for (chr = CharBuffer; chr < CharBuffer+257; chr++)
112 if (chr->chr_Data) FreeVec(chr->chr_Data);
113 chr->chr_Data = NULL;
117 BOOL LoadFont(struct TextAttr *font, ULONG width)
119 ULONG i;
120 struct TTextAttr tagfont;
121 ULONG tags[3];
123 if (width > 0)
125 CopyMem(font,&tagfont,sizeof(struct TextAttr));
126 tagfont.tta_Flags &= ~FPF_DESIGNED;
127 tagfont.tta_Style |= FSF_TAGGED;
128 tagfont.tta_Tags = (struct TagItem *)tags;
129 tags[0] = TA_DeviceDPI;
130 tags[1] = (width<<16)+tagfont.tta_YSize;
131 tags[2] = TAG_DONE;
132 NewFont = OpenDiskFont((struct TextAttr *)&tagfont);
134 else NewFont = OpenDiskFont(font);
136 if (NewFont == NULL)
138 printf("failed to load font\n");
139 return FALSE;
141 ClearCurrentFont();
142 Height = NewFont->tf_YSize;
143 Width = NewFont->tf_XSize;
144 Baseline = NewFont->tf_Baseline;
145 Smear = NewFont->tf_BoldSmear;
146 FirstChar = NewFont->tf_LoChar;
147 LastChar = NewFont->tf_HiChar;
148 Proportional = ((NewFont->tf_Flags & FPF_PROPORTIONAL) != 0);
149 Bold = ((NewFont->tf_Style & FSF_BOLD) != 0);
150 Italic = ((NewFont->tf_Style & FSF_ITALIC) != 0);
151 ULine = ((NewFont->tf_Style & FSF_UNDERLINED) != 0);
152 Extended = ((NewFont->tf_Style & FSF_EXTENDED) != 0);
153 Normal = ((Bold | Italic | ULine | Extended) == 0);
154 Reversed = ((NewFont->tf_Flags & FPF_REVPATH) != 0);
155 Aspect = 0;
156 if (NewFont->tf_Flags & FPF_TALLDOT) Aspect = 1;
157 if (NewFont->tf_Flags & FPF_WIDEDOT) Aspect = 2;
159 for (i = 0; i < 257; i++)
161 (CharBuffer+i)->chr_Width = (((struct charDef *)(NewFont->tf_CharLoc))
162 +LastChar-FirstChar+1)->charBitWidth;
163 (CharBuffer+i)->chr_Height = NewFont->tf_YSize;
164 SpaceTable[i] = NewFont->tf_XSize;
165 KernTable[i] = 0;
166 KernTables(i,LastChar-FirstChar+1);
168 for (i = 0; i < LastChar-FirstChar+1; i++)
170 if (!UnpackChar(CharBuffer+FirstChar+i,NewFont,i)) {
171 printf("out of memory\n");
172 return FALSE;
174 KernTables(FirstChar+i,i);
176 /* copy font stuff to OSD font */
177 osd_chars[i+FirstChar].width = ((struct Character *)(CharBuffer+FirstChar+i))->chr_Width;
178 osd_chars[i+FirstChar].height = ((struct Character *)(CharBuffer+FirstChar+i))->chr_Height;
179 osd_chars[i+FirstChar].data = ((struct Character *)(CharBuffer+FirstChar+i))->chr_Data;
180 osd_chars[i+FirstChar].space = SpaceTable[i+FirstChar];
181 osd_chars[i+FirstChar].kern = KernTable[i+FirstChar];
183 UnpackChar(CharBuffer+256,NewFont,LastChar-FirstChar+1);
184 KernTables(256,LastChar-FirstChar+1);
185 CloseFont(NewFont); NewFont = NULL;
186 return TRUE;
189 static char font_title[] = "Select subtitle font";
191 int osd_font_init(void)
193 int use_fallback = 1;
195 memset(osd_chars, 0, sizeof(osd_chars));
196 memset(SpaceTable, 0, sizeof(SpaceTable));
197 memset(KernTable, 0, sizeof(KernTable));
198 memset(CharBuffer, 0, sizeof(CharBuffer));
200 FontReq = AllocAslRequestTags(ASL_FontRequest, TAG_END);
201 if (FontReq == NULL) {
202 printf("failed to allocate font requester\n");
203 } else {
204 if (AslRequestTags(FontReq, ASLFO_TitleText, font_title, ASLFO_MaxHeight, 144, TAG_END)) {
205 LoadFont(&FontReq->fo_Attr, 0);
206 use_fallback = 0;
208 FreeAslRequest(FontReq);
209 FontReq = NULL;
212 if (use_fallback) {
213 LoadFont(&topaz8, 0);
216 return 0;
219 void osd_font_exit(void)
221 ClearCurrentFont();