Skip to content

Commit b30a8e1

Browse files
authored
Fix: LoadFont in libschrift lib that was crashing in Star6C (#97)
1 parent bb66a1d commit b30a8e1

9 files changed

Lines changed: 136 additions & 54 deletions

File tree

bmp/lib/schrift.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,14 @@ SFT_Font *sft_loadfile(char const *filename) {
238238
return font;
239239
}
240240

241-
void sft_freefont(SFT_Font *font) {
241+
int sft_freefont(SFT_Font *font) {
242242
if (!font)
243-
return;
243+
return 0;
244244
/* Only unmap if we mapped it ourselves. */
245245
if (font->source == SrcMapping)
246246
unmap_file(font);
247-
free(font);
247+
free(font);
248+
return 1;
248249
}
249250

250251
int sft_lmetrics(const SFT *sft, SFT_LMetrics *metrics) {
@@ -481,29 +482,53 @@ static void unmap_file(SFT_Font *font) {
481482
#else
482483

483484
static int map_file(SFT_Font *font, const char *filename) {
484-
struct stat info;
485-
int fd;
486-
font->memory = MAP_FAILED;
487-
font->size = 0;
488-
font->source = SrcMapping;
489-
if ((fd = open(filename, O_RDONLY)) < 0) {
490-
return -1;
491-
}
492-
if (fstat(fd, &info) < 0) {
493-
close(fd);
494-
return -1;
495-
}
496-
/* FIXME do some basic validation on info.st_size maybe - it is signed for
497-
* example, so it *could* be negative .. */
498-
font->memory = mmap(NULL, (size_t)info.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
499-
font->size = (uint_fast32_t)info.st_size;
500-
close(fd);
501-
return font->memory == MAP_FAILED ? -1 : 0;
485+
FILE *fp;
486+
size_t n;
487+
long size;
488+
489+
font->memory = NULL;
490+
font->size = 0;
491+
font->source = SrcMapping; /* still mark it so unmap_file knows it should free */
492+
493+
fp = fopen(filename, "rb");
494+
if (!fp)
495+
return -1;
496+
497+
if (fseek(fp, 0, SEEK_END) < 0) {
498+
fclose(fp);
499+
return -1;
500+
}
501+
size = ftell(fp);
502+
if (size < 0) {
503+
fclose(fp);
504+
return -1;
505+
}
506+
rewind(fp);
507+
508+
font->memory = malloc((size_t)size);
509+
if (!font->memory) {
510+
fclose(fp);
511+
return -1;
512+
}
513+
514+
n = fread((void *)font->memory, 1, (size_t)size, fp);
515+
fclose(fp);
516+
517+
if (n != (size_t)size) {
518+
free((void *)font->memory);
519+
font->memory = NULL;
520+
return -1;
521+
}
522+
523+
font->size = (uint_fast32_t)size;
524+
return 0;
502525
}
503526

504527
static void unmap_file(SFT_Font *font) {
505-
assert(font->memory != MAP_FAILED);
506-
munmap((void *)font->memory, font->size);
528+
if (font->memory) {
529+
free((void *)font->memory);
530+
font->memory = NULL;
531+
}
507532
}
508533

509534
#endif

bmp/lib/schrift.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const char *sft_version(void);
7373

7474
SFT_Font *sft_loadmem(const void *mem, size_t size);
7575
SFT_Font *sft_loadfile(const char *filename);
76-
void sft_freefont(SFT_Font *font);
76+
int sft_freefont(SFT_Font *font);
7777

7878
int sft_lmetrics(const SFT *sft, SFT_LMetrics *metrics);
7979
int sft_lookup(const SFT *sft, SFT_UChar codepoint, SFT_Glyph *glyph);

bmp/region.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "region.h"
22

3+
34
#if defined(__INFINITY6C__)
5+
#include "bmp/star/i6c_hal.h" // always include prototypes
46
#include "bmp/star/i6c_hal.c"
57
#endif
68

9+
710
// https://wx.comake.online/doc/ds82ff82j7jsd9-SSD220/customer/development/mi/en/exclude/mi_rgn.html
811

912
#ifdef __SIGMASTAR__
@@ -16,6 +19,28 @@ int PIXEL_FORMAT_DEFAULT = 3; // 0 for PIXEL_FORMAT_1555 , 4 for E_MI_RGN_PIXEL_
1619
extern bool verbose;
1720
const double inv16 = 1.0 / 16.0;
1821

22+
23+
24+
int InitRGN_SigmaStar(){
25+
#if __INFINITY6C__
26+
if (i6c_hal_init())
27+
fprintf(stderr, "[%s:%d]MI_SYS_Init failed with!\n", __func__, __LINE__);
28+
//int s32Ret = i6c_region_init(&g_stPaletteTable);
29+
int s32Ret = i6c_region_init((i6c_rgn_pal *)&g_stPaletteTable); //We are sure the structure is the same
30+
if (verbose)
31+
printf("MI_RGN_Init_6c results: %d \n", s32Ret);
32+
if (s32Ret)
33+
fprintf(stderr, "[%s:%d]RGN_Init_6c failed with %#x!\n", __func__, __LINE__, s32Ret);
34+
35+
#else
36+
int s32Ret = MI_RGN_Init(DEV &g_stPaletteTable);
37+
if (verbose)
38+
printf("MI_RGN_Init results: %d\n", s32Ret);
39+
if (s32Ret)
40+
fprintf(stderr, "[%s:%d]RGN_Init failed with %#x!\n", __func__, __LINE__, s32Ret);
41+
#endif
42+
}
43+
1944
int create_region(int *handle, int x, int y, int width, int height) {
2045
int s32Ret = -1;
2146
#if !defined(_x86) && !defined(__ROCKCHIP__)

bmp/region.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int set_bitmap(int handle, BITMAP *bitmap);
1818
unsigned long set_bitmapEx(int handle, BITMAP *bitmap, int BitsPerPixel);
1919
int unload_region(int *handle);
2020
void *get_directBMP(int handle);
21+
int InitRGN_SigmaStar();
2122
#ifdef __SIGMASTAR__
2223
int GetCanvas(int handle, MI_RGN_CanvasInfo_t *stCanvasInfo);
2324
uint32_t ST_OSD_DrawPoint(

bmp/star/i6c_hal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void i6c_region_destroy(char handle)
117117
i6c_rgn.fnDestroyRegion(0, handle);
118118
}
119119

120-
void i6c_region_init(i6c_rgn_pal *palette)
120+
int i6c_region_init(i6c_rgn_pal *palette)
121121
{
122122
//i6c_rgn_pal palette = {{{0, 0, 0, 0}}};
123123
i6c_rgn.fnInit(0, palette);

bmp/star/i6c_hal.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
#ifndef I6C_HAL_H
3+
#define I6C_HAL_H
4+
15
#pragma once
26

37
#include "i6c_common.h"
@@ -6,6 +10,7 @@
610

711
#include <sys/select.h>
812
#include <unistd.h>
13+
914

1015
extern char keepRunning;
1116

@@ -15,8 +20,9 @@ int i6c_hal_init(void);
1520
int i6c_region_create(char handle, hal_rect rect, short opacity);
1621
void i6c_region_deinit(void);
1722
void i6c_region_destroy(char handle);
18-
void i6c_region_init(i6c_rgn_pal *palette);
23+
int i6c_region_init(i6c_rgn_pal *palette);
1924
int i6c_region_setbitmap(int handle, hal_bitmap *bitmap);
2025

2126
void i6c_system_deinit(void);
22-
int i6c_system_init(void);
27+
int i6c_system_init(void);
28+
#endif

bmp/text.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,34 @@ static inline void calcdim(double *margin, double *height, double *width, const
107107
*width = MAX(*width, lwidth) + 2 * *margin;
108108
}
109109

110+
SFT *GetCachedFont(const char *font, double size) {
111+
if (sft.font == NULL || strcmp(last_font_name, font) != 0 || size!=last_font_size) {
112+
if (sft.font) {
113+
sft_freefont(sft.font);
114+
}
115+
loadfont(&sft, font, size, &lmtx);
116+
strncpy(last_font_name, font, sizeof(last_font_name) - 1);
117+
last_font_name[sizeof(last_font_name) - 1] = '\0';
118+
last_font_size=size;
119+
//printf("TrueType Font %s size:%f loaded.\n",last_font_name,last_font_size);
120+
}
121+
122+
return &sft;
123+
}
124+
125+
int FreeCachedFont() {
126+
if (sft.font) {
127+
//printf("Unload TrueType font %s sft.font = %p\n",last_font_name, sft.font);
128+
sft_freefont(sft.font);
129+
return 1;
130+
}
131+
132+
return 0;
133+
}
134+
110135
RECT measure_text(const char *font, double size, const char *text) {
111-
loadfont(&sft, font, size, &lmtx);
136+
//loadfont(&sft, font, size, &lmtx);
137+
GetCachedFont(font, size);//We will usually have only one font file. lets make it faster
112138

113139
double margin, height, width;
114140
calcdim(&margin, &height, &width, text);
@@ -118,12 +144,13 @@ RECT measure_text(const char *font, double size, const char *text) {
118144
rect.height += rect.height & 1;
119145
rect.width += rect.width & 1;
120146

121-
sft_freefont(sft.font);
147+
//sft_freefont(sft.font);
122148
return rect;
123149
}
124150

125151
BITMAP raster_text(const char *font, double size, const char *text, uint16_t color) {
126-
loadfont(&sft, font, size, &lmtx);
152+
//loadfont(&sft, font, size, &lmtx);
153+
GetCachedFont(font, size);//We will usually have only one font file. lets make it faster
127154

128155
double margin, height, width;
129156

@@ -175,6 +202,6 @@ BITMAP raster_text(const char *font, double size, const char *text, uint16_t col
175202
bitmap.pData = canvas.pixels;
176203
bitmap.enPixelFormat = PIXEL_FORMAT_1555;
177204

178-
sft_freefont(sft.font);
205+
//sft_freefont(sft.font);
179206
return bitmap;
180207
}

bmp/text.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ extern "C" {
1212
#include "common.h"
1313

1414
static SFT sft;
15+
static char last_font_name[256] = "";
16+
static double last_font_size=0;
1517
static SFT_Image canvas;
1618
static SFT_LMetrics lmtx;
1719
static BITMAP bitmap;
1820

1921
RECT measure_text(const char *font, double size, const char *text);
2022
BITMAP raster_text(const char *font, double size, const char *text, uint16_t color);
2123

24+
int FreeCachedFont();
25+
2226
#ifdef __cplusplus
2327
#if __cplusplus
2428
}

osd.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,8 +2445,10 @@ static void InitMSPHook() {
24452445

24462446
int rgn = 0;
24472447

2448-
osds = mmap(
2449-
NULL, sizeof(*osds) * MAX_OSD, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
2448+
// osds = mmap(NULL, sizeof(*osds) * MAX_OSD, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
2449+
osds = malloc(sizeof(*osds) * MAX_OSD);
2450+
2451+
24502452

24512453
for (int id = 0; id < MAX_OSD; id++) {
24522454
osds[id].hand = id;
@@ -2459,22 +2461,7 @@ static void InitMSPHook() {
24592461
}
24602462

24612463
#ifdef __SIGMASTAR__
2462-
#if __INFINITY6C__
2463-
if (i6c_hal_init())
2464-
fprintf(stderr, "[%s:%d]MI_SYS_Init failed with!\n", __func__, __LINE__);
2465-
int s32Ret = i6c_region_init(&g_stPaletteTable);
2466-
if (verbose)
2467-
printf("MI_RGN_Init_6c results: %d \n", s32Ret);
2468-
if (s32Ret)
2469-
fprintf(stderr, "[%s:%d]RGN_Init_6c failed with %#x!\n", __func__, __LINE__, s32Ret);
2470-
2471-
#else
2472-
int s32Ret = MI_RGN_Init(DEV &g_stPaletteTable);
2473-
if (verbose)
2474-
printf("MI_RGN_Init results: %d\n", s32Ret);
2475-
if (s32Ret)
2476-
fprintf(stderr, "[%s:%d]RGN_Init failed with %#x!\n", __func__, __LINE__, s32Ret);
2477-
#endif
2464+
InitRGN_SigmaStar();
24782465
#endif
24792466

24802467
int XOffs = (majestic_width - OVERLAY_WIDTH) / 2;
@@ -2605,7 +2592,9 @@ static void InitMSPHook() {
26052592

26062593
#endif
26072594
// free(bitmap.pData);
2608-
} else { // no font file still, show message on screen
2595+
} else { // no font file still, show message on screen
2596+
useDirectBMPBuffer = true;
2597+
printf("Use Direct Video Memory MODE!\n");
26092598
cntr = 0;
26102599
char msgbuff[120];
26112600
sprintf(msgbuff, "&F48 &L23 Waiting for data on %s ...", _port_name);
@@ -2633,6 +2622,8 @@ static void InitMSPHook() {
26332622
}
26342623

26352624
static void CloseMSP() {
2625+
2626+
26362627
int deinit = 0;
26372628
int s32Ret = 0;
26382629
#ifdef __SIGMASTAR__
@@ -2655,8 +2646,11 @@ static void CloseMSP() {
26552646
free(bmpBuff.pData);
26562647
if (bmpFntSmall.pData != NULL)
26572648
free(bmpFntSmall.pData);
2658-
2659-
int res_mun = munmap(osds, sizeof(*osds) * MAX_OSD);
2660-
2661-
printf("RGN_Destroy: %X, RGN_DeInit: %X, unmap: %d\n", s32Ret, deinit, res_mun);
2649+
2650+
printf("TrueType Font released: %d\n",FreeCachedFont(sft.font) );
2651+
2652+
//int res_mun = munmap(osds, sizeof(*osds) * MAX_OSD);
2653+
if (osds!=NULL)
2654+
free(osds);
2655+
printf("RGN_Destroy: %X, RGN_DeInit: %X\n", s32Ret, deinit);
26622656
}

0 commit comments

Comments
 (0)