Redo the print table layout

This appears to make the table layout much easier to follow, much easier
to modify and it has the edit benefit of working in my limited testing.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-02-16 17:35:01 -08:00
parent bb44e108a3
commit ce802ffdfc

146
print.c
View file

@ -12,6 +12,8 @@
#define FONT_SMALL (FONT_NORMAL / 1.2) #define FONT_SMALL (FONT_NORMAL / 1.2)
#define FONT_LARGE (FONT_NORMAL * 1.2) #define FONT_LARGE (FONT_NORMAL * 1.2)
static double rel_width[] = { 0.333, 1.166, 0.5, 0.5, 1.0, 1.0, 1.8 };
static struct options print_options; static struct options print_options;
typedef struct _Print_params { typedef struct _Print_params {
@ -118,7 +120,7 @@ static void show_dive_header(struct dive *dive, cairo_t *cr, double w,
* width to the same width as the date string. * width to the same width as the date string.
*/ */
pango_layout_get_extents (layout, &ink_ext, &logic_ext); pango_layout_get_extents (layout, &ink_ext, &logic_ext);
cairo_translate (cr, 0, ink_ext.height /(1.5 * (double) PANGO_SCALE)); cairo_translate (cr, 0, ink_ext.height /(1.5 * PANGO_SCALE));
pango_layout_set_height(layout, 1); pango_layout_set_height(layout, 1);
pango_layout_set_width(layout, width); pango_layout_set_width(layout, width);
set_font(layout, font, FONT_LARGE*(1.5/w_scale_factor), PANGO_ALIGN_LEFT); set_font(layout, font, FONT_LARGE*(1.5/w_scale_factor), PANGO_ALIGN_LEFT);
@ -492,14 +494,8 @@ static void show_table_header(cairo_t *cr, PangoLayout *layout, double w)
curwidth = 0; curwidth = 0;
for (i = 0; i < 7; i++) { for (i = 0; i < 7; i++) {
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); cairo_move_to(cr, curwidth / PANGO_SCALE, 0);
if (i == 0 || i == 2 || i == 3){ pango_layout_set_width(layout, colwidth * rel_width[i]);
// Column 0, 2 and 3 (Dive #, Depth and Time) get 1/2 width curwidth = curwidth + (colwidth * rel_width[i]);
pango_layout_set_width(layout, colwidth/ (double) 2);
curwidth = curwidth + (colwidth / 2);
} else {
pango_layout_set_width(layout, colwidth);
curwidth = curwidth + colwidth;
}
pango_layout_set_text(layout, _(headers[i]), -1); pango_layout_set_text(layout, _(headers[i]), -1);
pango_layout_set_justify(layout, 0); pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout); pango_cairo_show_layout(cr, layout);
@ -507,38 +503,45 @@ static void show_table_header(cairo_t *cr, PangoLayout *layout, double w)
cairo_move_to(cr, 0, 0); cairo_move_to(cr, 0, 0);
} }
static int show_table_cell(int i, cairo_t *cr, PangoLayout *layout, double colwidth, int height_count,
char *buffer, int len, double *curwidth)
{
PangoRectangle logic_ext;
double inner_colwidth = 0.95 * colwidth;
cairo_move_to(cr, *curwidth / PANGO_SCALE, 0);
pango_layout_set_width(layout, inner_colwidth * rel_width[i]);
pango_layout_set_text(layout, buffer, len);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
*curwidth += colwidth * rel_width[i];
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
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 depth; double depth;
const char *unit; const char *unit;
int len, decimals; int len, decimals;
double maxwidth, colwidth, curwidth; double maxwidth = w * PANGO_SCALE;
double colwidth = maxwidth / 7;
double curwidth = 0;
int height_count = 0;
struct tm tm; struct tm tm;
char buffer[160], divenr[20]; char buffer[300];
PangoRectangle logic_ext;
maxwidth = w * PANGO_SCALE;
colwidth = maxwidth / 7;
cairo_move_to(cr, 0, 0); cairo_move_to(cr, 0, 0);
curwidth = 0;
int height_count = 0;
// Col 1: Dive # // Col 1: Dive # (1/3 of the regular width)
*divenr = 0; *buffer = 0;
len = 0;
if (dive->number) if (dive->number)
snprintf(divenr, sizeof(divenr), "#%d", dive->number); len = snprintf(buffer, sizeof(buffer), "#%d", dive->number);
pango_layout_set_width(layout, colwidth/ (double) 2); height_count = show_table_cell(0, cr, layout, colwidth, height_count, buffer, len, &curwidth);
pango_layout_set_text(layout, divenr, -1);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + (colwidth / 2);
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 2: Date # // Col 2: Date # (1 + 1/6 of the regular width)
pango_layout_set_width(layout, colwidth);
utc_mkdate(dive->when, &tm); utc_mkdate(dive->when, &tm);
len = snprintf(buffer, sizeof(buffer), len = snprintf(buffer, sizeof(buffer),
/*++GETTEXT 160 chars: weekday, monthname, day, year, hour, min */ /*++GETTEXT 160 chars: weekday, monthname, day, year, hour, min */
@ -548,82 +551,30 @@ 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
); );
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); height_count = show_table_cell(1, cr, layout, colwidth, height_count, buffer, len, &curwidth);
pango_layout_set_text(layout, buffer, len);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + colwidth;
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 3: Depth (1/2 width)
// Col 3: Depth
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);
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); height_count = show_table_cell(2, cr, layout, colwidth, height_count, buffer, len, &curwidth);
pango_layout_set_width(layout, colwidth/ (double) 2);
pango_layout_set_text(layout, buffer, len);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + (colwidth / 2);
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 4: Duration (1/2 width)
// Col 4: Time
len = snprintf(buffer, sizeof(buffer), len = snprintf(buffer, sizeof(buffer),
_("%d min"),(dive->duration.seconds + 59) / 60); _("%d min"),(dive->duration.seconds + 59) / 60);
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); height_count = show_table_cell(3, cr, layout, colwidth, height_count, buffer, len, &curwidth);
pango_layout_set_width(layout, colwidth/ (double) 2);
pango_layout_set_text(layout, buffer, len);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + (colwidth / 2);
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 5: Master // Col 5: Master
pango_layout_set_width(layout, colwidth); height_count = show_table_cell(4, cr, layout, colwidth, height_count, dive->divemaster ? : "", -1, &curwidth);
cairo_move_to(cr, curwidth / PANGO_SCALE, 0);
pango_layout_set_text(layout, dive->divemaster ? : " ", -1);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + colwidth;
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 6: Buddy // Col 6: Buddy
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); height_count = show_table_cell(5, cr, layout, colwidth, height_count, dive->buddy ? : "", -1, &curwidth);
pango_layout_set_text(layout, dive->buddy ? : " ", -1);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
curwidth = curwidth + colwidth;
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
// Col 7: Location // Col 7: Location
cairo_move_to(cr, curwidth / PANGO_SCALE, 0); height_count = show_table_cell(6, cr, layout, colwidth, height_count, dive->location ? : "", -1, &curwidth);
pango_layout_set_width(layout, (maxwidth*0.90) - curwidth);
pango_layout_set_text(layout, dive->location ? : " ", -1);
pango_layout_set_justify(layout, 0);
pango_cairo_show_layout(cr, layout);
pango_layout_get_extents(layout, NULL, &logic_ext);
if (logic_ext.height > height_count)
height_count = logic_ext.height;
/* /* Return the biggest column height, will be used to plot the frame and
* Return the bigger column height, will be used to plot the frame and * and translate the next row */
* and translate the next row
*/
return (height_count); return (height_count);
} }
@ -693,12 +644,7 @@ static void print_table_frame(cairo_t *cr, double x, double y, double w, double
cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER); cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
cairo_stroke(cr); cairo_stroke(cr);
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
if (i == 0 || i == 2 || i == 3){ curwidth += maxwidth / 7 * rel_width[i];
// Column 0, 2 and 3 (Dive #, Depth and Time) get 1/2 width
curwidth = curwidth + (maxwidth/14);
} else {
curwidth = curwidth + (maxwidth/7);
}
cairo_move_to(cr, curwidth, y); cairo_move_to(cr, curwidth, y);
cairo_line_to(cr, curwidth, y + h); cairo_line_to(cr, curwidth, y + h);
cairo_set_line_width (cr, 0.01); cairo_set_line_width (cr, 0.01);
@ -836,9 +782,9 @@ static void draw_table(GtkPrintOperation *operation,
/* 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);
/* Draw a frame for each row */ /* Draw a frame for each row */
print_table_frame (cr, -0.05, -0.02, w, max_ext / (double) PANGO_SCALE); print_table_frame (cr, -0.05, 0, w, (max_ext + 5) / 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 / (double) PANGO_SCALE); cairo_translate (cr, 0, (max_ext + 5) / PANGO_SCALE);
} }
cairo_restore (cr); cairo_restore (cr);
pango_font_description_free(font); pango_font_description_free(font);