factor out message printing, use const where possible

This commit is contained in:
Caleb Connolly 2023-02-19 16:53:42 +00:00
parent d7e9eca325
commit 7b714e433e
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
3 changed files with 85 additions and 72 deletions

View file

@ -182,7 +182,7 @@ NSVGpath* nsvgDuplicatePath(NSVGpath* p);
// Deletes an image. // Deletes an image.
void nsvgDelete(NSVGimage* image); void nsvgDelete(NSVGimage* image);
NSVGshape** nsvgGetTextShapes(NSVGimage* image, char* text, int textLen); NSVGshape** nsvgGetTextShapes(NSVGimage* image, const char* text, int textLen);
#ifndef NANOSVG_CPLUSPLUS #ifndef NANOSVG_CPLUSPLUS
#ifdef __cplusplus #ifdef __cplusplus
@ -2963,7 +2963,7 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
} }
} }
NSVGshape** nsvgGetTextShapes(NSVGimage* image, char* text, int textLen) NSVGshape** nsvgGetTextShapes(NSVGimage* image, const char* text, int textLen)
{ {
NSVGshape *shape = NULL; NSVGshape *shape = NULL;
NSVGshape **ret = calloc(textLen, sizeof(shape)); // array of paths, text to render NSVGshape **ret = calloc(textLen, sizeof(shape)); // array of paths, text to render

View file

@ -69,7 +69,7 @@ void nsvgDeleteRasterizer(NSVGrasterizer*);
void nsvgRasterizeText(NSVGrasterizer* r, void nsvgRasterizeText(NSVGrasterizer* r,
NSVGimage* font, float tx, float ty, float scale, NSVGimage* font, float tx, float ty, float scale,
unsigned char* dst, int w, int h, int stride, unsigned char* dst, int w, int h, int stride,
char* text); const char* text);
#ifndef NANOSVGRAST_CPLUSPLUS #ifndef NANOSVGRAST_CPLUSPLUS
@ -1371,7 +1371,7 @@ static void dumpEdges(NSVGrasterizer* r, const char* name)
void nsvgRasterizeText(NSVGrasterizer* r, void nsvgRasterizeText(NSVGrasterizer* r,
NSVGimage* font, float tx, float ty, float scale, NSVGimage* font, float tx, float ty, float scale,
unsigned char* dst, int w, int h, int stride, unsigned char* dst, int w, int h, int stride,
char* text) const char* text)
{ {
NSVGshape *shape = NULL; NSVGshape *shape = NULL;
NSVGedge *e = NULL; NSVGedge *e = NULL;

View file

@ -136,7 +136,7 @@ static void draw_svg(NSVGimage *image, int x, int y, int w, int h)
nsvgDeleteRasterizer(rast); nsvgDeleteRasterizer(rast);
} }
static void draw_text(NSVGimage *font, char *text, int x, int y, int width, static void draw_text(NSVGimage *font, const char *text, int x, int y, int width,
int height, float scale, unsigned int tfb_col) int height, float scale, unsigned int tfb_col)
{ {
LOG("text '%s': fontsz=%f, x=%d, y=%d, dimensions: %d x %d\n", text, LOG("text '%s': fontsz=%f, x=%d, y=%d, dimensions: %d x %d\n", text,
@ -159,17 +159,18 @@ static void draw_text(NSVGimage *font, char *text, int x, int y, int width,
* Get the dimensions of a string in pixels. * Get the dimensions of a string in pixels.
* based on the font size and the font SVG file. * based on the font size and the font SVG file.
*/ */
static char *getTextDimensions(NSVGimage *font, char *text, float scale, static const char *getTextDimensions(NSVGimage *font, const char *text, float scale,
int *width, int *height) int *width, int *height)
{ {
int i; int i;
int fontHeight = (font->fontAscent - font->fontDescent) * scale; int fontHeight = (font->fontAscent - font->fontDescent) * scale;
int maxWidth = 0; int maxWidth = 0;
char *out_text = malloc(strlen(text));
if (text == NULL) if (text == NULL)
return text; return text;
char *out_text = malloc(strlen(text));
*width = font->defaultHorizAdv * scale; *width = font->defaultHorizAdv * scale;
// The height is simply the height of the font * the scale factor // The height is simply the height of the font * the scale factor
*height = fontHeight; *height = fontHeight;
@ -210,14 +211,16 @@ struct dpi_info {
int pixels_per_milli; int pixels_per_milli;
float logo_size_px; float logo_size_px;
int logo_size_max_mm; int logo_size_max_mm;
int font_size;
int font_size_b; // Bottom text font size
}; };
static void calculate_dpi_info(struct dpi_info *info) static void calculate_dpi_info(struct dpi_info *dpi_info)
{ {
int w_mm = tfb_screen_width_mm(); int w_mm = tfb_screen_width_mm();
int h_mm = tfb_screen_height_mm(); int h_mm = tfb_screen_height_mm();
if ((w_mm < 1 || h_mm < 1) && !info->dpi) { if ((w_mm < 1 || h_mm < 1) && !dpi_info->dpi) {
FILE *fp = fopen("/dev/kmsg", "w"); FILE *fp = fopen("/dev/kmsg", "w");
if (fp != NULL) { if (fp != NULL) {
fprintf(fp, "PBSPLASH: Invalid screen size: %dx%d\n", fprintf(fp, "PBSPLASH: Invalid screen size: %dx%d\n",
@ -231,38 +234,90 @@ static void calculate_dpi_info(struct dpi_info *info)
// Except ridiculous HiDPI displays // Except ridiculous HiDPI displays
// which honestly should expose their physical // which honestly should expose their physical
// dimensions.... // dimensions....
info->dpi = 300; dpi_info->dpi = 300;
} }
// If DPI is specified on cmdline then calculate display size from it // If DPI is specified on cmdline then calculate display size from it
// otherwise calculate the dpi based on the display size. // otherwise calculate the dpi based on the display size.
if (info->dpi > 0) { if (dpi_info->dpi > 0) {
w_mm = screenWidth / (float)info->dpi * 25.4; w_mm = screenWidth / (float)dpi_info->dpi * 25.4;
h_mm = screenHeight / (float)info->dpi * 25.4; h_mm = screenHeight / (float)dpi_info->dpi * 25.4;
} else { } else {
info->dpi = (float)screenWidth / (float)w_mm * 25.4; dpi_info->dpi = (float)screenWidth / (float)w_mm * 25.4;
} }
info->pixels_per_milli = (float)screenWidth / (float)w_mm; dpi_info->pixels_per_milli = (float)screenWidth / (float)w_mm;
if (info->logo_size_max_mm * info->pixels_per_milli > screenWidth) if (dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli > screenWidth)
info->logo_size_max_mm = (screenWidth * 0.75f) / info->pixels_per_milli; dpi_info->logo_size_max_mm = (screenWidth * 0.75f) / dpi_info->pixels_per_milli;
info->logo_size_px = dpi_info->logo_size_px =
(float)(screenWidth < screenHeight ? screenWidth : (float)(screenWidth < screenHeight ? screenWidth :
screenHeight) * screenHeight) *
0.75f; 0.75f;
if (w_mm > 0 && h_mm > 0) { if (w_mm > 0 && h_mm > 0) {
if (w_mm < h_mm) { if (w_mm < h_mm) {
if (w_mm > info->logo_size_max_mm * 1.2f) if (w_mm > dpi_info->logo_size_max_mm * 1.2f)
info->logo_size_px = info->logo_size_max_mm * info->pixels_per_milli; dpi_info->logo_size_px = dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli;
} else { } else {
if (h_mm > info->logo_size_max_mm * 1.2f) if (h_mm > dpi_info->logo_size_max_mm * 1.2f)
info->logo_size_px = info->logo_size_max_mm * info->pixels_per_milli; dpi_info->logo_size_px = dpi_info->logo_size_max_mm * dpi_info->pixels_per_milli;
} }
} }
LOG("%dx%d @ %dx%dmm, dpi=%ld, logo_size_px=%f\n", screenWidth, LOG("%dx%d @ %dx%dmm, dpi=%ld, logo_size_px=%f\n", screenWidth,
screenHeight, w_mm, h_mm, info->dpi, info->logo_size_px); screenHeight, w_mm, h_mm, dpi_info->dpi, dpi_info->logo_size_px);
}
int show_messages(const struct dpi_info *dpi_info, const char *font_path, const char *message, const char *message_bottom)
{
int textWidth, textHeight, bottomTextHeight = MM_TO_PX(dpi_info->dpi, 6);
int bottomTextOffset = MM_TO_PX(dpi_info->dpi, 3);
NSVGimage *font = NULL;
float fontsz;
int tx, ty;
font = nsvgParseFromFile(font_path, "px", 512);
if (!font || !font->shapes) {
fprintf(stderr, "failed to load SVG font, can't render messages\n");
return 1;
}
if (message_bottom) {
fontsz = ((float)dpi_info->font_size_b * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info->pixels_per_milli;
const char *text = getTextDimensions(font, message_bottom,
fontsz, &textWidth,
&bottomTextHeight);
tx = screenWidth / 2.f - textWidth / 2.f;
ty = screenHeight - bottomTextHeight - bottomTextOffset;
draw_text(font, text, tx, ty, textWidth,
bottomTextHeight, fontsz, tfb_gray);
if (text != message_bottom)
free((void *)text);
}
if (message) {
fontsz = ((float)dpi_info->font_size * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info->pixels_per_milli;
LOG("Fontsz: %f\n", fontsz);
const char *text = getTextDimensions(font, message, fontsz,
&textWidth, &textHeight);
tx = screenWidth / 2.f - textWidth / 2.f;
ty = (screenHeight - bottomTextHeight - bottomTextOffset) -
MM_TO_PX(dpi_info->dpi, dpi_info->font_size_b * PT_TO_MM) -
textHeight;
draw_text(font, message, tx, ty, textWidth, textHeight,
fontsz, tfb_gray);
if (text != message)
free((void *)text);
}
return 0;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -276,13 +331,13 @@ int main(int argc, char **argv)
NSVGimage *image = NULL; NSVGimage *image = NULL;
NSVGimage *font = NULL; NSVGimage *font = NULL;
struct sigaction action; struct sigaction action;
float font_size = FONT_SIZE_PT;
float font_size_b = FONT_SIZE_B_PT;
struct dpi_info dpi_info = { struct dpi_info dpi_info = {
.dpi = 0, .dpi = 0,
.pixels_per_milli = 0, .pixels_per_milli = 0,
.logo_size_px = 0, .logo_size_px = 0,
.logo_size_max_mm = LOGO_SIZE_MAX_MM .logo_size_max_mm = LOGO_SIZE_MAX_MM,
.font_size = FONT_SIZE_PT,
.font_size_b = FONT_SIZE_B_PT,
}; };
int optflag; int optflag;
bool animation = true; bool animation = true;
@ -316,7 +371,7 @@ int main(int argc, char **argv)
message_bottom = optarg; message_bottom = optarg;
break; break;
case 'o': case 'o':
font_size_b = strtof(optarg, &end); dpi_info.font_size_b = strtof(optarg, &end);
if (end == optarg) { if (end == optarg) {
fprintf(stderr, "Invalid font size: %s\n", fprintf(stderr, "Invalid font size: %s\n",
optarg); optarg);
@ -324,7 +379,7 @@ int main(int argc, char **argv)
} }
break; break;
case 'p': case 'p':
font_size = strtof(optarg, &end); dpi_info.font_size = strtof(optarg, &end);
if (end == optarg) { if (end == optarg) {
fprintf(stderr, "Invalid font size: %s\n", fprintf(stderr, "Invalid font size: %s\n",
optarg); optarg);
@ -414,50 +469,8 @@ int main(int argc, char **argv)
draw_svg(image, x, y, image_w, image_h); draw_svg(image, x, y, image_w, image_h);
if (message || message_bottom) { if (message || message_bottom)
int textWidth, textHeight, bottomTextHeight = MM_TO_PX(dpi_info.dpi, 6); show_messages(&dpi_info, font_path, message, message_bottom);
int bottomTextOffset = MM_TO_PX(dpi_info.dpi, 3);
float fontsz;
int tx, ty;
font = nsvgParseFromFile(font_path, "px", 512);
if (!font || !font->shapes) {
fprintf(stderr, "failed to load SVG font\n");
rc = 1;
goto out;
}
if (message_bottom) {
fontsz = ((float)font_size_b * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info.pixels_per_milli;
message_bottom = getTextDimensions(font, message_bottom,
fontsz, &textWidth,
&bottomTextHeight);
tx = screenWidth / 2.f - textWidth / 2.f;
ty = screenHeight - bottomTextHeight - bottomTextOffset;
draw_text(font, message_bottom, tx, ty, textWidth,
bottomTextHeight, fontsz, tfb_gray);
}
if (message) {
fontsz = ((float)font_size * PT_TO_MM) /
(font->fontAscent - font->fontDescent) *
dpi_info.pixels_per_milli;
LOG("Fontsz: %f\n", fontsz);
message = getTextDimensions(font, message, fontsz,
&textWidth, &textHeight);
tx = screenWidth / 2.f - textWidth / 2.f;
ty = (screenHeight - bottomTextHeight - bottomTextOffset) -
MM_TO_PX(dpi_info.dpi, font_size_b * PT_TO_MM) -
textHeight;
draw_text(font, message, tx, ty, textWidth, textHeight,
fontsz, tfb_gray);
}
}
tfb_flush_window(); tfb_flush_window();
tfb_flush_fb(); tfb_flush_fb();