1 #include <aros/debug.h>
2 #include <libraries/mui.h>
3 #include <graphics/gfx.h>
4 #include <diskfont/diskfonttag.h>
6 #include <proto/alib.h>
7 #include <proto/exec.h>
8 #include <proto/graphics.h>
9 #include <proto/intuition.h>
10 #include <proto/utility.h>
11 #include <proto/muimaster.h>
12 #include <proto/freetype2.h>
16 #include "fontbitmap_class.h"
23 struct BitMap
*GrayBitMap
;
26 typedef struct FontBitmapData FontBitmapData
;
28 IPTR
fbNew(Class
*cl
, Object
*o
, struct opSet
*msg
)
31 struct TagItem tags
[5];
32 STRPTR filename
= (STRPTR
)GetTagData(MUIA_FontBitmap_Filename
, (IPTR
) NULL
, msg
->ops_AttrList
);
33 STRPTR string
= (STRPTR
)GetTagData(MUIA_FontBitmap_String
, (IPTR
) "?", msg
->ops_AttrList
);
34 struct TagItem
*otags
= (struct TagItem
*)GetTagData(MUIA_FontBitmap_OTags
,
35 (IPTR
) NULL
, msg
->ops_AttrList
);
38 struct GlyphMap
*glyph
;
44 struct BitMap
*gray_bitmap
= NULL
;
46 int length
= strlen(string
);
48 int xmin
, xmax
, ymin
, ymax
;
49 int space_width
, size
, gray
;
55 DEBUG_FONTBITMAP(dprintf("FontBitmap: no filename.\n"));
59 engine
= OpenEngine();
62 DEBUG_FONTBITMAP(dprintf("FontBitmap: no engine.\n"));
66 size
= GetTagData(MUIA_FontBitmap_Size
, 30, msg
->ops_AttrList
);
67 gray
= GetTagData(MUIA_FontBitmap_Gray
, FALSE
, msg
->ops_AttrList
);
70 OT_OTagList
, (IPTR
) otags
,
71 OT_DeviceDPI
, 72 | (72 << 16),
72 OT_PointHeight
, size
<< 16,
75 space_width
= (int)(GetTagData(OT_SpaceWidth
, 0, otags
) / 65536.0 * size
) ;
77 info
= AllocVec(length
* sizeof(*info
), MEMF_CLEAR
);
80 DEBUG_FONTBITMAP(dprintf("FontBitmap: can't alloc glyphs.\n"));
87 xmin
= ymin
= 0x7fffffff;
88 xmax
= ymax
= -0x80000000;
89 tag
= gray
? OT_GlyphMap8Bit
: OT_GlyphMap
;
91 for (k
= 0; k
< length
; ++k
)
102 OT_GlyphCode
, previous
,
106 OT_TextKernPair
, (IPTR
)&kerning
,
109 x
-= (int)(kerning
/ 65536.0 * size
);
119 tag
, (IPTR
)&info
[k
].glyph
,
130 x1
= x
- g
->glm_X0
+ g
->glm_BlackLeft
;
131 y1
= y
- g
->glm_Y0
+ g
->glm_BlackTop
;
132 x2
= x1
+ g
->glm_BlackWidth
;
133 y2
= y1
+ g
->glm_BlackHeight
;
144 x
+= g
->glm_X1
- g
->glm_X0
;
145 y
+= g
->glm_Y1
- g
->glm_Y0
;
150 width
= xmax
- xmin
+ 1;
151 height
= ymax
- ymin
+ 1;
153 DEBUG_FONTBITMAP(dprintf("FontBitmap: bbox %d %d %d %d\n", xmin
, ymin
, xmax
, ymax
));
154 DEBUG_FONTBITMAP(dprintf("FontBitmap: width %d height %d\n", width
, height
));
156 if (width
> 0 && height
> 0 && width
< 32000 && height
< 32000)
161 int width1
= (width
+ 15) & ~15;
163 array
= AllocVec(width1
* height
, MEMF_CLEAR
);
166 for (k
= 0; k
< length
; ++k
)
168 struct GlyphMap
*g
= info
[k
].glyph
;
178 x
= info
[k
].x
- xmin
;
179 y
= info
[k
].y
- ymin
;
182 x
+= g
->glm_BlackLeft
;
183 y
+= g
->glm_BlackTop
;
188 x2
= x
+ g
->glm_BlackWidth
;
189 y2
= y
+ g
->glm_BlackHeight
;
191 if (x1
> width
|| x2
< 0 || y1
> height
|| y2
< 0)
201 p
-= y1
* g
->glm_BMModulo
;
217 for (x
= x1
; x
< x2
; ++x
)
219 int t
= array
[width1
* y1
+ x
] + p
[x
- x1
];
222 array
[width1
* y1
+ x
] = t
;
224 p
+= g
->glm_BMModulo
;
229 gray_bitmap
= AllocBitMap(width
, height
, 8, 0, NULL
);
232 struct RastPort rp
, tmp_rp
;
235 InitRastPort(&tmp_rp
);
237 rp
.BitMap
= gray_bitmap
;
238 tmp_rp
.BitMap
= AllocBitMap(width
, 1, 8, 0, NULL
);
242 WritePixelArray8(&rp
,
249 FreeBitMap(tmp_rp
.BitMap
);
258 InitBitMap(&bitmap
, 1, width
, height
);
259 bitmap
.Planes
[0] = AllocRaster(width
, height
);
261 if (bitmap
.Planes
[0])
271 for (k
= 0; k
< length
; ++k
)
273 struct GlyphMap
*g
= info
[k
].glyph
;
278 x
= info
[k
].x
- xmin
;
279 y
= info
[k
].y
- ymin
;
282 x
+= g
->glm_BlackLeft
;
283 y
+= g
->glm_BlackTop
;
285 /* glm_BitMap is not in chip mem.
288 BltTemplate((const PLANEPTR
)(g
->glm_BitMap
+
301 tags
[0].ti_Tag
= MUIA_Bitmap_Width
;
302 tags
[0].ti_Data
= width
;
303 tags
[1].ti_Tag
= MUIA_Bitmap_Height
;
304 tags
[1].ti_Data
= height
;
305 tags
[2].ti_Tag
= MUIA_FixWidth
;
306 tags
[2].ti_Data
= width
;
307 tags
[3].ti_Tag
= MUIA_FixHeight
;
308 tags
[3].ti_Data
= height
;
309 tags
[4].ti_Tag
= TAG_MORE
;
310 tags
[4].ti_Data
= (IPTR
)msg
->ops_AttrList
;
312 method
.MethodID
= OM_NEW
;
313 method
.ops_AttrList
= tags
;
314 method
.ops_GInfo
= NULL
;
316 o
= (Object
*)DoSuperMethodA(cl
, o
, (Msg
)&method
);
320 FontBitmapData
*dat
= INST_DATA(cl
, o
);
323 dat
->Height
= height
;
324 dat
->GrayBitMap
= gray_bitmap
;
328 static ULONG colors
[256 * 3];
334 ULONG
*p
= colors
, color
;
335 for (k
= 256; --k
>= 0; p
+= 3)
337 color
= (k
<< 24) | (k
<< 16) | (k
<< 8) | k
;
338 p
[0] = p
[1] = p
[2] = color
;
344 MUIA_Bitmap_Bitmap
, gray_bitmap
,
345 MUIA_Bitmap_SourceColors
, colors
,
350 dat
->BitMap
= bitmap
;
351 set(o
, MUIA_Bitmap_Bitmap
, &dat
->BitMap
);
358 FreeBitMap(gray_bitmap
);
360 else if (bitmap
.Planes
[0])
362 FreeRaster(bitmap
.Planes
[0], width
, height
);
371 for (k
= 0; k
< length
; ++k
)
376 tag
, (IPTR
)info
[k
].glyph
,
385 DEBUG_FONTBITMAP(dprintf("FontBitmap: created object 0x%lx.\n", o
));
390 IPTR
fbDispose(Class
*cl
, Object
*o
)
392 FontBitmapData
*dat
= INST_DATA(cl
, o
);
394 DEBUG_FONTBITMAP(dprintf("FontBitmap: destroy object 0x%lx.\n", o
));
398 FreeBitMap(dat
->GrayBitMap
);
400 else if (dat
->BitMap
.Planes
[0])
402 FreeRaster(dat
->BitMap
.Planes
[0], dat
->Width
, dat
->Height
);
405 return DoSuperMethod(cl
, o
, OM_DISPOSE
);
408 AROS_UFH3(ULONG
, FontBitmapDispatch
,
409 AROS_UFHA(Class
*, cl
, A0
),
410 AROS_UFHA(Object
*, o
, A2
),
411 AROS_UFHA(Msg
, msg
, A1
))
417 switch (msg
->MethodID
)
420 ret
= fbNew(cl
, o
, (struct opSet
*)msg
);
424 ret
= fbDispose(cl
, o
);
428 ret
= DoSuperMethodA(cl
, o
, msg
);
438 void CleanupFontBitmapClass(void)
442 MUI_DeleteCustomClass(FontBitmapClass
);
443 FontBitmapClass
= NULL
;
447 int InitFontBitmapClass(void)
449 FontBitmapClass
= MUI_CreateCustomClass(NULL
, MUIC_Bitmap
, NULL
,
450 sizeof(FontBitmapData
), UFHN(FontBitmapDispatch
));
451 return FontBitmapClass
!= NULL
;