summaryrefslogtreecommitdiff
path: root/src/bytecode/dump.c
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2008-10-25 01:24:03 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2008-10-25 01:24:03 +0000
commit5caf9a56956f55b70d937cea7e135417bada7b25 (patch)
tree1fec329c7b8133c3ef4b76b18ae48fb161f94c71 /src/bytecode/dump.c
parentc8e73871f9006987983fd1010d97b6168c13f6a3 (diff)
downloadlibcss-5caf9a56956f55b70d937cea7e135417bada7b25.tar.gz
libcss-5caf9a56956f55b70d937cea7e135417bada7b25.tar.bz2
A bunch of property parsers.
Split out !important parsing into a separate function. Support for dumping bytecode to a file handle in some kind of human-readable format. Strings can be represented in the bytecode as a pointer, length pair rather than embedding the string data into the bytecode -- all strings are interned by the core syntax parser. Add todo relating to early destruction of parser object (it shouldn't be needed once parsing is complete). Note documented issue surrounding interned string dictionary, however. In general, it seems wasteful to create a new dictionary containing string representations of keywords for every single parser instance. It would be better to have one central (statically allocated?) dictionary for this and then each parser instance can have a smaller dictionary containing any unknown strings contained within the stylesheet being parsed (e.g. string constants or URLs). svn path=/trunk/libcss/; revision=5627
Diffstat (limited to 'src/bytecode/dump.c')
-rw-r--r--src/bytecode/dump.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/bytecode/dump.c b/src/bytecode/dump.c
new file mode 100644
index 0000000..05213ee
--- /dev/null
+++ b/src/bytecode/dump.c
@@ -0,0 +1,229 @@
+/*
+ * This file is part of LibCSS.
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
+ */
+
+#include "bytecode/bytecode.h"
+#include "bytecode/opcodes.h"
+
+/**
+ * Opcode names, indexed by opcode
+ */
+static const char *opcode_names[] = {
+ "azimuth",
+ "background-attachment",
+ "background-color",
+ "background-image",
+ "background-position",
+ "background-repeat",
+ "border-collapse",
+ "border-spacing",
+ "border-trbl-color",
+ "border-trbl-style",
+ "border-trbl-width",
+ "bottom",
+ "caption-side",
+ "clear",
+ "clip",
+ "color",
+ "content",
+ "counter-increment",
+ "counter-reset",
+ "cue-after",
+ "cue-before",
+ "cursor",
+ "direction",
+ "display",
+ "elevation",
+ "empty-cells",
+ "float",
+ "font-family",
+ "font-size",
+ "font-style",
+ "font-variant",
+ "font-weight",
+ "height",
+ "left",
+ "letter-spacing",
+ "line-height",
+ "list-style-image",
+ "list-style-position",
+ "list-style-type",
+ "margin-trbl",
+ "max-height",
+ "max-width",
+ "min-height",
+ "min-width",
+ "orphans",
+ "outline-color",
+ "outline-style",
+ "outline-width",
+ "overflow",
+ "padding-trbl",
+ "page-break-after",
+ "page-break-before",
+ "page-break-inside",
+ "pause-after",
+ "pause-before",
+ "pitch-range",
+ "pitch",
+ "play-during",
+ "position",
+ "quotes",
+ "richness",
+ "right",
+ "speak-header",
+ "speak-numeral",
+ "speak-punctuation",
+ "speak",
+ "speech-rate",
+ "stress",
+ "table-layout",
+ "text-align",
+ "text-decoration",
+ "text-indent",
+ "text-transform",
+ "top",
+ "unicode-bidi",
+ "vertical-align",
+ "visibility",
+ "voice-family",
+ "volume",
+ "white-space",
+ "widows",
+ "width",
+ "word-spacing",
+ "z-index",
+};
+
+/**
+ * Dump a CSS bytecode stream to the given file handle
+ *
+ * \param bytecode The stream to dump
+ * \param length Length, in bytes, of bytecode
+ * \param fp File handle to output to
+ */
+void css_bytecode_dump(void *bytecode, uint32_t length, FILE *fp)
+{
+ uint32_t offset = 0;
+
+#define ADVANCE(n) do { \
+ offset += (n); \
+ bytecode = ((uint8_t *) bytecode) + (n); \
+} while(0)
+
+ while (offset < length) {
+ opcode op;
+ uint8_t flags;
+ uint16_t value;
+ uint32_t opv = *((uint32_t *) bytecode);
+
+ ADVANCE(sizeof(opv));
+
+ op = getOpcode(opv);
+
+ fprintf(fp, "%s: ", opcode_names[op]);
+
+ flags = getFlags(opv);
+
+ if (flags & FLAG_INHERIT) {
+ fprintf(fp, "inherit");
+ } else {
+ value = getValue(opv);
+
+ switch (op) {
+ case OP_BACKGROUND_ATTACHMENT:
+ switch (value) {
+ case BACKGROUND_ATTACHMENT_FIXED:
+ fprintf(fp, "fixed");
+ break;
+ case BACKGROUND_ATTACHMENT_SCROLL:
+ fprintf(fp, "scroll");
+ break;
+ }
+ break;
+ case OP_BACKGROUND_COLOR:
+ switch (value) {
+ case BACKGROUND_COLOR_TRANSPARENT:
+ fprintf(fp, "transparent");
+ break;
+ case BACKGROUND_COLOR_SET:
+ {
+ uint32_t colour =
+ *((uint32_t *) bytecode);
+ ADVANCE(sizeof(colour));
+ fprintf(fp, "#%08x", colour);
+ }
+ break;
+ }
+ break;
+ case OP_BACKGROUND_IMAGE:
+ switch (value) {
+ case BACKGROUND_IMAGE_NONE:
+ fprintf(fp, "none");
+ break;
+ case BACKGROUND_IMAGE_URI:
+ {
+ uint8_t *ptr =
+ *((uint8_t **) bytecode);
+ ADVANCE(sizeof(ptr));
+ size_t len =
+ *((size_t *) bytecode);
+ ADVANCE(sizeof(len));
+ fprintf(fp, "url('%.*s')", (int) len,
+ (char *) ptr);
+ }
+ break;
+ }
+ break;
+ case OP_BACKGROUND_REPEAT:
+ switch (value) {
+ case BACKGROUND_REPEAT_NO_REPEAT:
+ fprintf(fp, "no-repeat");
+ break;
+ case BACKGROUND_REPEAT_REPEAT_X:
+ fprintf(fp, "repeat-x");
+ break;
+ case BACKGROUND_REPEAT_REPEAT_Y:
+ fprintf(fp, "repeat-y");
+ break;
+ case BACKGROUND_REPEAT_REPEAT:
+ fprintf(fp, "repeat");
+ break;
+ }
+ break;
+ case OP_CLEAR:
+ switch (value) {
+ case CLEAR_NONE:
+ fprintf(fp, "none");
+ break;
+ case CLEAR_LEFT:
+ fprintf(fp, "left");
+ break;
+ case CLEAR_RIGHT:
+ fprintf(fp, "right");
+ break;
+ case CLEAR_BOTH:
+ fprintf(fp, "both");
+ break;
+ }
+ break;
+ default:
+ fprintf(fp, "Unknown opcode %x", op);
+ break;
+ }
+ }
+
+ if (flags & FLAG_IMPORTANT)
+ fprintf(fp, " !important");
+
+ fprintf(fp, "; ");
+ }
+
+#undef ADVANCE
+
+}
+
+