void duk_push_sphere_font(duk_context* ctx, font_t* font) { duk_push_sphere_obj(ctx, "Font", ref_font(font)); duk_push_sphere_color(ctx, color_new(255, 255, 255, 255)); duk_put_prop_string(ctx, -2, "\xFF" "color_mask"); }
void duk_push_sphere_font(duk_context* ctx, font_t* font) { ref_font(font); duk_push_object(ctx); duk_push_c_function(ctx, js_Font_finalize, DUK_VARARGS); duk_set_finalizer(ctx, -2); duk_push_c_function(ctx, js_Font_toString, DUK_VARARGS); duk_put_prop_string(ctx, -2, "toString"); duk_push_c_function(ctx, js_Font_clone, DUK_VARARGS); duk_put_prop_string(ctx, -2, "clone"); duk_push_c_function(ctx, js_Font_getCharacterImage, DUK_VARARGS); duk_put_prop_string(ctx, -2, "getCharacterImage"); duk_push_c_function(ctx, js_Font_getColorMask, DUK_VARARGS); duk_put_prop_string(ctx, -2, "getColorMask"); duk_push_c_function(ctx, js_Font_getHeight, DUK_VARARGS); duk_put_prop_string(ctx, -2, "getHeight"); duk_push_c_function(ctx, js_Font_setCharacterImage, DUK_VARARGS); duk_put_prop_string(ctx, -2, "setCharacterImage"); duk_push_c_function(ctx, js_Font_setColorMask, DUK_VARARGS); duk_put_prop_string(ctx, -2, "setColorMask"); duk_push_c_function(ctx, js_Font_drawText, DUK_VARARGS); duk_put_prop_string(ctx, -2, "drawText"); duk_push_c_function(ctx, js_Font_drawTextBox, DUK_VARARGS); duk_put_prop_string(ctx, -2, "drawTextBox"); duk_push_c_function(ctx, js_Font_drawZoomedText, DUK_VARARGS); duk_put_prop_string(ctx, -2, "drawZoomedText"); duk_push_c_function(ctx, js_Font_getStringHeight, DUK_VARARGS); duk_put_prop_string(ctx, -2, "getStringHeight"); duk_push_c_function(ctx, js_Font_getStringWidth, DUK_VARARGS); duk_put_prop_string(ctx, -2, "getStringWidth"); duk_push_c_function(ctx, js_Font_wordWrapString, DUK_VARARGS); duk_put_prop_string(ctx, -2, "wordWrapString"); duk_push_string(ctx, "font"); duk_put_prop_string(ctx, -2, "\xFF" "sphere_type"); duk_push_pointer(ctx, font); duk_put_prop_string(ctx, -2, "\xFF" "ptr"); duk_push_sphere_color(ctx, rgba(255, 255, 255, 255)); duk_put_prop_string(ctx, -2, "\xFF" "color_mask"); }
font_t* clone_font(const font_t* src_font) { font_t* font = NULL; int max_x = 0, max_y = 0; int min_width = INT_MAX; const struct font_glyph* src_glyph; uint32_t i; console_log(2, "cloning font #%u from source font #%u", s_next_font_id, src_font->id); if (!(font = calloc(1, sizeof(font_t)))) goto on_error; if (!(font->glyphs = calloc(src_font->num_glyphs, sizeof(struct font_glyph)))) goto on_error; font->num_glyphs = src_font->num_glyphs; // perform the clone get_font_metrics(src_font, &min_width, &max_x, &max_y); font->height = max_y; font->min_width = min_width; font->max_width = max_x; for (i = 0; i < src_font->num_glyphs; ++i) { src_glyph = &src_font->glyphs[i]; font->glyphs[i].image = ref_image(src_glyph->image); font->glyphs[i].width = src_glyph->width; font->glyphs[i].height = src_glyph->height; } font->id = s_next_font_id++; return ref_font(font); on_error: if (font != NULL) { for (i = 0; i < font->num_glyphs; ++i) { if (font->glyphs[i].image != NULL) free_image(font->glyphs[i].image); } free(font->glyphs); free(font); } return NULL; }
font_t* load_font(const char* filename) { image_t* atlas = NULL; int atlas_x, atlas_y; int atlas_size_x, atlas_size_y; sfs_file_t* file; font_t* font = NULL; struct font_glyph* glyph; struct rfn_glyph_header glyph_hdr; long glyph_start; uint8_t* grayscale; image_lock_t* lock = NULL; int max_x = 0, max_y = 0; int min_width = INT_MAX; int64_t n_glyphs_per_row; int pixel_size; struct rfn_header rfn; uint8_t *psrc; color_t *pdest; int i, x, y; console_log(2, "loading font #%u as `%s`", s_next_font_id, filename); memset(&rfn, 0, sizeof(struct rfn_header)); if ((file = sfs_fopen(g_fs, filename, NULL, "rb")) == NULL) goto on_error; if (!(font = calloc(1, sizeof(font_t)))) goto on_error; if (sfs_fread(&rfn, sizeof(struct rfn_header), 1, file) != 1) goto on_error; pixel_size = (rfn.version == 1) ? 1 : 4; if (!(font->glyphs = calloc(rfn.num_chars, sizeof(struct font_glyph)))) goto on_error; // pass 1: load glyph headers and find largest glyph glyph_start = sfs_ftell(file); for (i = 0; i < rfn.num_chars; ++i) { glyph = &font->glyphs[i]; if (sfs_fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1) goto on_error; sfs_fseek(file, glyph_hdr.width * glyph_hdr.height * pixel_size, SFS_SEEK_CUR); max_x = fmax(glyph_hdr.width, max_x); max_y = fmax(glyph_hdr.height, max_y); min_width = fmin(min_width, glyph_hdr.width); glyph->width = glyph_hdr.width; glyph->height = glyph_hdr.height; } font->num_glyphs = rfn.num_chars; font->min_width = min_width; font->max_width = max_x; font->height = max_y; // create glyph atlas n_glyphs_per_row = ceil(sqrt(rfn.num_chars)); atlas_size_x = max_x * n_glyphs_per_row; atlas_size_y = max_y * n_glyphs_per_row; if ((atlas = create_image(atlas_size_x, atlas_size_y)) == NULL) goto on_error; // pass 2: load glyph data sfs_fseek(file, glyph_start, SFS_SEEK_SET); if (!(lock = lock_image(atlas))) goto on_error; for (i = 0; i < rfn.num_chars; ++i) { glyph = &font->glyphs[i]; if (sfs_fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1) goto on_error; atlas_x = i % n_glyphs_per_row * max_x; atlas_y = i / n_glyphs_per_row * max_y; switch (rfn.version) { case 1: // RFN v1: 8-bit grayscale glyphs if (!(glyph->image = create_subimage(atlas, atlas_x, atlas_y, glyph_hdr.width, glyph_hdr.height))) goto on_error; grayscale = malloc(glyph_hdr.width * glyph_hdr.height); if (sfs_fread(grayscale, glyph_hdr.width * glyph_hdr.height, 1, file) != 1) goto on_error; psrc = grayscale; pdest = lock->pixels + atlas_x + atlas_y * lock->pitch; for (y = 0; y < glyph_hdr.height; ++y) { for (x = 0; x < glyph_hdr.width; ++x) pdest[x] = color_new(psrc[x], psrc[x], psrc[x], 255); pdest += lock->pitch; psrc += glyph_hdr.width; } break; case 2: // RFN v2: 32-bit truecolor glyphs if (!(glyph->image = read_subimage(file, atlas, atlas_x, atlas_y, glyph_hdr.width, glyph_hdr.height))) goto on_error; break; } } unlock_image(atlas, lock); sfs_fclose(file); free_image(atlas); font->id = s_next_font_id++; return ref_font(font); on_error: console_log(2, "failed to load font #%u", s_next_font_id++); sfs_fclose(file); if (font != NULL) { for (i = 0; i < rfn.num_chars; ++i) { if (font->glyphs[i].image != NULL) free_image(font->glyphs[i].image); } free(font->glyphs); free(font); } if (lock != NULL) unlock_image(atlas, lock); if (atlas != NULL) free_image(atlas); return NULL; }
font_t* load_font(const char* path) { image_t* atlas = NULL; int atlas_size_x, atlas_size_y; ALLEGRO_LOCKED_REGION* bitmap_lock; FILE* file; font_t* font = NULL; struct font_glyph* glyph; struct rfn_glyph_header glyph_hdr; long glyph_start; int max_x = 0, max_y = 0; int min_width = INT_MAX; int64_t n_glyphs_per_row; size_t pixel_size; struct rfn_header rfn; uint8_t *src_ptr, *dest_ptr; int i, x, y; memset(&rfn, 0, sizeof(struct rfn_header)); if ((file = fopen(path, "rb")) == NULL) goto on_error; if (!(font = calloc(1, sizeof(font_t)))) goto on_error; if (fread(&rfn, sizeof(struct rfn_header), 1, file) != 1) goto on_error; pixel_size = (rfn.version == 1) ? 1 : 4; if (!(font->glyphs = calloc(rfn.num_chars, sizeof(struct font_glyph)))) goto on_error; // pass 1: load glyph headers and find largest glyph glyph_start = ftell(file); for (i = 0; i < rfn.num_chars; ++i) { glyph = &font->glyphs[i]; if (fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1) goto on_error; fseek(file, glyph_hdr.width * glyph_hdr.height * pixel_size, SEEK_CUR); max_x = fmax(glyph_hdr.width, max_x); max_y = fmax(glyph_hdr.height, max_y); min_width = fmin(min_width, glyph_hdr.width); glyph->width = glyph_hdr.width; glyph->height = glyph_hdr.height; } font->min_width = min_width; font->max_width = max_x; font->height = max_y; // create glyph atlas n_glyphs_per_row = ceil(sqrt(rfn.num_chars)); atlas_size_x = max_x * n_glyphs_per_row; atlas_size_y = max_y * n_glyphs_per_row; if ((atlas = create_image(atlas_size_x, atlas_size_y)) == NULL) goto on_error; // pass 2: load glyph data fseek(file, glyph_start, SEEK_SET); for (i = 0; i < rfn.num_chars; ++i) { glyph = &font->glyphs[i]; if (fread(&glyph_hdr, sizeof(struct rfn_glyph_header), 1, file) != 1) goto on_error; size_t data_size = glyph_hdr.width * glyph_hdr.height * pixel_size; void* data = malloc(data_size); if (fread(data, 1, data_size, file) != data_size) goto on_error; glyph->image = create_subimage(atlas, i % n_glyphs_per_row * max_x, i / n_glyphs_per_row * max_y, glyph_hdr.width, glyph_hdr.height); if (glyph->image == NULL) goto on_error; if ((bitmap_lock = al_lock_bitmap(get_image_bitmap(glyph->image), ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_WRITEONLY)) == NULL) goto on_error; src_ptr = data; dest_ptr = bitmap_lock->data; switch (rfn.version) { case 1: // RFN v1: 8-bit grayscale glyphs for (y = 0; y < glyph_hdr.height; ++y) { for (x = 0; x < glyph_hdr.width; ++x) { dest_ptr[x] = src_ptr[x]; dest_ptr[x + 1] = src_ptr[x]; dest_ptr[x + 2] = src_ptr[x]; dest_ptr[x + 3] = 255; dest_ptr += 4; } dest_ptr += bitmap_lock->pitch - (glyph_hdr.width * 4); src_ptr += glyph_hdr.width; } break; case 2: // RFN v2: 32-bit truecolor glyphs for (y = 0; y < glyph_hdr.height; ++y) { memcpy(dest_ptr, src_ptr, glyph_hdr.width * 4); dest_ptr += bitmap_lock->pitch; src_ptr += glyph_hdr.width * pixel_size; } break; } al_unlock_bitmap(get_image_bitmap(glyph->image)); free(data); } fclose(file); free_image(atlas); return ref_font(font); on_error: if (font != NULL) { for (i = 0; i < rfn.num_chars; ++i) { if (font->glyphs[i].image != NULL) free_image(font->glyphs[i].image); } free(font->glyphs); free(font); } if (atlas != NULL) free_image(atlas); return NULL; }