1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* File: styleengine.cc
*
* Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include "prefs.h"
#include "styleengine.hh"
using namespace dw::core::style;
StyleEngine::StyleEngine (dw::core::Layout *layout) {
StyleAttrs style_attrs;
FontAttrs font_attrs;
stack = new lout::misc::SimpleVector <Node> (1);
cssContext = new CssContext ();
this->layout = layout;
stack->increase ();
Node *n = stack->getRef (stack->size () - 1);
/* Create a dummy font, attribute, and tag for the bottom of the stack. */
font_attrs.name = prefs.vw_fontname;
font_attrs.size = 12;
font_attrs.weight = 400;
font_attrs.style = FONT_STYLE_NORMAL;
style_attrs.initValues ();
style_attrs.font = Font::create (layout, &font_attrs);
style_attrs.color = Color::createSimple (layout, prefs.text_color);
n->style = Style::create (layout, &style_attrs);
}
StyleEngine::~StyleEngine () {
delete stack;
}
void StyleEngine::startElement (int tag, const char *id, const char *klass,
const char *style) {
// fprintf(stderr, "===> START %d %s %s %s\n", tag, id, klass, style);
if (stack->getRef (stack->size () - 1)->style == NULL)
style0 ();
stack->increase ();
Node *n = stack->getRef (stack->size () - 1);
n->style = NULL;
n->depth = stack->size ();
n->tag = tag;
n->id = id;
n->klass = klass;
n->styleAttribute = style;
}
void StyleEngine::setNonCssProperties (CssPropertyList *props) {
style0 (props); // evaluate now, so caller can free props
}
void StyleEngine::endElement (int tag) {
// fprintf(stderr, "===> END %d\n", tag);
assert (stack->size () > 0);
Node *n = stack->getRef (stack->size () - 1);
if (n->style)
n->style->unref ();
stack->setSize (stack->size () - 1);
}
void StyleEngine::apply (StyleAttrs *attrs, CssPropertyList *props) {
FontAttrs fontAttrs = *attrs->font;
for (int i = 0; i < props->size (); i++) {
CssProperty *p = props->getRef (i);
switch (p->name) {
/* \todo missing cases */
case CssProperty::CSS_PROPERTY_BACKGROUND_COLOR:
attrs->backgroundColor =
Color::createSimple (layout, p->value.color);
break;
case CssProperty::CSS_PROPERTY_BORDER_BOTTOM_COLOR:
attrs->borderColor.bottom =
Color::createSimple (layout, p->value.color);
break;
case CssProperty::CSS_PROPERTY_BORDER_BOTTOM_STYLE:
attrs->borderStyle.bottom = p->value.borderStyle;
break;
case CssProperty::CSS_PROPERTY_COLOR:
attrs->color =
Color::createSimple (layout, p->value.color);
break;
case CssProperty::CSS_PROPERTY_CURSOR:
attrs->cursor = p->value.cursor;
break;
case CssProperty::CSS_PROPERTY_FONT_FAMILY:
fontAttrs.name = p->value.name;
break;
case CssProperty::CSS_PROPERTY_FONT_SIZE:
fontAttrs.size = p->value.size;
break;
case CssProperty::CSS_PROPERTY_TEXT_DECORATION:
attrs->textDecoration |= p->value.textDecoration;
break;
case CssProperty::PROPERTY_X_LINK:
attrs->x_link = p->value.x_link;
break;
case CssProperty::PROPERTY_X_IMG:
attrs->x_img = p->value.x_img;
break;
default:
break;
}
}
attrs->font = Font::create (layout, &fontAttrs);
}
Style * StyleEngine::style0 (CssPropertyList *nonCssProperties) {
CssPropertyList props;
CssPropertyList *tagStyleProps = CssPropertyList::parse (
stack->getRef (stack->size () - 1)->styleAttribute);
StyleAttrs attrs = *stack->getRef (stack->size () - 2)->style;
cssContext->apply (&props, this, tagStyleProps, nonCssProperties);
apply (&attrs, &props);
stack->getRef (stack->size () - 1)->style = Style::create (layout, &attrs);
return stack->getRef (stack->size () - 1)->style;
}
|