void vita2d_pgf_text_dimensions(vita2d_pgf *font, float scale, const char *text, int *width, int *height) { int pen_x = 0; int max_h = 0; while (*text) { unsigned int character = *text++; if (!texture_atlas_exists(font->tex_atlas, character)) { if (!atlas_add_glyph_pgf(font, character)) { continue; } } bp2d_rectangle rect; int bitmap_left, bitmap_top; int advance_x, advance_y; if (!texture_atlas_get(font->tex_atlas, character, &rect, &bitmap_left, &bitmap_top, &advance_x, &advance_y, NULL)) continue; if (rect.h > max_h) max_h = rect.h; pen_x += (advance_x >> 6) * scale; } if (width) *width = pen_x; if (height) *height = max_h; }
int vita2d_pgf_draw_text(vita2d_pgf *font, int x, int y, unsigned int color, float scale, const char *text) { int pen_x = x; while (*text) { unsigned int character = *text++; if (!texture_atlas_exists(font->tex_atlas, character)) { if (!atlas_add_glyph_pgf(font, character)) { continue; } } bp2d_rectangle rect; int bitmap_left, bitmap_top; int advance_x, advance_y; if (!texture_atlas_get(font->tex_atlas, character, &rect, &bitmap_left, &bitmap_top, &advance_x, &advance_y, NULL)) continue; vita2d_draw_texture_tint_part_scale(font->tex_atlas->tex, pen_x + bitmap_left * scale, y - bitmap_top * scale, rect.x, rect.y, rect.w, rect.h, scale, scale, color); pen_x += (advance_x >> 6) * scale; } return pen_x - x; }
void vita2d_font_draw_text(vita2d_font *font, int x, int y, unsigned int color, unsigned int size, const char *text) { FTC_FaceID face_id = (FTC_FaceID)font; FT_Face face; FTC_Manager_LookupFace(font->ftcmanager, face_id, &face); FT_Int charmap_index; charmap_index = FT_Get_Charmap_Index(face->charmap); FT_Glyph glyph; FT_Bool use_kerning = FT_HAS_KERNING(face); FT_UInt glyph_index, previous = 0; int pen_x = x; int pen_y = y + size; FTC_ScalerRec scaler; scaler.face_id = face_id; scaler.width = size; scaler.height = size; scaler.pixel = 1; FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL; while (*text) { glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text); if (use_kerning && previous && glyph_index) { FT_Vector delta; FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta); pen_x += delta.x >> 6; } if (!texture_atlas_exists(font->tex_atlas, glyph_index)) { FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL); if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph, size)) { continue; } } bp2d_rectangle rect; int bitmap_left, bitmap_top; int advance_x, advance_y; int glyph_size; texture_atlas_get(font->tex_atlas, glyph_index, &rect, &bitmap_left, &bitmap_top, &advance_x, &advance_y, &glyph_size); const float draw_scale = size/(float)glyph_size; vita2d_draw_texture_tint_part_scale(font->tex_atlas->tex, pen_x + bitmap_left * draw_scale, pen_y - bitmap_top * draw_scale, rect.x, rect.y, rect.w, rect.h, draw_scale, draw_scale, color); pen_x += (advance_x >> 16) * draw_scale; pen_y += (advance_y >> 16) * draw_scale; previous = glyph_index; text++; }