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

Defining a table with widgets.

#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
static char *
cb_labels(void *tag, size_t r, size_t c) {
static char buf[32];
if (0==r && 0==c) {
snprintf(buf, sizeof buf, "Device name:");
} else if (0==r && 1==c) {
snprintf(buf, sizeof buf, "Date:");
} else if (1==r && 0==c) {
snprintf(buf, sizeof buf, "Battery strength:");
} else if (1==r && 1==c) {
snprintf(buf, sizeof buf, "Signal:");
} else {
return NULL;
}
return buf;
}
static char *
cb_date(void *tag, size_t r, size_t c) {
static char buf[64];
if ( ! run_as_unit_test ) {
time_t t = time(NULL);
ctime_r(&t, buf);
return buf;
} else {
return "Wed May 4 19:01:01 2022";
}
}
static char *
cb_device_name(void *tag, size_t r, size_t c) {
static char buf[32];
snprintf(buf, sizeof buf, "IoT Device ABC123");
return buf;
}
void
cb_draw_battery_widget(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r,
size_t c, HPDF_REAL xpos, HPDF_REAL ypos,
HPDF_REAL width, HPDF_REAL height) {
const HPDF_REAL segment_tot_width = width * 0.5;
const HPDF_REAL segment_height = height / 3;
const HPDF_REAL segment_xpos = xpos + 40;
const HPDF_REAL segment_ypos = ypos + 4;
const size_t num_segments = 10;
const HPDF_RGBColor on_color = HPDF_COLOR_DARK_GREEN;
const double val_percent = 0.4;
const _Bool val_text_hide = FALSE;
doc, page, segment_xpos, segment_ypos, segment_tot_width,
segment_height, num_segments, on_color, val_percent, val_text_hide);
}
void
cb_draw_signal_widget(HPDF_Doc doc, HPDF_Page page, void *tag, size_t r,
size_t c, HPDF_REAL xpos, HPDF_REAL ypos,
HPDF_REAL width, HPDF_REAL height) {
const HPDF_REAL wwidth = 35;
const HPDF_REAL wheight = 20;
const HPDF_REAL wxpos = xpos + 70;
const HPDF_REAL wypos = ypos + 4;
const size_t num_segments = 5;
const HPDF_RGBColor on_color = HPDF_COLOR_DARK_RED;
// This should be the real data retrieved from a DB (for example)
const size_t num_on_segments = 3;
hpdftbl_widget_strength_meter(doc, page, wxpos, wypos, wwidth, wheight,
num_segments, on_color, num_on_segments);
}
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
void
create_table_ex14(HPDF_Doc pdf_doc, HPDF_Page pdf_page) {
const size_t num_rows = 2;
const size_t num_cols = 2;
char *table_title = "tut_ex14: 2x2 table widget callbacks";
hpdftbl_t tbl = hpdftbl_create_title(num_rows, num_cols, table_title);
// Use one label callback for the entire table
hpdftbl_set_label_cb(tbl, cb_labels);
// Name in top left corner
hpdftbl_set_cell_content_cb(tbl, 0, 0, cb_device_name);
// Date in top right corner
hpdftbl_set_cell_content_cb(tbl, 0, 1, cb_date);
// Draw battery strength
hpdftbl_set_cell_canvas_cb(tbl, 1, 0, cb_draw_battery_widget);
// Draw signal strength
hpdftbl_set_cell_canvas_cb(tbl, 1, 1, cb_draw_signal_widget);
HPDF_REAL xpos = hpdftbl_cm2dpi(1);
HPDF_REAL ypos = hpdftbl_cm2dpi(A4PAGE_HEIGHT_CM - 1);
HPDF_REAL width = hpdftbl_cm2dpi(12);
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_ex14, FALSE)
int hpdftbl_use_labelgrid(hpdftbl_t t, _Bool use)
Shorter vertical line to mark labels.
Definition: hpdftbl.c:687
int hpdftbl_use_labels(hpdftbl_t t, _Bool use)
Enable/Disable the use of cell labels.
Definition: hpdftbl.c:666
hpdftbl_t hpdftbl_create_title(size_t rows, size_t cols, char *title)
Create a new table with title top row.
Definition: hpdftbl.c:326
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
void hpdftbl_widget_segment_hbar(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, size_t num_segments, HPDF_RGBColor on_color, double val_percent, _Bool hide_val)
Draw a horizontal segment meter that can be used to visualize a discrete value.
Definition: hpdftbl_widget.c:299
#define TRUE
Boolean truth value.
Definition: hpdftbl.h:47
#define FALSE
Boolean false value.
Definition: hpdftbl.h:52
int hpdftbl_set_cell_content_cb(hpdftbl_t t, size_t r, size_t c, hpdftbl_content_callback_t cb)
Set cell content callback.
Definition: hpdftbl_callback.c:107
void hpdftbl_widget_strength_meter(HPDF_Doc doc, HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, HPDF_REAL width, HPDF_REAL height, size_t num_segments, HPDF_RGBColor on_color, size_t num_on_segments)
Draw a phone strength meter.
Definition: hpdftbl_widget.c:383
#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_cell_canvas_cb(hpdftbl_t t, size_t r, size_t c, hpdftbl_canvas_callback_t cb)
Set cell canvas callback.
Definition: hpdftbl_callback.c:178
int hpdftbl_set_label_cb(hpdftbl_t t, hpdftbl_content_callback_t cb)
Set table label callback.
Definition: hpdftbl_callback.c:212
Core table handle.
Definition: hpdftbl.h:470
Common functions for all unit-test/examples.
_Bool run_as_unit_test
For the case when we use this example as a unit/integration test we do not want data such as dates,...
Definition: unit_test.inc.h:35
#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