Print: better calculation of the row height in table print

Instead of odd hard coded offsets we evaluate the height of a single line and
modify the height per row (and the vertical positioning of the text within that
row) by a fraction of that height.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-02-16 23:01:54 -08:00
parent f65afb9800
commit f0ff972d91

33
print.c
View file

@ -504,11 +504,11 @@ static void show_table_header(cairo_t *cr, PangoLayout *layout, double w)
} }
static int show_table_cell(int i, cairo_t *cr, PangoLayout *layout, double colwidth, int height_count, static int show_table_cell(int i, cairo_t *cr, PangoLayout *layout, double colwidth, int height_count,
char *buffer, int len, double *curwidth) char *buffer, int len, double *curwidth, double y)
{ {
PangoRectangle logic_ext; PangoRectangle logic_ext;
double inner_colwidth = 0.95 * colwidth; double inner_colwidth = 0.95 * colwidth;
cairo_move_to(cr, *curwidth / PANGO_SCALE, 0); cairo_move_to(cr, *curwidth / PANGO_SCALE, y);
pango_layout_set_width(layout, inner_colwidth * rel_width[i]); pango_layout_set_width(layout, inner_colwidth * rel_width[i]);
pango_layout_set_text(layout, buffer, len); pango_layout_set_text(layout, buffer, len);
pango_layout_set_justify(layout, 0); pango_layout_set_justify(layout, 0);
@ -520,7 +520,7 @@ static int show_table_cell(int i, cairo_t *cr, PangoLayout *layout, double colwi
return height_count; return height_count;
} }
static int show_dive_table(struct dive *dive, cairo_t *cr, PangoLayout *layout, double w) static int show_dive_table(struct dive *dive, cairo_t *cr, PangoLayout *layout, double w, double dy)
{ {
double depth; double depth;
const char *unit; const char *unit;
@ -532,14 +532,14 @@ static int show_dive_table(struct dive *dive, cairo_t *cr, PangoLayout *layout,
struct tm tm; struct tm tm;
char buffer[300]; char buffer[300];
cairo_move_to(cr, 0, 0); cairo_move_to(cr, 0, dy);
// Col 1: Dive # (1/3 of the regular width) // Col 1: Dive # (1/3 of the regular width)
*buffer = 0; *buffer = 0;
len = 0; len = 0;
if (dive->number) if (dive->number)
len = snprintf(buffer, sizeof(buffer), "#%d", dive->number); len = snprintf(buffer, sizeof(buffer), "#%d", dive->number);
height_count = show_table_cell(0, cr, layout, colwidth, height_count, buffer, len, &curwidth); height_count = show_table_cell(0, cr, layout, colwidth, height_count, buffer, len, &curwidth, dy);
// Col 2: Date # (1 + 1/6 of the regular width) // Col 2: Date # (1 + 1/6 of the regular width)
utc_mkdate(dive->when, &tm); utc_mkdate(dive->when, &tm);
@ -551,27 +551,27 @@ static int show_dive_table(struct dive *dive, cairo_t *cr, PangoLayout *layout,
tm.tm_mday, tm.tm_year + 1900, tm.tm_mday, tm.tm_year + 1900,
tm.tm_hour, tm.tm_min tm.tm_hour, tm.tm_min
); );
height_count = show_table_cell(1, cr, layout, colwidth, height_count, buffer, len, &curwidth); height_count = show_table_cell(1, cr, layout, colwidth, height_count, buffer, len, &curwidth, dy);
// Col 3: Depth (1/2 width) // Col 3: Depth (1/2 width)
depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit); depth = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
len = snprintf(buffer, sizeof(buffer), len = snprintf(buffer, sizeof(buffer),
"%.*f %s", decimals, depth, unit); "%.*f %s", decimals, depth, unit);
height_count = show_table_cell(2, cr, layout, colwidth, height_count, buffer, len, &curwidth); height_count = show_table_cell(2, cr, layout, colwidth, height_count, buffer, len, &curwidth, dy);
// Col 4: Duration (1/2 width) // Col 4: Duration (1/2 width)
len = snprintf(buffer, sizeof(buffer), len = snprintf(buffer, sizeof(buffer),
_("%d min"),(dive->duration.seconds + 59) / 60); _("%d min"),(dive->duration.seconds + 59) / 60);
height_count = show_table_cell(3, cr, layout, colwidth, height_count, buffer, len, &curwidth); height_count = show_table_cell(3, cr, layout, colwidth, height_count, buffer, len, &curwidth, dy);
// Col 5: Master // Col 5: Master
height_count = show_table_cell(4, cr, layout, colwidth, height_count, dive->divemaster ? : "", -1, &curwidth); height_count = show_table_cell(4, cr, layout, colwidth, height_count, dive->divemaster ? : "", -1, &curwidth, dy);
// Col 6: Buddy // Col 6: Buddy
height_count = show_table_cell(5, cr, layout, colwidth, height_count, dive->buddy ? : "", -1, &curwidth); height_count = show_table_cell(5, cr, layout, colwidth, height_count, dive->buddy ? : "", -1, &curwidth, dy);
// Col 7: Location // Col 7: Location
height_count = show_table_cell(6, cr, layout, colwidth, height_count, dive->location ? : "", -1, &curwidth); height_count = show_table_cell(6, cr, layout, colwidth, height_count, dive->location ? : "", -1, &curwidth, dy);
/* Return the biggest column height, will be used to plot the frame and /* Return the biggest column height, will be used to plot the frame and
* and translate the next row */ * and translate the next row */
@ -739,7 +739,7 @@ static void draw_table(GtkPrintOperation *operation,
int i, nr, maxwidth, maxheight; int i, nr, maxwidth, maxheight;
int n_dive_per_page = 35; int n_dive_per_page = 35;
cairo_t *cr; cairo_t *cr;
double w, h, max_ext; double w, h, max_ext, delta_y;
struct dive *dive; struct dive *dive;
PangoFontDescription *font; PangoFontDescription *font;
PangoLayout *layout; PangoLayout *layout;
@ -773,18 +773,19 @@ static void draw_table(GtkPrintOperation *operation,
/* Get the height for an only line but move two down */ /* Get the height for an only line but move two down */
pango_layout_get_extents(layout, NULL, &logic_ext); pango_layout_get_extents(layout, NULL, &logic_ext);
cairo_translate (cr, 0, (2 * logic_ext.height) / (double) PANGO_SCALE); delta_y = logic_ext.height;
cairo_translate (cr, 0, 2 * delta_y / (double) PANGO_SCALE);
for (i = 0; i < n_dive_per_page; i++) { for (i = 0; i < n_dive_per_page; i++) {
dive = get_dive_for_printing(nr+i); dive = get_dive_for_printing(nr+i);
if (!dive) if (!dive)
return; return;
/* Write the dive data and get the max. height of the row */ /* Write the dive data and get the max. height of the row */
max_ext = show_dive_table(dive, cr, layout, w*2); max_ext = show_dive_table(dive, cr, layout, w*2, delta_y / 4 / PANGO_SCALE);
/* Draw a frame for each row */ /* Draw a frame for each row */
print_table_frame (cr, -0.05, 0, w, (max_ext + 5) / PANGO_SCALE); print_table_frame (cr, -0.05, 0, w, (max_ext + delta_y / 2) / PANGO_SCALE);
/* and move down by the max. height of it */ /* and move down by the max. height of it */
cairo_translate (cr, 0, (max_ext + 5) / PANGO_SCALE); cairo_translate (cr, 0, (max_ext + delta_y / 2) / PANGO_SCALE);
} }
cairo_restore (cr); cairo_restore (cr);
pango_font_description_free(font); pango_font_description_free(font);