tuupola / hagl

Hardware Agnostic Graphics Library for embedded
https://www.appelsiini.net/tags/hagl/
MIT License
314 stars 49 forks source link

Add auto wrap text function #89

Open jamesSaporito opened 2 years ago

jamesSaporito commented 2 years ago

Overview

Added a function hagl_put_wrap_text that will automatically wrap the text to the next line, but without splitting a word into multiple lines. Only full words will be printed on a single line, unless the word is too long to fit. In the scenario where it is too long to fit, it will wrap around mid-word for that word only.

As of now, this was only tested using a small 160x80 ST7735S display with an ESP32 development board.

Testing

Sample program for testing a few scenarios:

hagl_backend_t *display = hagl_init();
hagl_clear(display);

// Text color
color_t color = hagl_color(display, 255, 255, 255);

// Text to display
char text[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
    "do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.";

hagl_set_clip(display, 0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
uint8_t result = hagl_put_wrap_text(display, text, color, font6x9);

/**
 * Testing a word that is longer than the window/screen width. This will be forced to
 * wrap around.
 */
// char long_word[] = "thisisjustaverylongwordthatwillwraparound";
// uint8_t result = hagl_put_wrap_text(display, long_word, color, font6x9);

/**
 * Narrower bounds
 */
// hagl_set_clip(display, 50, 0, 100, 80);
// uint8_t result = hagl_put_wrap_text(display, text, color, font6x9);

if (result > 0) {
    ESP_LOGI(TAG, "Success");
} else {
    ESP_LOGI(TAG, "Window Error");
}

hagl_flush(display);
jamesSaporito commented 1 year ago

@tuupola Hey there, just wanted to ask if you'd like me to close this PR? No worries if this functionality isn't needed!

tuupola commented 1 year ago

@jamesSaporito Sorry I did not mean to ignore you. I have the usual usual "started to work for my own company and much less time for open source" thing going on.

I want to add something like this but I also have not decided how the text styling API would look. @CHiPs44 has created one idea about this https://github.com/tuupola/hagl/pull/96. There is also couple of related issues https://github.com/tuupola/hagl/issues/39 and https://github.com/tuupola/hagl/issues/42.

Main problem for me is that I do not want to open a can of worms and bloat the core library. I want to keep it reasonably small and there can be endless font styling options. So either have to choose a core set of styling rules or make an easily extendable styling API. One option could also be to make the font rendering an external library and core library would only have the 8 bit rendering style.

Lets keeps this open for now so it also works as a TODO list entry for me.

jamesSaporito commented 1 year ago

@tuupola Oh no worries at all, just wanted to follow up!

Yep I totally get not wanting the bloat. Sounds good to me, I will leave it open. Thank you!