From de673b05781f63a60e2c2c6a0ffac21bee288a67 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 20 Sep 2014 21:37:42 +0200 Subject: Add \fontsize and \bold --- src/sc_interp.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 6 deletions(-) (limited to 'src/sc_interp.c') diff --git a/src/sc_interp.c b/src/sc_interp.c index 9a9c9d8..525b54a 100644 --- a/src/sc_interp.c +++ b/src/sc_interp.c @@ -112,16 +112,11 @@ int sc_interp_get_height(SCInterpreter *scin) } -static void set_font(SCInterpreter *scin, const char *font_name) +static void update_font(SCInterpreter *scin) { PangoFontMetrics *metrics; struct sc_state *st = &scin->state[scin->j]; - st->fontdesc = pango_font_description_from_string(font_name); - if ( st->fontdesc == NULL ) { - fprintf(stderr, "Couldn't describe font.\n"); - return; - } st->font = pango_font_map_load_font(pango_context_get_font_map(scin->pc), scin->pc, st->fontdesc); if ( st->font == NULL ) { @@ -137,6 +132,65 @@ static void set_font(SCInterpreter *scin, const char *font_name) } +static void set_font(SCInterpreter *scin, const char *font_name) +{ + struct sc_state *st = &scin->state[scin->j]; + + st->fontdesc = pango_font_description_from_string(font_name); + if ( st->fontdesc == NULL ) { + fprintf(stderr, "Couldn't describe font.\n"); + return; + } + + update_font(scin); +} + + +static void copy_top_fontdesc(SCInterpreter *scin) +{ + struct sc_state *st = &scin->state[scin->j]; + + /* If this is the first stack frame, don't even check */ + if ( scin->j == 0 ) return; + + /* If the fontdesc at the top of the stack is the same as the one + * below, make a copy because we're about to do something to it (which + * should not affect the next level up). */ + if ( st->fontdesc == scin->state[scin->j-1].fontdesc ) { + st->fontdesc = pango_font_description_copy(st->fontdesc); + } +} + + +static void set_fontsize(SCInterpreter *scin, const char *size_str) +{ + struct sc_state *st = &scin->state[scin->j]; + int size; + char *end; + + if ( size_str[0] == '\0' ) return; + + size = strtoul(size_str, &end, 10); + if ( end[0] != '\0' ) { + fprintf(stderr, "Invalid font size '%s'\n", size_str); + return; + } + + copy_top_fontdesc(scin); + pango_font_description_set_size(st->fontdesc, size*PANGO_SCALE); + update_font(scin); +} + + +static void set_bold(SCInterpreter *scin) +{ + struct sc_state *st = &scin->state[scin->j]; + copy_top_fontdesc(scin); + pango_font_description_set_weight(st->fontdesc, PANGO_WEIGHT_BOLD); + update_font(scin); +} + + /* This sets the colour for the font at the top of the stack */ static void set_colour(SCInterpreter *scin, const char *colour) { @@ -669,6 +723,16 @@ int sc_interp_add_blocks(SCInterpreter *scin, SCBlock *bl) set_font(scin, options); maybe_recurse_after(scin, child); + } else if ( strcmp(name, "fontsize") == 0 ) { + maybe_recurse_before(scin, child); + set_fontsize(scin, options); + maybe_recurse_after(scin, child); + + } else if ( strcmp(name, "bold") == 0 ) { + maybe_recurse_before(scin, child); + set_bold(scin); + maybe_recurse_after(scin, child); + } else if ( strcmp(name, "fgcol") == 0 ) { maybe_recurse_before(scin, child); set_colour(scin, options); -- cgit v1.2.3