- if (xr->fsm != NULL && !xr->fsm->render (xr->fsm, xr))
- xr_driver_destroy_fsm (xr);
-}
-\f
-static void
-xr_layout_cell (struct xr_driver *, const struct table_cell *, int footnote_idx,
- int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
- int *width, int *height, int *brk);
-
-static void
-dump_line (struct xr_driver *xr, int x0, int y0, int x1, int y1)
-{
- cairo_new_path (xr->cairo);
- cairo_move_to (xr->cairo, xr_to_pt (x0 + xr->x), xr_to_pt (y0 + xr->y));
- cairo_line_to (xr->cairo, xr_to_pt (x1 + xr->x), xr_to_pt (y1 + xr->y));
- cairo_stroke (xr->cairo);
-}
-
-static void UNUSED
-dump_rectangle (struct xr_driver *xr, int x0, int y0, int x1, int y1)
-{
- cairo_new_path (xr->cairo);
- cairo_move_to (xr->cairo, xr_to_pt (x0 + xr->x), xr_to_pt (y0 + xr->y));
- cairo_line_to (xr->cairo, xr_to_pt (x1 + xr->x), xr_to_pt (y0 + xr->y));
- cairo_line_to (xr->cairo, xr_to_pt (x1 + xr->x), xr_to_pt (y1 + xr->y));
- cairo_line_to (xr->cairo, xr_to_pt (x0 + xr->x), xr_to_pt (y1 + xr->y));
- cairo_close_path (xr->cairo);
- cairo_stroke (xr->cairo);
-}
-
-/* Draws a horizontal line X0...X2 at Y if LEFT says so,
- shortening it to X0...X1 if SHORTEN is true.
- Draws a horizontal line X1...X3 at Y if RIGHT says so,
- shortening it to X2...X3 if SHORTEN is true. */
-static void
-horz_line (struct xr_driver *xr, int x0, int x1, int x2, int x3, int y,
- enum render_line_style left, enum render_line_style right,
- bool shorten)
-{
- if (left != RENDER_LINE_NONE && right != RENDER_LINE_NONE && !shorten)
- dump_line (xr, x0, y, x3, y);
- else
- {
- if (left != RENDER_LINE_NONE)
- dump_line (xr, x0, y, shorten ? x1 : x2, y);
- if (right != RENDER_LINE_NONE)
- dump_line (xr, shorten ? x2 : x1, y, x3, y);
- }
-}
-
-/* Draws a vertical line Y0...Y2 at X if TOP says so,
- shortening it to Y0...Y1 if SHORTEN is true.
- Draws a vertical line Y1...Y3 at X if BOTTOM says so,
- shortening it to Y2...Y3 if SHORTEN is true. */
-static void
-vert_line (struct xr_driver *xr, int y0, int y1, int y2, int y3, int x,
- enum render_line_style top, enum render_line_style bottom,
- bool shorten)
-{
- if (top != RENDER_LINE_NONE && bottom != RENDER_LINE_NONE && !shorten)
- dump_line (xr, x, y0, x, y3);
- else
- {
- if (top != RENDER_LINE_NONE)
- dump_line (xr, x, y0, x, shorten ? y1 : y2);
- if (bottom != RENDER_LINE_NONE)
- dump_line (xr, x, shorten ? y2 : y1, x, y3);
- }
-}
-
-static void
-xr_draw_line (void *xr_, int bb[TABLE_N_AXES][2],
- enum render_line_style styles[TABLE_N_AXES][2])
-{
- const int x0 = bb[H][0];
- const int y0 = bb[V][0];
- const int x3 = bb[H][1];
- const int y3 = bb[V][1];
- const int top = styles[H][0];
- const int left = styles[V][0];
- const int bottom = styles[H][1];
- const int right = styles[V][1];
-
- /* The algorithm here is somewhat subtle, to allow it to handle
- all the kinds of intersections that we need.
-
- Three additional ordinates are assigned along the x axis. The
- first is xc, midway between x0 and x3. The others are x1 and
- x2; for a single vertical line these are equal to xc, and for
- a double vertical line they are the ordinates of the left and
- right half of the double line.
-
- yc, y1, and y2 are assigned similarly along the y axis.
-
- The following diagram shows the coordinate system and output
- for double top and bottom lines, single left line, and no
- right line:
-
- x0 x1 xc x2 x3
- y0 ________________________
- | # # |
- | # # |
- | # # |
- | # # |
- | # # |
- y1 = y2 = yc |######### # |
- | # # |
- | # # |
- | # # |
- | # # |
- y3 |________#_____#_______|
- */
- struct xr_driver *xr = xr_;
-
- /* Offset from center of each line in a pair of double lines. */
- int double_line_ofs = (xr->line_space + xr->line_width) / 2;
-
- /* Are the lines along each axis single or double?
- (It doesn't make sense to have different kinds of line on the
- same axis, so we don't try to gracefully handle that case.) */
- bool double_vert = top == RENDER_LINE_DOUBLE || bottom == RENDER_LINE_DOUBLE;
- bool double_horz = left == RENDER_LINE_DOUBLE || right == RENDER_LINE_DOUBLE;
-
- /* When horizontal lines are doubled,
- the left-side line along y1 normally runs from x0 to x2,
- and the right-side line along y1 from x3 to x1.
- If the top-side line is also doubled, we shorten the y1 lines,
- so that the left-side line runs only to x1,
- and the right-side line only to x2.
- Otherwise, the horizontal line at y = y1 below would cut off
- the intersection, which looks ugly:
- x0 x1 x2 x3
- y0 ________________________
- | # # |
- | # # |
- | # # |
- | # # |
- y1 |######### ########|
- | |
- | |
- y2 |######################|
- | |
- | |
- y3 |______________________|
- It is more of a judgment call when the horizontal line is
- single. We actually choose to cut off the line anyhow, as
- shown in the first diagram above.
- */
- bool shorten_y1_lines = top == RENDER_LINE_DOUBLE;
- bool shorten_y2_lines = bottom == RENDER_LINE_DOUBLE;
- bool shorten_yc_line = shorten_y1_lines && shorten_y2_lines;
- int horz_line_ofs = double_vert ? double_line_ofs : 0;
- int xc = (x0 + x3) / 2;
- int x1 = xc - horz_line_ofs;
- int x2 = xc + horz_line_ofs;
-
- bool shorten_x1_lines = left == RENDER_LINE_DOUBLE;
- bool shorten_x2_lines = right == RENDER_LINE_DOUBLE;
- bool shorten_xc_line = shorten_x1_lines && shorten_x2_lines;
- int vert_line_ofs = double_horz ? double_line_ofs : 0;
- int yc = (y0 + y3) / 2;
- int y1 = yc - vert_line_ofs;
- int y2 = yc + vert_line_ofs;
-
- if (!double_horz)
- horz_line (xr, x0, x1, x2, x3, yc, left, right, shorten_yc_line);
- else
- {
- horz_line (xr, x0, x1, x2, x3, y1, left, right, shorten_y1_lines);
- horz_line (xr, x0, x1, x2, x3, y2, left, right, shorten_y2_lines);
- }
-
- if (!double_vert)
- vert_line (xr, y0, y1, y2, y3, xc, top, bottom, shorten_xc_line);
- else
- {
- vert_line (xr, y0, y1, y2, y3, x1, top, bottom, shorten_x1_lines);
- vert_line (xr, y0, y1, y2, y3, x2, top, bottom, shorten_x2_lines);
- }
-}
-
-static void
-xr_measure_cell_width (void *xr_, const struct table_cell *cell,
- int footnote_idx, int *min_width, int *max_width)
-{
- struct xr_driver *xr = xr_;
- int bb[TABLE_N_AXES][2];
- int clip[TABLE_N_AXES][2];
- int h;
-
- bb[H][0] = 0;
- bb[H][1] = INT_MAX;
- 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, footnote_idx, bb, clip, max_width, &h, NULL);
-
- bb[H][1] = 1;
- xr_layout_cell (xr, cell, footnote_idx, bb, clip, min_width, &h, NULL);
-
- if (*min_width > 0)
- *min_width += xr->cell_margin * 2;
- if (*max_width > 0)
- *max_width += xr->cell_margin * 2;
-}
-
-static int
-xr_measure_cell_height (void *xr_, const struct table_cell *cell,
- int footnote_idx, int width)
-{
- struct xr_driver *xr = xr_;
- int bb[TABLE_N_AXES][2];
- int clip[TABLE_N_AXES][2];
- int w, h;
-
- bb[H][0] = 0;
- bb[H][1] = width - xr->cell_margin * 2;
- if (bb[H][1] <= 0)
- return 0;
- 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, footnote_idx, bb, clip, &w, &h, NULL);
- return h;
-}
-
-static void
-xr_draw_cell (void *xr_, const struct table_cell *cell, int footnote_idx,
- int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2])
-{
- struct xr_driver *xr = xr_;
- int w, h, brk;
-
- bb[H][0] += xr->cell_margin;
- bb[H][1] -= xr->cell_margin;
- if (bb[H][0] >= bb[H][1])
- return;
- xr_layout_cell (xr, cell, footnote_idx, bb, clip, &w, &h, &brk);
-}
-
-static int
-xr_adjust_break (void *xr_, const struct table_cell *cell, int footnote_idx,
- int width, int height)
-{
- struct xr_driver *xr = xr_;
- int bb[TABLE_N_AXES][2];
- int clip[TABLE_N_AXES][2];
- int w, h, brk;
-
- if (xr_measure_cell_height (xr_, cell, footnote_idx, width) < height)
- return -1;
-
- bb[H][0] = 0;
- bb[H][1] = width - 2 * xr->cell_margin;
- if (bb[H][1] <= 0)
- return 0;
- bb[V][0] = 0;
- bb[V][1] = height;
- clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
- xr_layout_cell (xr, cell, footnote_idx, bb, clip, &w, &h, &brk);
- return brk;
-}
-\f
-static void
-xr_clip (struct xr_driver *xr, int clip[TABLE_N_AXES][2])
-{
- if (clip[H][1] != INT_MAX || clip[V][1] != INT_MAX)
- {
- double x0 = xr_to_pt (clip[H][0] + xr->x);
- double y0 = xr_to_pt (clip[V][0] + xr->y);
- double x1 = xr_to_pt (clip[H][1] + xr->x);
- double y1 = xr_to_pt (clip[V][1] + xr->y);
-
- cairo_rectangle (xr->cairo, x0, y0, x1 - x0, y1 - y0);
- cairo_clip (xr->cairo);
- }
-}
-
-static void
-add_attr_with_start (PangoAttrList *list, PangoAttribute *attr, guint start_index)
-{
- attr->start_index = start_index;
- pango_attr_list_insert (list, attr);
-}
-
-static int
-xr_layout_cell_text (struct xr_driver *xr,
- const struct cell_contents *contents, int footnote_idx,
- int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
- int y, int *widthp, int *brk)
-{
- unsigned int options = contents->options;
- struct xr_font *font;
- bool merge_footnotes;
- size_t length;
- int w, h;
-
- if (contents->n_footnotes == 0)
- merge_footnotes = false;
- else if (contents->n_footnotes == 1 && (options & TAB_ALIGNMENT) == TAB_RIGHT)
- {
- PangoAttrList *attrs;
- char marker[16];
-
- font = &xr->fonts[XR_FONT_MARKER];
-
- str_format_26adic (footnote_idx + 1, false, marker, sizeof marker);
- pango_layout_set_text (font->layout, marker, strlen (marker));
-
- 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);
-
- pango_layout_get_size (font->layout, &w, &h);
- merge_footnotes = w > xr->cell_margin;
- if (!merge_footnotes && clip[H][0] != clip[H][1])
- {
- cairo_save (xr->cairo);
- xr_clip (xr, clip);
- cairo_translate (xr->cairo,
- xr_to_pt (bb[H][1] + xr->x),
- xr_to_pt (y + xr->y));
- pango_layout_set_alignment (font->layout, PANGO_ALIGN_LEFT);
- pango_layout_set_width (font->layout, -1);
- pango_cairo_show_layout (xr->cairo, font->layout);
- cairo_restore (xr->cairo);
- }
-
- pango_layout_set_attributes (font->layout, NULL);
- }
- else
- merge_footnotes = true;
-
- font = (options & TAB_FIX ? &xr->fonts[XR_FONT_FIXED]
- : options & TAB_EMPH ? &xr->fonts[XR_FONT_EMPHASIS]
- : &xr->fonts[XR_FONT_PROPORTIONAL]);
-
- length = strlen (contents->text);
- if (merge_footnotes)
- {
- PangoAttrList *attrs;
- struct string s;
- size_t i;
-
- bb[H][1] += xr->cell_margin;
-
- ds_init_empty (&s);
- ds_extend (&s, length + contents->n_footnotes * 10);
- ds_put_cstr (&s, contents->text);
- for (i = 0; i < contents->n_footnotes; i++)
- {
- char marker[16];
-
- if (i > 0)
- ds_put_byte (&s, ',');
- str_format_26adic (footnote_idx + i + 1, false, marker, sizeof marker);
- ds_put_cstr (&s, marker);
- }
- pango_layout_set_text (font->layout, ds_cstr (&s), ds_length (&s));
- ds_destroy (&s);
-
- attrs = pango_attr_list_new ();
- add_attr_with_start (attrs, pango_attr_rise_new (7000), length);
- add_attr_with_start (
- attrs, pango_attr_font_desc_new (xr->fonts[XR_FONT_MARKER].desc), length);
- pango_layout_set_attributes (font->layout, attrs);
- pango_attr_list_unref (attrs);
- }
- else
- pango_layout_set_text (font->layout, contents->text, -1);
-
- pango_layout_set_alignment (
- font->layout,
- ((options & TAB_ALIGNMENT) == TAB_RIGHT ? PANGO_ALIGN_RIGHT
- : (options & TAB_ALIGNMENT) == TAB_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]));
- pango_layout_set_wrap (font->layout, PANGO_WRAP_WORD);
-
- if (clip[H][0] != clip[H][1])