diff options
Diffstat (limited to 'libstorycode')
-rw-r--r-- | libstorycode/scparse_priv.h | 6 | ||||
-rw-r--r-- | libstorycode/storycode.y | 30 | ||||
-rw-r--r-- | libstorycode/stylesheet.c | 110 | ||||
-rw-r--r-- | libstorycode/stylesheet.h | 17 |
4 files changed, 103 insertions, 60 deletions
diff --git a/libstorycode/scparse_priv.h b/libstorycode/scparse_priv.h index c327b1a..22be698 100644 --- a/libstorycode/scparse_priv.h +++ b/libstorycode/scparse_priv.h @@ -56,10 +56,10 @@ struct scpctx enum alignment alignment; struct length padding[4]; struct length paraspace[4]; - double fgcol[4]; + struct colour fgcol; enum gradient bggrad; - double bgcol[4]; - double bgcol2[4]; + struct colour bgcol; + struct colour bgcol2; }; #endif /* SCPARSE_PRIV_H */ diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y index 4e3c6d0..d98515b 100644 --- a/libstorycode/storycode.y +++ b/libstorycode/storycode.y @@ -43,7 +43,7 @@ struct frame_geom geom; char character; double val; - double rgba[4]; + struct colour col; enum alignment align; enum gradient grad; } @@ -87,7 +87,7 @@ %type <str> frameopt %type <geom> geometry %type <lenquad> lenquad -%type <rgba> colour +%type <col> colour %type <str> HEXCOL %type <len> length %type <align> alignment @@ -114,6 +114,15 @@ %{ + +static void copy_col(struct colour *to, struct colour from) +{ + int i; + for ( i=0; i<4; i++ ) to->rgba[i] = from.rgba[i]; + to->hexcode = from.hexcode; +} + + static int hex_to_double(const char *v, double *r) { char c[5]; @@ -285,11 +294,14 @@ lenquad: ; colour: - VALUE ',' VALUE ',' VALUE ',' VALUE { $$[0] = $1; $$[1] = $3; - $$[2] = $5; $$[3] = $7; } + VALUE ',' VALUE ',' VALUE ',' VALUE { $$.rgba[0] = $1; $$.rgba[1] = $3; + $$.rgba[2] = $5; $$.rgba[3] = $7; + $$.hexcode = 0; } | HEXCOL { double col[3]; if ( hex_to_double($1, col) ) { - $$[0] = col[0]; $$[1] = col[1]; $$[2] = col[2]; $$[3] = 1.0; + $$.rgba[0] = col[0]; $$.rgba[1] = col[1]; + $$.rgba[2] = col[2]; $$.rgba[3] = 1.0; + $$.hexcode = 1; } } ; @@ -368,11 +380,11 @@ style_slidesize: ; background: - BGCOL colour { for ( int i=0; i<4; i++ ) ctx->bgcol[i] = $2[i]; + BGCOL colour { copy_col(&ctx->bgcol, $2); ctx->bggrad = GRAD_NONE; ctx->mask |= STYMASK_BGCOL; } -| BGCOL gradtype colour colour { for ( int i=0; i<4; i++ ) ctx->bgcol[i] = $3[i]; - for ( int i=0; i<4; i++ ) ctx->bgcol2[i] = $4[i]; +| BGCOL gradtype colour colour { copy_col(&ctx->bgcol, $3); + copy_col(&ctx->bgcol2, $4); ctx->bggrad = $2; ctx->mask |= STYMASK_BGCOL; } ; @@ -403,7 +415,7 @@ styledef: ctx->mask |= STYMASK_PADDING; } | PARASPACE lenquad { for ( int i=0; i<4; i++ ) ctx->paraspace[i] = $2[i]; ctx->mask |= STYMASK_PARASPACE; } -| FGCOL colour { for ( int i=0; i<4; i++ ) ctx->fgcol[i] = $2[i]; +| FGCOL colour { copy_col(&ctx->fgcol, $2); ctx->mask |= STYMASK_FGCOL; } | background { /* Handled in rule 'background' */ } | ALIGN alignment { ctx->alignment = $2; diff --git a/libstorycode/stylesheet.c b/libstorycode/stylesheet.c index e3aecee..cde2216 100644 --- a/libstorycode/stylesheet.c +++ b/libstorycode/stylesheet.c @@ -52,10 +52,10 @@ struct style struct frame_geom geom; char *font; - double fgcol[4]; /* r g b a */ + struct colour fgcol; enum gradient bggrad; - double bgcol[4]; /* r g b a */ - double bgcol2[4]; /* r g b a, if gradient */ + struct colour bgcol; + struct colour bgcol2; struct length paraspace[4]; /* l r t b */ struct length padding[4]; /* l r t b */ enum alignment alignment; @@ -73,6 +73,14 @@ struct _stylesheet }; +static void copy_col(struct colour *to, struct colour from) +{ + int i; + for ( i=0; i<4; i++ ) to->rgba[i] = from.rgba[i]; + to->hexcode = from.hexcode; +} + + static void default_style(struct style *s) { s->geom.x.len = 0.0; @@ -87,22 +95,25 @@ static void default_style(struct style *s) s->font = strdup("Sans 12"); s->alignment = ALIGN_LEFT; - s->fgcol[0] = 0.0; - s->fgcol[1] = 0.0; - s->fgcol[2] = 0.0; - s->fgcol[3] = 1.0; + s->fgcol.rgba[0] = 0.0; + s->fgcol.rgba[1] = 0.0; + s->fgcol.rgba[2] = 0.0; + s->fgcol.rgba[3] = 1.0; + s->fgcol.hexcode = 1; s->bggrad = GRAD_NONE; - s->bgcol[0] = 1.0; - s->bgcol[1] = 1.0; - s->bgcol[2] = 1.0; - s->bgcol[3] = 1.0; + s->bgcol.rgba[0] = 1.0; + s->bgcol.rgba[1] = 1.0; + s->bgcol.rgba[2] = 1.0; + s->bgcol.rgba[3] = 1.0; + s->bgcol.hexcode = 1; - s->bgcol2[0] = 1.0; - s->bgcol2[1] = 1.0; - s->bgcol2[2] = 1.0; - s->bgcol2[3] = 1.0; + s->bgcol2.rgba[0] = 1.0; + s->bgcol2.rgba[1] = 1.0; + s->bgcol2.rgba[2] = 1.0; + s->bgcol2.rgba[3] = 1.0; + s->bgcol2.hexcode = 1; s->paraspace[0].len = 0.0; s->paraspace[1].len = 0.0; @@ -295,30 +306,33 @@ int stylesheet_set_paraspace(Stylesheet *s, enum style_element el, struct length } -int stylesheet_set_fgcol(Stylesheet *s, enum style_element el, double rgba[4]) +int stylesheet_set_fgcol(Stylesheet *s, enum style_element el, struct colour fgcol) { int i; struct style *sty = get_style(s, el); if ( sty == NULL ) return 1; for ( i=0; i<4; i++ ) { - sty->fgcol[i] = rgba[i]; + sty->fgcol.rgba[i] = fgcol.rgba[i]; } + sty->fgcol.hexcode = fgcol.hexcode; sty->set |= SM_FGCOL; return 0; } int stylesheet_set_background(Stylesheet *s, enum style_element el, enum gradient grad, - double bgcol[4], double bgcol2[4]) + struct colour bgcol, struct colour bgcol2) { int i; struct style *sty = get_style(s, el); if ( sty == NULL ) return 1; sty->bggrad = grad; for ( i=0; i<4; i++ ) { - sty->bgcol[i] = bgcol[i]; - sty->bgcol2[i] = bgcol2[i]; + sty->bgcol.rgba[i] = bgcol.rgba[i]; + sty->bgcol2.rgba[i] = bgcol2.rgba[i]; } + sty->bgcol.hexcode = bgcol.hexcode; + sty->bgcol2.hexcode = bgcol2.hexcode; sty->set |= SM_BGCOL; return 0; } @@ -345,33 +359,28 @@ int stylesheet_get_geometry(Stylesheet *s, enum style_element el, struct frame_g const char *stylesheet_get_font(Stylesheet *s, enum style_element el, - double *fgcol, enum alignment *alignment) + struct colour *fgcol, enum alignment *alignment) { - int i; struct style *sty = get_style(s, el); if ( sty == NULL ) return NULL; *alignment = sty->alignment; if ( fgcol != NULL ) { - for ( i=0; i<4; i++ ) { - fgcol[i] = sty->fgcol[i]; - } + copy_col(fgcol, sty->fgcol); } return sty->font; } int stylesheet_get_background(Stylesheet *s, enum style_element el, - enum gradient *grad, double *bgcol, double *bgcol2) + enum gradient *grad, struct colour *bgcol, + struct colour *bgcol2) { - int i; struct style *sty = get_style(s, el); if ( sty == NULL ) return 1; - for ( i=0; i<4; i++ ) { - bgcol[i] = sty->bgcol[i]; - bgcol2[i] = sty->bgcol2[i]; - } + copy_col(bgcol, sty->bgcol); + copy_col(bgcol2, sty->bgcol2); *grad = sty->bggrad; return 0; } @@ -424,6 +433,20 @@ static void add_text(char **text, size_t *len, size_t *lenmax, const char *prefi } +static void format_col(char *a, size_t max_len, struct colour col) +{ + if ( !col.hexcode ) { + snprintf(a, max_len, "%.4g,%.4g,%.4g,%.4g", + col.rgba[0], col.rgba[1], col.rgba[2], col.rgba[3]); + } else { + snprintf(a, max_len, "#%.2X%.2X%.2X", + (unsigned int)(col.rgba[0]*255), + (unsigned int)(col.rgba[1]*255), + (unsigned int)(col.rgba[2]*255)); + } +} + + static void add_style(char **text, size_t *len, size_t *lenmax, const char *prefix, struct style *sty) { @@ -448,24 +471,23 @@ static void add_style(char **text, size_t *len, size_t *lenmax, const char *pref if ( sty->set & SM_FGCOL ) { char tmp[256]; - snprintf(tmp, 255, "FGCOL %.4g,%.4g,%.4g,%.4g\n", - sty->fgcol[0], sty->fgcol[1], sty->fgcol[2], sty->fgcol[3]); - add_text(text, len, lenmax, prefix, tmp); + format_col(tmp, 255, sty->fgcol); + add_text(text, len, lenmax, prefix, "FGCOL "); + add_text(text, len, lenmax, "", tmp); + add_text(text, len, lenmax, "", "\n"); } if ( sty->set & SM_BGCOL ) { char tmp[256]; - if ( sty->bggrad == GRAD_NONE ) { - snprintf(tmp, 255, "BGCOL %s%.4g,%.4g,%.4g,%.4g\n", - bgcolc(sty->bggrad), - sty->bgcol[0], sty->bgcol[1], sty->bgcol[2], sty->bgcol[3]); - } else { - snprintf(tmp, 255, "BGCOL %s%.4g,%.4g,%.4g,%.4g %.4g,%.4g,%.4g,%.4g\n", - bgcolc(sty->bggrad), - sty->bgcol[0], sty->bgcol[1], sty->bgcol[2], sty->bgcol[3], - sty->bgcol2[0], sty->bgcol2[1], sty->bgcol2[2], sty->bgcol2[3]); + add_text(text, len, lenmax, prefix, "BGCOL "); + add_text(text, len, lenmax, "", bgcolc(sty->bggrad)); + format_col(tmp, 255, sty->bgcol); + add_text(text, len, lenmax, "", tmp); + if ( sty->bggrad != GRAD_NONE ) { + format_col(tmp, 255, sty->bgcol2); + add_text(text, len, lenmax, " ", tmp); } - add_text(text, len, lenmax, prefix, tmp); + add_text(text, len, lenmax, "", "\n"); } if ( sty->set & SM_PARASPACE ) { diff --git a/libstorycode/stylesheet.h b/libstorycode/stylesheet.h index 98ac089..00f3c6b 100644 --- a/libstorycode/stylesheet.h +++ b/libstorycode/stylesheet.h @@ -45,6 +45,14 @@ enum length_unit }; +struct colour +{ + double rgba[4]; + int hexcode; /* If true, colour came from a hexcode + * (and should be written back as one) */ +}; + + struct length { double len; @@ -94,16 +102,17 @@ extern int stylesheet_set_font(Stylesheet *s, enum style_element el, char *font) extern int stylesheet_set_alignment(Stylesheet *s, enum style_element el, enum alignment align); extern int stylesheet_set_padding(Stylesheet *s, enum style_element el, struct length padding[4]); extern int stylesheet_set_paraspace(Stylesheet *s, enum style_element el, struct length paraspace[4]); -extern int stylesheet_set_fgcol(Stylesheet *s, enum style_element el, double rgba[4]); +extern int stylesheet_set_fgcol(Stylesheet *s, enum style_element el, struct colour fgcol); extern int stylesheet_set_background(Stylesheet *s, enum style_element el, enum gradient grad, - double bgcol[4], double bgcol2[4]); + struct colour bgcol, struct colour bgcol2); extern int stylesheet_get_geometry(Stylesheet *s, enum style_element el, struct frame_geom *geom); extern const char *stylesheet_get_font(Stylesheet *s, enum style_element el, - double *fgcol, enum alignment *alignment); + struct colour *fgcol, enum alignment *alignment); extern int stylesheet_get_background(Stylesheet *s, enum style_element el, - enum gradient *grad, double *bgcol, double *bgcol2); + enum gradient *grad, struct colour *bgcol, + struct colour *bgcol2); extern int stylesheet_get_padding(Stylesheet *s, enum style_element el, struct length padding[4]); extern int stylesheet_get_paraspace(Stylesheet *s, enum style_element el, |