markup: Initial work on new marked up text rendering library

This commit is contained in:
K. Lange 2021-07-27 10:53:12 +09:00
parent d81da78dc0
commit b293e989de
5 changed files with 189 additions and 17 deletions

View File

@ -325,9 +325,9 @@ static void show_volume_status(void) {
/* Add the current volume status */
char volume_level_label[100];
if (volume_level < 10) {
snprintf(volume_level_label, 99, "Volume: Muted");
snprintf(volume_level_label, 99, "<b>Volume:</b> <i>Muted</i>");
} else {
snprintf(volume_level_label, 99, "Volume: %d%%", (int)(100 * ((float)volume_level / (float)0xFc000000)));
snprintf(volume_level_label, 99, "<b>Volume:</b> %d%%", (int)(100 * ((float)volume_level / (float)0xFc000000)));
}
menu_insert(volume_menu, menu_create_normal(NULL, NULL, volume_level_label, NULL));
@ -429,15 +429,15 @@ static void update_weather_status(void) {
weather_icon = hashmap_get(weather_icons, icon);
char tmp[300];
sprintf(tmp, "Weather for %s", city);
sprintf(tmp, "Weather for <b>%s</b>", city);
weather_title_str = strdup(tmp);
sprintf(tmp, "%s", updated);
sprintf(tmp, "<small><i>%s</i></small>", updated);
weather_updated_str = strdup(tmp);
sprintf(tmp, "%s° - %s", temp, conditions);
sprintf(tmp, "<b>%s°</b> - %s", temp, conditions);
weather_conditions_str = strdup(tmp);
sprintf(tmp, "Humidity: %s%%", humidity);
sprintf(tmp, "<b>Humidity:</b> %s%%", humidity);
weather_humidity_str = strdup(tmp);
sprintf(tmp, "Clouds: %s%%", clouds);
sprintf(tmp, "<b>Clouds:</b> %s%%", clouds);
weather_clouds_str = strdup(tmp);
sprintf(tmp, "%s°", temp_r);
@ -564,8 +564,8 @@ static void show_weather_status(void) {
menu_insert(weather, menu_create_normal("refresh", NULL, "Refresh...", weather_refresh));
menu_insert(weather, menu_create_normal("config", NULL, "Configure...", weather_configure));
menu_insert(weather, menu_create_separator());
menu_insert(weather, menu_create_normal(NULL, NULL, "Weather data provided by", NULL));
menu_insert(weather, menu_create_normal(NULL, NULL, "OpenWeatherMap.org", NULL));
menu_insert(weather, menu_create_normal(NULL, NULL, "<small><i>Weather data provided by</i></small>", NULL));
menu_insert(weather, menu_create_normal(NULL, NULL, "<color #0000FF>OpenWeatherMap.org</color>", NULL));
}
if (weather_status_valid) {
menu_update_title(weather_title_entry, weather_title_str);
@ -598,7 +598,7 @@ static void show_network_status(void) {
if (!netstat) {
netstat = menu_create();
netstat->flags |= MENU_FLAG_BUBBLE_LEFT;
menu_insert(netstat, menu_create_normal(NULL, NULL, "Network Status", NULL));
menu_insert(netstat, menu_create_normal(NULL, NULL, "<b>Network Status</b>", NULL));
menu_insert(netstat, menu_create_separator());
}
while (netstat->entries->length > 2) {

View File

@ -0,0 +1,11 @@
#pragma once
#include <_cheader.h>
#include <toaru/graphics.h>
_Begin_C_Header
int markup_string_width(const char * str);
int markup_draw_string(gfx_context_t * ctx, int x, int y, const char * str, uint32_t color);
void markup_text_init(void);
_End_C_Header

163
lib/markup_text.c Normal file
View File

@ -0,0 +1,163 @@
#include <toaru/markup.h>
#include <toaru/list.h>
#include <toaru/graphics.h>
#include <toaru/text.h>
static struct TT_Font * dejaVuSans = NULL;
static struct TT_Font * dejaVuSans_Bold = NULL;
static struct TT_Font * dejaVuSans_Oblique = NULL;
static struct TT_Font * dejaVuSans_BoldOblique = NULL;
struct MarkupState {
list_t * state;
int current_state;
int cursor_x;
int cursor_y;
int initial_left;
uint32_t color;
gfx_context_t * ctx;
int max_cursor_x;
list_t * colors;
};
#define STATE_BOLD (1 << 0)
#define STATE_OBLIQUE (1 << 1)
#define STATE_HEADING (1 << 2)
#define STATE_SMALL (1 << 3)
static void push_state(struct MarkupState * state, int val) {
list_insert(state->state, (void*)(uintptr_t)state->current_state);
state->current_state |= val;
}
static void pop_state(struct MarkupState * state) {
node_t * nstate = list_pop(state->state);
state->current_state = (int)(uintptr_t)nstate->value;
free(nstate);
}
static uint32_t parseColor(const char * c) {
if (*c != '#' || strlen(c) != 7) return rgba(0,0,0,255);
char r[3] = {c[1],c[2],'\0'};
char g[3] = {c[3],c[4],'\0'};
char b[3] = {c[5],c[6],'\0'};
return rgba(strtoul(r,NULL,16),strtoul(g,NULL,16),strtoul(b,NULL,16),255);
}
static int parser_open(struct markup_state * self, void * user, struct markup_tag * tag) {
struct MarkupState * state = (struct MarkupState*)user;
if (!strcmp(tag->name, "b")) {
push_state(state, STATE_BOLD);
} else if (!strcmp(tag->name, "i")) {
push_state(state, STATE_OBLIQUE);
} else if (!strcmp(tag->name, "h1")) {
push_state(state, STATE_HEADING);
} else if (!strcmp(tag->name, "small")) {
push_state(state, STATE_SMALL);
} else if (!strcmp(tag->name, "br")) {
state->cursor_x = state->initial_left;
state->cursor_y += 20; /* state->line_height? */
} else if (!strcmp(tag->name, "color")) {
/* get options */
list_t * args = hashmap_keys(tag->options);
if (args->length == 1) {
list_insert(state->colors, (void*)(uintptr_t)state->color);
state->color = parseColor((char*)args->head->value);
}
free(args);
}
markup_free_tag(tag);
return 0;
}
static int parser_close(struct markup_state * self, void * user, char * tag_name) {
struct MarkupState * state = (struct MarkupState*)user;
if (!strcmp(tag_name, "b")) {
pop_state(state);
} else if (!strcmp(tag_name, "i")) {
pop_state(state);
} else if (!strcmp(tag_name, "h1")) {
pop_state(state);
} else if (!strcmp(tag_name, "small")) {
pop_state(state);
} else if (!strcmp(tag_name, "color")) {
node_t * ncolor = list_pop(state->colors);
state->color = (uint32_t)(uintptr_t)ncolor->value;
free(ncolor);
}
return 0;
}
static struct TT_Font * fontForState(struct MarkupState * state) {
if (state->current_state & (1 << 0)) {
if (state->current_state & (1 << 1)) {
return dejaVuSans_BoldOblique;
}
return dejaVuSans_Bold;
} else if (state->current_state & (1 << 1)) {
return dejaVuSans_Oblique;
}
return dejaVuSans;
}
static int sizeForState(struct MarkupState * state) {
if (state->current_state & STATE_HEADING) return 18;
if (state->current_state & STATE_SMALL) return 10;
return 13;
}
static int parser_data(struct markup_state * self, void * user, char * data) {
struct MarkupState * state = (struct MarkupState*)user;
struct TT_Font * font = fontForState(state);
tt_set_size(font, sizeForState(state));
state->cursor_x += tt_draw_string(state->ctx, font, state->cursor_x, state->cursor_y, data, state->color);
if (state->cursor_x > state->max_cursor_x) state->max_cursor_x = state->cursor_x;
return 0;
}
static int parser_dryrun(struct markup_state * self, void * user, char * data) {
struct MarkupState * state = (struct MarkupState*)user;
struct TT_Font * font = fontForState(state);
tt_set_size(font, sizeForState(state));
state->cursor_x += tt_string_width(font, data);
if (state->cursor_x > state->max_cursor_x) state->max_cursor_x = state->cursor_x;
return 0;
}
int markup_string_width(const char * str) {
struct MarkupState state = {list_create(), 0, 0, 0, 0, 0, NULL, 0, list_create()};
struct markup_state * parser = markup_init(&state, parser_open, parser_close, parser_dryrun);
while (*str) {
if (markup_parse(parser, *str++)) {
break;
}
}
markup_finish(parser);
list_free(state.state);
list_free(state.colors);
return state.max_cursor_x - state.initial_left;
}
int markup_draw_string(gfx_context_t * ctx, int x, int y, const char * str, uint32_t color) {
struct MarkupState state = {list_create(), 0, x, y, x, color, ctx, x, list_create()};
struct markup_state * parser = markup_init(&state, parser_open, parser_close, parser_data);
while (*str) {
if (markup_parse(parser, *str++)) {
break;
}
}
markup_finish(parser);
list_free(state.state);
list_free(state.colors);
return state.max_cursor_x - state.initial_left;
}
void markup_text_init(void) {
dejaVuSans = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
dejaVuSans_Bold = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf");
dejaVuSans_Oblique = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf");
dejaVuSans_BoldOblique = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf");
}

View File

@ -21,6 +21,7 @@
#include <toaru/list.h>
#include <toaru/icon_cache.h>
#include <toaru/text.h>
#include <toaru/markup_text.h>
#include <toaru/menu.h>
@ -36,8 +37,6 @@
static hashmap_t * menu_windows = NULL;
static yutani_t * my_yctx = NULL;
static struct TT_Font * _tt_font = NULL;
static struct MenuList * hovered_menu = NULL;
int menu_definitely_close(struct MenuList * menu);
@ -45,7 +44,7 @@ int menu_definitely_close(struct MenuList * menu);
__attribute__((constructor))
static void _init_menus(void) {
menu_windows = hashmap_create_int(10);
_tt_font = tt_font_from_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
markup_text_init();
}
hashmap_t * menu_get_windows_hash(void) {
@ -53,13 +52,11 @@ hashmap_t * menu_get_windows_hash(void) {
}
static int string_width(const char * s) {
tt_set_size(_tt_font, 13);
return tt_string_width(_tt_font, s);
return markup_string_width(s);
}
static int draw_string(gfx_context_t * ctx, int x, int y, uint32_t color, const char * s) {
tt_set_size(_tt_font, 13);
return tt_draw_string(ctx, _tt_font, x, y + 13, s, color);
return markup_draw_string(ctx,x,y+13,s,color);
}
void _menu_draw_MenuEntry_Normal(gfx_context_t * ctx, struct MenuEntry * self, int offset) {

View File

@ -31,6 +31,7 @@ class Classifier:
'<toaru/menu.h>': (None, '-ltoaru_menu', ['<toaru/yutani.h>', '<toaru/icon_cache.h>', '<toaru/graphics.h>', '<toaru/hashmap.h>']),
'<toaru/button.h>': (None, '-ltoaru_button', ['<toaru/graphics.h>','<toaru/text.h>', '<toaru/icon_cache.h>']),
'<toaru/text.h>': (None, '-ltoaru_text', ['<toaru/graphics.h>', '<toaru/hashmap.h>']),
'<toaru/markup_text.h>': (None, '-ltoaru_markup_text', ['<toaru/graphics.h>', '<toaru/markup.h>', '<toaru/text.h>']),
# Kuroko
'<kuroko/kuroko.h>': ('../../../kuroko/src', '-lkuroko', []),
}