Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ATLAS_W 512
- #define ATLAS_H 512
- typedef struct {
- int x, y, w, h, left, top, v;
- int advance_x, advance_y;
- } atlas_position;
- struct sftd_font {
- sftd_font_load_from from;
- union {
- char *filename;
- struct {
- const void *font_buffer;
- unsigned int buffer_size;
- };
- };
- FTC_CMapCache cmapcache;
- FTC_ImageCache imagecache;
- // Atlas
- sf2d_texture *atlas;
- atlas_position atlas_pos[65536];
- int atlas_x;
- int atlas_y;
- };
- sftd_font *sftd_load_font_mem(const void *buffer, unsigned int size)
- {
- sftd_font *font = malloc(sizeof(*font));
- font->font_buffer = buffer;
- font->buffer_size = size;
- FTC_CMapCache_New(ftcmanager, &font->cmapcache);
- FTC_ImageCache_New(ftcmanager, &font->imagecache);
- // Atlas
- font->atlas = sf2d_create_texture(ATLAS_W, ATLAS_H, TEXFMT_RGBA8, SF2D_PLACE_RAM);
- memset(font->atlas_pos, 0, sizeof(*font->atlas_pos) * 65536);
- font->atlas->tiled = 1;
- font->atlas_x = 0;
- font->atlas_y = 0;
- font->from = SFTD_LOAD_FROM_MEM;
- return font;
- }
- void sftd_draw_text(sftd_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(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;
- 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 (font->atlas_pos[glyph_index].v) {
- sf2d_draw_texture_part(font->atlas,
- pen_x + font->atlas_pos[glyph_index].left + x,
- pen_y - font->atlas_pos[glyph_index].top + y,
- font->atlas_pos[glyph_index].x,
- font->atlas_pos[glyph_index].y,
- font->atlas_pos[glyph_index].w,
- font->atlas_pos[glyph_index].h);
- pen_x += font->atlas_pos[glyph_index].advance_x >> 16;
- pen_y += font->atlas_pos[glyph_index].advance_y >> 16;
- } else {
- FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);
- if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
- FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
- FT_Bitmap *bitmap = &bitmap_glyph->bitmap;
- // Get next atlas pos;
- if (font->atlas_x + size > ATLAS_W) {
- font->atlas_x = 0;
- font->atlas_y += size;
- }
- //printf("atlas_x: %i atlas_y: %i\n", font->atlas_x, font->atlas_y);
- //printf("bwidth: %i brows: %i\n", bitmap->width, bitmap->rows);
- //draw_bitmap(&bitmap_glyph->bitmap, pen_x + bitmap_glyph->left + x, pen_y - bitmap_glyph->top + y, color);
- int j, k;
- for (j = 0; j < bitmap->rows; j++) {
- for (k = 0; k < bitmap->width; k++) {
- sf2d_set_pixel(font->atlas,
- (font->atlas_x + k),
- (font->atlas_y + j),
- __builtin_bswap32((color & ~0xFF) | bitmap->buffer[j*bitmap->width + k]));
- }
- }
- font->atlas_pos[glyph_index].left = bitmap_glyph->left;
- font->atlas_pos[glyph_index].top = bitmap_glyph->top;
- font->atlas_pos[glyph_index].x = font->atlas_x;
- font->atlas_pos[glyph_index].y = font->atlas_y;
- font->atlas_pos[glyph_index].w = bitmap->width;
- font->atlas_pos[glyph_index].h = bitmap->rows;
- font->atlas_pos[glyph_index].advance_x = bitmap_glyph->root.advance.x;
- font->atlas_pos[glyph_index].advance_y = bitmap_glyph->root.advance.y;
- font->atlas_pos[glyph_index].v = 1;
- sf2d_draw_texture_part(font->atlas,
- pen_x + font->atlas_pos[glyph_index].left + x,
- pen_y - font->atlas_pos[glyph_index].top + y,
- font->atlas_pos[glyph_index].x,
- font->atlas_pos[glyph_index].y,
- font->atlas_pos[glyph_index].w,
- font->atlas_pos[glyph_index].h);
- font->atlas_x += size;
- pen_x += bitmap_glyph->root.advance.x >> 16;
- pen_y += bitmap_glyph->root.advance.y >> 16;
- }
- }
- previous = glyph_index;
- text++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement