Mercurial > otakunoraifu
diff font/font_peer_ft2.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author | thib |
---|---|
date | Sat, 18 Apr 2009 18:35:39 +0000 |
parents | 5f548e5957a8 |
children | 4416cfac86ae |
line wrap: on
line diff
--- a/font/font_peer_ft2.cc +++ b/font/font_peer_ft2.cc @@ -41,10 +41,10 @@ using namespace std; namespace XKFont { typedef struct _FontLibrary { - FT_Library super; - int ref_count; - char **paths; - int num_paths; + FT_Library super; + int ref_count; + char **paths; + int num_paths; } FontLibrary; static FontLibrary *library = NULL; @@ -64,87 +64,79 @@ static void font_library_ft2_remove_path static void font_library_ft2_add_path(const char *path); static char *font_library_ft2_build_path(const char *base, const char *name); -static int -font_library_ft2_alloc() +static int font_library_ft2_alloc() { - int i; - - if (!library) { - library = (FontLibrary*) calloc(sizeof(FontLibrary), 1); - if (library) { - if (FT_Init_FreeType(&library->super)) - goto _1; - fprintf(stderr, "XKFont::font_library_ft2_alloc : FreeType allocated successfully.\n"); - for (i = 0; default_paths[i]; i++) - font_library_ft2_add_path(default_paths[i]); - } - } + int i; - library->ref_count++; - return 1; + if (!library) { + library = (FontLibrary*) calloc(sizeof(FontLibrary), 1); + if (library) { + if (FT_Init_FreeType(&library->super)) { + free(library); + library = NULL; + fprintf(stderr, "XKFont::font_library_ft2_alloc: Couldn't allocate FreeType.\n"); + return 0; + } + fprintf(stderr, "XKFont::font_library_ft2_alloc : FreeType allocated successfully.\n"); + for (i = 0; default_paths[i]; i++) + font_library_ft2_add_path(default_paths[i]); + } + } - _1: - free(library); - library = NULL; - fprintf(stderr, "XKFont::font_library_ft2_alloc: Couldn't allocate FreeType.\n"); - return 0; + library->ref_count++; + return 1; } -static void -font_library_ft2_free() +static void font_library_ft2_free() { - int i; - - if (!library || library->ref_count <= 0) - return; - if (--library->ref_count == 0) { - FT_Done_FreeType(library->super); - for (i = 0; i < library->num_paths; i++) - free(library->paths[i]); - free(library->paths); - free(library); - library = NULL; - fprintf(stderr, "XKFont::font_library_ft2_free : FreeType done.\n"); - } + int i; + + if (!library || library->ref_count <= 0) + return; + if (--library->ref_count == 0) { + FT_Done_FreeType(library->super); + for (i = 0; i < library->num_paths; i++) + free(library->paths[i]); + free(library->paths); + free(library); + library = NULL; + fprintf(stderr, "XKFont::font_library_ft2_free : FreeType done.\n"); + } } -static void -font_library_ft2_add_path(const char *path) +static void font_library_ft2_add_path(const char *path) { - library->num_paths++; - if (!library->paths) - library->paths = (char**) malloc(sizeof(char *)); - else - library->paths = (char**) realloc( (void*)library->paths, - library->num_paths * sizeof(char *)); - library->paths[library->num_paths - 1] = strdup(path); + library->num_paths++; + if (!library->paths) + library->paths = (char**) malloc(sizeof(char *)); + else + library->paths = (char**) realloc( (void*)library->paths, library->num_paths * sizeof(char *)); + library->paths[library->num_paths - 1] = strdup(path); } -static void -font_library_ft2_remove_path(const char *path) +static void font_library_ft2_remove_path(const char *path) { - int i, j; + int i, j; - for (i = 0; i < library->num_paths; i++) { - if (!strcmp(path, library->paths[i])) { - library->num_paths--; - for (j = i; j < library->num_paths; j++) - library->paths[j] = library->paths[j + 1]; - if (library->num_paths > 0) - library->paths = (char**) realloc(library->paths, - library->num_paths * sizeof(char *)); - else { - free(library->paths); - library->paths = NULL; - } - } - } + for (i = 0; i < library->num_paths; i++) { + if (!strcmp(path, library->paths[i])) { + library->num_paths--; + for (j = i; j < library->num_paths; j++) + library->paths[j] = library->paths[j + 1]; + if (library->num_paths > 0) + library->paths = (char**) realloc(library->paths, library->num_paths * sizeof(char *)); + else { + free(library->paths); + library->paths = NULL; + } + } + } } typedef struct _FontEncoding { - FT_UShort platform_id; - FT_Encoding encoding; - FontCodeConverter conv_func; + FT_UShort platform_id; + FT_Encoding encoding; + FontCodeConverter conv_func; } FontEncoding; static FontEncoding encodings[] = { @@ -154,66 +146,65 @@ static FontEncoding encodings[] = { { (FT_UShort)-1, (FT_Encoding)-1, NULL } }; -static char * -font_library_ft2_build_path(const char *base, const char *name) +static char * font_library_ft2_build_path(const char *base, const char *name) { - char *path; - const char *strs[] = { base, "/", name, NULL }; - int i = 0; - - path = (char*) calloc(sizeof(char), strlen(base) + strlen(name) + 2); - if (path) - while (strs[i]) - strcat(path, strs[i++]); - - return path; + char *path; + const char *strs[] = { base, "/", name, NULL }; + int i = 0; + + path = (char*) calloc(sizeof(char), strlen(base) + strlen(name) + 2); + if (path) + while (strs[i]) + strcat(path, strs[i++]); + + return path; } PeerFt2::PeerFt2(const char *name, int index, int hsize, int vsize) { - int i,j; + int i,j; + + font_library_ft2_alloc(); + + for (i = 0; i < library->num_paths; i++) { + char *path = font_library_ft2_build_path(library->paths[i], name); + if (path) { + if (FT_New_Face(library->super, path, index, &face)) + face = NULL; + free(path); + } + if (face) break; + } + + if (!face) { + string err = string("XKFont::PeerFt2::PeerFt : Cannot open font(TrueType) : ")+name; + throw std::invalid_argument(err); + } - font_library_ft2_alloc(); - - for (i = 0; i < library->num_paths; i++) { - char *path = font_library_ft2_build_path(library->paths[i], name); - if (path) { - if (FT_New_Face(library->super, path, index, &face)) - face = NULL; - free(path); - } - if (face) - break; - } - - if (!face) { - string err = string("XKFont::PeerFt2::PeerFt : Cannot open font(TrueType) : ")+name; - throw std::invalid_argument(err); - } - - conv_func = 0; - for (i=0; encodings[i].conv_func != 0; i++) { - FT_UShort platform_id = encodings[i].platform_id; - FT_Encoding encoding = encodings[i].encoding; - for (j = 0; j < face->num_charmaps; j++) { - FT_CharMap cmap = face->charmaps[j]; - if (cmap->platform_id == platform_id && cmap->encoding == encoding) { - if (FT_Set_Charmap(face, cmap) == 0) { - conv_func = encodings[i].conv_func; - break; - } - } - } - if (conv_func) break; - } - if (conv_func == 0) { - FT_Done_Face(face); - fprintf(stderr,"cannot find charmap\n"); - string err = string("XKFont::PeerFt2::PeerFt : No supported code converter of font (TrueType) ")+name; - throw std::invalid_argument(err); - } - FT_Set_Pixel_Sizes(face, hsize, vsize); + conv_func = 0; + for (i=0; encodings[i].conv_func != 0; i++) { + FT_UShort platform_id = encodings[i].platform_id; + FT_Encoding encoding = encodings[i].encoding; + for (j = 0; j < face->num_charmaps; j++) { + FT_CharMap cmap = face->charmaps[j]; + if (cmap->platform_id == platform_id && cmap->encoding == encoding) { + if (FT_Set_Charmap(face, cmap) == 0) { + conv_func = encodings[i].conv_func; + break; + } + } + } + if (conv_func) break; + } + if (conv_func == 0) { + FT_Done_Face(face); + fprintf(stderr,"cannot find charmap\n"); + string err = string("XKFont::PeerFt2::PeerFt : No supported code converter of font (TrueType) ")+name; + throw std::invalid_argument(err); + } + + FT_Set_Pixel_Sizes(face, hsize, vsize); } PeerFt2::~PeerFt2() { @@ -221,71 +212,70 @@ PeerFt2::~PeerFt2() { font_library_ft2_free(); } -bool -PeerFt2::GlyphCreate(unsigned int code, Glyph* glyph) +bool PeerFt2::GlyphCreate(unsigned int code, Glyph* glyph) { - FT_GlyphSlot slot; - FT_UInt index; - int bmsize; + FT_GlyphSlot slot; + FT_UInt index; + int bmsize; - if (face == 0) return false; - code = conv_func(code); - if (code == 0) return false; - index = FT_Get_Char_Index(face, code); - if (index == 0) return false; + if (face == 0) return false; + code = conv_func(code); + if (code == 0) return false; + index = FT_Get_Char_Index(face, code); + if (index == 0) return false; - /* Don't consider error */ - slot = face->glyph; - if (slot) { - // if (! FT_Load_Glyph(face, index, FT_LOAD_DEFAULT)) { -// BITMAP だと なぜか render してくれない…… -// LOAD_DEFAULT でも、下に対応コードを付けたので一応は大丈夫 - if (! FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP)) { - FT_Render_Glyph(slot, ft_render_mode_normal); - } - } - - glyph->bitmap_left = slot->bitmap_left; - glyph->bitmap_top = slot->bitmap_top; - glyph->bitmap.width = slot->bitmap.width; - glyph->bitmap.rows = slot->bitmap.rows; + /* Don't consider error */ + slot = face->glyph; + if (slot) { + // if (! FT_Load_Glyph(face, index, FT_LOAD_DEFAULT)) { + // BITMAP だと なぜか render してくれない…… + // LOAD_DEFAULT でも、下に対応コードを付けたので一応は大丈夫 + if (! FT_Load_Glyph(face, index, FT_LOAD_NO_BITMAP)) + FT_Render_Glyph(slot, ft_render_mode_normal); + } + + glyph->bitmap_left = slot->bitmap_left; + glyph->bitmap_top = slot->bitmap_top; + glyph->bitmap.width = slot->bitmap.width; + glyph->bitmap.rows = slot->bitmap.rows; /* - glyph->metrics.ascender = private->face->size->metrics.ascender >> 6; - glyph->metrics.descender = private->face->size->metrics.descender >> 6; + glyph->metrics.ascender = private->face->size->metrics.ascender >> 6; + glyph->metrics.descender = private->face->size->metrics.descender >> 6; */ - glyph->advance.x = slot->advance.x >> 6; - glyph->advance.y = slot->advance.y >> 6; + glyph->advance.x = slot->advance.x >> 6; + glyph->advance.y = slot->advance.y >> 6; - bmsize = glyph->bitmap.width * glyph->bitmap.rows; - if (bmsize > 0) { - glyph->bitmap.buffer = new unsigned char[bmsize]; - memcpy(glyph->bitmap.buffer, slot->bitmap.buffer, bmsize); - } + bmsize = glyph->bitmap.width * glyph->bitmap.rows; + if (bmsize > 0) { + glyph->bitmap.buffer = new unsigned char[bmsize]; + memcpy(glyph->bitmap.buffer, slot->bitmap.buffer, bmsize); + } // なぜか Render したのに MONO なことがある…… /* for freetype < 2.1.3, use ``ft_pixel_mode_mono'' */ -if (slot->bitmap.pixel_mode == ft_pixel_mode_mono) {int i,j; + if (slot->bitmap.pixel_mode == ft_pixel_mode_mono) { + int i, j; // if (slot->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {int i,j; - char* d = (char*)slot->bitmap.buffer; - for (i=0; i<glyph->bitmap.rows; i++) { - int flag = *d++; int len = 8; - unsigned char* buf = glyph->bitmap.buffer + i*slot->bitmap.width; - for (j=0; j<glyph->bitmap.width; j++) { - if (len == 0) { - flag = *d++; - len = 8; + char* d = (char*)slot->bitmap.buffer; + for (i=0; i < glyph->bitmap.rows; i++) { + int flag = *d++; + int len = 8; + unsigned char* buf = glyph->bitmap.buffer + i*slot->bitmap.width; + for (j=0; j < glyph->bitmap.width; j++) { + if (len == 0) { + flag = *d++; + len = 8; + } + if (flag & 0x80) *buf++ = 0xff; + else *buf++ = 0; + flag <<= 1; + len--; } - if (flag & 0x80) *buf++ = 0xff; - else *buf++ = 0; - flag <<= 1; - len--; } } -} - return true; - + return true; } } /* end of namespace XKFont */