support newlines + automatic line splitting

This commit is contained in:
Caleb Connolly 2022-07-24 18:57:35 +01:00
parent 5780f60199
commit 3c25e1ba44
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
3 changed files with 62 additions and 21 deletions

View file

@ -2970,6 +2970,10 @@ NSVGshape** nsvgGetTextShapes(NSVGimage* image, char* text, int textLen)
// make list of paths representing glyphs to render // make list of paths representing glyphs to render
for (i = 0; i < textLen; i++) for (i = 0; i < textLen; i++)
{ {
if (text[i] == ' ' || text[i] == '\n') {
ret[i] = NULL;
continue;
}
for (shape = image->shapes; shape != NULL; shape = shape->next) { for (shape = image->shapes; shape != NULL; shape = shape->next) {
if (!(shape->flags & NSVG_FLAGS_VISIBLE)) if (!(shape->flags & NSVG_FLAGS_VISIBLE))
continue; continue;

View file

@ -1372,6 +1372,13 @@ void nsvgRasterizeText(NSVGrasterizer* r,
NSVGcachedPaint cache; NSVGcachedPaint cache;
NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text)); NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text));
int i = 0, j = 0, textLen = strlen(text); int i = 0, j = 0, textLen = strlen(text);
int fontHeight = (font->fontAscent - font->fontDescent) * scale;
int xStart = tx;
int charWidth = font->defaultHorizAdv * scale;
// Hack because for some reason this has Y increase UP and we
// need to go DOWN every line
ty = ty + h - fontHeight;
r->bitmap = dst; r->bitmap = dst;
r->width = w; r->width = w;
@ -1391,10 +1398,16 @@ void nsvgRasterizeText(NSVGrasterizer* r,
} }
for (i = 0; i < textLen; i++) { for (i = 0; i < textLen; i++) {
if (text[i] == '\n') {
ty -= fontHeight;
// No clue why this is needed
tx = xStart - charWidth;
continue;
}
shape = shapes[i]; shape = shapes[i];
if (!shape) { if (!shape) {
if (text[i] == ' ') if (text[i] == ' ')
tx += font->defaultHorizAdv * scale; tx += charWidth;
continue; continue;
} }
if (!(shape->flags & NSVG_FLAGS_VISIBLE)) if (!(shape->flags & NSVG_FLAGS_VISIBLE))

View file

@ -1,3 +1,4 @@
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
@ -36,6 +37,8 @@ volatile sig_atomic_t terminate = 0;
bool debug = false; bool debug = false;
struct col background_color = { .r = 0, .g = 0, .b = 0, .a = 255 }; struct col background_color = { .r = 0, .g = 0, .b = 0, .a = 255 };
static int screenWidth, screenHeight;
#define LOG(fmt, ...) \ #define LOG(fmt, ...) \
do { \ do { \
if (debug) \ if (debug) \
@ -151,21 +154,39 @@ 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 void getTextDimensions(NSVGimage *font, char *text, float scale, static char* getTextDimensions(NSVGimage *font, char *text, float scale,
int *width, int *height) int *width, int *height)
{ {
int i = 0; int i;
int fontHeight = (font->fontAscent - font->fontDescent) * scale;
int maxWidth = 0;
char *out_text = malloc(strlen(text));
if (text == NULL)
return text;
*width = 0; *width = 0;
// 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 = (font->fontAscent - font->fontDescent) * scale; *height = fontHeight;
if (text == NULL)
return;
NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text)); NSVGshape **shapes = nsvgGetTextShapes(font, text, strlen(text));
// Iterate over every glyph in the string to get the total width // Iterate over every glyph in the string to get the total width
for (i = 0; i < strlen(text); i++) { for (i = 0; text[i] != '\0'; i++) {
NSVGshape *shape = shapes[i]; NSVGshape *shape = shapes[i];
out_text[i] = text[i];
if (*width > screenWidth * 0.95) {
while (out_text[i] != ' ' && i > 0)
i--;
out_text[i] = '\n';
}
if (out_text[i] == '\n') {
*height += fontHeight;
maxWidth = *width > maxWidth ? *width : maxWidth;
*width = 0;
continue;
}
if (shape) { if (shape) {
*width += (float)shapes[i]->horizAdvX * scale + 0.5; *width += (float)shapes[i]->horizAdvX * scale + 0.5;
} else { } else {
@ -173,7 +194,10 @@ static void getTextDimensions(NSVGimage *font, char *text, float scale,
} }
} }
*width = *width > maxWidth ? *width : maxWidth;
free(shapes); free(shapes);
return out_text;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -261,22 +285,22 @@ int main(int argc, char **argv)
return rc; return rc;
} }
int w = (int)tfb_screen_width(); screenWidth = (int)tfb_screen_width();
int h = (int)tfb_screen_height(); screenHeight = (int)tfb_screen_height();
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 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 (dpi > 0) { if (dpi > 0) {
w_mm = w / (float)dpi * 25.4; w_mm = screenWidth / (float)dpi * 25.4;
h_mm = h / (float)dpi * 25.4; h_mm = screenHeight / (float)dpi * 25.4;
} else { } else {
dpi = (float)w / (float)w_mm * 25.4; dpi = (float)screenWidth / (float)w_mm * 25.4;
} }
int pixels_per_milli = (float)w / (float)w_mm; int pixels_per_milli = (float)screenWidth / (float)w_mm;
float logo_size_px = (float)(w < h ? w : h) * 0.75f; float logo_size_px = (float)(screenWidth < screenHeight ? screenWidth : screenHeight) * 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 > (float)LOGO_SIZE_MAX_MM * 1.2f) if (w_mm > (float)LOGO_SIZE_MAX_MM * 1.2f)
@ -289,7 +313,7 @@ int main(int argc, char **argv)
} }
} }
LOG("%dx%d @ %dx%dmm, dpi=%ld, logo_size_px=%f\n", w, h, w_mm, h_mm, LOG("%dx%d @ %dx%dmm, dpi=%ld, logo_size_px=%f\n", screenWidth, screenHeight, w_mm, h_mm,
dpi, logo_size_px); dpi, logo_size_px);
image = nsvgParseFromFile(splash_image, "", logo_size_px); image = nsvgParseFromFile(splash_image, "", logo_size_px);
@ -304,8 +328,8 @@ int main(int argc, char **argv)
(image->width > image->height ? image->width : image->height); (image->width > image->height ? image->width : image->height);
int image_w = image->width * sz + 0.5; int image_w = image->width * sz + 0.5;
int image_h = image->height * sz + 0.5; int image_h = image->height * sz + 0.5;
float x = (float)w / 2; float x = (float)screenWidth / 2;
float y = (float)h / 2; float y = (float)screenHeight / 2;
// Center the image // Center the image
x -= image_w * 0.5f; x -= image_w * 0.5f;
y -= image_h * 0.5f; y -= image_h * 0.5f;
@ -328,11 +352,11 @@ int main(int argc, char **argv)
float fontsz = ((float)font_size * PT_TO_MM) / float fontsz = ((float)font_size * PT_TO_MM) /
(font->fontAscent - font->fontDescent) * (font->fontAscent - font->fontDescent) *
pixels_per_milli; pixels_per_milli;
LOG("Fontsz: %f\n", fontsz);
getTextDimensions(font, message, fontsz, &textWidth, message = getTextDimensions(font, message, fontsz, &textWidth,
&textHeight); &textHeight);
int tx = w / 2.f - textWidth / 2.f; int tx = screenWidth / 2.f - textWidth / 2.f;
int ty = y + image_h + textHeight * 0.5f + MM_TO_PX(dpi, 2); int ty = y + image_h + textHeight * 0.5f + MM_TO_PX(dpi, 2);
draw_text(font, message, tx, ty, textWidth, textHeight, fontsz, draw_text(font, message, tx, ty, textWidth, textHeight, fontsz,
@ -345,7 +369,7 @@ int main(int argc, char **argv)
int frame = 0; int frame = 0;
int tty = open(active_tty, O_RDWR); int tty = open(active_tty, O_RDWR);
while (!terminate) { while (!terminate) {
animate_frame(frame++, w, h, dpi); animate_frame(frame++, screenWidth, screenHeight, dpi);
tfb_flush_fb(); tfb_flush_fb();
} }