forked from sile-typesetter/libtexpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |