+#include"abc_html.h"
+
+typedef struct css_border_s
+{
+ char** names;
+ int type;
+} css_border_t;
+
+static char* css_solid [] = {"solid", "实线", NULL};
+static char* css_dotted[] = {"dotted", "点线", NULL};
+static char* css_dashed[] = {"dashed", "虚线", NULL};
+
+static css_border_t css_borders[] =
+{
+ {css_solid, ABC_BORDER_SOLID},
+ {css_dotted, ABC_BORDER_DOTTED},
+ {css_dashed, ABC_BORDER_DASHED},
+};
+
+static void __css_border_type(int* type, const char* name, size_t name_len)
+{
+ int i;
+ for (i = 0; i < sizeof(css_borders) / sizeof(css_borders[0]); i++) {
+ css_border_t* b = &css_borders[i];
+
+ int j;
+ for (j = 0; b->names[j]; j++) {
+ if (!__html_strncmp(b->names[j], name, name_len)) {
+ *type = b->type;
+ return;
+ }
+ }
+ }
+}
+
+int abc_css_border(double* r, double* g, double* b, int* width, int* type, abc_obj_t* obj, const uint8_t* str)
+{
+ *r = 0.0;
+ *g = 0.0;
+ *b = 0.0;
+ *width = 1;
+ *type = ABC_BORDER_SOLID;
+
+ const uint8_t* p = str;
+ const uint8_t* unit = NULL;
+
+ double value = 0.0;
+ int dot = 0;
+ int percent = 0;
+ int prev = 0;
+ int i = 0;
+ int c = 0;
+
+ do {
+ c = *str;
+
+ if ('.' == c)
+ dot = 10;
+ else if ('%' == c)
+ percent++;
+
+ else if ('#' == c) {
+ if (' ' == prev || '\t' == prev || '\r' == prev || '\n' == prev)
+ p = str;
+
+ } else if ('a' <= c && 'z' >= c) {
+
+ if (' ' == prev || '\t' == prev || '\r' == prev || '\n' == prev)
+ p = str;
+ else if (!unit)
+ unit = str;
+
+ } else if ('0' <= c && '9' >= c) {
+
+ if (dot > 0) {
+ value += (c - '0') / (double)dot;
+ dot *= 10;
+ } else {
+ value *= 10.0;
+ value += c - '0';
+ }
+
+ } else if ('\0' == c || ' ' == c || '\t' == c || '\r' == c || '\n' == c) {
+
+ if (' ' == prev || '\t' == prev || '\r' == prev || '\n' == prev)
+ goto next;
+
+ if (percent) {
+ value *= 0.01;
+
+ if (0 == i)
+ *width = value;
+
+ } else if ('a' <= *p && *p <= 'z') {
+ if (1 == i)
+ __css_border_type(type, p, (size_t)(str - p));
+ else if (i > 1)
+ abc_css_color(r, g, b, p);
+
+ } else if ('#' == *p) {
+ if (i > 1)
+ abc_css_color(r, g, b, p);
+
+ } else if (unit) {
+ // for other css units, ignore
+
+ if (0 == i)
+ *width = value;
+ }
+
+ p = str;
+ unit = NULL;
+ value = 0.0;
+ dot = 0;
+ percent = 0;
+ i++;
+
+ if (i > 2)
+ break;
+ }
+
+next:
+ prev = c;
+ str++;
+ } while (c);
+
+ return 0;
+}
+
+int abc_css_margin(abc_obj_t* obj)
+{
+ int type = ABC_BORDER_SOLID;
+ int margin = 2;
+ int border = 0;
+ int padding = 2;
+
+ double r = 0.0;
+ double g = 0.0;
+ double b = 0.0;
+
+ abc_obj_t* attr = abc_obj_find_attr(obj, ABC_HTML_ATTR_MARGIN);
+ if (attr)
+ margin = atoi(attr->value->data);
+
+ attr = abc_obj_find_attr(obj, ABC_HTML_ATTR_BORDER);
+ if (attr)
+ abc_css_border(&r, &g, &b, &border, &type, obj, attr->value->data);
+
+ attr = abc_obj_find_attr(obj, ABC_HTML_ATTR_PADDING);
+ if (attr)
+ padding = atoi(attr->value->data);
+
+ return margin + border + padding;
+}