This commit is contained in:
Philip Minakov 2024-02-21 12:44:41 +04:00
parent 842c8b03d7
commit 41955c8367
No known key found for this signature in database
GPG key ID: 932F1B884934DB0F
10 changed files with 129 additions and 18 deletions

19
src/bcfg.c Normal file
View file

@ -0,0 +1,19 @@
#include "include/sie/config_loader.h"
char BCFG_PATH[64] = "0:\\zbin\\etc\\SieLibrary.bcfg";
void Sie_BCFG_Load() {
__CONFIG_EXTERN(0, cfghdr_fonts_b);
if (Sie_Config_Load(BCFG_PATH, __CONFIG(0, cfghdr_fonts_b))) {
return;
}
BCFG_PATH[0] = '4';
if (Sie_Config_Load(BCFG_PATH, __CONFIG(0, cfghdr_fonts_b))) {
return;
}
BCFG_PATH[0] = '\0';
}
const char *Sie_BCFG_GetPath() {
return BCFG_PATH;
}

13
src/config_data.c Normal file
View file

@ -0,0 +1,13 @@
#include <cfg_items.h>
__CFG_BEGIN(0)
__root CFG_HDR cfghdr_fonts_b = {CFG_LEVEL, "Fonts", 1, 0};
__root CFG_HDR cfghdr_fonts_0 = {CFG_STR_UTF8, "Font regular path", 3, 127};
__root char CFG_FONT_REGULAR_PATH[128] = "0:\\zbin\\fonts\\Roboto-Regular.ttf";
__root CFG_HDR cfghdr_fonts_e = {CFG_LEVEL, "", 0, 0};
__CFG_END(0)

50
src/config_loader.c Normal file
View file

@ -0,0 +1,50 @@
#include <swilib.h>
#include <stdlib.h>
#include <cfg_items.h>
static size_t GetSize(CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) {
volatile unsigned int segb = (volatile unsigned int)config_begin;
volatile unsigned int sege = (volatile unsigned int)config_end;
return (sege - segb) - 4;
}
int Sie_Config_Save(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) {
unsigned int err;
int fp = _open(path, A_ReadWrite + A_Create + A_Truncate,
P_READ + P_WRITE, &err);
if (fp != -1) {
size_t size = GetSize(cfghdr0, config_begin, config_end);
int write_size = _write(fp, cfghdr0, (int)size, &err);
_close(fp, &err);
if (size == write_size) {
return 1;
}
}
return 0;
}
int Sie_Config_Load(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end) {
int result = 0;
size_t size = GetSize(cfghdr0, config_begin, config_end);
char *buf = malloc(size);
if (buf) {
unsigned int err;
int fp = _open(path, A_ReadOnly + A_BIN, 0, &err);
if (fp != -1) {
if (_read(fp, buf, (int)size, &err) == size) {
memcpy(cfghdr0, buf, size);
_close(fp, &err);
result = 2;
} else {
_close(fp, &err);
goto SAVE_NEW_CFG;
}
} else {
SAVE_NEW_CFG:
result = Sie_Config_Save(path, cfghdr0, config_begin, config_end);
}
mfree(buf);
}
return result;
}

View file

@ -72,8 +72,8 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2,
unsigned int w = 0, h = 0;
WSHDR *copy_ws = AllocWS(len);
wstrcpy(copy_ws, ws);
Sie_FT_GetStringSize(copy_ws, font_size, &w, &h);
if (attr & SIE_FT_TEXT_ALIGN_CENTER) {
Sie_FT_GetStringSize(copy_ws, font_size, &w, &h);
if (w <= x2 - x) {
x_text = x + (x2 - x - (int)w) / 2;
} else {
@ -85,7 +85,6 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2,
if (len <= 1) {
break;
} else {
Sie_FT_GetStringSize(copy_ws, font_size, &w, &h);
x_text = x2 - (int)w;
if (w > x2 -x) {
wsRemoveChars(copy_ws, 1, 2);
@ -101,7 +100,6 @@ static void DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2,
if (len <= 1) {
break;
} else {
Sie_FT_GetStringSize(copy_ws, font_size, &w, &h);
if (w > x2 - x) {
wsRemoveChars(copy_ws, len - 1, len);
len -= 1;

View file

@ -4,19 +4,17 @@
#include "../include/sie/gui/gui.h"
#include "../include/sie/freetype/freetype_cache.h"
FT_Library *FT_LIBRARY = NULL;
FT_Face *FT_FACE_REGULAR = NULL;
unsigned int FT_CLIENTS_COUNT = 0;
extern char CFG_FONT_REGULAR_PATH[];
FT_Library *FT_LIBRARY;
FT_Face *FT_FACE_REGULAR;
unsigned int FT_CLIENTS_COUNT;
void Sie_FT_Init() {
if (!FT_LIBRARY) {
FT_LIBRARY = malloc(sizeof(FT_Library));
FT_Init_FreeType(FT_LIBRARY);
if (!FT_FACE_REGULAR) {
FT_FACE_REGULAR = malloc(sizeof(FT_Face));
FT_New_Face(*FT_LIBRARY, "0:\\ZBin\\fonts\\Roboto-Regular.ttf",
0, FT_FACE_REGULAR);
}
Sie_FT_LoadFaces();
}
FT_CLIENTS_COUNT++;
}
@ -27,11 +25,7 @@ void Sie_FT_Destroy() {
}
if (!FT_CLIENTS_COUNT) {
if (FT_LIBRARY) {
if (FT_FACE_REGULAR) {
FT_Done_Face(*FT_FACE_REGULAR);
mfree(FT_FACE_REGULAR);
FT_FACE_REGULAR = NULL;
}
Sie_FT_UnloadFaces();
FT_Done_FreeType(*FT_LIBRARY);
mfree(FT_LIBRARY);
FT_LIBRARY = NULL;
@ -40,6 +34,20 @@ void Sie_FT_Destroy() {
}
}
void Sie_FT_UnloadFaces() {
if (FT_FACE_REGULAR) {
FT_Done_Face(*FT_FACE_REGULAR);
mfree(FT_FACE_REGULAR);
FT_FACE_REGULAR = NULL;
}
}
void Sie_FT_LoadFaces() {
FT_FACE_REGULAR = malloc(sizeof(FT_Face));
FT_New_Face(*FT_LIBRARY, CFG_FONT_REGULAR_PATH,
SIE_FT_FACE_REGULAR_ID, FT_FACE_REGULAR);
}
int Sie_FT_GetMaxHeight(int font_size) {
FT_Face *face = FT_FACE_REGULAR;
FT_Set_Pixel_Sizes(*face, 0, font_size);

7
src/include/sie/bcfg.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef __SIE_BCFG_H__
#define __SIE_BCFG_H__
void Sie_BCFG_Load();
const char *Sie_BCFG_GetPath();
#endif

View file

@ -0,0 +1,9 @@
#ifndef __SIE_CONFIG_LOADER_H__
#define __SIE_CONFIG_LOADER_H__
#include <cfg_items.h>
int Sie_Config_Save(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end);
int Sie_Config_Load(const char *path, CFG_HDR *cfghdr0, const int *config_begin, const int *config_end);
#endif

View file

@ -6,6 +6,8 @@
#include "draw_text.h"
#include "draw_string.h"
#define SIE_FT_FACE_REGULAR_ID 0x00
#define SIE_FT_TEXT_ALIGN_LEFT TEXT_ALIGNLEFT
#define SIE_FT_TEXT_ALIGN_CENTER TEXT_ALIGNMIDDLE
#define SIE_FT_TEXT_ALIGN_RIGHT TEXT_ALIGNRIGHT
@ -27,10 +29,12 @@ typedef struct {
void (*OnBeforeDraw)(int x, int y, int x2, int y2);
} SIE_FT_SCROLL_STRING;
void Sie_FT_UnloadFaces();
void Sie_FT_LoadFaces();
int Sie_FT_GetMaxHeight(int font_size);
void Sie_FT_GetStringSize(WSHDR *ws, int font_size, unsigned int *w, unsigned int *h);
void Sie_FT_DrawBoundingString(WSHDR *ws, int x, int y, int x2, int y2, int font_size, int attr, const char *color);
void Sie_FT_DrawBoundingScrollString(SIE_FT_SCROLL_STRING *ss, GBSTMR *tmr);

View file

@ -3,10 +3,12 @@
#include "ext.h"
#include "exec.h"
#include "bcfg.h"
#include "strings.h"
#include "subproc.h"
#include "settings.h"
#include "resources.h"
#include "config_loader.h"
#include "fs/fs.h"
#include "gui/gui.h"
#include "freetype/freetype.h"

View file

@ -1,10 +1,11 @@
#include <swilib.h>
#include "include/sie/bcfg.h"
__attribute__((constructor))
void InitLibrary() {
extern void Sie_FT_Init();
extern void Sie_SubProc_Init();
extern void Sie_Resources_Init();
Sie_BCFG_Load();
Sie_FT_Init();
Sie_SubProc_Init();
Sie_Resources_Init();