Skip to content

Commit

Permalink
Add a simple unit test to create a PDF file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
franko committed Jan 3, 2023
1 parent 9cdf818 commit 939a4e2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Setup
run: |
meson setup .build
- name: Build
- name: Dist Check
run: |
ninja dist -C .build
- name: Unit Tests
run: |
ninja test -C .build
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ libtexpdf_dep = declare_dependency(
link_with : libtexpdf,
)

subdir('tests')

pkg.generate(libtexpdf,
filebase : 'libtexpdf',
name : 'libtexpdf',
Expand Down
8 changes: 8 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
libtexpdf_include = include_directories('..')

test_create = executable('test-create', 'test-create.c',
dependencies: libtexpdf_dep,
include_directories: libtexpdf_include,
)

test('Create a PDF file', test_create, args : ['pdf-test.pdf'])
59 changes: 59 additions & 0 deletions tests/test-create.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libtexpdf.h>

double precision = 65536.0;
char* producer = "SILE";

pdf_doc *pdf_init (const char *fn, double w, double height) {
pdf_doc *p = texpdf_open_document(fn, 0, w, height, 0,0,0);
texpdf_init_device(p, 1 / precision, 2, 0);

pdf_rect mediabox;
mediabox.llx = 0.0;
mediabox.lly = 0.0;
mediabox.urx = w;
mediabox.ury = height;
texpdf_files_init();
texpdf_init_fontmaps();
texpdf_tt_aux_set_always_embed();
texpdf_doc_set_mediabox(p, 0, &mediabox);
texpdf_add_dict(p->info,
texpdf_new_name("Producer"),
texpdf_new_string(producer, strlen(producer)));
return p;
}

int pdf_finish(pdf_doc *p) {
texpdf_files_close();
texpdf_close_device();
texpdf_close_document(p);
texpdf_close_fontmaps();
return 0;
}

int pdf_add_content(pdf_doc *p, const char *input) {
int input_l = strlen(input);
texpdf_graphics_mode(p); /* Don't be mid-string! */
texpdf_doc_add_page_content(p, " ", 1);
texpdf_doc_add_page_content(p, input, input_l);
texpdf_doc_add_page_content(p, " ", 1);
return 0;
}

int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s <output-file>\n", argv[0]);
return 1;
}
const char *filename = argv[1];
const double width = 400.0, height = 600.0;
pdf_doc *p = pdf_init(filename, width, height);
texpdf_doc_begin_page(p, 1, 0, height);
pdf_add_content(p, "Hello world!");
texpdf_doc_end_page(p);
pdf_finish(p);
return 0;
}

0 comments on commit 939a4e2

Please sign in to comment.