static struct font_context char_size(int unicode, const char *utf8, int size, int edge) { const struct dfont_rect * rect = dfont_lookup(Dfont,unicode,FONT_SIZE,edge); struct font_context ctx; font_create(FONT_SIZE, &ctx); if (ctx.font == NULL) { ctx.w = 0; ctx.h = 0; return ctx; } if (rect == NULL) { font_size(utf8, unicode, &ctx); //see gen_char ctx.w += 1; ctx.h += 1; ctx.w = (ctx.w -1) * size / FONT_SIZE; ctx.h = ctx.h * size / FONT_SIZE; } else { ctx.w = (rect->w -1) * size / FONT_SIZE; ctx.h = rect->h * size / FONT_SIZE; } // font_release should not reset ctx.w/ctx.h font_release(&ctx); return ctx; }
static int draw_height(int unicode, const char *utf8, int size) { const struct dfont_rect * rect = dfont_lookup(Dfont,unicode,FONT_SIZE); if (rect == NULL) { rect = gen_char(unicode,utf8,size); } if (rect) { return rect->h * size / FONT_SIZE; } return 0; }
static int draw_size(int unicode, const char *utf8, int size, int gen_new) { const struct dfont_rect * rect = dfont_lookup(Dfont,unicode,FONT_SIZE); if (rect == NULL && gen_new) { rect = gen_char(unicode,utf8,size,Outline); } if (rect) { return (rect->w -1) * size / FONT_SIZE; } return 0; }
const struct dfont_rect * dfont_insert(struct dfont *df, int c, int font, int width, int height) { if (width > df->width) return NULL; assert(dfont_lookup(df,c,font) == NULL); for (;;) { struct font_line *line = find_line(df, width, height); if (line == NULL) break; struct hash_rect * hr = find_space(df, line, width); if (hr) { return insert_char(df,c,font,hr); } } struct hash_rect * hr = release_space(df, width, height); if (hr) { return insert_char(df,c,font,hr); } return NULL; }
static int draw_utf8(int unicode, int cx, int cy, int size, const struct srt *srt, uint32_t color, const struct sprite_trans *arg) { const struct dfont_rect * rect = dfont_lookup(Dfont, unicode, FONT_SIZE); if (rect == NULL) { return 0; } struct matrix tmp; struct matrix mat1 = {{ 1024,0,0,1024, cx* SCREEN_SCALE, cy*SCREEN_SCALE }}; struct matrix *m; if (arg->mat) { m=&tmp; matrix_mul(m, &mat1, arg->mat); } else { m=&mat1; } matrix_srt(m, srt); draw_rect(rect,size,m,color); return (rect->w-1) * size / FONT_SIZE ; }
static int ldfont_lookup(lua_State *L) { struct dfont *df = get_dfont(L); if (!df) { return luaL_error(L, "invalid dfont table"); } int id = luaL_checkinteger(L, 2); int rect_size = luaL_checkinteger(L, 3); const struct dfont_rect * rect = dfont_lookup(df, id, rect_size, 0); if (rect) { lua_pushinteger(L, rect->x); lua_pushinteger(L, rect->y); lua_pushinteger(L, rect->w); lua_pushinteger(L, rect->h); return 4; } else { return 0; } }
void label_rawdraw(const char * str, float fx, float y, struct pack_label * l) { shader_texture(Tex, 0); uint32_t color = l->color; int edge = l->edge; int size = l->size; int width = raw_width(str, l); int x = (int)fx; switch (l->align) { case LABEL_ALIGN_LEFT: break; case LABEL_ALIGN_RIGHT: x += l->width - width; break; case LABEL_ALIGN_CENTER: x += (l->width - width)/2; break; } char utf8[7]; int i; struct matrix mat = {{ 1024,0,0,1024,0,y * SCREEN_SCALE}}; for (i=0; str[i];) { int len = unicode_len(str[i]); int unicode = copystr(utf8, str+i, len); i+=len; const struct dfont_rect * rect = dfont_lookup(Dfont, unicode, FONT_SIZE, edge); if (rect == NULL) { rect = gen_char(unicode,utf8,FONT_SIZE,edge); if (rect == NULL) continue; } mat.m[4]=x*SCREEN_SCALE; draw_rect(rect,size,&mat,color,0); x += (rect->w-1) * size / FONT_SIZE + l->space_w; } }
static int ldfont_insert(lua_State *L) { struct dfont *df = get_dfont(L); if (!df) { return luaL_error(L, "invalid dfont table"); } lua_getfield(L, 1, "texture"); int tid = luaL_checkinteger(L, -1); lua_pop(L, 1); RID tex = texture_glid(tid); int id = luaL_checkinteger(L, 2); int rect_size = luaL_checkinteger(L, 3); int w = luaL_checkinteger(L, 4); int h = luaL_checkinteger(L, 5); void* buff = lua_touserdata(L, 6); const struct dfont_rect * rect = dfont_lookup(df, id, rect_size, 0); if (rect==NULL) { rect = dfont_insert(df, id, rect_size, w, h, 0); if (rect) { render_texture_subupdate(R, tex, buff, rect->x, rect->y, rect->w, rect->h); } } if (rect) { lua_pushinteger(L, rect->x); lua_pushinteger(L, rect->y); lua_pushinteger(L, rect->w); lua_pushinteger(L, rect->h); return 4; } else { return 0; } }