diff options
-rw-r--r-- | src/css.cc | 12 | ||||
-rw-r--r-- | src/cssparser.hh | 2 | ||||
-rw-r--r-- | src/styleengine.cc | 49 | ||||
-rw-r--r-- | src/styleengine.hh | 2 |
4 files changed, 64 insertions, 1 deletions
@@ -149,7 +149,7 @@ void CssContext::addRule (CssRule *rule, CssPrimaryOrder order) { void CssContext::buildUserAgentStyle () { char *cssBuf = "body {background-color: 0xdcd1ba; font-family: helvetica; color: black;" -// " margin-left: 5px; margin-top: 5px; margin-bottom: 5px; margin-right: 5px;" + " margin-left: 5px; margin-top: 5px; margin-bottom: 5px; margin-right: 5px;" " }" ":link {color: blue; text-decoration: underline; cursor: pointer; } " ":visited {color: green; text-decoration: underline; cursor: pointer; } " @@ -159,6 +159,16 @@ void CssContext::buildUserAgentStyle () { "h4 {font-weight: bold} "; a_Css_parse (this, cssBuf, strlen (cssBuf), 0, CSS_ORIGIN_USER_AGENT); + + char buf[10000]; + + FILE *fp = fopen ("/tmp/style.css", "r"); + if (fp) { + fread (buf, 1, sizeof (buf), fp); + + a_Css_parse (this, buf, strlen (buf), 0, CSS_ORIGIN_AUTHOR); + + } } void CssContext::buildUserStyle () { diff --git a/src/cssparser.hh b/src/cssparser.hh index c5b69da0..fc9cbccd 100644 --- a/src/cssparser.hh +++ b/src/cssparser.hh @@ -1,6 +1,8 @@ #ifndef __CSSPARSER_HH__ #define __CSSPARSER_HH__ +#include "css.hh" + /* The last three ones are never parsed. */ #define CSS_NUM_INTERNAL_PROPERTIES 3 #define CSS_NUM_PARSED_PROPERTIES \ diff --git a/src/styleengine.cc b/src/styleengine.cc index 76f23777..f89e60eb 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -10,6 +10,8 @@ */ #include <stdio.h> +#include <math.h> +#include "cssparser.hh" #include "styleengine.hh" using namespace dw::core::style; @@ -207,9 +209,56 @@ void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) { } } + fontAttrs.size = computeValue (fontAttrs.size, + stack->get (stack->size () - 2).style->font); attrs->font = Font::create (layout, &fontAttrs); + + computeValues (&attrs->borderWidth, attrs->font); + computeValues (&attrs->margin, attrs->font); + computeValues (&attrs->padding, attrs->font); + attrs->width = computeValue (attrs->width, attrs->font); + attrs->height = computeValue (attrs->height, attrs->font); +} + +/** + * \brief Resolve relative lengths to absolute values. + * The font must be set already. + */ +void StyleEngine::computeValues (Box *box, Font *font) { + box->bottom = computeValue (box->bottom, font); + box->left = computeValue (box->left, font); + box->right = computeValue (box->right, font); + box->top = computeValue (box->top, font); } +int StyleEngine::computeValue (int value, Font *font) { + int ret; + static float dpmm; + + if (dpmm == 0.0) + dpmm = layout->dpiX () / 2.54; /* assume dpiX == dpiY */ + + switch (CSS_LENGTH_TYPE (value)) { + case CSS_LENGTH_TYPE_PX: + ret = (int) rintf (CSS_LENGTH_VALUE(value)); + break; + case CSS_LENGTH_TYPE_MM: + ret = (int) rintf (CSS_LENGTH_VALUE(value) * dpmm); + break; + case CSS_LENGTH_TYPE_EM: + ret = (int) rintf (CSS_LENGTH_VALUE(value) * font->size); + break; + case CSS_LENGTH_TYPE_EX: + ret = (int) rintf (CSS_LENGTH_VALUE(value) * font->xHeight); + break; + default: + ret = value; + break; + } + return ret; +} + + /** * \brief Create a new style object based on the previously opened / closed * HTML elements and the nonCssProperties that have been set. diff --git a/src/styleengine.hh b/src/styleengine.hh index 76ca226c..3c9a7951 100644 --- a/src/styleengine.hh +++ b/src/styleengine.hh @@ -19,6 +19,8 @@ class StyleEngine : public Doctree { dw::core::style::Style *style0 (CssPropertyList *nonCssProperties = NULL); void apply (dw::core::style::StyleAttrs *attrs, CssPropertyList *props); + int computeValue (int value, dw::core::style::Font *font); + void computeValues (dw::core::style::Box *box, dw::core::style::Font *font); public: StyleEngine (dw::core::Layout *layout); |