+static int
+lookup_color_name (const char *s)
+{
+ struct color
+ {
+ struct hmap_node hmap_node;
+ const char *name;
+ int code;
+ };
+
+ static struct color colors[] =
+ {
+ { .name = "aliceblue", .code = 0xf0f8ff },
+ { .name = "antiquewhite", .code = 0xfaebd7 },
+ { .name = "aqua", .code = 0x00ffff },
+ { .name = "aquamarine", .code = 0x7fffd4 },
+ { .name = "azure", .code = 0xf0ffff },
+ { .name = "beige", .code = 0xf5f5dc },
+ { .name = "bisque", .code = 0xffe4c4 },
+ { .name = "black", .code = 0x000000 },
+ { .name = "blanchedalmond", .code = 0xffebcd },
+ { .name = "blue", .code = 0x0000ff },
+ { .name = "blueviolet", .code = 0x8a2be2 },
+ { .name = "brown", .code = 0xa52a2a },
+ { .name = "burlywood", .code = 0xdeb887 },
+ { .name = "cadetblue", .code = 0x5f9ea0 },
+ { .name = "chartreuse", .code = 0x7fff00 },
+ { .name = "chocolate", .code = 0xd2691e },
+ { .name = "coral", .code = 0xff7f50 },
+ { .name = "cornflowerblue", .code = 0x6495ed },
+ { .name = "cornsilk", .code = 0xfff8dc },
+ { .name = "crimson", .code = 0xdc143c },
+ { .name = "cyan", .code = 0x00ffff },
+ { .name = "darkblue", .code = 0x00008b },
+ { .name = "darkcyan", .code = 0x008b8b },
+ { .name = "darkgoldenrod", .code = 0xb8860b },
+ { .name = "darkgray", .code = 0xa9a9a9 },
+ { .name = "darkgreen", .code = 0x006400 },
+ { .name = "darkgrey", .code = 0xa9a9a9 },
+ { .name = "darkkhaki", .code = 0xbdb76b },
+ { .name = "darkmagenta", .code = 0x8b008b },
+ { .name = "darkolivegreen", .code = 0x556b2f },
+ { .name = "darkorange", .code = 0xff8c00 },
+ { .name = "darkorchid", .code = 0x9932cc },
+ { .name = "darkred", .code = 0x8b0000 },
+ { .name = "darksalmon", .code = 0xe9967a },
+ { .name = "darkseagreen", .code = 0x8fbc8f },
+ { .name = "darkslateblue", .code = 0x483d8b },
+ { .name = "darkslategray", .code = 0x2f4f4f },
+ { .name = "darkslategrey", .code = 0x2f4f4f },
+ { .name = "darkturquoise", .code = 0x00ced1 },
+ { .name = "darkviolet", .code = 0x9400d3 },
+ { .name = "deeppink", .code = 0xff1493 },
+ { .name = "deepskyblue", .code = 0x00bfff },
+ { .name = "dimgray", .code = 0x696969 },
+ { .name = "dimgrey", .code = 0x696969 },
+ { .name = "dodgerblue", .code = 0x1e90ff },
+ { .name = "firebrick", .code = 0xb22222 },
+ { .name = "floralwhite", .code = 0xfffaf0 },
+ { .name = "forestgreen", .code = 0x228b22 },
+ { .name = "fuchsia", .code = 0xff00ff },
+ { .name = "gainsboro", .code = 0xdcdcdc },
+ { .name = "ghostwhite", .code = 0xf8f8ff },
+ { .name = "gold", .code = 0xffd700 },
+ { .name = "goldenrod", .code = 0xdaa520 },
+ { .name = "gray", .code = 0x808080 },
+ { .name = "green", .code = 0x008000 },
+ { .name = "greenyellow", .code = 0xadff2f },
+ { .name = "grey", .code = 0x808080 },
+ { .name = "honeydew", .code = 0xf0fff0 },
+ { .name = "hotpink", .code = 0xff69b4 },
+ { .name = "indianred", .code = 0xcd5c5c },
+ { .name = "indigo", .code = 0x4b0082 },
+ { .name = "ivory", .code = 0xfffff0 },
+ { .name = "khaki", .code = 0xf0e68c },
+ { .name = "lavender", .code = 0xe6e6fa },
+ { .name = "lavenderblush", .code = 0xfff0f5 },
+ { .name = "lawngreen", .code = 0x7cfc00 },
+ { .name = "lemonchiffon", .code = 0xfffacd },
+ { .name = "lightblue", .code = 0xadd8e6 },
+ { .name = "lightcoral", .code = 0xf08080 },
+ { .name = "lightcyan", .code = 0xe0ffff },
+ { .name = "lightgoldenrodyellow", .code = 0xfafad2 },
+ { .name = "lightgray", .code = 0xd3d3d3 },
+ { .name = "lightgreen", .code = 0x90ee90 },
+ { .name = "lightgrey", .code = 0xd3d3d3 },
+ { .name = "lightpink", .code = 0xffb6c1 },
+ { .name = "lightsalmon", .code = 0xffa07a },
+ { .name = "lightseagreen", .code = 0x20b2aa },
+ { .name = "lightskyblue", .code = 0x87cefa },
+ { .name = "lightslategray", .code = 0x778899 },
+ { .name = "lightslategrey", .code = 0x778899 },
+ { .name = "lightsteelblue", .code = 0xb0c4de },
+ { .name = "lightyellow", .code = 0xffffe0 },
+ { .name = "lime", .code = 0x00ff00 },
+ { .name = "limegreen", .code = 0x32cd32 },
+ { .name = "linen", .code = 0xfaf0e6 },
+ { .name = "magenta", .code = 0xff00ff },
+ { .name = "maroon", .code = 0x800000 },
+ { .name = "mediumaquamarine", .code = 0x66cdaa },
+ { .name = "mediumblue", .code = 0x0000cd },
+ { .name = "mediumorchid", .code = 0xba55d3 },
+ { .name = "mediumpurple", .code = 0x9370db },
+ { .name = "mediumseagreen", .code = 0x3cb371 },
+ { .name = "mediumslateblue", .code = 0x7b68ee },
+ { .name = "mediumspringgreen", .code = 0x00fa9a },
+ { .name = "mediumturquoise", .code = 0x48d1cc },
+ { .name = "mediumvioletred", .code = 0xc71585 },
+ { .name = "midnightblue", .code = 0x191970 },
+ { .name = "mintcream", .code = 0xf5fffa },
+ { .name = "mistyrose", .code = 0xffe4e1 },
+ { .name = "moccasin", .code = 0xffe4b5 },
+ { .name = "navajowhite", .code = 0xffdead },
+ { .name = "navy", .code = 0x000080 },
+ { .name = "oldlace", .code = 0xfdf5e6 },
+ { .name = "olive", .code = 0x808000 },
+ { .name = "olivedrab", .code = 0x6b8e23 },
+ { .name = "orange", .code = 0xffa500 },
+ { .name = "orangered", .code = 0xff4500 },
+ { .name = "orchid", .code = 0xda70d6 },
+ { .name = "palegoldenrod", .code = 0xeee8aa },
+ { .name = "palegreen", .code = 0x98fb98 },
+ { .name = "paleturquoise", .code = 0xafeeee },
+ { .name = "palevioletred", .code = 0xdb7093 },
+ { .name = "papayawhip", .code = 0xffefd5 },
+ { .name = "peachpuff", .code = 0xffdab9 },
+ { .name = "peru", .code = 0xcd853f },
+ { .name = "pink", .code = 0xffc0cb },
+ { .name = "plum", .code = 0xdda0dd },
+ { .name = "powderblue", .code = 0xb0e0e6 },
+ { .name = "purple", .code = 0x800080 },
+ { .name = "red", .code = 0xff0000 },
+ { .name = "rosybrown", .code = 0xbc8f8f },
+ { .name = "royalblue", .code = 0x4169e1 },
+ { .name = "saddlebrown", .code = 0x8b4513 },
+ { .name = "salmon", .code = 0xfa8072 },
+ { .name = "sandybrown", .code = 0xf4a460 },
+ { .name = "seagreen", .code = 0x2e8b57 },
+ { .name = "seashell", .code = 0xfff5ee },
+ { .name = "sienna", .code = 0xa0522d },
+ { .name = "silver", .code = 0xc0c0c0 },
+ { .name = "skyblue", .code = 0x87ceeb },
+ { .name = "slateblue", .code = 0x6a5acd },
+ { .name = "slategray", .code = 0x708090 },
+ { .name = "slategrey", .code = 0x708090 },
+ { .name = "snow", .code = 0xfffafa },
+ { .name = "springgreen", .code = 0x00ff7f },
+ { .name = "steelblue", .code = 0x4682b4 },
+ { .name = "tan", .code = 0xd2b48c },
+ { .name = "teal", .code = 0x008080 },
+ { .name = "thistle", .code = 0xd8bfd8 },
+ { .name = "tomato", .code = 0xff6347 },
+ { .name = "turquoise", .code = 0x40e0d0 },
+ { .name = "violet", .code = 0xee82ee },
+ { .name = "wheat", .code = 0xf5deb3 },
+ { .name = "white", .code = 0xffffff },
+ { .name = "whitesmoke", .code = 0xf5f5f5 },
+ { .name = "yellow", .code = 0xffff00 },
+ { .name = "yellowgreen", .code = 0x9acd32 },
+ };
+
+ static struct hmap color_table = HMAP_INITIALIZER (color_table);
+
+ if (hmap_is_empty (&color_table))
+ for (size_t i = 0; i < sizeof colors / sizeof *colors; i++)
+ hmap_insert (&color_table, &colors[i].hmap_node,
+ hash_string (colors[i].name, 0));
+
+ const struct color *color;
+ HMAP_FOR_EACH_WITH_HASH (color, struct color, hmap_node,
+ hash_string (s, 0), &color_table)
+ if (!strcmp (color->name, s))
+ return color->code;
+ return -1;
+}
+
+static bool
+parse_color__ (const char *s, struct cell_color *color)
+{
+ /* #rrrrggggbbbb */
+ uint16_t r16, g16, b16;
+ int len;
+ if (sscanf (s, "#%4"SCNx16"%4"SCNx16"%4"SCNx16"%n",
+ &r16, &g16, &b16, &len) == 3
+ && len == 13
+ && !s[len])
+ {
+ color->r = r16 >> 8;
+ color->g = g16 >> 8;
+ color->b = b16 >> 8;
+ return true;
+ }
+
+ /* #rrggbb */
+ uint8_t r, g, b;
+ if (sscanf (s, "#%2"SCNx8"%2"SCNx8"%2"SCNx8"%n", &r, &g, &b, &len) == 3
+ && len == 7
+ && !s[len])
+ {
+ color->r = r;
+ color->g = g;
+ color->b = b;
+ return true;
+ }
+
+ /* rrggbb */
+ if (sscanf (s, "%2"SCNx8"%2"SCNx8"%2"SCNx8"%n", &r, &g, &b, &len) == 3
+ && len == 6
+ && !s[len])
+ {
+ color->r = r;
+ color->g = g;
+ color->b = b;
+ return true;
+ }
+
+ /* rgb(r,g,b) */
+ if (sscanf (s, "rgb (%"SCNi8" , %"SCNi8" , %"SCNi8") %n",
+ &r, &g, &b, &len) == 3
+ && !s[len])
+ {
+ color->r = r;
+ color->g = g;
+ color->b = b;
+ return true;
+ }
+
+ /* rgba(r,g,b,a), ignoring a. */
+ if (sscanf (s, "rgba (%"SCNi8" , %"SCNi8" , %"SCNi8", %*f) %n",
+ &r, &g, &b, &len) == 3
+ && !s[len])
+ {
+ color->r = r;
+ color->g = g;
+ color->b = b;
+ return true;
+ }
+
+ int code = lookup_color_name (s);
+ if (code >= 0)
+ {
+ color->r = code >> 16;
+ color->g = code >> 8;
+ color->b = code;
+ return true;
+ }
+
+ return false;
+}
+
+/* Parse color information specified by KEY into {RED,GREEN,BLUE}.
+ Currently, the input string must be of the form "#RRRRGGGGBBBB"
+ Future implementations might allow things like "yellow" and
+ "sky-blue-ultra-brown"
+*/
+void
+parse_color (struct output_driver *d, struct string_map *options,
+ const char *key, const char *default_value,
+ struct cell_color *color)
+{
+ char *string = parse_string (opt (d, options, key, default_value));
+ if (!parse_color__ (string, color) && !parse_color__ (default_value, color))
+ *color = (struct cell_color) CELL_COLOR_BLACK;
+ free (string);
+}
+
+static PangoFontDescription *
+parse_font (const char *font, int default_size, bool bold, bool italic)
+{
+ if (!c_strcasecmp (font, "Monospaced"))
+ font = "Monospace";
+
+ PangoFontDescription *desc = pango_font_description_from_string (font);
+ if (desc == NULL)
+ return NULL;
+
+ /* If the font description didn't include an explicit font size, then set it
+ to DEFAULT_SIZE, which is in inch/72000 units. */
+ if (!(pango_font_description_get_set_fields (desc) & PANGO_FONT_MASK_SIZE))
+ pango_font_description_set_size (desc,
+ (default_size / 1000.0) * PANGO_SCALE);
+
+ pango_font_description_set_weight (desc, (bold
+ ? PANGO_WEIGHT_BOLD
+ : PANGO_WEIGHT_NORMAL));
+ pango_font_description_set_style (desc, (italic
+ ? PANGO_STYLE_ITALIC
+ : PANGO_STYLE_NORMAL));
+
+ return desc;
+}
+
+static PangoFontDescription *
+parse_font_option (struct output_driver *d, struct string_map *options,
+ const char *key, const char *default_value,
+ int default_size, bool bold, bool italic)
+{
+ char *string = parse_string (opt (d, options, key, default_value));
+ PangoFontDescription *desc = parse_font (string, default_size, bold, italic);
+ if (!desc)
+ {
+ msg (MW, _("`%s': bad font specification"), string);
+
+ /* Fall back to DEFAULT_VALUE, which had better be a valid font
+ description. */
+ desc = parse_font (default_value, default_size, bold, italic);
+ assert (desc != NULL);
+ }
+ free (string);
+
+ return desc;
+}
+
+static void
+apply_options (struct xr_driver *xr, struct string_map *o)
+{
+ struct output_driver *d = &xr->driver;
+
+ /* In inch/72000 units used by parse_paper_size() and parse_dimension(). */
+ int left_margin, right_margin;
+ int top_margin, bottom_margin;
+ int paper_width, paper_length;
+ int font_size;
+ int min_break[TABLE_N_AXES];
+
+ /* Scale factor from inch/72000 to inch/(72 * XR_POINT). */
+ const double scale = XR_POINT / 1000.;
+
+ int i;
+
+ for (i = 0; i < XR_N_FONTS; i++)
+ {
+ struct xr_font *font = &xr->fonts[i];
+
+ if (font->desc != NULL)
+ pango_font_description_free (font->desc);
+ }
+
+ font_size = parse_int (opt (d, o, "font-size", "10000"), 1000, 1000000);
+ xr->fonts[XR_FONT_FIXED].desc = parse_font_option
+ (d, o, "fixed-font", "monospace", font_size, false, false);
+ xr->fonts[XR_FONT_PROPORTIONAL].desc = parse_font_option (
+ d, o, "prop-font", "sans serif", font_size, false, false);
+ xr->fonts[XR_FONT_EMPHASIS].desc = parse_font_option (
+ d, o, "emph-font", "sans serif", font_size, false, true);
+
+ parse_color (d, o, "background-color", "#FFFFFFFFFFFF", &xr->bg);
+ parse_color (d, o, "foreground-color", "#000000000000", &xr->fg);
+
+ xr->transparent = parse_boolean (opt (d, o, "transparent", "false"));
+ xr->systemcolors = parse_boolean (opt (d, o, "systemcolors", "false"));
+
+ /* Get dimensions. */
+ parse_paper_size (opt (d, o, "paper-size", ""), &paper_width, &paper_length);
+ left_margin = parse_dimension (opt (d, o, "left-margin", ".5in"));
+ right_margin = parse_dimension (opt (d, o, "right-margin", ".5in"));
+ top_margin = parse_dimension (opt (d, o, "top-margin", ".5in"));
+ bottom_margin = parse_dimension (opt (d, o, "bottom-margin", ".5in"));
+
+ min_break[H] = parse_dimension (opt (d, o, "min-hbreak", NULL)) * scale;
+ min_break[V] = parse_dimension (opt (d, o, "min-vbreak", NULL)) * scale;
+
+ int object_spacing = (parse_dimension (opt (d, o, "object-spacing", NULL))
+ * scale);
+
+ /* Convert to inch/(XR_POINT * 72). */
+ xr->left_margin = left_margin * scale;
+ xr->right_margin = right_margin * scale;
+ xr->top_margin = top_margin * scale;
+ xr->bottom_margin = bottom_margin * scale;
+ xr->width = (paper_width - left_margin - right_margin) * scale;
+ xr->length = (paper_length - top_margin - bottom_margin) * scale;
+ xr->min_break[H] = min_break[H] >= 0 ? min_break[H] : xr->width / 2;
+ xr->min_break[V] = min_break[V] >= 0 ? min_break[V] : xr->length / 2;
+ xr->object_spacing = object_spacing >= 0 ? object_spacing : XR_POINT * 12;
+
+ /* There are no headings so headings_height can stay 0. */
+}
+