*/}}

pdfgen.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. * Simple engine for creating PDF files.
  3. * It supports text, shapes, images etc...
  4. * Capable of handling millions of objects without too much performance
  5. * penalty.
  6. * Public domain license - no warrenty implied; use at your own risk.
  7. * @file pdfgen.h
  8. */
  9. #ifndef PDFGEN_H
  10. #define PDFGEN_H
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. /**
  17. * @defgroup subsystem Simple PDF Generation
  18. * Allows for quick generation of simple PDF documents.
  19. * This is useful for producing easily printed output from C code, where
  20. * advanced formatting is not required
  21. *
  22. * Note: All coordinates/sizes are in points (1/72 of an inch).
  23. * All coordinates are based on 0,0 being the bottom left of the page.
  24. * All colours are specified as a packed 32-bit value - see @ref PDF_RGB.
  25. * Text strings are interpreted as UTF-8 encoded, but only a small subset of
  26. * characters beyond 7-bit ascii are supported (see @ref pdf_add_text for
  27. * details).
  28. *
  29. * @par PDF library example:
  30. * @code
  31. #include "pdfgen.h"
  32. ...
  33. struct pdf_info info = {
  34. .creator = "My software",
  35. .producer = "My software",
  36. .title = "My document",
  37. .author = "My name",
  38. .subject = "My subject",
  39. .date = "Today"
  40. };
  41. struct pdf_doc *pdf = pdf_create(PDF_A4_WIDTH, PDF_A4_HEIGHT, &info);
  42. pdf_set_font(pdf, "Times-Roman");
  43. pdf_append_page(pdf);
  44. pdf_add_text(pdf, NULL, "This is text", 12, 50, 20, PDF_BLACK);
  45. pdf_add_line(pdf, NULL, 50, 24, 150, 24);
  46. pdf_save(pdf, "output.pdf");
  47. pdf_destroy(pdf);
  48. * @endcode
  49. */
  50. struct pdf_doc;
  51. struct pdf_object;
  52. /**
  53. * pdf_info describes the metadata to be inserted into the
  54. * header of the output PDF
  55. */
  56. struct pdf_info {
  57. char creator[64]; //!< Software used to create the PDF
  58. char producer[64]; //!< Software used to create the PDF
  59. char title[64]; //!< The title of the PDF (typically displayed in the
  60. //!< window bar when viewing)
  61. char author[64]; //!< Who created the PDF
  62. char subject[64]; //!< What is the PDF about
  63. char date[64]; //!< The date the PDF was created
  64. };
  65. /**
  66. * Enum that declares the different image file formats we currently support.
  67. * Each value has a corresponding header struct used within
  68. * the format_specific_img_info union.
  69. */
  70. enum {
  71. IMAGE_PNG,
  72. IMAGE_JPG,
  73. IMAGE_PPM,
  74. IMAGE_BMP,
  75. IMAGE_UNKNOWN
  76. };
  77. /**
  78. * Since we're casting random areas of memory to these, make sure
  79. * they're packed properly to match the image format requirements
  80. */
  81. #pragma pack(push, 1)
  82. /**
  83. * Information about color type of PNG format
  84. * As defined by https://www.w3.org/TR/2003/REC-PNG-20031110/#6Colour-values
  85. */
  86. enum /* png colortype */ {
  87. // Greyscale
  88. PNG_COLOR_GREYSCALE = 0,
  89. // Truecolour
  90. PNG_COLOR_RGB = 2,
  91. // Indexed-colour
  92. PNG_COLOR_INDEXED = 3,
  93. // Greyscale with alpha
  94. PNG_COLOR_GREYSCALE_A = 4,
  95. // Truecolour with alpha
  96. PNG_COLOR_RGBA = 6,
  97. PNG_COLOR_INVALID = 255
  98. };
  99. /**
  100. * png_header describes the header information extracted from .PNG files
  101. */
  102. struct png_header {
  103. uint32_t width; //!< Width in pixels
  104. uint32_t height; //!< Height in pixels
  105. uint8_t bitDepth; //!< Bit Depth
  106. uint8_t colorType; //!< Color type - see PNG_COLOR_xx
  107. uint8_t deflate; //!< Deflate setting
  108. uint8_t filtering; //!< Filtering
  109. uint8_t interlace; //!< Interlacing
  110. };
  111. /**
  112. * bmp_header describes the header information extracted from .BMP files
  113. */
  114. struct bmp_header {
  115. uint32_t bfSize; //!< size of BMP in bytes
  116. uint16_t bfReserved1; //!< ignore!
  117. uint16_t bfReserved2; //!< ignore!
  118. uint32_t bfOffBits; //!< Offset to BMP data
  119. uint32_t biSize; //!< Size of this header (40)
  120. int32_t biWidth; //!< Width in pixels
  121. int32_t biHeight; //!< Height in pixels
  122. uint16_t biPlanes; //!< Number of colour planes - must be 1
  123. uint16_t biBitCount; //!< Bits Per Pixel
  124. uint32_t biCompression; //!< Compression Method
  125. };
  126. #pragma pack(pop)
  127. /**
  128. * jpeg_header describes the header information extracted from .JPG files
  129. */
  130. struct jpeg_header {
  131. int ncolours; //!< Number of colours
  132. };
  133. /**
  134. * PPM color spaces
  135. */
  136. enum {
  137. PPM_BINARY_COLOR_RGB, //!< binary ppm with RGB colors (magic number P5)
  138. PPM_BINARY_COLOR_GRAY, //!< binary ppm with grayscale colors (magic number
  139. //!< P6)
  140. };
  141. /**
  142. * ppm_header describes the header information extracted from .PPM files
  143. */
  144. struct ppm_header {
  145. size_t size; //!< Indicate the size of the image data
  146. size_t data_begin_pos; //!< position in the data where the image starts
  147. int color_space; //!< PPM color space
  148. };
  149. /**
  150. * pdf_img_info describes the metadata for an arbitrary image
  151. */
  152. struct pdf_img_info {
  153. int image_format; //!< Indicates the image format (IMAGE_PNG, ...)
  154. uint32_t width; //!< Width in pixels
  155. uint32_t height; //!< Height in pixels
  156. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  157. // Doxygen doesn't like anonymous unions
  158. //!< Image specific details
  159. union {
  160. struct bmp_header bmp; //!< BMP header info
  161. struct jpeg_header jpeg; //!< JPEG header info
  162. struct png_header png; //!< PNG header info
  163. struct ppm_header ppm; //!< PPM header info
  164. };
  165. #endif
  166. };
  167. /**
  168. * pdf_path_operation holds information about a path
  169. * drawing operation.
  170. * See PDF reference for detailed usage.
  171. */
  172. struct pdf_path_operation {
  173. char op; /*!< Operation command. Possible operators are: m = move to, l =
  174. line to, c = cubic bezier curve with two control points, v =
  175. cubic bezier curve with one control point fixed at first
  176. point, y = cubic bezier curve with one control point fixed
  177. at second point, h = close path */
  178. float x1; /*!< X offset of the first point. Used with: m, l, c, v, y */
  179. float y1; /*!< Y offset of the first point. Used with: m, l, c, v, y */
  180. float x2; /*!< X offset of the second point. Used with: c, v, y */
  181. float y2; /*!< Y offset of the second point. Used with: c, v, y */
  182. float x3; /*!< X offset of the third point. Used with: c */
  183. float y3; /*!< Y offset of the third point. Used with: c */
  184. };
  185. /**
  186. * Convert a value in inches into a number of points.
  187. * @param inch inches value to convert to points
  188. */
  189. #define PDF_INCH_TO_POINT(inch) ((float)((inch)*72.0f))
  190. /**
  191. * Convert a value in milli-meters into a number of points.
  192. * @param mm millimeter value to convert to points
  193. */
  194. #define PDF_MM_TO_POINT(mm) ((float)((mm)*72.0f / 25.4f))
  195. /*! Point width of a standard US-Letter page */
  196. #define PDF_LETTER_WIDTH PDF_INCH_TO_POINT(8.5f)
  197. /*! Point height of a standard US-Letter page */
  198. #define PDF_LETTER_HEIGHT PDF_INCH_TO_POINT(11.0f)
  199. /*! Point width of a standard A4 page */
  200. #define PDF_A4_WIDTH PDF_MM_TO_POINT(210.0f)
  201. /*! Point height of a standard A4 page */
  202. #define PDF_A4_HEIGHT PDF_MM_TO_POINT(297.0f)
  203. /*! Point width of a standard A3 page */
  204. #define PDF_A3_WIDTH PDF_MM_TO_POINT(297.0f)
  205. /*! Point height of a standard A3 page */
  206. #define PDF_A3_HEIGHT PDF_MM_TO_POINT(420.0f)
  207. /**
  208. * Convert three 8-bit RGB values into a single packed 32-bit
  209. * colour. These 32-bit colours are used by various functions
  210. * in PDFGen
  211. */
  212. #define PDF_RGB(r, g, b) \
  213. (uint32_t)((((r)&0xff) << 16) | (((g)&0xff) << 8) | (((b)&0xff)))
  214. /**
  215. * Convert four 8-bit ARGB values into a single packed 32-bit
  216. * colour. These 32-bit colours are used by various functions
  217. * in PDFGen. Alpha values range from 0 (opaque) to 0xff
  218. * (transparent)
  219. */
  220. #define PDF_ARGB(a, r, g, b) \
  221. (uint32_t)(((uint32_t)((a)&0xff) << 24) | (((r)&0xff) << 16) | \
  222. (((g)&0xff) << 8) | (((b)&0xff)))
  223. /*! Utility macro to provide bright red */
  224. #define PDF_RED PDF_RGB(0xff, 0, 0)
  225. /*! Utility macro to provide bright green */
  226. #define PDF_GREEN PDF_RGB(0, 0xff, 0)
  227. /*! Utility macro to provide bright blue */
  228. #define PDF_BLUE PDF_RGB(0, 0, 0xff)
  229. /*! Utility macro to provide black */
  230. #define PDF_BLACK PDF_RGB(0, 0, 0)
  231. /*! Utility macro to provide white */
  232. #define PDF_WHITE PDF_RGB(0xff, 0xff, 0xff)
  233. /*!
  234. * Utility macro to provide a transparent colour
  235. * This is used in some places for 'fill' colours, where no fill is required
  236. */
  237. #define PDF_TRANSPARENT (uint32_t)(0xffu << 24)
  238. /**
  239. * Different alignment options for rendering text
  240. */
  241. enum {
  242. PDF_ALIGN_LEFT, //!< Align text to the left
  243. PDF_ALIGN_RIGHT, //!< Align text to the right
  244. PDF_ALIGN_CENTER, //!< Align text in the center
  245. PDF_ALIGN_JUSTIFY, //!< Align text in the center, with padding to fill the
  246. //!< available space
  247. PDF_ALIGN_JUSTIFY_ALL, //!< Like PDF_ALIGN_JUSTIFY, except even short
  248. //!< lines will be fully justified
  249. PDF_ALIGN_NO_WRITE, //!< Fake alignment for only checking wrap height with
  250. //!< no writes
  251. };
  252. /**
  253. * Create a new PDF object, with the given page
  254. * width/height
  255. * @param width Width of the page
  256. * @param height Height of the page
  257. * @param info Optional information to be put into the PDF header
  258. * @return PDF document object, or NULL on failure
  259. */
  260. struct pdf_doc *pdf_create(float width, float height,
  261. const struct pdf_info *info);
  262. /**
  263. * Destroy the pdf object, and all of its associated memory
  264. * @param pdf PDF document to clean up
  265. */
  266. void pdf_destroy(struct pdf_doc *pdf);
  267. /**
  268. * Retrieve the error message if any operation fails
  269. * @param pdf pdf document to retrieve error message from
  270. * @param errval optional pointer to an integer to be set to the error code
  271. * @return NULL if no error message, string description of error otherwise
  272. */
  273. const char *pdf_get_err(const struct pdf_doc *pdf, int *errval);
  274. /**
  275. * Acknowledge an outstanding pdf error
  276. * @param pdf pdf document to clear the error message from
  277. */
  278. void pdf_clear_err(struct pdf_doc *pdf);
  279. /**
  280. * Sets the font to use for text objects. Default value is Times-Roman if
  281. * this function is not called.
  282. * Note: The font selection should be done before text is output,
  283. * and will remain until pdf_set_font is called again.
  284. * @param pdf PDF document to update font on
  285. * @param font New font to use. This must be one of the standard PDF fonts:
  286. * Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique,
  287. * Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique,
  288. * Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic,
  289. * Symbol or ZapfDingbats
  290. * @return < 0 on failure, 0 on success
  291. */
  292. int pdf_set_font(struct pdf_doc *pdf, const char *font);
  293. /**
  294. * Calculate the width of a given string in the current font
  295. * @param pdf PDF document
  296. * @param font_name Name of the font to get the width of.
  297. * This must be one of the standard PDF fonts:
  298. * Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique,
  299. * Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique,
  300. * Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic,
  301. * Symbol or ZapfDingbats
  302. * @param text Text to determine width of
  303. * @param size Size of the text, in points
  304. * @param text_width area to store calculated width in
  305. * @return < 0 on failure, 0 on success
  306. */
  307. int pdf_get_font_text_width(struct pdf_doc *pdf, const char *font_name,
  308. const char *text, float size, float *text_width);
  309. /**
  310. * Retrieves a PDF document height
  311. * @param pdf PDF document to get height of
  312. * @return height of PDF document (in points)
  313. */
  314. float pdf_height(const struct pdf_doc *pdf);
  315. /**
  316. * Retrieves a PDF document width
  317. * @param pdf PDF document to get width of
  318. * @return width of PDF document (in points)
  319. */
  320. float pdf_width(const struct pdf_doc *pdf);
  321. /**
  322. * Retrieves page height
  323. * @param page Page object to get height of
  324. * @return height of page (in points)
  325. */
  326. float pdf_page_height(const struct pdf_object *page);
  327. /**
  328. * Retrieves page width
  329. * @param page Page object to get width of
  330. * @return width of page (in points)
  331. */
  332. float pdf_page_width(const struct pdf_object *page);
  333. /**
  334. * Add a new page to the given pdf
  335. * @param pdf PDF document to append page to
  336. * @return new page object
  337. */
  338. struct pdf_object *pdf_append_page(struct pdf_doc *pdf);
  339. /**
  340. * Retrieve a page by its number.
  341. *
  342. * Note: The page must have already been created via \ref pdf_append_page
  343. *
  344. * @param pdf PDF document to get page from
  345. * @param page_number Page number to retrieve, starting from 1.
  346. * @return Page object if the given page is found, NULL otherwise
  347. */
  348. struct pdf_object *pdf_get_page(struct pdf_doc *pdf, int page_number);
  349. /**
  350. * Adjust the width/height of a specific page
  351. * @param pdf PDF document that the page belongs to
  352. * @param page object returned from @ref pdf_append_page
  353. * @param width Width of the page in points
  354. * @param height Height of the page in points
  355. * @return < 0 on failure, 0 on success
  356. */
  357. int pdf_page_set_size(struct pdf_doc *pdf, struct pdf_object *page,
  358. float width, float height);
  359. /**
  360. * Save the given pdf document to the supplied filename.
  361. * @param pdf PDF document to save
  362. * @param filename Name of the file to store the PDF into (NULL for stdout)
  363. * @return < 0 on failure, >= 0 on success
  364. */
  365. int pdf_save(struct pdf_doc *pdf, const char *filename);
  366. /**
  367. * Save the given pdf document to the given FILE output
  368. * @param pdf PDF document to save
  369. * @param fp FILE pointer to store the data into (must be writable)
  370. * @return < 0 on failure, >= 0 on success
  371. */
  372. int pdf_save_file(struct pdf_doc *pdf, FILE *fp);
  373. /**
  374. * Add a text string to the document
  375. * @param pdf PDF document to add to
  376. * @param page Page to add object to (NULL => most recently added page)
  377. * @param text String to display
  378. * @param size Point size of the font
  379. * @param xoff X location to put it in
  380. * @param yoff Y location to put it in
  381. * @param colour Colour to draw the text
  382. * @return 0 on success, < 0 on failure
  383. */
  384. int pdf_add_text(struct pdf_doc *pdf, struct pdf_object *page,
  385. const char *text, float size, float xoff, float yoff,
  386. uint32_t colour);
  387. /**
  388. * Add a text string to the document at a rotated angle
  389. * @param pdf PDF document to add to
  390. * @param page Page to add object to (NULL => most recently added page)
  391. * @param text String to display
  392. * @param size Point size of the font
  393. * @param xoff X location to put it in
  394. * @param yoff Y location to put it in
  395. * @param angle Rotation angle of text (in radians)
  396. * @param colour Colour to draw the text
  397. * @return 0 on success, < 0 on failure
  398. */
  399. int pdf_add_text_rotate(struct pdf_doc *pdf, struct pdf_object *page,
  400. const char *text, float size, float xoff, float yoff,
  401. float angle, uint32_t colour);
  402. /**
  403. * Add a text string to the document, making it wrap if it is too
  404. * long
  405. * @param pdf PDF document to add to
  406. * @param page Page to add object to (NULL => most recently added page)
  407. * @param text String to display
  408. * @param size Point size of the font
  409. * @param xoff X location to put it in
  410. * @param yoff Y location to put it in
  411. * @param angle Rotation angle of text (in radians)
  412. * @param colour Colour to draw the text
  413. * @param wrap_width Width at which to wrap the text
  414. * @param align Text alignment (see PDF_ALIGN_xxx)
  415. * @param height Store the final height of the wrapped text here (optional)
  416. * @return < 0 on failure, >= 0 on success
  417. */
  418. int pdf_add_text_wrap(struct pdf_doc *pdf, struct pdf_object *page,
  419. const char *text, float size, float xoff, float yoff,
  420. float angle, uint32_t colour, float wrap_width,
  421. int align, float *height);
  422. /**
  423. * Add a line to the document
  424. * @param pdf PDF document to add to
  425. * @param page Page to add object to (NULL => most recently added page)
  426. * @param x1 X offset of start of line
  427. * @param y1 Y offset of start of line
  428. * @param x2 X offset of end of line
  429. * @param y2 Y offset of end of line
  430. * @param width Width of the line
  431. * @param colour Colour to draw the line
  432. * @return 0 on success, < 0 on failure
  433. */
  434. int pdf_add_line(struct pdf_doc *pdf, struct pdf_object *page, float x1,
  435. float y1, float x2, float y2, float width, uint32_t colour);
  436. /**
  437. * Add a cubic bezier curve to the document
  438. * @param pdf PDF document to add to
  439. * @param page Page to add object to (NULL => most recently added page)
  440. * @param x1 X offset of the initial point of the curve
  441. * @param y1 Y offset of the initial point of the curve
  442. * @param x2 X offset of the final point of the curve
  443. * @param y2 Y offset of the final point of the curve
  444. * @param xq1 X offset of the first control point of the curve
  445. * @param yq1 Y offset of the first control point of the curve
  446. * @param xq2 X offset of the second control of the curve
  447. * @param yq2 Y offset of the second control of the curve
  448. * @param width Width of the curve
  449. * @param colour Colour to draw the curve
  450. * @return 0 on success, < 0 on failure
  451. */
  452. int pdf_add_cubic_bezier(struct pdf_doc *pdf, struct pdf_object *page,
  453. float x1, float y1, float x2, float y2, float xq1,
  454. float yq1, float xq2, float yq2, float width,
  455. uint32_t colour);
  456. /**
  457. * Add a quadratic bezier curve to the document
  458. * @param pdf PDF document to add to
  459. * @param page Page to add object to (NULL => most recently added page)
  460. * @param x1 X offset of the initial point of the curve
  461. * @param y1 Y offset of the initial point of the curve
  462. * @param x2 X offset of the final point of the curve
  463. * @param y2 Y offset of the final point of the curve
  464. * @param xq1 X offset of the control point of the curve
  465. * @param yq1 Y offset of the control point of the curve
  466. * @param width Width of the curve
  467. * @param colour Colour to draw the curve
  468. * @return 0 on success, < 0 on failure
  469. */
  470. int pdf_add_quadratic_bezier(struct pdf_doc *pdf, struct pdf_object *page,
  471. float x1, float y1, float x2, float y2,
  472. float xq1, float yq1, float width,
  473. uint32_t colour);
  474. /**
  475. * Add a custom path to the document
  476. * @param pdf PDF document to add to
  477. * @param page Page to add object to (NULL => most recently added page)
  478. * @param operations Array of drawing operations
  479. * @param operation_count The number of operations
  480. * @param stroke_width Width of the stroke
  481. * @param stroke_colour Colour to stroke the curve
  482. * @param fill_colour Colour to fill the path
  483. * @return 0 on success, < 0 on failure
  484. */
  485. int pdf_add_custom_path(struct pdf_doc *pdf, struct pdf_object *page,
  486. const struct pdf_path_operation *operations,
  487. int operation_count, float stroke_width,
  488. uint32_t stroke_colour, uint32_t fill_colour);
  489. /**
  490. * Add an ellipse to the document
  491. * @param pdf PDF document to add to
  492. * @param page Page to add object to (NULL => most recently added page)
  493. * @param x X offset of the center of the ellipse
  494. * @param y Y offset of the center of the ellipse
  495. * @param xradius Radius of the ellipse in the X axis
  496. * @param yradius Radius of the ellipse in the Y axis
  497. * @param width Width of the ellipse outline stroke
  498. * @param colour Colour to draw the ellipse outline stroke
  499. * @param fill_colour Colour to fill the ellipse
  500. * @return 0 on success, < 0 on failure
  501. */
  502. int pdf_add_ellipse(struct pdf_doc *pdf, struct pdf_object *page, float x,
  503. float y, float xradius, float yradius, float width,
  504. uint32_t colour, uint32_t fill_colour);
  505. /**
  506. * Add a circle to the document
  507. * @param pdf PDF document to add to
  508. * @param page Page to add object to (NULL => most recently added page)
  509. * @param x X offset of the center of the circle
  510. * @param y Y offset of the center of the circle
  511. * @param radius Radius of the circle
  512. * @param width Width of the circle outline stroke
  513. * @param colour Colour to draw the circle outline stroke
  514. * @param fill_colour Colour to fill the circle
  515. * @return 0 on success, < 0 on failure
  516. */
  517. int pdf_add_circle(struct pdf_doc *pdf, struct pdf_object *page, float x,
  518. float y, float radius, float width, uint32_t colour,
  519. uint32_t fill_colour);
  520. /**
  521. * Add an outline rectangle to the document
  522. * @param pdf PDF document to add to
  523. * @param page Page to add object to (NULL => most recently added page)
  524. * @param x X offset to start rectangle at
  525. * @param y Y offset to start rectangle at
  526. * @param width Width of rectangle
  527. * @param height Height of rectangle
  528. * @param border_width Width of rectangle border
  529. * @param colour Colour to draw the rectangle
  530. * @return 0 on success, < 0 on failure
  531. */
  532. int pdf_add_rectangle(struct pdf_doc *pdf, struct pdf_object *page, float x,
  533. float y, float width, float height, float border_width,
  534. uint32_t colour);
  535. /**
  536. * Add a filled rectangle to the document
  537. * @param pdf PDF document to add to
  538. * @param page Page to add object to (NULL => most recently added page)
  539. * @param x X offset to start rectangle at
  540. * @param y Y offset to start rectangle at
  541. * @param width Width of rectangle
  542. * @param height Height of rectangle
  543. * @param border_width Width of rectangle border
  544. * @param colour_fill Colour to fill the rectangle
  545. * @param colour_border Colour to draw the rectangle
  546. * @return 0 on success, < 0 on failure
  547. */
  548. int pdf_add_filled_rectangle(struct pdf_doc *pdf, struct pdf_object *page,
  549. float x, float y, float width, float height,
  550. float border_width, uint32_t colour_fill,
  551. uint32_t colour_border);
  552. /**
  553. * Add an outline polygon to the document
  554. * @param pdf PDF document to add to
  555. * @param page Page to add object to (NULL => most recently added page)
  556. * @param x array of X offsets for points comprising the polygon
  557. * @param y array of Y offsets for points comprising the polygon
  558. * @param count Number of points comprising the polygon
  559. * @param border_width Width of polygon border
  560. * @param colour Colour to draw the polygon
  561. * @return 0 on success, < 0 on failure
  562. */
  563. int pdf_add_polygon(struct pdf_doc *pdf, struct pdf_object *page, float x[],
  564. float y[], int count, float border_width,
  565. uint32_t colour);
  566. /**
  567. * Add a filled polygon to the document
  568. * @param pdf PDF document to add to
  569. * @param page Page to add object to (NULL => most recently added page)
  570. * @param x array of X offsets of points comprising the polygon
  571. * @param y array of Y offsets of points comprising the polygon
  572. * @param count Number of points comprising the polygon
  573. * @param border_width Width of polygon border
  574. * @param colour Colour to draw the polygon
  575. * @return 0 on success, < 0 on failure
  576. */
  577. int pdf_add_filled_polygon(struct pdf_doc *pdf, struct pdf_object *page,
  578. float x[], float y[], int count,
  579. float border_width, uint32_t colour);
  580. /**
  581. * Add a bookmark to the document
  582. * @param pdf PDF document to add bookmark to
  583. * @param page Page to jump to for bookmark
  584. (or NULL for the most recently added page)
  585. * @param parent ID of a previously created bookmark that is the parent
  586. of this one. -1 if this should be a top-level bookmark.
  587. * @param name String to associate with the bookmark
  588. * @return < 0 on failure, new bookmark id on success
  589. */
  590. int pdf_add_bookmark(struct pdf_doc *pdf, struct pdf_object *page, int parent,
  591. const char *name);
  592. /**
  593. * Add a link annotation to the document
  594. * @param pdf PDF document to add link to
  595. * @param page Page that holds the clickable rectangle
  596. (or NULL for the most recently added page)
  597. * @param x X coordinate of bottom LHS corner of clickable rectangle
  598. * @param y Y coordinate of bottom LHS corner of clickable rectangle
  599. * @param width width of clickable rectangle
  600. * @param height height of clickable rectangle
  601. * @param target_page Page to jump to for link
  602. * @param target_x X coordinate to position at the left of the view
  603. * @param target_y Y coordinate to position at the top of the view
  604. * @return < 0 on failure, new bookmark id on success
  605. */
  606. int pdf_add_link(struct pdf_doc *pdf, struct pdf_object *page, float x,
  607. float y, float width, float height,
  608. struct pdf_object *target_page, float target_x,
  609. float target_y);
  610. /**
  611. * List of different barcode encodings that are supported
  612. */
  613. enum {
  614. PDF_BARCODE_128A, //!< Produce code-128A style barcodes
  615. PDF_BARCODE_39, //!< Produce code-39 style barcodes
  616. PDF_BARCODE_EAN13, //!< Produce EAN-13 style barcodes
  617. PDF_BARCODE_UPCA, //!< Produce UPC-A style barcodes
  618. PDF_BARCODE_EAN8, //!< Produce EAN-8 style barcodes
  619. PDF_BARCODE_UPCE, //!< Produce UPC-E style barcodes
  620. };
  621. /**
  622. * Add a barcode to the document
  623. * @param pdf PDF document to add barcode to
  624. * @param page Page to add barcode to (NULL => most recently added page)
  625. * @param code Type of barcode to add (PDF_BARCODE_xxx)
  626. * @param x X offset to put barcode at
  627. * @param y Y offset to put barcode at
  628. * @param width Width of barcode
  629. * @param height Height of barcode
  630. * @param string Barcode contents
  631. * @param colour Colour to draw barcode
  632. * @return < 0 on failure, >= 0 on success
  633. */
  634. int pdf_add_barcode(struct pdf_doc *pdf, struct pdf_object *page, int code,
  635. float x, float y, float width, float height,
  636. const char *string, uint32_t colour);
  637. /**
  638. * Add image data as an image to the document.
  639. * Image data must be one of: JPEG, PNG, PPM, PGM or BMP formats
  640. * Passing 0 for either the display width or height will
  641. * include the image but not render it visible.
  642. * Passing a negative number either the display height or width will
  643. * have the image be resized while keeping the original aspect ratio.
  644. * @param pdf PDF document to add image to
  645. * @param page Page to add image to (NULL => most recently added page)
  646. * @param x X offset to put image at
  647. * @param y Y offset to put image at
  648. * @param display_width Displayed width of image
  649. * @param display_height Displayed height of image
  650. * @param data Image data bytes
  651. * @param len Length of data
  652. * @return < 0 on failure, >= 0 on success
  653. */
  654. int pdf_add_image_data(struct pdf_doc *pdf, struct pdf_object *page, float x,
  655. float y, float display_width, float display_height,
  656. const uint8_t *data, size_t len);
  657. /**
  658. * Add a raw 24 bit per pixel RGB buffer as an image to the document
  659. * Passing 0 for either the display width or height will
  660. * include the image but not render it visible.
  661. * Passing a negative number either the display height or width will
  662. * have the image be resized while keeping the original aspect ratio.
  663. * @param pdf PDF document to add image to
  664. * @param page Page to add image to (NULL => most recently added page)
  665. * @param x X offset to put image at
  666. * @param y Y offset to put image at
  667. * @param display_width Displayed width of image
  668. * @param display_height Displayed height of image
  669. * @param data RGB data to add
  670. * @param width width of image in pixels
  671. * @param height height of image in pixels
  672. * @return < 0 on failure, >= 0 on success
  673. */
  674. int pdf_add_rgb24(struct pdf_doc *pdf, struct pdf_object *page, float x,
  675. float y, float display_width, float display_height,
  676. const uint8_t *data, uint32_t width, uint32_t height);
  677. /**
  678. * Add a raw 8 bit per pixel grayscale buffer as an image to the document
  679. * @param pdf PDF document to add image to
  680. * @param page Page to add image to (NULL => most recently added page)
  681. * @param x X offset to put image at
  682. * @param y Y offset to put image at
  683. * @param display_width Displayed width of image
  684. * @param display_height Displayed height of image
  685. * @param data grayscale pixel data to add
  686. * @param width width of image in pixels
  687. * @param height height of image in pixels
  688. * @return < 0 on failure, >= 0 on success
  689. */
  690. int pdf_add_grayscale8(struct pdf_doc *pdf, struct pdf_object *page, float x,
  691. float y, float display_width, float display_height,
  692. const uint8_t *data, uint32_t width, uint32_t height);
  693. /**
  694. * Add an image file as an image to the document.
  695. * Passing 0 for either the display width or height will
  696. * include the image but not render it visible.
  697. * Passing a negative number either the display height or width will
  698. * have the image be resized while keeping the original aspect ratio.
  699. * Supports image formats: JPEG, PNG, PPM, PGM & BMP
  700. * @param pdf PDF document to add bookmark to
  701. * @param page Page to add image to (NULL => most recently added page)
  702. * @param x X offset to put image at
  703. * @param y Y offset to put image at
  704. * @param display_width Displayed width of image
  705. * @param display_height Displayed height of image
  706. * @param image_filename Filename of image file to display
  707. * @return < 0 on failure, >= 0 on success
  708. */
  709. int pdf_add_image_file(struct pdf_doc *pdf, struct pdf_object *page, float x,
  710. float y, float display_width, float display_height,
  711. const char *image_filename);
  712. /**
  713. * Parse image data to determine the image type & metadata
  714. * @param info structure to hold the parsed metadata
  715. * @param data image data to parse
  716. * @param length number of bytes in data
  717. * @param err_msg area to put any failure details
  718. * @param err_msg_length maximum number of bytes to store in err_msg
  719. * @return < 0 on failure, >= 0 on success
  720. */
  721. int pdf_parse_image_header(struct pdf_img_info *info, const uint8_t *data,
  722. size_t length, char *err_msg,
  723. size_t err_msg_length);
  724. #ifdef __cplusplus
  725. }
  726. #endif
  727. #endif // PDFGEN_H