Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new function hagl_put_text_in_window() #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/hagl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern "C" {

#include "hagl_hal.h"
#include "bitmap.h"
#include "window.h"

#define ABS(x) ((x) > 0 ? (x) : -(x))

Expand Down Expand Up @@ -109,6 +110,22 @@ uint8_t hagl_put_char(wchar_t code, int16_t x0, int16_t y0, color_t color, const
*/
uint16_t hagl_put_text(const wchar_t *str, int16_t x0, int16_t y0, color_t color, const unsigned char *font);

/**
* Draw a string in window
*
* Output will be move the text to a new line to fit it into the window.
* Library itself includes only a couple of fonts. You can find more fonts at:
*
* https://github.com/tuupola/fonts
*
* @param str pointer to an wide char string
* @param window
* @param color
* @param font pointer to a FONTX font
* @return height of the drawn string
*/
uint16_t hagl_put_text_in_window(const wchar_t *str, window_t window, color_t color, const unsigned char *font);

/**
* Extract a glyph into a bitmap
*
Expand Down
44 changes: 44 additions & 0 deletions src/hagl.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,50 @@ uint16_t hagl_put_text(const wchar_t *str, int16_t x0, int16_t y0, color_t color
return x0 - original;
}

/*
* Write a string of text by calling hagl_put_char() repeadetly. CR and LF
* continue from the next line. If the text goes outside the window,
* it will move the text to a new line
*/
uint16_t hagl_put_text_in_window(const wchar_t *str, window_t window, color_t color, const unsigned char *font)
{
wchar_t temp;
uint8_t status;
fontx_meta_t meta;

status = fontx_meta(&meta, font);
if (0 != status) {
return 0;
}

uint16_t x0 = window.x0;
uint16_t y0 = window.y0;
int16_t x_lim = window.x1 - meta.width + 1;
int16_t y_lim = window.y1 - meta.height + 1;

if ((x0 > x_lim) || (y0 > y_lim)) {
return 0;
}

do {
temp = *str++;
if (13 == temp || 10 == temp || x0 > x_lim) {
x0 = window.x0;
if (y0 > y_lim - meta.height) {
break;
}
y0 += meta.height;
}
if ((x0 == window.x0 && 32 == temp) || 13 == temp || 10 == temp) {
continue;
}
x0 += hagl_put_char(temp, x0, y0, color, font);
} while (*str != 0);

y0 += meta.height;
return y0;
}

/*
* Blits a bitmap to a destination hardcoded in the HAL driver. Destination
* parameter is left out intentionally to keep the API simpler. If you need
Expand Down