Refactoring

This commit is contained in:
Philip Minakov 2024-02-21 23:14:49 +04:00
parent ea646e6654
commit 6b8f2f243e
No known key found for this signature in database
GPG key ID: 932F1B884934DB0F
7 changed files with 65 additions and 66 deletions

View file

@ -1,9 +1,9 @@
#include "../include/sie/freetype/freetype_cache.h"
#include "../include/sie/freetype/cache.h"
SIE_FT_CACHE *FT_CACHE;
unsigned int FT_CACHE_SIZE;
SIE_FT_CACHE *Sie_FT_Cache_GetOrCreate(const FT_Face *face, int font_size) {
SIE_FT_CACHE *FT_GetOrCreateCache(const FT_Face *face, int font_size) {
SIE_FT_CACHE *ft_cache = NULL;
for (unsigned int i = 0; i < FT_CACHE_SIZE; i++) {
if (FT_CACHE[i].face == face && FT_CACHE[i].font_size == font_size) {
@ -24,22 +24,7 @@ SIE_FT_CACHE *Sie_FT_Cache_GetOrCreate(const FT_Face *face, int font_size) {
}
}
void Sie_FT_Cache_Destroy() {
for (int i = 0; i < FT_CACHE_SIZE; i++) {
SIE_FT_CACHE ft_cache = FT_CACHE[i];
for (unsigned int j = 0; j < ft_cache.size; j++) {
SIE_FT_GLYPH_CACHE ft_wc_cache = ft_cache.cache[j];
mfree(ft_wc_cache.img->bitmap);
mfree(ft_wc_cache.img);
}
mfree(ft_cache.cache);
}
mfree(FT_CACHE);
FT_CACHE = NULL;
FT_CACHE_SIZE = 0;
}
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphGet(SIE_FT_CACHE *ft_cache, FT_ULong charcode) {
static SIE_FT_GLYPH_CACHE *GetGlyphCache(SIE_FT_CACHE *ft_cache, FT_ULong charcode) {
SIE_FT_GLYPH_CACHE *glyph_cache = NULL;
for (unsigned int i = 0; i < ft_cache->size; i++) {
glyph_cache = &(ft_cache->cache[i]);
@ -50,7 +35,7 @@ SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphGet(SIE_FT_CACHE *ft_cache, FT_ULong charc
return NULL;
}
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphAdd(FT_Face *face, SIE_FT_CACHE *ft_cache, FT_ULong charcode) {
static SIE_FT_GLYPH_CACHE *AddGlyphCache(FT_Face *face, SIE_FT_CACHE *ft_cache, FT_ULong charcode) {
FT_UInt glyph_index;
FT_Bitmap *bitmap;
@ -99,11 +84,11 @@ SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphAdd(FT_Face *face, SIE_FT_CACHE *ft_cache,
return cc_cache;
}
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphGetOrAdd(FT_Face *face, SIE_FT_CACHE *cache, FT_ULong charcode) {
SIE_FT_GLYPH_CACHE *FT_GlyphGetOrAddCache(FT_Face *face, SIE_FT_CACHE *cache, FT_ULong charcode) {
SIE_FT_GLYPH_CACHE *glyph_cache = NULL;
glyph_cache = Sie_FT_Cache_GlyphGet(cache, charcode);
glyph_cache = GetGlyphCache(cache, charcode);
if (!glyph_cache) {
glyph_cache = Sie_FT_Cache_GlyphAdd(face, cache, charcode);
glyph_cache = AddGlyphCache(face, cache, charcode);
}
return glyph_cache;
}

View file

@ -1,6 +1,6 @@
#include <swilib.h>
#include "../include/sie/gui/gui.h"
#include "../include/sie/freetype/freetype_cache.h"
#include "../include/sie/freetype/cache.h"
#define SS_TMR_MS_START (216 / 1.5)
#define SS_TMR_MS (16)
@ -22,7 +22,7 @@ static int GetTextY(int y, int y2, unsigned int h, int attr) {
static void DrawStr(WSHDR *ws, int x, int y, int x2, int x_offset, int font_size, const char *color) {
char rgb[3] = SIE_COLOR_TEXT_PRIMARY;
FT_Face *face = FT_FACE_REGULAR;
SIE_FT_CACHE *cache = Sie_FT_Cache_GetOrCreate(face, font_size);
SIE_FT_CACHE *cache = FT_GetOrCreateCache(face, font_size);
SIE_FT_GLYPH_CACHE *glyph_cache = NULL;
FT_Set_Pixel_Sizes(*face, 0, font_size);
@ -35,7 +35,7 @@ static void DrawStr(WSHDR *ws, int x, int y, int x2, int x_offset, int font_size
unsigned int width = 0;
for (int i = 0; i < wstrlen(ws); i++) {
unsigned short charcode = ws->wsbody[1 + i];
glyph_cache = Sie_FT_Cache_GlyphGetOrAdd(face, cache, charcode);
glyph_cache = FT_GlyphGetOrAddCache(face, cache, charcode);
width += glyph_cache->h_advance;
if (x_offset) {

View file

@ -1,14 +1,14 @@
#include <swilib.h>
#include <sys/param.h>
#include "../include/sie/gui/gui.h"
#include "../include/sie/freetype/freetype_cache.h"
#include "../include/sie/freetype/cache.h"
extern FT_Face *FT_FACE_REGULAR;
extern IMGHDR *BrushGlyphIMGHDR(IMGHDR *img, const char *color);
static SIE_FT_RENDER *Render(WSHDR *ws, int x, int x2, int font_size) {
FT_Face *face = FT_FACE_REGULAR;
SIE_FT_CACHE *cache = Sie_FT_Cache_GetOrCreate(face, font_size);
SIE_FT_CACHE *cache = FT_GetOrCreateCache(face, font_size);
FT_Set_Pixel_Sizes(*face, 0, font_size);
int line_id = 0;
@ -23,7 +23,7 @@ static SIE_FT_RENDER *Render(WSHDR *ws, int x, int x2, int font_size) {
render->lines = malloc(sizeof(SIE_FT_RENDER_LINE) * max_lines);
for (int i = 0; i < wstrlen(ws); i++) {
unsigned short charcode = ws->wsbody[1 + i];
SIE_FT_GLYPH_CACHE *glyph_cache = Sie_FT_Cache_GlyphGetOrAdd(face, cache, charcode);
SIE_FT_GLYPH_CACHE *glyph_cache = FT_GlyphGetOrAddCache(face, cache, charcode);
word_width += glyph_cache->h_advance;
if (word_width > x2 - x) { // break line
if (line_id > max_lines / 2) {
@ -59,7 +59,7 @@ static SIE_FT_RENDER *Render(WSHDR *ws, int x, int x2, int font_size) {
SIE_FT_RENDER *DrawText(WSHDR *ws, int x, int y, int x2, int y2, int font_size, int attr, const char *color) {
char rgb[] = SIE_COLOR_TEXT_PRIMARY;
FT_Face *face = FT_FACE_REGULAR;
SIE_FT_CACHE *ft_cache = Sie_FT_Cache_GetOrCreate(face, font_size);
SIE_FT_CACHE *ft_cache = FT_GetOrCreateCache(face, font_size);
FT_Set_Pixel_Sizes(*face, 0, font_size);
if (color) {
memcpy(rgb, color, 3);
@ -77,7 +77,7 @@ SIE_FT_RENDER *DrawText(WSHDR *ws, int x, int y, int x2, int y2, int font_size,
int max_v_advance = 0;
for (int i = 0; i < wstrlen(ws); i++) {
unsigned short charcode = ws->wsbody[1 + i];
SIE_FT_GLYPH_CACHE *glyph_cache = Sie_FT_Cache_GlyphGetOrAdd(face, ft_cache, charcode);
SIE_FT_GLYPH_CACHE *glyph_cache = FT_GlyphGetOrAddCache(face, ft_cache, charcode);
max_v_advance = MAX(max_v_advance, glyph_cache->v_advance);
SIE_FT_RENDER_LINE *line = (render->lines) ? &(render->lines[line_id]) : NULL;

View file

@ -2,7 +2,7 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#include "../include/sie/gui/gui.h"
#include "../include/sie/freetype/freetype_cache.h"
#include "../include/sie/freetype/cache.h"
extern char CFG_FONT_REGULAR_PATH[];
@ -29,7 +29,7 @@ void Sie_FT_Destroy() {
FT_Done_FreeType(*FT_LIBRARY);
mfree(FT_LIBRARY);
FT_LIBRARY = NULL;
Sie_FT_Cache_Destroy();
Sie_FT_DestroyCache();
}
}
}
@ -48,6 +48,23 @@ void Sie_FT_LoadFaces() {
SIE_FT_FACE_REGULAR_ID, FT_FACE_REGULAR);
}
void Sie_FT_DestroyCache() {
extern SIE_FT_CACHE *FT_CACHE;
extern unsigned int FT_CACHE_SIZE;
for (int i = 0; i < FT_CACHE_SIZE; i++) {
SIE_FT_CACHE ft_cache = FT_CACHE[i];
for (unsigned int j = 0; j < ft_cache.size; j++) {
SIE_FT_GLYPH_CACHE ft_wc_cache = ft_cache.cache[j];
mfree(ft_wc_cache.img->bitmap);
mfree(ft_wc_cache.img);
}
mfree(ft_cache.cache);
}
mfree(FT_CACHE);
FT_CACHE = NULL;
FT_CACHE_SIZE = 0;
}
int Sie_FT_GetMaxHeight(int font_size) {
FT_Face *face = FT_FACE_REGULAR;
FT_Set_Pixel_Sizes(*face, 0, font_size);
@ -58,14 +75,14 @@ int Sie_FT_GetMaxHeight(int font_size) {
void Sie_FT_GetStringSize(WSHDR *ws, int font_size, unsigned int *w, unsigned int *h) {
FT_Face *face = FT_FACE_REGULAR;
SIE_FT_CACHE *cache = Sie_FT_Cache_GetOrCreate(face, font_size);
SIE_FT_CACHE *cache = FT_GetOrCreateCache(face, font_size);
SIE_FT_GLYPH_CACHE *glyph_cache = NULL;
unsigned int width = 0, height = Sie_FT_GetMaxHeight(font_size);
FT_ULong charcode;
for (unsigned int i = 0; i < wstrlen(ws); i++) {
charcode = ws->wsbody[1 + i];
glyph_cache = Sie_FT_Cache_GlyphGetOrAdd(face, cache, charcode);
glyph_cache = FT_GlyphGetOrAddCache(face, cache, charcode);
width += glyph_cache->h_advance;
}
*w = width;

View file

@ -0,0 +1,28 @@
#ifndef __CACHE_H__
#define __CACHE_H__
#include <swilib.h>
#include <wchar.h>
#include <ft2build.h>
#include FT_FREETYPE_H
typedef struct {
FT_ULong charcode;
IMGHDR *img;
int y_offset;
int h_advance;
int v_advance;
int h_bearing_x;
} SIE_FT_GLYPH_CACHE;
typedef struct {
FT_Face *face;
int font_size;
SIE_FT_GLYPH_CACHE *cache;
unsigned int size;
} SIE_FT_CACHE;
SIE_FT_CACHE *FT_GetOrCreateCache(const FT_Face *face, int font_size);
SIE_FT_GLYPH_CACHE *FT_GlyphGetOrAddCache(FT_Face *face, SIE_FT_CACHE *cache, FT_ULong charcode);
#endif

View file

@ -31,6 +31,7 @@ typedef struct {
void Sie_FT_UnloadFaces();
void Sie_FT_LoadFaces();
void Sie_FT_DestroyCache();
int Sie_FT_GetMaxHeight(int font_size);
void Sie_FT_GetStringSize(WSHDR *ws, int font_size, unsigned int *w, unsigned int *h);

View file

@ -1,32 +0,0 @@
#ifndef __SIE_FREETYPE_CACHE_H__
#define __SIE_FREETYPE_CACHE_H__
#include <swilib.h>
#include <wchar.h>
#include <ft2build.h>
#include FT_FREETYPE_H
typedef struct {
FT_ULong charcode;
IMGHDR *img;
int y_offset;
int h_advance;
int v_advance;
int h_bearing_x;
} SIE_FT_GLYPH_CACHE;
typedef struct {
FT_Face *face;
int font_size;
SIE_FT_GLYPH_CACHE *cache;
unsigned int size;
} SIE_FT_CACHE;
SIE_FT_CACHE *Sie_FT_Cache_GetOrCreate(const FT_Face *face, int font_size);
void Sie_FT_Cache_Destroy();
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphGet(SIE_FT_CACHE *ft_cache, FT_ULong charcode);
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphAdd(FT_Face *face, SIE_FT_CACHE *ft_cache, FT_ULong charcode);
SIE_FT_GLYPH_CACHE *Sie_FT_Cache_GlyphGetOrAdd(FT_Face *face, SIE_FT_CACHE *cache, FT_ULong charcode);
#endif