mirror of
https://github.com/michaelrsweet/pdfio.git
synced 2025-07-13 14:34:28 +02:00
Compare commits
73 Commits
v1.2.0
...
a39b01ec9c
Author | SHA1 | Date | |
---|---|---|---|
a39b01ec9c | |||
4b29c9a1c2 | |||
5a4afad566 | |||
7a45adb7f5 | |||
45ac66874c | |||
eb9dad9b51 | |||
2ecb9cfb2d | |||
91a467e55c | |||
d705d7eb5d | |||
55745bcea8 | |||
2ea99597cc | |||
a3a3512ed8 | |||
afac83530f | |||
21ac2b52d1 | |||
21b8e3b06f | |||
91392a931f | |||
1d8bcf4d73 | |||
1e55779906 | |||
0e45e49ea4 | |||
0ab291a78b | |||
cac6d4891c | |||
4f29ad89da | |||
9c04d1dc20 | |||
335472023e | |||
25834e07ef | |||
2d2a7126d2 | |||
df1064ff39 | |||
853fa4fe8f | |||
2cadfd8a1e | |||
f5d40a305e | |||
eb5be57b4a | |||
3de47ea63d | |||
8f2c47cb07 | |||
74dfefdcc1 | |||
ee31096019 | |||
121b933307 | |||
f4409146e3 | |||
4312933409 | |||
a19949834b | |||
04c4f44324 | |||
206f75403a | |||
7d22477917 | |||
7c3651671b | |||
6cb661f0f4 | |||
7e01451b18 | |||
138f3955d1 | |||
82844ad2ce | |||
d7cce4dfbc | |||
1cec42f399 | |||
f3f70e7877 | |||
90923c3818 | |||
986cc512cd | |||
c35ddbec00 | |||
e4e1c39578 | |||
1d4f77cab1 | |||
b035130cde | |||
d6d5813b04 | |||
6492f210cf | |||
207062a996 | |||
7d37abb0df | |||
0c1122b689 | |||
d4f8dd46b5 | |||
986c5f0438 | |||
a81907bdb9 | |||
63a7a2cdbd | |||
f040cc41c2 | |||
23883268e3 | |||
a1e14503fd | |||
0766869ad1 | |||
6c1db141a1 | |||
b117959725 | |||
e882622233 | |||
c13b5a5e90 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -60,3 +60,5 @@ jobs:
|
||||
run: nuget restore pdfio.sln
|
||||
- name: Build PDFio
|
||||
run: msbuild pdfio.sln
|
||||
- name: Test PDFio
|
||||
run: .\runtests.bat x64\Debug
|
||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
*.1.dylib
|
||||
*.a
|
||||
*.dSYM
|
||||
*.log
|
||||
*.o
|
||||
*.so.1
|
||||
@ -8,7 +9,10 @@
|
||||
/autom4te.cache
|
||||
/config.log
|
||||
/config.status
|
||||
/configure~
|
||||
/doc/pdfio.epub
|
||||
/examples/code128
|
||||
/examples/md2pdf
|
||||
/Makefile
|
||||
/packages
|
||||
/pdfio.pc
|
||||
|
83
CHANGES.md
83
CHANGES.md
@ -2,8 +2,47 @@ Changes in PDFio
|
||||
================
|
||||
|
||||
|
||||
v1.2.0 (January 24, 2024)
|
||||
-------------------------
|
||||
v1.4.0 - YYYY-MM-DD
|
||||
-------------------
|
||||
|
||||
- Added new `pdfioDictGetKey` and `pdfioDictGetNumPairs` APIs (Issue #63)
|
||||
- Added new `pdfioArrayRemove` and `pdfioDictClear` APIs (Issue #74)
|
||||
- Added new `pdfioFileCreateNameObj` and `pdfioObjGetName` APIs for creating and
|
||||
getting name object values (Issue #76)
|
||||
- Updated documentation (Issue #78)
|
||||
- Fixed reading of PDF files whose trailer is missing a newline (Issue #80)
|
||||
- Fixed builds with some versions of VC++ (Issue #81)
|
||||
|
||||
|
||||
v1.3.2 - 2024-08-15
|
||||
-------------------
|
||||
|
||||
- Added some more sanity checks to the TrueType font reader.
|
||||
- Updated documentation (Issue #77)
|
||||
- Fixed an issue when opening certain encrypted PDF files (Issue #62)
|
||||
|
||||
|
||||
v1.3.1 - 2024-08-05
|
||||
-------------------
|
||||
|
||||
- CVE 2024-42358: Updated TrueType font reader to avoid large memory
|
||||
allocations.
|
||||
- Fixed some documentation errors and added examples (Issue #68, Issue #69)
|
||||
|
||||
|
||||
v1.3.0 - 2024-06-28
|
||||
-------------------
|
||||
|
||||
- Added `pdfioFileGetCatalog` API for accessing the root/catalog object of a
|
||||
PDF file (Issue #67)
|
||||
- Updated number support to avoid locale issues (Issue #61)
|
||||
- Updated the PDFio private header to allow compilation with MingW; note that
|
||||
MingW is NOT a supported toolchain for PDFio (Issue #66)
|
||||
- Optimized string pool code.
|
||||
|
||||
|
||||
v1.2.0 - 2024-01-24
|
||||
-------------------
|
||||
|
||||
- Now use autoconf to configure the PDFio sources (Issue #54)
|
||||
- Added `pdfioFileCreateNumberObj` and `pdfioFileCreateStringObj` functions
|
||||
@ -26,8 +65,8 @@ v1.2.0 (January 24, 2024)
|
||||
65536 in the xref table (Issue #59)
|
||||
|
||||
|
||||
v1.1.4 (December 3, 2023)
|
||||
-------------------------
|
||||
v1.1.4 - 2023-12-03
|
||||
-------------------
|
||||
|
||||
- Fixed detection of encrypted strings that are too short (Issue #52)
|
||||
- Fixed a TrueType CMAP decoding bug.
|
||||
@ -35,15 +74,15 @@ v1.1.4 (December 3, 2023)
|
||||
- Added a ToUnicode map for Unicode text to support text copying.
|
||||
|
||||
|
||||
v1.1.3 (November 15, 2023)
|
||||
--------------------------
|
||||
v1.1.3 - 2023-11-15
|
||||
-------------------
|
||||
|
||||
- Fixed Unicode font support (Issue #16)
|
||||
- Fixed missing initializer for 40-bit RC4 encryption (Issue #51)
|
||||
|
||||
|
||||
v1.1.2 (October 10, 2023)
|
||||
-------------------------
|
||||
v1.1.2 - 2023-10-10
|
||||
-------------------
|
||||
|
||||
- Updated `pdfioContentSetDashPattern` to support setting a solid (0 length)
|
||||
dash pattern (Issue #41)
|
||||
@ -58,15 +97,15 @@ v1.1.2 (October 10, 2023)
|
||||
(Issue #48)
|
||||
|
||||
|
||||
v1.1.1 (March 20, 2023)
|
||||
-----------------------
|
||||
v1.1.1 - 2023-03-20
|
||||
-------------------
|
||||
|
||||
- CVE-2023-28428: Fixed a potential denial-of-service with corrupt PDF files.
|
||||
- Fixed a few build issues.
|
||||
|
||||
|
||||
v1.1.0 (February 6, 2023)
|
||||
-------------------------
|
||||
v1.1.0 - 2023-02-06
|
||||
-------------------
|
||||
|
||||
- CVE-2023-24808: Fixed a potential denial-of-service with corrupt PDF files.
|
||||
- Added `pdfioFileCreateTemporary` function (Issue #29)
|
||||
@ -80,28 +119,28 @@ v1.1.0 (February 6, 2023)
|
||||
- Fixed `pdfioContentMatrixRotate` function.
|
||||
|
||||
|
||||
v1.0.1 (March 2, 2022)
|
||||
----------------------
|
||||
v1.0.1 - 2022-03-02
|
||||
-------------------
|
||||
|
||||
- Added missing `pdfioPageGetNumStreams` and `pdfioPageOpenStream` functions.
|
||||
- Added demo pdfiototext utility.
|
||||
- Fixed bug in `pdfioStreamGetToken`.
|
||||
|
||||
|
||||
v1.0.0 (December 14, 2021)
|
||||
--------------------------
|
||||
v1.0.0 - 2021-12-14
|
||||
-------------------
|
||||
|
||||
- First stable release.
|
||||
|
||||
|
||||
v1.0rc1 (November 30, 2021)
|
||||
---------------------------
|
||||
v1.0rc1 - 2021-11-30
|
||||
--------------------
|
||||
|
||||
- Fixed a few stack/buffer overflow bugs discovered via fuzzing.
|
||||
|
||||
|
||||
v1.0b2 (November 7, 2021)
|
||||
-------------------------
|
||||
v1.0b2 - 2021-11-07
|
||||
-------------------
|
||||
|
||||
- Added `pdfioFileCreateOutput` API to support streaming output of PDF
|
||||
(Issue #21)
|
||||
@ -112,7 +151,7 @@ v1.0b2 (November 7, 2021)
|
||||
- Fixed some issues identified by a Coverity scan.
|
||||
|
||||
|
||||
v1.0b1 (August 30, 2021)
|
||||
------------------------
|
||||
v1.0b1 - 2021-08-30
|
||||
-------------------
|
||||
|
||||
- Initial release
|
||||
|
18
EXAMPLES.md
Normal file
18
EXAMPLES.md
Normal file
@ -0,0 +1,18 @@
|
||||
PDFio Examples
|
||||
==============
|
||||
|
||||
The "examples" subdirectory contains example code showing how to do different
|
||||
things with PDFio.
|
||||
|
||||
|
||||
code128.c
|
||||
---------
|
||||
|
||||
This example shows how to embed and use a barcode font.
|
||||
|
||||
|
||||
md2pdf.c
|
||||
--------
|
||||
|
||||
This example shows how to generate pages with multiple fonts, embedded images,
|
||||
and headers and footers.
|
@ -1,7 +1,7 @@
|
||||
#
|
||||
# Makefile for PDFio.
|
||||
#
|
||||
# Copyright © 2021-2023 by Michael R Sweet.
|
||||
# Copyright © 2021-2024 by Michael R Sweet.
|
||||
#
|
||||
# Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
# information.
|
||||
@ -163,7 +163,9 @@ install: $(TARGETS)
|
||||
# Test everything
|
||||
test: testpdfio testttf
|
||||
./testttf 2>test.log
|
||||
./testpdfio 2>test.log
|
||||
./testpdfio 2>>test.log
|
||||
LANG=fr_FR.UTF-8 ./testpdfio 2>>test.log
|
||||
|
||||
|
||||
valgrind: testpdfio
|
||||
valgrind --leak-check=full ./testpdfio
|
||||
@ -256,5 +258,5 @@ clang:
|
||||
|
||||
# Analyze code using Cppcheck <http://cppcheck.sourceforge.net>
|
||||
cppcheck:
|
||||
cppcheck $(CPPFLAGS) --template=gcc --addon=cert.py --suppressions-list=.cppcheck $(OBJS:.o=.c) 2>cppcheck.log
|
||||
cppcheck $(CPPFLAGS) --template=gcc --suppressions-list=.cppcheck $(OBJS:.o=.c) 2>cppcheck.log
|
||||
test -s cppcheck.log && (echo "$(GHA_ERROR)Cppcheck detected issues."; echo ""; cat cppcheck.log; exit 1) || exit 0
|
||||
|
27
configure
vendored
27
configure
vendored
@ -1,6 +1,6 @@
|
||||
#! /bin/sh
|
||||
# Guess values for system-dependent variables and create Makefiles.
|
||||
# Generated by GNU Autoconf 2.71 for pdfio 1.2.0.
|
||||
# Generated by GNU Autoconf 2.71 for pdfio 1.4.0.
|
||||
#
|
||||
# Report bugs to <https://github.com/michaelrsweet/pdfio/issues>.
|
||||
#
|
||||
@ -610,8 +610,8 @@ MAKEFLAGS=
|
||||
# Identity of this package.
|
||||
PACKAGE_NAME='pdfio'
|
||||
PACKAGE_TARNAME='pdfio'
|
||||
PACKAGE_VERSION='1.2.0'
|
||||
PACKAGE_STRING='pdfio 1.2.0'
|
||||
PACKAGE_VERSION='1.4.0'
|
||||
PACKAGE_STRING='pdfio 1.4.0'
|
||||
PACKAGE_BUGREPORT='https://github.com/michaelrsweet/pdfio/issues'
|
||||
PACKAGE_URL='https://www.msweet.org/pdfio'
|
||||
|
||||
@ -1293,7 +1293,7 @@ if test "$ac_init_help" = "long"; then
|
||||
# Omit some internal or obsolete options to make the list less imposing.
|
||||
# This message is too long to be a string in the A/UX 3.1 sh.
|
||||
cat <<_ACEOF
|
||||
\`configure' configures pdfio 1.2.0 to adapt to many kinds of systems.
|
||||
\`configure' configures pdfio 1.4.0 to adapt to many kinds of systems.
|
||||
|
||||
Usage: $0 [OPTION]... [VAR=VALUE]...
|
||||
|
||||
@ -1359,7 +1359,7 @@ fi
|
||||
|
||||
if test -n "$ac_init_help"; then
|
||||
case $ac_init_help in
|
||||
short | recursive ) echo "Configuration of pdfio 1.2.0:";;
|
||||
short | recursive ) echo "Configuration of pdfio 1.4.0:";;
|
||||
esac
|
||||
cat <<\_ACEOF
|
||||
|
||||
@ -1456,7 +1456,7 @@ fi
|
||||
test -n "$ac_init_help" && exit $ac_status
|
||||
if $ac_init_version; then
|
||||
cat <<\_ACEOF
|
||||
pdfio configure 1.2.0
|
||||
pdfio configure 1.4.0
|
||||
generated by GNU Autoconf 2.71
|
||||
|
||||
Copyright (C) 2021 Free Software Foundation, Inc.
|
||||
@ -1612,7 +1612,7 @@ cat >config.log <<_ACEOF
|
||||
This file contains any messages produced by compilers while
|
||||
running configure, to aid debugging if configure makes a mistake.
|
||||
|
||||
It was created by pdfio $as_me 1.2.0, which was
|
||||
It was created by pdfio $as_me 1.4.0, which was
|
||||
generated by GNU Autoconf 2.71. Invocation command line was
|
||||
|
||||
$ $0$ac_configure_args_raw
|
||||
@ -2368,9 +2368,9 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
|
||||
|
||||
|
||||
|
||||
PDFIO_VERSION="1.2.0"
|
||||
PDFIO_VERSION_MAJOR="`echo 1.2.0 | awk -F. '{print $1}'`"
|
||||
PDFIO_VERSION_MINOR="`echo 1.2.0 | awk -F. '{printf("%d\n",$2);}'`"
|
||||
PDFIO_VERSION="1.4.0"
|
||||
PDFIO_VERSION_MAJOR="`echo 1.4.0 | awk -F. '{print $1}'`"
|
||||
PDFIO_VERSION_MINOR="`echo 1.4.0 | awk -F. '{printf("%d\n",$2);}'`"
|
||||
|
||||
|
||||
|
||||
@ -4029,8 +4029,8 @@ then :
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
|
||||
printf "%s\n" "yes" >&6; }
|
||||
LIBS="$($PKGCONFIG --libs zlib) $LIBS"
|
||||
CPPFLAGS="$($PKGCONFIG --cflags zlib) $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs zlib) $LIBS"
|
||||
|
||||
else $as_nop
|
||||
|
||||
@ -4093,6 +4093,7 @@ then :
|
||||
|
||||
fi
|
||||
|
||||
PKGCONFIG_REQUIRES=""
|
||||
PKGCONFIG_LIBS_PRIVATE="-lz $PKGCONFIG_LIBS_PRIVATE"
|
||||
|
||||
fi
|
||||
@ -4934,7 +4935,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
|
||||
# report actual input values of CONFIG_FILES etc. instead of their
|
||||
# values after options handling.
|
||||
ac_log="
|
||||
This file was extended by pdfio $as_me 1.2.0, which was
|
||||
This file was extended by pdfio $as_me 1.4.0, which was
|
||||
generated by GNU Autoconf 2.71. Invocation command line was
|
||||
|
||||
CONFIG_FILES = $CONFIG_FILES
|
||||
@ -4990,7 +4991,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\
|
||||
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
|
||||
ac_cs_config='$ac_cs_config_escaped'
|
||||
ac_cs_version="\\
|
||||
pdfio config.status 1.2.0
|
||||
pdfio config.status 1.4.0
|
||||
configured by $0, generated by GNU Autoconf 2.71,
|
||||
with options \\"\$ac_cs_config\\"
|
||||
|
||||
|
@ -21,7 +21,7 @@ AC_PREREQ([2.70])
|
||||
|
||||
|
||||
dnl Package name and version...
|
||||
AC_INIT([pdfio], [1.2.0], [https://github.com/michaelrsweet/pdfio/issues], [pdfio], [https://www.msweet.org/pdfio])
|
||||
AC_INIT([pdfio], [1.4.0], [https://github.com/michaelrsweet/pdfio/issues], [pdfio], [https://www.msweet.org/pdfio])
|
||||
|
||||
PDFIO_VERSION="AC_PACKAGE_VERSION"
|
||||
PDFIO_VERSION_MAJOR="`echo AC_PACKAGE_VERSION | awk -F. '{print $1}'`"
|
||||
|
458
doc/pdfio.3
458
doc/pdfio.3
@ -1,4 +1,4 @@
|
||||
.TH pdfio 3 "pdf read/write library" "2024-01-24" "pdf read/write library"
|
||||
.TH pdfio 3 "pdf read/write library" "2024-10-25" "pdf read/write library"
|
||||
.SH NAME
|
||||
pdfio \- pdf read/write library
|
||||
.SH Introduction
|
||||
@ -138,6 +138,121 @@ PDFio also provides PDF content helper functions for producing PDF content that
|
||||
|
||||
#include <pdfio\-content.h>
|
||||
.fi
|
||||
.SS Understanding PDF Files
|
||||
.PP
|
||||
A PDF file provides data and commands for displaying pages of graphics and text, and is structured in a way that allows it to be displayed in the same way across multiple devices and platforms. The following is a PDF which shows "Hello, World!" on one page:
|
||||
.nf
|
||||
|
||||
%PDF\-1.0 % Header starts here
|
||||
%âãÏÓ
|
||||
1 0 obj % Body starts here
|
||||
<<
|
||||
/Kids [2 0 R]
|
||||
/Count 1
|
||||
/Type /Pages
|
||||
.fi
|
||||
.PP
|
||||
> endobj 2 0 obj <
|
||||
/Rotate 0
|
||||
/Parent 1 0 R
|
||||
/Resources 3 0 R
|
||||
/MediaBox [0 0 612 792]
|
||||
/Contents [4 0 R]/Type /Page
|
||||
endobj 3 0 obj <
|
||||
/Font
|
||||
<<
|
||||
/F0
|
||||
<<
|
||||
/BaseFont /Times\-Italic
|
||||
/Subtype /Type1
|
||||
/Type /Font
|
||||
> > endobj 4 0 obj <
|
||||
/Length 65
|
||||
stream
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
0. 0. 1. 50. 700. cm BT /F0 36. Tf (Hello, World!) Tj ET endstream endobj 5 0 obj << /Pages 1 0 R /Type /Catalog
|
||||
|
||||
|
||||
.PP
|
||||
> endobj xref % Cross\-reference table starts here 0 6 0000000000 65535 f 0000000015 00000 n 0000000074 00000 n 0000000192 00000 n 0000000291 00000 n 0000000409 00000 n trailer % Trailer starts here << /Root 5 0 R /Size 6 > startxref 459 %%EOF
|
||||
.nf
|
||||
|
||||
.fi
|
||||
|
||||
.PP
|
||||
Header
|
||||
.PP
|
||||
The header is the first line of a PDF file that specifies the version of the PDF format that has been used, for example %PDF\-1.0\.
|
||||
.PP
|
||||
Since PDF files almost always contain binary data, they can become corrupted if line endings are changed. For example, if the file is transferred using FTP in text mode or is edited in Notepad on Windows. To allow legacy file transfer programs to determine that the file is binary, the PDF standard recommends including some bytes with character codes higher than 127 in the header, for example:
|
||||
.nf
|
||||
|
||||
%âãÏÓ
|
||||
.fi
|
||||
.PP
|
||||
The percent sign indicates a comment line while the other few bytes are arbitrary character codes in excess of 127. So, the whole header in our example is:
|
||||
.nf
|
||||
|
||||
%PDF\-1.0
|
||||
%âãÏÓ
|
||||
.fi
|
||||
.PP
|
||||
Body
|
||||
.PP
|
||||
The file body consists of a sequence of objects, each preceded by an object number, generation number, and the obj keyword on one line, and followed by the endobj keyword on another. For example:
|
||||
.nf
|
||||
|
||||
1 0 obj
|
||||
<<
|
||||
/Kids [2 0 R]
|
||||
/Count 1
|
||||
/Type /Pages
|
||||
.fi
|
||||
.PP
|
||||
> endobj
|
||||
.nf
|
||||
|
||||
.fi
|
||||
|
||||
.PP
|
||||
In this example, the object number is 1 and the generation number is 0, meaning it is the first version of the object. The content for object 1 is between the initial 1 0 obj and trailing endobj lines. In this case, the content is the dictionary <</Kids [2 0 R] /Count 1 /Type /Pages>>\.
|
||||
.PP
|
||||
Cross\-Reference Table
|
||||
.PP
|
||||
The cross\-reference table lists the byte offset of each object in the file body. This allows random access to objects, meaning they don't have to be read in order. Objects that are not used are never read, making the process efficient. Operations like counting the number of pages in a PDF document are fast, even in large files.
|
||||
.PP
|
||||
Each object has an object number and a generation number. Generation numbers are used when a cross\-reference table entry is reused. For simplicity, we will assume generation numbers to be always zero and ignore them. The cross\-reference table consists of a header line that indicates the number of entries, a free entry line for object 0, and a line for each of the objects in the file body. For example:
|
||||
.nf
|
||||
|
||||
0 6 % Six entries in table, starting at 0
|
||||
0000000000 65535 f % Free entry for object 0
|
||||
0000000015 00000 n % Object 1 is at byte offset 15
|
||||
0000000074 00000 n % Object 2 is at byte offset 74
|
||||
0000000192 00000 n % etc...
|
||||
0000000291 00000 n
|
||||
0000000409 00000 n % Object 5 is at byte offset 409
|
||||
.fi
|
||||
.PP
|
||||
Trailer
|
||||
.PP
|
||||
The first line of the trailer is just the trailer keyword. This is followed by the trailer dictionary which contains at least the /Size entry specifying the number of entries in the cross\-reference table and the /Root entry which references the object for the document catalog which is the root element of the graph of objects in the body.
|
||||
.PP
|
||||
There follows a line with just the startxref keyword, a line with a single number specifying the byte offset of the start of the cross\-reference table within the file, and then the line %%EOF which signals the end of the PDF file.
|
||||
.nf
|
||||
|
||||
trailer % Trailer keyword
|
||||
<< % The trailer dictinonary
|
||||
/Root 5 0 R
|
||||
/Size 6
|
||||
.fi
|
||||
.PP
|
||||
> startxref % startxref keyword 459 % Byte offset of cross\-reference table %%EOF % End\-of\-file marker
|
||||
.nf
|
||||
|
||||
.fi
|
||||
|
||||
.SH API Overview
|
||||
.PP
|
||||
PDFio exposes several types:
|
||||
@ -218,7 +333,90 @@ Each PDF file contains one or more pages. The pdfioFileGetNumPages function retu
|
||||
}
|
||||
.fi
|
||||
.PP
|
||||
Each page is represented by a "page tree" object (what pdfioFileGetPage returns) that specifies information about the page and one or more "content" objects that contain the images, fonts, text, and graphics that appear on the page. Use the pdfioPageGetNumStreams and pdfioPageOpenStream functions to access the content streams for each page.
|
||||
Each page is represented by a "page tree" object (what pdfioFileGetPage returns) that specifies information about the page and one or more "content" objects that contain the images, fonts, text, and graphics that appear on the page. Use the pdfioPageGetNumStreams and pdfioPageOpenStream functions to access the content streams for each page, and pdfioObjGetDict to get the associated page object dictionary. For example, if you want to display the media and crop boxes for a given page:
|
||||
.nf
|
||||
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
size_t count; // Number of pages
|
||||
pdfio_obj_t *page; // Current page
|
||||
pdfio_dict_t *dict; // Current page dictionary
|
||||
pdfio_array_t *media_box; // MediaBox array
|
||||
double media_values[4]; // MediaBox values
|
||||
pdfio_array_t *crop_box; // CropBox array
|
||||
double crop_values[4]; // CropBox values
|
||||
|
||||
// Iterate the pages in the PDF file
|
||||
for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
|
||||
{
|
||||
page = pdfioFileGetPage(pdf, i);
|
||||
dict = pdfioObjGetDict(page);
|
||||
|
||||
media_box = pdfioDictGetArray(dict, "MediaBox");
|
||||
media_values[0] = pdfioArrayGetNumber(media_box, 0);
|
||||
media_values[1] = pdfioArrayGetNumber(media_box, 1);
|
||||
media_values[2] = pdfioArrayGetNumber(media_box, 2);
|
||||
media_values[3] = pdfioArrayGetNumber(media_box, 3);
|
||||
|
||||
crop_box = pdfioDictGetArray(dict, "CropBox");
|
||||
crop_values[0] = pdfioArrayGetNumber(crop_box, 0);
|
||||
crop_values[1] = pdfioArrayGetNumber(crop_box, 1);
|
||||
crop_values[2] = pdfioArrayGetNumber(crop_box, 2);
|
||||
crop_values[3] = pdfioArrayGetNumber(crop_box, 3);
|
||||
|
||||
printf("Page %u: MediaBox=[%g %g %g %g], CropBox=[%g %g %g %g]\\n",
|
||||
(unsigned)(i + 1),
|
||||
media_values[0], media_values[1], media_values[2], media_values[3],
|
||||
crop_values[0], crop_values[1], crop_values[2], crop_values[3]);
|
||||
}
|
||||
.fi
|
||||
.PP
|
||||
Page object dictionaries have several (mostly optional) key/value pairs, including:
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Annots": An array of annotation dictionaries for the page; use pdfioDictGetArray to get the array
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"CropBox": The crop box as an array of four numbers for the left, bottom, right, and top coordinates of the target media; use pdfioDictGetArray to get a pointer to the array of numbers
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Dur": The number of seconds the page should be displayed; use pdfioDictGetNumber to get the page duration value
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Group": The dictionary of transparency group values for the page; use pdfioDictGetDict to get a pointer to the resources dictionary
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"LastModified": The date and time when this page was last modified; use pdfioDictGetDate to get the Unix time_t value
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Parent": The parent page tree node object for this page; use pdfioDictGetObj to get a pointer to the object
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"MediaBox": The media box as an array of four numbers for the left, bottom, right, and top coordinates of the target media; use pdfioDictGetArray to get a pointer to the array of numbers
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Resources": The dictionary of resources for the page; use pdfioDictGetDict to get a pointer to the resources dictionary
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Rotate": A number indicating the number of degrees of counter\-clockwise rotation to apply to the page when viewing; use pdfioDictGetNumber to get the rotation angle
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Thumb": A thumbnail image object for the page; use pdfioDictGetObj to get a pointer to the thumbnail image object
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
"Trans": The page transition dictionary; use pdfioDictGetDict to get a pointer to the dictionary
|
||||
|
||||
|
||||
.PP
|
||||
The pdfioFileClose function closes a PDF file and frees all memory that was used for it:
|
||||
.nf
|
||||
@ -361,7 +559,7 @@ pdfioStreamWrite writes a buffer of data to the stream
|
||||
.PP
|
||||
The PDF content helper functions provide additional functions for writing specific PDF page stream commands.
|
||||
.PP
|
||||
When you are done writing the stream, call pdfioStreamCLose to close both the stream and the object.
|
||||
When you are done writing the stream, call pdfioStreamClose to close both the stream and the object.
|
||||
.SS PDF Content Helper Functions
|
||||
.PP
|
||||
PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into five categories:
|
||||
@ -787,6 +985,125 @@ pdfioContentTextShowf draws a formatted string in a text block
|
||||
pdfioContentTextShowJustified draws an array of literal strings with offsets between them
|
||||
|
||||
|
||||
.SH Examples
|
||||
.SS Read PDF Metadata
|
||||
.PP
|
||||
The following example function will open a PDF file and print the title, author, creation date, and number of pages:
|
||||
.nf
|
||||
|
||||
#include <pdfio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
void
|
||||
show_pdf_info(const char *filename)
|
||||
{
|
||||
pdfio_file_t *pdf;
|
||||
time_t creation_date;
|
||||
struct tm *creation_tm;
|
||||
char creation_text[256];
|
||||
|
||||
|
||||
// Open the PDF file with the default callbacks...
|
||||
pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_cbdata*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
|
||||
if (pdf == NULL)
|
||||
return;
|
||||
|
||||
// Get the creation date and convert to a string...
|
||||
creation_date = pdfioFileGetCreationDate(pdf);
|
||||
creation_tm = localtime(&creation_date);
|
||||
strftime(creation_text, sizeof(creation_text), "%c", &creation_tm);
|
||||
|
||||
// Print file information to stdout...
|
||||
printf("%s:\\n", filename);
|
||||
printf(" Title: %s\\n", pdfioFileGetTitle(pdf));
|
||||
printf(" Author: %s\\n", pdfioFileGetAuthor(pdf));
|
||||
printf(" Created On: %s\\n", creation_text);
|
||||
printf(" Number Pages: %u\\n", (unsigned)pdfioFileGetNumPages(pdf));
|
||||
|
||||
// Close the PDF file...
|
||||
pdfioFileClose(pdf);
|
||||
}
|
||||
.fi
|
||||
.SS Create PDF File With Text and Image
|
||||
.PP
|
||||
The following example function will create a PDF file, embed a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with the text centered below:
|
||||
.nf
|
||||
|
||||
#include <pdfio.h>
|
||||
#include <pdfio\-content.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
void
|
||||
create_pdf_image_file(const char *pdfname, const char *imagename, const char *caption)
|
||||
{
|
||||
pdfio_file_t *pdf;
|
||||
pdfio_obj_t *font;
|
||||
pdfio_obj_t *image;
|
||||
pdfio_dict_t *dict;
|
||||
pdfio_stream_t *page;
|
||||
double width, height;
|
||||
double swidth, sheight;
|
||||
double tx, ty;
|
||||
|
||||
|
||||
// Create the PDF file...
|
||||
pdf = pdfioFileCreate(pdfname, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
|
||||
|
||||
// Create a Courier base font for the caption
|
||||
font = pdfioFileCreateFontObjFromBase(pdf, "Courier");
|
||||
|
||||
// Create an image object from the JPEG/PNG image file...
|
||||
image = pdfioFileCreateImageObjFromFile(pdf, imagename, true);
|
||||
|
||||
// Create a page dictionary with the font and image...
|
||||
dict = pdfioDictCreate(pdf);
|
||||
pdfioPageDictAddFont(dict, "F1", font);
|
||||
pdfioPageDictAddImage(dict, "IM1", image);
|
||||
|
||||
// Create the page and its content stream...
|
||||
page = pdfioFileCreatePage(pdf, dict);
|
||||
|
||||
// Position and scale the image on the page...
|
||||
width = pdfioImageGetWidth(image);
|
||||
height = pdfioImageGetHeight(image);
|
||||
|
||||
// Default media_box is "universal" 595.28x792 points (8.27x11in or 210x279mm)
|
||||
// Use margins of 36 points (0.5in or 12.7mm) with another 36 points for the
|
||||
// caption underneath...
|
||||
swidth = 595.28 \- 72.0;
|
||||
sheight = swidth * height / width;
|
||||
if (sheight > (792.0 \- 36.0 \- 72.0))
|
||||
{
|
||||
sheight = 792.0 \- 36.0 \- 72.0;
|
||||
swidth = sheight * width / height;
|
||||
}
|
||||
|
||||
tx = 0.5 * (595.28 \- swidth);
|
||||
ty = 0.5 * (792 \- 36 \- sheight);
|
||||
|
||||
pdfioContentDrawImage(page, "IM1", tx, ty + 36.0, swidth, sheight);
|
||||
|
||||
// Draw the caption in black...
|
||||
pdfioContentSetFillColorDeviceGray(page, 0.0);
|
||||
|
||||
// Compute the starting point for the text \- Courier is monospaced with a
|
||||
// nominal width of 0.6 times the text height...
|
||||
tx = 0.5 * (595.28 \- 18.0 * 0.6 * strlen(caption));
|
||||
|
||||
// Position and draw the caption underneath...
|
||||
pdfioContentTextBegin(page);
|
||||
pdfioContentSetTextFont(page, "F1", 18.0);
|
||||
pdfioContentTextMoveTo(page, tx, ty);
|
||||
pdfioContentTextShow(page, /*unicode*/false, caption);
|
||||
pdfioContentTextEnd(page);
|
||||
|
||||
// Close the page stream and the PDF file...
|
||||
pdfioStreamClose(page);
|
||||
pdfioFileClose(pdf);
|
||||
}
|
||||
.fi
|
||||
|
||||
.SH ENUMERATIONS
|
||||
.SS pdfio_cs_e
|
||||
@ -1285,6 +1602,15 @@ pdfio_valtype_t pdfioArrayGetType (
|
||||
size_t n
|
||||
);
|
||||
.fi
|
||||
.SS pdfioArrayRemove
|
||||
Remove an array entry.
|
||||
.PP
|
||||
.nf
|
||||
bool pdfioArrayRemove (
|
||||
pdfio_array_t *a,
|
||||
size_t n
|
||||
);
|
||||
.fi
|
||||
.SS pdfioContentClip
|
||||
Clip output to the current path.
|
||||
.PP
|
||||
@ -1865,6 +2191,15 @@ bool pdfioContentTextShowf (
|
||||
...
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictClear
|
||||
Remove a key/value pair from a dictionary.
|
||||
.PP
|
||||
.nf
|
||||
bool pdfioDictClear (
|
||||
pdfio_dict_t *dict,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictCopy
|
||||
Copy a dictionary to a PDF file.
|
||||
.PP
|
||||
@ -1928,6 +2263,15 @@ pdfio_dict_t * pdfioDictGetDict (
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictGetKey
|
||||
Get the key for the specified pair.
|
||||
.PP
|
||||
.nf
|
||||
const char * pdfioDictGetKey (
|
||||
pdfio_dict_t *dict,
|
||||
size_t n
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictGetName
|
||||
Get a key name value from a dictionary.
|
||||
.PP
|
||||
@ -1937,6 +2281,14 @@ const char * pdfioDictGetName (
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictGetNumPairs
|
||||
Get the number of key/value pairs in a dictionary.
|
||||
.PP
|
||||
.nf
|
||||
size_t pdfioDictGetNumPairs (
|
||||
pdfio_dict_t *dict
|
||||
);
|
||||
.fi
|
||||
.SS pdfioDictGetNumber
|
||||
Get a key number value from a dictionary.
|
||||
.PP
|
||||
@ -2148,7 +2500,7 @@ pdfio_file_t * pdfioFileCreate (
|
||||
pdfio_rect_t *media_box,
|
||||
pdfio_rect_t *crop_box,
|
||||
pdfio_error_cb_t error_cb,
|
||||
void *error_data
|
||||
void *error_cbdata
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
@ -2162,7 +2514,7 @@ The "media_box" and "crop_box" arguments specify the default MediaBox and
|
||||
CropBox for pages in the PDF file - if \fBNULL\fR then a default "Universal" size
|
||||
of 8.27x11in (the intersection of US Letter and ISO A4) is used.
|
||||
.PP
|
||||
The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
and its data pointer - if \fBNULL\fR the default error handler is used that
|
||||
writes error messages to \fBstderr\fR.
|
||||
.SS pdfioFileCreateArrayObj
|
||||
@ -2298,6 +2650,18 @@ Note: Currently PNG support is limited to grayscale, RGB, or indexed files
|
||||
without interlacing or alpha. Transparency (masking) based on color/index
|
||||
.IP 5
|
||||
is supported.
|
||||
.SS pdfioFileCreateNameObj
|
||||
Create a new object in a PDF file containing a name.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_obj_t * pdfioFileCreateNameObj (
|
||||
pdfio_file_t *pdf,
|
||||
const char *name
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function creates a new object with a name value in a PDF file.
|
||||
You must call \fIpdfioObjClose\fR to write the object to the file.
|
||||
.SS pdfioFileCreateNumberObj
|
||||
Create a new object in a PDF file containing a number.
|
||||
.PP
|
||||
@ -2325,23 +2689,23 @@ Create a PDF file through an output callback.
|
||||
.nf
|
||||
pdfio_file_t * pdfioFileCreateOutput (
|
||||
pdfio_output_cb_t output_cb,
|
||||
void *output_ctx,
|
||||
void *output_cbdata,
|
||||
const char *version,
|
||||
pdfio_rect_t *media_box,
|
||||
pdfio_rect_t *crop_box,
|
||||
pdfio_error_cb_t error_cb,
|
||||
void *error_data
|
||||
void *error_cbdata
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function creates a new PDF file that is streamed though an output
|
||||
callback. The "output_cb" and "output_ctx" arguments specify the output
|
||||
callback and its context pointer which is called whenever data needs to be
|
||||
callback. The "output_cb" and "output_cbdata" arguments specify the output
|
||||
callback and its data pointer which is called whenever data needs to be
|
||||
written:
|
||||
.PP
|
||||
.nf
|
||||
ssize_t
|
||||
output_cb(void *output_ctx, const void *buffer, size_t bytes)
|
||||
output_cb(void *output_cbdata, const void *buffer, size_t bytes)
|
||||
{
|
||||
// Write buffer to output and return the number of bytes written
|
||||
}
|
||||
@ -2355,7 +2719,7 @@ The "media_box" and "crop_box" arguments specify the default MediaBox and
|
||||
CropBox for pages in the PDF file - if \fBNULL\fR then a default "Universal" size
|
||||
of 8.27x11in (the intersection of US Letter and ISO A4) is used.
|
||||
.PP
|
||||
The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
and its data pointer - if \fBNULL\fR the default error handler is used that
|
||||
writes error messages to \fBstderr\fR.
|
||||
.PP
|
||||
@ -2397,7 +2761,7 @@ pdfio_file_t * pdfioFileCreateTemporary (
|
||||
pdfio_rect_t *media_box,
|
||||
pdfio_rect_t *crop_box,
|
||||
pdfio_error_cb_t error_cb,
|
||||
void *error_data
|
||||
void *error_cbdata
|
||||
);
|
||||
.fi
|
||||
.SS pdfioFileFindObj
|
||||
@ -2420,6 +2784,16 @@ const char * pdfioFileGetAuthor (
|
||||
pdfio_file_t *pdf
|
||||
);
|
||||
.fi
|
||||
.SS pdfioFileGetCatalog
|
||||
Get the document catalog dictionary.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_dict_t * pdfioFileGetCatalog (
|
||||
pdfio_file_t *pdf
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
|
||||
.SS pdfioFileGetCreationDate
|
||||
Get the creation date for a PDF file.
|
||||
.PP
|
||||
@ -2545,22 +2919,22 @@ Open a PDF file for reading.
|
||||
pdfio_file_t * pdfioFileOpen (
|
||||
const char *filename,
|
||||
pdfio_password_cb_t password_cb,
|
||||
void *password_data,
|
||||
void *password_cbdata,
|
||||
pdfio_error_cb_t error_cb,
|
||||
void *error_data
|
||||
void *error_cbdata
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function opens an existing PDF file. The "filename" argument specifies
|
||||
the name of the PDF file to create.
|
||||
.PP
|
||||
The "password_cb" and "password_data" arguments specify a password callback
|
||||
The "password_cb" and "password_cbdata" arguments specify a password callback
|
||||
and its data pointer for PDF files that use one of the standard Adobe
|
||||
"security" handlers. The callback returns a password string or \fBNULL\fR to
|
||||
cancel the open. If \fBNULL\fR is specified for the callback function and the
|
||||
PDF file requires a password, the open will always fail.
|
||||
.PP
|
||||
The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
and its data pointer - if \fBNULL\fR the default error handler is used that
|
||||
writes error messages to \fBstderr\fR.
|
||||
.SS pdfioFileSetAuthor
|
||||
@ -2724,6 +3098,14 @@ size_t pdfioObjGetLength (
|
||||
pdfio_obj_t *obj
|
||||
);
|
||||
.fi
|
||||
.SS pdfioObjGetName
|
||||
Get the name value associated with an object.
|
||||
.PP
|
||||
.nf
|
||||
const char * pdfioObjGetName (
|
||||
pdfio_obj_t *obj
|
||||
);
|
||||
.fi
|
||||
.SS pdfioObjGetNumber
|
||||
Get the object's number.
|
||||
.PP
|
||||
@ -2740,6 +3122,29 @@ const char * pdfioObjGetSubtype (
|
||||
pdfio_obj_t *obj
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function returns an object's PDF subtype name, if any. Common subtype
|
||||
names include:
|
||||
.PP
|
||||
.IP \(bu 5
|
||||
"CIDFontType0": A CID Type0 font
|
||||
.IP \(bu 5
|
||||
"CIDFontType2": A CID TrueType font
|
||||
.IP \(bu 5
|
||||
"Image": An image or image mask
|
||||
.IP \(bu 5
|
||||
"Form": A fillable form
|
||||
.IP \(bu 5
|
||||
"OpenType": An OpenType font
|
||||
.IP \(bu 5
|
||||
"Type0": A composite font
|
||||
.IP \(bu 5
|
||||
"Type1": A PostScript Type1 font
|
||||
.IP \(bu 5
|
||||
"Type3": A PDF Type3 font
|
||||
.IP \(bu 5
|
||||
"TrueType": A TrueType font</li>
|
||||
</ul>
|
||||
.SS pdfioObjGetType
|
||||
Get an object's type.
|
||||
.PP
|
||||
@ -2748,6 +3153,27 @@ const char * pdfioObjGetType (
|
||||
pdfio_obj_t *obj
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function returns an object's PDF type name, if any. Common type names
|
||||
include:
|
||||
.PP
|
||||
.IP \(bu 5
|
||||
"CMap": A character map for composite fonts
|
||||
.IP \(bu 5
|
||||
"Font": An embedded font (\fIpdfioObjGetSubtype\fR will tell you the
|
||||
font format)
|
||||
.IP \(bu 5
|
||||
"FontDescriptor": A font descriptor
|
||||
.IP \(bu 5
|
||||
"Page": A (visible) page
|
||||
.IP \(bu 5
|
||||
"Pages": A page tree node
|
||||
.IP \(bu 5
|
||||
"Template": An invisible template page
|
||||
.IP \(bu 5
|
||||
"XObject": An image, image mask, or form (\fIpdfioObjGetSubtype\fR will
|
||||
tell you which)</li>
|
||||
</ul>
|
||||
.SS pdfioObjOpenStream
|
||||
Open an object's (data) stream for reading.
|
||||
.PP
|
||||
|
774
doc/pdfio.html
774
doc/pdfio.html
File diff suppressed because it is too large
Load Diff
378
doc/pdfio.md
378
doc/pdfio.md
@ -120,6 +120,182 @@ that are defined in a separate header file:
|
||||
```
|
||||
|
||||
|
||||
Understanding PDF Files
|
||||
-----------------------
|
||||
|
||||
A PDF file provides data and commands for displaying pages of graphics and text,
|
||||
and is structured in a way that allows it to be displayed in the same way across
|
||||
multiple devices and platforms. The following is a PDF which shows "Hello,
|
||||
World!" on one page:
|
||||
|
||||
```
|
||||
%PDF-1.0 % Header starts here
|
||||
%âãÏÓ
|
||||
1 0 obj % Body starts here
|
||||
<<
|
||||
/Kids [2 0 R]
|
||||
/Count 1
|
||||
/Type /Pages
|
||||
>>
|
||||
endobj
|
||||
2 0 obj
|
||||
<<
|
||||
/Rotate 0
|
||||
/Parent 1 0 R
|
||||
/Resources 3 0 R
|
||||
/MediaBox [0 0 612 792]
|
||||
/Contents [4 0 R]/Type /Page
|
||||
>>
|
||||
endobj
|
||||
3 0 obj
|
||||
<<
|
||||
/Font
|
||||
<<
|
||||
/F0
|
||||
<<
|
||||
/BaseFont /Times-Italic
|
||||
/Subtype /Type1
|
||||
/Type /Font
|
||||
>>
|
||||
>>
|
||||
>>
|
||||
endobj
|
||||
4 0 obj
|
||||
<<
|
||||
/Length 65
|
||||
>>
|
||||
stream
|
||||
1. 0. 0. 1. 50. 700. cm
|
||||
BT
|
||||
/F0 36. Tf
|
||||
(Hello, World!) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
5 0 obj
|
||||
<<
|
||||
/Pages 1 0 R
|
||||
/Type /Catalog
|
||||
>>
|
||||
endobj
|
||||
xref % Cross-reference table starts here
|
||||
0 6
|
||||
0000000000 65535 f
|
||||
0000000015 00000 n
|
||||
0000000074 00000 n
|
||||
0000000192 00000 n
|
||||
0000000291 00000 n
|
||||
0000000409 00000 n
|
||||
trailer % Trailer starts here
|
||||
<<
|
||||
/Root 5 0 R
|
||||
/Size 6
|
||||
>>
|
||||
startxref
|
||||
459
|
||||
%%EOF
|
||||
```
|
||||
|
||||
|
||||
### Header
|
||||
|
||||
The header is the first line of a PDF file that specifies the version of the PDF
|
||||
format that has been used, for example `%PDF-1.0`.
|
||||
|
||||
Since PDF files almost always contain binary data, they can become corrupted if
|
||||
line endings are changed. For example, if the file is transferred using FTP in
|
||||
text mode or is edited in Notepad on Windows. To allow legacy file transfer
|
||||
programs to determine that the file is binary, the PDF standard recommends
|
||||
including some bytes with character codes higher than 127 in the header, for
|
||||
example:
|
||||
|
||||
```
|
||||
%âãÏÓ
|
||||
```
|
||||
|
||||
The percent sign indicates a comment line while the other few bytes are
|
||||
arbitrary character codes in excess of 127. So, the whole header in our example
|
||||
is:
|
||||
|
||||
```
|
||||
%PDF-1.0
|
||||
%âãÏÓ
|
||||
```
|
||||
|
||||
|
||||
### Body
|
||||
|
||||
The file body consists of a sequence of objects, each preceded by an object
|
||||
number, generation number, and the obj keyword on one line, and followed by the
|
||||
endobj keyword on another. For example:
|
||||
|
||||
```
|
||||
1 0 obj
|
||||
<<
|
||||
/Kids [2 0 R]
|
||||
/Count 1
|
||||
/Type /Pages
|
||||
>>
|
||||
endobj
|
||||
```
|
||||
|
||||
In this example, the object number is 1 and the generation number is 0, meaning
|
||||
it is the first version of the object. The content for object 1 is between the
|
||||
initial `1 0 obj` and trailing `endobj` lines. In this case, the content is the
|
||||
dictionary `<</Kids [2 0 R] /Count 1 /Type /Pages>>`.
|
||||
|
||||
|
||||
### Cross-Reference Table
|
||||
|
||||
The cross-reference table lists the byte offset of each object in the file body.
|
||||
This allows random access to objects, meaning they don't have to be read in
|
||||
order. Objects that are not used are never read, making the process efficient.
|
||||
Operations like counting the number of pages in a PDF document are fast, even in
|
||||
large files.
|
||||
|
||||
Each object has an object number and a generation number. Generation numbers
|
||||
are used when a cross-reference table entry is reused. For simplicity, we will
|
||||
assume generation numbers to be always zero and ignore them. The
|
||||
cross-reference table consists of a header line that indicates the number of
|
||||
entries, a free entry line for object 0, and a line for each of the objects in
|
||||
the file body. For example:
|
||||
|
||||
```
|
||||
0 6 % Six entries in table, starting at 0
|
||||
0000000000 65535 f % Free entry for object 0
|
||||
0000000015 00000 n % Object 1 is at byte offset 15
|
||||
0000000074 00000 n % Object 2 is at byte offset 74
|
||||
0000000192 00000 n % etc...
|
||||
0000000291 00000 n
|
||||
0000000409 00000 n % Object 5 is at byte offset 409
|
||||
```
|
||||
|
||||
|
||||
### Trailer
|
||||
|
||||
The first line of the trailer is just the `trailer` keyword. This is followed
|
||||
by the trailer dictionary which contains at least the `/Size` entry specifying
|
||||
the number of entries in the cross-reference table and the `/Root` entry which
|
||||
references the object for the document catalog which is the root element of the
|
||||
graph of objects in the body.
|
||||
|
||||
There follows a line with just the `startxref` keyword, a line with a single
|
||||
number specifying the byte offset of the start of the cross-reference table
|
||||
within the file, and then the line `%%EOF` which signals the end of the PDF
|
||||
file.
|
||||
|
||||
```
|
||||
trailer % Trailer keyword
|
||||
<< % The trailer dictinonary
|
||||
/Root 5 0 R
|
||||
/Size 6
|
||||
>>
|
||||
startxref % startxref keyword
|
||||
459 % Byte offset of cross-reference table
|
||||
%%EOF % End-of-file marker
|
||||
```
|
||||
|
||||
|
||||
API Overview
|
||||
============
|
||||
|
||||
@ -132,6 +308,7 @@ PDFio exposes several types:
|
||||
- `pdfio_stream_t`: An object stream
|
||||
|
||||
|
||||
|
||||
Reading PDF Files
|
||||
-----------------
|
||||
|
||||
@ -202,7 +379,74 @@ Each page is represented by a "page tree" object (what [`pdfioFileGetPage`](@@)
|
||||
returns) that specifies information about the page and one or more "content"
|
||||
objects that contain the images, fonts, text, and graphics that appear on the
|
||||
page. Use the [`pdfioPageGetNumStreams`](@@) and [`pdfioPageOpenStream`](@@)
|
||||
functions to access the content streams for each page.
|
||||
functions to access the content streams for each page, and
|
||||
[`pdfioObjGetDict`](@@) to get the associated page object dictionary. For
|
||||
example, if you want to display the media and crop boxes for a given page:
|
||||
|
||||
```c
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
size_t count; // Number of pages
|
||||
pdfio_obj_t *page; // Current page
|
||||
pdfio_dict_t *dict; // Current page dictionary
|
||||
pdfio_array_t *media_box; // MediaBox array
|
||||
double media_values[4]; // MediaBox values
|
||||
pdfio_array_t *crop_box; // CropBox array
|
||||
double crop_values[4]; // CropBox values
|
||||
|
||||
// Iterate the pages in the PDF file
|
||||
for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
|
||||
{
|
||||
page = pdfioFileGetPage(pdf, i);
|
||||
dict = pdfioObjGetDict(page);
|
||||
|
||||
media_box = pdfioDictGetArray(dict, "MediaBox");
|
||||
media_values[0] = pdfioArrayGetNumber(media_box, 0);
|
||||
media_values[1] = pdfioArrayGetNumber(media_box, 1);
|
||||
media_values[2] = pdfioArrayGetNumber(media_box, 2);
|
||||
media_values[3] = pdfioArrayGetNumber(media_box, 3);
|
||||
|
||||
crop_box = pdfioDictGetArray(dict, "CropBox");
|
||||
crop_values[0] = pdfioArrayGetNumber(crop_box, 0);
|
||||
crop_values[1] = pdfioArrayGetNumber(crop_box, 1);
|
||||
crop_values[2] = pdfioArrayGetNumber(crop_box, 2);
|
||||
crop_values[3] = pdfioArrayGetNumber(crop_box, 3);
|
||||
|
||||
printf("Page %u: MediaBox=[%g %g %g %g], CropBox=[%g %g %g %g]\n",
|
||||
(unsigned)(i + 1),
|
||||
media_values[0], media_values[1], media_values[2], media_values[3],
|
||||
crop_values[0], crop_values[1], crop_values[2], crop_values[3]);
|
||||
}
|
||||
```
|
||||
|
||||
Page object dictionaries have several (mostly optional) key/value pairs,
|
||||
including:
|
||||
|
||||
- "Annots": An array of annotation dictionaries for the page; use
|
||||
[`pdfioDictGetArray`](@@) to get the array
|
||||
- "CropBox": The crop box as an array of four numbers for the left, bottom,
|
||||
right, and top coordinates of the target media; use [`pdfioDictGetArray`](@@)
|
||||
to get a pointer to the array of numbers
|
||||
- "Dur": The number of seconds the page should be displayed; use
|
||||
[`pdfioDictGetNumber`](@@) to get the page duration value
|
||||
- "Group": The dictionary of transparency group values for the page; use
|
||||
[`pdfioDictGetDict`](@@) to get a pointer to the resources dictionary
|
||||
- "LastModified": The date and time when this page was last modified; use
|
||||
[`pdfioDictGetDate`](@@) to get the Unix `time_t` value
|
||||
- "Parent": The parent page tree node object for this page; use
|
||||
[`pdfioDictGetObj`](@@) to get a pointer to the object
|
||||
- "MediaBox": The media box as an array of four numbers for the left, bottom,
|
||||
right, and top coordinates of the target media; use [`pdfioDictGetArray`](@@)
|
||||
to get a pointer to the array of numbers
|
||||
- "Resources": The dictionary of resources for the page; use
|
||||
[`pdfioDictGetDict`](@@) to get a pointer to the resources dictionary
|
||||
- "Rotate": A number indicating the number of degrees of counter-clockwise
|
||||
rotation to apply to the page when viewing; use [`pdfioDictGetNumber`](@@)
|
||||
to get the rotation angle
|
||||
- "Thumb": A thumbnail image object for the page; use [`pdfioDictGetObj`](@@)
|
||||
to get a pointer to the thumbnail image object
|
||||
- "Trans": The page transition dictionary; use [`pdfioDictGetDict`](@@) to get
|
||||
a pointer to the dictionary
|
||||
|
||||
The [`pdfioFileClose`](@@) function closes a PDF file and frees all memory that
|
||||
was used for it:
|
||||
@ -345,7 +589,7 @@ to the stream:
|
||||
The [PDF content helper functions](@) provide additional functions for writing
|
||||
specific PDF page stream commands.
|
||||
|
||||
When you are done writing the stream, call [`pdfioStreamCLose`](@@) to close
|
||||
When you are done writing the stream, call [`pdfioStreamClose`](@@) to close
|
||||
both the stream and the object.
|
||||
|
||||
|
||||
@ -586,3 +830,133 @@ escaping, as needed:
|
||||
- [`pdfioContentTextShowf`](@@) draws a formatted string in a text block
|
||||
- [`pdfioContentTextShowJustified`](@@) draws an array of literal strings with
|
||||
offsets between them
|
||||
|
||||
|
||||
Examples
|
||||
========
|
||||
|
||||
Read PDF Metadata
|
||||
-----------------
|
||||
|
||||
The following example function will open a PDF file and print the title, author,
|
||||
creation date, and number of pages:
|
||||
|
||||
```c
|
||||
#include <pdfio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
void
|
||||
show_pdf_info(const char *filename)
|
||||
{
|
||||
pdfio_file_t *pdf;
|
||||
time_t creation_date;
|
||||
struct tm *creation_tm;
|
||||
char creation_text[256];
|
||||
|
||||
|
||||
// Open the PDF file with the default callbacks...
|
||||
pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_cbdata*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
|
||||
if (pdf == NULL)
|
||||
return;
|
||||
|
||||
// Get the creation date and convert to a string...
|
||||
creation_date = pdfioFileGetCreationDate(pdf);
|
||||
creation_tm = localtime(&creation_date);
|
||||
strftime(creation_text, sizeof(creation_text), "%c", &creation_tm);
|
||||
|
||||
// Print file information to stdout...
|
||||
printf("%s:\n", filename);
|
||||
printf(" Title: %s\n", pdfioFileGetTitle(pdf));
|
||||
printf(" Author: %s\n", pdfioFileGetAuthor(pdf));
|
||||
printf(" Created On: %s\n", creation_text);
|
||||
printf(" Number Pages: %u\n", (unsigned)pdfioFileGetNumPages(pdf));
|
||||
|
||||
// Close the PDF file...
|
||||
pdfioFileClose(pdf);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Create PDF File With Text and Image
|
||||
-----------------------------------
|
||||
|
||||
The following example function will create a PDF file, embed a base font and the
|
||||
named JPEG or PNG image file, and then creates a page with the image centered on
|
||||
the page with the text centered below:
|
||||
|
||||
```c
|
||||
#include <pdfio.h>
|
||||
#include <pdfio-content.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
void
|
||||
create_pdf_image_file(const char *pdfname, const char *imagename, const char *caption)
|
||||
{
|
||||
pdfio_file_t *pdf;
|
||||
pdfio_obj_t *font;
|
||||
pdfio_obj_t *image;
|
||||
pdfio_dict_t *dict;
|
||||
pdfio_stream_t *page;
|
||||
double width, height;
|
||||
double swidth, sheight;
|
||||
double tx, ty;
|
||||
|
||||
|
||||
// Create the PDF file...
|
||||
pdf = pdfioFileCreate(pdfname, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
|
||||
|
||||
// Create a Courier base font for the caption
|
||||
font = pdfioFileCreateFontObjFromBase(pdf, "Courier");
|
||||
|
||||
// Create an image object from the JPEG/PNG image file...
|
||||
image = pdfioFileCreateImageObjFromFile(pdf, imagename, true);
|
||||
|
||||
// Create a page dictionary with the font and image...
|
||||
dict = pdfioDictCreate(pdf);
|
||||
pdfioPageDictAddFont(dict, "F1", font);
|
||||
pdfioPageDictAddImage(dict, "IM1", image);
|
||||
|
||||
// Create the page and its content stream...
|
||||
page = pdfioFileCreatePage(pdf, dict);
|
||||
|
||||
// Position and scale the image on the page...
|
||||
width = pdfioImageGetWidth(image);
|
||||
height = pdfioImageGetHeight(image);
|
||||
|
||||
// Default media_box is "universal" 595.28x792 points (8.27x11in or 210x279mm)
|
||||
// Use margins of 36 points (0.5in or 12.7mm) with another 36 points for the
|
||||
// caption underneath...
|
||||
swidth = 595.28 - 72.0;
|
||||
sheight = swidth * height / width;
|
||||
if (sheight > (792.0 - 36.0 - 72.0))
|
||||
{
|
||||
sheight = 792.0 - 36.0 - 72.0;
|
||||
swidth = sheight * width / height;
|
||||
}
|
||||
|
||||
tx = 0.5 * (595.28 - swidth);
|
||||
ty = 0.5 * (792 - 36 - sheight);
|
||||
|
||||
pdfioContentDrawImage(page, "IM1", tx, ty + 36.0, swidth, sheight);
|
||||
|
||||
// Draw the caption in black...
|
||||
pdfioContentSetFillColorDeviceGray(page, 0.0);
|
||||
|
||||
// Compute the starting point for the text - Courier is monospaced with a
|
||||
// nominal width of 0.6 times the text height...
|
||||
tx = 0.5 * (595.28 - 18.0 * 0.6 * strlen(caption));
|
||||
|
||||
// Position and draw the caption underneath...
|
||||
pdfioContentTextBegin(page);
|
||||
pdfioContentSetTextFont(page, "F1", 18.0);
|
||||
pdfioContentTextMoveTo(page, tx, ty);
|
||||
pdfioContentTextShow(page, /*unicode*/false, caption);
|
||||
pdfioContentTextEnd(page);
|
||||
|
||||
// Close the page stream and the PDF file...
|
||||
pdfioStreamClose(page);
|
||||
pdfioFileClose(pdf);
|
||||
}
|
||||
```
|
||||
|
46
examples/Makefile
Normal file
46
examples/Makefile
Normal file
@ -0,0 +1,46 @@
|
||||
#
|
||||
# Makefile for PDFio examples.
|
||||
#
|
||||
# Copyright © 2024 by Michael R Sweet.
|
||||
#
|
||||
# Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
# information.
|
||||
#
|
||||
|
||||
# POSIX makefile
|
||||
.POSIX:
|
||||
|
||||
|
||||
# Common options
|
||||
CFLAGS = -g $(CPPFLAGS)
|
||||
CPPFLAGS = -I..
|
||||
LIBS = -L.. -lpdfio -lz
|
||||
|
||||
|
||||
# Targets
|
||||
TARGETS = \
|
||||
code128 \
|
||||
md2pdf
|
||||
|
||||
|
||||
# Make everything
|
||||
all: $(TARGETS)
|
||||
|
||||
|
||||
# Clean everything
|
||||
clean:
|
||||
rm -f $(TARGETS)
|
||||
|
||||
|
||||
# code128
|
||||
code128: code128.c
|
||||
$(CC) $(CFLAGS) -o $@ code128.c $(LIBS)
|
||||
|
||||
|
||||
# md2pdf
|
||||
md2pdf: md2pdf.c mmd.c mmd.h
|
||||
$(CC) $(CFLAGS) -o $@ md2pdf.c mmd.c $(LIBS)
|
||||
|
||||
|
||||
# Common dependencies...
|
||||
$(TARGETS): Makefile ../pdfio.h ../pdfio-content.h
|
BIN
examples/Roboto-Bold.ttf
Normal file
BIN
examples/Roboto-Bold.ttf
Normal file
Binary file not shown.
BIN
examples/Roboto-Italic.ttf
Normal file
BIN
examples/Roboto-Italic.ttf
Normal file
Binary file not shown.
BIN
examples/Roboto-Regular.ttf
Normal file
BIN
examples/Roboto-Regular.ttf
Normal file
Binary file not shown.
BIN
examples/RobotoMono-Regular.ttf
Normal file
BIN
examples/RobotoMono-Regular.ttf
Normal file
Binary file not shown.
209
examples/code128.c
Normal file
209
examples/code128.c
Normal file
@ -0,0 +1,209 @@
|
||||
//
|
||||
// Code 128 barcode example for PDFio.
|
||||
//
|
||||
// Copyright © 2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// ./code128 "BARCODE" ["TEXT"] >FILENAME.pdf
|
||||
//
|
||||
|
||||
#include <pdfio.h>
|
||||
#include <pdfio-content.h>
|
||||
|
||||
|
||||
//
|
||||
// 'make_code128()' - Make a Code 128 barcode string.
|
||||
//
|
||||
// This function produces a Code B (printable ASCII) representation of the
|
||||
// source string and doesn't try to optimize using Code C. Non-printable and
|
||||
// extended characters are ignored in the source string.
|
||||
//
|
||||
|
||||
|
||||
static char * // O - Output string
|
||||
make_code128(char *dst, // I - Destination buffer
|
||||
const char *src, // I - Source string
|
||||
size_t dstsize) // I - Size of destination buffer
|
||||
{
|
||||
char *dstptr, // Pointer into destination buffer
|
||||
*dstend; // End of destination buffer
|
||||
int sum; // Weighted sum
|
||||
static const char *code128_chars = // Code 128 characters
|
||||
" !\"#$%&'()*+,-./0123456789:;<=>?"
|
||||
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
|
||||
"`abcdefghijklmnopqrstuvwxyz{|}~\303"
|
||||
"\304\305\306\307\310\311\312";
|
||||
static const char code128_fnc_3 = '\304';
|
||||
// FNC 3
|
||||
static const char code128_fnc_2 = '\305';
|
||||
// FNC 2
|
||||
static const char code128_shift_b = '\306';
|
||||
// Shift B (for lowercase)
|
||||
static const char code128_code_c = '\307';
|
||||
// Code C
|
||||
static const char code128_code_b = '\310';
|
||||
// Code B
|
||||
static const char code128_fnc_4 = '\311';
|
||||
// FNC 4
|
||||
static const char code128_fnc_1 = '\312';
|
||||
// FNC 1
|
||||
static const char code128_start_code_a = '\313';
|
||||
// Start code A
|
||||
static const char code128_start_code_b = '\314';
|
||||
// Start code A
|
||||
static const char code128_start_code_c = '\315';
|
||||
// Start code A
|
||||
static const char code128_stop = '\316';
|
||||
// Stop pattern
|
||||
|
||||
|
||||
// Start a Code B barcode...
|
||||
dstptr = dst;
|
||||
dstend = dst + dstsize - 3;
|
||||
|
||||
*dstptr++ = code128_start_code_b;
|
||||
sum = code128_start_code_b - 100;
|
||||
|
||||
while (*src && dstptr < dstend)
|
||||
{
|
||||
if (*src >= ' ' && *src < 0x7f)
|
||||
{
|
||||
sum += (dstptr - dst) * (*src - ' ');
|
||||
*dstptr++ = *src;
|
||||
}
|
||||
|
||||
src ++;
|
||||
}
|
||||
|
||||
// Add the weighted sum modulo 103
|
||||
*dstptr++ = code128_chars[sum % 103];
|
||||
|
||||
// Add the stop pattern and return...
|
||||
*dstptr++ = code128_stop;
|
||||
*dstptr = '\0';
|
||||
|
||||
return (dst);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'output_cb()' - Write PDF data to the standard output...
|
||||
//
|
||||
|
||||
static ssize_t // O - Number of bytes written
|
||||
output_cb(void *output_cbdata, // I - Callback data (not used)
|
||||
const void *buffer, // I - Buffer to write
|
||||
size_t bytes) // I - Number of bytes to write
|
||||
{
|
||||
(void)output_cbdata;
|
||||
|
||||
return ((ssize_t)fwrite(buffer, 1, bytes, stdout));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'main()' - Produce a single-page barcode file.
|
||||
//
|
||||
|
||||
int // O - Exit status
|
||||
main(int argc, // I - Number of command-line arguments
|
||||
char *argv[]) // I - Command-line arguments
|
||||
{
|
||||
const char *barcode, // Barcode to show
|
||||
*text; // Text to display under barcode
|
||||
pdfio_file_t *pdf; // Output PDF file
|
||||
pdfio_obj_t *barcode_font; // Barcode font object
|
||||
pdfio_obj_t *text_font = NULL; // Text font object
|
||||
pdfio_dict_t *page_dict; // Page dictionary
|
||||
pdfio_rect_t media_box; // Media/CropBox for page
|
||||
pdfio_stream_t *page_st; // Page stream
|
||||
char barcode_temp[256]; // Barcode buffer
|
||||
double barcode_height = 36.0, // Height of barcode
|
||||
barcode_width, // Width of barcode
|
||||
text_height = 0.0, // Height of text
|
||||
text_width = 0.0; // Width of text
|
||||
|
||||
|
||||
// Get the barcode and optional text from the command-line...
|
||||
if (argc < 2 || argc > 3)
|
||||
{
|
||||
fputs("Usage: code128 \"BARCODE\" [\"TEXT\"] >FILENAME.pdf\n", stderr);
|
||||
return (1);
|
||||
}
|
||||
|
||||
barcode = argv[1];
|
||||
text = argv[2];
|
||||
|
||||
// Output a PDF file to the standard output...
|
||||
#ifdef _WIN32
|
||||
setmode(1, O_BINARY); // Force binary output on Windows
|
||||
#endif // _WIN32
|
||||
|
||||
if ((pdf = pdfioFileCreateOutput(output_cb, /*output_cbdata*/NULL, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_data*/NULL)) == NULL)
|
||||
return (1);
|
||||
|
||||
// Load fonts...
|
||||
barcode_font = pdfioFileCreateFontObjFromFile(pdf, "code128.ttf", /*unicode*/false);
|
||||
if (text)
|
||||
text_font = pdfioFileCreateFontObjFromFile(pdf, "../testfiles/OpenSans-Regular.ttf", /*unicode*/true);
|
||||
|
||||
// Generate Code128 characters for the desired barcode...
|
||||
if (!(barcode[0] & 0x80))
|
||||
barcode = make_code128(barcode_temp, barcode, sizeof(barcode_temp));
|
||||
|
||||
// Compute sizes of the text...
|
||||
barcode_width = pdfioContentTextMeasure(barcode_font, barcode, barcode_height);
|
||||
if (text && text_font)
|
||||
{
|
||||
text_height = 9.0;
|
||||
text_width = pdfioContentTextMeasure(text_font, text, text_height);
|
||||
}
|
||||
|
||||
// Compute the size of the PDF page...
|
||||
media_box.x1 = 0.0;
|
||||
media_box.y1 = 0.0;
|
||||
media_box.x2 = (barcode_width > text_width ? barcode_width : text_width) + 18.0;
|
||||
media_box.y2 = barcode_height + text_height + 18.0;
|
||||
|
||||
// Start a page for the barcode...
|
||||
page_dict = pdfioDictCreate(pdf);
|
||||
|
||||
pdfioDictSetRect(page_dict, "MediaBox", &media_box);
|
||||
pdfioDictSetRect(page_dict, "CropBox", &media_box);
|
||||
|
||||
pdfioPageDictAddFont(page_dict, "B128", barcode_font);
|
||||
if (text_font)
|
||||
pdfioPageDictAddFont(page_dict, "TEXT", text_font);
|
||||
|
||||
page_st = pdfioFileCreatePage(pdf, page_dict);
|
||||
|
||||
// Draw the page...
|
||||
pdfioContentSetStrokeColorGray(page_st, 0.0);
|
||||
|
||||
pdfioContentSetTextFont(page_st, "B128", barcode_height);
|
||||
pdfioContentTextBegin(page_st);
|
||||
pdfioContentTextMoveTo(page_st, 0.5 * (media_box.x2 - barcode_width), 9.0 + text_height);
|
||||
pdfioContentTextShow(page_st, /*unicode*/false, barcode);
|
||||
pdfioContentTextEnd(page_st);
|
||||
|
||||
if (text && text_font)
|
||||
{
|
||||
pdfioContentSetTextFont(page_st, "TEXT", text_height);
|
||||
pdfioContentTextBegin(page_st);
|
||||
pdfioContentTextMoveTo(page_st, 0.5 * (media_box.x2 - text_width), 9.0);
|
||||
pdfioContentTextShow(page_st, /*unicode*/true, text);
|
||||
pdfioContentTextEnd(page_st);
|
||||
}
|
||||
|
||||
pdfioStreamClose(page_st);
|
||||
|
||||
// Close and return...
|
||||
pdfioFileClose(pdf);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
BIN
examples/code128.ttf
Normal file
BIN
examples/code128.ttf
Normal file
Binary file not shown.
645
examples/md2pdf.c
Normal file
645
examples/md2pdf.c
Normal file
@ -0,0 +1,645 @@
|
||||
//
|
||||
// Simple markdown to PDF converter example for PDFio.
|
||||
//
|
||||
// Copyright © 2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// ./md2pdf FILENAME.md >FILENAME.pdf
|
||||
//
|
||||
// The generated PDF file is formatted for a "universal" paper size (8.27x11",
|
||||
// the intersection of US Letter and ISO A4) with 1" top and bottom margins and
|
||||
// 0.5" side margins. The document title (if present) is centered at the top
|
||||
// of the second and subsequent pages while the current heading and page number
|
||||
// are provided at the bottom of each page.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "mmd.h"
|
||||
#include <pdfio.h>
|
||||
#include <pdfio-content.h>
|
||||
|
||||
|
||||
//
|
||||
// Types...
|
||||
//
|
||||
|
||||
typedef enum doccolor_e // Document color enumeration
|
||||
{
|
||||
DOCCOLOR_BLACK, // #000
|
||||
DOCCOLOR_RED, // #900
|
||||
DOCCOLOR_GREEN, // #090
|
||||
DOCCOLOR_BLUE, // #00C
|
||||
DOCCOLOR_GRAY // #555
|
||||
} doccolor_t;
|
||||
|
||||
typedef enum docfont_e // Document font enumeration
|
||||
{
|
||||
DOCFONT_REGULAR, // Roboto-Regular
|
||||
DOCFONT_BOLD, // Roboto-Bold
|
||||
DOCFONT_ITALIC, // Roboto-Italic
|
||||
DOCFONT_MONOSPACE, // RobotoMono-Regular
|
||||
DOCFONT_MAX // Number of fonts
|
||||
} docfont_t;
|
||||
|
||||
typedef struct docimage_s // Document image info
|
||||
{
|
||||
const char *url; // Reference URL
|
||||
pdfio_obj_t *obj; // Image object
|
||||
} docimage_t;
|
||||
|
||||
#define DOCIMAGE_MAX 1000 // Maximum number of images
|
||||
|
||||
typedef struct docdata_s // Document formatting data
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
pdfio_rect_t media_box; // Media (page) box
|
||||
pdfio_rect_t crop_box; // Crop box (for margins)
|
||||
pdfio_rect_t art_box; // Art box (for markdown content)
|
||||
pdfio_obj_t *fonts[DOCFONT_MAX]; // Embedded fonts
|
||||
size_t num_images; // Number of embedded images
|
||||
docimage_t images[DOCIMAGE_MAX]; // Embedded images
|
||||
const char *title; // Document title
|
||||
char *heading; // Current document heading
|
||||
pdfio_stream_t *st; // Current page stream
|
||||
double y; // Current position on page
|
||||
doccolor_t color; // Current color
|
||||
} docdata_t;
|
||||
|
||||
|
||||
//
|
||||
// Macros...
|
||||
//
|
||||
|
||||
#define in2pt(in) (in * 72.0)
|
||||
#define mm2pt(mm) (mm * 72.0 / 25.4)
|
||||
|
||||
|
||||
//
|
||||
// Constants...
|
||||
//
|
||||
|
||||
static const char * const docfont_filenames[] =
|
||||
{
|
||||
"Roboto-Regular.ttf",
|
||||
"Roboto-Bold.ttf",
|
||||
"Roboto-Italic.ttf",
|
||||
"RobotoMono-Regular.ttf"
|
||||
};
|
||||
|
||||
static const char * const docfont_names[] =
|
||||
{
|
||||
"FR",
|
||||
"FB",
|
||||
"FI",
|
||||
"FM"
|
||||
};
|
||||
|
||||
#define LINE_HEIGHT 1.4 // Multiplier for line height
|
||||
|
||||
#define SIZE_BODY 11.0 // Size of body text (points)
|
||||
#define SIZE_CODEBLOCK 10.0 // Size of code block text (points)
|
||||
#define SIZE_HEADFOOT 9.0 // Size of header/footer text (points)
|
||||
#define SIZE_HEADING_1 18.0 // Size of first level heading (points)
|
||||
#define SIZE_HEADING_2 16.0 // Size of top level heading (points)
|
||||
#define SIZE_HEADING_3 15.0 // Size of top level heading (points)
|
||||
#define SIZE_HEADING_4 14.0 // Size of top level heading (points)
|
||||
#define SIZE_HEADING_5 13.0 // Size of top level heading (points)
|
||||
#define SIZE_HEADING_6 12.0 // Size of top level heading (points)
|
||||
#define SIZE_TABLE 10.0 // Size of table text (points)
|
||||
|
||||
#define PAGE_WIDTH mm2pt(210) // Page width in points
|
||||
#define PAGE_LENGTH in2pt(11) // Page length in points
|
||||
#define PAGE_LEFT in2pt(0.5) // Left margin in points
|
||||
#define PAGE_RIGHT (PAGE_WIDTH - in2pt(0.5))
|
||||
// Right margin in points
|
||||
#define PAGE_BOTTOM in2pt(1.0) // Bottom margin in points
|
||||
#define PAGE_TOP (PAGE_LENGTH - in2pt(1.0))
|
||||
// Top margin in points
|
||||
#define PAGE_HEADER (PAGE_LENGTH - in2pt(0.5))
|
||||
// Vertical position of header
|
||||
#define PAGE_FOOTER in2pt(0.5) // Vertical position of footer
|
||||
|
||||
#define UNICODE_VALUE true // `true` for Unicode text, `false` for ISO-8859-1
|
||||
|
||||
|
||||
//
|
||||
// 'set_color()' - Set the stroke and fill color.
|
||||
//
|
||||
|
||||
static void
|
||||
set_color(docdata_t *dd, // I - Document data
|
||||
doccolor_t color) // I - Document color
|
||||
{
|
||||
switch (color)
|
||||
{
|
||||
case DOCCOLOR_BLACK :
|
||||
pdfioContentSetFillColorDeviceGray(dd->st, 0.0);
|
||||
pdfioContentSetStrokeColorDeviceGray(dd->st, 0.0);
|
||||
break;
|
||||
case DOCCOLOR_RED :
|
||||
pdfioContentSetFillColorDeviceRGB(dd->st, 0.6, 0.0, 0.0);
|
||||
pdfioContentSetStrokeColorDeviceRGB(dd->st, 0.6, 0.0, 0.0);
|
||||
break;
|
||||
case DOCCOLOR_GREEN :
|
||||
pdfioContentSetFillColorDeviceRGB(dd->st, 0.0, 0.6, 0.0);
|
||||
pdfioContentSetStrokeColorDeviceRGB(dd->st, 0.0, 0.6, 0.0);
|
||||
break;
|
||||
case DOCCOLOR_BLUE :
|
||||
pdfioContentSetFillColorDeviceRGB(dd->st, 0.0, 0.0, 0.8);
|
||||
pdfioContentSetStrokeColorDeviceRGB(dd->st, 0.0, 0.0, 0.8);
|
||||
break;
|
||||
case DOCCOLOR_GRAY :
|
||||
pdfioContentSetFillColorDeviceGray(dd->st, 0.333);
|
||||
pdfioContentSetStrokeColorDeviceGray(dd->st, 0.333);
|
||||
break;
|
||||
}
|
||||
|
||||
dd->color = color;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'new_page()' - Start a new page.
|
||||
//
|
||||
|
||||
static void
|
||||
new_page(docdata_t *dd) // I - Document data
|
||||
{
|
||||
pdfio_dict_t *page_dict; // Page dictionary
|
||||
docfont_t fontface; // Current font face
|
||||
size_t i; // Looping var
|
||||
char temp[32]; // Temporary string
|
||||
double width; // Width of fragment
|
||||
|
||||
|
||||
// Close the current page...
|
||||
pdfioStreamClose(dd->st);
|
||||
|
||||
// Prep the new page...
|
||||
page_dict = pdfioDictCreate(dd->pdf);
|
||||
|
||||
pdfioDictSetRect(page_dict, "MediaBox", &dd->media_box);
|
||||
// pdfioDictSetRect(page_dict, "CropBox", &dd->crop_box);
|
||||
pdfioDictSetRect(page_dict, "ArtBox", &dd->art_box);
|
||||
|
||||
for (fontface = DOCFONT_REGULAR; fontface < DOCFONT_MAX; fontface ++)
|
||||
pdfioPageDictAddFont(page_dict, docfont_names[fontface], dd->fonts[fontface]);
|
||||
|
||||
for (i = 0; i < dd->num_images; i ++)
|
||||
{
|
||||
snprintf(temp, sizeof(temp), "I%u", (unsigned)i);
|
||||
pdfioPageDictAddImage(page_dict, temp, dd->images[i].obj);
|
||||
}
|
||||
|
||||
dd->st = pdfioFileCreatePage(dd->pdf, page_dict);
|
||||
|
||||
// Add header/footer text
|
||||
set_color(dd, DOCCOLOR_GRAY);
|
||||
pdfioContentSetTextFont(dd->st, docfont_names[DOCFONT_REGULAR], SIZE_HEADFOOT);
|
||||
|
||||
if (pdfioFileGetNumPages(dd->pdf) > 1 && dd->title)
|
||||
{
|
||||
// Show title in header...
|
||||
width = pdfioContentTextMeasure(dd->fonts[DOCFONT_REGULAR], dd->title, SIZE_HEADFOOT);
|
||||
|
||||
pdfioContentTextBegin(dd->st);
|
||||
pdfioContentTextMoveTo(dd->st, dd->crop_box.x1 + 0.5 * (dd->crop_box.x2 - dd->crop_box.x1 - width), dd->crop_box.y2 - SIZE_HEADFOOT);
|
||||
pdfioContentTextShow(dd->st, UNICODE_VALUE, dd->title);
|
||||
pdfioContentTextEnd(dd->st);
|
||||
|
||||
pdfioContentPathMoveTo(dd->st, dd->crop_box.x1, dd->crop_box.y2 - 2 * SIZE_HEADFOOT * LINE_HEIGHT + SIZE_HEADFOOT);
|
||||
pdfioContentPathLineTo(dd->st, dd->crop_box.x2, dd->crop_box.y2 - 2 * SIZE_HEADFOOT * LINE_HEIGHT + SIZE_HEADFOOT);
|
||||
pdfioContentStroke(dd->st);
|
||||
}
|
||||
|
||||
// Show page number and current heading...
|
||||
pdfioContentPathMoveTo(dd->st, dd->crop_box.x1, dd->crop_box.y1 + SIZE_HEADFOOT * LINE_HEIGHT);
|
||||
pdfioContentPathLineTo(dd->st, dd->crop_box.x2, dd->crop_box.y1 + SIZE_HEADFOOT * LINE_HEIGHT);
|
||||
pdfioContentStroke(dd->st);
|
||||
|
||||
pdfioContentTextBegin(dd->st);
|
||||
snprintf(temp, sizeof(temp), "%u", (unsigned)pdfioFileGetNumPages(dd->pdf));
|
||||
if (pdfioFileGetNumPages(dd->pdf) & 1)
|
||||
{
|
||||
// Page number on right...
|
||||
width = pdfioContentTextMeasure(dd->fonts[DOCFONT_REGULAR], temp, SIZE_HEADFOOT);
|
||||
pdfioContentTextMoveTo(dd->st, dd->crop_box.x2 - width, dd->crop_box.y1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Page number on left...
|
||||
pdfioContentTextMoveTo(dd->st, dd->crop_box.x1, dd->crop_box.y1);
|
||||
}
|
||||
|
||||
pdfioContentTextShow(dd->st, UNICODE_VALUE, temp);
|
||||
pdfioContentTextEnd(dd->st);
|
||||
|
||||
if (dd->heading)
|
||||
{
|
||||
pdfioContentTextBegin(dd->st);
|
||||
|
||||
if (pdfioFileGetNumPages(dd->pdf) & 1)
|
||||
{
|
||||
// Current heading on left...
|
||||
pdfioContentTextMoveTo(dd->st, dd->crop_box.x1, dd->crop_box.y1);
|
||||
}
|
||||
else
|
||||
{
|
||||
width = pdfioContentTextMeasure(dd->fonts[DOCFONT_REGULAR], dd->heading, SIZE_HEADFOOT);
|
||||
pdfioContentTextMoveTo(dd->st, dd->crop_box.x2 - width, dd->crop_box.y1);
|
||||
}
|
||||
|
||||
pdfioContentTextShow(dd->st, UNICODE_VALUE, dd->heading);
|
||||
pdfioContentTextEnd(dd->st);
|
||||
}
|
||||
|
||||
// The rest of the text will be full black...
|
||||
set_color(dd, DOCCOLOR_BLACK);
|
||||
|
||||
dd->y = dd->art_box.y2;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'format_block()' - Format a block of text
|
||||
//
|
||||
|
||||
static void
|
||||
format_block(docdata_t *dd, // I - Document data
|
||||
mmd_t *block, // I - Block to format
|
||||
docfont_t fontface, // I - Default font
|
||||
double fontsize, // I - Size of font
|
||||
double left, // I - Left margin
|
||||
double right, // I - Right margin
|
||||
const char *leader) // I - Leader text on the first line
|
||||
{
|
||||
mmd_type_t blocktype; // Block type
|
||||
mmd_t *current, // Current node
|
||||
*next; // Next node
|
||||
mmd_type_t curtype; // Current node type
|
||||
const char *curtext, // Current text
|
||||
*cururl; // Current URL, if any
|
||||
bool curws; // Current whitespace
|
||||
docfont_t curface, // Current font face
|
||||
prevface; // Previous font face
|
||||
double x, y; // Current position
|
||||
double width, // Width of current fragment
|
||||
lwidth, // Leader width
|
||||
wswidth; // Width of whitespace
|
||||
doccolor_t color; // Color of text
|
||||
|
||||
|
||||
blocktype = mmdGetType(block);
|
||||
|
||||
if (!dd->st)
|
||||
new_page(dd);
|
||||
|
||||
if ((y = dd->y - 2.0 * fontsize * LINE_HEIGHT) < dd->art_box.y1)
|
||||
{
|
||||
new_page(dd);
|
||||
y = dd->y - fontsize;
|
||||
}
|
||||
|
||||
if (leader)
|
||||
{
|
||||
// Add leader text on first line...
|
||||
pdfioContentSetTextFont(dd->st, docfont_names[prevface = fontface], fontsize);
|
||||
|
||||
lwidth = pdfioContentTextMeasure(dd->fonts[fontface], leader, fontsize);
|
||||
|
||||
pdfioContentTextBegin(dd->st);
|
||||
pdfioContentTextMoveTo(dd->st, left - lwidth, y);
|
||||
pdfioContentTextShow(dd->st, UNICODE_VALUE, leader);
|
||||
}
|
||||
else
|
||||
{
|
||||
// No leader text...
|
||||
prevface = DOCFONT_MAX;
|
||||
lwidth = 0.0;
|
||||
}
|
||||
|
||||
for (current = mmdGetFirstChild(block), x = left; current; current = next)
|
||||
{
|
||||
// Get information about the current node...
|
||||
curtype = mmdGetType(current);
|
||||
curtext = mmdGetText(current);
|
||||
cururl = mmdGetURL(current);
|
||||
curws = mmdGetWhitespace(current);
|
||||
|
||||
// fprintf(stderr, "current=%p, curtype=%d, curtext=\"%s\", cururl=\"%s\", curws=%s\n", (void *)current, curtype, curtext, cururl, curws ? "true" : "false");
|
||||
|
||||
// Figure out the next node under this block...
|
||||
if ((next = mmdGetFirstChild(current)) == NULL)
|
||||
{
|
||||
if ((next = mmdGetNextSibling(current)) == NULL)
|
||||
{
|
||||
mmd_t *parent; // Parent node
|
||||
|
||||
if ((parent = mmdGetParent(current)) != block)
|
||||
{
|
||||
while ((next = mmdGetNextSibling(parent)) == NULL)
|
||||
{
|
||||
if ((parent = mmdGetParent(parent)) == block)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the node...
|
||||
if (!curtext)
|
||||
continue;
|
||||
|
||||
if (curtype == MMD_TYPE_EMPHASIZED_TEXT)
|
||||
curface = DOCFONT_ITALIC;
|
||||
else if (curtype == MMD_TYPE_STRONG_TEXT)
|
||||
curface = DOCFONT_BOLD;
|
||||
else if (curtype == MMD_TYPE_CODE_TEXT)
|
||||
curface = DOCFONT_MONOSPACE;
|
||||
else
|
||||
curface = fontface;
|
||||
|
||||
if (curtype == MMD_TYPE_CODE_TEXT)
|
||||
color = DOCCOLOR_RED;
|
||||
else if (curtype == MMD_TYPE_LINKED_TEXT)
|
||||
color = DOCCOLOR_BLUE;
|
||||
else
|
||||
color = DOCCOLOR_BLACK;
|
||||
|
||||
width = pdfioContentTextMeasure(dd->fonts[curface], curtext, fontsize);
|
||||
if (curws)
|
||||
wswidth = pdfioContentTextMeasure(dd->fonts[curface], " ", fontsize);
|
||||
else
|
||||
wswidth = 0.0;
|
||||
|
||||
if (x > left && (x + width + wswidth) >= right)
|
||||
{
|
||||
// New line...
|
||||
x = left;
|
||||
y -= fontsize * LINE_HEIGHT;
|
||||
|
||||
if (y < dd->art_box.y1)
|
||||
{
|
||||
// New page...
|
||||
if (prevface != DOCFONT_MAX)
|
||||
{
|
||||
pdfioContentTextEnd(dd->st);
|
||||
prevface = DOCFONT_MAX;
|
||||
}
|
||||
|
||||
new_page(dd);
|
||||
|
||||
y = dd->y - fontsize * LINE_HEIGHT;
|
||||
}
|
||||
else
|
||||
{
|
||||
pdfioContentTextMoveTo(dd->st, lwidth, -fontsize * LINE_HEIGHT);
|
||||
lwidth = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
if (curface != prevface)
|
||||
{
|
||||
if (prevface == DOCFONT_MAX)
|
||||
{
|
||||
pdfioContentTextBegin(dd->st);
|
||||
pdfioContentTextMoveTo(dd->st, x, y);
|
||||
}
|
||||
|
||||
pdfioContentSetTextFont(dd->st, docfont_names[prevface = curface], fontsize);
|
||||
}
|
||||
|
||||
if (color != dd->color)
|
||||
set_color(dd, color);
|
||||
|
||||
if (x > left && curws)
|
||||
{
|
||||
pdfioContentTextShowf(dd->st, UNICODE_VALUE, " %s", curtext);
|
||||
x += width + wswidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
pdfioContentTextShow(dd->st, UNICODE_VALUE, curtext);
|
||||
x += width;
|
||||
}
|
||||
|
||||
if (blocktype == MMD_TYPE_CODE_BLOCK)
|
||||
{
|
||||
// Force a new line...
|
||||
x = left;
|
||||
y -= fontsize * LINE_HEIGHT;
|
||||
|
||||
if (y < dd->art_box.y1)
|
||||
{
|
||||
// New page...
|
||||
if (prevface != DOCFONT_MAX)
|
||||
{
|
||||
pdfioContentTextEnd(dd->st);
|
||||
prevface = DOCFONT_MAX;
|
||||
}
|
||||
|
||||
new_page(dd);
|
||||
|
||||
y = dd->y - fontsize * LINE_HEIGHT;
|
||||
}
|
||||
else
|
||||
{
|
||||
pdfioContentTextMoveTo(dd->st, lwidth, -fontsize * LINE_HEIGHT);
|
||||
lwidth = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End the current text block and save out position on the page...
|
||||
if (prevface != DOCFONT_MAX)
|
||||
pdfioContentTextEnd(dd->st);
|
||||
|
||||
dd->y = y;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'format_doc()' - Format a document.
|
||||
//
|
||||
|
||||
static void
|
||||
format_doc(docdata_t *dd, // I - Document data
|
||||
mmd_t *doc, // I - Document node to format
|
||||
double left, // I - Left margin
|
||||
double right) // I - Right margin
|
||||
{
|
||||
int i; // Child number
|
||||
mmd_type_t doctype; // Document node type
|
||||
mmd_t *current; // Current node
|
||||
mmd_type_t curtype; // Current node type
|
||||
char leader[32]; // Leader
|
||||
static const double heading_sizes[] = // Heading font sizes
|
||||
{
|
||||
SIZE_HEADING_1,
|
||||
SIZE_HEADING_2,
|
||||
SIZE_HEADING_3,
|
||||
SIZE_HEADING_4,
|
||||
SIZE_HEADING_5,
|
||||
SIZE_HEADING_6
|
||||
};
|
||||
|
||||
|
||||
doctype = mmdGetType(doc);
|
||||
|
||||
for (i = 1, current = mmdGetFirstChild(doc); current; i ++, current = mmdGetNextSibling(current))
|
||||
{
|
||||
switch (curtype = mmdGetType(current))
|
||||
{
|
||||
default :
|
||||
break;
|
||||
|
||||
case MMD_TYPE_BLOCK_QUOTE :
|
||||
format_doc(dd, current, left + 36.0, right - 36.0);
|
||||
break;
|
||||
|
||||
case MMD_TYPE_ORDERED_LIST :
|
||||
case MMD_TYPE_UNORDERED_LIST :
|
||||
format_doc(dd, current, left + 36.0, right);
|
||||
break;
|
||||
|
||||
case MMD_TYPE_LIST_ITEM :
|
||||
if (doctype == MMD_TYPE_ORDERED_LIST)
|
||||
{
|
||||
snprintf(leader, sizeof(leader), "%d. ", i);
|
||||
format_block(dd, current, DOCFONT_REGULAR, SIZE_BODY, left, right, leader);
|
||||
}
|
||||
else
|
||||
{
|
||||
format_block(dd, current, DOCFONT_REGULAR, SIZE_BODY, left, right, /*leader*/"• ");
|
||||
}
|
||||
break;
|
||||
|
||||
case MMD_TYPE_HEADING_1 :
|
||||
case MMD_TYPE_HEADING_2 :
|
||||
case MMD_TYPE_HEADING_3 :
|
||||
case MMD_TYPE_HEADING_4 :
|
||||
case MMD_TYPE_HEADING_5 :
|
||||
case MMD_TYPE_HEADING_6 :
|
||||
if (dd->heading)
|
||||
free(dd->heading);
|
||||
|
||||
dd->heading = mmdCopyAllText(current);
|
||||
|
||||
format_block(dd, current, DOCFONT_BOLD, heading_sizes[curtype - MMD_TYPE_HEADING_1], left, right, /*leader*/NULL);
|
||||
break;
|
||||
|
||||
case MMD_TYPE_PARAGRAPH :
|
||||
format_block(dd, current, DOCFONT_REGULAR, SIZE_BODY, left, right, /*leader*/NULL);
|
||||
break;
|
||||
|
||||
case MMD_TYPE_CODE_BLOCK :
|
||||
format_block(dd, current, DOCFONT_MONOSPACE, SIZE_BODY, left + 36.0, right, /*leader*/NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'output_cb()' - Write PDF data to the standard output...
|
||||
//
|
||||
|
||||
static ssize_t // O - Number of bytes written
|
||||
output_cb(void *output_cbdata, // I - Callback data (not used)
|
||||
const void *buffer, // I - Buffer to write
|
||||
size_t bytes) // I - Number of bytes to write
|
||||
{
|
||||
(void)output_cbdata;
|
||||
|
||||
return ((ssize_t)fwrite(buffer, 1, bytes, stdout));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'main()' - Convert markdown to PDF.
|
||||
//
|
||||
|
||||
int // O - Exit status
|
||||
main(int argc, // I - Number of command-line arguments
|
||||
char *argv[]) // I - Command-line arguments
|
||||
{
|
||||
docdata_t dd; // Document data
|
||||
docfont_t fontface; // Current font
|
||||
mmd_t *doc; // Markdown document
|
||||
const char *value; // Metadata value
|
||||
|
||||
|
||||
// Get the markdown file from the command-line...
|
||||
if (argc != 2)
|
||||
{
|
||||
fputs("Usage: md2pdf FILENANE.md >FILENAME.pdf\n", stderr);
|
||||
return (1);
|
||||
}
|
||||
|
||||
if ((doc = mmdLoad(/*root*/NULL, argv[1])) == NULL)
|
||||
return (1);
|
||||
|
||||
// Initialize the document data
|
||||
memset(&dd, 0, sizeof(dd));
|
||||
|
||||
dd.media_box.x2 = PAGE_WIDTH;
|
||||
dd.media_box.y2 = PAGE_LENGTH;
|
||||
|
||||
dd.crop_box.x1 = PAGE_LEFT;
|
||||
dd.crop_box.y1 = PAGE_FOOTER;
|
||||
dd.crop_box.x2 = PAGE_RIGHT;
|
||||
dd.crop_box.y2 = PAGE_HEADER;
|
||||
|
||||
dd.art_box.x1 = PAGE_LEFT;
|
||||
dd.art_box.y1 = PAGE_BOTTOM;
|
||||
dd.art_box.x2 = PAGE_RIGHT;
|
||||
dd.art_box.y2 = PAGE_TOP;
|
||||
|
||||
dd.title = mmdGetMetadata(doc, "title");
|
||||
|
||||
// Output a PDF file to the standard output...
|
||||
#ifdef _WIN32
|
||||
setmode(1, O_BINARY); // Force binary output on Windows
|
||||
#endif // _WIN32
|
||||
|
||||
if ((dd.pdf = pdfioFileCreateOutput(output_cb, /*output_cbdata*/NULL, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_data*/NULL)) == NULL)
|
||||
return (1);
|
||||
|
||||
if ((value = mmdGetMetadata(doc, "author")) != NULL)
|
||||
pdfioFileSetAuthor(dd.pdf, value);
|
||||
|
||||
if ((value = mmdGetMetadata(doc, "keywords")) != NULL)
|
||||
pdfioFileSetKeywords(dd.pdf, value);
|
||||
|
||||
if ((value = mmdGetMetadata(doc, "subject")) != NULL)
|
||||
pdfioFileSetSubject(dd.pdf, value);
|
||||
else if ((value = mmdGetMetadata(doc, "copyright")) != NULL)
|
||||
pdfioFileSetSubject(dd.pdf, value);
|
||||
|
||||
if (dd.title)
|
||||
pdfioFileSetTitle(dd.pdf, dd.title);
|
||||
|
||||
// Add fonts...
|
||||
for (fontface = DOCFONT_REGULAR; fontface < DOCFONT_MAX; fontface ++)
|
||||
{
|
||||
if ((dd.fonts[fontface] = pdfioFileCreateFontObjFromFile(dd.pdf, docfont_filenames[fontface], UNICODE_VALUE)) == NULL)
|
||||
return (1);
|
||||
}
|
||||
|
||||
// Parse the markdown document...
|
||||
format_doc(&dd, doc, dd.art_box.x1, dd.art_box.x2);
|
||||
|
||||
// Close the PDF and return...
|
||||
pdfioStreamClose(dd.st);
|
||||
pdfioFileClose(dd.pdf);
|
||||
|
||||
mmdFree(doc);
|
||||
|
||||
return (0);
|
||||
}
|
2381
examples/mmd.c
Normal file
2381
examples/mmd.c
Normal file
File diff suppressed because it is too large
Load Diff
112
examples/mmd.h
Normal file
112
examples/mmd.h
Normal file
@ -0,0 +1,112 @@
|
||||
//
|
||||
// Header file for miniature markdown library.
|
||||
//
|
||||
// https://www.msweet.org/mmd
|
||||
//
|
||||
// Copyright © 2017-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
|
||||
#ifndef MMD_H
|
||||
# define MMD_H
|
||||
# include <stdio.h>
|
||||
# include <stdbool.h>
|
||||
# ifdef __cplusplus
|
||||
extern "C" {
|
||||
# endif // __cplusplus
|
||||
|
||||
|
||||
//
|
||||
// Constants...
|
||||
//
|
||||
|
||||
enum mmd_option_e
|
||||
{
|
||||
MMD_OPTION_NONE = 0x00, // No markdown extensions
|
||||
MMD_OPTION_METADATA = 0x01, // Jekyll metadata extension
|
||||
MMD_OPTION_TABLES = 0x02, // Github table extension
|
||||
MMD_OPTION_TASKS = 0x04, // Github task item extension (check boxes)
|
||||
MMD_OPTION_ALL = 0x07 // All supported markdown extensions
|
||||
};
|
||||
typedef unsigned mmd_option_t;
|
||||
|
||||
typedef enum mmd_type_e
|
||||
{
|
||||
MMD_TYPE_NONE = -1,
|
||||
MMD_TYPE_DOCUMENT, // The document root
|
||||
MMD_TYPE_METADATA, // Document metadata
|
||||
MMD_TYPE_BLOCK_QUOTE, // <blockquote>
|
||||
MMD_TYPE_ORDERED_LIST, // <ol>
|
||||
MMD_TYPE_UNORDERED_LIST, // <ul>
|
||||
MMD_TYPE_LIST_ITEM, // <li>
|
||||
MMD_TYPE_TABLE, // <table>
|
||||
MMD_TYPE_TABLE_HEADER, // <thead>
|
||||
MMD_TYPE_TABLE_BODY, // <tbody>
|
||||
MMD_TYPE_TABLE_ROW, // <tr>
|
||||
MMD_TYPE_HEADING_1 = 10, // <h1>
|
||||
MMD_TYPE_HEADING_2, // <h2>
|
||||
MMD_TYPE_HEADING_3, // <h3>
|
||||
MMD_TYPE_HEADING_4, // <h4>
|
||||
MMD_TYPE_HEADING_5, // <h5>
|
||||
MMD_TYPE_HEADING_6, // <h6>
|
||||
MMD_TYPE_PARAGRAPH, // <p>
|
||||
MMD_TYPE_CODE_BLOCK, // <pre><code>
|
||||
MMD_TYPE_THEMATIC_BREAK, // <hr />
|
||||
MMD_TYPE_TABLE_HEADER_CELL, // <th>
|
||||
MMD_TYPE_TABLE_BODY_CELL_LEFT, // <td align="left">
|
||||
MMD_TYPE_TABLE_BODY_CELL_CENTER, // <td align="center">
|
||||
MMD_TYPE_TABLE_BODY_CELL_RIGHT, // <td align="right">
|
||||
MMD_TYPE_NORMAL_TEXT = 100, // Normal text
|
||||
MMD_TYPE_EMPHASIZED_TEXT, // <em>text</em>
|
||||
MMD_TYPE_STRONG_TEXT, // <strong>text</strong>
|
||||
MMD_TYPE_STRUCK_TEXT, // <del>text</del>
|
||||
MMD_TYPE_LINKED_TEXT, // <a href="link">text</a>
|
||||
MMD_TYPE_CODE_TEXT, // <code>text</code>
|
||||
MMD_TYPE_IMAGE, // <img src="link" />
|
||||
MMD_TYPE_HARD_BREAK, // <br />
|
||||
MMD_TYPE_SOFT_BREAK, // <wbr />
|
||||
MMD_TYPE_METADATA_TEXT, // name: value
|
||||
MMD_TYPE_CHECKBOX // [ ] or [x]
|
||||
} mmd_type_t;
|
||||
|
||||
|
||||
//
|
||||
// Types...
|
||||
//
|
||||
|
||||
typedef struct _mmd_s mmd_t; // Markdown node
|
||||
typedef size_t (*mmd_iocb_t)(void *cbdata, char *buffer, size_t bytes);
|
||||
// mmdLoadIO callback function
|
||||
|
||||
//
|
||||
// Functions...
|
||||
//
|
||||
|
||||
extern char *mmdCopyAllText(mmd_t *node);
|
||||
extern void mmdFree(mmd_t *node);
|
||||
extern const char *mmdGetExtra(mmd_t *node);
|
||||
extern mmd_t *mmdGetFirstChild(mmd_t *node);
|
||||
extern mmd_t *mmdGetLastChild(mmd_t *node);
|
||||
extern const char *mmdGetMetadata(mmd_t *doc, const char *keyword);
|
||||
extern mmd_t *mmdGetNextSibling(mmd_t *node);
|
||||
extern mmd_option_t mmdGetOptions(void);
|
||||
extern mmd_t *mmdGetParent(mmd_t *node);
|
||||
extern mmd_t *mmdGetPrevSibling(mmd_t *node);
|
||||
extern const char *mmdGetText(mmd_t *node);
|
||||
extern mmd_type_t mmdGetType(mmd_t *node);
|
||||
extern const char *mmdGetURL(mmd_t *node);
|
||||
extern bool mmdGetWhitespace(mmd_t *node);
|
||||
extern bool mmdIsBlock(mmd_t *node);
|
||||
extern mmd_t *mmdLoad(mmd_t *root, const char *filename);
|
||||
extern mmd_t *mmdLoadFile(mmd_t *root, FILE *fp);
|
||||
extern mmd_t *mmdLoadIO(mmd_t *root, mmd_iocb_t cb, void *cbdata);
|
||||
extern mmd_t *mmdLoadString(mmd_t *root, const char *s);
|
||||
extern void mmdSetOptions(mmd_option_t options);
|
||||
|
||||
|
||||
# ifdef __cplusplus
|
||||
}
|
||||
# endif // __cplusplus
|
||||
#endif // !MMD_H
|
53
makesrcdist
53
makesrcdist
@ -2,18 +2,61 @@
|
||||
#
|
||||
# makesrcdist - make a source distribution of pdfio.
|
||||
#
|
||||
# Usage:
|
||||
#
|
||||
# ./makesrcdist [--snapshot] VERSION
|
||||
#
|
||||
|
||||
# Support "--snapshot" option...
|
||||
if test "$1" == "--snapshot"; then
|
||||
shift
|
||||
snapshot=1
|
||||
else
|
||||
snapshot=0
|
||||
fi
|
||||
|
||||
# Get version...
|
||||
if test $# != 1; then
|
||||
echo "Usage: ./makesrcdist version"
|
||||
exit 1
|
||||
echo "Usage: ./makesrcdist [--snapshot] VERSION"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
version=$1
|
||||
|
||||
echo Creating tag for release...
|
||||
git tag -m "Tag $version" v$version
|
||||
git push origin v$version
|
||||
# Check that version number has been updated everywhere...
|
||||
if test $(grep AC_INIT configure.ac | awk '{print $2}') != "[$version],"; then
|
||||
echo "Still need to update AC_INIT version in 'configure.ac'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test $(grep PDFIO_VERSION= configure | awk -F \" '{print $2}') != "$version"; then
|
||||
echo "Still need to run 'autoconf -f'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test $(grep '<version>' pdfio_native.nuspec | sed -E -e '1,$s/^.*<version>([0-9.]+).*$/\1/') != "$version"; then
|
||||
echo "Still need to update version in 'pdfio_native.nuspec'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test $(grep '<version>' pdfio_native.redist.nuspec | sed -E -e '1,$s/^.*<version>([0-9.]+).*$/\1/') != "$version"; then
|
||||
echo "Still need to update version in 'pdfio_native.redist.nuspec'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test $(grep PDFIO_VERSION pdfio.h | awk -F \" '{print $2}') != "$version"; then
|
||||
echo "Still need to update PDFIO_VERSION in 'pdfio.h'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Tag release...
|
||||
if test $snapshot = 0; then
|
||||
echo Creating tag for release...
|
||||
git tag -m "Tag $version" v$version
|
||||
git push origin v$version
|
||||
fi
|
||||
|
||||
# Make source archives...
|
||||
echo Creating pdfio-$version.tar.gz...
|
||||
git archive --format tar --prefix=pdfio-$version/ HEAD | gzip -v9 >pdfio-$version.tar.gz
|
||||
gpg --detach-sign pdfio-$version.tar.gz
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF array functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -363,6 +363,9 @@ _pdfioArrayDebug(pdfio_array_t *a, // I - Array
|
||||
_pdfio_value_t *v; // Current value
|
||||
|
||||
|
||||
if (!a)
|
||||
return;
|
||||
|
||||
putc('[', fp);
|
||||
for (i = a->num_values, v = a->values; i > 0; i --, v ++)
|
||||
_pdfioValueDebug(v, fp);
|
||||
@ -634,6 +637,28 @@ _pdfioArrayRead(pdfio_file_t *pdf, // I - PDF file
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioArrayRemove()' - Remove an array entry.
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` otherwise
|
||||
pdfioArrayRemove(pdfio_array_t *a, // I - Array
|
||||
size_t n) // I - Index
|
||||
{
|
||||
if (!a || n >= a->num_values)
|
||||
return (false);
|
||||
|
||||
if (a->values[n].type == PDFIO_VALTYPE_BINARY)
|
||||
free(a->values[n].value.binary.data);
|
||||
|
||||
a->num_values --;
|
||||
if (n < a->num_values)
|
||||
memmove(a->values + n, a->values + n + 1, (a->num_values - n) * sizeof(_pdfio_value_t));
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioArrayWrite()' - Write an array to a PDF file.
|
||||
//
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Common support functions for pdfio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -261,7 +261,7 @@ _pdfioFilePrintf(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
// Format the string...
|
||||
va_start(ap, format);
|
||||
vsnprintf(buffer, sizeof(buffer), format, ap);
|
||||
_pdfio_vsnprintf(pdf, buffer, sizeof(buffer), format, ap);
|
||||
va_end(ap);
|
||||
|
||||
// Write it...
|
||||
|
@ -2349,7 +2349,7 @@ pdfioPageDictAddColorSpace(
|
||||
bool // O - `true` on success, `false` on failure
|
||||
pdfioPageDictAddFont(
|
||||
pdfio_dict_t *dict, // I - Page dictionary
|
||||
const char *name, // I - Font name
|
||||
const char *name, // I - Font name; must not contain spaces
|
||||
pdfio_obj_t *obj) // I - Font object
|
||||
{
|
||||
pdfio_dict_t *resources; // Resource dictionary
|
||||
|
63
pdfio-dict.c
63
pdfio-dict.c
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF dictionary functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -18,19 +18,22 @@ static int compare_pairs(_pdfio_pair_t *a, _pdfio_pair_t *b);
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioDictClear()' - Remove a key/value pair from a dictionary.
|
||||
// 'pdfioDictClear()' - Remove a key/value pair from a dictionary.
|
||||
//
|
||||
|
||||
void
|
||||
_pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary
|
||||
const char *key) // I - Key
|
||||
bool // O - `true` if cleared, `false` otherwise
|
||||
pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary
|
||||
const char *key) // I - Key
|
||||
{
|
||||
size_t idx; // Index into pairs
|
||||
_pdfio_pair_t *pair, // Current pair
|
||||
pkey; // Search key
|
||||
|
||||
|
||||
PDFIO_DEBUG("_pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key);
|
||||
PDFIO_DEBUG("pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key);
|
||||
|
||||
if (!dict || !key)
|
||||
return (false);
|
||||
|
||||
// See if the key is already set...
|
||||
if (dict->num_pairs > 0)
|
||||
@ -48,8 +51,12 @@ _pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary
|
||||
|
||||
if (idx < dict->num_pairs)
|
||||
memmove(pair, pair + 1, (dict->num_pairs - idx) * sizeof(_pdfio_pair_t));
|
||||
|
||||
return (true);
|
||||
}
|
||||
}
|
||||
|
||||
return (false);
|
||||
}
|
||||
|
||||
|
||||
@ -194,6 +201,9 @@ _pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
|
||||
_pdfio_pair_t *pair; // Current pair
|
||||
|
||||
|
||||
if (!dict)
|
||||
return;
|
||||
|
||||
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
|
||||
{
|
||||
fprintf(fp, "/%s", pair->key);
|
||||
@ -332,6 +342,18 @@ pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioDictGetKey()' - Get the key for the specified pair.
|
||||
//
|
||||
|
||||
const char * // O - Key for specified pair
|
||||
pdfioDictGetKey(pdfio_dict_t *dict, // I - Dictionary
|
||||
size_t n) // I - Pair index (`0`-based)
|
||||
{
|
||||
return ((dict && n < dict->num_pairs) ? dict->pairs[n].key : NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioDictGetName()' - Get a key name value from a dictionary.
|
||||
//
|
||||
@ -350,6 +372,17 @@ pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioDictGetNumPairs()' - Get the number of key/value pairs in a dictionary.
|
||||
//
|
||||
|
||||
size_t // O - Number of pairs
|
||||
pdfioDictGetNumPairs(pdfio_dict_t *dict)// I - Dictionary
|
||||
{
|
||||
return (dict ? dict->num_pairs : 0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioDictGetNumber()' - Get a key number value from a dictionary.
|
||||
//
|
||||
@ -426,9 +459,27 @@ pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary
|
||||
|
||||
|
||||
if (value && value->type == PDFIO_VALTYPE_STRING)
|
||||
{
|
||||
return (value->value.string);
|
||||
}
|
||||
else if (value && value->type == PDFIO_VALTYPE_BINARY && value->value.binary.datalen < 4096)
|
||||
{
|
||||
// Convert binary string to regular string...
|
||||
char temp[4096]; // Temporary string
|
||||
|
||||
memcpy(temp, value->value.binary.data, value->value.binary.datalen);
|
||||
temp[value->value.binary.datalen] = '\0';
|
||||
|
||||
free(value->value.binary.data);
|
||||
value->type = PDFIO_VALTYPE_STRING;
|
||||
value->value.string = pdfioStringCreate(dict->pdf, temp);
|
||||
|
||||
return (value->value.string);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
663
pdfio-file.c
663
pdfio-file.c
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF file functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -19,11 +19,12 @@
|
||||
|
||||
static pdfio_obj_t *add_obj(pdfio_file_t *pdf, size_t number, unsigned short generation, off_t offset);
|
||||
static int compare_objmaps(_pdfio_objmap_t *a, _pdfio_objmap_t *b);
|
||||
static pdfio_file_t *create_common(const char *filename, int fd, pdfio_output_cb_t output_cb, void *output_cbdata, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_cbdata);
|
||||
static const char *get_info_string(pdfio_file_t *pdf, const char *key);
|
||||
static struct lconv *get_lconv(void);
|
||||
static bool load_obj_stream(pdfio_obj_t *obj);
|
||||
static bool load_pages(pdfio_file_t *pdf, pdfio_obj_t *obj, size_t depth);
|
||||
static bool load_xref(pdfio_file_t *pdf, off_t xref_offset, pdfio_password_cb_t password_cb, void *password_data);
|
||||
static bool write_catalog(pdfio_file_t *pdf);
|
||||
static bool write_pages(pdfio_file_t *pdf);
|
||||
static bool write_trailer(pdfio_file_t *pdf);
|
||||
|
||||
@ -119,11 +120,8 @@ pdfioFileClose(pdfio_file_t *pdf) // I - PDF file
|
||||
{
|
||||
ret = false;
|
||||
|
||||
if (pdfioObjClose(pdf->info_obj))
|
||||
if (write_pages(pdf))
|
||||
if (write_catalog(pdf))
|
||||
if (write_trailer(pdf))
|
||||
ret = _pdfioFileFlush(pdf);
|
||||
if (pdfioObjClose(pdf->info_obj) && write_pages(pdf) && pdfioObjClose(pdf->root_obj) && write_trailer(pdf))
|
||||
ret = _pdfioFileFlush(pdf);
|
||||
}
|
||||
|
||||
if (pdf->fd >= 0 && close(pdf->fd) < 0)
|
||||
@ -172,7 +170,7 @@ pdfioFileClose(pdfio_file_t *pdf) // I - PDF file
|
||||
// CropBox for pages in the PDF file - if `NULL` then a default "Universal" size
|
||||
// of 8.27x11in (the intersection of US Letter and ISO A4) is used.
|
||||
//
|
||||
// The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
// The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
// and its data pointer - if `NULL` the default error handler is used that
|
||||
// writes error messages to `stderr`.
|
||||
//
|
||||
@ -184,122 +182,39 @@ pdfioFileCreate(
|
||||
pdfio_rect_t *media_box, // I - Default MediaBox for pages
|
||||
pdfio_rect_t *crop_box, // I - Default CropBox for pages
|
||||
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default
|
||||
void *error_data) // I - Error callback data, if any
|
||||
void *error_cbdata) // I - Error callback data, if any
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
pdfio_dict_t *dict; // Dictionary for pages object
|
||||
pdfio_dict_t *info_dict; // Dictionary for information object
|
||||
unsigned char id_value[16]; // File ID value
|
||||
int fd; // File descriptor
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioFileCreate(filename=\"%s\", version=\"%s\", media_box=%p, crop_box=%p, error_cb=%p, error_cbdata=%p)\n", filename, version, (void *)media_box, (void *)crop_box, (void *)error_cb, (void *)error_cbdata);
|
||||
|
||||
// Range check input...
|
||||
if (!filename)
|
||||
return (NULL);
|
||||
|
||||
if (!version)
|
||||
version = "2.0";
|
||||
|
||||
if (!error_cb)
|
||||
{
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_data = NULL;
|
||||
}
|
||||
|
||||
// Allocate a PDF file structure...
|
||||
if ((pdf = (pdfio_file_t *)calloc(1, sizeof(pdfio_file_t))) == NULL)
|
||||
// Create the file...
|
||||
if ((fd = open(filename, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0666)) < 0)
|
||||
{
|
||||
pdfio_file_t temp; // Dummy file
|
||||
char message[8192]; // Message string
|
||||
|
||||
temp.filename = (char *)filename;
|
||||
snprintf(message, sizeof(message), "Unable to allocate memory for PDF file - %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_data);
|
||||
snprintf(message, sizeof(message), "Unable to create '%s': %s", filename, strerror(errno));
|
||||
(error_cb)(&temp, message, error_cbdata);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdf->filename = strdup(filename);
|
||||
pdf->version = strdup(version);
|
||||
pdf->mode = _PDFIO_MODE_WRITE;
|
||||
pdf->error_cb = error_cb;
|
||||
pdf->error_data = error_data;
|
||||
pdf->permissions = PDFIO_PERMISSION_ALL;
|
||||
pdf->bufptr = pdf->buffer;
|
||||
pdf->bufend = pdf->buffer + sizeof(pdf->buffer);
|
||||
|
||||
if (media_box)
|
||||
if ((pdf = create_common(filename, fd, /*output_cb*/NULL, /*output_cbdata*/NULL, version, media_box, crop_box, error_cb, error_cbdata)) == NULL)
|
||||
{
|
||||
pdf->media_box = *media_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->media_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->media_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
if (crop_box)
|
||||
{
|
||||
pdf->crop_box = *crop_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->crop_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->crop_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
// Create the file...
|
||||
if ((pdf->fd = open(filename, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 0666)) < 0)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to create file - %s", strerror(errno));
|
||||
free(pdf->filename);
|
||||
free(pdf->version);
|
||||
free(pdf);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Write a standard PDF header...
|
||||
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
|
||||
goto error;
|
||||
|
||||
// Create the pages object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Pages");
|
||||
|
||||
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create the info object...
|
||||
if ((info_dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetDate(info_dict, "CreationDate", time(NULL));
|
||||
pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION);
|
||||
|
||||
if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create random file ID values...
|
||||
_pdfioCryptoMakeRandom(id_value, sizeof(id_value));
|
||||
|
||||
if ((pdf->id_array = pdfioArrayCreate(pdf)) != NULL)
|
||||
{
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
// Remove the newly created file if we can't create the PDF file object...
|
||||
close(fd);
|
||||
unlink(filename);
|
||||
}
|
||||
|
||||
return (pdf);
|
||||
|
||||
// Common error handling code...
|
||||
error:
|
||||
|
||||
pdfioFileClose(pdf);
|
||||
|
||||
unlink(filename);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
@ -329,6 +244,35 @@ pdfioFileCreateArrayObj(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioFileCreateNameObj()' - Create a new object in a PDF file containing a name.
|
||||
//
|
||||
// This function creates a new object with a name value in a PDF file.
|
||||
// You must call @link pdfioObjClose@ to write the object to the file.
|
||||
//
|
||||
|
||||
pdfio_obj_t * // O - New object
|
||||
pdfioFileCreateNameObj(
|
||||
pdfio_file_t *pdf, // I - PDF file
|
||||
const char *name) // I - Name value
|
||||
{
|
||||
_pdfio_value_t value; // Object value
|
||||
|
||||
|
||||
// Range check input...
|
||||
if (!pdf || !name)
|
||||
return (NULL);
|
||||
|
||||
value.type = PDFIO_VALTYPE_NAME;
|
||||
value.value.name = pdfioStringCreate(pdf, name);
|
||||
|
||||
if (!value.value.name)
|
||||
return (NULL);
|
||||
|
||||
return (_pdfioFileCreateObj(pdf, NULL, &value));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioFileCreateNumberObj()' - Create a new object in a PDF file containing a number.
|
||||
//
|
||||
@ -439,13 +383,13 @@ _pdfioFileCreateObj(
|
||||
// 'pdfioFileCreateOutput()' - Create a PDF file through an output callback.
|
||||
//
|
||||
// This function creates a new PDF file that is streamed though an output
|
||||
// callback. The "output_cb" and "output_ctx" arguments specify the output
|
||||
// callback and its context pointer which is called whenever data needs to be
|
||||
// callback. The "output_cb" and "output_cbdata" arguments specify the output
|
||||
// callback and its data pointer which is called whenever data needs to be
|
||||
// written:
|
||||
//
|
||||
// ```
|
||||
// ssize_t
|
||||
// output_cb(void *output_ctx, const void *buffer, size_t bytes)
|
||||
// output_cb(void *output_cbdata, const void *buffer, size_t bytes)
|
||||
// {
|
||||
// // Write buffer to output and return the number of bytes written
|
||||
// }
|
||||
@ -458,7 +402,7 @@ _pdfioFileCreateObj(
|
||||
// CropBox for pages in the PDF file - if `NULL` then a default "Universal" size
|
||||
// of 8.27x11in (the intersection of US Letter and ISO A4) is used.
|
||||
//
|
||||
// The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
// The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
// and its data pointer - if `NULL` the default error handler is used that
|
||||
// writes error messages to `stderr`.
|
||||
//
|
||||
@ -469,121 +413,17 @@ _pdfioFileCreateObj(
|
||||
|
||||
pdfio_file_t * // O - PDF file or `NULL` on error
|
||||
pdfioFileCreateOutput(
|
||||
pdfio_output_cb_t output_cb, // I - Output callback
|
||||
void *output_ctx, // I - Output context
|
||||
pdfio_output_cb_t output_cb, // I - Output callback function
|
||||
void *output_cbdata, // I - Output callback data
|
||||
const char *version, // I - PDF version number or `NULL` for default (2.0)
|
||||
pdfio_rect_t *media_box, // I - Default MediaBox for pages
|
||||
pdfio_rect_t *crop_box, // I - Default CropBox for pages
|
||||
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default
|
||||
void *error_data) // I - Error callback data, if any
|
||||
void *error_cbdata) // I - Error callback data, if any
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
pdfio_dict_t *dict; // Dictionary for pages object
|
||||
pdfio_dict_t *info_dict; // Dictionary for information object
|
||||
unsigned char id_value[16]; // File ID value
|
||||
PDFIO_DEBUG("pdfioFileCreate(output_cb=%p, output_cbdata=%p, version=\"%s\", media_box=%p, crop_box=%p, error_cb=%p, error_cbdata=%p)\n", (void *)output_cb, (void *)output_cbdata, version, (void *)media_box, (void *)crop_box, (void *)error_cb, (void *)error_cbdata);
|
||||
|
||||
|
||||
// Range check input...
|
||||
if (!output_cb)
|
||||
return (NULL);
|
||||
|
||||
if (!version)
|
||||
version = "2.0";
|
||||
|
||||
if (!error_cb)
|
||||
{
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_data = NULL;
|
||||
}
|
||||
|
||||
// Allocate a PDF file structure...
|
||||
if ((pdf = (pdfio_file_t *)calloc(1, sizeof(pdfio_file_t))) == NULL)
|
||||
{
|
||||
pdfio_file_t temp; // Dummy file
|
||||
char message[8192]; // Message string
|
||||
|
||||
temp.filename = (char *)"output.pdf";
|
||||
snprintf(message, sizeof(message), "Unable to allocate memory for PDF file - %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_data);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdf->filename = strdup("output.pdf");
|
||||
pdf->version = strdup(version);
|
||||
pdf->mode = _PDFIO_MODE_WRITE;
|
||||
pdf->error_cb = error_cb;
|
||||
pdf->error_data = error_data;
|
||||
pdf->permissions = PDFIO_PERMISSION_ALL;
|
||||
pdf->bufptr = pdf->buffer;
|
||||
pdf->bufend = pdf->buffer + sizeof(pdf->buffer);
|
||||
|
||||
if (media_box)
|
||||
{
|
||||
pdf->media_box = *media_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->media_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->media_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
if (crop_box)
|
||||
{
|
||||
pdf->crop_box = *crop_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->crop_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->crop_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
// Save output callback...
|
||||
pdf->fd = -1;
|
||||
pdf->output_cb = output_cb;
|
||||
pdf->output_ctx = output_ctx;
|
||||
|
||||
// Write a standard PDF header...
|
||||
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
|
||||
goto error;
|
||||
|
||||
// Create the pages object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Pages");
|
||||
|
||||
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create the info object...
|
||||
if ((info_dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetDate(info_dict, "CreationDate", time(NULL));
|
||||
pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION);
|
||||
|
||||
if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create random file ID values...
|
||||
_pdfioCryptoMakeRandom(id_value, sizeof(id_value));
|
||||
|
||||
if ((pdf->id_array = pdfioArrayCreate(pdf)) != NULL)
|
||||
{
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
}
|
||||
|
||||
return (pdf);
|
||||
|
||||
// Common error handling code...
|
||||
error:
|
||||
|
||||
pdfioFileClose(pdf);
|
||||
|
||||
return (NULL);
|
||||
return (create_common("output.pdf", /*fd*/-1, output_cb, output_cbdata, version, media_box, crop_box, error_cb, error_cbdata));
|
||||
}
|
||||
|
||||
|
||||
@ -705,13 +545,11 @@ pdfioFileCreateTemporary(
|
||||
pdfio_rect_t *media_box, // I - Default MediaBox for pages
|
||||
pdfio_rect_t *crop_box, // I - Default CropBox for pages
|
||||
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default
|
||||
void *error_data) // I - Error callback data, if any
|
||||
void *error_cbdata) // I - Error callback data, if any
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
pdfio_dict_t *dict; // Dictionary for pages object
|
||||
pdfio_dict_t *info_dict; // Dictionary for information object
|
||||
unsigned char id_value[16]; // File ID value
|
||||
int i; // Looping var
|
||||
int i, // Looping var
|
||||
fd; // File descriptor
|
||||
const char *tmpdir; // Temporary directory
|
||||
#if _WIN32 || defined(__APPLE__)
|
||||
char tmppath[256]; // Temporary directory path
|
||||
@ -719,6 +557,8 @@ pdfioFileCreateTemporary(
|
||||
unsigned tmpnum; // Temporary filename number
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioFileCreate(buffer=%p, bufsize=%lu, version=\"%s\", media_box=%p, crop_box=%p, error_cb=%p, error_cbdata=%p)\n", (void *)buffer, (unsigned long)bufsize, version, (void *)media_box, (void *)crop_box, (void *)error_cb, (void *)error_cbdata);
|
||||
|
||||
// Range check input...
|
||||
if (!buffer || bufsize < 32)
|
||||
{
|
||||
@ -727,31 +567,7 @@ pdfioFileCreateTemporary(
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (!version)
|
||||
version = "2.0";
|
||||
|
||||
if (!error_cb)
|
||||
{
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_data = NULL;
|
||||
}
|
||||
|
||||
// Allocate a PDF file structure...
|
||||
if ((pdf = (pdfio_file_t *)calloc(1, sizeof(pdfio_file_t))) == NULL)
|
||||
{
|
||||
pdfio_file_t temp; // Dummy file
|
||||
char message[8192]; // Message string
|
||||
|
||||
temp.filename = (char *)"temporary.pdf";
|
||||
snprintf(message, sizeof(message), "Unable to allocate memory for PDF file - %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_data);
|
||||
|
||||
*buffer = '\0';
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Create the file...
|
||||
// Create the temporary PDF file...
|
||||
#if _WIN32
|
||||
if ((tmpdir = getenv("TEMP")) == NULL)
|
||||
{
|
||||
@ -779,98 +595,35 @@ pdfioFileCreateTemporary(
|
||||
tmpdir = "/tmp";
|
||||
#endif // _WIN32
|
||||
|
||||
for (i = 0; i < 1000; i ++)
|
||||
for (i = 0, fd = -1; i < 1000; i ++)
|
||||
{
|
||||
_pdfioCryptoMakeRandom((uint8_t *)&tmpnum, sizeof(tmpnum));
|
||||
snprintf(buffer, bufsize, "%s/%08x.pdf", tmpdir, tmpnum);
|
||||
if ((pdf->fd = open(buffer, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, 0666)) >= 0)
|
||||
if ((fd = open(buffer, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, 0666)) >= 0)
|
||||
break;
|
||||
}
|
||||
|
||||
pdf->filename = strdup(buffer);
|
||||
|
||||
if (i >= 1000)
|
||||
if (fd < 0)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to create file - %s", strerror(errno));
|
||||
free(pdf->filename);
|
||||
free(pdf);
|
||||
*buffer = '\0';
|
||||
pdfio_file_t temp; // Dummy file
|
||||
char message[8192]; // Message string
|
||||
|
||||
temp.filename = (char *)"<temporary>";
|
||||
snprintf(message, sizeof(message), "Unable to create temporary PDF file: %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_cbdata);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdf->version = strdup(version);
|
||||
pdf->mode = _PDFIO_MODE_WRITE;
|
||||
pdf->error_cb = error_cb;
|
||||
pdf->error_data = error_data;
|
||||
pdf->permissions = PDFIO_PERMISSION_ALL;
|
||||
pdf->bufptr = pdf->buffer;
|
||||
pdf->bufend = pdf->buffer + sizeof(pdf->buffer);
|
||||
|
||||
if (media_box)
|
||||
if ((pdf = create_common(buffer, fd, /*output_cb*/NULL, /*output_cbdata*/NULL, version, media_box, crop_box, error_cb, error_cbdata)) == NULL)
|
||||
{
|
||||
pdf->media_box = *media_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->media_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->media_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
if (crop_box)
|
||||
{
|
||||
pdf->crop_box = *crop_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->crop_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->crop_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
// Write a standard PDF header...
|
||||
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
|
||||
goto error;
|
||||
|
||||
// Create the pages object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Pages");
|
||||
|
||||
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create the info object...
|
||||
if ((info_dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetDate(info_dict, "CreationDate", time(NULL));
|
||||
pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION);
|
||||
|
||||
if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create random file ID values...
|
||||
_pdfioCryptoMakeRandom(id_value, sizeof(id_value));
|
||||
|
||||
if ((pdf->id_array = pdfioArrayCreate(pdf)) != NULL)
|
||||
{
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
// Remove the temporary file if we can't create the PDF file object...
|
||||
close(fd);
|
||||
unlink(buffer);
|
||||
*buffer = '\0';
|
||||
}
|
||||
|
||||
return (pdf);
|
||||
|
||||
// Common error handling code...
|
||||
error:
|
||||
|
||||
pdfioFileClose(pdf);
|
||||
|
||||
unlink(buffer);
|
||||
*buffer = '\0';
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
@ -930,11 +683,12 @@ pdfioFileFindObj(
|
||||
if ((current = number - 1) >= pdf->num_objs)
|
||||
current = pdf->num_objs / 2;
|
||||
|
||||
PDFIO_DEBUG("pdfioFileFindObj: objs[current=%lu]=%p\n", (unsigned long)current, (void *)pdf->objs[current]);
|
||||
PDFIO_DEBUG("pdfioFileFindObj: objs[current=%lu]=%p(%lu)\n", (unsigned long)current, (void *)pdf->objs[current], (unsigned long)(pdf->objs[current] ? pdf->objs[current]->number : 0));
|
||||
|
||||
if (number == pdf->objs[current]->number)
|
||||
{
|
||||
// Fast match...
|
||||
PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)current, pdf->objs[current]);
|
||||
return (pdf->objs[current]);
|
||||
}
|
||||
else if (number < pdf->objs[current]->number)
|
||||
@ -961,11 +715,20 @@ pdfioFileFindObj(
|
||||
}
|
||||
|
||||
if (number == pdf->objs[left]->number)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)left, pdf->objs[left]);
|
||||
return (pdf->objs[left]);
|
||||
}
|
||||
else if (number == pdf->objs[right]->number)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioFileFindObj: Returning %lu (%p)\n", (unsigned long)right, pdf->objs[right]);
|
||||
return (pdf->objs[right]);
|
||||
}
|
||||
else
|
||||
{
|
||||
PDFIO_DEBUG("pdfioFileFindObj: Returning NULL\n");
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -980,6 +743,19 @@ pdfioFileGetAuthor(pdfio_file_t *pdf) // I - PDF file
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioFileGetCatalog()' - Get the document catalog dictionary.
|
||||
//
|
||||
// @since PDFio 1.3@
|
||||
//
|
||||
|
||||
pdfio_dict_t * // O - Catalog dictionary
|
||||
pdfioFileGetCatalog(pdfio_file_t *pdf) // I - PDF file
|
||||
{
|
||||
return (pdf ? pdfioObjGetDict(pdf->root_obj) : NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioFileGetCreationDate()' - Get the creation date for a PDF file.
|
||||
//
|
||||
@ -1169,13 +945,13 @@ pdfioFileGetVersion(
|
||||
// This function opens an existing PDF file. The "filename" argument specifies
|
||||
// the name of the PDF file to create.
|
||||
//
|
||||
// The "password_cb" and "password_data" arguments specify a password callback
|
||||
// The "password_cb" and "password_cbdata" arguments specify a password callback
|
||||
// and its data pointer for PDF files that use one of the standard Adobe
|
||||
// "security" handlers. The callback returns a password string or `NULL` to
|
||||
// cancel the open. If `NULL` is specified for the callback function and the
|
||||
// PDF file requires a password, the open will always fail.
|
||||
//
|
||||
// The "error_cb" and "error_data" arguments specify an error handler callback
|
||||
// The "error_cb" and "error_cbdata" arguments specify an error handler callback
|
||||
// and its data pointer - if `NULL` the default error handler is used that
|
||||
// writes error messages to `stderr`.
|
||||
//
|
||||
@ -1184,9 +960,10 @@ pdfio_file_t * // O - PDF file
|
||||
pdfioFileOpen(
|
||||
const char *filename, // I - Filename
|
||||
pdfio_password_cb_t password_cb, // I - Password callback or `NULL` for none
|
||||
void *password_data, // I - Password callback data, if any
|
||||
void *password_cbdata,
|
||||
// I - Password callback data, if any
|
||||
pdfio_error_cb_t error_cb, // I - Error callback or `NULL` for default
|
||||
void *error_data) // I - Error callback data, if any
|
||||
void *error_cbdata) // I - Error callback data, if any
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
char line[1025], // Line from file
|
||||
@ -1196,14 +973,16 @@ pdfioFileOpen(
|
||||
off_t xref_offset; // Offset to xref table
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioFileOpen(filename=\"%s\", password_cb=%p, password_cbdata=%p, error_cb=%p, error_cbdata=%p)\n", filename, (void *)password_cb, (void *)password_cbdata, (void *)error_cb, (void *)error_cbdata);
|
||||
|
||||
// Range check input...
|
||||
if (!filename)
|
||||
return (NULL);
|
||||
|
||||
if (!error_cb)
|
||||
{
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_data = NULL;
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_cbdata = NULL;
|
||||
}
|
||||
|
||||
// Allocate a PDF file structure...
|
||||
@ -1214,14 +993,15 @@ pdfioFileOpen(
|
||||
|
||||
temp.filename = (char *)filename;
|
||||
snprintf(message, sizeof(message), "Unable to allocate memory for PDF file - %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_data);
|
||||
(error_cb)(&temp, message, error_cbdata);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdf->loc = get_lconv();
|
||||
pdf->filename = strdup(filename);
|
||||
pdf->mode = _PDFIO_MODE_READ;
|
||||
pdf->error_cb = error_cb;
|
||||
pdf->error_data = error_data;
|
||||
pdf->error_data = error_cbdata;
|
||||
pdf->permissions = PDFIO_PERMISSION_ALL;
|
||||
|
||||
// Open the file...
|
||||
@ -1277,7 +1057,7 @@ pdfioFileOpen(
|
||||
|
||||
xref_offset = (off_t)strtol(ptr + 9, NULL, 10);
|
||||
|
||||
if (!load_xref(pdf, xref_offset, password_cb, password_data))
|
||||
if (!load_xref(pdf, xref_offset, password_cb, password_cbdata))
|
||||
goto error;
|
||||
|
||||
return (pdf);
|
||||
@ -1369,7 +1149,7 @@ pdfioFileSetPermissions(
|
||||
if (!pdf)
|
||||
return (false);
|
||||
|
||||
if (pdf->num_objs > 2) // First two objects are pages and info
|
||||
if (pdf->num_objs > 3) // First three objects are pages, info, and root
|
||||
{
|
||||
_pdfioFileError(pdf, "You must call pdfioFileSetPermissions before adding any objects.");
|
||||
return (false);
|
||||
@ -1531,6 +1311,144 @@ compare_objmaps(_pdfio_objmap_t *a, // I - First object map
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'create_common()' - Allocate and initialize a pdfio_file_t object for writing.
|
||||
//
|
||||
|
||||
static pdfio_file_t * // O - New PDF file
|
||||
create_common(
|
||||
const char *filename, // I - Filename
|
||||
int fd, // I - File descriptor, if any
|
||||
pdfio_output_cb_t output_cb, // I - Output callback function, if any
|
||||
void *output_cbdata, // I - Output callback data, if any
|
||||
const char *version, // I - PDF version
|
||||
pdfio_rect_t *media_box, // I - Media box or `NULL` for default
|
||||
pdfio_rect_t *crop_box, // I - Crop box of `NULL` for default
|
||||
pdfio_error_cb_t error_cb, // I - Error callback function
|
||||
void *error_cbdata) // I - Error callback data
|
||||
{
|
||||
pdfio_file_t *pdf; // New PDF file
|
||||
pdfio_dict_t *dict; // Dictionary
|
||||
unsigned char id_value[16]; // File ID value
|
||||
|
||||
|
||||
PDFIO_DEBUG("create_common(filename=\"%s\", fd=%d, output_cb=%p, output_cbdata=%p, version=\"%s\", media_box=%p, crop_box=%p, error_cb=%p, error_cbdata=%p)\n", filename, fd, (void *)output_cb, (void *)output_cbdata, version, (void *)media_box, (void *)crop_box, (void *)error_cb, (void *)error_cbdata);
|
||||
|
||||
// Range check input...
|
||||
if (!filename || (fd < 0 && !output_cb))
|
||||
return (NULL);
|
||||
|
||||
if (!version)
|
||||
version = "2.0";
|
||||
|
||||
if (!error_cb)
|
||||
{
|
||||
error_cb = _pdfioFileDefaultError;
|
||||
error_cbdata = NULL;
|
||||
}
|
||||
|
||||
// Allocate a PDF file structure...
|
||||
if ((pdf = (pdfio_file_t *)calloc(1, sizeof(pdfio_file_t))) == NULL)
|
||||
{
|
||||
pdfio_file_t temp; // Dummy file
|
||||
char message[8192]; // Message string
|
||||
|
||||
temp.filename = (char *)filename;
|
||||
snprintf(message, sizeof(message), "Unable to allocate memory for PDF file: %s", strerror(errno));
|
||||
(error_cb)(&temp, message, error_cbdata);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Initialize PDF object...
|
||||
pdf->loc = get_lconv();
|
||||
pdf->fd = fd;
|
||||
pdf->output_cb = output_cb;
|
||||
pdf->output_ctx = output_cbdata;
|
||||
pdf->filename = strdup(filename);
|
||||
pdf->version = strdup(version);
|
||||
pdf->mode = _PDFIO_MODE_WRITE;
|
||||
pdf->error_cb = error_cb;
|
||||
pdf->error_data = error_cbdata;
|
||||
pdf->permissions = PDFIO_PERMISSION_ALL;
|
||||
pdf->bufptr = pdf->buffer;
|
||||
pdf->bufend = pdf->buffer + sizeof(pdf->buffer);
|
||||
|
||||
if (media_box)
|
||||
{
|
||||
pdf->media_box = *media_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->media_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->media_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
if (crop_box)
|
||||
{
|
||||
pdf->crop_box = *crop_box;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to "universal" size (intersection of A4 and US Letter)
|
||||
pdf->crop_box.x2 = 210.0 * 72.0f / 25.4f;
|
||||
pdf->crop_box.y2 = 11.0f * 72.0f;
|
||||
}
|
||||
|
||||
// Write a standard PDF header...
|
||||
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
|
||||
goto error;
|
||||
|
||||
// Create the pages object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Pages");
|
||||
|
||||
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create the info object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetDate(dict, "CreationDate", time(NULL));
|
||||
pdfioDictSetString(dict, "Producer", "pdfio/" PDFIO_VERSION);
|
||||
|
||||
if ((pdf->info_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create the root object...
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Catalog");
|
||||
pdfioDictSetObj(dict, "Pages", pdf->pages_obj);
|
||||
|
||||
if ((pdf->root_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// Create random file ID values...
|
||||
_pdfioCryptoMakeRandom(id_value, sizeof(id_value));
|
||||
|
||||
if ((pdf->id_array = pdfioArrayCreate(pdf)) != NULL)
|
||||
{
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
pdfioArrayAppendBinary(pdf->id_array, id_value, sizeof(id_value));
|
||||
}
|
||||
|
||||
return (pdf);
|
||||
|
||||
// Common error handling code...
|
||||
error:
|
||||
|
||||
pdfioFileClose(pdf);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'get_info_string()' - Get a string value from the Info dictionary.
|
||||
//
|
||||
@ -1543,36 +1461,35 @@ get_info_string(pdfio_file_t *pdf, // I - PDF file
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
pdfio_dict_t *dict; // Info dictionary
|
||||
_pdfio_value_t *value; // Value
|
||||
|
||||
|
||||
// Range check input...
|
||||
if (!pdf || !pdf->info_obj || (dict = pdfioObjGetDict(pdf->info_obj)) == NULL || (value = _pdfioDictGetValue(dict, key)) == NULL)
|
||||
if (!pdf || !pdf->info_obj || (dict = pdfioObjGetDict(pdf->info_obj)) == NULL)
|
||||
return (NULL);
|
||||
|
||||
// If we already have a value, return it...
|
||||
if (value->type == PDFIO_VALTYPE_NAME || value->type == PDFIO_VALTYPE_STRING)
|
||||
{
|
||||
return (value->value.string);
|
||||
}
|
||||
else if (value->type == PDFIO_VALTYPE_BINARY && value->value.binary.datalen < 4096)
|
||||
{
|
||||
// Convert binary string to regular string...
|
||||
char temp[4096]; // Temporary string
|
||||
|
||||
memcpy(temp, value->value.binary.data, value->value.binary.datalen);
|
||||
temp[value->value.binary.datalen] = '\0';
|
||||
|
||||
free(value->value.binary.data);
|
||||
value->type = PDFIO_VALTYPE_STRING;
|
||||
value->value.string = pdfioStringCreate(pdf, temp);
|
||||
|
||||
return (value->value.string);
|
||||
}
|
||||
else
|
||||
return (pdfioDictGetString(dict, key));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'get_lconv()' - Get any locale-specific numeric information.
|
||||
//
|
||||
|
||||
static struct lconv * // O - Locale information or `NULL`
|
||||
get_lconv(void)
|
||||
{
|
||||
struct lconv *loc; // Locale information
|
||||
|
||||
|
||||
if ((loc = localeconv()) != NULL)
|
||||
{
|
||||
// Something else that is not a string...
|
||||
return (NULL);
|
||||
PDFIO_DEBUG("get_lconv: loc=%p, loc->decimal_point=\"%s\"\n", loc, loc->decimal_point);
|
||||
|
||||
if (!loc->decimal_point || !strcmp(loc->decimal_point, "."))
|
||||
loc = NULL;
|
||||
}
|
||||
|
||||
return (loc);
|
||||
}
|
||||
|
||||
|
||||
@ -2060,7 +1977,7 @@ load_xref(
|
||||
{
|
||||
PDFIO_DEBUG("load_xref: '%s' at offset %lu\n", line, (unsigned long)trailer_offset);
|
||||
|
||||
if (!strncmp(line, "trailer", 7) && (!line[7] || isspace(line[7] & 255)))
|
||||
if (!strncmp(line, "trailer", 7) && (!line[7] || isspace(line[7] & 255) || line[7] == '<'))
|
||||
{
|
||||
if (line[7])
|
||||
{
|
||||
@ -2218,30 +2135,6 @@ load_xref(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'write_catalog()' - Write the PDF root object/catalog.
|
||||
//
|
||||
|
||||
static bool // O - `true` on success, `false` on failure
|
||||
write_catalog(pdfio_file_t *pdf) // I - PDF file
|
||||
{
|
||||
pdfio_dict_t *dict; // Dictionary for catalog...
|
||||
|
||||
|
||||
if ((dict = pdfioDictCreate(pdf)) == NULL)
|
||||
return (false);
|
||||
|
||||
pdfioDictSetName(dict, "Type", "Catalog");
|
||||
pdfioDictSetObj(dict, "Pages", pdf->pages_obj);
|
||||
// TODO: Add support for all of the root object dictionary keys
|
||||
|
||||
if ((pdf->root_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
return (false);
|
||||
else
|
||||
return (pdfioObjClose(pdf->root_obj));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'write_pages()' - Write the PDF pages objects.
|
||||
//
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF object functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -99,7 +99,7 @@ pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
return (NULL);
|
||||
|
||||
if (dstobj->value.type == PDFIO_VALTYPE_DICT)
|
||||
_pdfioDictClear(dstobj->value.value.dict, "Length");
|
||||
pdfioDictClear(dstobj->value.value.dict, "Length");
|
||||
|
||||
if (srcobj->stream_offset)
|
||||
{
|
||||
@ -333,6 +333,26 @@ pdfioObjGetLength(pdfio_obj_t *obj) // I - Object
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioObjGetName()' - Get the name value associated with an object.
|
||||
//
|
||||
|
||||
const char * // O - Dictionary or `NULL` on error
|
||||
pdfioObjGetName(pdfio_obj_t *obj) // I - Object
|
||||
{
|
||||
if (!obj)
|
||||
return (NULL);
|
||||
|
||||
if (obj->value.type == PDFIO_VALTYPE_NONE)
|
||||
_pdfioObjLoad(obj);
|
||||
|
||||
if (obj->value.type == PDFIO_VALTYPE_NAME)
|
||||
return (obj->value.value.name);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioObjGetNumber()' - Get the object's number.
|
||||
//
|
||||
@ -347,8 +367,21 @@ pdfioObjGetNumber(pdfio_obj_t *obj) // I - Object
|
||||
//
|
||||
// 'pdfioObjGetSubtype()' - Get an object's subtype.
|
||||
//
|
||||
// This function returns an object's PDF subtype name, if any. Common subtype
|
||||
// names include:
|
||||
//
|
||||
// - "CIDFontType0": A CID Type0 font
|
||||
// - "CIDFontType2": A CID TrueType font
|
||||
// - "Image": An image or image mask
|
||||
// - "Form": A fillable form
|
||||
// - "OpenType": An OpenType font
|
||||
// - "Type0": A composite font
|
||||
// - "Type1": A PostScript Type1 font
|
||||
// - "Type3": A PDF Type3 font
|
||||
// - "TrueType": A TrueType font
|
||||
//
|
||||
|
||||
const char * // O - Object subtype
|
||||
const char * // O - Object subtype name or `NULL` for none
|
||||
pdfioObjGetSubtype(pdfio_obj_t *obj) // I - Object
|
||||
{
|
||||
pdfio_dict_t *dict; // Object dictionary
|
||||
@ -364,8 +397,21 @@ pdfioObjGetSubtype(pdfio_obj_t *obj) // I - Object
|
||||
//
|
||||
// 'pdfioObjGetType()' - Get an object's type.
|
||||
//
|
||||
// This function returns an object's PDF type name, if any. Common type names
|
||||
// include:
|
||||
//
|
||||
// - "CMap": A character map for composite fonts
|
||||
// - "Font": An embedded font (@link pdfioObjGetSubtype@ will tell you the
|
||||
// font format)
|
||||
// - "FontDescriptor": A font descriptor
|
||||
// - "Page": A (visible) page
|
||||
// - "Pages": A page tree node
|
||||
// - "Template": An invisible template page
|
||||
// - "XObject": An image, image mask, or form (@link pdfioObjGetSubtype@ will
|
||||
// tell you which)
|
||||
//
|
||||
|
||||
const char * // O - Object type
|
||||
const char * // O - Object type name or `NULL` for none
|
||||
pdfioObjGetType(pdfio_obj_t *obj) // I - Object
|
||||
{
|
||||
pdfio_dict_t *dict; // Object dictionary
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Private header file for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -20,6 +20,7 @@
|
||||
# include <errno.h>
|
||||
# include <inttypes.h>
|
||||
# include <fcntl.h>
|
||||
# include <locale.h>
|
||||
# ifdef _WIN32
|
||||
# include <io.h>
|
||||
# include <direct.h>
|
||||
@ -37,9 +38,11 @@
|
||||
# define unlink _unlink
|
||||
# define vsnprintf _vsnprintf
|
||||
# define write _write
|
||||
# define F_OK 00 // POSIX parameters/flags
|
||||
# define W_OK 02
|
||||
# define R_OK 04
|
||||
# ifndef F_OK
|
||||
# define F_OK 00 // POSIX parameters/flags
|
||||
# define W_OK 02
|
||||
# define R_OK 04
|
||||
# endif // !F_OK
|
||||
# define O_RDONLY _O_RDONLY // Map standard POSIX open flags
|
||||
# define O_WRONLY _O_WRONLY
|
||||
# define O_CREAT _O_CREAT
|
||||
@ -224,6 +227,7 @@ typedef struct _pdfio_objmap_s // PDF object map
|
||||
struct _pdfio_file_s // PDF file structure
|
||||
{
|
||||
char *filename; // Filename
|
||||
struct lconv *loc; // Locale data
|
||||
char *version; // Version number
|
||||
pdfio_rect_t media_box, // Default MediaBox value
|
||||
crop_box; // Default CropBox value
|
||||
@ -322,6 +326,9 @@ struct _pdfio_stream_s // Stream
|
||||
// Functions...
|
||||
//
|
||||
|
||||
extern double _pdfio_strtod(pdfio_file_t *pdf, const char *s) _PDFIO_INTERNAL;
|
||||
extern ssize_t _pdfio_vsnprintf(pdfio_file_t *pdf, char *buffer, size_t bufsize, const char *format, va_list ap) _PDFIO_INTERNAL;
|
||||
|
||||
extern bool _pdfioArrayDecrypt(pdfio_file_t *pdf, pdfio_obj_t *obj, pdfio_array_t *a, size_t depth) _PDFIO_INTERNAL;
|
||||
extern void _pdfioArrayDebug(pdfio_array_t *a, FILE *fp) _PDFIO_INTERNAL;
|
||||
extern void _pdfioArrayDelete(pdfio_array_t *a) _PDFIO_INTERNAL;
|
||||
@ -346,7 +353,6 @@ extern void _pdfioCryptoSHA256Init(_pdfio_sha256_t *ctx) _PDFIO_INTERNAL;
|
||||
extern void _pdfioCryptoSHA256Finish(_pdfio_sha256_t *ctx, uint8_t *Message_Digest) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioCryptoUnlock(pdfio_file_t *pdf, pdfio_password_cb_t password_cb, void *password_data) _PDFIO_INTERNAL;
|
||||
|
||||
extern void _pdfioDictClear(pdfio_dict_t *dict, const char *key) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioDictDecrypt(pdfio_file_t *pdf, pdfio_obj_t *obj, pdfio_dict_t *dict, size_t depth) _PDFIO_INTERNAL;
|
||||
extern void _pdfioDictDebug(pdfio_dict_t *dict, FILE *fp) _PDFIO_INTERNAL;
|
||||
extern void _pdfioDictDelete(pdfio_dict_t *dict) _PDFIO_INTERNAL;
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF stream functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -408,6 +408,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
pdfio_stream_t *st; // Stream
|
||||
pdfio_dict_t *dict = pdfioObjGetDict(obj);
|
||||
// Object dictionary
|
||||
const char *type; // Object type
|
||||
|
||||
|
||||
PDFIO_DEBUG("_pdfioStreamOpen(obj=%p(%u), decode=%s)\n", obj, (unsigned)obj->number, decode ? "true" : "false");
|
||||
@ -434,7 +435,9 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (obj->pdf->encryption)
|
||||
type = pdfioObjGetType(obj);
|
||||
|
||||
if (obj->pdf->encryption && (!type || strcmp(type, "XRef")))
|
||||
{
|
||||
uint8_t iv[64]; // Initialization vector
|
||||
size_t ivlen; // Length of initialization vector, if any
|
||||
@ -687,7 +690,7 @@ pdfioStreamPrintf(
|
||||
|
||||
// Format the string...
|
||||
va_start(ap, format);
|
||||
vsnprintf(buffer, sizeof(buffer), format, ap);
|
||||
_pdfio_vsnprintf(st->pdf, buffer, sizeof(buffer), format, ap);
|
||||
va_end(ap);
|
||||
|
||||
// Write the string...
|
||||
@ -1061,19 +1064,11 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
st->flate.next_out = (Bytef *)buffer;
|
||||
st->flate.avail_out = (uInt)bytes;
|
||||
|
||||
avail_in = st->flate.avail_in;
|
||||
avail_out = st->flate.avail_out;
|
||||
|
||||
if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status));
|
||||
return (-1);
|
||||
}
|
||||
else if (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Corrupt stream data.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (st->flate.next_out - (Bytef *)buffer);
|
||||
}
|
||||
|
453
pdfio-string.c
453
pdfio-string.c
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF dictionary functions for PDFio.
|
||||
// PDF string functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -14,7 +14,360 @@
|
||||
// Local functions...
|
||||
//
|
||||
|
||||
static int compare_strings(char **a, char **b);
|
||||
static size_t find_string(pdfio_file_t *pdf, const char *s, int *rdiff);
|
||||
|
||||
|
||||
//
|
||||
// '_pdfio_strtod()' - Convert a string to a double value.
|
||||
//
|
||||
// This function wraps strtod() to avoid locale issues.
|
||||
//
|
||||
|
||||
double // O - Double value
|
||||
_pdfio_strtod(pdfio_file_t *pdf, // I - PDF file
|
||||
const char *s) // I - String
|
||||
{
|
||||
char temp[64], // Temporary buffer
|
||||
*tempptr; // Pointer into temporary buffer
|
||||
|
||||
|
||||
// See if the locale has a special decimal point string...
|
||||
if (!pdf->loc)
|
||||
return (strtod(s, NULL));
|
||||
|
||||
// Copy leading sign, numbers, period, and then numbers...
|
||||
tempptr = temp;
|
||||
temp[sizeof(temp) - 1] = '\0';
|
||||
|
||||
while (*s && *s != '.')
|
||||
{
|
||||
if (tempptr < (temp + sizeof(temp) - 1))
|
||||
*tempptr++ = *s++;
|
||||
else
|
||||
return (0.0);
|
||||
}
|
||||
|
||||
if (*s == '.')
|
||||
{
|
||||
// Convert decimal point to locale equivalent...
|
||||
size_t declen = strlen(pdf->loc->decimal_point);
|
||||
// Length of decimal point
|
||||
s ++;
|
||||
|
||||
if (declen <= (sizeof(temp) - (size_t)(tempptr - temp)))
|
||||
{
|
||||
memcpy(tempptr, pdf->loc->decimal_point, declen);
|
||||
tempptr += declen;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (0.0);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy any remaining characters...
|
||||
while (*s)
|
||||
{
|
||||
if (tempptr < (temp + sizeof(temp) - 1))
|
||||
*tempptr++ = *s++;
|
||||
else
|
||||
return (0.0);
|
||||
}
|
||||
|
||||
// Nul-terminate the temporary string and convert the string...
|
||||
*tempptr = '\0';
|
||||
|
||||
return (strtod(temp, NULL));
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// '_pdfio_vsnprintf()' - Format a string.
|
||||
//
|
||||
// This function emulates vsnprintf() to avoid locale issues.
|
||||
//
|
||||
|
||||
ssize_t // O - Number of bytes
|
||||
_pdfio_vsnprintf(pdfio_file_t *pdf, // I - PDF file
|
||||
char *buffer, // I - Output buffer
|
||||
size_t bufsize, // I - Size of output buffer
|
||||
const char *format, // I - printf-style format string
|
||||
va_list ap) // I - Pointer to additional arguments
|
||||
{
|
||||
char *bufptr, // Pointer to position in buffer
|
||||
*bufend, // Pointer to end of buffer
|
||||
size, // Size character (h, l, L)
|
||||
type; // Format type character
|
||||
int width, // Width of field
|
||||
prec; // Number of characters of precision
|
||||
char tformat[100], // Temporary format string for snprintf()
|
||||
*tptr, // Pointer into temporary format
|
||||
temp[1024], // Buffer for formatted numbers
|
||||
*tempptr; // Pointer into buffer
|
||||
char *s; // Pointer to string
|
||||
ssize_t bytes; // Total number of bytes needed
|
||||
const char *dec = pdf->loc ? pdf->loc->decimal_point : ".";
|
||||
// Decimal point string
|
||||
char *decptr; // Pointer to decimal point
|
||||
|
||||
|
||||
// Loop through the format string, formatting as needed...
|
||||
bufptr = buffer;
|
||||
bufend = buffer + bufsize - 1;
|
||||
*bufend = '\0';
|
||||
bytes = 0;
|
||||
|
||||
while (*format)
|
||||
{
|
||||
if (*format == '%')
|
||||
{
|
||||
// Format character...
|
||||
tptr = tformat;
|
||||
*tptr++ = *format++;
|
||||
|
||||
if (*format == '%')
|
||||
{
|
||||
if (bufptr < bufend)
|
||||
*bufptr++ = *format;
|
||||
bytes ++;
|
||||
format ++;
|
||||
continue;
|
||||
}
|
||||
else if (strchr(" -+#\'", *format))
|
||||
{
|
||||
*tptr++ = *format++;
|
||||
}
|
||||
|
||||
if (*format == '*')
|
||||
{
|
||||
// Get width from argument...
|
||||
format ++;
|
||||
width = va_arg(ap, int);
|
||||
|
||||
snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", width);
|
||||
tptr += strlen(tptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
width = 0;
|
||||
|
||||
while (isdigit(*format & 255))
|
||||
{
|
||||
if (tptr < (tformat + sizeof(tformat) - 1))
|
||||
*tptr++ = *format;
|
||||
|
||||
width = width * 10 + *format++ - '0';
|
||||
}
|
||||
}
|
||||
|
||||
if (*format == '.')
|
||||
{
|
||||
if (tptr < (tformat + sizeof(tformat) - 1))
|
||||
*tptr++ = *format;
|
||||
|
||||
format ++;
|
||||
|
||||
if (*format == '*')
|
||||
{
|
||||
// Get precision from argument...
|
||||
format ++;
|
||||
prec = va_arg(ap, int);
|
||||
|
||||
snprintf(tptr, sizeof(tformat) - (size_t)(tptr - tformat), "%d", prec);
|
||||
tptr += strlen(tptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
prec = 0;
|
||||
|
||||
while (isdigit(*format & 255))
|
||||
{
|
||||
if (tptr < (tformat + sizeof(tformat) - 1))
|
||||
*tptr++ = *format;
|
||||
|
||||
prec = prec * 10 + *format++ - '0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (*format == 'l' && format[1] == 'l')
|
||||
{
|
||||
size = 'L';
|
||||
|
||||
if (tptr < (tformat + sizeof(tformat) - 2))
|
||||
{
|
||||
*tptr++ = 'l';
|
||||
*tptr++ = 'l';
|
||||
}
|
||||
|
||||
format += 2;
|
||||
}
|
||||
else if (*format == 'h' || *format == 'l' || *format == 'L')
|
||||
{
|
||||
if (tptr < (tformat + sizeof(tformat) - 1))
|
||||
*tptr++ = *format;
|
||||
|
||||
size = *format++;
|
||||
}
|
||||
else
|
||||
{
|
||||
size = 0;
|
||||
}
|
||||
|
||||
if (!*format)
|
||||
break;
|
||||
|
||||
if (tptr < (tformat + sizeof(tformat) - 1))
|
||||
*tptr++ = *format;
|
||||
|
||||
type = *format++;
|
||||
*tptr = '\0';
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case 'E' : // Floating point formats
|
||||
case 'G' :
|
||||
case 'e' :
|
||||
case 'f' :
|
||||
case 'g' :
|
||||
if ((size_t)(width + 2) > sizeof(temp))
|
||||
break;
|
||||
|
||||
snprintf(temp, sizeof(temp), tformat, va_arg(ap, double));
|
||||
|
||||
if ((decptr = strstr(temp, dec)) != NULL)
|
||||
{
|
||||
// Convert locale decimal point to "."
|
||||
PDFIO_DEBUG("_pdfio_vsnprintf: Before \"%s\"\n", temp);
|
||||
tempptr = decptr + strlen(dec);
|
||||
if (tempptr > (decptr + 1))
|
||||
memmove(decptr + 1, tempptr, strlen(tempptr) + 1);
|
||||
*decptr = '.';
|
||||
|
||||
// Strip trailing 0's...
|
||||
for (tempptr = temp + strlen(temp) - 1; tempptr > temp && *tempptr == '0'; tempptr --)
|
||||
*tempptr = '\0';
|
||||
|
||||
if (*tempptr == '.')
|
||||
*tempptr = '\0'; // Strip trailing decimal point
|
||||
|
||||
PDFIO_DEBUG("_pdfio_vsnprintf: After \"%s\"\n", temp);
|
||||
}
|
||||
|
||||
// Copy to the output buffer
|
||||
bytes += (int)strlen(temp);
|
||||
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
strncpy(bufptr, temp, (size_t)(bufend - bufptr - 1));
|
||||
bufptr += strlen(bufptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'B' : // Integer formats
|
||||
case 'X' :
|
||||
case 'b' :
|
||||
case 'd' :
|
||||
case 'i' :
|
||||
case 'o' :
|
||||
case 'u' :
|
||||
case 'x' :
|
||||
if ((size_t)(width + 2) > sizeof(temp))
|
||||
break;
|
||||
|
||||
# ifdef HAVE_LONG_LONG
|
||||
if (size == 'L')
|
||||
snprintf(temp, sizeof(temp), tformat, va_arg(ap, long long));
|
||||
else
|
||||
# endif // HAVE_LONG_LONG
|
||||
if (size == 'l')
|
||||
snprintf(temp, sizeof(temp), tformat, va_arg(ap, long));
|
||||
else
|
||||
snprintf(temp, sizeof(temp), tformat, va_arg(ap, int));
|
||||
|
||||
bytes += (int)strlen(temp);
|
||||
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
strncpy(bufptr, temp, (size_t)(bufend - bufptr - 1));
|
||||
bufptr += strlen(bufptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'p' : // Pointer value
|
||||
if ((size_t)(width + 2) > sizeof(temp))
|
||||
break;
|
||||
|
||||
snprintf(temp, sizeof(temp), tformat, va_arg(ap, void *));
|
||||
|
||||
bytes += (int)strlen(temp);
|
||||
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
strncpy(bufptr, temp, (size_t)(bufend - bufptr - 1));
|
||||
bufptr += strlen(bufptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'c' : // Character or character array
|
||||
bytes += width;
|
||||
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
if (width <= 1)
|
||||
{
|
||||
*bufptr++ = (char)va_arg(ap, int);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((bufptr + width) > bufend)
|
||||
width = (int)(bufend - bufptr);
|
||||
|
||||
memcpy(bufptr, va_arg(ap, char *), (size_t)width);
|
||||
bufptr += width;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 's' : // String
|
||||
if ((s = va_arg(ap, char *)) == NULL)
|
||||
s = "(null)";
|
||||
|
||||
bytes += strlen(s);
|
||||
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
strncpy(bufptr, s, (size_t)(bufend - bufptr - 1));
|
||||
bufptr += strlen(bufptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'n' : // Output number of chars so far
|
||||
*(va_arg(ap, int *)) = (int)bytes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Literal character...
|
||||
bytes ++;
|
||||
|
||||
if (bufptr < bufend)
|
||||
*bufptr++ = *format++;
|
||||
}
|
||||
}
|
||||
|
||||
// Nul-terminate the string and return the number of characters needed.
|
||||
if (bufptr < bufend)
|
||||
{
|
||||
// Everything fit in the buffer...
|
||||
*bufptr = '\0';
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfio_vsnprintf: Returning %ld \"%s\"\n", (long)bytes, buffer);
|
||||
|
||||
return (bytes);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
@ -32,8 +385,9 @@ pdfioStringCreate(
|
||||
pdfio_file_t *pdf, // I - PDF file
|
||||
const char *s) // I - Nul-terminated string
|
||||
{
|
||||
char *news; // New string
|
||||
char **match; // Matching string
|
||||
char *news; // New string
|
||||
size_t idx; // Index into strings
|
||||
int diff; // Different
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioStringCreate(pdf=%p, s=\"%s\")\n", pdf, s);
|
||||
@ -43,8 +397,17 @@ pdfioStringCreate(
|
||||
return (NULL);
|
||||
|
||||
// See if the string has already been added...
|
||||
if (pdf->num_strings > 0 && (match = (char **)bsearch(&s, pdf->strings, pdf->num_strings, sizeof(char *), (int (*)(const void *, const void *))compare_strings)) != NULL)
|
||||
return (*match);
|
||||
if (pdf->num_strings > 0)
|
||||
{
|
||||
idx = find_string(pdf, s, &diff);
|
||||
if (diff == 0)
|
||||
return (pdf->strings[idx]);
|
||||
}
|
||||
else
|
||||
{
|
||||
idx = 0;
|
||||
diff = -1;
|
||||
}
|
||||
|
||||
// Not already added, so add it...
|
||||
if ((news = strdup(s)) == NULL)
|
||||
@ -65,11 +428,17 @@ pdfioStringCreate(
|
||||
pdf->alloc_strings += 128;
|
||||
}
|
||||
|
||||
// TODO: Change to insertion sort as needed...
|
||||
pdf->strings[pdf->num_strings ++] = news;
|
||||
// Insert the string...
|
||||
if (diff > 0)
|
||||
idx ++;
|
||||
|
||||
if (pdf->num_strings > 1)
|
||||
qsort(pdf->strings, pdf->num_strings, sizeof(char *), (int (*)(const void *, const void *))compare_strings);
|
||||
PDFIO_DEBUG("pdfioStringCreate: Inserting \"%s\" at %u\n", news, (unsigned)idx);
|
||||
|
||||
if (idx < pdf->num_strings)
|
||||
memmove(pdf->strings + idx + 1, pdf->strings + idx, (pdf->num_strings - idx) * sizeof(char *));
|
||||
|
||||
pdf->strings[idx] = news;
|
||||
pdf->num_strings ++;
|
||||
|
||||
PDFIO_DEBUG("pdfioStringCreate: %lu strings\n", (unsigned long)pdf->num_strings);
|
||||
|
||||
@ -120,17 +489,67 @@ _pdfioStringIsAllocated(
|
||||
pdfio_file_t *pdf, // I - PDF file
|
||||
const char *s) // I - String
|
||||
{
|
||||
return (pdf->num_strings > 0 && bsearch(&s, pdf->strings, pdf->num_strings, sizeof(char *), (int (*)(const void *, const void *))compare_strings) != NULL);
|
||||
int diff; // Difference
|
||||
|
||||
|
||||
if (pdf->num_strings == 0)
|
||||
return (false);
|
||||
|
||||
find_string(pdf, s, &diff);
|
||||
|
||||
return (diff == 0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'compare_strings()' - Compare two strings.
|
||||
// 'find_string()' - Find an element in the array.
|
||||
//
|
||||
|
||||
static int // O - Result of comparison
|
||||
compare_strings(char **a, // I - First string
|
||||
char **b) // I - Second string
|
||||
static size_t // O - Index of match
|
||||
find_string(pdfio_file_t *pdf, // I - PDF file
|
||||
const char *s, // I - String to find
|
||||
int *rdiff) // O - Difference of match
|
||||
{
|
||||
return (strcmp(*a, *b));
|
||||
size_t left, // Left side of search
|
||||
right, // Right side of search
|
||||
current; // Current element
|
||||
int diff; // Comparison with current element
|
||||
|
||||
|
||||
// Do a binary search for the string...
|
||||
left = 0;
|
||||
right = pdf->num_strings - 1;
|
||||
|
||||
do
|
||||
{
|
||||
current = (left + right) / 2;
|
||||
diff = strcmp(s, pdf->strings[current]);
|
||||
|
||||
if (diff == 0)
|
||||
break;
|
||||
else if (diff < 0)
|
||||
right = current;
|
||||
else
|
||||
left = current;
|
||||
}
|
||||
while ((right - left) > 1);
|
||||
|
||||
if (diff != 0)
|
||||
{
|
||||
// Check the last 1 or 2 elements...
|
||||
if ((diff = strcmp(s, pdf->strings[left])) <= 0)
|
||||
{
|
||||
current = left;
|
||||
}
|
||||
else
|
||||
{
|
||||
diff = strcmp(s, pdf->strings[right]);
|
||||
current = right;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the closest string and the difference...
|
||||
*rdiff = diff;
|
||||
|
||||
return (current);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF value functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -215,6 +215,9 @@ void
|
||||
_pdfioValueDebug(_pdfio_value_t *v, // I - Value
|
||||
FILE *fp) // I - Output file
|
||||
{
|
||||
if (!v)
|
||||
return;
|
||||
|
||||
switch (v->type)
|
||||
{
|
||||
case PDFIO_VALTYPE_ARRAY :
|
||||
@ -497,7 +500,7 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
// If we get here, we have a number...
|
||||
v->type = PDFIO_VALTYPE_NUMBER;
|
||||
v->value.number = (double)strtod(token, NULL);
|
||||
v->value.number = _pdfio_strtod(pdf, token);
|
||||
}
|
||||
else if (!strcmp(token, "true") || !strcmp(token, "false"))
|
||||
{
|
||||
|
11
pdfio.h
11
pdfio.h
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Public header file for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -23,7 +23,7 @@ extern "C" {
|
||||
// Version number...
|
||||
//
|
||||
|
||||
# define PDFIO_VERSION "1.2.0"
|
||||
# define PDFIO_VERSION "1.4.0"
|
||||
|
||||
|
||||
//
|
||||
@ -151,7 +151,9 @@ extern pdfio_obj_t *pdfioArrayGetObj(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC;
|
||||
extern size_t pdfioArrayGetSize(pdfio_array_t *a) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioArrayGetString(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC;
|
||||
extern pdfio_valtype_t pdfioArrayGetType(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC;
|
||||
extern bool pdfioArrayRemove(pdfio_array_t *a, size_t n) _PDFIO_PUBLIC;
|
||||
|
||||
extern bool pdfioDictClear(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioDictCopy(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioDictCreate(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern pdfio_array_t *pdfioDictGetArray(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
@ -159,7 +161,9 @@ extern unsigned char *pdfioDictGetBinary(pdfio_dict_t *dict, const char *key, si
|
||||
extern bool pdfioDictGetBoolean(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern time_t pdfioDictGetDate(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioDictGetDict(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioDictGetKey(pdfio_dict_t *dict, size_t n) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioDictGetName(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern size_t pdfioDictGetNumPairs(pdfio_dict_t *dict) _PDFIO_PUBLIC;
|
||||
extern double pdfioDictGetNumber(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC;
|
||||
@ -182,6 +186,7 @@ extern bool pdfioDictSetStringf(pdfio_dict_t *dict, const char *key, const char
|
||||
extern bool pdfioFileClose(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern pdfio_file_t *pdfioFileCreate(const char *filename, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioFileCreateArrayObj(pdfio_file_t *pdf, pdfio_array_t *array) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioFileCreateNameObj(pdfio_file_t *pdf, const char *name) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioFileCreateNumberObj(pdfio_file_t *pdf, double number) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioFileCreateObj(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC;
|
||||
extern pdfio_file_t *pdfioFileCreateOutput(pdfio_output_cb_t output_cb, void *output_ctx, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC;
|
||||
@ -191,6 +196,7 @@ extern pdfio_obj_t *pdfioFileCreateStringObj(pdfio_file_t *pdf, const char *s) _
|
||||
extern pdfio_file_t *pdfioFileCreateTemporary(char *buffer, size_t bufsize, const char *version, pdfio_rect_t *media_box, pdfio_rect_t *crop_box, pdfio_error_cb_t error_cb, void *error_data) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioFileFindObj(pdfio_file_t *pdf, size_t number) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioFileGetAuthor(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioFileGetCatalog(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern time_t pdfioFileGetCreationDate(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioFileGetCreator(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
extern pdfio_array_t *pdfioFileGetID(pdfio_file_t *pdf) _PDFIO_PUBLIC;
|
||||
@ -221,6 +227,7 @@ extern pdfio_array_t *pdfioObjGetArray(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioObjGetDict(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern unsigned short pdfioObjGetGeneration(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern size_t pdfioObjGetLength(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioObjGetName(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern size_t pdfioObjGetNumber(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioObjGetSubtype(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioObjGetType(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
|
15
pdfio1.def
15
pdfio1.def
@ -1,7 +1,8 @@
|
||||
LIBRARY pdfio1
|
||||
VERSION 1.2
|
||||
VERSION 1.4
|
||||
EXPORTS
|
||||
_pdfioArrayDebug
|
||||
_pdfioArrayDecrypt
|
||||
_pdfioArrayDelete
|
||||
_pdfioArrayGetValue
|
||||
_pdfioArrayRead
|
||||
@ -22,8 +23,8 @@ _pdfioCryptoSHA256Append
|
||||
_pdfioCryptoSHA256Finish
|
||||
_pdfioCryptoSHA256Init
|
||||
_pdfioCryptoUnlock
|
||||
_pdfioDictClear
|
||||
_pdfioDictDebug
|
||||
_pdfioDictDecrypt
|
||||
_pdfioDictDelete
|
||||
_pdfioDictGetValue
|
||||
_pdfioDictRead
|
||||
@ -61,9 +62,12 @@ _pdfioTokenPush
|
||||
_pdfioTokenRead
|
||||
_pdfioValueCopy
|
||||
_pdfioValueDebug
|
||||
_pdfioValueDecrypt
|
||||
_pdfioValueDelete
|
||||
_pdfioValueRead
|
||||
_pdfioValueWrite
|
||||
_pdfio_strtod
|
||||
_pdfio_vsnprintf
|
||||
pdfioArrayAppendArray
|
||||
pdfioArrayAppendBinary
|
||||
pdfioArrayAppendBoolean
|
||||
@ -91,6 +95,7 @@ pdfioArrayGetObj
|
||||
pdfioArrayGetSize
|
||||
pdfioArrayGetString
|
||||
pdfioArrayGetType
|
||||
pdfioArrayRemove
|
||||
pdfioContentClip
|
||||
pdfioContentDrawImage
|
||||
pdfioContentFill
|
||||
@ -148,6 +153,7 @@ pdfioContentTextNextLine
|
||||
pdfioContentTextShow
|
||||
pdfioContentTextShowJustified
|
||||
pdfioContentTextShowf
|
||||
pdfioDictClear
|
||||
pdfioDictCopy
|
||||
pdfioDictCreate
|
||||
pdfioDictGetArray
|
||||
@ -155,7 +161,9 @@ pdfioDictGetBinary
|
||||
pdfioDictGetBoolean
|
||||
pdfioDictGetDate
|
||||
pdfioDictGetDict
|
||||
pdfioDictGetKey
|
||||
pdfioDictGetName
|
||||
pdfioDictGetNumPairs
|
||||
pdfioDictGetNumber
|
||||
pdfioDictGetObj
|
||||
pdfioDictGetRect
|
||||
@ -182,6 +190,7 @@ pdfioFileCreateFontObjFromFile
|
||||
pdfioFileCreateICCObjFromFile
|
||||
pdfioFileCreateImageObjFromData
|
||||
pdfioFileCreateImageObjFromFile
|
||||
pdfioFileCreateNameObj
|
||||
pdfioFileCreateNumberObj
|
||||
pdfioFileCreateObj
|
||||
pdfioFileCreateOutput
|
||||
@ -190,6 +199,7 @@ pdfioFileCreateStringObj
|
||||
pdfioFileCreateTemporary
|
||||
pdfioFileFindObj
|
||||
pdfioFileGetAuthor
|
||||
pdfioFileGetCatalog
|
||||
pdfioFileGetCreationDate
|
||||
pdfioFileGetCreator
|
||||
pdfioFileGetID
|
||||
@ -222,6 +232,7 @@ pdfioObjGetArray
|
||||
pdfioObjGetDict
|
||||
pdfioObjGetGeneration
|
||||
pdfioObjGetLength
|
||||
pdfioObjGetName
|
||||
pdfioObjGetNumber
|
||||
pdfioObjGetSubtype
|
||||
pdfioObjGetType
|
||||
|
@ -3,7 +3,7 @@
|
||||
<metadata>
|
||||
<id>pdfio_native</id>
|
||||
<title>PDFio Library for VS2019+</title>
|
||||
<version>1.2.0</version>
|
||||
<version>1.3.2</version>
|
||||
<authors>Michael R Sweet</authors>
|
||||
<owners>michaelrsweet</owners>
|
||||
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
|
||||
@ -16,7 +16,7 @@
|
||||
<copyright>Copyright © 2019-2024 by Michael R Sweet</copyright>
|
||||
<tags>pdf file native</tags>
|
||||
<dependencies>
|
||||
<dependency id="pdfio_native.redist" version="1.2.0" />
|
||||
<dependency id="pdfio_native.redist" version="1.3.2" />
|
||||
<dependency id="zlib_native.redist" version="1.2.11" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
|
@ -3,7 +3,7 @@
|
||||
<metadata>
|
||||
<id>pdfio_native.redist</id>
|
||||
<title>PDFio Library for VS2019+</title>
|
||||
<version>1.2.0</version>
|
||||
<version>1.3.2</version>
|
||||
<authors>Michael R Sweet</authors>
|
||||
<owners>michaelrsweet</owners>
|
||||
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
|
||||
|
16
runtests.bat
Normal file
16
runtests.bat
Normal file
@ -0,0 +1,16 @@
|
||||
:: Script to run unit test program
|
||||
::
|
||||
:: Usage:
|
||||
::
|
||||
:: .\runtests.bat x64\{Debug|Release}
|
||||
::
|
||||
|
||||
:: Copy dependent DLLs to the named build directory
|
||||
echo Copying DLLs
|
||||
copy packages\zlib_native.redist.1.2.11\build\native\bin\x64\Debug\*.dll %1
|
||||
copy packages\zlib_native.redist.1.2.11\build\native\bin\x64\Release\*.dll %1
|
||||
|
||||
:: Run unit test program
|
||||
echo Running %1\testpdfio.exe
|
||||
cd %1
|
||||
testpdfio.exe
|
173
testpdfio.c
173
testpdfio.c
@ -1,7 +1,7 @@
|
||||
//
|
||||
// Test program for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2023 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -16,6 +16,7 @@
|
||||
#include "pdfio-private.h"
|
||||
#include "pdfio-content.h"
|
||||
#include <math.h>
|
||||
#include <locale.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846264338327950288
|
||||
#endif // M_PI
|
||||
@ -26,7 +27,7 @@
|
||||
//
|
||||
|
||||
static int do_crypto_tests(void);
|
||||
static int do_test_file(const char *filename, int objnum, bool verbose);
|
||||
static int do_test_file(const char *filename, int objnum, const char *password, bool verbose);
|
||||
static int do_unit_tests(void);
|
||||
static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label);
|
||||
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
|
||||
@ -36,6 +37,7 @@ static const char *password_cb(void *data, const char *filename);
|
||||
static int read_unit_file(const char *filename, size_t num_pages, size_t first_image, bool is_output);
|
||||
static ssize_t token_consume_cb(const char **s, size_t bytes);
|
||||
static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes);
|
||||
static int usage(FILE *fp);
|
||||
static int verify_image(pdfio_file_t *pdf, size_t number);
|
||||
static int write_alpha_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
|
||||
static int write_color_patch(pdfio_stream_t *st, bool device);
|
||||
@ -47,7 +49,7 @@ static int write_images_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
|
||||
static int write_jpeg_test(pdfio_file_t *pdf, const char *title, int number, pdfio_obj_t *font, pdfio_obj_t *image);
|
||||
static int write_png_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
|
||||
static int write_text_test(pdfio_file_t *pdf, int first_page, pdfio_obj_t *font, const char *filename);
|
||||
static int write_unit_file(pdfio_file_t *inpdf, pdfio_file_t *outpdf, size_t *num_pages, size_t *first_image);
|
||||
static int write_unit_file(pdfio_file_t *inpdf, const char *outname, pdfio_file_t *outpdf, size_t *num_pages, size_t *first_image);
|
||||
|
||||
|
||||
//
|
||||
@ -58,20 +60,33 @@ int // O - Exit status
|
||||
main(int argc, // I - Number of command-line arguments
|
||||
char *argv[]) // I - Command-line arguments
|
||||
{
|
||||
int ret = 0; // Return value
|
||||
int ret = 0; // Return value
|
||||
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
int i; // Looping var
|
||||
const char *password = NULL; // Password
|
||||
bool verbose = false; // Be verbose?
|
||||
|
||||
for (i = 1; i < argc; i ++)
|
||||
{
|
||||
if (!strcmp(argv[i], "--help"))
|
||||
{
|
||||
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]");
|
||||
return (0);
|
||||
return (usage(stdout));
|
||||
}
|
||||
else if (!strcmp(argv[i], "--password"))
|
||||
{
|
||||
i ++;
|
||||
if (i < argc)
|
||||
{
|
||||
password = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
fputs("testpdfio: Missing password after '--password'.\n", stderr);
|
||||
return (usage(stderr));
|
||||
}
|
||||
}
|
||||
else if (!strcmp(argv[i], "--verbose"))
|
||||
{
|
||||
@ -79,24 +94,27 @@ main(int argc, // I - Number of command-line arguments
|
||||
}
|
||||
else if (argv[i][0] == '-')
|
||||
{
|
||||
printf("Unknown option '%s'.\n\n", argv[i]);
|
||||
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]");
|
||||
return (1);
|
||||
fprintf(stderr, "testpdfio: Unknown option '%s'.\n", argv[i]);
|
||||
return (usage(stderr));
|
||||
}
|
||||
else if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255))
|
||||
{
|
||||
// filename.pdf object-number
|
||||
if (do_test_file(argv[i], atoi(argv[i + 1]), verbose))
|
||||
if (do_test_file(argv[i], atoi(argv[i + 1]), password, verbose))
|
||||
ret = 1;
|
||||
|
||||
i ++;
|
||||
}
|
||||
else if (do_test_file(argv[i], 0, verbose))
|
||||
else if (do_test_file(argv[i], 0, password, verbose))
|
||||
{
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "testpdfio: Test locale is \"%s\".\n", setlocale(LC_ALL, getenv("LANG")));
|
||||
|
||||
#if _WIN32
|
||||
// Windows puts executables in Platform/Configuration subdirs...
|
||||
if (!_access("../../testfiles", 0))
|
||||
@ -360,6 +378,7 @@ do_crypto_tests(void)
|
||||
static int // O - Exit status
|
||||
do_test_file(const char *filename, // I - PDF filename
|
||||
int objnum, // I - Object number to dump, if any
|
||||
const char *password, // I - Password for file
|
||||
bool verbose) // I - Be verbose?
|
||||
{
|
||||
bool error = false; // Have we shown an error yet?
|
||||
@ -378,7 +397,7 @@ do_test_file(const char *filename, // I - PDF filename
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
if ((pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_data*/NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
|
||||
if ((pdf = pdfioFileOpen(filename, password_cb, (void *)password, (pdfio_error_cb_t)error_cb, &error)) != NULL)
|
||||
{
|
||||
if (objnum)
|
||||
{
|
||||
@ -1041,7 +1060,7 @@ do_unit_tests(void)
|
||||
else
|
||||
goto fail;
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-out.pdf", outpdf, &num_pages, &first_image))
|
||||
goto fail;
|
||||
|
||||
if (read_unit_file("testpdfio-out.pdf", num_pages, first_image, false))
|
||||
@ -1060,7 +1079,7 @@ do_unit_tests(void)
|
||||
else
|
||||
goto fail;
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-out2.pdf", outpdf, &num_pages, &first_image))
|
||||
goto fail;
|
||||
|
||||
close(outfd);
|
||||
@ -1081,7 +1100,7 @@ do_unit_tests(void)
|
||||
else
|
||||
return (1);
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-rc4.pdf", outpdf, &num_pages, &first_image))
|
||||
return (1);
|
||||
|
||||
if (read_unit_file("testpdfio-rc4.pdf", num_pages, first_image, false))
|
||||
@ -1100,7 +1119,7 @@ do_unit_tests(void)
|
||||
else
|
||||
return (1);
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-rc4p.pdf", outpdf, &num_pages, &first_image))
|
||||
return (1);
|
||||
|
||||
if (read_unit_file("testpdfio-rc4p.pdf", num_pages, first_image, false))
|
||||
@ -1118,7 +1137,7 @@ do_unit_tests(void)
|
||||
else
|
||||
return (1);
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-aes.pdf", outpdf, &num_pages, &first_image))
|
||||
return (1);
|
||||
|
||||
if (read_unit_file("testpdfio-aes.pdf", num_pages, first_image, false))
|
||||
@ -1136,7 +1155,7 @@ do_unit_tests(void)
|
||||
else
|
||||
return (1);
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "testpdfio-aesp.pdf", outpdf, &num_pages, &first_image))
|
||||
return (1);
|
||||
|
||||
if (read_unit_file("testpdfio-aesp.pdf", num_pages, first_image, false))
|
||||
@ -1148,7 +1167,7 @@ do_unit_tests(void)
|
||||
else
|
||||
return (1);
|
||||
|
||||
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
|
||||
if (write_unit_file(inpdf, "<temporary>", outpdf, &num_pages, &first_image))
|
||||
return (1);
|
||||
|
||||
if (read_unit_file(temppdf, num_pages, first_image, false))
|
||||
@ -1305,6 +1324,7 @@ read_unit_file(const char *filename, // I - File to read
|
||||
bool is_output) // I - File written with output callback?
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
pdfio_dict_t *catalog; // Catalog dictionary
|
||||
size_t i; // Looping var
|
||||
const char *s; // String
|
||||
bool error = false; // Error callback data
|
||||
@ -1317,6 +1337,83 @@ read_unit_file(const char *filename, // I - File to read
|
||||
else
|
||||
return (1);
|
||||
|
||||
// Get the root object/catalog dictionary
|
||||
fputs("pdfioFileGetCatalog: ", stdout);
|
||||
if ((catalog = pdfioFileGetCatalog(pdf)) != NULL)
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected dictionary)");
|
||||
return (1);
|
||||
}
|
||||
|
||||
// Verify some catalog values...
|
||||
fputs("pdfioDictGetName(PageLayout): ", stdout);
|
||||
if ((s = pdfioDictGetName(catalog, "PageLayout")) != NULL && !strcmp(s, "SinglePage"))
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else if (s)
|
||||
{
|
||||
printf("FAIL (got '%s', expected 'SinglePage')\n", s);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected 'SinglePage')");
|
||||
return (1);
|
||||
}
|
||||
|
||||
fputs("pdfioDictGetName(PageLayout): ", stdout);
|
||||
if ((s = pdfioDictGetName(catalog, "PageLayout")) != NULL && !strcmp(s, "SinglePage"))
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else if (s)
|
||||
{
|
||||
printf("FAIL (got '%s', expected 'SinglePage')\n", s);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected 'SinglePage')");
|
||||
return (1);
|
||||
}
|
||||
|
||||
fputs("pdfioDictGetName(PageMode): ", stdout);
|
||||
if ((s = pdfioDictGetName(catalog, "PageMode")) != NULL && !strcmp(s, "UseThumbs"))
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else if (s)
|
||||
{
|
||||
printf("FAIL (got '%s', expected 'UseThumbs')\n", s);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected 'UseThumbs')");
|
||||
return (1);
|
||||
}
|
||||
|
||||
fputs("pdfioDictGetString(Lang): ", stdout);
|
||||
if ((s = pdfioDictGetString(catalog, "Lang")) != NULL && !strcmp(s, "en"))
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else if (s)
|
||||
{
|
||||
printf("FAIL (got '%s', expected 'en')\n", s);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected 'en')");
|
||||
return (1);
|
||||
}
|
||||
|
||||
// Verify metadata...
|
||||
fputs("pdfioFileGetAuthor: ", stdout);
|
||||
if ((s = pdfioFileGetAuthor(pdf)) != NULL && !strcmp(s, "Michael R Sweet"))
|
||||
@ -1478,6 +1575,23 @@ token_peek_cb(const char **s, // IO - Test string
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'usage()' - Show program usage.
|
||||
//
|
||||
|
||||
static int // O - Exit status
|
||||
usage(FILE *fp) // I - Output file
|
||||
{
|
||||
fputs("Usage: ./testpdfio [OPTIONS] [FILENAME [OBJNUM]] ...\n", fp);
|
||||
fputs("Options:\n", fp);
|
||||
fputs(" --help Show program help.\n", fp);
|
||||
fputs(" --password PASSWORD Set PDF password.\n", fp);
|
||||
fputs(" --verbose Be verbose.\n", fp);
|
||||
|
||||
return (fp != stdout);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'verify_image()' - Verify an image object.
|
||||
//
|
||||
@ -3243,6 +3357,7 @@ write_text_test(pdfio_file_t *pdf, // I - PDF file
|
||||
static int // O - Exit status
|
||||
write_unit_file(
|
||||
pdfio_file_t *inpdf, // I - Input PDF file
|
||||
const char *outname, // I - Output PDF file name
|
||||
pdfio_file_t *outpdf, // I - Output PDF file
|
||||
size_t *num_pages, // O - Number of pages
|
||||
size_t *first_image) // O - First image object
|
||||
@ -3252,8 +3367,26 @@ write_unit_file(
|
||||
*gray_jpg, // gray.jpg image
|
||||
*helvetica, // Helvetica font
|
||||
*page; // Page from test PDF file
|
||||
pdfio_dict_t *catalog; // Catalog dictionary
|
||||
|
||||
|
||||
// Get the root object/catalog dictionary
|
||||
fputs("pdfioFileGetCatalog: ", stdout);
|
||||
if ((catalog = pdfioFileGetCatalog(outpdf)) != NULL)
|
||||
{
|
||||
puts("PASS");
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL (got NULL, expected dictionary)");
|
||||
return (1);
|
||||
}
|
||||
|
||||
// Set some catalog values...
|
||||
pdfioDictSetName(catalog, "PageLayout", "SinglePage");
|
||||
pdfioDictSetName(catalog, "PageMode", "UseThumbs");
|
||||
pdfioDictSetString(catalog, "Lang", "en");
|
||||
|
||||
// Set info values...
|
||||
fputs("pdfioFileGet/SetAuthor: ", stdout);
|
||||
pdfioFileSetAuthor(outpdf, "Michael R Sweet");
|
||||
@ -3437,7 +3570,7 @@ write_unit_file(
|
||||
}
|
||||
|
||||
// Close the new PDF file...
|
||||
fputs("pdfioFileClose(...): ", stdout);
|
||||
printf("pdfioFileClose(\"%s\"): ", outname);
|
||||
if (pdfioFileClose(outpdf))
|
||||
puts("PASS");
|
||||
else
|
||||
|
@ -3,7 +3,7 @@
|
||||
//
|
||||
// https://github.com/michaelrsweet/ttf
|
||||
//
|
||||
// Copyright © 2018-2023 by Michael R Sweet.
|
||||
// Copyright © 2018-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -120,6 +120,7 @@ test_font(const char *filename) // I - Font filename
|
||||
|
||||
|
||||
printf("ttfCreate(\"%s\"): ", filename);
|
||||
fflush(stdout);
|
||||
if ((font = ttfCreate(filename, 0, error_cb, NULL)) != NULL)
|
||||
puts("PASS");
|
||||
else
|
||||
|
127
ttf.c
127
ttf.c
@ -3,7 +3,7 @@
|
||||
//
|
||||
// https://github.com/michaelrsweet/ttf
|
||||
//
|
||||
// Copyright © 2018-2023 by Michael R Sweet.
|
||||
// Copyright © 2018-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -62,7 +62,7 @@
|
||||
# define O_CREAT _O_CREAT
|
||||
# define O_TRUNC _O_TRUNC
|
||||
|
||||
typedef __int64 ssize_t; // POSIX type not present on Windows...
|
||||
typedef __int64 ssize_t; // POSIX type not present on Windows... @private@
|
||||
|
||||
#else
|
||||
# include <unistd.h>
|
||||
@ -99,6 +99,8 @@ typedef __int64 ssize_t; // POSIX type not present on Windows...
|
||||
//
|
||||
|
||||
#define TTF_FONT_MAX_CHAR 262144 // Maximum number of character values
|
||||
#define TTF_FONT_MAX_GROUPS 65536 // Maximum number of sub-groups
|
||||
#define TTF_FONT_MAX_NAMES 16777216// Maximum size of names table we support
|
||||
|
||||
|
||||
//
|
||||
@ -254,7 +256,7 @@ typedef struct _ttf_off_hhea_s // Horizontal header
|
||||
{
|
||||
short ascender, // Ascender
|
||||
descender; // Descender
|
||||
int numberOfHMetrics; // Number of horizontal metrics
|
||||
unsigned short numberOfHMetrics; // Number of horizontal metrics
|
||||
} _ttf_off_hhea_t;
|
||||
|
||||
typedef struct _ttf_off_os_2_s // OS/2 information
|
||||
@ -297,7 +299,28 @@ static unsigned seek_table(ttf_t *font, unsigned tag, unsigned offset, bool requ
|
||||
|
||||
|
||||
//
|
||||
// 'ttfCreate()' - Create a new font object for the named font family.
|
||||
// 'ttfCreate()' - Create a new font object for the named font file.
|
||||
//
|
||||
// This function creates a new font object for the named TrueType or OpenType
|
||||
// font file or collection. The "filename" argument specifies the name of the
|
||||
// file to read.
|
||||
//
|
||||
// The "idx" argument specifies the font to load from a collection - the first
|
||||
// font is number `0`. Once created, you can call the @link ttfGetNumFonts@
|
||||
// function to determine whether the loaded font file is a collection with more
|
||||
// than one font.
|
||||
//
|
||||
// The "err_cb" and "err_data" arguments specify a callback function and data
|
||||
// pointer for receiving error messages. If `NULL`, errors are sent to the
|
||||
// `stderr` file. The callback function receives the data pointer and a text
|
||||
// message string, for example:
|
||||
//
|
||||
// ```
|
||||
// void my_err_cb(void *err_data, const char *message)
|
||||
// {
|
||||
// fprintf(stderr, "ERROR: %s\n", message);
|
||||
// }
|
||||
// ```
|
||||
//
|
||||
|
||||
ttf_t * // O - New font object
|
||||
@ -550,6 +573,10 @@ ttfGetAscent(ttf_t *font) // I - Font
|
||||
//
|
||||
// 'ttfGetBounds()' - Get the bounds of all characters in a font.
|
||||
//
|
||||
// This function gets the bounds of all characters in a font. The "bounds"
|
||||
// argument is a pointer to a `ttf_rect_t` structure that will be filled with
|
||||
// the limits for characters in the font scaled to a 1000x1000 unit square.
|
||||
//
|
||||
|
||||
ttf_rect_t * // O - Bounds or `NULL` on error
|
||||
ttfGetBounds(ttf_t *font, // I - Font
|
||||
@ -631,8 +658,11 @@ ttfGetDescent(ttf_t *font) // I - Font
|
||||
//
|
||||
// 'ttfGetExtents()' - Get the extents of a UTF-8 string.
|
||||
//
|
||||
// This function computes the extents of a UTF-8 string when rendered using the
|
||||
// specified font and size.
|
||||
// This function computes the extents of the UTF-8 string "s" when rendered
|
||||
// using the specified font "font" and size "size". The "extents" argument is
|
||||
// a pointer to a `ttf_rect_t` structure that is filled with the extents of a
|
||||
// simple rendering of the string with no kerning or rewriting applied. The
|
||||
// values are scaled using the specified font size.
|
||||
//
|
||||
|
||||
ttf_rect_t * // O - Pointer to extents or `NULL` on error
|
||||
@ -1272,20 +1302,38 @@ read_cmap(ttf_t *font) // I - Font
|
||||
for (i = 0; i < numGlyphIdArray; i ++)
|
||||
glyphIdArray[i] = read_ushort(font);
|
||||
|
||||
#ifdef DEBUG
|
||||
for (i = 0; i < segCount; i ++)
|
||||
TTF_DEBUG("read_cmap: segment[%d].startCode=%d, endCode=%d, idDelta=%d, idRangeOffset=%d\n", i, segments[i].startCode, segments[i].endCode, segments[i].idDelta, segments[i].idRangeOffset);
|
||||
for (i = 0, segment = segments; i < segCount; i ++, segment ++)
|
||||
{
|
||||
TTF_DEBUG("read_cmap: segment[%d].startCode=%d, endCode=%d, idDelta=%d, idRangeOffset=%d\n", i, segment->startCode, segment->endCode, segment->idDelta, segment->idRangeOffset);
|
||||
|
||||
if (segment->startCode > segment->endCode)
|
||||
{
|
||||
errorf(font, "Bad cmap table segment %u to %u.", segments->startCode, segment->endCode);
|
||||
free(segments);
|
||||
free(glyphIdArray);
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Based on the end code of the segment table, allocate space for the
|
||||
// uncompressed cmap table...
|
||||
if (segment->endCode >= font->num_cmap)
|
||||
font->num_cmap = segment->endCode + 1;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
for (i = 0; i < numGlyphIdArray; i ++)
|
||||
TTF_DEBUG("read_cmap: glyphIdArray[%d]=%d\n", i, glyphIdArray[i]);
|
||||
#endif /* DEBUG */
|
||||
|
||||
// Based on the end code of the segent table, allocate space for the
|
||||
// uncompressed cmap table...
|
||||
// segCount --; // Last segment is not used (sigh)
|
||||
if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
|
||||
{
|
||||
errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
|
||||
free(segments);
|
||||
free(glyphIdArray);
|
||||
return (false);
|
||||
}
|
||||
|
||||
font->num_cmap = segments[segCount - 1].endCode + 1;
|
||||
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
|
||||
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
|
||||
|
||||
if (!font->cmap)
|
||||
{
|
||||
@ -1356,6 +1404,12 @@ read_cmap(ttf_t *font) // I - Font
|
||||
|
||||
TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups);
|
||||
|
||||
if (nGroups > TTF_FONT_MAX_GROUPS)
|
||||
{
|
||||
errorf(font, "Invalid cmap table with %u groups.", nGroups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
if ((groups = (_ttf_off_cmap12_t *)calloc(nGroups, sizeof(_ttf_off_cmap12_t))) == NULL)
|
||||
{
|
||||
errorf(font, "Unable to allocate memory for cmap.");
|
||||
@ -1369,6 +1423,13 @@ read_cmap(ttf_t *font) // I - Font
|
||||
group->startGlyphID = read_ulong(font);
|
||||
TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, startGlyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->startGlyphID);
|
||||
|
||||
if (group->startCharCode > group->endCharCode)
|
||||
{
|
||||
errorf(font, "Bad cmap table segment %u to %u.", group->startCharCode, group->endCharCode);
|
||||
free(groups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (group->endCharCode >= font->num_cmap)
|
||||
font->num_cmap = group->endCharCode + 1;
|
||||
}
|
||||
@ -1376,6 +1437,14 @@ read_cmap(ttf_t *font) // I - Font
|
||||
// Based on the end code of the segent table, allocate space for the
|
||||
// uncompressed cmap table...
|
||||
TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap);
|
||||
|
||||
if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
|
||||
{
|
||||
errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
|
||||
free(groups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
|
||||
|
||||
if (!font->cmap)
|
||||
@ -1426,6 +1495,12 @@ read_cmap(ttf_t *font) // I - Font
|
||||
|
||||
TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups);
|
||||
|
||||
if (nGroups > TTF_FONT_MAX_GROUPS)
|
||||
{
|
||||
errorf(font, "Invalid cmap table with %u groups.", nGroups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
if ((groups = (_ttf_off_cmap13_t *)calloc(nGroups, sizeof(_ttf_off_cmap13_t))) == NULL)
|
||||
{
|
||||
errorf(font, "Unable to allocate memory for cmap.");
|
||||
@ -1439,6 +1514,13 @@ read_cmap(ttf_t *font) // I - Font
|
||||
group->glyphID = read_ulong(font);
|
||||
TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, glyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->glyphID);
|
||||
|
||||
if (group->startCharCode > group->endCharCode)
|
||||
{
|
||||
errorf(font, "Bad cmap table segment %u to %u.", group->startCharCode, group->endCharCode);
|
||||
free(groups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (group->endCharCode >= font->num_cmap)
|
||||
font->num_cmap = group->endCharCode + 1;
|
||||
}
|
||||
@ -1446,6 +1528,14 @@ read_cmap(ttf_t *font) // I - Font
|
||||
// Based on the end code of the segent table, allocate space for the
|
||||
// uncompressed cmap table...
|
||||
TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap);
|
||||
|
||||
if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
|
||||
{
|
||||
errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
|
||||
free(groups);
|
||||
return (false);
|
||||
}
|
||||
|
||||
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
|
||||
|
||||
if (!font->cmap)
|
||||
@ -1565,7 +1655,7 @@ read_hmtx(ttf_t *font, // I - Font
|
||||
_ttf_off_hhea_t *hhea) // O - hhea table data
|
||||
{
|
||||
unsigned length; // Length of hmtx table
|
||||
int i; // Looping var
|
||||
unsigned i; // Looping var
|
||||
_ttf_metric_t *widths; // Glyph metrics array
|
||||
|
||||
|
||||
@ -1644,8 +1734,15 @@ read_names(ttf_t *font) // I - Font
|
||||
return (false);
|
||||
|
||||
font->names.storage_size = length - (unsigned)offset;
|
||||
if (font->names.storage_size > TTF_FONT_MAX_NAMES)
|
||||
{
|
||||
errorf(font, "Name table too large - %u bytes.", (unsigned)font->names.storage_size);
|
||||
return (false);
|
||||
}
|
||||
|
||||
if ((font->names.storage = malloc(font->names.storage_size)) == NULL)
|
||||
return (false);
|
||||
|
||||
memset(font->names.storage, 'A', font->names.storage_size);
|
||||
|
||||
for (i = font->names.num_names, name = font->names.names; i > 0; i --, name ++)
|
||||
|
17
ttf.h
17
ttf.h
@ -3,7 +3,7 @@
|
||||
//
|
||||
// https://github.com/michaelrsweet/ttf
|
||||
//
|
||||
// Copyright © 2018-2023 by Michael R Sweet.
|
||||
// Copyright © 2018-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@ -11,6 +11,7 @@
|
||||
|
||||
#ifndef TTF_H
|
||||
# define TTF_H
|
||||
# include <stddef.h>
|
||||
# include <stdbool.h>
|
||||
# include <sys/types.h>
|
||||
# ifdef __cplusplus
|
||||
@ -22,12 +23,12 @@ extern "C" {
|
||||
// Types...
|
||||
//
|
||||
|
||||
typedef struct _ttf_s ttf_t; //// Font object
|
||||
typedef struct _ttf_s ttf_t; // Font object
|
||||
|
||||
typedef void (*ttf_err_cb_t)(void *data, const char *message);
|
||||
//// Font error callback
|
||||
// Font error callback
|
||||
|
||||
typedef enum ttf_stretch_e //// Font stretch
|
||||
typedef enum ttf_stretch_e // Font stretch
|
||||
{
|
||||
TTF_STRETCH_NORMAL, // normal
|
||||
TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed
|
||||
@ -40,20 +41,20 @@ typedef enum ttf_stretch_e //// Font stretch
|
||||
TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded
|
||||
} ttf_stretch_t;
|
||||
|
||||
typedef enum ttf_style_e //// Font style
|
||||
typedef enum ttf_style_e // Font style
|
||||
{
|
||||
TTF_STYLE_NORMAL, // Normal font
|
||||
TTF_STYLE_ITALIC, // Italic font
|
||||
TTF_STYLE_OBLIQUE // Oblique (angled) font
|
||||
} ttf_style_t;
|
||||
|
||||
typedef enum ttf_variant_e //// Font variant
|
||||
typedef enum ttf_variant_e // Font variant
|
||||
{
|
||||
TTF_VARIANT_NORMAL, // Normal font
|
||||
TTF_VARIANT_SMALL_CAPS // Font whose lowercase letters are small capitals
|
||||
} ttf_variant_t;
|
||||
|
||||
typedef enum ttf_weight_e //// Font weight
|
||||
typedef enum ttf_weight_e // Font weight
|
||||
{
|
||||
TTF_WEIGHT_100 = 100, // Weight 100 (Thin)
|
||||
TTF_WEIGHT_200 = 200, // Weight 200 (Extra/Ultra-Light)
|
||||
@ -66,7 +67,7 @@ typedef enum ttf_weight_e //// Font weight
|
||||
TTF_WEIGHT_900 = 900 // Weight 900 (Black/Heavy)
|
||||
} ttf_weight_t;
|
||||
|
||||
typedef struct ttf_rect_s //// Bounding rectangle
|
||||
typedef struct ttf_rect_s // Bounding rectangle
|
||||
{
|
||||
float left; // Left offset
|
||||
float top; // Top offset
|
||||
|
Reference in New Issue
Block a user