some fixes to accented characters
[tangerine.git] / rom / graphics / fontsupport.c
blob4c8fa3fb431aed6e6a9f69088a98cf44fa5bf56f
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Misc font help funcs
6 Lang: english
7 */
9 #include <proto/exec.h>
10 #include <proto/oop.h>
11 #include <proto/graphics.h>
13 #include <exec/memory.h>
14 #include <graphics/text.h>
16 #include "graphics_intern.h"
17 #include "gfxfuncsupport.h"
18 #include "fontsupport.h"
20 /****************************************************************************************/
22 ULONG CalcHashIndex(ULONG n)
24 UBYTE Index = (n & 0xff) +
25 ((n >> 8) & 0xff) +
26 ((n >> 16) & 0xff) +
27 ((n >> 24) & 0xff);
28 Index &= 0x07;
30 return Index;
32 /****************************************************************************************/
34 /* Functions for solving the TextFontExtension problem using hashes */
36 #define GFBI(x) ((struct GfxBase_intern *)x)
38 /****************************************************************************************/
40 static inline ULONG tfe_calchashidx(APTR ptr)
42 ULONG val = (ULONG)ptr;
44 ULONG idx = (val & 0xff) +
45 ((val >> 8) & 0xff) +
46 ((val >> 16) & 0xff) +
47 ((val >> 24) & 0xff);
49 idx &= (TFE_HASHTABSIZE - 1);
51 return idx;
54 /****************************************************************************************/
56 struct tfe_hashnode *tfe_hashlookup(struct TextFont *tf, struct GfxBase *GfxBase)
59 ULONG idx = tfe_calchashidx(tf);
60 struct tfe_hashnode *n = NULL;
63 ObtainSemaphoreShared( &GFBI(GfxBase)->tfe_hashtab_sema );
65 for ( n = GFBI(GfxBase)->tfe_hashtab[idx]; n; n = n->next)
67 if (n->back == tf)
69 break;
73 ReleaseSemaphore( &GFBI(GfxBase)->tfe_hashtab_sema );
75 return n;
78 /****************************************************************************************/
80 struct tfe_hashnode *tfe_hashnode_create(struct GfxBase *GfxBase)
82 struct tfe_hashnode *n;
84 n = AllocMem( sizeof (struct tfe_hashnode), MEMF_ANY|MEMF_CLEAR);
86 return n;
89 /****************************************************************************************/
91 void tfe_hashadd(struct tfe_hashnode *hn
92 , struct TextFont *tf
93 , struct TextFontExtension *etf
94 , struct GfxBase *GfxBase)
97 ULONG idx = tfe_calchashidx(tf);
99 hn->back = tf;
100 hn->ext = etf;
102 ObtainSemaphore( &GFBI(GfxBase)->tfe_hashtab_sema );
104 hn->next = GFBI(GfxBase)->tfe_hashtab[idx];
105 GFBI(GfxBase)->tfe_hashtab[idx] = hn;
107 ReleaseSemaphore( &GFBI(GfxBase)->tfe_hashtab_sema );
109 return;
112 /****************************************************************************************/
114 void tfe_hashdelete(struct TextFont *tf, struct GfxBase *GfxBase)
116 ULONG idx = tfe_calchashidx(tf);
117 struct tfe_hashnode *n, *last = NULL;
119 ObtainSemaphore( &GFBI(GfxBase)->tfe_hashtab_sema );
121 for (n = GFBI(GfxBase)->tfe_hashtab[idx]; n; n = n->next)
123 if (n->back == tf)
125 if (last)
126 last->next = n->next;
127 else
128 GFBI(GfxBase)->tfe_hashtab[idx] = n->next;
130 FreeMem(n, sizeof (struct tfe_hashnode));
131 break;
133 last = n;
136 ReleaseSemaphore( &GFBI(GfxBase)->tfe_hashtab_sema );
138 return;
142 /****************************************************************************************/
144 OOP_Object *fontbm_to_hiddbm(struct TextFont *font, struct GfxBase *GfxBase)
146 ULONG width, height;
147 OOP_Object *bm_obj;
148 OOP_Object *tmp_gc;
150 /* Caclulate sizes for the font bitmap */
151 struct TagItem bm_tags[] =
153 { aHidd_BitMap_Width , 0 },
154 { aHidd_BitMap_Height , 0 },
155 { aHidd_BitMap_StdPixFmt, vHidd_StdPixFmt_Plane },
156 { TAG_DONE }
159 tmp_gc = obtain_cache_object(SDD(GfxBase)->gc_cache, GfxBase);
160 if (NULL == tmp_gc)
161 return NULL;
163 width = font->tf_Modulo * 8;
164 height = font->tf_YSize;
166 bm_tags[0].ti_Data = width;
167 bm_tags[1].ti_Data = height;
169 #warning Handle color textfonts
171 bm_obj = HIDD_Gfx_NewBitMap(SDD(GfxBase)->gfxhidd, bm_tags);
172 if (NULL != bm_obj)
174 struct TagItem gc_tags[] =
176 { aHidd_GC_DrawMode , vHidd_GC_DrawMode_Copy },
177 { aHidd_GC_Foreground , 1 },
178 { aHidd_GC_Background , 0 },
179 { aHidd_GC_ColorExpansionMode , vHidd_GC_ColExp_Opaque },
180 { TAG_DONE }
183 /* Copy the character data into the bitmap */
184 OOP_SetAttrs(tmp_gc, gc_tags);
186 HIDD_BM_PutTemplate(bm_obj, tmp_gc, font->tf_CharData, font->tf_Modulo,
187 0, 0, 0, width, height, FALSE);
191 release_cache_object(SDD(GfxBase)->gc_cache, tmp_gc, GfxBase);
193 return bm_obj;
196 /****************************************************************************************/
198 UBYTE *colorfontbm_to_chunkybuffer(struct TextFont *font, struct GfxBase *GfxBase)
200 ULONG width, height;
201 UBYTE *chunky;
203 width = font->tf_Modulo * 8;
204 height = font->tf_YSize;
206 chunky = AllocVec(width * height, MEMF_CLEAR);
207 if (chunky)
209 struct BitMap bm;
210 struct RastPort rp;
211 UBYTE d, shift = 1, plane = 0;
213 InitBitMap(&bm, CTF(font)->ctf_Depth, width, height);
215 for(d = 0; d < CTF(font)->ctf_Depth; d++, shift <<= 1)
217 if (CTF(font)->ctf_PlanePick & shift)
219 bm.Planes[d] = (PLANEPTR)(CTF(font)->ctf_CharData[plane++]);
221 else
223 bm.Planes[d] = (CTF(font)->ctf_PlaneOnOff & shift) ? (PLANEPTR)-1 : NULL;
227 InitRastPort(&rp);
228 rp.BitMap = &bm;
230 ReadPixelArray8(&rp, 0, 0, width - 1, height - 1, chunky, NULL);
231 DeinitRastPort(&rp);
234 return chunky;
237 /****************************************************************************************/