libhpdftbl 1.5.0
Table construction library for Haru PDF library
Loading...
Searching...
No Matches
hpdftbl_load.c File Reference

Functions for load (internalizing) serialized data structure. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <hpdf.h>
#include "hpdftbl.h"
#include <jansson.h>

Macros

#define GETJSON_STRING(table, k, var)
 
#define GETJSON_UINT(table, k, var)
 
#define GETJSON_REAL(table, k, var)
 
#define GETJSON_BOOLEAN(table, k, var)
 
#define GETJSON_RGB(table, k, var)
 
#define GETJSON_GRIDSTYLE(table, k, var)
 
#define GETJSON_TXTSTYLE(table, k, var)
 
#define GETJSON_REALARRAY(table, k, var)
 
#define GETJSON_DYNCB(table, key)
 
#define GETJSON_CELLDYNCB(table, key, r, c)
 
#define GETJSON_CELLTXTSTYLE(table, key, r, c)
 

Functions

int hpdftbl_theme_loads (hpdftbl_theme_t *theme, char *buff)
 Load theme from a serialized string. This is the invert function of hpdftbl_theme_dumps(). More...
 
int hpdftbl_theme_load (hpdftbl_theme_t *theme, char *filename)
 Read a theme from a previous serialized theme from a named file. More...
 
int hpdftbl_load (hpdftbl_t tbl, char *filename)
 Import a table structure from a serialized table on file. More...
 
int hpdftbl_loads (hpdftbl_t tbl, char *buff)
 Import a table structure from a serialized json buffert. More...
 

Detailed Description

Functions for load (internalizing) serialized data structure.

Author
Johan Persson (johan.nosp@m.162@.nosp@m.gmail.nosp@m..com)

Copyright (C) 2022 Johan Persson

See also
LICENSE

Released under the MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Macro Definition Documentation

◆ GETJSON_BOOLEAN

#define GETJSON_BOOLEAN (   table,
  k,
  var 
)
Value:
do { \
json_t *_elem=json_object_get(table,k); \
if(!_elem) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
var=json_boolean_value(_elem); \
} while(0)

◆ GETJSON_CELLDYNCB

#define GETJSON_CELLDYNCB (   table,
  key,
  r,
 
)
Value:
do { \
json_t *_elem=json_object_get(table,#key); \
if(!_elem) { \
json_not_found_str=#key; \
goto json_raise_notfound_error; \
} if( strlen(json_string_value(_elem)) != 0 ) \
hpdftbl_set_cell_ ## key(t, r, c, json_string_value(_elem)); \
} while(0);

◆ GETJSON_CELLTXTSTYLE

#define GETJSON_CELLTXTSTYLE (   table,
  key,
  r,
 
)
Value:
do { \
json_t *__elem=json_object_get(table, #key); \
if(!__elem) { \
json_not_found_str= #key; \
goto json_raise_notfound_error; \
} \
if( json_is_object(__elem) ) { \
GETJSON_STRING(__elem,"font",t->cells[t->cols*r+c].key.font); \
GETJSON_REAL(__elem,"fsize",t->cells[t->cols*r+c].key.fsize); \
GETJSON_RGB(__elem,"color",t->cells[t->cols*r+c].key.color); \
GETJSON_RGB(__elem,"background",t->cells[t->cols*r+c].key.background); \
GETJSON_UINT(__elem,"halign",t->cells[t->cols*r+c].key.halign); \
} else { \
json_not_found_str= #key; \
goto json_raise_notfound_error; \
} \
} while(0);

◆ GETJSON_DYNCB

#define GETJSON_DYNCB (   table,
  key 
)
Value:
do { \
json_t *_elem=json_object_get(table, #key); \
if(!_elem) { \
json_not_found_str= #key; \
goto json_raise_notfound_error; \
} if( strlen(json_string_value(_elem)) != 0 ) \
hpdftbl_set_ ## key(t, json_string_value(_elem)); \
} while(0);

◆ GETJSON_GRIDSTYLE

#define GETJSON_GRIDSTYLE (   table,
  k,
  var 
)
Value:
do { \
json_t *_grid=json_object_get(table,k); \
if(!_grid) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
if(json_is_object(_grid)) { \
GETJSON_REAL(_grid,"width",var.width); \
GETJSON_UINT(_grid,"dashstyle",var.line_dashstyle); \
GETJSON_RGB(_grid,"color",var.color); \
} \
} while(0)

◆ GETJSON_REAL

#define GETJSON_REAL (   table,
  k,
  var 
)
Value:
do { \
json_t *_elem=json_object_get(table,k); \
if(!_elem) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
var=json_real_value(_elem); \
} while(0)

◆ GETJSON_REALARRAY

#define GETJSON_REALARRAY (   table,
  k,
  var 
)
Value:
do { \
json_t *_array = json_object_get(table, k); \
if(!_array){ \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
size_t _idx; \
json_t *_val; \
json_array_foreach(_array, _idx, _val) { \
var[_idx] = json_real_value(_val); \
} \
} while(0)

◆ GETJSON_RGB

#define GETJSON_RGB (   table,
  k,
  var 
)
Value:
do { \
json_t *_elem; \
json_t *_val; \
size_t _idx; \
double tmpcol[3]; \
_elem = json_object_get(table, k);\
if(!_elem) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
json_array_foreach(_elem, _idx, _val) { \
tmpcol[_idx] = json_real_value(_val); \
} \
var.r = tmpcol[0]; \
var.g = tmpcol[1]; \
var.b = tmpcol[2]; \
} while(0)

◆ GETJSON_STRING

#define GETJSON_STRING (   table,
  k,
  var 
)
Value:
do { \
json_t *_elem=json_object_get(table,k); \
if(!_elem) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
if( strlen(json_string_value(_elem)) == 0 ) \
var=NULL; \
else \
var=strdup(json_string_value(_elem)); \
} while(0)

◆ GETJSON_TXTSTYLE

#define GETJSON_TXTSTYLE (   table,
  k,
  var 
)
Value:
do { \
json_t *_txtstyle=json_object_get(table,k); \
if(!_txtstyle) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
if(json_is_object(_txtstyle)) { \
GETJSON_STRING(_txtstyle,"font",var.font); \
GETJSON_REAL(_txtstyle,"fsize",var.fsize); \
GETJSON_RGB(_txtstyle,"color",var.color); \
GETJSON_RGB(_txtstyle,"background",var.background); \
GETJSON_UINT(_txtstyle,"halign",var.halign); \
} \
} while(0)

◆ GETJSON_UINT

#define GETJSON_UINT (   table,
  k,
  var 
)
Value:
do { \
json_t *_elem=json_object_get(table,k); \
if(!_elem) { \
json_not_found_str=k; \
goto json_raise_notfound_error; \
} \
var=(size_t)json_integer_value(_elem); \
} while(0)

Function Documentation

◆ hpdftbl_load()

int hpdftbl_load ( hpdftbl_t  tbl,
char *  filename 
)

Import a table structure from a serialized table on file.

The json file make it possible to adjust the table directly. However it is easy to get it wrong. Some things to keep in mind while doing manual changes.

  • A real number must always be written as a decimal number with at least one decimal point (even if it .0)
  • Remember that the width of the table is specified manually and not automatically recalculated based on the text width.

After reading a serialized table it can asily be be stroked with only two lines of code as the following code-snippet shows

hpdftbl_t tbl = calloc(1, sizeof (struct hpdftbl));
if(0 == hpdftbl_load(tbl, "mytablefile.json") ) {
hpdftbl_stroke_pos(pdf_doc, pdf_page, tbl);
}
int hpdftbl_stroke_pos(HPDF_Doc pdf, const HPDF_Page page, hpdftbl_t t)
Stroke the table using the already specified size and position within the table.
Definition: hpdftbl.c:1654
int hpdftbl_load(hpdftbl_t tbl, char *filename)
Import a table structure from a serialized table on file.
Definition: hpdftbl_load.c:330
Core table handle.
Definition: hpdftbl.h:470
Note
The hpdftbl_t is a pointer type to struct hpdftbl and hence must must be either dynamically allocated as the example here shows or an instance of the struct must be created whose address is given to this functions.
Parameters
tblTable to read into
filenameFile to read from
Returns
0 on success, -1 on file parse error, -2 on nay other error

◆ hpdftbl_loads()

int hpdftbl_loads ( hpdftbl_t  tbl,
char *  buff 
)

Import a table structure from a serialized json buffert.

This is the preferred way on how to store a table structure in for example a database.

Example:

char *mybuffer = ....
hpdftbl_t tbl = calloc(1, sizeof (struct hpdftbl));
if(0 == hpdftbl_load(tbl, mybuffer) ) {
hpdftbl_stroke_pos(pdf_doc, pdf_page, tbl);
}
Parameters
tblReference to table handle to be populated
buffBuffer with serialized data to read back
Returns
0 on success, -1 on file parse error, -2 on nay other error
See also
hpdftbl_dump(), hpdftbl_load(), hpdftbl_stroke_pos()

Referenced by hpdftbl_load().

◆ hpdftbl_theme_load()

int hpdftbl_theme_load ( hpdftbl_theme_t theme,
char *  filename 
)

Read a theme from a previous serialized theme from a named file.

Example:

hpdftbl_t tbl = calloc(1, sizeof (struct hpdftbl));
if( 0 == hpdftbl_load(tbl, "tests/tut_ex41.json") ) {
if( 0 == hpdftbl_theme_load(&theme, "mytheme.json") ) {
hpdftbl_apply_theme(tbl, &theme);
hpdftbl_stroke_pos(pdf_doc, pdf_page, tbl);
}
}
int hpdftbl_apply_theme(hpdftbl_t t, hpdftbl_theme_t *theme)
Apply a specified theme to a table.
Definition: hpdftbl_theme.c:177
int hpdftbl_theme_load(hpdftbl_theme_t *tbl, char *filename)
Read a theme from a previous serialized theme from a named file.
Definition: hpdftbl_load.c:282
Define a set of styles into a table theme.
Definition: hpdftbl.h:635
Parameters
themeTheme to read into
filenameFile to read from
Returns
0 on success, -1 on failure

◆ hpdftbl_theme_loads()

int hpdftbl_theme_loads ( hpdftbl_theme_t theme,
char *  buff 
)

Load theme from a serialized string. This is the invert function of hpdftbl_theme_dumps().

Parameters
themeTheme to load to.
buffBuffer which holds the previous serialized theme
Returns
0 on success, -1 on failure
See also
hpdftbl_theme_dumps(), hpdftbl_theme_load(), hpdftbl_apply_theme()

Referenced by hpdftbl_theme_load().