static duk_ret_t js_Font_wordWrapString(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); int width = duk_require_int(ctx, 1); font_t* font; int num_lines; wraptext_t* wraptext; int i; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); wraptext = word_wrap_text(font, text, width); num_lines = get_wraptext_line_count(wraptext); duk_push_array(ctx); for (i = 0; i < num_lines; ++i) { duk_push_string(ctx, get_wraptext_line(wraptext, i)); duk_put_prop_index(ctx, -2, i); } free_wraptext(wraptext); return 1; }
static duk_ret_t js_Font_drawZoomedText(duk_context* ctx) { int x = duk_require_int(ctx, 0); int y = duk_require_int(ctx, 1); float scale = duk_require_number(ctx, 2); const char* text = duk_to_string(ctx, 3); ALLEGRO_BITMAP* bitmap; font_t* font; color_t mask; int text_w, text_h; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_get_prop_string(ctx, -1, "\xFF" "color_mask"); mask = duk_require_sphere_color(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (!screen_is_skipframe(g_screen)) { text_w = get_text_width(font, text); text_h = get_font_line_height(font); bitmap = al_create_bitmap(text_w, text_h); al_set_target_bitmap(bitmap); draw_text(font, mask, 0, 0, TEXT_ALIGN_LEFT, text); al_set_target_backbuffer(screen_display(g_screen)); al_draw_scaled_bitmap(bitmap, 0, 0, text_w, text_h, x, y, text_w * scale, text_h * scale, 0x0); al_destroy_bitmap(bitmap); } return 0; }
static duk_ret_t js_RawFile_read(duk_context* ctx) { int n_args = duk_get_top(ctx); long num_bytes = n_args >= 1 ? duk_require_int(ctx, 0) : 0; bytearray_t* array; FILE* file; long pos; void* read_buffer; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): File has been closed"); if (n_args < 1) { // if no arguments, read entire file back to front pos = ftell(file); num_bytes = (fseek(file, 0, SEEK_END), ftell(file)); fseek(file, 0, SEEK_SET); } if (num_bytes <= 0) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "RawFile:read(): Read size out of range (%u)", num_bytes); if (!(read_buffer = malloc(num_bytes))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): Failed to allocate buffer for file read"); num_bytes = (long)fread(read_buffer, 1, num_bytes, file); if (n_args < 1) // reset file position after whole-file read fseek(file, pos, SEEK_SET); if (!(array = bytearray_from_buffer(read_buffer, (int)num_bytes))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): Failed to create byte array"); duk_push_sphere_bytearray(ctx, array); return 1; }
static duk_ret_t js_Font_finalize(duk_context* ctx) { font_t* font; font = duk_require_sphere_obj(ctx, 0, "Font"); free_font(font); return 0; }
static duk_ret_t js_RawFile_finalize(duk_context* ctx) { FILE* file; file = duk_require_sphere_obj(ctx, 0, "RawFile"); if (file != NULL) fclose(file); return 0; }
static duk_ret_t js_Logger_finalize(duk_context* ctx) { logger_t* logger; logger = duk_require_sphere_obj(ctx, 0, "Logger"); free_logger(logger); return 0; }
static duk_ret_t js_Logger_endBlock(duk_context* ctx) { logger_t* logger; duk_push_this(ctx); logger = duk_require_sphere_obj(ctx, -1, "Logger"); end_log_block(logger); return 0; }
static duk_ret_t js_Font_get_height(duk_context* ctx) { font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); duk_push_int(ctx, get_font_line_height(font)); return 1; }
static duk_ret_t js_Font_set_colorMask(duk_context* ctx) { font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_dup(ctx, 0); duk_put_prop_string(ctx, -2, "\xFF" "color_mask"); duk_pop(ctx); duk_pop(ctx); return 0; }
static duk_ret_t js_Logger_write(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); logger_t* logger; duk_push_this(ctx); logger = duk_require_sphere_obj(ctx, -1, "Logger"); write_log_line(logger, NULL, text); return 0; }
static duk_ret_t js_RawFile_get_position(duk_context* ctx) { FILE* file; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:position - File has been closed"); duk_push_int(ctx, ftell(file)); return 1; }
static duk_ret_t js_Font_getStringWidth(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); duk_push_int(ctx, get_text_width(font, text)); return 1; }
static duk_ret_t js_Logger_beginBlock(duk_context* ctx) { const char* title = duk_to_string(ctx, 0); logger_t* logger; duk_push_this(ctx); logger = duk_require_sphere_obj(ctx, -1, "Logger"); if (!begin_log_block(logger, title)) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Log:beginBlock(): Failed to create new log block"); return 0; }
static duk_ret_t js_Font_getCharacterImage(duk_context* ctx) { int cp = duk_require_int(ctx, 0); font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); duk_push_sphere_image(ctx, get_glyph_image(font, cp)); return 1; }
static duk_ret_t js_Font_clone(duk_context* ctx) { font_t* dolly_font; font_t* font; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); if (!(dolly_font = clone_font(font))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "Font:clone(): unable to clone font"); duk_push_sphere_font(ctx, dolly_font); return 1; }
static duk_ret_t js_RawFile_close(duk_context* ctx) { FILE* file; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_push_pointer(ctx, NULL); duk_put_prop_string(ctx, -2, "\xFF" "file_ptr"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:close(): File has been closed"); fclose(file); return 0; }
static duk_ret_t js_RawFile_write(duk_context* ctx) { bytearray_t* array; const void* data; FILE* file; size_t write_size; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:write(): File has been closed"); if (duk_is_string(ctx, 0)) data = duk_get_lstring(ctx, 0, &write_size); else { array = duk_require_sphere_obj(ctx, 0, "ByteArray"); data = get_bytearray_buffer(array); write_size = get_bytearray_size(array); } if (fwrite(data, 1, write_size, file) != write_size) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:write(): Write error. The file may be read-only."); return 0; }
static duk_ret_t js_RawFile_set_position(duk_context* ctx) { int new_pos = duk_require_int(ctx, 0); FILE* file; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:position - File has been closed"); if (!fseek(file, new_pos, SEEK_SET)) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:position - Failed to set read/write position"); return 0; }
static duk_ret_t js_RawFile_get_size(duk_context* ctx) { FILE* file; long file_pos; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:size - File has been closed"); file_pos = ftell(file); fseek(file, 0, SEEK_END); duk_push_int(ctx, ftell(file)); fseek(file, file_pos, SEEK_SET); return 1; }
static duk_ret_t js_Font_drawText(duk_context* ctx) { int x = duk_require_int(ctx, 0); int y = duk_require_int(ctx, 1); const char* text = duk_to_string(ctx, 2); font_t* font; color_t mask; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_get_prop_string(ctx, -1, "\xFF" "color_mask"); mask = duk_require_sphere_color(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (!screen_is_skipframe(g_screen)) draw_text(font, mask, x, y, TEXT_ALIGN_LEFT, text); return 0; }
static duk_ret_t js_RawFile_readString(duk_context* ctx) { size_t num_bytes = duk_require_uint(ctx, 0); FILE* file; void* read_buffer; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (num_bytes <= 0 || num_bytes > INT_MAX) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "RawFile:read(): Read size out of range (%i)", num_bytes / 1048576); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): File has been closed"); if (!(read_buffer = malloc(num_bytes))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): Failed to allocate buffer for file read"); num_bytes = fread(read_buffer, 1, num_bytes, file); duk_push_lstring(ctx, read_buffer, num_bytes); return 1; }
static duk_ret_t js_Font_drawTextBox(duk_context* ctx) { int x = duk_require_int(ctx, 0); int y = duk_require_int(ctx, 1); int w = duk_require_int(ctx, 2); int h = duk_require_int(ctx, 3); int offset = duk_require_int(ctx, 4); const char* text = duk_to_string(ctx, 5); font_t* font; int line_height; const char* line_text; color_t mask; int num_lines; int i; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_get_prop_string(ctx, -1, "\xFF" "color_mask"); mask = duk_require_sphere_color(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (!screen_is_skipframe(g_screen)) { duk_push_c_function(ctx, js_Font_wordWrapString, DUK_VARARGS); duk_push_this(ctx); duk_push_string(ctx, text); duk_push_int(ctx, w); duk_call_method(ctx, 2); duk_get_prop_string(ctx, -1, "length"); num_lines = duk_get_int(ctx, -1); duk_pop(ctx); line_height = get_font_line_height(font); for (i = 0; i < num_lines; ++i) { duk_get_prop_index(ctx, -1, i); line_text = duk_get_string(ctx, -1); duk_pop(ctx); draw_text(font, mask, x + offset, y, TEXT_ALIGN_LEFT, line_text); y += line_height; } duk_pop(ctx); } return 0; }
static duk_ret_t js_Font_getStringHeight(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); int width = duk_require_int(ctx, 1); font_t* font; int num_lines; duk_push_this(ctx); font = duk_require_sphere_obj(ctx, -1, "Font"); duk_pop(ctx); duk_push_c_function(ctx, js_Font_wordWrapString, DUK_VARARGS); duk_push_this(ctx); duk_push_string(ctx, text); duk_push_int(ctx, width); duk_call_method(ctx, 2); duk_get_prop_string(ctx, -1, "length"); num_lines = duk_get_int(ctx, -1); duk_pop(ctx); duk_pop(ctx); duk_push_int(ctx, get_font_line_height(font) * num_lines); return 1; }