gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Japanese Fonts ?

#133713 - jonnyhilly - Sun Jul 08, 2007 1:50 am

Hi, I'm interested in coding something up that can print text in both english and Japanese (Katakana, Hiragana and Kanji characters)
So I guess I'd need a Unicode font or something ?

any ideas where I could get a unicode-utf-8 font, with all the other languages' graphics stripped out, so its just english and Japanese ?

Is there any support in DS libs for printing Unicode characters or fonts ?
if so what type of encoding does it use ?

any help appreciated.
Thanks

#133718 - masscat - Sun Jul 08, 2007 2:14 am

Lick has ported FreeType2 to the DS. Otherwise write some PC software to convert TrueType into some other format for you to use.

As for free Japanese fonts, have a look at a Linux distribution for possibilities.

#133734 - Dwedit - Sun Jul 08, 2007 6:38 am

There's always the GNU Unifont, if you don't mind spreading the viral GPL over your code.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#133808 - abszero - Sun Jul 08, 2007 10:10 pm

If you're not really set on using Unicode as your text encoding, you could try using a BDF font - that's what I'm using. They're (generally) pixel fonts and so are often optimized for small screens like on the DS.

http://palm.roguelife.org/cjkos/ gives a good overview of what's out there. M+ in particular is nice because it's public domain IIRC ( http://mplus-fonts.sourceforge.jp/mplus-bitmap-fonts/design/index.html ) .

#133852 - jonnyhilly - Mon Jul 09, 2007 8:36 am

yeah, a bdf is the best thing I've found so far.
I'm kinda stuck with Unicode utf-8 as the kanji database I am using is in that format, though I guess I could convert from one format to another...

I did find one unicode bdf font with 19208 characters for an x-wondows system... but it might work OK. I dunno
http://www.cl.cam.ac.uk/~mgk25/ucs-fonts.html
Though I can't even see what the font looks like till I code it up.
...which is slightly worrying

thanks for the links


abszero wrote:
If you're not really set on using Unicode as your text encoding, you could try using a BDF font - that's what I'm using. They're (generally) pixel fonts and so are often optimized for small screens like on the DS.

http://palm.roguelife.org/cjkos/ gives a good overview of what's out there. M+ in particular is nice because it's public domain IIRC ( http://mplus-fonts.sourceforge.jp/mplus-bitmap-fonts/design/index.html ) .

#133865 - masscat - Mon Jul 09, 2007 11:19 am

You can make your own bitmap fonts from fonts files supported by libfreetype using ttf2bdf (under linux) or write your own converter.
Here is a program that dumps the first 128 characters at a given height in my own custom format. Feel free to adapt it as you please should it be useful:
Code:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <ft2build.h>
#include FT_FREETYPE_H


static void
dump_bitmap( uint8_t char_num, const char *font_name,
        FT_Bitmap *bitmap, FT_Glyph_Metrics *metrics,
        FT_Int left, FT_Int top) {
  /*printf( "row %d, width %d, pitch %d, num_grays %d, pixel_mode %d, palette_mode %d\n",
     bitmap->rows, bitmap->width, bitmap->pitch, bitmap->num_grays, bitmap->pixel_mode,
     bitmap->palette_mode);*/

  /*printf( "metrics:\n"
     "width %d, height %d, bearing x %d, bearing y %d, "
     "advance %d\n",
     metrics->width, metrics->height, metrics->horiBearingX,
     metrics->horiBearingY, metrics->horiAdvance);*/

  /*printf( "left %d, top %d\n", left, top);*/

  if ( isprint( char_num)) {
    printf( "/* '%c' 0x%x\n", (char)char_num, char_num);
  }
  else {
    printf( "/* 0x%x\n", char_num);
  }
  printf( " *\n");

  if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
    int y;

    for ( y = 0; y < bitmap->rows; y++) {
      uint8_t *bitmap_row = &bitmap->buffer[ y * bitmap->pitch];
      int x;

      printf(" * ");

      for ( x = 0; x < bitmap->width; x++) {
   int bits_index = x / 8;
   uint8_t bits = bitmap_row[bits_index];
   uint8_t mask = 0x80 >> ( x % 8);

   if ( bits & mask)
     printf("*");
   else
     printf("-");
      }
      printf("\n");
    }
  }

  if ( bitmap->pixel_mode == FT_PIXEL_MODE_MONO) {
    int y;

    printf( " */\n");
    printf( "static unsigned char\n");
    printf( "%s_%d_bitmap[] = {\n", font_name, char_num);
    printf( "\t%d, %d", bitmap->width, bitmap->rows);

    for ( y = 0; y < bitmap->rows; y++) {
      uint8_t *bitmap_row = &bitmap->buffer[ y * bitmap->pitch];
      int x;

      printf( ",\n\t");

      for ( x = 0; x < (bitmap->width + 7) / 8; x++) {
   uint8_t bits = bitmap_row[x];

   if ( x != 0)
     printf( ", ");

   printf( "0x%x", bits);
      }
    }
    printf("\n");


    printf( "};\n");

    printf( "static struct font_glyph\n");
    printf( "%s_%d_font_glyph = {\n", font_name, char_num);
    printf( "\t%s_%d_bitmap,\n", font_name, char_num);
    printf( "\t%d, /* top */\n", top);
    printf( "\t%d, /* left */\n", left);
    printf( "\t%d /* advance */\n", metrics->horiAdvance >> 6);
    printf( "};\n\n");
  }
}


#define NUM_CHARS 128


int
main( int argc, char *argv[]) {
  int pixel_size;
  int max_top = 0;
  FT_Error error;
  FT_Library library;
  FT_Face face;
  int i;
  int max_size = 0;

  char font_name[50];
  char filename[200];
  const char *post_name;

  if ( argc != 3) {
    fprintf( stderr, "USAGE: %s <font_file> <bitmap_height>\n", argv[0]);
    exit( EXIT_FAILURE);
  }

  pixel_size = atoi( argv[2]);

  error = FT_Init_FreeType( &library);

  if ( error) {
    fprintf( stderr, "Failed to init freetype lib (%d)\n", error);
    exit( EXIT_FAILURE);
  }

  error = FT_New_Face( library, argv[1], 0, &face);

  if ( error) {
    fprintf( stderr, "Failed to create face (%d) from font file %s\n",
        error, argv[1]);
    FT_Done_FreeType( library);
    exit( EXIT_FAILURE);
  }

  error = FT_Set_Pixel_Sizes( face, 0, pixel_size);
  if ( error) {
    fprintf( stderr, "Failed (%d) to set pixel size to %d\n",
        error, pixel_size);
    FT_Done_FreeType( library);
    exit( EXIT_FAILURE);
  }

  post_name = FT_Get_Postscript_Name( face);
  for ( i = 0; i < strlen(post_name); i++) {
    font_name[i] = toupper( post_name[i]);
  }
  font_name[i] = '\0';

  fprintf( stderr, "Dumping font file \"%s\" as %d bitmap characters\n",
      argv[1], pixel_size);
  fprintf( stderr, "Face name is \"%s\"\n", post_name);


  printf("#ifndef _FONT_%s_%d_H_\n", font_name, pixel_size);
  printf("#define _FONT_%s_%d_H_ 1\n\n", font_name, pixel_size);

  FT_Int major, minor, patch;
  FT_Library_Version( library, &major, &minor, &patch);

  printf("/*\n");
  printf(" * Bitmap font create from font %s character size %d\n", post_name, pixel_size);
  printf(" * created using %s using FreeType library %d.%d.%d\n", argv[0], major, minor, patch);
  printf(" */\n\n");

  printf("#include \"font_structs.h\"\n\n");

  /*
   * Load and render the glyph
   */
  for ( i = 0; i < NUM_CHARS; i++) {
    error = FT_Load_Char( face, i,
           FT_LOAD_RENDER | FT_LOAD_TARGET_MONO);

    if ( error) {
      if ( isprint( i)) {
   fprintf( stderr, "Failed (%d) to load character '%c' (%d)\n",
       error, (char)i, i);
      }
      else {
   fprintf( stderr, "Failed (%d) to load character %d\n",
       error, i);
      }
      //FT_Done_FreeType( library);
      //exit( EXIT_FAILURE);
    }
    else {
      /* dump the bitmap */
      dump_bitmap( i, post_name, &face->glyph->bitmap, &face->glyph->metrics,
                   face->glyph->bitmap_left, face->glyph->bitmap_top);

      if ( face->glyph->bitmap_top > max_top) {
        max_top = face->glyph->bitmap_top;
      }
      if ( face->glyph->bitmap.rows > max_size) {
        max_size = face->glyph->bitmap.rows;
      }
    }
  }

  /*
   * collect all the glyphs together in an array
   */
  printf("static struct font_glyph *\n");
  printf("font_%s_%d_glyphs[%d] = {\n",
    post_name, pixel_size, NUM_CHARS);
  for ( i = 0; i < NUM_CHARS; i++) {
    if ( i != 0)
      printf(",\n");

    printf("\t&%s_%d_font_glyph", post_name, i);
  }
  printf("\n};\n\n");

  /*
   * Create the font structure
   */
  printf( "struct bitmap_font\n");
  printf( "font_%s_%d = {\n", post_name, pixel_size);
  printf( "\t/* font glyphs */\n");
  printf( "\tfont_%s_%d_glyphs,\n", post_name, pixel_size);
  printf( "\t/* requested glyph size */\n");
  printf( "\t%d,\n", pixel_size);
  printf( "\t/* actual glyph size */\n");
  printf( "\t%d,\n", max_size);
  printf( "\t/* number of glyphs in font */\n");
  printf( "\t%d,\n", NUM_CHARS);
  printf( "\t/* the max top value in font */\n");
  printf( "\t%d\n", max_top);
  printf("};\n\n");


  printf("\n#endif /* End of _FONT_%s_%d_H_ */\n", font_name, pixel_size);

  FT_Done_FreeType( library);
}

#134233 - sgeos - Wed Jul 11, 2007 8:51 pm

k14.bdf is a nice bitmap Japanese font. Google: k14.bdf and you should find it eventually. I suspect that it lives somewhere on all *nix distributions, but I could be wrong.

-Brendan