Add support for reading and writing SPV files.
[pspp] / src / output / cairo.c
index d00a66be68e2d9aedfa1cac1cf7dbfd69c77449f..11f24d0404925c9f4451caa0316186a5bbccbbf4 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "libpspp/assertion.h"
 #include "libpspp/cast.h"
+#include "libpspp/hash-functions.h"
 #include "libpspp/message.h"
 #include "libpspp/pool.h"
 #include "libpspp/start-date.h"
@@ -43,8 +44,8 @@
 #include "output/message-item.h"
 #include "output/options.h"
 #include "output/page-setup-item.h"
+#include "output/pivot-table.h"
 #include "output/render.h"
-#include "output/tab.h"
 #include "output/table-item.h"
 #include "output/table.h"
 #include "output/text-item.h"
@@ -53,6 +54,7 @@
 #include <cairo/cairo-ps.h>
 #include <cairo/cairo-svg.h>
 #include <cairo/cairo.h>
+#include <inttypes.h>
 #include <math.h>
 #include <pango/pango-font.h>
 #include <pango/pango-layout.h>
@@ -153,8 +155,8 @@ struct xr_driver
     int min_break[TABLE_N_AXES]; /* Min cell size to break across pages. */
     int object_spacing;         /* Space between output objects. */
 
-    struct xr_color bg;    /* Background color */
-    struct xr_color fg;    /* Foreground color */
+    struct cell_color bg;       /* Background color */
+    struct cell_color fg;       /* Foreground color */
 
     int initial_page_number;
 
@@ -214,6 +216,256 @@ opt (struct output_driver *d, struct string_map *options, const char *key,
   return driver_option_get (d, options, key, default_value);
 }
 
+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
@@ -222,27 +474,12 @@ opt (struct output_driver *d, struct string_map *options, const char *key,
 void
 parse_color (struct output_driver *d, struct string_map *options,
             const char *key, const char *default_value,
-            struct xr_color *color)
+            struct cell_color *color)
 {
-  int red, green, blue;
   char *string = parse_string (opt (d, options, key, default_value));
-
-  if (3 != sscanf (string, "#%04x%04x%04x", &red, &green, &blue))
-    {
-      /* If the parsed option string fails, then try the default value */
-      if ( 3 != sscanf (default_value, "#%04x%04x%04x", &red, &green, &blue))
-       {
-         /* ... and if that fails set everything to zero */
-         red = green = blue = 0;
-       }
-    }
-
+  if (!parse_color__ (string, color) && !parse_color__ (default_value, color))
+    *color = (struct cell_color) CELL_COLOR_BLACK;
   free (string);
-
-  /* Convert 16 bit ints to float */
-  color->red = red / (double) 0xFFFF;
-  color->green = green / (double) 0xFFFF;
-  color->blue = blue / (double) 0xFFFF;
 }
 
 static PangoFontDescription *
@@ -408,6 +645,14 @@ xr_measure_fonts (cairo_t *cairo, const struct xr_font fonts[XR_N_FONTS],
     }
 }
 
+static int
+get_layout_height (PangoLayout *layout)
+{
+  int w, h;
+  pango_layout_get_size (layout, &w, &h);
+  return h;
+}
+
 static int
 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
                         const struct page_heading *ph, int page_number,
@@ -426,10 +671,12 @@ xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
       pango_layout_set_markup (layout, markup, -1);
       free (markup);
 
-      pango_layout_set_alignment (layout,
-                                  (pp->halign == TAB_RIGHT ? PANGO_ALIGN_RIGHT
-                                   : pp->halign == TAB_LEFT ? PANGO_ALIGN_LEFT
-                                   : PANGO_ALIGN_CENTER));
+      pango_layout_set_alignment (
+        layout,
+        (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
+         : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
+         : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
+         : PANGO_ALIGN_RIGHT));
       pango_layout_set_width (layout, xr_to_pango (width));
       if (draw)
         {
@@ -439,9 +686,7 @@ xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
           cairo_restore (cairo);
         }
 
-      int w, h;
-      pango_layout_get_size (layout, &w, &h);
-      y += pango_to_xr (h);
+      y += pango_to_xr (get_layout_height (layout));
     }
 
   g_object_unref (G_OBJECT (layout));
@@ -553,7 +798,8 @@ xr_set_cairo (struct xr_driver *xr, cairo_t *cairo)
       xr->params->rtl = render_direction_rtl ();
     }
 
-  cairo_set_source_rgb (xr->cairo, xr->fg.red, xr->fg.green, xr->fg.blue);
+  cairo_set_source_rgb (xr->cairo,
+                        xr->fg.r / 255.0, xr->fg.g / 255.0, xr->fg.b / 255.0);
 }
 
 static struct output_driver *
@@ -751,7 +997,8 @@ void
 xr_driver_next_page (struct xr_driver *xr, cairo_t *cairo)
 {
   cairo_save (cairo);
-  cairo_set_source_rgb (cairo, xr->bg.red, xr->bg.green, xr->bg.blue);
+  cairo_set_source_rgb (cairo,
+                        xr->bg.r / 255.0, xr->bg.g / 255.0, xr->bg.b / 255.0);
   cairo_rectangle (cairo, 0, 0, xr->width, xr->length);
   cairo_fill (cairo);
   cairo_restore (cairo);
@@ -824,13 +1071,20 @@ xr_layout_cell (struct xr_driver *, const struct table_cell *,
                 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
                 int *width, int *height, int *brk);
 
+static void
+set_source_rgba (cairo_t *cairo, const struct cell_color *color)
+{
+  cairo_set_source_rgba (cairo,
+                         color->r / 255., color->g / 255., color->b / 255.,
+                         color->alpha / 255.);
+}
+
 static void
 dump_line (struct xr_driver *xr, int x0, int y0, int x1, int y1, int style,
            const struct cell_color *color)
 {
   cairo_new_path (xr->cairo);
-  cairo_set_source_rgb (xr->cairo,
-                        color->r / 255.0, color->g / 255.0, color->b / 255.0);
+  set_source_rgba (xr->cairo, color);
   cairo_set_line_width (
     xr->cairo,
     xr_to_pt (style == RENDER_LINE_THICK ? XR_LINE_WIDTH * 2
@@ -1056,11 +1310,11 @@ xr_measure_cell_width (void *xr_, const struct table_cell *cell,
   xr_layout_cell (xr, cell, bb, clip, min_width, &h, NULL);
 
   if (*min_width > 0)
-    *min_width += px_to_xr (cell->style->margin[H][0]
-                            + cell->style->margin[H][1]);
+    *min_width += px_to_xr (cell->style->cell_style.margin[H][0]
+                            + cell->style->cell_style.margin[H][1]);
   if (*max_width > 0)
-    *max_width += px_to_xr (cell->style->margin[H][0]
-                            + cell->style->margin[H][1]);
+    *max_width += px_to_xr (cell->style->cell_style.margin[H][0]
+                            + cell->style->cell_style.margin[H][1]);
 }
 
 static int
@@ -1072,13 +1326,14 @@ xr_measure_cell_height (void *xr_, const struct table_cell *cell, int width)
   int w, h;
 
   bb[H][0] = 0;
-  bb[H][1] = width - px_to_xr (cell->style->margin[H][0]
-                               + cell->style->margin[H][1]);
+  bb[H][1] = width - px_to_xr (cell->style->cell_style.margin[H][0]
+                               + cell->style->cell_style.margin[H][1]);
   bb[V][0] = 0;
   bb[V][1] = INT_MAX;
   clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
   xr_layout_cell (xr, cell, bb, clip, &w, &h, NULL);
-  h += px_to_xr (cell->style->margin[V][0] + cell->style->margin[V][1]);
+  h += px_to_xr (cell->style->cell_style.margin[V][0]
+                 + cell->style->cell_style.margin[V][1]);
   return h;
 }
 
@@ -1106,10 +1361,7 @@ xr_draw_cell (void *xr_, const struct table_cell *cell, int color_idx,
         bg_clip[axis][1] += spill[axis][1];
     }
   xr_clip (xr, bg_clip);
-  cairo_set_source_rgb (xr->cairo,
-                        cell->style->bg[color_idx].r / 255.,
-                        cell->style->bg[color_idx].g / 255.,
-                        cell->style->bg[color_idx].b / 255.);
+  set_source_rgba (xr->cairo, &cell->style->font_style.bg[color_idx]);
   fill_rectangle (xr,
                   bb[H][0] - spill[H][0],
                   bb[V][0] - spill[V][0],
@@ -1118,15 +1370,12 @@ xr_draw_cell (void *xr_, const struct table_cell *cell, int color_idx,
   cairo_restore (xr->cairo);
 
   cairo_save (xr->cairo);
-  cairo_set_source_rgb (xr->cairo,
-                        cell->style->fg[color_idx].r / 255.,
-                        cell->style->fg[color_idx].g / 255.,
-                        cell->style->fg[color_idx].b / 255.);
+  set_source_rgba (xr->cairo, &cell->style->font_style.fg[color_idx]);
 
   for (int axis = 0; axis < TABLE_N_AXES; axis++)
     {
-      bb[axis][0] += px_to_xr (cell->style->margin[axis][0]);
-      bb[axis][1] -= px_to_xr (cell->style->margin[axis][1]);
+      bb[axis][0] += px_to_xr (cell->style->cell_style.margin[axis][0]);
+      bb[axis][1] -= px_to_xr (cell->style->cell_style.margin[axis][1]);
     }
   if (bb[H][0] < bb[H][1] && bb[V][0] < bb[V][1])
     xr_layout_cell (xr, cell, bb, clip, &w, &h, &brk);
@@ -1146,13 +1395,13 @@ xr_adjust_break (void *xr_, const struct table_cell *cell,
     return -1;
 
   bb[H][0] = 0;
-  bb[H][1] = width - px_to_xr (cell->style->margin[H][0]
-                               + cell->style->margin[H][1]);
+  bb[H][1] = width - px_to_xr (cell->style->cell_style.margin[H][0]
+                               + cell->style->cell_style.margin[H][1]);
   if (bb[H][1] <= 0)
     return 0;
   bb[V][0] = 0;
-  bb[V][1] = height - px_to_xr (cell->style->margin[V][0]
-                                + cell->style->margin[V][1]);
+  bb[V][1] = height - px_to_xr (cell->style->cell_style.margin[V][0]
+                                + cell->style->cell_style.margin[V][1]);
   clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
   xr_layout_cell (xr, cell, bb, clip, &w, &h, &brk);
   return brk;
@@ -1201,25 +1450,36 @@ markup_escape (const char *in, struct string *out)
       }
 }
 
+static int
+get_layout_dimension (PangoLayout *layout, enum table_axis axis)
+{
+  int size[TABLE_N_AXES];
+  pango_layout_get_size (layout, &size[H], &size[V]);
+  return size[axis];
+}
+
 static int
 xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
                      int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
                      int *widthp, int *brk)
 {
-  const struct cell_style *style = cell->style;
+  const struct font_style *font_style = &cell->style->font_style;
+  const struct cell_style *cell_style = &cell->style->cell_style;
   unsigned int options = cell->options;
-  int w, h;
+
+  enum table_axis X = options & TAB_ROTATE ? V : H;
+  enum table_axis Y = !X;
+  int R = options & TAB_ROTATE ? 0 : 1;
 
   struct xr_font *font = (options & TAB_FIX ? &xr->fonts[XR_FONT_FIXED]
-                          : options & TAB_EMPH ? &xr->fonts[XR_FONT_EMPHASIS]
                           : &xr->fonts[XR_FONT_PROPORTIONAL]);
   struct xr_font local_font;
-  if (cell->style->font)
+  if (font_style->typeface)
     {
       PangoFontDescription *desc = parse_font (
-        style->font,
-        style->font_size ? style->font_size * 1000 * 72 / 128 : 10000,
-        style->bold, style->italic);
+        font_style->typeface,
+        font_style->size ? font_style->size * 1000 * 72 / 128 : 10000,
+        font_style->bold, font_style->italic);
       if (desc)
         {
           PangoLayout *layout = pango_cairo_create_layout (xr->cairo);
@@ -1231,29 +1491,26 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
         }
     }
 
-  int footnote_adjustment;
-  if (cell->n_footnotes == 0)
-    footnote_adjustment = 0;
-  else if (cell->n_footnotes == 1 && (options & TAB_HALIGN) == TAB_RIGHT)
+  const char *text = cell->text;
+  enum table_halign halign = table_halign_interpret (
+    cell_style->halign, cell->options & TAB_NUMERIC);
+  if (cell_style->halign == TABLE_HALIGN_DECIMAL && !(options & TAB_ROTATE))
     {
-      PangoAttrList *attrs;
-
-      const char *marker = cell->footnotes[0]->marker;
-      pango_layout_set_text (font->layout, marker, strlen (marker));
+      int margin_adjustment = -px_to_xr (cell_style->decimal_offset);
 
-      attrs = pango_attr_list_new ();
-      pango_attr_list_insert (attrs, pango_attr_rise_new (7000));
-      pango_layout_set_attributes (font->layout, attrs);
-      pango_attr_list_unref (attrs);
+      const char *decimal = strrchr (text, cell_style->decimal_char);
+      if (decimal)
+        {
+          pango_layout_set_text (font->layout, decimal, strlen (decimal));
+          pango_layout_set_width (font->layout, -1);
+          margin_adjustment += get_layout_dimension (font->layout, H);
+        }
 
-      pango_layout_get_size (font->layout, &w, &h);
-      footnote_adjustment = MIN (w, px_to_xr (style->margin[H][1]));
+      if (margin_adjustment < 0)
+        bb[H][1] += margin_adjustment;
     }
-  else
-    footnote_adjustment = px_to_xr (style->margin[H][1]);
 
   struct string tmp = DS_EMPTY_INITIALIZER;
-  const char *text = cell->text;
 
   /* Deal with an oddity of the Unicode line-breaking algorithm (or perhaps in
      Pango's implementation of it): it will break after a period or a comma
@@ -1269,7 +1526,7 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
      happen with grouping like 1,234,567.89 or 1.234.567,89 because if groups
      are present then there will always be a digit on both sides of every
      period and comma. */
-  if (bb[H][1] != INT_MAX)
+  if (options & TAB_ROTATE || bb[H][1] != INT_MAX)
     {
       const char *decimal = text + strcspn (text, ".,");
       if (decimal[0]
@@ -1283,9 +1540,33 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
         }
     }
 
-  if (footnote_adjustment)
+  if (cell->n_footnotes)
     {
-      bb[H][1] += footnote_adjustment;
+      int footnote_adjustment;
+      if (cell->n_footnotes == 1 && halign == TABLE_HALIGN_RIGHT)
+        {
+          const char *marker = cell->footnotes[0]->marker;
+          pango_layout_set_text (font->layout, marker, strlen (marker));
+
+          PangoAttrList *attrs = pango_attr_list_new ();
+          pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_SMALL));
+          pango_attr_list_insert (attrs, pango_attr_rise_new (3000));
+          pango_layout_set_attributes (font->layout, attrs);
+          pango_attr_list_unref (attrs);
+
+          int w = get_layout_dimension (font->layout, X);
+          int right_margin = px_to_xr (cell_style->margin[X][R]);
+          footnote_adjustment = MIN (w, right_margin);
+
+          pango_layout_set_attributes (font->layout, NULL);
+        }
+      else
+        footnote_adjustment = px_to_xr (cell_style->margin[X][R]);
+
+      if (R)
+        bb[X][R] += footnote_adjustment;
+      else
+        bb[X][R] -= footnote_adjustment;
 
       if (ds_is_empty (&tmp))
         {
@@ -1313,10 +1594,11 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
         pango_layout_set_text (font->layout, ds_cstr (&tmp), ds_length (&tmp));
 
       PangoAttrList *attrs = pango_attr_list_new ();
-      if (style->underline)
+      if (font_style->underline)
         pango_attr_list_insert (attrs, pango_attr_underline_new (
                                PANGO_UNDERLINE_SINGLE));
-      add_attr_with_start (attrs, pango_attr_rise_new (7000), initial_length);
+      add_attr_with_start (attrs, pango_attr_scale_new (PANGO_SCALE_SMALL), initial_length);
+      add_attr_with_start (attrs, pango_attr_rise_new (3000), initial_length);
       add_attr_with_start (
         attrs, pango_attr_font_desc_new (font->desc), initial_length);
       pango_layout_set_attributes (font->layout, attrs);
@@ -1330,7 +1612,7 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
       else
         pango_layout_set_text (font->layout, content, -1);
 
-      if (style->underline)
+      if (font_style->underline)
         {
           PangoAttrList *attrs = pango_attr_list_new ();
           pango_attr_list_insert (attrs, pango_attr_underline_new (
@@ -1341,23 +1623,31 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
     }
   ds_destroy (&tmp);
 
-  pango_layout_set_alignment (
-    font->layout,
-    ((options & TAB_HALIGN) == TAB_RIGHT ? PANGO_ALIGN_RIGHT
-     : (options & TAB_HALIGN) == TAB_LEFT ? PANGO_ALIGN_LEFT
-     : PANGO_ALIGN_CENTER));
+  pango_layout_set_alignment (font->layout,
+                              (halign == TABLE_HALIGN_RIGHT ? PANGO_ALIGN_RIGHT
+                               : halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
+                               : PANGO_ALIGN_CENTER));
   pango_layout_set_width (
     font->layout,
-    bb[H][1] == INT_MAX ? -1 : xr_to_pango (bb[H][1] - bb[H][0]));
+    bb[X][1] == INT_MAX ? -1 : xr_to_pango (bb[X][1] - bb[X][0]));
   pango_layout_set_wrap (font->layout, PANGO_WRAP_WORD);
 
   if (clip[H][0] != clip[H][1])
     {
       cairo_save (xr->cairo);
-      xr_clip (xr, clip);
-      cairo_translate (xr->cairo,
-                       xr_to_pt (bb[H][0] + xr->x),
-                       xr_to_pt (bb[V][0] + xr->y));
+      if (!(options & TAB_ROTATE))
+        xr_clip (xr, clip);
+      if (options & TAB_ROTATE)
+        {
+          cairo_translate (xr->cairo,
+                           xr_to_pt (bb[H][0] + xr->x),
+                           xr_to_pt (bb[V][1] + xr->y));
+          cairo_rotate (xr->cairo, -M_PI_2);
+        }
+      else
+        cairo_translate (xr->cairo,
+                         xr_to_pt (bb[H][0] + xr->x),
+                         xr_to_pt (bb[V][0] + xr->y));
       pango_cairo_show_layout (xr->cairo, font->layout);
 
       /* If enabled, this draws a blue rectangle around the extents of each
@@ -1388,12 +1678,13 @@ xr_layout_cell_text (struct xr_driver *xr, const struct table_cell *cell,
       cairo_restore (xr->cairo);
     }
 
-  pango_layout_get_size (font->layout, &w, &h);
-  w = pango_to_xr (w);
-  h = pango_to_xr (h);
+  int size[TABLE_N_AXES];
+  pango_layout_get_size (font->layout, &size[H], &size[V]);
+  int w = pango_to_xr (size[X]);
+  int h = pango_to_xr (size[Y]);
   if (w > *widthp)
     *widthp = w;
-  if (bb[V][0] + h >= bb[V][1])
+  if (bb[V][0] + h >= bb[V][1] && !(options & TAB_ROTATE))
     {
       PangoLayoutIter *iter;
       int best UNUSED = 0;
@@ -1533,12 +1824,11 @@ xr_driver_destroy (struct xr_driver *xr)
 static struct xr_rendering *
 xr_rendering_create_text (struct xr_driver *xr, const char *text, cairo_t *cr)
 {
-  struct table_item *table_item;
-  struct xr_rendering *r;
-
-  table_item = table_item_create (table_from_string (TAB_LEFT, text),
-                                  NULL, NULL);
-  r = xr_rendering_create (xr, &table_item->output_item, cr);
+  struct pivot_table *pt = pivot_table_create_for_text (
+    NULL, pivot_value_new_user_text (text, -1));
+  struct table_item *table_item = table_item_create (pt);
+  struct xr_rendering *r = xr_rendering_create (xr, &table_item->output_item,
+                                                cr);
   table_item_unref (table_item);
 
   return r;
@@ -1599,18 +1889,25 @@ xr_rendering_destroy (struct xr_rendering *r)
 }
 
 void
-xr_rendering_measure (struct xr_rendering *r, int *w, int *h)
+xr_rendering_measure (struct xr_rendering *r, int *wp, int *hp)
 {
+  int w, h;
+
   if (is_table_item (r->item))
     {
-      *w = render_pager_get_size (r->p, H) / XR_POINT;
-      *h = render_pager_get_size (r->p, V) / XR_POINT;
+      w = render_pager_get_size (r->p, H) / XR_POINT;
+      h = render_pager_get_size (r->p, V) / XR_POINT;
     }
   else
     {
-      *w = CHART_WIDTH;
-      *h = CHART_HEIGHT;
+      w = CHART_WIDTH;
+      h = CHART_HEIGHT;
     }
+
+  if (wp)
+    *wp = w;
+  if (hp)
+    *hp = h;
 }
 
 static void xr_draw_chart (const struct chart_item *, cairo_t *,
@@ -1673,9 +1970,8 @@ xr_draw_chart (const struct chart_item *chart_item, cairo_t *cr,
 char *
 xr_draw_png_chart (const struct chart_item *item,
                    const char *file_name_template, int number,
-                  const struct xr_color *fg,
-                  const struct xr_color *bg
-                  )
+                  const struct cell_color *fg,
+                  const struct cell_color *bg)
 {
   const int width = 640;
   const int length = 480;
@@ -1696,10 +1992,10 @@ xr_draw_png_chart (const struct chart_item *item,
   surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
   cr = cairo_create (surface);
 
-  cairo_set_source_rgb (cr, bg->red, bg->green, bg->blue);
+  cairo_set_source_rgb (cr, bg->r / 255.0, bg->g / 255.0, bg->b / 255.0);
   cairo_paint (cr);
 
-  cairo_set_source_rgb (cr, fg->red, fg->green, fg->blue);
+  cairo_set_source_rgb (cr, fg->r / 255.0, fg->g / 255.0, fg->b / 255.0);
 
   xr_draw_chart (item, cr, 0.0, 0.0, width, length);
 
@@ -1855,11 +2151,6 @@ xr_render_text (struct xr_driver *xr, const struct text_item *text_item)
       string_map_replace (&xr->heading_vars, "PageTitle", text);
       break;
 
-    case TEXT_ITEM_BLANK_LINE:
-      if (xr->y > 0)
-        xr->y += xr->char_height;
-      break;
-
     case TEXT_ITEM_EJECT_PAGE:
       if (xr->y > 0)
         return xr_render_eject ();
@@ -1878,7 +2169,7 @@ xr_render_message (struct xr_driver *xr,
                    const struct message_item *message_item)
 {
   char *s = msg_to_string (message_item_get_msg (message_item));
-  struct text_item *item = text_item_create (TEXT_ITEM_PARAGRAPH, s);
+  struct text_item *item = text_item_create (TEXT_ITEM_LOG, s);
   free (s);
   return xr_render_table (xr, text_item_to_table_item (item));
 }