libhpdftbl 1.5.0
Table construction library for Haru PDF library
Loading...
Searching...
No Matches
tut_ex09.c

Adjusting font style with a callback.

#include "unit_test.inc.h"
#ifndef _MSC_VER
// Silent gcc about unused "arg" in the callback and error functions
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
_Bool
cb_style(void *tag, size_t r, size_t c, char *content, hpdf_text_style_t *style) {
// Format the header row/column with a grey background and Helvetica font while the rest of the
// table uses "Times Roman"
if( 0==r || 0==c ) { // Headers
style->fsize = 12;
style->color = HPDF_COLOR_BLACK;
style->background = HPDF_COLOR_LIGHT_GRAY;
if ( c > 0 )
style->halign = CENTER;
else
style->halign = LEFT;
} else { // Content
style->font = HPDF_FF_TIMES;
style->fsize = 11;
style->color = HPDF_COLOR_BLACK;
style->background = HPDF_COLOR_WHITE;
style->halign = CENTER;
}
return TRUE;
}
static char *
cb_content(void *tag, size_t r, size_t c) {
static char buf[64];
if( 0==r && 0==c ) return NULL;
if( 0==c ) {
#if (defined _WIN32 || defined __WIN32__)
snprintf(buf, sizeof buf, "Extra long Header %2ix%2i", r, c);
#else
snprintf(buf, sizeof buf, "Extra long Header %zux%zu", r, c);
#endif
} else if( 0==r ) {
#if (defined _WIN32 || defined __WIN32__)
snprintf(buf, sizeof buf, "Header %2ix%2i", r, c);
#else
snprintf(buf, sizeof buf, "Header %zux%zu", r, c);
#endif
} else {
#if (defined _WIN32 || defined __WIN32__)
snprintf(buf, sizeof buf, "Content %2ix%2i", r, c);
#else
snprintf(buf, sizeof buf, "Content %zux%zu", r, c);
#endif
}
return buf;
}
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
void
create_table_ex09(HPDF_Doc pdf_doc, HPDF_Page pdf_page) {
const size_t num_rows = 4;
const size_t num_cols = 4;
hpdftbl_t tbl = hpdftbl_create(num_rows, num_cols);
hpdftbl_set_content_cb(tbl, cb_content);
HPDF_REAL xpos = hpdftbl_cm2dpi(1);
HPDF_REAL ypos = hpdftbl_cm2dpi(A4PAGE_HEIGHT_CM - 1);
HPDF_REAL width = hpdftbl_cm2dpi(A4PAGE_WIDTH_CM - 4);
HPDF_REAL height = 0; // Calculate height automatically
// Stroke the table to the page
hpdftbl_stroke(pdf_doc, pdf_page, tbl, xpos, ypos, width, height);
}
TUTEX_MAIN(create_table_ex09, FALSE)
hpdftbl_t hpdftbl_create(size_t rows, size_t cols)
Create a new table with no title.
Definition: hpdftbl.c:311
int hpdftbl_set_colwidth_percent(hpdftbl_t t, size_t c, float w)
Set column width as percentage of overall table width.
Definition: hpdftbl.c:435
int hpdftbl_stroke(HPDF_Doc pdf, const HPDF_Page page, hpdftbl_t t, const HPDF_REAL xpos, const HPDF_REAL ypos, const HPDF_REAL width, HPDF_REAL height)
Stroke the table.
Definition: hpdftbl.c:1682
int hpdftbl_set_content_style_cb(hpdftbl_t t, hpdftbl_content_style_callback_t cb)
Set callback to specify cell content style.
Definition: hpdftbl_callback.c:690
@ CENTER
Definition: hpdftbl.h:294
@ LEFT
Definition: hpdftbl.h:293
#define HPDF_FF_HELVETICA_BOLD
Definition: hpdftbl.h:106
#define A4PAGE_WIDTH_CM
Standard A4 paper width in cm.
Definition: hpdftbl.h:203
#define HPDF_FF_TIMES
Definition: hpdftbl.h:94
#define TRUE
Boolean truth value.
Definition: hpdftbl.h:47
#define FALSE
Boolean false value.
Definition: hpdftbl.h:52
#define A4PAGE_HEIGHT_CM
Standard A4 paper height in cm.
Definition: hpdftbl.h:198
#define hpdftbl_cm2dpi(c)
Convert cm to dots using the default resolution (72 DPI)
Definition: hpdftbl.h:256
int hpdftbl_set_content_cb(hpdftbl_t t, hpdftbl_content_callback_t cb)
Set table content callback.
Definition: hpdftbl_callback.c:86
Core table handle.
Definition: hpdftbl.h:470
Specification of a text style.
Definition: hpdftbl.h:303
char * font
Definition: hpdftbl.h:304
HPDF_RGBColor color
Definition: hpdftbl.h:306
HPDF_REAL fsize
Definition: hpdftbl.h:305
hpdftbl_text_align_t halign
Definition: hpdftbl.h:308
HPDF_RGBColor background
Definition: hpdftbl.h:307
Common functions for all unit-test/examples.
#define TUTEX_MAIN(_tbl_, _showgrid_)
Macro to create a main() function to call the table creation function for each example....
Definition: unit_test.inc.h:322