From 448efdf236c5224047250af97238279f8f29a2aa Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Fri, 6 Feb 2009 20:59:22 +0100 Subject: switch font-weight handling to new multi type system --- src/css.hh | 14 +++++--------- src/cssparser.cc | 28 ++++++++-------------------- src/cssparser.hh | 2 +- src/styleengine.cc | 46 +++++++++++++++++++++++++++++++--------------- 4 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/css.hh b/src/css.hh index 28051e29..56995e62 100644 --- a/src/css.hh +++ b/src/css.hh @@ -168,15 +168,11 @@ typedef union { } CssPropertyValue; typedef enum { - CSS_FONT_WEIGHT_LIGHTER = -1, - CSS_FONT_WEIGHT_BOLDER = -2, - CSS_FONT_WEIGHT_STEP = 300, - /* Some special font weights. */ - CSS_FONT_WEIGHT_LIGHT = 100, - CSS_FONT_WEIGHT_NORMAL = 400, - CSS_FONT_WEIGHT_BOLD = 700, - CSS_FONT_WEIGHT_MIN = 100, - CSS_FONT_WEIGHT_MAX = 900, + CSS_FONT_WEIGHT_BOLD, + CSS_FONT_WEIGHT_BOLDER, + CSS_FONT_WEIGHT_LIGHT, + CSS_FONT_WEIGHT_LIGHTER, + CSS_FONT_WEIGHT_NORMAL, } CssFontWeightExtensions; /** diff --git a/src/cssparser.cc b/src/cssparser.cc index b8facb6d..fbe23f95 100644 --- a/src/cssparser.cc +++ b/src/cssparser.cc @@ -60,6 +60,10 @@ static const char *const Css_font_style_enum_vals[] = { "normal", "italic", "oblique", NULL }; +static const char *const Css_font_weight_enum_vals[] = { + "bold", "bolder", "light", "lighter", "normal", NULL +}; + static const char *const Css_list_style_type_enum_vals[] = { "disc", "circle", "square", "decimal", "decimal-leading-zero", "lower-roman", "upper-roman", "lower-greek", "lower-alpha", @@ -123,7 +127,7 @@ const CssPropertyInfo Css_property_info[CSS_PROPERTY_LAST] = { {"font-stretch", {CSS_TYPE_UNUSED}, NULL}, {"font-style", {CSS_TYPE_ENUM, CSS_TYPE_UNUSED}, Css_font_style_enum_vals}, {"font-variant", {CSS_TYPE_UNUSED}, NULL}, - {"font-weight", {CSS_TYPE_FONT_WEIGHT, CSS_TYPE_UNUSED}, NULL}, + {"font-weight", {CSS_TYPE_ENUM, CSS_TYPE_FONT_WEIGHT, CSS_TYPE_UNUSED}, Css_font_weight_enum_vals}, {"height", {CSS_TYPE_LENGTH_PERCENTAGE, CSS_TYPE_UNUSED}, NULL}, {"left", {CSS_TYPE_UNUSED}, NULL}, {"letter-spacing", {CSS_TYPE_UNUSED}, NULL}, @@ -549,7 +553,7 @@ static bool Css_token_matches_property(CssParser * parser, { int i, err = 1; - for (int j = 0; j < 2 && Css_property_info[prop].type[j] != CSS_TYPE_UNUSED; j++) { + for (int j = 0; Css_property_info[prop].type[j] != CSS_TYPE_UNUSED; j++) { if (type) *type = Css_property_info[prop].type[j]; @@ -610,22 +614,15 @@ static bool Css_token_matches_property(CssParser * parser, i = strtol(parser->tval, NULL, 10); if (i >= 100 && i <= 900) return true; - } else if (parser->ttype == CSS_TK_SYMBOL && - (dStrcasecmp(parser->tval, "normal") == 0 || - dStrcasecmp(parser->tval, "bold") == 0 || - dStrcasecmp(parser->tval, "bolder") == 0 || - dStrcasecmp(parser->tval, "lighter") == 0)) - return true; + } break; case CSS_TYPE_UNUSED: - return false; - case CSS_TYPE_INTEGER: /* Not used for parser values. */ default: assert(false); - return false; + break; } } @@ -771,15 +768,6 @@ static bool Css_parse_value(CssParser * parser, if (ival < 100 || ival > 900) /* invalid */ ival = 0; - } else if (parser->ttype == CSS_TK_SYMBOL) { - if (dStrcasecmp(parser->tval, "normal") == 0) - ival = CSS_FONT_WEIGHT_NORMAL; - if (dStrcasecmp(parser->tval, "bold") == 0) - ival = CSS_FONT_WEIGHT_BOLD; - if (dStrcasecmp(parser->tval, "bolder") == 0) - ival = CSS_FONT_WEIGHT_BOLDER; - if (dStrcasecmp(parser->tval, "lighter") == 0) - ival = CSS_FONT_WEIGHT_LIGHTER; } if (ival != 0) { diff --git a/src/cssparser.hh b/src/cssparser.hh index 2b083df5..7fd3b5d0 100644 --- a/src/cssparser.hh +++ b/src/cssparser.hh @@ -18,7 +18,7 @@ typedef enum { typedef struct { const char *symbol; - const CssValueType type[2]; + const CssValueType type[3]; const char *const *enum_symbols; } CssPropertyInfo; diff --git a/src/styleengine.cc b/src/styleengine.cc index 264406bb..badacbae 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -32,7 +32,7 @@ StyleEngine::StyleEngine (dw::core::Layout *layout) { /* Create a dummy font, attribute, and tag for the bottom of the stack. */ font_attrs.name = prefs.font_sans_serif; font_attrs.size = (int) (14 * prefs.font_factor + 0.5); - font_attrs.weight = CSS_FONT_WEIGHT_NORMAL; + font_attrs.weight = 400; font_attrs.style = FONT_STYLE_NORMAL; style_attrs.initValues (); @@ -193,21 +193,37 @@ void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) { fontAttrs.style = (FontStyle) p->value.intVal; break; case CSS_PROPERTY_FONT_WEIGHT: - switch (p->value.intVal) { - case CSS_FONT_WEIGHT_LIGHTER: - fontAttrs.weight -= CSS_FONT_WEIGHT_STEP; - break; - case CSS_FONT_WEIGHT_BOLDER: - fontAttrs.weight += CSS_FONT_WEIGHT_STEP; - break; - default: - fontAttrs.weight = p->value.intVal; - break; + + if (p->type == CSS_TYPE_ENUM) { + switch (p->value.intVal) { + case CSS_FONT_WEIGHT_BOLD: + fontAttrs.weight = 700; + break; + case CSS_FONT_WEIGHT_BOLDER: + fontAttrs.weight += 300; + break; + case CSS_FONT_WEIGHT_LIGHT: + fontAttrs.weight = 100; + break; + case CSS_FONT_WEIGHT_LIGHTER: + fontAttrs.weight -= 300; + break; + case CSS_FONT_WEIGHT_NORMAL: + fontAttrs.weight = 400; + break; + default: + assert(false); // invalid font weight value + break; + } + } else { + fontAttrs.weight = p->value.intVal; } - if (fontAttrs.weight < CSS_FONT_WEIGHT_MIN) - fontAttrs.weight = CSS_FONT_WEIGHT_MIN; - if (fontAttrs.weight > CSS_FONT_WEIGHT_MAX) - fontAttrs.weight = CSS_FONT_WEIGHT_MAX; + + if (fontAttrs.weight < 100) + fontAttrs.weight = 100; + if (fontAttrs.weight > 900) + fontAttrs.weight = 900; + break; default: break; -- cgit v1.2.3