Re: [AD] Miscelaneous issues

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Thursday 31 March 2005 12:20, Elias Pschernig wrote:
> Yes, I think if truecolor fonts just ignore the color, it would work.
> The docs would mention that this is only temporarily and likely to
> change.

Patch attached. This actually does a bit more: it also exposes the 
functionality to grab a font from a bitmap instead of having to read it 
from a bitmap file. This is a trivial change and makes it possible to 
generate or modify fonts at runtime, which I'm sure someone finds useful.

Evert
Index: docs/src/allegro._tx
===================================================================
RCS file: /cvsroot/alleg/allegro/docs/src/allegro._tx,v
retrieving revision 1.312
diff -u -r1.312 allegro._tx
--- docs/src/allegro._tx	31 Mar 2005 19:58:53 -0000	1.312
+++ docs/src/allegro._tx	31 Mar 2005 20:39:33 -0000
@@ -6834,6 +6834,7 @@
 
 @@FONT *@load_bitmap_font(const char *filename, RGB *pal, void *param)
 @xref register_font_file_type, load_font, load_bitmap, set_color_depth
+@xref grab_font_from_bitmap
 @shortdesc Grabs a font from a bitmap file.
    Tries to grab a font from a bitmap. The bitmap can be in any format that
    load_bitmap understands but it must have a color depth of 8 bits.
@@ -6856,6 +6857,18 @@
    responsible for destroying the font when you are finished with it to
    avoid memory leaks.
 
+@@FONT *@grab_font_from_bitmap(BITMAP *bmp)
+@xref load_bitmap_font
+@shortdesc Grabs a font from a bitmap
+   This function is the work-horse of load_bitmap_font, and can be used to
+   grab a font from a bitmap in memory. You can use this if you want to
+   generate or modify a font at runtime. The bitmap should follow the layout
+   described for load_bitmap_font.
+@retval
+   Returns a pointer to the font or NULL on error. Remember that you are
+   responsible for destroying the font when you are finished with it to
+   avoid memory leaks.
+
 @@FONT *@load_txt_font(const char *filename, RGB *pal, void *param)
 @xref register_font_file_type, load_font
 @shortdesc Loads a font script.
Index: include/allegro/font.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/font.h,v
retrieving revision 1.4
diff -u -r1.4 font.h
--- include/allegro/font.h	14 Feb 2005 17:27:03 -0000	1.4
+++ include/allegro/font.h	31 Mar 2005 20:39:33 -0000
@@ -55,6 +55,8 @@
 AL_FUNC(FONT *, load_bitmap_font, (AL_CONST char *fname, RGB *pal, void *param));
 AL_FUNC(FONT *, load_txt_font, (AL_CONST char *fname, RGB *pal, void *param));
 
+AL_FUNC(FONT *, grab_font_from_bitmap, (BITMAP *bmp));
+
 AL_FUNC(int, get_font_ranges, (FONT *f));
 AL_FUNC(int, get_font_range_begin, (FONT *f, int range));
 AL_FUNC(int, get_font_range_end, (FONT *f, int range));
Index: src/font.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/font.c,v
retrieving revision 1.14
diff -u -r1.14 font.c
--- src/font.c	19 Mar 2005 11:15:06 -0000	1.14
+++ src/font.c	31 Mar 2005 20:39:35 -0000
@@ -895,12 +895,17 @@
 
     g = _color_find_glyph(f, ch);
     if(g) {
-	if(fg < 0) {
-	    bmp->vtable->draw_256_sprite(bmp, g, x, y + (h-g->h)/2);
-	}
+        if (bitmap_color_depth(bmp) == 8) {
+	    if(fg < 0) {
+	        bmp->vtable->draw_256_sprite(bmp, g, x, y + (h-g->h)/2);
+	    }
+            else {
+	        bmp->vtable->draw_character(bmp, g, x, y + (h-g->h)/2, fg, bg);
+	    }
+        }
         else {
-	    bmp->vtable->draw_character(bmp, g, x, y + (h-g->h)/2, fg, bg);
-	}
+	    masked_blit(g, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
+        }        
 
 	w = g->w;
     }
Index: src/fontbmp.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/fontbmp.c,v
retrieving revision 1.2
diff -u -r1.2 fontbmp.c
--- src/fontbmp.c	19 Mar 2005 11:12:08 -0000	1.2
+++ src/fontbmp.c	31 Mar 2005 20:39:35 -0000
@@ -22,8 +22,6 @@
 #include "allegro/internal/aintern.h"
 
 /* state information for the bitmap font importer */
-static BITMAP *import_bmp = NULL;
-
 static int import_x = 0;
 static int import_y = 0;
 
@@ -82,7 +80,7 @@
 /* import_bitmap_font_mono:
  *  Helper for import_bitmap_font, below.
  */
-static int import_bitmap_font_mono(FONT_GLYPH** gl, int num)
+static int import_bitmap_font_mono(BITMAP *import_bmp, FONT_GLYPH** gl, int num)
 {
    int w = 1, h = 1, i;
 
@@ -124,7 +122,7 @@
 /* import_bitmap_font_color:
  *  Helper for import_bitmap_font, below.
  */
-static int import_bitmap_font_color(BITMAP** bits, int num)
+static int import_bitmap_font_color(BITMAP *import_bmp, BITMAP** bits, int num)
 {
    int w = 1, h = 1, i;
 
@@ -194,9 +192,8 @@
 FONT *load_bitmap_font(AL_CONST char *fname, RGB *pal, void *param)
 {
    /* NB: `end' is -1 if we want every glyph */
-   int begin = ' ';
-   int end = -1;
    int color_conv_mode;
+   BITMAP *import_bmp;
    FONT *f;
    ASSERT(fname);
 
@@ -206,9 +203,6 @@
    import_bmp = load_bitmap(fname, pal);
    set_color_conversion(color_conv_mode);
 
-   import_x = 0;
-   import_y = 0;
-
    if(!import_bmp) 
      return NULL;
 
@@ -218,15 +212,35 @@
       return NULL;
    }
 
-   f = _al_malloc(sizeof(FONT));
-   if (end == -1) end = bitmap_font_count(import_bmp) + begin;
+   f = grab_font_from_bitmap(import_bmp);
+
+   destroy_bitmap(import_bmp);
+
+   return f;
+}
+
+
 
-   if (bitmap_font_ismono(import_bmp)) {
+/* work horse for grabbing a font from an Allegro bitmap */
+FONT *grab_font_from_bitmap(BITMAP *bmp)
+{
+   int begin = ' ';
+   int end = -1;
+   FONT *f;
+   ASSERT(bmp)
+
+   import_x = 0;
+   import_y = 0;
+
+   f = _al_malloc(sizeof *f);
+   if (end == -1) end = bitmap_font_count(bmp) + begin;
+
+   if (bitmap_font_ismono(bmp)) {
       FONT_MONO_DATA* mf = _al_malloc(sizeof(FONT_MONO_DATA));
 
       mf->glyphs = _al_malloc(sizeof(FONT_GLYPH*) * (end - begin));
 
-      if ( import_bitmap_font_mono(mf->glyphs, end - begin) ) {
+      if ( import_bitmap_font_mono(bmp, mf->glyphs, end - begin) ) {
 	 free(mf->glyphs);
 	 free(mf);
 	 free(f);
@@ -246,7 +260,7 @@
       FONT_COLOR_DATA* cf = _al_malloc(sizeof(FONT_COLOR_DATA));
       cf->bitmaps = _al_malloc(sizeof(BITMAP*) * (end - begin));
 
-      if( import_bitmap_font_color(cf->bitmaps, end - begin) ) {
+      if( import_bitmap_font_color(bmp, cf->bitmaps, end - begin) ) {
 	 free(cf->bitmaps);
 	 free(cf);
 	 free(f);
@@ -262,9 +276,4 @@
 	 cf->next = 0;
       }
    }
-
-   destroy_bitmap(import_bmp);
-   import_bmp = 0;
-
-   return f;
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/