Skip to content

Commit 939a4e2

Browse files
committed
Add a simple unit test to create a PDF file
The text does not appear in the document. The page size should be set to some more sensible values. We should probably check the output file, at least that it is not empty and check that the header correspond to a valid PDF file.
1 parent 9cdf818 commit 939a4e2

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
- name: Setup
3434
run: |
3535
meson setup .build
36-
- name: Build
36+
- name: Dist Check
3737
run: |
3838
ninja dist -C .build
39+
- name: Unit Tests
40+
run: |
41+
ninja test -C .build

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ libtexpdf_dep = declare_dependency(
180180
link_with : libtexpdf,
181181
)
182182

183+
subdir('tests')
184+
183185
pkg.generate(libtexpdf,
184186
filebase : 'libtexpdf',
185187
name : 'libtexpdf',

tests/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
libtexpdf_include = include_directories('..')
2+
3+
test_create = executable('test-create', 'test-create.c',
4+
dependencies: libtexpdf_dep,
5+
include_directories: libtexpdf_include,
6+
)
7+
8+
test('Create a PDF file', test_create, args : ['pdf-test.pdf'])

tests/test-create.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <libtexpdf.h>
6+
7+
double precision = 65536.0;
8+
char* producer = "SILE";
9+
10+
pdf_doc *pdf_init (const char *fn, double w, double height) {
11+
pdf_doc *p = texpdf_open_document(fn, 0, w, height, 0,0,0);
12+
texpdf_init_device(p, 1 / precision, 2, 0);
13+
14+
pdf_rect mediabox;
15+
mediabox.llx = 0.0;
16+
mediabox.lly = 0.0;
17+
mediabox.urx = w;
18+
mediabox.ury = height;
19+
texpdf_files_init();
20+
texpdf_init_fontmaps();
21+
texpdf_tt_aux_set_always_embed();
22+
texpdf_doc_set_mediabox(p, 0, &mediabox);
23+
texpdf_add_dict(p->info,
24+
texpdf_new_name("Producer"),
25+
texpdf_new_string(producer, strlen(producer)));
26+
return p;
27+
}
28+
29+
int pdf_finish(pdf_doc *p) {
30+
texpdf_files_close();
31+
texpdf_close_device();
32+
texpdf_close_document(p);
33+
texpdf_close_fontmaps();
34+
return 0;
35+
}
36+
37+
int pdf_add_content(pdf_doc *p, const char *input) {
38+
int input_l = strlen(input);
39+
texpdf_graphics_mode(p); /* Don't be mid-string! */
40+
texpdf_doc_add_page_content(p, " ", 1);
41+
texpdf_doc_add_page_content(p, input, input_l);
42+
texpdf_doc_add_page_content(p, " ", 1);
43+
return 0;
44+
}
45+
46+
int main(int argc, char *argv[]) {
47+
if (argc != 2) {
48+
fprintf(stderr, "usage: %s <output-file>\n", argv[0]);
49+
return 1;
50+
}
51+
const char *filename = argv[1];
52+
const double width = 400.0, height = 600.0;
53+
pdf_doc *p = pdf_init(filename, width, height);
54+
texpdf_doc_begin_page(p, 1, 0, height);
55+
pdf_add_content(p, "Hello world!");
56+
texpdf_doc_end_page(p);
57+
pdf_finish(p);
58+
return 0;
59+
}

0 commit comments

Comments
 (0)