95 Commits

Author SHA1 Message Date
6906a9a708 Fix docos for pdfioFileOpen. 2023-12-05 19:22:47 -05:00
6a381a55fe Update macOS build docos. 2023-12-05 18:41:26 -05:00
fc3580a948 Update build docos. 2023-12-05 18:39:20 -05:00
6b5c30b4be Remove PDFIO_ENCRYPTION_AES_256 from docos. 2023-12-05 14:30:41 -05:00
a0cdb261ff Update CONTRIBUTING docos. 2023-12-05 14:07:52 -05:00
34dbf6c2fe Documentation cleanup. 2023-12-05 13:49:58 -05:00
86d842167a Bring back mis-named pdfioContentTextNextLine. 2023-12-05 13:33:07 -05:00
16c8b830b8 Add pdfioFileCreateNumber/StringObj functions (Issue #14) 2023-12-05 08:16:41 -05:00
7ff051fc8b Add pdfioContentTextNewLineShow/f functions (Issue #24) 2023-12-05 07:49:49 -05:00
927452d1eb Changelog and exports updates. 2023-12-04 21:22:13 -05:00
f1ad982fd1 Update docos. 2023-12-04 21:21:18 -05:00
c188cb8dad Finish implementation of pdfioContentTextMeasure (Issue #17) 2023-12-04 21:20:51 -05:00
4919783da5 Save work on string measure support (Issue #17) 2023-12-04 18:54:33 -05:00
86281750e5 Fix man page installation to use mandir (Issue #55) 2023-12-04 08:50:28 -05:00
d92b72ed02 Add math library. 2023-12-03 20:26:27 -05:00
a195c023af Fix CI scripts. 2023-12-03 20:20:23 -05:00
43000ff01f Fix up targets. 2023-12-03 19:56:25 -05:00
c6f17cc20f Fix some warnings. 2023-12-03 19:23:36 -05:00
41146adbdf Adopt autoconf (Issue #54) 2023-12-03 19:16:34 -05:00
cd80c3037d Prep for 1.1.4 release. 2023-12-03 16:55:57 -05:00
97934ab995 Add ToUnicode map. 2023-11-19 20:49:30 -05:00
088646e1cf Drop the FEFF prefix on Unicode strings. 2023-11-19 07:30:17 -05:00
3f0aad7564 Fix another bug in the CMAPv4 code, and a bug in the unit test program. 2023-11-19 07:06:35 -05:00
d36df63b57 Fix a TrueType CMAP decoding bug and add a NotoSans-Regular test page. 2023-11-18 22:15:52 -05:00
a5dfac7495 Bump version. 2023-11-18 18:23:22 -05:00
0258384d53 Range check encrypted string length (Issue #52) 2023-11-18 18:22:11 -05:00
9fec2195d0 Update changelog. 2023-11-15 10:14:02 -05:00
8ccbdaed94 Update documentation. 2023-11-15 09:28:57 -05:00
4804db38a5 Fix missing ivlen initializer for 40-bit RC4 (Issue #51) 2023-11-15 08:43:07 -05:00
ddd984215a Save work (debug printfs, etc.) 2023-11-15 08:38:47 -05:00
efe7c01015 Fix typo and 'make debug'. 2023-11-14 18:38:26 -05:00
600fa4ce59 Fix Unicode font handling (Issue #16) 2023-11-14 18:19:34 -05:00
688810f143 Save work. 2023-11-13 16:18:02 -05:00
858cc101b6 Save work. 2023-11-13 13:39:06 -05:00
43114f43bf Bump version. 2023-10-10 10:22:27 -04:00
c4abceef79 Make Visual Studio compiler happy. 2023-10-10 07:24:27 -04:00
2e91e05d7b Allow "compound" filters that consist of a single named filter (Issue #47) 2023-10-10 07:14:12 -04:00
7e3db6b639 Merge pull request #48 from crystalidea/master
added windows.h header for GetTempPathA
2023-10-10 07:07:46 -04:00
acb6b66bdb added windows.h header for GetTempPathA 2023-10-10 09:12:03 +02:00
b0a66eef78 Fix reading of PDF files from Crystal Reports (Issue #45) 2023-10-09 10:04:20 -04:00
ed88322496 Debug logging, work in progress for Unicode text support. 2023-10-07 12:05:18 -04:00
59959bf0e5 Merge TTF changes to fix off-by-one error. 2023-10-06 16:44:20 -04:00
19c45871fa Update pdfioContentSetDashPattern to support setting solid line styles (Issue #41) 2023-10-06 15:47:27 -04:00
b0e4646f9d Rework CR/LF skip code to be more consistent. 2023-10-06 14:41:55 -04:00
12ef2fe2c3 Remove LGTM badges. 2023-10-06 14:40:40 -04:00
4630060ee7 Update security reporting and contribution text. 2023-10-06 14:40:28 -04:00
74a6fb1860 Get rid of superfluous comments. 2023-10-06 14:40:08 -04:00
a3ea0a99ff Cleanup spacing and comments. 2023-10-06 14:39:42 -04:00
fdfa700442 Update ignored files. 2023-10-06 14:39:10 -04:00
d759baf11e Bump version and put PDFIO_VERSION definition in the pdfio.h header. 2023-10-06 14:38:38 -04:00
7f6ffcda22 Fix a couple issues with parsing PDF files produced by Microsoft Reporting
Services (Issue #46)

- Odd cross-reference stream containing 3-byte generation number field for this
  16-bit value
- Odd empty hex strings
2023-10-06 10:46:30 -04:00
87ca4db73f Clean up private header. 2023-10-02 05:27:40 -04:00
a83f7f50ff Allow extra whitespace/newlines after stream tokens (Issue #40) 2023-10-02 05:06:33 -04:00
6a4ce57d09 Bump versions for Mac/Windows project files. 2023-03-20 10:40:25 -04:00
d4c594cec4 Bump copyright in readme. 2023-03-20 10:22:19 -04:00
35c674b633 Fix another build issue. 2023-03-20 10:11:05 -04:00
97d4955666 Fix potential denial-of-service in flate stream code. 2023-03-20 09:27:19 -04:00
e138232a93 Fix build error due to mismatched function declarations. 2023-03-20 08:19:31 -04:00
8d8225f4a1 Fix release date. 2023-02-07 17:10:48 -05:00
7045d9dad9 Bump Windows version numbers and update exports file. 2023-02-06 17:36:54 -05:00
4f10021e7e Fix denial-of-service attack when reading corrupt PDF files. 2023-02-03 20:39:04 -05:00
57d5894f33 Update pdfioStreamGetToken documentation (Issue #37) 2023-01-11 17:13:58 -05:00
2b8a1c8481 Fix CodeQL config file syntax. 2022-12-09 11:31:56 -05:00
948ee16b06 Fix the one "value" complaint from CodeQL and suppress all useless queries. 2022-12-09 11:22:10 -05:00
c7101ae9dd Add CodeQL scanning. 2022-12-09 11:09:34 -05:00
599640eda1 Update makefile to be silent with basic progress reporting. 2022-08-02 09:41:13 -04:00
a3f3bbfe11 Fix pdfioFileGetAuthor, etc. APIs (Issue #33) 2022-07-12 18:36:08 -04:00
26d485cfc5 Update Windows DLL exports file. 2022-07-06 15:25:45 -04:00
64d306a322 Cleanup. 2022-07-06 08:47:52 -04:00
067683cbcd Add some protection against opening multiple streams in the same file at the same time. 2022-07-04 13:03:11 -04:00
50f27974cf Update documentation. 2022-07-03 10:01:20 -04:00
ae9a91719b Add pdfioContentPathEnd function. 2022-07-03 10:01:10 -04:00
1a17933635 Fix pdfioContentMatrixRotate function. 2022-07-01 20:30:40 -04:00
acea6fdbed Changelog. 2022-06-27 17:17:44 -04:00
66fa12f928 Update Windows DLL exports file. 2022-06-27 10:17:21 -04:00
f4b8983c61 Implement pdfioDictIterateKeys API (Issue #31) 2022-06-27 10:17:00 -04:00
ed4e2fc38a Merge pull request #32 from ire4ever1190/patch-1
Fix `install-shared` Make task
2022-06-09 09:46:23 -04:00
1ed7f0089c Update Makefile 2022-06-09 14:33:53 +10:00
563d53edd4 Update Windows DLL exports file. 2022-05-24 19:16:20 -04:00
316b0ad559 Add pdfioFileCreateTemporary function (Issue #29) 2022-05-15 22:52:53 -04:00
f8b471acfd Update README and NOTICE files... 2022-03-02 09:50:14 -05:00
cedd7d104f Changelog update. 2022-03-02 09:47:14 -05:00
6378047026 Update VC project. 2022-03-02 09:31:33 -05:00
54578144a0 Update documentation and prep for 1.0.1 release. 2022-03-02 09:30:01 -05:00
f7f2969e3a Fix pdfioStreamGetToken implementation (wasn't flushing input), update
pdfiototext code to better handle different text operators that affect the
location of the text.
2022-03-01 09:18:56 -05:00
93a3fcea6c Add missing pdfioPageGetNumStreams and pdfioPageOpenStream functions.
Add initial version of pdfiototext text extraction utility.
2022-02-28 15:00:25 -05:00
fa20982e5d Coverity certs are fixed. 2021-12-15 18:20:54 -05:00
44d20eba1b Add stub code for AES-256 to force Coverity to re-analyze... 2021-12-15 07:35:55 -05:00
c0b7925cdf Fix typo. 2021-12-15 07:28:17 -05:00
68dcf021b2 Download Entrust root cert for validation. 2021-12-15 07:25:44 -05:00
b0a8e60968 Also allow posts to coverity.com while we wait for Ubuntu to pick up the new Entrust root certificate. 2021-12-15 07:10:13 -05:00
9d47745e43 Prep for 1.0rc1. 2021-12-15 06:53:09 -05:00
b0bf2e04b9 Coverity's certificate has expired. 2021-12-14 16:26:57 -05:00
f030112372 See what is happening when downloading Coverity build tool (drop quiet option). 2021-12-14 16:21:49 -05:00
79c4b6f8a8 See what is happening when downloading Coverity build tool. 2021-12-14 16:20:34 -05:00
49 changed files with 12618 additions and 932 deletions

22
.github/codeql.yml vendored Normal file
View File

@ -0,0 +1,22 @@
paths-ignore:
- testpdfio.c
query-filters:
- exclude:
id: cpp/commented-out-code
- exclude:
id: cpp/toctou-race-condition
- exclude:
id: cpp/weak-cryptographic-algorithm
- exclude:
id: cpp/world-writable-file-creation
- exclude:
id: cpp/uncontrolled-allocation-size
- exclude:
id: cpp/path-injection
- exclude:
id: cpp/stack-address-escape
- exclude:
id: cpp/loop-variable-changed
- exclude:
id: cpp/long-switch

View File

@ -9,23 +9,26 @@ on:
jobs: jobs:
build-linux: build-linux:
runs-on: ubuntu-20.04 runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - name: Checkout PDFio sources
- name: update build environment uses: actions/checkout@v4
- name: Update Build Environment
run: sudo apt-get update --fix-missing -y run: sudo apt-get update --fix-missing -y
- name: install prerequisites - name: Install Prerequisites
run: sudo apt-get install -y cppcheck zlib1g-dev run: sudo apt-get install -y cppcheck zlib1g-dev
- name: make - name: Configure PDFio
run: ./configure --enable-debug --enable-sanitizer --enable-maintainer
- name: Build PDFio
run: make "COMMONFLAGS=-g -fsanitize=address" run: make "COMMONFLAGS=-g -fsanitize=address"
- name: test - name: Test PDFio
env: env:
ASAN_OPTIONS: leak_check_at_exit=false ASAN_OPTIONS: leak_check_at_exit=false
run: make test run: make test
- name: clang static analyzer - name: Run Clang Static Analyzer
run: make CC=clang "GHA_ERROR=::error::" clang run: make CC=clang "GHA_ERROR=::error::" clang
- name: cppcheck - name: Run cppcheck
run: make "GHA_ERROR=::error::" cppcheck run: make "GHA_ERROR=::error::" cppcheck
build-macos: build-macos:
@ -33,12 +36,15 @@ jobs:
runs-on: macos-latest runs-on: macos-latest
steps: steps:
- uses: actions/checkout@v2 - name: Checkout PDFio sources
- name: make uses: actions/checkout@v4
- name: Configure PDFio
run: ./configure --enable-debug --enable-sanitizer --enable-maintainer
- name: Build PDFio
run: make "COMMONFLAGS=-g -fsanitize=address" run: make "COMMONFLAGS=-g -fsanitize=address"
- name: test - name: Test PDFio
run: make test run: make test
- name: clang static analyzer - name: Run Clang Static Analyzer
run: make CC=clang "GHA_ERROR=::error::" clang run: make CC=clang "GHA_ERROR=::error::" clang
build-windows: build-windows:
@ -46,10 +52,11 @@ jobs:
runs-on: windows-latest runs-on: windows-latest
steps: steps:
- uses: actions/checkout@v2 - name: Checkout PDFio sources
- name: setup-msbuild uses: actions/checkout@v4
- name: Setup MSBuild
uses: microsoft/setup-msbuild@v1.0.2 uses: microsoft/setup-msbuild@v1.0.2
- name: nuget restore - name: Nuget Restore
run: nuget restore pdfio.sln run: nuget restore pdfio.sln
- name: msbuild - name: Build PDFio
run: msbuild pdfio.sln run: msbuild pdfio.sln

50
.github/workflows/codeql.yml vendored Normal file
View File

@ -0,0 +1,50 @@
name: "CodeQL"
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
schedule:
- cron: "46 3 * * 0"
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ cpp ]
steps:
- name: Checkout PDFio sources
uses: actions/checkout@v4
with:
submodules: recursive
- name: Update Build Environment
run: sudo apt-get update --fix-missing -y
- name: Install Prerequisites
run: sudo apt-get install -y zlib1g-dev
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
config-file: ./.github/codeql.yml
queries: +security-and-quality
- name: Autobuild
uses: github/codeql-action/autobuild@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"

View File

@ -7,10 +7,11 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
environment: Coverity environment: Coverity
steps: steps:
- uses: actions/checkout@v2 - name: Checkout PDFio sources
- name: update build environment uses: actions/checkout@v4
- name: Update Build Environment
run: sudo apt-get update --fix-missing -y run: sudo apt-get update --fix-missing -y
- name: install prerequisites - name: Install Prerequisites
run: sudo apt-get install -y zlib1g-dev run: sudo apt-get install -y zlib1g-dev
- name: Download Coverity Build Tool - name: Download Coverity Build Tool
run: | run: |
@ -20,11 +21,15 @@ jobs:
env: env:
TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }} TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }}
- name: Build with cov-build - name: Configure PDFio
run: ./configure --enable-debug --enable-maintainer
- name: Build PDFio with cov-build
run: | run: |
export PATH=`pwd`/cov-analysis-linux64/bin:$PATH export PATH=`pwd`/cov-analysis-linux64/bin:$PATH
cov-build --dir cov-int make cov-build --dir cov-int make
- name: Submit the result to Coverity Scan
- name: Submit the Result to Coverity Scan
run: | run: |
tar czvf cov.tgz cov-int tar czvf cov.tgz cov-int
curl \ curl \

9
.gitignore vendored
View File

@ -5,9 +5,18 @@
*.so.1 *.so.1
/.vs /.vs
/afl-output /afl-output
/autom4te.cache
/config.log
/config.status
/doc/pdfio.epub /doc/pdfio.epub
/Makefile
/packages /packages
/pdfio.pc
/pdfio.xcodeproj/xcshareddata /pdfio.xcodeproj/xcshareddata
/pdfio-*.tar.gz*
/pdfio-*.zip*
/pdfiototext
/testpdfio /testpdfio
/testpdfio-*.pdf /testpdfio-*.pdf
/testttf
/x64 /x64

View File

@ -1,2 +0,0 @@
queries:
- exclude: cpp/toctou-race-condition

View File

@ -2,6 +2,80 @@ Changes in PDFio
================ ================
v1.2.0 (Month DD, YYYY)
-----------------------
- Added `pdfioFileCreateNumberObj` and `pdfioFileCreateStringObj` functions
(Issue #14)
- Added `pdfioContentTextMeasure` function (Issue #17)
- Added `pdfioContentTextNewLineShow` and `pdfioContentTextNewLineShowf`
functions (Issue #24)
- Renamed `pdfioContentTextNextLine` to `pdfioContentTextNewLine`.
- Now use autoconf to configure the PDFio sources (Issue #54)
v1.1.4 (December 3, 2023)
-------------------------
- Fixed detection of encrypted strings that are too short (Issue #52)
- Fixed a TrueType CMAP decoding bug.
- Fixed a text rendering issue for Asian text.
- Added a ToUnicode map for Unicode text to support text copying.
v1.1.3 (November 15, 2023)
--------------------------
- Fixed Unicode font support (Issue #16)
- Fixed missing initializer for 40-bit RC4 encryption (Issue #51)
v1.1.2 (October 10, 2023)
-------------------------
- Updated `pdfioContentSetDashPattern` to support setting a solid (0 length)
dash pattern (Issue #41)
- Fixed an issue with broken PDF files containing extra CR and/or LF separators
after the object stream token (Issue #40)
- Fixed an issue with PDF files produced by Crystal Reports (Issue #45)
- Fixed an issue with PDF files produced by Microsoft Reporting Services
(Issue #46)
- Fixed support for compound filters where the filter array consists of a
single named filter (Issue #47)
- Fixed builds on Windows - needed windows.h header for temporary files
(Issue #48)
v1.1.1 (March 20, 2023)
-----------------------
- CVE-2023-28428: Fixed a potential denial-of-service with corrupt PDF files.
- Fixed a few build issues.
v1.1.0 (February 6, 2023)
-------------------------
- CVE-2023-24808: Fixed a potential denial-of-service with corrupt PDF files.
- Added `pdfioFileCreateTemporary` function (Issue #29)
- Added `pdfioDictIterateKeys` function (Issue #31)
- Added `pdfioContentPathEnd` function.
- Added protection against opening multiple streams in the same file at the
same time.
- Documentation updates (Issue #37)
- Fixed "install-shared" target (Issue #32)
- Fixed `pdfioFileGet...` metadata APIs (Issue #33)
- Fixed `pdfioContentMatrixRotate` function.
v1.0.1 (March 2, 2022)
----------------------
- Added missing `pdfioPageGetNumStreams` and `pdfioPageOpenStream` functions.
- Added demo pdfiototext utility.
- Fixed bug in `pdfioStreamGetToken`.
v1.0.0 (December 14, 2021) v1.0.0 (December 14, 2021)
-------------------------- --------------------------

View File

@ -43,14 +43,17 @@ Contents
Build System Build System
------------ ------------
The build system uses a simple POSIX makefile to build a static or shared The build system uses [GNU autoconf][AUTOCONF] to create a simple POSIX makefile
library. To improve portability, makefiles *must not* make use of features to build static and/or shared libraries. To improve portability, makefiles
unique to GNU make. See the [Makefile Guidelines](#makefile-guidelines) section *must not* make use of features unique to GNU make. See the
for a description of the allowed make features and makefile guidelines. [Makefile Guidelines](#makefile-guidelines) section for a description of the
allowed make features and makefile guidelines.
An Xcode project is provided for macOS/iOS developers, and a Visual Studio An Xcode project is provided for macOS/iOS developers, and a Visual Studio
solution and projects for Windows developers. solution and projects for Windows developers.
[AUTOCONF]: https://www.gnu.org/software/autoconf/
Version Numbering Version Numbering
----------------- -----------------
@ -118,7 +121,7 @@ the source file and the copyright and licensing notice:
// //
// Description of file contents. // Description of file contents.
// //
// Copyright YYYY by AUTHOR. // Copyright © YYYY by AUTHOR.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -330,7 +333,7 @@ typedef, for example:
All constant names are uppercase with underscores between words, e.g., All constant names are uppercase with underscores between words, e.g.,
`PDFIO_THIS_CONSTANT`, `PDFIO_THAT_CONSTANT`, etc. Constants begin with the `PDFIO_THIS_CONSTANT`, `PDFIO_THAT_CONSTANT`, etc. Constants begin with the
"PDFio\_" prefix to avoid conflicts with system constants. Private constants "PDFIO\_" prefix to avoid conflicts with system constants. Private constants
start with an underscore, e.g., `_PDFIO_THIS_CONSTANT`, start with an underscore, e.g., `_PDFIO_THIS_CONSTANT`,
`_PDFIO_THAT_CONSTANT`, etc. `_PDFIO_THAT_CONSTANT`, etc.
@ -369,17 +372,19 @@ extensions MUST NOT be used.
The following variables are defined in the makefile: The following variables are defined in the makefile:
- `AR`; the static library archiver command, - `AR`; the static library archiver command,
- `ARFLAGS`; options for the static library archiver command, - `ARFLAGS`; options for the static library archiver,
- `CC`; the C compiler command, - `CC`; the C compiler command,
- `CFLAGS`; options for the C compiler command, - `CFLAGS`; options for the C compiler,
- `CODESIGN_IDENTITY`: the code signing identity, - `CODESIGN_IDENTITY`: the code signing identity,
- `COMMONFLAGS`; common compiler optimization options, - `CPPFLAGS`; options for the C preprocessor,
- `DESTDIR`/`DSTROOT`: the destination root directory when installing. - `DESTDIR`/`DSTROOT`: the destination root directory when installing.
- `DSO`; the shared library building command, - `DSO`; the shared library building command,
- `DSOFLAGS`; options for the shared library building command, - `DSOFLAGS`; options for the shared library building command,
- `DSONAME`: the root name of the shared library
- `LDFLAGS`; options for the linker, - `LDFLAGS`; options for the linker,
- `LIBPDFIO`: the name of the primary (shared or static) library
- `LIBPDFIO_STATIC`: the name of the secondary (static) library
- `LIBS`; libraries for all programs, - `LIBS`; libraries for all programs,
- `OPTIM`; common compiler optimization options,
- `prefix`; the installation prefix directory, - `prefix`; the installation prefix directory,
- `RANLIB`; the static library indexing command, - `RANLIB`; the static library indexing command,
- `SHELL`; the sh (POSIX shell) command, - `SHELL`; the sh (POSIX shell) command,
@ -388,11 +393,7 @@ The following variables are defined in the makefile:
The following standard targets are defined in the makefile: The following standard targets are defined in the makefile:
- `all`; creates the static library and unit test program. - `all`; creates the static library and unit test program.
- `all-shared`; creates a shared library appropriate for the local system.
- `clean`; removes all target programs libraries, documentation files, and - `clean`; removes all target programs libraries, documentation files, and
object files, object files,
- `debug`: creates a clean build of the static library and unit test program
with debug printfs and the clang address sanitizer enabled.
- `install`; installs all distribution files in their corresponding locations. - `install`; installs all distribution files in their corresponding locations.
- `install-shared`; same as `install` but also installs the shared library.
- `test`; runs the unit test program, building it as needed. - `test`; runs the unit test program, building it as needed.

199
Makefile
View File

@ -1,199 +0,0 @@
#
# Makefile for PDFio.
#
# Copyright © 2021 by Michael R Sweet.
#
# Licensed under Apache License v2.0. See the file "LICENSE" for more
# information.
#
# POSIX makefile
.POSIX:
# Variables...
AR = ar
ARFLAGS = cr
CC = cc
CFLAGS =
CODESIGN_IDENTITY = Developer ID
COMMONFLAGS = -Os -g
#COMMONFLAGS = -O0 -g -fsanitize=address
CPPFLAGS = '-DPDFIO_VERSION="$(VERSION)"'
DESTDIR = $(DSTROOT)
DSO = cc
DSOFLAGS =
DSONAME =
LDFLAGS =
LIBS = -lm -lz
RANLIB = ranlib
VERSION = 1.0.0
prefix = /usr/local
# Base rules
.SUFFIXES: .c .h .o
.c.o:
$(CC) $(CFLAGS) $(CPPFLAGS) $(COMMONFLAGS) -c $<
# Files
PUBHEADERS = \
pdfio.h \
pdfio-content.h
PUBOBJS = \
pdfio-aes.o \
pdfio-array.o \
pdfio-common.o \
pdfio-content.o \
pdfio-crypto.o \
pdfio-dict.o \
pdfio-file.o \
pdfio-md5.o \
pdfio-object.o \
pdfio-page.o \
pdfio-rc4.o \
pdfio-sha256.o \
pdfio-stream.o \
pdfio-string.o \
pdfio-token.o \
pdfio-value.o
LIBOBJS = \
$(PUBOBJS) \
ttf.o
OBJS = \
$(LIBOBJS) \
testpdfio.o
TARGETS = \
$(DSONAME) \
libpdfio.a \
testpdfio
# Make everything
all: $(TARGETS)
all-shared:
if test `uname` = Darwin; then \
$(MAKE) DSONAME="libpdfio.1.dylib" -$(MAKEFLAGS) all; \
else \
$(MAKE) COMMONFLAGS="-g -Os -fPIC" DSONAME="libpdfio.so.1" -$(MAKEFLAGS) all; \
fi
debug:
$(MAKE) -$(MAKEFLAGS) COMMONFLAGS="-g -fsanitize=address -DDEBUG=1" clean all
# Clean everything
clean:
rm -f $(TARGETS) $(OBJS)
# Install everything
install: $(TARGETS)
-mkdir -p $(DESTDIR)$(prefix)/include
cp $(PUBHEADERS) $(DESTDIR)$(prefix)/include
-mkdir -p $(DESTDIR)$(prefix)/lib
cp libpdfio.a $(DESTDIR)$(prefix)/lib
$(RANLIB) $(DESTDIR)$(prefix)/lib/libpdfio.a
if test "x$(DSONAME)" = xlibpdfio.so.1; then \
cp $(DSONAME) $(DESTDIR)$(prefix)/lib; \
ln -sf libpdfio.so.1 $(DESTDIR)$(prefix)/lib/libpdfio.so; \
elif test "x$(DSONAME)" = xlibpdfio.1.dylib; then \
cp $(DSONAME) $(DESTDIR)$(prefix)/lib; \
codesign -s "$(CODESIGN_IDENTITY)" -o runtime --timestamp $(DESTDIR)$(prefix)/lib/libpdfio.1.dylib; \
ln -sf libpdfio.1.dylib $(DESTDIR)$(prefix)/lib/libpdfio.dylib; \
fi
-mkdir -p $(DESTDIR)$(prefix)/lib/pkgconfig
echo 'prefix="$(prefix)"' >$(DESTDIR)$(prefix)/lib/pkgconfig/pdfio.pc
echo 'Version: $(VERSION)' >>$(DESTDIR)$(prefix)/lib/pkgconfig/pdfio.pc
cat pdfio.pc.in >>$(DESTDIR)$(prefix)/lib/pkgconfig/pdfio.pc
-mkdir -p $(DESTDIR)$(prefix)/share/doc/pdfio
cp doc/pdfio.html doc/pdfio-512.png LICENSE NOTICE $(DESTDIR)$(prefix)/share/doc/pdfio
-mkdir -p $(DESTDIR)$(prefix)/share/man/man3
cp doc/pdfio.3 $(DESTDIR)$(prefix)/share/man/man3
install-shared:
if test `uname` = Darwin; then \
$(MAKE) DSONAME="libpdfio.1.dylib" -$(MAKEFLAGS) install; \
else
$(MAKE) DSONAME="libpdfio.so.1" -$(MAKEFLAGS) install; \
fi
# Test everything
test: testpdfio
./testpdfio
valgrind: testpdfio
valgrind --leak-check=full ./testpdfio
# pdfio library
libpdfio.a: $(LIBOBJS)
$(AR) $(ARFLAGS) $@ $(LIBOBJS)
$(RANLIB) $@
libpdfio.so.1: $(LIBOBJS)
$(CC) $(DSOFLAGS) $(COMMONFLAGS) -shared -o $@ -Wl,-soname,$@ $(LIBOBJS) $(LIBS)
libpdfio.1.dylib: $(LIBOBJS)
$(CC) $(DSOFLAGS) $(COMMONFLAGS) -dynamiclib -o $@ -install_name $(prefix)/lib/$@ -current_version $(VERSION) -compatibility_version 1.0 $(LIBOBJS) $(LIBS)
# pdfio1.def (Windows DLL exports file...)
#
# I'd love to use __declspec(dllexport) but MS puts it before the function
# declaration instead of after like everyone else, and it breaks Codedoc and
# other tools I rely on...
pdfio1.def: $(LIBOBJS) Makefile
echo Generating $@...
echo "LIBRARY pdfio1" >$@
echo "VERSION 1.0" >>$@
echo "EXPORTS" >>$@
nm $(LIBOBJS) 2>/dev/null | grep "T _" | awk '{print $$3}' | \
grep -v '^_ttf' | sed -e '1,$$s/^_//' | sort >>$@
# pdfio test program
testpdfio: testpdfio.o libpdfio.a
$(CC) $(LDFLAGS) $(COMMONFLAGS) -o $@ testpdfio.o libpdfio.a $(LIBS)
# Dependencies
$(OBJS): pdfio.h pdfio-private.h Makefile
pdfio-content.o: pdfio-content.h ttf.h
ttf.o: ttf.h
# Make documentation using Codedoc <https://www.msweet.org/codedoc>
DOCFLAGS = \
--author "Michael R Sweet" \
--copyright "Copyright (c) 2021 by Michael R Sweet" \
--docversion $(VERSION)
.PHONY: doc
doc:
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(VERSION)" $(PUBHEADERS) $(PUBOBJS:.o=.c) --body doc/pdfio.md --coverimage doc/pdfio-512.png pdfio.xml >doc/pdfio.html
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(VERSION)" --body doc/pdfio.md --coverimage doc/pdfio-epub.png pdfio.xml --epub doc/pdfio.epub
codedoc $(DOCFLAGS) --title "pdf read/write library" --man pdfio --section 3 --body doc/pdfio.md pdfio.xml >doc/pdfio.3
rm -f pdfio.xml
# Fuzz-test the library <>
.PHONY: afl
afl:
$(MAKE) -$(MAKEFLAGS) CC="afl-clang-fast" COMMONFLAGS="-g" clean all
test afl-output || rm -rf afl-output
afl-fuzz -x afl-pdf.dict -i afl-input -o afl-output -V 600 -e pdf -t 5000 ./testpdfio @@
# Analyze code with the Clang static analyzer <https://clang-analyzer.llvm.org>
clang:
clang $(CPPFLAGS) --analyze $(OBJS:.o=.c) 2>clang.log
rm -rf $(OBJS:.o=.plist)
test -s clang.log && (echo "$(GHA_ERROR)Clang detected issues."; echo ""; cat clang.log; exit 1) || exit 0
# 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
test -s cppcheck.log && (echo "$(GHA_ERROR)Cppcheck detected issues."; echo ""; cat cppcheck.log; exit 1) || exit 0

259
Makefile.in Normal file
View File

@ -0,0 +1,259 @@
#
# Makefile for PDFio.
#
# Copyright © 2021-2023 by Michael R Sweet.
#
# Licensed under Apache License v2.0. See the file "LICENSE" for more
# information.
#
# POSIX makefile
.POSIX:
# Build silently
.SILENT:
# Version number...
PDFIO_VERSION = @PDFIO_VERSION@
PDFIO_VERSION_MAJOR = @PDFIO_VERSION_MAJOR@
PDFIO_VERSION_MINOR = @PDFIO_VERSION_MINOR@
# Programs and options...
AR = @AR@
ARFLAGS = @ARFLAGS@
CC = @CC@
CFLAGS = @CFLAGS@ $(CPPFLAGS) $(OPTIM) $(WARNINGS)
CODE_SIGN = @CODE_SIGN@
CODESIGN_IDENTITY = -
CPPFLAGS = @CPPFLAGS@
CSFLAGS = -s "$(CODESIGN_IDENTITY)" @CSFLAGS@ --timestamp
DSOFLAGS = @DSOFLAGS@ $(CFLAGS)
INSTALL = @INSTALL@
LDFLAGS = @LDFLAGS@ $(OPTIM)
LIBS = @LIBS@ -lm
LN = @LN@
OPTIM = @OPTIM@
RANLIB = @RANLIB@
RM = @RM@ -f
RMDIR = @RMDIR@
SHELL = /bin/sh
WARNINGS = @WARNINGS@
# Targets
LIBPDFIO = @LIBPDFIO@
LIBPDFIO_STATIC = @LIBPDFIO_STATIC@
# Directories...
bindir = @bindir@
datadir = @datadir@
datarootdir = @datarootdir@
exec_prefix = @exec_prefix@
includedir = @includedir@
infodir = @infodir@
libdir = @libdir@
libexecdir = @libexecdir@
localstatedir = @localstatedir@
mandir = @mandir@
oldincludedir = @oldincludedir@
prefix = @prefix@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
sysconfdir = @sysconfdir@
top_srcdir = @top_srcdir@
BUILDROOT = $(DSTROOT)$(RPM_BUILD_ROOT)$(DESTDIR)
# Build commands...
.SUFFIXES: .c .h .o
.c.o:
echo Compiling $<...
$(CC) $(CFLAGS) -c -o $@ $<
# Files
PUBHEADERS = \
pdfio.h \
pdfio-content.h
PUBOBJS = \
pdfio-aes.o \
pdfio-array.o \
pdfio-common.o \
pdfio-content.o \
pdfio-crypto.o \
pdfio-dict.o \
pdfio-file.o \
pdfio-md5.o \
pdfio-object.o \
pdfio-page.o \
pdfio-rc4.o \
pdfio-sha256.o \
pdfio-stream.o \
pdfio-string.o \
pdfio-token.o \
pdfio-value.o
LIBOBJS = \
$(PUBOBJS) \
ttf.o
OBJS = \
$(LIBOBJS) \
pdfiototext.o \
testpdfio.o \
testttf.o
TARGETS = \
$(LIBPDFIO) \
$(LIBPDFIO_STATIC) \
pdfiototext \
testpdfio \
testttf
# Make everything
all: $(TARGETS)
# Clean everything
clean:
rm -f $(TARGETS) $(OBJS)
# Install everything
install: $(TARGETS)
echo Installing header files to $(BUILDROOT)$(includedir)...
$(INSTALL) -d -m 755 $(BUILDROOT)$(includedir)
for file in $(PUBHEADERS); do \
$(INSTALL) -c -m 644 $$file $(BUILDROOT)$(includedir); \
done
echo Installing library files to $(BUILDROOT)$(libdir)...
$(INSTALL) -d -m 755 $(BUILDROOT)$(libdir)
if test "x$(LIBPDFIO_STATIC)" != x; then \
$(INSTALL) -c -m 644 $(LIBPDFIO_STATIC) $(BUILDROOT)$(libdir); \
$(RANLIB) $(BUILDROOT)$(libdir)/$(LIBPDFIO_STATIC); \
fi
if test "x$(LIBPDFIO)" = xlibpdfio.so.1; then \
$(INSTALL) -c -m 755 libpdfio.so.1 $(BUILDROOT)$(libdir); \
ln -sf libpdfio.so.1 $(BUILDROOT)$(libdir)/libpdfio.so; \
elif test "x$(LIBPDFIO)" = xlibpdfio.1.dylib; then \
$(INSTALL) -c -m 755 libpdfio.1.dylib $(BUILDROOT)$(libdir); \
codesign -s "$(CODESIGN_IDENTITY)" -o runtime --timestamp $(BUILDROOT)$(libdir)/libpdfio.1.dylib; \
ln -sf libpdfio.1.dylib $(BUILDROOT)$(libdir)/libpdfio.dylib; \
else \
$(INSTALL) -c -m 644 $(LIBPDFIO) $(BUILDROOT)$(libdir); \
$(RANLIB) $(BUILDROOT)$(libdir)/$(LIBPDFIO); \
fi
echo Installing pkg-config files to $(BUILDROOT)$(libdir)/pkgconfig...
$(INSTALL) -d -m 755 $(BUILDROOT)$(libdir)/pkgconfig
$(INSTALL) -c -m 644 pdfio.pc $(BUILDROOT)$(libdir)/pkgconfig
echo Installing documentation to $(BUILDROOT)$(datadir)/doc/pdfio...
$(INSTALL) -d -m 755 $(BUILDROOT)$(datadir)/doc/pdfio
for file in doc/pdfio.html doc/pdfio-512.png LICENSE NOTICE; do \
$(INSTALL) -c -m 644 $$file $(BUILDROOT)$(datadir)/doc/pdfio; \
done
echo Installing man page to $(BUILDROOT)$(mandir)/man3...
$(INSTALL) -d -m 755 $(BUILDROOT)$(mandir)/man3
$(INSTALL) -c -m 644 doc/pdfio.3 $(BUILDROOT)$(mandir)/man3
# Test everything
test: testpdfio testttf
./testttf 2>test.log
./testpdfio 2>test.log
valgrind: testpdfio
valgrind --leak-check=full ./testpdfio
# pdfio library
libpdfio.a: $(LIBOBJS)
echo Archiving $@...
$(AR) $(ARFLAGS) $@ $(LIBOBJS)
$(RANLIB) $@
libpdfio.so.1: $(LIBOBJS)
echo Linking $@...
$(CC) $(DSOFLAGS) -shared -o $@ -Wl,-soname,$@ $(LIBOBJS) $(LIBS)
libpdfio.1.dylib: $(LIBOBJS)
echo Linking $@...
$(CC) $(DSOFLAGS) -dynamiclib -o $@ -install_name $(libdir)/$@ -current_version $(PDFIO_VERSION_MAJOR).$(PDFIO_VERSION_MINOR) -compatibility_version 1.0 $(LIBOBJS) $(LIBS)
# pdfio1.def (Windows DLL exports file...)
#
# I'd love to use __declspec(dllexport) but MS puts it before the function
# declaration instead of after like everyone else, and it breaks Codedoc and
# other tools I rely on...
pdfio1.def: $(LIBOBJS) Makefile
echo Generating $@...
echo "LIBRARY pdfio1" >$@
echo "VERSION $(PDFIO_VERSION_MAJOR).$(PDFIO_VERSION_MINOR)" >>$@
echo "EXPORTS" >>$@
nm $(LIBOBJS) 2>/dev/null | grep "T _" | awk '{print $$3}' | \
grep -v '^_ttf' | sed -e '1,$$s/^_//' | sort >>$@
# pdfio text extraction (demo, doesn't handle a lot of things yet)
pdfiototext: pdfiototext.o libpdfio.a
echo Linking $@...
$(CC) $(LDFLAGS) -o $@ pdfiototext.o libpdfio.a $(LIBS)
# pdfio test program
testpdfio: testpdfio.o libpdfio.a
echo Linking $@...
$(CC) $(LDFLAGS) -o $@ testpdfio.o libpdfio.a $(LIBS)
# TTF test program
testttf: ttf.o testttf.o
echo Linking $@...
$(CC) $(LDFLAGS) -o testttf ttf.o testttf.o $(LIBS)
# Dependencies
$(OBJS): pdfio.h pdfio-private.h Makefile
pdfio-content.o: pdfio-content.h ttf.h
testttf.o: ttf.h
ttf.o: ttf.h
# Make documentation using Codedoc <https://www.msweet.org/codedoc>
DOCFLAGS = \
--author "Michael R Sweet" \
--copyright "Copyright (c) 2021-2023 by Michael R Sweet" \
--docversion $(PDFIO_VERSION)
.PHONY: doc
doc:
echo Generating documentation...
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSION)" $(PUBHEADERS) $(PUBOBJS:.o=.c) --body doc/pdfio.md --coverimage doc/pdfio-512.png pdfio.xml >doc/pdfio.html
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSION)" --body doc/pdfio.md --coverimage doc/pdfio-epub.png pdfio.xml --epub doc/pdfio.epub
codedoc $(DOCFLAGS) --title "pdf read/write library" --man pdfio --section 3 --body doc/pdfio.md pdfio.xml >doc/pdfio.3
rm -f pdfio.xml
# Fuzz-test the library <https://lcamtuf.coredump.cx/afl/>
.PHONY: afl
afl:
$(MAKE) -$(MAKEFLAGS) CC="afl-clang-fast" COMMONFLAGS="-g" clean all
test afl-output || rm -rf afl-output
afl-fuzz -x afl-pdf.dict -i afl-input -o afl-output -V 600 -e pdf -t 5000 ./testpdfio @@
# Analyze code with the Clang static analyzer <https://clang-analyzer.llvm.org>
clang:
clang $(CPPFLAGS) --analyze $(OBJS:.o=.c) 2>clang.log
rm -rf $(OBJS:.o=.plist)
test -s clang.log && (echo "$(GHA_ERROR)Clang detected issues."; echo ""; cat clang.log; exit 1) || exit 0
# 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
test -s cppcheck.log && (echo "$(GHA_ERROR)Cppcheck detected issues."; echo ""; cat cppcheck.log; exit 1) || exit 0

2
NOTICE
View File

@ -1,6 +1,6 @@
PDFio - PDF Read/Write Library PDFio - PDF Read/Write Library
Copyright © 2021 by Michael R Sweet. Copyright © 2021-2023 by Michael R Sweet.
(Optional) Exceptions to the Apache 2.0 License: (Optional) Exceptions to the Apache 2.0 License:
================================================ ================================================

View File

@ -3,10 +3,8 @@ pdfio - PDF Read/Write Library
![Version](https://img.shields.io/github/v/release/michaelrsweet/pdfio?include_prereleases) ![Version](https://img.shields.io/github/v/release/michaelrsweet/pdfio?include_prereleases)
![Apache 2.0](https://img.shields.io/github/license/michaelrsweet/pdfio) ![Apache 2.0](https://img.shields.io/github/license/michaelrsweet/pdfio)
[![Build](https://github.com/michaelrsweet/pdfio/workflows/Build/badge.svg)](https://github.com/michaelrsweet/pdfio/actions/workflows/build.yml) [![Build Status](https://img.shields.io/github/workflow/status/michaelrsweet/pdfio/Build)](https://github.com/michaelrsweet/pdfio/actions/workflows/build.yml)
[![Coverity Scan Status](https://img.shields.io/coverity/scan/22385.svg)](https://scan.coverity.com/projects/michaelrsweet-pdfio) [![Coverity Scan Status](https://img.shields.io/coverity/scan/22385.svg)](https://scan.coverity.com/projects/michaelrsweet-pdfio)
[![LGTM Grade](https://img.shields.io/lgtm/grade/cpp/github/michaelrsweet/pdfio)](https://lgtm.com/projects/g/michaelrsweet/pdfio/context:cpp)
[![LGTM Alerts](https://img.shields.io/lgtm/alerts/github/michaelrsweet/pdfio)](https://lgtm.com/projects/g/michaelrsweet/pdfio/)
PDFio is a simple C library for reading and writing PDF files. The primary PDFio is a simple C library for reading and writing PDF files. The primary
goals of PDFio are: goals of PDFio are:
@ -30,7 +28,7 @@ PDFio requires the following to build the software:
- A C99 compiler such as Clang, GCC, or MS Visual C - A C99 compiler such as Clang, GCC, or MS Visual C
- A POSIX-compliant `make` program - A POSIX-compliant `make` program
- ZLIB (<https://www.zlib.net>) 1.0 or higher - ZLIB (<https://www.zlib.net>) 1.1 or higher
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided. IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
@ -38,15 +36,27 @@ IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
Documentation Documentation
------------- -------------
See the man page (`pdfio.3`), frequently ask questions (`FAQ.md`), and full HTML See the man page (`pdfio.3`) and full HTML documentation (`pdfio.html`) for
documentation (`pdfio.html`) for information on using PDFio. information on using PDFio.
Installing pdfio Installing PDFio
---------------- ----------------
PDFio comes with a portable makefile that will work on any POSIX-compliant PDFio uses a configure script on Unix systems to generate a makefile:
system with ZLIB installed. To make it, run:
./configure
If you want a shared library, run:
./configure --enable-shared
The default installation location is "/usr/local". Pass the `--prefix` option
to make to install it to another location:
./configure --prefix=/some/other/directory
Once configured, run the following to make the library:
make all make all
@ -56,45 +66,7 @@ To test it, run:
To install it, run: To install it, run:
make install sudo make install
If you want a shared library, run:
make all-shared
make install-shared
The default installation location is "/usr/local". Pass the `prefix` variable
to make to install it to another location:
make install prefix=/some/other/directory
The makefile installs the pdfio header to "${prefix}/include", the library to
"${prefix}/lib", the `pkg-config` file to "${prefix}/lib/pkgconfig", the man
page to "${prefix}/share/man/man3", and the documentation to
"${prefix}/share/doc/pdfio".
The makefile supports the following variables that can be specified in the make
command or as environment variables:
- `AR`: the library archiver (default "ar")
- `ARFLAGS`: options for the library archiver (default "cr")
- `CC`: the C compiler (default "cc")
- `CFLAGS`: options for the C compiler (default "")
- `CODESIGN_IDENTITY`: the identity to use when code signing the shared library
on macOS (default "Developer ID")
- `COMMONFLAGS`: options for the C compiler and linker (typically architecture
and optimization options, default is "-Os -g")
- `CPPFLAGS`: options for the C preprocessor (default "")
- `DESTDIR` and `DSTROOT`: specifies a root directory when installing
(default is "", specify only one)
- `DSOFLAGS`: options for the C compiler when linking the shared library
(default "")
- `LDFLAGS`: options for the C compiler when linking the test programs
(default "")
- `LIBS`: library options when linking the test programs (default "-lz")
- `RANLIB`: program that generates a table-of-contents in a library
(default "ranlib")
- `prefix`: specifies the installation directory (default "/usr/local")
Visual Studio Project Visual Studio Project
@ -113,15 +85,11 @@ generates a static library that will be installed under "/usr/local" with:
sudo xcodebuild install sudo xcodebuild install
You can reproduce this with the makefile using:
sudo make COMMONFLAGS="-Os -mmacosx-version-min=10.14 -arch x86_64 -arch arm64" install
Legal Stuff Legal Stuff
----------- -----------
PDFio is Copyright © 2021 by Michael R Sweet. PDFio is Copyright © 2021-2023 by Michael R Sweet.
This software is licensed under the Apache License Version 2.0 with an This software is licensed under the Apache License Version 2.0 with an
(optional) exception to allow linking against GPL2/LGPL2 software. See the (optional) exception to allow linking against GPL2/LGPL2 software. See the

View File

@ -5,6 +5,25 @@ This file describes how security issues are reported and handled, and what the
expectations are for security issues reported to this project. expectations are for security issues reported to this project.
Reporting a Security Bug
------------------------
For the purposes of this project, a security bug is a software defect that
allows a *local or remote user* to gain unauthorized access or privileges on the
host computer or to cause the software to crash. Such defects should be
reported to the project security advisory page at
<https://github.com/michaelrsweet/pdfio/security/advisories>.
Alternately, security bugs can be reported to "security AT msweet.org" using the
PGP public key below. Expect a response within 5 business days. Any proposed
embargo date should be at least 30 days and no more than 90 days in the future.
> *Note:* If you've found a software defect that allows a *program* to gain
> unauthorized access or privileges on the host computer or causes the program
> to crash, that defect should be reported as an ordinary project issue at
> <https://github.com/michaelrsweet/pdfio/issues>.
Responsible Disclosure Responsible Disclosure
---------------------- ----------------------
@ -50,14 +69,6 @@ example:
1.0rc1 1.0rc1
Reporting a Vulnerability
-------------------------
Report all security issues to "security AT msweet.org". Expect a response
within 5 business days. Any proposed embargo date should be at least 30 days
and no more than 90 days in the future.
PGP Public Key PGP Public Key
-------------- --------------

1774
config.guess vendored Executable file

File diff suppressed because it is too large Load Diff

1907
config.sub vendored Executable file

File diff suppressed because it is too large Load Diff

5554
configure vendored Executable file

File diff suppressed because it is too large Load Diff

272
configure.ac Normal file
View File

@ -0,0 +1,272 @@
dnl
dnl Configuration script for PDFio
dnl
dnl Copyright © 2023 by Michael R Sweet
dnl
dnl Licensed under Apache License v2.0. See the file "LICENSE" for more
dnl information.
dnl
dnl ***********************************************************************
dnl
dnl Note: Using autoheader or automake on this project will break the PDFio
dnl build system. Use "autoconf -f" to regenerate the configure script if
dnl you make changes to this file.
dnl
dnl ***********************************************************************
dnl We need at least autoconf 2.70 for --runstatedir...
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])
PDFIO_VERSION="AC_PACKAGE_VERSION"
PDFIO_VERSION_MAJOR="`echo AC_PACKAGE_VERSION | awk -F. '{print $1}'`"
PDFIO_VERSION_MINOR="`echo AC_PACKAGE_VERSION | awk -F. '{printf("%d\n",$2);}'`"
AC_SUBST([PDFIO_VERSION])
AC_SUBST([PDFIO_VERSION_MAJOR])
AC_SUBST([PDFIO_VERSION_MINOR])
dnl This line is provided to ensure that you don't run the autoheader program
dnl against this project. Doing so is completely unsupported and WILL cause
dnl problems!
AH_TOP([#error "Somebody ran autoheader on this project which is unsupported and WILL cause problems."])
dnl Get the build and host platforms and split the host_os value
AC_CANONICAL_BUILD
AC_CANONICAL_HOST
[host_os_name="$(echo $host_os | sed -e '1,$s/[0-9.]*$//g')"]
[host_os_version="$(echo $host_os | sed -e '1,$s/^[^0-9.]*//g' | awk -F. '{print $1 $2}')"]
# Linux often does not yield an OS version we can use...
AS_IF([test "x$host_os_version" = x], [
host_os_version="0"
])
dnl Compiler options...
CFLAGS="${CFLAGS:=}"
CPPFLAGS="${CPPFLAGS:=}"
DSOFLAGS="${DSOFLAGS:=}"
LDFLAGS="${LDFLAGS:=}"
LIBS="${LIBS:=}"
OPTIM="${OPTIM:=}"
AC_SUBST([DSOFLAGS])
AC_SUBST([OPTIM])
dnl Standard programs...
AC_PROG_CC
AC_PROG_RANLIB
AC_PATH_PROG([AR], [ar])
AC_PATH_PROGS([CODE_SIGN], [codesign true])
AC_PATH_PROG([MKDIR], [mkdir])
AC_PATH_PROG([RM], [rm])
AC_PATH_PROG([RMDIR], [rmdir])
AC_PATH_PROG([LN], [ln])
dnl Figure out the correct "ar" command flags...
AS_IF([test "$ac_cv_prog_ranlib" = ":"], [
ARFLAGS="crs"
], [
ARFLAGS="cr"
])
AC_SUBST([ARFLAGS])
dnl install-sh
AC_MSG_CHECKING([for install-sh script])
INSTALL="$(pwd)/install-sh"
AC_SUBST([INSTALL])
AC_MSG_RESULT([using $INSTALL])
dnl Check for pkg-config, which is used for some other tests later on...
AC_PATH_TOOL([PKGCONFIG], [pkg-config])
PKGCONFIG_CFLAGS="-I\${includedir}"
PKGCONFIG_LIBS="-L\${libdir} -lpdfio"
PKGCONFIG_LIBS_PRIVATE="-lm"
PKGCONFIG_REQUIRES="zlib"
AC_SUBST([PKGCONFIG_CFLAGS])
AC_SUBST([PKGCONFIG_LIBS])
AC_SUBST([PKGCONFIG_LIBS_PRIVATE])
AC_SUBST([PKGCONFIG_REQUIRES])
dnl ZLIB
AC_MSG_CHECKING([for zlib via pkg-config])
AS_IF([$PKCONFIG --exists zlib], [
AC_MSG_RESULT([yes])
LIBS="$($PKGCONFIG --libs zlib) $LIBS"
CPPFLAGS="$($PKGCONFIG --cflags zlib) $CPPFLAGS"
],[
AC_MSG_RESULT([no])
AC_CHECK_HEADER([zlib.h])
AC_CHECK_LIB([z], [inflateCopy])
AS_IF([test x$ac_cv_header_zlib_h != xyes -o x$ac_cv_lib_z_inflateCopy != xyes], [
AC_MSG_ERROR([Sorry, this software requires zlib 1.1 or higher.])
])
PKGCONFIG_LIBS_PRIVATE="-lz $PKGCONFIG_LIBS_PRIVATE"
])
dnl Library target...
AC_ARG_ENABLE([static], AS_HELP_STRING([--disable-static], [do not install static library]))
AC_ARG_ENABLE([shared], AS_HELP_STRING([--enable-shared], [install shared library]))
AS_IF([test x$enable_shared = xyes], [
AS_IF([test "$host_os_name" = darwin], [
LIBPDFIO="libpdfio.1.dylib"
], [
LIBPDFIO="libpdfio.so.1"
])
AS_IF([test x$enable_static != xno], [
LIBPDFIO_STATIC="libpdfio.a"
], [
LIBPDFIO_STATIC=""
])
], [
LIBPDFIO="libpdfio.a"
LIBPDFIO_STATIC=""
PKGCONFIG_LIBS="$PKGCONFIG_LIBS $PKGCONFIG_LIBS_PRIVATE"
PKGCONFIG_LIBS_PRIVATE=""
])
AC_SUBST([LIBPDFIO])
AC_SUBST([LIBPDFIO_STATIC])
dnl Extra compiler options...
AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug], [turn on debugging, default=no]))
AC_ARG_ENABLE([maintainer], AS_HELP_STRING([--enable-maintainer], [turn on maintainer mode, default=no]))
AC_ARG_ENABLE([sanitizer], AS_HELP_STRING([--enable-sanitizer], [build with AddressSanitizer, default=no]))
AS_IF([test x$enable_debug = xyes], [
OPTIM="$OPTIM -g"
CSFLAGS=""
], [
OPTIM="$OPTIM -g -Os"
CSFLAGS="-o runtime"
])
AC_SUBST([CSFLAGS])
WARNINGS=""
AC_SUBST([WARNINGS])
AS_IF([test -n "$GCC"], [
AS_IF([test x$enable_sanitizer = xyes], [
# Use -fsanitize=address with debugging...
OPTIM="$OPTIM -fsanitize=address"
], [
# Otherwise use the Fortify enhancements to catch any unbounded
# string operations...
CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2"
])
dnl Show all standard warnings + unused variables when compiling...
WARNINGS="-Wall -Wunused"
dnl Drop some not-useful/unreliable warnings...
for warning in char-subscripts format-truncation format-y2k switch unused-result; do
AC_MSG_CHECKING([whether compiler supports -Wno-$warning])
OLDCFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wno-$warning -Werror"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM()], [
AC_MSG_RESULT(yes)
WARNINGS="$WARNINGS -Wno-$warning"
], [
AC_MSG_RESULT(no)
])
CFLAGS="$OLDCFLAGS"
done
dnl Maintainer mode enables -Werror...
AS_IF([test x$enable_maintainer = xyes], [
WARNINGS="$WARNINGS -Werror -Wno-error=deprecated"
])
dnl See if PIE options are supported...
AC_MSG_CHECKING(whether compiler supports -fPIE)
OLDCFLAGS="$CFLAGS"
AS_CASE(["$host_os_name"],
[darwin*], [
CFLAGS="$CFLAGS -fPIC -fPIE -Wl,-pie"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[
OLDCFLAGS="-fPIC $OLDCFLAGS"
LDFLAGS="-fPIE -Wl,-pie $LDFLAGS"
AC_MSG_RESULT(yes)
],[
AC_MSG_RESULT(no)
])
], [*], [
CFLAGS="$CFLAGS -fPIC -fPIE -pie"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[
OLDCFLAGS="-fPIC $OLDCFLAGS"
LDFLAGS="-fPIE -pie $LDFLAGS"
AC_MSG_RESULT(yes)
],[
AC_MSG_RESULT(no)
])
])
CFLAGS="$OLDCFLAGS"
dnl OS-specific compiler options...
AC_MSG_CHECKING([for OS-specific compiler options])
AS_CASE(["$host_os_name"], [linux*], [
# Make sure we get the full set of 64-bit Linux APIs from the headers...
CPPFLAGS="$CPPFLAGS -D__USE_MISC -D_GNU_SOURCE -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64"
# Mark read-only sections as relocatable to random addresses...
LDFLAGS="$LDFLAGS -Wl,-z,relro,-z,now"
AC_MSG_RESULT([-D__USE_MISC -D_GNU_SOURCE -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64 -Wl,-z,relro,-z,now])
], [darwin*], [
# When not building for debug, target macOS 11 or later, "universal"
# binaries when possible...
AS_IF([echo "$CPPFLAGS $CFLAGS $LDFLAGS $OPTIM" | grep -q "\\-arch "], [
# Don't add architecture/min-version flags if they are already present
AC_MSG_RESULT([none])
], [echo "$CPPFLAGS $CFLAGS $LDFLAGS $OPTIM" | grep -q "\\-mmacosx-version-"], [
# Don't add architecture/min-version flags if they are already present
AC_MSG_RESULT([none])
], [test "$host_os_version" -ge 200 -a x$enable_debug != xyes], [
# macOS 11.0 and higher support the Apple Silicon (arm64) CPUs
OPTIM="$OPTIM -mmacosx-version-min=11.0 -arch x86_64 -arch arm64"
AC_MSG_RESULT([-mmacosx-version-min=11.0 -arch x86_64 -arch arm64])
], [
# Don't add architecture/min-version flags if debug enabled
AC_MSG_RESULT([none])
])
], [*], [
AC_MSG_RESULT([none])
])
])
dnl Extra linker options...
AC_ARG_WITH([dsoflags], AS_HELP_STRING([--with-dsoflags=...], [Specify additional DSOFLAGS]), [
DSOFLAGS="$withval $DSOFLAGS"
])
AC_ARG_WITH([ldflags], AS_HELP_STRING([--with-ldflags=...], [Specify additional LDFLAGS]), [
LDFLAGS="$withval $LDFLAGS"
])
dnl Generate the Makefile and pkg-config file...
AC_CONFIG_FILES([Makefile pdfio.pc])
AC_OUTPUT

View File

@ -1,4 +1,4 @@
.TH pdfio 3 "pdf read/write library" "2021-12-14" "pdf read/write library" .TH pdfio 3 "pdf read/write library" "2023-12-05" "pdf read/write library"
.SH NAME .SH NAME
pdfio \- pdf read/write library pdfio \- pdf read/write library
.SH Introduction .SH Introduction
@ -34,7 +34,7 @@ PDFio is
.I not .I not
concerned with rendering or viewing a PDF file, although a PDF RIP or viewer could be written using it. concerned with rendering or viewing a PDF file, although a PDF RIP or viewer could be written using it.
.PP .PP
PDFio is Copyright \[co] 2021 by Michael R Sweet and is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software. See the files "LICENSE" and "NOTICE" for more information. PDFio is Copyright \[co] 2021\-2023 by Michael R Sweet and is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software. See the files "LICENSE" and "NOTICE" for more information.
.SS Requirements .SS Requirements
.PP .PP
PDFio requires the following to build the software: PDFio requires the following to build the software:
@ -46,6 +46,10 @@ A C99 compiler such as Clang, GCC, or MS Visual C
.PP .PP
A POSIX\-compliant make program A POSIX\-compliant make program
.IP \(bu 5
.PP
A POSIX\-compliant sh program
.IP \(bu 5 .IP \(bu 5
.PP .PP
ZLIB (https://www.zlib.net) 1.0 or higher ZLIB (https://www.zlib.net) 1.0 or higher
@ -55,10 +59,11 @@ ZLIB (https://www.zlib.net) 1.0 or higher
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided. IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
.SS Installing pdfio .SS Installing pdfio
.PP .PP
PDFio comes with a portable makefile that will work on any POSIX\-compliant system with ZLIB installed. To make it, run: PDFio comes with a configure script that creates a portable makefile that will work on any POSIX\-compliant system with ZLIB installed. To make it, run:
.nf .nf
make all \./configure
make
.fi .fi
.PP .PP
To test it, run: To test it, run:
@ -70,78 +75,28 @@ To test it, run:
To install it, run: To install it, run:
.nf .nf
make install sudo make install
.fi .fi
.PP .PP
If you want a shared library, run: If you want a shared library, run:
.nf .nf
make all\-shared \./configure \-\-enable\-shared
make install\-shared make
sudo make install
.fi .fi
.PP .PP
The default installation location is "/usr/local". Pass the prefix variable to make to install it to another location: The default installation location is "/usr/local". Pass the \-\-prefix option to make to install it to another location:
.nf .nf
make install prefix=/some/other/directory \./configure \-\-prefix=/some/other/directory
.fi .fi
.PP .PP
The makefile installs the pdfio header to "${prefix}/include", the library to "${prefix}/lib", the pkg\-config file to "${prefix}/lib/pkgconfig", the man page to "${prefix}/share/man/man3", and the documentation to "${prefix}/share/doc/pdfio". Other configure options can be found using the \-\-help option:
.PP .nf
The makefile supports the following variables that can be specified in the make command or as environment variables:
.IP \(bu 5
.PP
AR: the library archiver (default "ar")
.IP \(bu 5
.PP
ARFLAGS: options for the library archiver (default "cr")
.IP \(bu 5
.PP
CC: the C compiler (default "cc")
.IP \(bu 5
.PP
CFLAGS: options for the C compiler (default "")
.IP \(bu 5
.PP
CODESIGN_IDENTITY: the identity to use when code signing the shared library on macOS (default "Developer ID")
.IP \(bu 5
.PP
COMMONFLAGS: options for the C compiler and linker (typically architecture and optimization options, default is "\-Os \-g")
.IP \(bu 5
.PP
CPPFLAGS: options for the C preprocessor (default "")
.IP \(bu 5
.PP
DESTDIR and DSTROOT: specifies a root directory when installing (default is "", specify only one)
.IP \(bu 5
.PP
DSOFLAGS: options for the C compiler when linking the shared library (default "")
.IP \(bu 5
.PP
LDFLAGS: options for the C compiler when linking the test programs (default "")
.IP \(bu 5
.PP
LIBS: library options when linking the test programs (default "\-lz")
.IP \(bu 5
.PP
RANLIB: program that generates a table\-of\-contents in a library (default "ranlib")
.IP \(bu 5
.PP
prefix: specifies the installation directory (default "/usr/local")
\./configure \-\-help
.fi
.SS Visual Studio Project .SS Visual Studio Project
.PP .PP
The Visual Studio solution ("pdfio.sln") is provided for Windows developers and generates both a static library and DLL. The Visual Studio solution ("pdfio.sln") is provided for Windows developers and generates both a static library and DLL.
@ -152,12 +107,6 @@ There is also an Xcode project ("pdfio.xcodeproj") you can use on macOS which ge
sudo xcodebuild install sudo xcodebuild install
.fi .fi
.PP
You can reproduce this with the makefile using:
.nf
sudo make COMMONFLAGS="\-Os \-mmacosx\-version\-min=10.14 \-arch x86_64 \-arch arm64" install
.fi
.SS Detecting PDFio .SS Detecting PDFio
.PP .PP
PDFio can be detected using the pkg\-config command, for example: PDFio can be detected using the pkg\-config command, for example:
@ -218,10 +167,25 @@ pdfio_stream_t: An object stream
You open an existing PDF file using the pdfioFileOpen function: You open an existing PDF file using the pdfioFileOpen function:
.nf .nf
pdfio_file_t *pdf = pdfioFileOpen("myinputfile.pdf", error_cb, error_data); pdfio_file_t *pdf = pdfioFileOpen("myinputfile.pdf", password_cb, password_data,
error_cb, error_data);
.fi .fi
.PP .PP
where the three arguments to the function are the filename ("myinputfile.pdf"), an optional error callback function (error_cb), and an optional pointer value for the error callback function (error_data). The error callback is called for both errors and warnings and accepts the pdfio_file_t pointer, a message string, and the callback pointer value, for example: where the five arguments to the function are the filename ("myinputfile.pdf"), an optional password callback function (password_cb) and data pointer value (password_data), and an optional error callback function (error_cb) and data pointer value (error_data). The password callback is called for encrypted PDF files that are not using the default password, for example:
.nf
const char *
password_cb(void *data, const char *filename)
{
(void)data; // This callback doesn't use the data pointer
(void)filename; // This callback doesn't use the filename
// Return a password string for the file...
return ("Password42");
}
.fi
.PP
The error callback is called for both errors and warnings and accepts the pdfio_file_t pointer, a message string, and the callback pointer value, for example:
.nf .nf
bool bool
@ -254,7 +218,7 @@ Each PDF file contains one or more pages. The pdfioFileGetNumPages function retu
} }
.fi .fi
.PP .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. 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.
.PP .PP
The pdfioFileClose function closes a PDF file and frees all memory that was used for it: The pdfioFileClose function closes a PDF file and frees all memory that was used for it:
.nf .nf
@ -324,6 +288,14 @@ Some PDF objects have an associated data stream, such as for pages, images, ICC
.PP .PP
The first argument is the object pointer. The second argument is a boolean value that specifies whether you want to decode (typically decompress) the stream data or return it as\-is. The first argument is the object pointer. The second argument is a boolean value that specifies whether you want to decode (typically decompress) the stream data or return it as\-is.
.PP .PP
When reading a page stream you'll use the pdfioPageOpenStream function instead:
.nf
pdfio_file_t *pdf = pdfioFileOpen(...);
pdfio_obj_t *obj = pdfioFileGetPage(pdf, number);
pdfio_stream_t *st = pdfioPageOpenStream(obj, 0, true);
.fi
.PP
Once you have the stream open, you can use one of several functions to read from it: Once you have the stream open, you can use one of several functions to read from it:
.IP \(bu 5 .IP \(bu 5
.PP .PP
@ -353,12 +325,21 @@ To create a stream for a new object, call the pdfioObjCreateStream function:
.nf .nf
pdfio_file_t *pdf = pdfioFileCreate(...); pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_obj_t *pdfioFileCreateObj(pdf, ...); pdfio_obj_t *obj = pdfioFileCreateObj(pdf, ...);
pdfio_stream_t *pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE); pdfio_stream_t *st = pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE);
.fi .fi
.PP .PP
The first argument is the newly created object. The second argument is either PDFIO_FILTER_NONE to specify that any encoding is done by your program or PDFIO_FILTER_FLATE to specify that PDFio should Flate compress the stream. The first argument is the newly created object. The second argument is either PDFIO_FILTER_NONE to specify that any encoding is done by your program or PDFIO_FILTER_FLATE to specify that PDFio should Flate compress the stream.
.PP .PP
To create a page content stream call the pdfioFileCreatePage function:
.nf
pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_dict_t *dict = pdfioDictCreate(pdf);
\... set page dictionary keys and values ...
pdfio_stream_t *st = pdfioFileCreatePage(pdf, dict);
.fi
.PP
Once you have created the stream, use any of the following functions to write to the stream: Once you have created the stream, use any of the following functions to write to the stream:
.IP \(bu 5 .IP \(bu 5
.PP .PP
@ -383,7 +364,7 @@ The PDF content helper functions provide additional functions for writing specif
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 .SS PDF Content Helper Functions
.PP .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 ??? categories: 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:
.IP \(bu 5 .IP \(bu 5
.PP .PP
Color Space Functions Color Space Functions
@ -535,6 +516,7 @@ will embed an OpenSans Regular TrueType font using the Windows CP1252 subset of
will embed the NotoSansJP Regular OpenType font with full support for Unicode. will embed the NotoSansJP Regular OpenType font with full support for Unicode.
.PP .PP
Note: Not all fonts support Unicode. Note: Not all fonts support Unicode.
.PP .PP
Image Object Functions Image Object Functions
.PP .PP
@ -782,7 +764,15 @@ pdfioContentTextMoveTo moves within the current line in a text block
.IP \(bu 5 .IP \(bu 5
.PP .PP
pdfioContentTextNextLine moves to the beginning of the next line in a text block pdfioContentTextNewLine moves to the beginning of the next line in a text block
.IP \(bu 5
.PP
pdfioContentTextNewLineShow moves to the beginning of the next line in a text block and shows literal text with optional word and character spacing
.IP \(bu 5
.PP
pdfioContentTextNewLineShowf moves to the beginning of the next line in a text block and shows formatted text with optional word and character spacing
.IP \(bu 5 .IP \(bu 5
.PP .PP
@ -1340,7 +1330,7 @@ bool pdfioContentFillAndStroke (
.fi .fi
.SS pdfioContentMatrixConcat .SS pdfioContentMatrixConcat
Concatenate a matrix to the current graphics Concatenate a matrix to the current graphics
state. state.
.PP .PP
.nf .nf
bool pdfioContentMatrixConcat ( bool pdfioContentMatrixConcat (
@ -1423,6 +1413,14 @@ bool pdfioContentPathCurve23 (
double y3 double y3
); );
.fi .fi
.SS pdfioContentPathEnd
Clear the current path.
.PP
.nf
bool pdfioContentPathEnd (
pdfio_stream_t *st
);
.fi
.SS pdfioContentPathLineTo .SS pdfioContentPathLineTo
Add a straight line to the current path. Add a straight line to the current path.
.PP .PP
@ -1482,6 +1480,9 @@ bool pdfioContentSetDashPattern (
double off double off
); );
.fi .fi
.PP
This function sets the stroke pattern when drawing lines. If "on" and "off"
are 0, a solid line is drawn.
.SS pdfioContentSetFillColorDeviceCMYK .SS pdfioContentSetFillColorDeviceCMYK
Set device CMYK fill color. Set device CMYK fill color.
.PP .PP
@ -1746,6 +1747,20 @@ bool pdfioContentTextEnd (
pdfio_stream_t *st pdfio_stream_t *st
); );
.fi .fi
.SS pdfioContentTextMeasure
Measure a text string and return its width.
.PP
.nf
double pdfioContentTextMeasure (
pdfio_obj_t *font,
const char *s,
double size
);
.fi
.PP
This function measures the given text string "s" and returns its width based
on "size". The text string must always use the UTF-8 (Unicode) encoding but
any control characters (such as newlines) are ignored.
.SS pdfioContentTextMoveLine .SS pdfioContentTextMoveLine
Move to the next line and offset. Move to the next line and offset.
.PP .PP
@ -1766,14 +1781,49 @@ bool pdfioContentTextMoveTo (
double ty double ty
); );
.fi .fi
.SS pdfioContentTextNextLine .SS pdfioContentTextNewLine
Move to the next line. Move to the next line.
.PP .PP
.nf .nf
bool pdfioContentTextNextLine ( bool pdfioContentTextNewLine (
pdfio_stream_t *st pdfio_stream_t *st
); );
.fi .fi
.SS pdfioContentTextNewLineShow
Move to the next line and show text.
.PP
.nf
bool pdfioContentTextNewLineShow (
pdfio_stream_t *st,
double ws,
double cs,
bool unicode,
const char *s
);
.fi
.PP
This function moves to the next line and then shows some text with optional
word and character spacing in a PDF content stream. The "unicode" argument
specifies that the current font maps to full Unicode. The "s" argument
specifies a UTF-8 encoded string.
.SS pdfioContentTextNewLineShowf
Show formatted text.
.PP
.nf
bool pdfioContentTextNewLineShowf (
pdfio_stream_t *st,
double ws,
double cs,
bool unicode,
const char *format,
...
);
.fi
.PP
This function moves to the next line and shows some formatted text with
optional word and character spacing in a PDF content stream. The "unicode"
argument specifies that the current font maps to full Unicode. The "format"
argument specifies a UTF-8 encoded \fBprintf\fR-style format string.
.SS pdfioContentTextShow .SS pdfioContentTextShow
Show text. Show text.
.PP .PP
@ -1933,6 +1983,32 @@ pdfio_valtype_t pdfioDictGetType (
const char *key const char *key
); );
.fi .fi
.SS pdfioDictIterateKeys
Iterate the keys in a dictionary.
.PP
.nf
void pdfioDictIterateKeys (
pdfio_dict_t *dict,
pdfio_dict_cb_t cb,
void *cb_data
);
.fi
.PP
This function iterates the keys in a dictionary, calling the supplied
function "cb":
.PP
.nf
bool
my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data)
{
... "key" contains the dictionary key ...
... return true to continue or false to stop ...
}
.fi
The iteration continues as long as the callback returns \fBtrue\fR or all keys
have been iterated.
.SS pdfioDictSetArray .SS pdfioDictSetArray
Set a key array in a dictionary. Set a key array in a dictionary.
.PP .PP
@ -2222,6 +2298,18 @@ Note: Currently PNG support is limited to grayscale, RGB, or indexed files
without interlacing or alpha. Transparency (masking) based on color/index without interlacing or alpha. Transparency (masking) based on color/index
.IP 5 .IP 5
is supported. is supported.
.SS pdfioFileCreateNumberObj
Create a new object in a PDF file containing a number.
.PP
.nf
pdfio_obj_t * pdfioFileCreateNumberObj (
pdfio_file_t *pdf,
double number
);
.fi
.PP
This function creates a new object with a number value in a PDF file.
You must call \fIpdfioObjClose\fR to write the object to the file.
.SS pdfioFileCreateObj .SS pdfioFileCreateObj
Create a new object in a PDF file. Create a new object in a PDF file.
.PP .PP
@ -2255,10 +2343,11 @@ written:
ssize_t ssize_t
output_cb(void *output_ctx, const void *buffer, size_t bytes) output_cb(void *output_ctx, const void *buffer, size_t bytes)
{ {
// Write buffer to output and return the number of bytes written // Write buffer to output and return the number of bytes written
} }
.fi .fi
The "version" argument specifies the PDF version number for the file or The "version" argument specifies the PDF version number for the file or
\fBNULL\fR for the default ("2.0"). \fBNULL\fR for the default ("2.0").
.PP .PP
@ -2285,6 +2374,32 @@ pdfio_stream_t * pdfioFileCreatePage (
pdfio_dict_t *dict pdfio_dict_t *dict
); );
.fi .fi
.SS pdfioFileCreateStringObj
Create a new object in a PDF file containing a string.
.PP
.nf
pdfio_obj_t * pdfioFileCreateStringObj (
pdfio_file_t *pdf,
const char *string
);
.fi
.PP
This function creates a new object with a string value in a PDF file.
You must call \fIpdfioObjClose\fR to write the object to the file.
.SS pdfioFileCreateTemporary
.PP
.nf
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
);
.fi
.SS pdfioFileFindObj .SS pdfioFileFindObj
Find an object using its object number. Find an object using its object number.
.PP .PP
@ -2552,7 +2667,7 @@ double pdfioImageGetWidth (
.fi .fi
.SS pdfioObjClose .SS pdfioObjClose
Close an object, writing any data as needed to the PDF Close an object, writing any data as needed to the PDF
file. file.
.PP .PP
.nf .nf
bool pdfioObjClose ( bool pdfioObjClose (
@ -2693,6 +2808,24 @@ bool pdfioPageDictAddImage (
pdfio_obj_t *obj pdfio_obj_t *obj
); );
.fi .fi
.SS pdfioPageGetNumStreams
Get the number of content streams for a page object.
.PP
.nf
size_t pdfioPageGetNumStreams (
pdfio_obj_t *page
);
.fi
.SS pdfioPageOpenStream
Open a content stream for a page.
.PP
.nf
pdfio_stream_t * pdfioPageOpenStream (
pdfio_obj_t *page,
size_t n,
bool decode
);
.fi
.SS pdfioStreamClose .SS pdfioStreamClose
Close a (data) stream in a PDF file. Close a (data) stream in a PDF file.
.PP .PP
@ -2720,6 +2853,13 @@ bool pdfioStreamGetToken (
size_t bufsize size_t bufsize
); );
.fi .fi
.PP
This function reads a single PDF token from a stream. Operator tokens,
boolean values, and numbers are returned as-is in the provided string buffer.
String values start with the opening parenthesis ('(') but have all escaping
resolved and the terminating parenthesis removed. Hexadecimal string values
start with the opening angle bracket ('<') and have all whitespace and the
terminating angle bracket removed.
.SS pdfioStreamPeek .SS pdfioStreamPeek
Peek at data in a stream. Peek at data in a stream.
.PP .PP
@ -2840,6 +2980,12 @@ Standard color spaces
.nf .nf
typedef enum pdfio_cs_e pdfio_cs_t; typedef enum pdfio_cs_e pdfio_cs_t;
.fi .fi
.SS pdfio_dict_cb_t
Dictionary iterator callback
.PP
.nf
typedef bool(*)(pdfio_dict_t *dict, const char *key, void *cb_data) pdfio_dict_cb_t;
.fi
.SS pdfio_dict_t .SS pdfio_dict_t
Key/value dictionary Key/value dictionary
.PP .PP
@ -2936,15 +3082,9 @@ PDF value types
.nf .nf
typedef enum pdfio_valtype_e pdfio_valtype_t; typedef enum pdfio_valtype_e pdfio_valtype_t;
.fi .fi
.SS state_t[4][4]
4x4 AES state table
.PP
.nf
typedef uint8_t state_t[4][4];
.fi
.SH AUTHOR .SH AUTHOR
.PP .PP
Michael R Sweet Michael R Sweet
.SH COPYRIGHT .SH COPYRIGHT
.PP .PP
Copyright (c) 2021 by Michael R Sweet Copyright (c) 2021-2023 by Michael R Sweet

View File

@ -1,13 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en-US"> <html lang="en-US">
<head> <head>
<title>PDFio Programming Manual v1.0.0</title> <title>PDFio Programming Manual v1.2.0</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="generator" content="codedoc v3.7"> <meta name="generator" content="codedoc v3.7">
<meta name="author" content="Michael R Sweet"> <meta name="author" content="Michael R Sweet">
<meta name="language" content="en-US"> <meta name="language" content="en-US">
<meta name="copyright" content="Copyright © 2021 by Michael R Sweet"> <meta name="copyright" content="Copyright © 2021-2023 by Michael R Sweet">
<meta name="version" content="1.0.0"> <meta name="version" content="1.2.0">
<style type="text/css"><!-- <style type="text/css"><!--
body { body {
background: white; background: white;
@ -245,9 +245,9 @@ span.string {
<body> <body>
<div class="header"> <div class="header">
<p><img class="title" src="pdfio-512.png"></p> <p><img class="title" src="pdfio-512.png"></p>
<h1 class="title">PDFio Programming Manual v1.0.0</h1> <h1 class="title">PDFio Programming Manual v1.2.0</h1>
<p>Michael R Sweet</p> <p>Michael R Sweet</p>
<p>Copyright © 2021 by Michael R Sweet</p> <p>Copyright © 2021-2023 by Michael R Sweet</p>
</div> </div>
<div class="contents"> <div class="contents">
<h2 class="title">Contents</h2> <h2 class="title">Contents</h2>
@ -307,6 +307,7 @@ span.string {
<li><a href="#pdfioContentPathCurve">pdfioContentPathCurve</a></li> <li><a href="#pdfioContentPathCurve">pdfioContentPathCurve</a></li>
<li><a href="#pdfioContentPathCurve13">pdfioContentPathCurve13</a></li> <li><a href="#pdfioContentPathCurve13">pdfioContentPathCurve13</a></li>
<li><a href="#pdfioContentPathCurve23">pdfioContentPathCurve23</a></li> <li><a href="#pdfioContentPathCurve23">pdfioContentPathCurve23</a></li>
<li><a href="#pdfioContentPathEnd">pdfioContentPathEnd</a></li>
<li><a href="#pdfioContentPathLineTo">pdfioContentPathLineTo</a></li> <li><a href="#pdfioContentPathLineTo">pdfioContentPathLineTo</a></li>
<li><a href="#pdfioContentPathMoveTo">pdfioContentPathMoveTo</a></li> <li><a href="#pdfioContentPathMoveTo">pdfioContentPathMoveTo</a></li>
<li><a href="#pdfioContentPathRect">pdfioContentPathRect</a></li> <li><a href="#pdfioContentPathRect">pdfioContentPathRect</a></li>
@ -341,9 +342,12 @@ span.string {
<li><a href="#pdfioContentStroke">pdfioContentStroke</a></li> <li><a href="#pdfioContentStroke">pdfioContentStroke</a></li>
<li><a href="#pdfioContentTextBegin">pdfioContentTextBegin</a></li> <li><a href="#pdfioContentTextBegin">pdfioContentTextBegin</a></li>
<li><a href="#pdfioContentTextEnd">pdfioContentTextEnd</a></li> <li><a href="#pdfioContentTextEnd">pdfioContentTextEnd</a></li>
<li><a href="#pdfioContentTextMeasure">pdfioContentTextMeasure</a></li>
<li><a href="#pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></li> <li><a href="#pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></li>
<li><a href="#pdfioContentTextMoveTo">pdfioContentTextMoveTo</a></li> <li><a href="#pdfioContentTextMoveTo">pdfioContentTextMoveTo</a></li>
<li><a href="#pdfioContentTextNextLine">pdfioContentTextNextLine</a></li> <li><a href="#pdfioContentTextNewLine">pdfioContentTextNewLine</a></li>
<li><a href="#pdfioContentTextNewLineShow">pdfioContentTextNewLineShow</a></li>
<li><a href="#pdfioContentTextNewLineShowf">pdfioContentTextNewLineShowf</a></li>
<li><a href="#pdfioContentTextShow">pdfioContentTextShow</a></li> <li><a href="#pdfioContentTextShow">pdfioContentTextShow</a></li>
<li><a href="#pdfioContentTextShowJustified">pdfioContentTextShowJustified</a></li> <li><a href="#pdfioContentTextShowJustified">pdfioContentTextShowJustified</a></li>
<li><a href="#pdfioContentTextShowf">pdfioContentTextShowf</a></li> <li><a href="#pdfioContentTextShowf">pdfioContentTextShowf</a></li>
@ -360,6 +364,7 @@ span.string {
<li><a href="#pdfioDictGetRect">pdfioDictGetRect</a></li> <li><a href="#pdfioDictGetRect">pdfioDictGetRect</a></li>
<li><a href="#pdfioDictGetString">pdfioDictGetString</a></li> <li><a href="#pdfioDictGetString">pdfioDictGetString</a></li>
<li><a href="#pdfioDictGetType">pdfioDictGetType</a></li> <li><a href="#pdfioDictGetType">pdfioDictGetType</a></li>
<li><a href="#pdfioDictIterateKeys">pdfioDictIterateKeys</a></li>
<li><a href="#pdfioDictSetArray">pdfioDictSetArray</a></li> <li><a href="#pdfioDictSetArray">pdfioDictSetArray</a></li>
<li><a href="#pdfioDictSetBinary">pdfioDictSetBinary</a></li> <li><a href="#pdfioDictSetBinary">pdfioDictSetBinary</a></li>
<li><a href="#pdfioDictSetBoolean">pdfioDictSetBoolean</a></li> <li><a href="#pdfioDictSetBoolean">pdfioDictSetBoolean</a></li>
@ -380,9 +385,12 @@ span.string {
<li><a href="#pdfioFileCreateICCObjFromFile">pdfioFileCreateICCObjFromFile</a></li> <li><a href="#pdfioFileCreateICCObjFromFile">pdfioFileCreateICCObjFromFile</a></li>
<li><a href="#pdfioFileCreateImageObjFromData">pdfioFileCreateImageObjFromData</a></li> <li><a href="#pdfioFileCreateImageObjFromData">pdfioFileCreateImageObjFromData</a></li>
<li><a href="#pdfioFileCreateImageObjFromFile">pdfioFileCreateImageObjFromFile</a></li> <li><a href="#pdfioFileCreateImageObjFromFile">pdfioFileCreateImageObjFromFile</a></li>
<li><a href="#pdfioFileCreateNumberObj">pdfioFileCreateNumberObj</a></li>
<li><a href="#pdfioFileCreateObj">pdfioFileCreateObj</a></li> <li><a href="#pdfioFileCreateObj">pdfioFileCreateObj</a></li>
<li><a href="#pdfioFileCreateOutput">pdfioFileCreateOutput</a></li> <li><a href="#pdfioFileCreateOutput">pdfioFileCreateOutput</a></li>
<li><a href="#pdfioFileCreatePage">pdfioFileCreatePage</a></li> <li><a href="#pdfioFileCreatePage">pdfioFileCreatePage</a></li>
<li><a href="#pdfioFileCreateStringObj">pdfioFileCreateStringObj</a></li>
<li><a href="#pdfioFileCreateTemporary">pdfioFileCreateTemporary</a></li>
<li><a href="#pdfioFileFindObj">pdfioFileFindObj</a></li> <li><a href="#pdfioFileFindObj">pdfioFileFindObj</a></li>
<li><a href="#pdfioFileGetAuthor">pdfioFileGetAuthor</a></li> <li><a href="#pdfioFileGetAuthor">pdfioFileGetAuthor</a></li>
<li><a href="#pdfioFileGetCreationDate">pdfioFileGetCreationDate</a></li> <li><a href="#pdfioFileGetCreationDate">pdfioFileGetCreationDate</a></li>
@ -425,6 +433,8 @@ span.string {
<li><a href="#pdfioPageDictAddColorSpace">pdfioPageDictAddColorSpace</a></li> <li><a href="#pdfioPageDictAddColorSpace">pdfioPageDictAddColorSpace</a></li>
<li><a href="#pdfioPageDictAddFont">pdfioPageDictAddFont</a></li> <li><a href="#pdfioPageDictAddFont">pdfioPageDictAddFont</a></li>
<li><a href="#pdfioPageDictAddImage">pdfioPageDictAddImage</a></li> <li><a href="#pdfioPageDictAddImage">pdfioPageDictAddImage</a></li>
<li><a href="#pdfioPageGetNumStreams">pdfioPageGetNumStreams</a></li>
<li><a href="#pdfioPageOpenStream">pdfioPageOpenStream</a></li>
<li><a href="#pdfioStreamClose">pdfioStreamClose</a></li> <li><a href="#pdfioStreamClose">pdfioStreamClose</a></li>
<li><a href="#pdfioStreamConsume">pdfioStreamConsume</a></li> <li><a href="#pdfioStreamConsume">pdfioStreamConsume</a></li>
<li><a href="#pdfioStreamGetToken">pdfioStreamGetToken</a></li> <li><a href="#pdfioStreamGetToken">pdfioStreamGetToken</a></li>
@ -440,6 +450,7 @@ span.string {
<li><a href="#TYPES">Data Types</a><ul class="subcontents"> <li><a href="#TYPES">Data Types</a><ul class="subcontents">
<li><a href="#pdfio_array_t">pdfio_array_t</a></li> <li><a href="#pdfio_array_t">pdfio_array_t</a></li>
<li><a href="#pdfio_cs_t">pdfio_cs_t</a></li> <li><a href="#pdfio_cs_t">pdfio_cs_t</a></li>
<li><a href="#pdfio_dict_cb_t">pdfio_dict_cb_t</a></li>
<li><a href="#pdfio_dict_t">pdfio_dict_t</a></li> <li><a href="#pdfio_dict_t">pdfio_dict_t</a></li>
<li><a href="#pdfio_encryption_t">pdfio_encryption_t</a></li> <li><a href="#pdfio_encryption_t">pdfio_encryption_t</a></li>
<li><a href="#pdfio_error_cb_t">pdfio_error_cb_t</a></li> <li><a href="#pdfio_error_cb_t">pdfio_error_cb_t</a></li>
@ -456,7 +467,6 @@ span.string {
<li><a href="#pdfio_stream_t">pdfio_stream_t</a></li> <li><a href="#pdfio_stream_t">pdfio_stream_t</a></li>
<li><a href="#pdfio_textrendering_t">pdfio_textrendering_t</a></li> <li><a href="#pdfio_textrendering_t">pdfio_textrendering_t</a></li>
<li><a href="#pdfio_valtype_t">pdfio_valtype_t</a></li> <li><a href="#pdfio_valtype_t">pdfio_valtype_t</a></li>
<li><a href="#state_t[4][4]">state_t[4][4]</a></li>
</ul></li> </ul></li>
<li><a href="#STRUCTURES">Structures</a><ul class="subcontents"> <li><a href="#STRUCTURES">Structures</a><ul class="subcontents">
<li><a href="#pdfio_rect_s">pdfio_rect_s</a></li> <li><a href="#pdfio_rect_s">pdfio_rect_s</a></li>
@ -491,7 +501,7 @@ span.string {
</li> </li>
</ul> </ul>
<p>PDFio is <em>not</em> concerned with rendering or viewing a PDF file, although a PDF RIP or viewer could be written using it.</p> <p>PDFio is <em>not</em> concerned with rendering or viewing a PDF file, although a PDF RIP or viewer could be written using it.</p>
<p>PDFio is Copyright © 2021 by Michael R Sweet and is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software. See the files &quot;LICENSE&quot; and &quot;NOTICE&quot; for more information.</p> <p>PDFio is Copyright © 2021-2023 by Michael R Sweet and is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2 software. See the files &quot;LICENSE&quot; and &quot;NOTICE&quot; for more information.</p>
<h3 class="title" id="requirements">Requirements</h3> <h3 class="title" id="requirements">Requirements</h3>
<p>PDFio requires the following to build the software:</p> <p>PDFio requires the following to build the software:</p>
<ul> <ul>
@ -499,66 +509,40 @@ span.string {
</li> </li>
<li><p>A POSIX-compliant <code>make</code> program</p> <li><p>A POSIX-compliant <code>make</code> program</p>
</li> </li>
<li><p>A POSIX-compliant <code>sh</code> program</p>
</li>
<li><p>ZLIB (<a href="https://www.zlib.net">https://www.zlib.net</a>) 1.0 or higher</p> <li><p>ZLIB (<a href="https://www.zlib.net">https://www.zlib.net</a>) 1.0 or higher</p>
</li> </li>
</ul> </ul>
<p>IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.</p> <p>IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.</p>
<h3 class="title" id="installing-pdfio">Installing pdfio</h3> <h3 class="title" id="installing-pdfio">Installing pdfio</h3>
<p>PDFio comes with a portable makefile that will work on any POSIX-compliant system with ZLIB installed. To make it, run:</p> <p>PDFio comes with a configure script that creates a portable makefile that will work on any POSIX-compliant system with ZLIB installed. To make it, run:</p>
<pre><code>make all <pre><code>./configure
make
</code></pre> </code></pre>
<p>To test it, run:</p> <p>To test it, run:</p>
<pre><code>make test <pre><code>make test
</code></pre> </code></pre>
<p>To install it, run:</p> <p>To install it, run:</p>
<pre><code>make install <pre><code>sudo make install
</code></pre> </code></pre>
<p>If you want a shared library, run:</p> <p>If you want a shared library, run:</p>
<pre><code>make all-shared <pre><code>./configure --enable-shared
make install-shared make
sudo make install
</code></pre> </code></pre>
<p>The default installation location is &quot;/usr/local&quot;. Pass the <code>prefix</code> variable to make to install it to another location:</p> <p>The default installation location is &quot;/usr/local&quot;. Pass the <code>--prefix</code> option to make to install it to another location:</p>
<pre><code>make install prefix=/some/other/directory <pre><code>./configure --prefix=/some/other/directory
</code></pre>
<p>Other configure options can be found using the <code>--help</code> option:</p>
<pre><code>./configure --help
</code></pre> </code></pre>
<p>The makefile installs the pdfio header to &quot;${prefix}/include&quot;, the library to &quot;${prefix}/lib&quot;, the <code>pkg-config</code> file to &quot;${prefix}/lib/pkgconfig&quot;, the man page to &quot;${prefix}/share/man/man3&quot;, and the documentation to &quot;${prefix}/share/doc/pdfio&quot;.</p>
<p>The makefile supports the following variables that can be specified in the make command or as environment variables:</p>
<ul>
<li><p><code>AR</code>: the library archiver (default &quot;ar&quot;)</p>
</li>
<li><p><code>ARFLAGS</code>: options for the library archiver (default &quot;cr&quot;)</p>
</li>
<li><p><code>CC</code>: the C compiler (default &quot;cc&quot;)</p>
</li>
<li><p><code>CFLAGS</code>: options for the C compiler (default &quot;&quot;)</p>
</li>
<li><p><code>CODESIGN_IDENTITY</code>: the identity to use when code signing the shared library on macOS (default &quot;Developer ID&quot;)</p>
</li>
<li><p><code>COMMONFLAGS</code>: options for the C compiler and linker (typically architecture and optimization options, default is &quot;-Os -g&quot;)</p>
</li>
<li><p><code>CPPFLAGS</code>: options for the C preprocessor (default &quot;&quot;)</p>
</li>
<li><p><code>DESTDIR</code> and <code>DSTROOT</code>: specifies a root directory when installing (default is &quot;&quot;, specify only one)</p>
</li>
<li><p><code>DSOFLAGS</code>: options for the C compiler when linking the shared library (default &quot;&quot;)</p>
</li>
<li><p><code>LDFLAGS</code>: options for the C compiler when linking the test programs (default &quot;&quot;)</p>
</li>
<li><p><code>LIBS</code>: library options when linking the test programs (default &quot;-lz&quot;)</p>
</li>
<li><p><code>RANLIB</code>: program that generates a table-of-contents in a library (default &quot;ranlib&quot;)</p>
</li>
<li><p><code>prefix</code>: specifies the installation directory (default &quot;/usr/local&quot;)</p>
</li>
</ul>
<h3 class="title" id="visual-studio-project">Visual Studio Project</h3> <h3 class="title" id="visual-studio-project">Visual Studio Project</h3>
<p>The Visual Studio solution (&quot;pdfio.sln&quot;) is provided for Windows developers and generates both a static library and DLL.</p> <p>The Visual Studio solution (&quot;pdfio.sln&quot;) is provided for Windows developers and generates both a static library and DLL.</p>
<h3 class="title" id="xcode-project">Xcode Project</h3> <h3 class="title" id="xcode-project">Xcode Project</h3>
<p>There is also an Xcode project (&quot;pdfio.xcodeproj&quot;) you can use on macOS which generates a static library that will be installed under &quot;/usr/local&quot; with:</p> <p>There is also an Xcode project (&quot;pdfio.xcodeproj&quot;) you can use on macOS which generates a static library that will be installed under &quot;/usr/local&quot; with:</p>
<pre><code>sudo xcodebuild install <pre><code>sudo xcodebuild install
</code></pre> </code></pre>
<p>You can reproduce this with the makefile using:</p>
<pre><code>sudo make COMMONFLAGS=&quot;-Os -mmacosx-version-min=10.14 -arch x86_64 -arch arm64&quot; install
</code></pre>
<h3 class="title" id="detecting-pdfio">Detecting PDFio</h3> <h3 class="title" id="detecting-pdfio">Detecting PDFio</h3>
<p>PDFio can be detected using the <code>pkg-config</code> command, for example:</p> <p>PDFio can be detected using the <code>pkg-config</code> command, for example:</p>
<pre><code>if pkg-config --exists pdfio; then <pre><code>if pkg-config --exists pdfio; then
@ -593,9 +577,21 @@ LIBS += `pkg-config --libs pdfio`
</ul> </ul>
<h3 class="title" id="reading-pdf-files">Reading PDF Files</h3> <h3 class="title" id="reading-pdf-files">Reading PDF Files</h3>
<p>You open an existing PDF file using the <a href="#pdfioFileOpen"><code>pdfioFileOpen</code></a> function:</p> <p>You open an existing PDF file using the <a href="#pdfioFileOpen"><code>pdfioFileOpen</code></a> function:</p>
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileOpen(<span class="string">&quot;myinputfile.pdf&quot;</span>, error_cb, error_data); <pre><code class="language-c">pdfio_file_t *pdf = pdfioFileOpen(<span class="string">&quot;myinputfile.pdf&quot;</span>, password_cb, password_data,
error_cb, error_data);
</code></pre> </code></pre>
<p>where the three arguments to the function are the filename (&quot;myinputfile.pdf&quot;), an optional error callback function (<code>error_cb</code>), and an optional pointer value for the error callback function (<code>error_data</code>). The error callback is called for both errors and warnings and accepts the <code>pdfio_file_t</code> pointer, a message string, and the callback pointer value, for example:</p> <p>where the five arguments to the function are the filename (&quot;myinputfile.pdf&quot;), an optional password callback function (<code>password_cb</code>) and data pointer value (<code>password_data</code>), and an optional error callback function (<code>error_cb</code>) and data pointer value (<code>error_data</code>). The password callback is called for encrypted PDF files that are not using the default password, for example:</p>
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
password_cb(<span class="reserved">void</span> *data, <span class="reserved">const</span> <span class="reserved">char</span> *filename)
{
(<span class="reserved">void</span>)data; <span class="comment">// This callback doesn't use the data pointer</span>
(<span class="reserved">void</span>)filename; <span class="comment">// This callback doesn't use the filename</span>
<span class="comment">// Return a password string for the file...</span>
<span class="reserved">return</span> (<span class="string">&quot;Password42&quot;</span>);
}
</code></pre>
<p>The error callback is called for both errors and warnings and accepts the <code>pdfio_file_t</code> pointer, a message string, and the callback pointer value, for example:</p>
<pre><code class="language-c"><span class="reserved">bool</span> <pre><code class="language-c"><span class="reserved">bool</span>
error_cb(pdfio_file_t *pdf, <span class="reserved">const</span> <span class="reserved">char</span> *message, <span class="reserved">void</span> *data) error_cb(pdfio_file_t *pdf, <span class="reserved">const</span> <span class="reserved">char</span> *message, <span class="reserved">void</span> *data)
{ {
@ -621,7 +617,7 @@ pdfio_obj_t *page; <span class="comment">// Current page</span>
<span class="comment">// do something with page</span> <span class="comment">// do something with page</span>
} }
</code></pre> </code></pre>
<p>Each page is represented by a &quot;page tree&quot; object (what <a href="#pdfioFileGetPage"><code>pdfioFileGetPage</code></a> returns) that specifies information about the page and one or more &quot;content&quot; objects that contain the images, fonts, text, and graphics that appear on the page.</p> <p>Each page is represented by a &quot;page tree&quot; object (what <a href="#pdfioFileGetPage"><code>pdfioFileGetPage</code></a> returns) that specifies information about the page and one or more &quot;content&quot; objects that contain the images, fonts, text, and graphics that appear on the page. Use the <a href="#pdfioPageGetNumStreams"><code>pdfioPageGetNumStreams</code></a> and <a href="#pdfioPageOpenStream"><code>pdfioPageOpenStream</code></a> functions to access the content streams for each page.</p>
<p>The <a href="#pdfioFileClose"><code>pdfioFileClose</code></a> function closes a PDF file and frees all memory that was used for it:</p> <p>The <a href="#pdfioFileClose"><code>pdfioFileClose</code></a> function closes a PDF file and frees all memory that was used for it:</p>
<pre><code class="language-c">pdfioFileClose(pdf); <pre><code class="language-c">pdfioFileClose(pdf);
</code></pre> </code></pre>
@ -663,6 +659,11 @@ pdfio_obj_t *obj = pdfioFileFindObj(pdf, number);
pdfio_stream_t *st = pdfioObjOpenStream(obj, <span class="reserved">true</span>); pdfio_stream_t *st = pdfioObjOpenStream(obj, <span class="reserved">true</span>);
</code></pre> </code></pre>
<p>The first argument is the object pointer. The second argument is a boolean value that specifies whether you want to decode (typically decompress) the stream data or return it as-is.</p> <p>The first argument is the object pointer. The second argument is a boolean value that specifies whether you want to decode (typically decompress) the stream data or return it as-is.</p>
<p>When reading a page stream you'll use the <a href="#pdfioPageOpenStream"><code>pdfioPageOpenStream</code></a> function instead:</p>
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileOpen(...);
pdfio_obj_t *obj = pdfioFileGetPage(pdf, number);
pdfio_stream_t *st = pdfioPageOpenStream(obj, <span class="number">0</span>, <span class="reserved">true</span>);
</code></pre>
<p>Once you have the stream open, you can use one of several functions to read from it:</p> <p>Once you have the stream open, you can use one of several functions to read from it:</p>
<ul> <ul>
<li><p><a href="#pdfioStreamConsume"><code>pdfioStreamConsume</code></a> reads and discards a number of bytes in the stream</p> <li><p><a href="#pdfioStreamConsume"><code>pdfioStreamConsume</code></a> reads and discards a number of bytes in the stream</p>
@ -679,10 +680,16 @@ pdfio_stream_t *st = pdfioObjOpenStream(obj, <span class="reserved">true</span>)
</code></pre> </code></pre>
<p>To create a stream for a new object, call the <a href="#pdfioObjCreateStream"><code>pdfioObjCreateStream</code></a> function:</p> <p>To create a stream for a new object, call the <a href="#pdfioObjCreateStream"><code>pdfioObjCreateStream</code></a> function:</p>
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...); <pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_obj_t *pdfioFileCreateObj(pdf, ...); pdfio_obj_t *obj = pdfioFileCreateObj(pdf, ...);
pdfio_stream_t *pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE); pdfio_stream_t *st = pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE);
</code></pre> </code></pre>
<p>The first argument is the newly created object. The second argument is either <code>PDFIO_FILTER_NONE</code> to specify that any encoding is done by your program or <code>PDFIO_FILTER_FLATE</code> to specify that PDFio should Flate compress the stream.</p> <p>The first argument is the newly created object. The second argument is either <code>PDFIO_FILTER_NONE</code> to specify that any encoding is done by your program or <code>PDFIO_FILTER_FLATE</code> to specify that PDFio should Flate compress the stream.</p>
<p>To create a page content stream call the <a href="#pdfioFileCreatePage"><code>pdfioFileCreatePage</code></a> function:</p>
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_dict_t *dict = pdfioDictCreate(pdf);
... set page dictionary keys <span class="reserved">and</span> values ...
pdfio_stream_t *st = pdfioFileCreatePage(pdf, dict);
</code></pre>
<p>Once you have created the stream, use any of the following functions to write to the stream:</p> <p>Once you have created the stream, use any of the following functions to write to the stream:</p>
<ul> <ul>
<li><p><a href="#pdfioStreamPrintf"><code>pdfioStreamPrintf</code></a> writes a formatted string to the stream</p> <li><p><a href="#pdfioStreamPrintf"><code>pdfioStreamPrintf</code></a> writes a formatted string to the stream</p>
@ -697,7 +704,7 @@ pdfio_stream_t *pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE);
<p>The <a href="#pdf-content-helper-functions">PDF content helper functions</a> provide additional functions for writing specific PDF page stream commands.</p> <p>The <a href="#pdf-content-helper-functions">PDF content helper functions</a> provide additional functions for writing specific PDF page stream commands.</p>
<p>When you are done writing the stream, call <a href="#pdfioStreamCLose"><code>pdfioStreamCLose</code></a> to close both the stream and the object.</p> <p>When you are done writing the stream, call <a href="#pdfioStreamCLose"><code>pdfioStreamCLose</code></a> to close both the stream and the object.</p>
<h3 class="title" id="pdf-content-helper-functions">PDF Content Helper Functions</h3> <h3 class="title" id="pdf-content-helper-functions">PDF Content Helper Functions</h3>
<p>PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into ??? categories:</p> <p>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:</p>
<ul> <ul>
<li><p><a href="#color-space-functions">Color Space Functions</a></p> <li><p><a href="#color-space-functions">Color Space Functions</a></p>
</li> </li>
@ -783,7 +790,9 @@ pdfio_obj_t *arial = pdfioFileCreateFontObjFromFile(pdf, <span class="string">&q
pdfio_obj_t *arial = pdfioFileCreateFontObjFromFile(pdf, <span class="string">&quot;NotoSansJP-Regular.otf&quot;</span>, <span class="reserved">true</span>); pdfio_obj_t *arial = pdfioFileCreateFontObjFromFile(pdf, <span class="string">&quot;NotoSansJP-Regular.otf&quot;</span>, <span class="reserved">true</span>);
</code></pre> </code></pre>
<p>will embed the NotoSansJP Regular OpenType font with full support for Unicode.</p> <p>will embed the NotoSansJP Regular OpenType font with full support for Unicode.</p>
<blockquote>
<p>Note: Not all fonts support Unicode.</p> <p>Note: Not all fonts support Unicode.</p>
</blockquote>
<h4 id="image-object-functions">Image Object Functions</h4> <h4 id="image-object-functions">Image Object Functions</h4>
<p>PDF supports images with many different color spaces and bit depths with optional transparency. PDFio provides two helper functions for creating image objects that can be referenced in page streams. The first function is <a href="#pdfioFileCreateImageObjFromData"><code>pdfioFileCreateImageObjFromData</code></a> which creates an image object from data in memory, for example:</p> <p>PDF supports images with many different color spaces and bit depths with optional transparency. PDFio provides two helper functions for creating image objects that can be referenced in page streams. The first function is <a href="#pdfioFileCreateImageObjFromData"><code>pdfioFileCreateImageObjFromData</code></a> which creates an image object from data in memory, for example:</p>
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...); <pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...);
@ -914,7 +923,11 @@ pdfio_obj_t *img = pdfioFileCreateImageObjFromFile(pdf, <span class="string">&qu
</li> </li>
<li><p><a href="#pdfioContentTextMoveTo"><code>pdfioContentTextMoveTo</code></a> moves within the current line in a text block</p> <li><p><a href="#pdfioContentTextMoveTo"><code>pdfioContentTextMoveTo</code></a> moves within the current line in a text block</p>
</li> </li>
<li><p><a href="#pdfioContentTextNextLine"><code>pdfioContentTextNextLine</code></a> moves to the beginning of the next line in a text block</p> <li><p><a href="#pdfioContentTextNewLine"><code>pdfioContentTextNewLine</code></a> moves to the beginning of the next line in a text block</p>
</li>
<li><p><a href="#pdfioContentTextNewLineShow"><code>pdfioContentTextNewLineShow</code></a> moves to the beginning of the next line in a text block and shows literal text with optional word and character spacing</p>
</li>
<li><p><a href="#pdfioContentTextNewLineShowf"><code>pdfioContentTextNewLineShowf</code></a> moves to the beginning of the next line in a text block and shows formatted text with optional word and character spacing</p>
</li> </li>
<li><p><a href="#pdfioContentTextShow"><code>pdfioContentTextShow</code></a> draws a literal string in a text block</p> <li><p><a href="#pdfioContentTextShow"><code>pdfioContentTextShow</code></a> draws a literal string in a text block</p>
</li> </li>
@ -1371,7 +1384,7 @@ bool pdfioContentFillAndStroke(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st,
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentMatrixConcat">pdfioContentMatrixConcat</a></h3> <h3 class="function"><a id="pdfioContentMatrixConcat">pdfioContentMatrixConcat</a></h3>
<p class="description">Concatenate a matrix to the current graphics <p class="description">Concatenate a matrix to the current graphics
state.</p> state.</p>
<p class="code"> <p class="code">
bool pdfioContentMatrixConcat(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, pdfio_matrix_t m);</p> bool pdfioContentMatrixConcat(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, pdfio_matrix_t m);</p>
<h4 class="parameters">Parameters</h4> <h4 class="parameters">Parameters</h4>
@ -1498,6 +1511,17 @@ bool pdfioContentPathCurve23(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, d
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentPathEnd">pdfioContentPathEnd</a></h3>
<p class="description">Clear the current path.</p>
<p class="code">
bool pdfioContentPathEnd(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>st</th>
<td class="description">Stream</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentPathLineTo">pdfioContentPathLineTo</a></h3> <h3 class="function"><a id="pdfioContentPathLineTo">pdfioContentPathLineTo</a></h3>
<p class="description">Add a straight line to the current path.</p> <p class="description">Add a straight line to the current path.</p>
<p class="code"> <p class="code">
@ -1586,6 +1610,9 @@ bool pdfioContentSetDashPattern(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function sets the stroke pattern when drawing lines. If &quot;on&quot; and &quot;off&quot;
are 0, a solid line is drawn.</p>
<h3 class="function"><a id="pdfioContentSetFillColorDeviceCMYK">pdfioContentSetFillColorDeviceCMYK</a></h3> <h3 class="function"><a id="pdfioContentSetFillColorDeviceCMYK">pdfioContentSetFillColorDeviceCMYK</a></h3>
<p class="description">Set device CMYK fill color.</p> <p class="description">Set device CMYK fill color.</p>
<p class="code"> <p class="code">
@ -1974,6 +2001,25 @@ bool pdfioContentTextEnd(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);</p>
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentTextMeasure">pdfioContentTextMeasure</a></h3>
<p class="description">Measure a text string and return its width.</p>
<p class="code">
double pdfioContentTextMeasure(<a href="#pdfio_obj_t">pdfio_obj_t</a> *font, const char *s, double size);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>font</th>
<td class="description">Font object created by <a href="#pdfioFileCreateFontObjFromFile"><code>pdfioFileCreateFontObjFromFile</code></a></td></tr>
<tr><th>s</th>
<td class="description">UTF-8 string</td></tr>
<tr><th>size</th>
<td class="description">Font size/height</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">Width</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function measures the given text string &quot;s&quot; and returns its width based
on &quot;size&quot;. The text string must always use the UTF-8 (Unicode) encoding but
any control characters (such as newlines) are ignored.</p>
<h3 class="function"><a id="pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></h3> <h3 class="function"><a id="pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></h3>
<p class="description">Move to the next line and offset.</p> <p class="description">Move to the next line and offset.</p>
<p class="code"> <p class="code">
@ -2004,10 +2050,10 @@ bool pdfioContentTextMoveTo(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, do
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentTextNextLine">pdfioContentTextNextLine</a></h3> <h3 class="function"><a id="pdfioContentTextNewLine">pdfioContentTextNewLine</a></h3>
<p class="description">Move to the next line.</p> <p class="description">Move to the next line.</p>
<p class="code"> <p class="code">
bool pdfioContentTextNextLine(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);</p> bool pdfioContentTextNewLine(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);</p>
<h4 class="parameters">Parameters</h4> <h4 class="parameters">Parameters</h4>
<table class="list"><tbody> <table class="list"><tbody>
<tr><th>st</th> <tr><th>st</th>
@ -2015,6 +2061,56 @@ bool pdfioContentTextNextLine(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentTextNewLineShow">pdfioContentTextNewLineShow</a></h3>
<p class="description">Move to the next line and show text.</p>
<p class="code">
bool pdfioContentTextNewLineShow(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, double ws, double cs, bool unicode, const char *s);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>st</th>
<td class="description">Stream</td></tr>
<tr><th>ws</th>
<td class="description">Word spacing or <code>0.0</code> for none</td></tr>
<tr><th>cs</th>
<td class="description">Character spacing or <code>0.0</code> for none</td></tr>
<tr><th>unicode</th>
<td class="description">Unicode text?</td></tr>
<tr><th>s</th>
<td class="description">String to show</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function moves to the next line and then shows some text with optional
word and character spacing in a PDF content stream. The &quot;unicode&quot; argument
specifies that the current font maps to full Unicode. The &quot;s&quot; argument
specifies a UTF-8 encoded string.</p>
<h3 class="function"><a id="pdfioContentTextNewLineShowf">pdfioContentTextNewLineShowf</a></h3>
<p class="description">Show formatted text.</p>
<p class="code">
bool pdfioContentTextNewLineShowf(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, double ws, double cs, bool unicode, const char *format, ...);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>st</th>
<td class="description">Stream</td></tr>
<tr><th>ws</th>
<td class="description">Word spacing or <code>0.0</code> for none</td></tr>
<tr><th>cs</th>
<td class="description">Character spacing or <code>0.0</code> for none</td></tr>
<tr><th>unicode</th>
<td class="description">Unicode text?</td></tr>
<tr><th>format</th>
<td class="description"><code>printf</code>-style format string</td></tr>
<tr><th>...</th>
<td class="description">Additional arguments as needed</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function moves to the next line and shows some formatted text with
optional word and character spacing in a PDF content stream. The &quot;unicode&quot;
argument specifies that the current font maps to full Unicode. The &quot;format&quot;
argument specifies a UTF-8 encoded <code>printf</code>-style format string.</p>
<h3 class="function"><a id="pdfioContentTextShow">pdfioContentTextShow</a></h3> <h3 class="function"><a id="pdfioContentTextShow">pdfioContentTextShow</a></h3>
<p class="description">Show text.</p> <p class="description">Show text.</p>
<p class="code"> <p class="code">
@ -2074,9 +2170,9 @@ bool pdfioContentTextShowf(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, boo
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description">Show formatted text.</p> <p class="description">Show formatted text.</p>
<p class="discussion">This function shows some text in a PDF content stream. The &quot;unicode&quot; argument <p class="discussion">This function shows some formatted text in a PDF content stream. The
specifies that the current font maps to full Unicode. The &quot;format&quot; argument &quot;unicode&quot; argument specifies that the current font maps to full Unicode.
specifies a UTF-8 encoded <code>printf</code>-style format string.</p> The &quot;format&quot; argument specifies a UTF-8 encoded <code>printf</code>-style format string.</p>
<h3 class="function"><a id="pdfioDictCopy">pdfioDictCopy</a></h3> <h3 class="function"><a id="pdfioDictCopy">pdfioDictCopy</a></h3>
<p class="description">Copy a dictionary to a PDF file.</p> <p class="description">Copy a dictionary to a PDF file.</p>
<p class="code"> <p class="code">
@ -2248,6 +2344,34 @@ const char *pdfioDictGetString(<a href="#pdfio_dict_t">pdfio_dict_t</a> *dict, c
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description">Value type</p> <p class="description">Value type</p>
<h3 class="function"><a id="pdfioDictIterateKeys">pdfioDictIterateKeys</a></h3>
<p class="description">Iterate the keys in a dictionary.</p>
<p class="code">
void pdfioDictIterateKeys(<a href="#pdfio_dict_t">pdfio_dict_t</a> *dict, <a href="#pdfio_dict_cb_t">pdfio_dict_cb_t</a> cb, void *cb_data);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>dict</th>
<td class="description">Dictionary</td></tr>
<tr><th>cb</th>
<td class="description">Callback function</td></tr>
<tr><th>cb_data</th>
<td class="description">Callback data</td></tr>
</tbody></table>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function iterates the keys in a dictionary, calling the supplied
function &quot;cb&quot;:
<pre>
bool
my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data)
{
... &quot;key&quot; contains the dictionary key ...
... return true to continue or false to stop ...
}
</pre>
The iteration continues as long as the callback returns <code>true</code> or all keys
have been iterated.</p>
<h3 class="function"><a id="pdfioDictSetArray">pdfioDictSetArray</a></h3> <h3 class="function"><a id="pdfioDictSetArray">pdfioDictSetArray</a></h3>
<p class="description">Set a key array in a dictionary.</p> <p class="description">Set a key array in a dictionary.</p>
<p class="code"> <p class="code">
@ -2640,6 +2764,22 @@ image on the page.<br>
Note: Currently PNG support is limited to grayscale, RGB, or indexed files Note: Currently PNG support is limited to grayscale, RGB, or indexed files
without interlacing or alpha. Transparency (masking) based on color/index without interlacing or alpha. Transparency (masking) based on color/index
is supported.</blockquote> is supported.</blockquote>
<h3 class="function"><a id="pdfioFileCreateNumberObj">pdfioFileCreateNumberObj</a></h3>
<p class="description">Create a new object in a PDF file containing a number.</p>
<p class="code">
<a href="#pdfio_obj_t">pdfio_obj_t</a> *pdfioFileCreateNumberObj(<a href="#pdfio_file_t">pdfio_file_t</a> *pdf, double number);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>pdf</th>
<td class="description">PDF file</td></tr>
<tr><th>number</th>
<td class="description">Number value</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">New object</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function creates a new object with a number value in a PDF file.
You must call <a href="#pdfioObjClose"><code>pdfioObjClose</code></a> to write the object to the file.</p>
<h3 class="function"><a id="pdfioFileCreateObj">pdfioFileCreateObj</a></h3> <h3 class="function"><a id="pdfioFileCreateObj">pdfioFileCreateObj</a></h3>
<p class="description">Create a new object in a PDF file.</p> <p class="description">Create a new object in a PDF file.</p>
<p class="code"> <p class="code">
@ -2686,9 +2826,10 @@ written:
ssize_t ssize_t
output_cb(void *output_ctx, const void *buffer, size_t bytes) output_cb(void *output_ctx, const void *buffer, size_t bytes)
{ {
// Write buffer to output and return the number of bytes written // Write buffer to output and return the number of bytes written
} }
</pre> </pre>
The &quot;version&quot; argument specifies the PDF version number for the file or The &quot;version&quot; argument specifies the PDF version number for the file or
<code>NULL</code> for the default (&quot;2.0&quot;).<br> <code>NULL</code> for the default (&quot;2.0&quot;).<br>
<br> <br>
@ -2717,6 +2858,51 @@ stored as indirect object references.</blockquote>
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description">Contents stream</p> <p class="description">Contents stream</p>
<h3 class="function"><a id="pdfioFileCreateStringObj">pdfioFileCreateStringObj</a></h3>
<p class="description">Create a new object in a PDF file containing a string.</p>
<p class="code">
<a href="#pdfio_obj_t">pdfio_obj_t</a> *pdfioFileCreateStringObj(<a href="#pdfio_file_t">pdfio_file_t</a> *pdf, const char *string);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>pdf</th>
<td class="description">PDF file</td></tr>
<tr><th>string</th>
<td class="description">String</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">New object</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function creates a new object with a string value in a PDF file.
You must call <a href="#pdfioObjClose"><code>pdfioObjClose</code></a> to write the object to the file.</p>
<h3 class="function"><a id="pdfioFileCreateTemporary">pdfioFileCreateTemporary</a></h3>
<p class="description"></p>
<p class="code">
<a href="#pdfio_file_t">pdfio_file_t</a> *pdfioFileCreateTemporary(char *buffer, size_t bufsize, const char *version, <a href="#pdfio_rect_t">pdfio_rect_t</a> *media_box, <a href="#pdfio_rect_t">pdfio_rect_t</a> *crop_box, <a href="#pdfio_error_cb_t">pdfio_error_cb_t</a> error_cb, void *error_data);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>buffer</th>
<td class="description">Filename buffer</td></tr>
<tr><th>bufsize</th>
<td class="description">Size of filename buffer</td></tr>
<tr><th>version</th>
<td class="description">PDF version number or <code>NULL</code> for default (2.0)</td></tr>
<tr><th>media_box</th>
<td class="description">Default MediaBox for pages</td></tr>
<tr><th>crop_box</th>
<td class="description">Default CropBox for pages</td></tr>
<tr><th>error_cb</th>
<td class="description">Error callback or <code>NULL</code> for default</td></tr>
<tr><th>error_data</th>
<td class="description">Error callback data, if any</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">Create a temporary PDF file.</p>
<p class="discussion">This function creates a PDF file with a unique filename in the current
temporary directory. The temporary file is stored in the string &quot;buffer&quot; an
will have a &quot;.pdf&quot; extension. Otherwise, this function works the same as
the <a href="#pdfioFileCreate"><code>pdfioFileCreate</code></a> function.
</p>
<h3 class="function"><a id="pdfioFileFindObj">pdfioFileFindObj</a></h3> <h3 class="function"><a id="pdfioFileFindObj">pdfioFileFindObj</a></h3>
<p class="description">Find an object using its object number.</p> <p class="description">Find an object using its object number.</p>
<p class="code"> <p class="code">
@ -3068,7 +3254,7 @@ double pdfioImageGetWidth(<a href="#pdfio_obj_t">pdfio_obj_t</a> *obj);</p>
<p class="description">Width in columns</p> <p class="description">Width in columns</p>
<h3 class="function"><a id="pdfioObjClose">pdfioObjClose</a></h3> <h3 class="function"><a id="pdfioObjClose">pdfioObjClose</a></h3>
<p class="description">Close an object, writing any data as needed to the PDF <p class="description">Close an object, writing any data as needed to the PDF
file.</p> file.</p>
<p class="code"> <p class="code">
bool pdfioObjClose(<a href="#pdfio_obj_t">pdfio_obj_t</a> *obj);</p> bool pdfioObjClose(<a href="#pdfio_obj_t">pdfio_obj_t</a> *obj);</p>
<h4 class="parameters">Parameters</h4> <h4 class="parameters">Parameters</h4>
@ -3264,6 +3450,32 @@ bool pdfioPageDictAddImage(<a href="#pdfio_dict_t">pdfio_dict_t</a> *dict, const
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioPageGetNumStreams">pdfioPageGetNumStreams</a></h3>
<p class="description">Get the number of content streams for a page object.</p>
<p class="code">
size_t pdfioPageGetNumStreams(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>page</th>
<td class="description">Page object</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">Number of streams</p>
<h3 class="function"><a id="pdfioPageOpenStream">pdfioPageOpenStream</a></h3>
<p class="description">Open a content stream for a page.</p>
<p class="code">
<a href="#pdfio_stream_t">pdfio_stream_t</a> *pdfioPageOpenStream(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, size_t n, bool decode);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>page</th>
<td class="description">Page object</td></tr>
<tr><th>n</th>
<td class="description">Stream index (0-based)</td></tr>
<tr><th>decode</th>
<td class="description"><code>true</code> to decode/decompress stream</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">Stream</p>
<h3 class="function"><a id="pdfioStreamClose">pdfioStreamClose</a></h3> <h3 class="function"><a id="pdfioStreamClose">pdfioStreamClose</a></h3>
<p class="description">Close a (data) stream in a PDF file.</p> <p class="description">Close a (data) stream in a PDF file.</p>
<p class="code"> <p class="code">
@ -3303,6 +3515,13 @@ bool pdfioStreamGetToken(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st, char
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on EOF</p> <p class="description"><code>true</code> on success, <code>false</code> on EOF</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function reads a single PDF token from a stream. Operator tokens,
boolean values, and numbers are returned as-is in the provided string buffer.
String values start with the opening parenthesis ('(') but have all escaping
resolved and the terminating parenthesis removed. Hexadecimal string values
start with the opening angle bracket ('&lt;') and have all whitespace and the
terminating angle bracket removed.</p>
<h3 class="function"><a id="pdfioStreamPeek">pdfioStreamPeek</a></h3> <h3 class="function"><a id="pdfioStreamPeek">pdfioStreamPeek</a></h3>
<p class="description">Peek at data in a stream.</p> <p class="description">Peek at data in a stream.</p>
<p class="code"> <p class="code">
@ -3445,6 +3664,11 @@ typedef struct _pdfio_array_s pdfio_array_t;
<p class="code"> <p class="code">
typedef enum <a href="#pdfio_cs_e">pdfio_cs_e</a> pdfio_cs_t; typedef enum <a href="#pdfio_cs_e">pdfio_cs_e</a> pdfio_cs_t;
</p> </p>
<h3 class="typedef"><a id="pdfio_dict_cb_t">pdfio_dict_cb_t</a></h3>
<p class="description">Dictionary iterator callback</p>
<p class="code">
typedef bool (*pdfio_dict_cb_t)(<a href="#pdfio_dict_t">pdfio_dict_t</a> *dict, const char *key, void *cb_data);
</p>
<h3 class="typedef"><a id="pdfio_dict_t">pdfio_dict_t</a></h3> <h3 class="typedef"><a id="pdfio_dict_t">pdfio_dict_t</a></h3>
<p class="description">Key/value dictionary</p> <p class="description">Key/value dictionary</p>
<p class="code"> <p class="code">
@ -3525,11 +3749,6 @@ typedef enum <a href="#pdfio_textrendering_e">pdfio_textrendering_e</a> pdfio_te
<p class="code"> <p class="code">
typedef enum <a href="#pdfio_valtype_e">pdfio_valtype_e</a> pdfio_valtype_t; typedef enum <a href="#pdfio_valtype_e">pdfio_valtype_e</a> pdfio_valtype_t;
</p> </p>
<h3 class="typedef"><a id="state_t[4][4]">state_t[4][4]</a></h3>
<p class="description">4x4 AES state table</p>
<p class="code">
typedef uint8_t state_t[4][4];
</p>
<h2 class="title"><a id="STRUCTURES">Structures</a></h2> <h2 class="title"><a id="STRUCTURES">Structures</a></h2>
<h3 class="struct"><a id="pdfio_rect_s">pdfio_rect_s</a></h3> <h3 class="struct"><a id="pdfio_rect_s">pdfio_rect_s</a></h3>
<p class="description">PDF rectangle</p> <p class="description">PDF rectangle</p>
@ -3564,7 +3783,6 @@ typedef uint8_t state_t[4][4];
<h4 class="constants">Constants</h4> <h4 class="constants">Constants</h4>
<table class="list"><tbody> <table class="list"><tbody>
<tr><th>PDFIO_ENCRYPTION_AES_128 </th><td class="description">128-bit AES encryption (PDF 1.6)</td></tr> <tr><th>PDFIO_ENCRYPTION_AES_128 </th><td class="description">128-bit AES encryption (PDF 1.6)</td></tr>
<tr><th>PDFIO_ENCRYPTION_AES_256 </th><td class="description">256-bit AES encryption (PDF 2.0)</td></tr>
<tr><th>PDFIO_ENCRYPTION_NONE </th><td class="description">No encryption</td></tr> <tr><th>PDFIO_ENCRYPTION_NONE </th><td class="description">No encryption</td></tr>
<tr><th>PDFIO_ENCRYPTION_RC4_128 </th><td class="description">128-bit RC4 encryption (PDF 1.4)</td></tr> <tr><th>PDFIO_ENCRYPTION_RC4_128 </th><td class="description">128-bit RC4 encryption (PDF 1.4)</td></tr>
<tr><th>PDFIO_ENCRYPTION_RC4_40 </th><td class="description">40-bit RC4 encryption (PDF 1.3)</td></tr> <tr><th>PDFIO_ENCRYPTION_RC4_40 </th><td class="description">40-bit RC4 encryption (PDF 1.3)</td></tr>

View File

@ -15,8 +15,8 @@ goals of pdfio are:
PDFio is *not* concerned with rendering or viewing a PDF file, although a PDF PDFio is *not* concerned with rendering or viewing a PDF file, although a PDF
RIP or viewer could be written using it. RIP or viewer could be written using it.
PDFio is Copyright © 2021 by Michael R Sweet and is licensed under the Apache PDFio is Copyright © 2021-2023 by Michael R Sweet and is licensed under the
License Version 2.0 with an (optional) exception to allow linking against Apache License Version 2.0 with an (optional) exception to allow linking against
GPL2/LGPL2 software. See the files "LICENSE" and "NOTICE" for more information. GPL2/LGPL2 software. See the files "LICENSE" and "NOTICE" for more information.
@ -27,6 +27,7 @@ PDFio requires the following to build the software:
- A C99 compiler such as Clang, GCC, or MS Visual C - A C99 compiler such as Clang, GCC, or MS Visual C
- A POSIX-compliant `make` program - A POSIX-compliant `make` program
- A POSIX-compliant `sh` program
- ZLIB (<https://www.zlib.net>) 1.0 or higher - ZLIB (<https://www.zlib.net>) 1.0 or higher
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided. IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
@ -35,10 +36,11 @@ IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
Installing pdfio Installing pdfio
---------------- ----------------
PDFio comes with a portable makefile that will work on any POSIX-compliant PDFio comes with a configure script that creates a portable makefile that will
system with ZLIB installed. To make it, run: work on any POSIX-compliant system with ZLIB installed. To make it, run:
make all ./configure
make
To test it, run: To test it, run:
@ -46,45 +48,22 @@ To test it, run:
To install it, run: To install it, run:
make install sudo make install
If you want a shared library, run: If you want a shared library, run:
make all-shared ./configure --enable-shared
make install-shared make
sudo make install
The default installation location is "/usr/local". Pass the `prefix` variable The default installation location is "/usr/local". Pass the `--prefix` option
to make to install it to another location: to make to install it to another location:
make install prefix=/some/other/directory ./configure --prefix=/some/other/directory
The makefile installs the pdfio header to "${prefix}/include", the library to Other configure options can be found using the `--help` option:
"${prefix}/lib", the `pkg-config` file to "${prefix}/lib/pkgconfig", the man
page to "${prefix}/share/man/man3", and the documentation to
"${prefix}/share/doc/pdfio".
The makefile supports the following variables that can be specified in the make ./configure --help
command or as environment variables:
- `AR`: the library archiver (default "ar")
- `ARFLAGS`: options for the library archiver (default "cr")
- `CC`: the C compiler (default "cc")
- `CFLAGS`: options for the C compiler (default "")
- `CODESIGN_IDENTITY`: the identity to use when code signing the shared library
on macOS (default "Developer ID")
- `COMMONFLAGS`: options for the C compiler and linker (typically architecture
and optimization options, default is "-Os -g")
- `CPPFLAGS`: options for the C preprocessor (default "")
- `DESTDIR` and `DSTROOT`: specifies a root directory when installing
(default is "", specify only one)
- `DSOFLAGS`: options for the C compiler when linking the shared library
(default "")
- `LDFLAGS`: options for the C compiler when linking the test programs
(default "")
- `LIBS`: library options when linking the test programs (default "-lz")
- `RANLIB`: program that generates a table-of-contents in a library
(default "ranlib")
- `prefix`: specifies the installation directory (default "/usr/local")
Visual Studio Project Visual Studio Project
@ -102,10 +81,6 @@ generates a static library that will be installed under "/usr/local" with:
sudo xcodebuild install sudo xcodebuild install
You can reproduce this with the makefile using:
sudo make COMMONFLAGS="-Os -mmacosx-version-min=10.14 -arch x86_64 -arch arm64" install
Detecting PDFio Detecting PDFio
--------------- ---------------
@ -163,15 +138,32 @@ Reading PDF Files
You open an existing PDF file using the [`pdfioFileOpen`](@@) function: You open an existing PDF file using the [`pdfioFileOpen`](@@) function:
```c ```c
pdfio_file_t *pdf = pdfioFileOpen("myinputfile.pdf", error_cb, error_data); pdfio_file_t *pdf = pdfioFileOpen("myinputfile.pdf", password_cb, password_data,
error_cb, error_data);
``` ```
where the three arguments to the function are the filename ("myinputfile.pdf"), where the five arguments to the function are the filename ("myinputfile.pdf"),
an optional error callback function (`error_cb`), and an optional pointer value an optional password callback function (`password_cb`) and data pointer value
for the error callback function (`error_data`). The error callback is called (`password_data`), and an optional error callback function (`error_cb`) and data
for both errors and warnings and accepts the `pdfio_file_t` pointer, a message pointer value (`error_data`). The password callback is called for encrypted PDF
string, and the callback pointer value, for example: files that are not using the default password, for example:
```c
const char *
password_cb(void *data, const char *filename)
{
(void)data; // This callback doesn't use the data pointer
(void)filename; // This callback doesn't use the filename
// Return a password string for the file...
return ("Password42");
}
```
The error callback is called for both errors and warnings and accepts the
`pdfio_file_t` pointer, a message string, and the callback pointer value, for
example:
```c ```c
bool bool
@ -209,7 +201,8 @@ for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
Each page is represented by a "page tree" object (what [`pdfioFileGetPage`](@@) Each page is represented by a "page tree" object (what [`pdfioFileGetPage`](@@)
returns) that specifies information about the page and one or more "content" 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 objects that contain the images, fonts, text, and graphics that appear on the
page. page. Use the [`pdfioPageGetNumStreams`](@@) and [`pdfioPageOpenStream`](@@)
functions to access the content streams for each page.
The [`pdfioFileClose`](@@) function closes a PDF file and frees all memory that The [`pdfioFileClose`](@@) function closes a PDF file and frees all memory that
was used for it: was used for it:
@ -294,6 +287,15 @@ The first argument is the object pointer. The second argument is a boolean
value that specifies whether you want to decode (typically decompress) the value that specifies whether you want to decode (typically decompress) the
stream data or return it as-is. stream data or return it as-is.
When reading a page stream you'll use the [`pdfioPageOpenStream`](@@) function
instead:
```c
pdfio_file_t *pdf = pdfioFileOpen(...);
pdfio_obj_t *obj = pdfioFileGetPage(pdf, number);
pdfio_stream_t *st = pdfioPageOpenStream(obj, 0, true);
```
Once you have the stream open, you can use one of several functions to read Once you have the stream open, you can use one of several functions to read
from it: from it:
@ -315,14 +317,23 @@ function:
```c ```c
pdfio_file_t *pdf = pdfioFileCreate(...); pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_obj_t *pdfioFileCreateObj(pdf, ...); pdfio_obj_t *obj = pdfioFileCreateObj(pdf, ...);
pdfio_stream_t *pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE); pdfio_stream_t *st = pdfioObjCreateStream(obj, PDFIO_FILTER_FLATE);
``` ```
The first argument is the newly created object. The second argument is either The first argument is the newly created object. The second argument is either
`PDFIO_FILTER_NONE` to specify that any encoding is done by your program or `PDFIO_FILTER_NONE` to specify that any encoding is done by your program or
`PDFIO_FILTER_FLATE` to specify that PDFio should Flate compress the stream. `PDFIO_FILTER_FLATE` to specify that PDFio should Flate compress the stream.
To create a page content stream call the [`pdfioFileCreatePage`](@@) function:
```c
pdfio_file_t *pdf = pdfioFileCreate(...);
pdfio_dict_t *dict = pdfioDictCreate(pdf);
... set page dictionary keys and values ...
pdfio_stream_t *st = pdfioFileCreatePage(pdf, dict);
```
Once you have created the stream, use any of the following functions to write Once you have created the stream, use any of the following functions to write
to the stream: to the stream:
@ -342,7 +353,7 @@ PDF Content Helper Functions
---------------------------- ----------------------------
PDFio includes many helper functions for embedding or writing specific kinds of PDFio includes many helper functions for embedding or writing specific kinds of
content to a PDF file. These functions can be roughly grouped into ??? content to a PDF file. These functions can be roughly grouped into five
categories: categories:
- [Color Space Functions](@) - [Color Space Functions](@)
@ -436,7 +447,7 @@ pdfio_obj_t *arial = pdfioFileCreateFontObjFromFile(pdf, "NotoSansJP-Regular.otf
will embed the NotoSansJP Regular OpenType font with full support for Unicode. will embed the NotoSansJP Regular OpenType font with full support for Unicode.
Note: Not all fonts support Unicode. > Note: Not all fonts support Unicode.
### Image Object Functions ### Image Object Functions
@ -565,8 +576,12 @@ escaping, as needed:
- [`pdfioContentTextMoveLine`](@@) moves to the next line with an offset in a - [`pdfioContentTextMoveLine`](@@) moves to the next line with an offset in a
text block text block
- [`pdfioContentTextMoveTo`](@@) moves within the current line in a text block - [`pdfioContentTextMoveTo`](@@) moves within the current line in a text block
- [`pdfioContentTextNextLine`](@@) moves to the beginning of the next line in a - [`pdfioContentTextNewLine`](@@) moves to the beginning of the next line in a
text block text block
- [`pdfioContentTextNewLineShow`](@@) moves to the beginning of the next line in a
text block and shows literal text with optional word and character spacing
- [`pdfioContentTextNewLineShowf`](@@) moves to the beginning of the next line in a
text block and shows formatted text with optional word and character spacing
- [`pdfioContentTextShow`](@@) draws a literal string in a text block - [`pdfioContentTextShow`](@@) draws a literal string in a text block
- [`pdfioContentTextShowf`](@@) draws a formatted string in a text block - [`pdfioContentTextShowf`](@@) draws a formatted string in a text block
- [`pdfioContentTextShowJustified`](@@) draws an array of literal strings with - [`pdfioContentTextShowJustified`](@@) draws an array of literal strings with

232
install-sh Executable file
View File

@ -0,0 +1,232 @@
#!/bin/sh
#
# Install a program, script, or datafile.
#
# Copyright 2008-2012 by Apple Inc.
#
# This script is not compatible with BSD (or any other) install program, as it
# allows owner and group changes to fail with a warning and makes sure that the
# destination directory permissions are as specified - BSD install and the
# original X11 install script did not change permissions of existing
# directories. It also does not support the transform options since CUPS does
# not use them...
#
# Original script from X11R5 (mit/util/scripts/install.sh)
# Copyright 1991 by the Massachusetts Institute of Technology
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of M.I.T. not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission. M.I.T. makes no representations about the
# suitability of this software for any purpose. It is provided "as is"
# without express or implied warranty.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
# set DOITPROG to echo to test this script
# Don't use :- since 4.3BSD and earlier shells don't like it.
doit="${DOITPROG-}"
# Force umask to 022...
umask 022
# put in absolute paths if you don't have them in your path; or use env. vars.
mvprog="${MVPROG-mv}"
cpprog="${CPPROG-cp}"
chmodprog="${CHMODPROG-chmod}"
chownprog="${CHOWNPROG-chown}"
chgrpprog="${CHGRPPROG-chgrp}"
stripprog="${STRIPPROG-strip}"
rmprog="${RMPROG-rm}"
mkdirprog="${MKDIRPROG-mkdir}"
gzipprog="${GZIPPROG-gzip}"
transformbasename=""
transform_arg=""
instcmd="$mvprog"
chmodcmd="$chmodprog 0755"
chowncmd=""
chgrpcmd=""
stripcmd=""
rmcmd="$rmprog -f"
mvcmd="$mvprog"
src=""
dst=""
dir_arg=""
gzipcp() {
# gzipcp from to
$gzipprog -9 <"$1" >"$2"
}
while [ x"$1" != x ]; do
case $1 in
-c)
instcmd="$cpprog"
shift
continue
;;
-d)
dir_arg=true
shift
continue
;;
-m)
chmodcmd="$chmodprog $2"
shift
shift
continue
;;
-o)
chowncmd="$chownprog $2"
shift
shift
continue
;;
-g)
chgrpcmd="$chgrpprog $2"
shift
shift
continue
;;
-s)
stripcmd="$stripprog"
shift
continue
;;
-z)
instcmd="gzipcp"
shift
continue
;;
*)
if [ x"$src" = x ]; then
src="$1"
else
dst="$1"
fi
shift
continue
;;
esac
done
if [ x"$src" = x ]; then
echo "install-sh: No input file specified"
exit 1
fi
if [ x"$dir_arg" != x ]; then
dst="$src"
src=""
if [ -d "$dst" ]; then
instcmd=:
else
instcmd=$mkdirprog
fi
else
# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
# might cause directories to be created, which would be especially bad
# if $src (and thus $dsttmp) contains '*'.
if [ ! -f "$src" -a ! -d "$src" ]; then
echo "install: $src does not exist"
exit 1
fi
if [ x"$dst" = x ]; then
echo "install: No destination specified"
exit 1
fi
# If destination is a directory, append the input filename.
if [ -d "$dst" ]; then
dst="$dst/`basename $src`"
fi
fi
## this sed command emulates the dirname command
dstdir="`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`"
# Make sure that the destination directory exists.
# This part is taken from Noah Friedman's mkinstalldirs script
# Skip lots of stat calls in the usual case.
if [ ! -d "$dstdir" ]; then
defaultIFS='
'
IFS="${IFS-${defaultIFS}}"
oIFS="${IFS}"
# Some sh's can't handle IFS=/ for some reason.
IFS='%'
set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
IFS="${oIFS}"
pathcomp=''
while [ $# -ne 0 ] ; do
pathcomp="${pathcomp}${1}"
shift
if [ ! -d "${pathcomp}" ]; then $doit $mkdirprog "${pathcomp}"; fi
pathcomp="${pathcomp}/"
done
fi
if [ x"$dir_arg" != x ]; then
# Make a directory...
$doit $instcmd $dst || exit 1
# Allow chown/chgrp to fail, but log a warning
if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst || echo "warning: Unable to change owner of $dst!"; fi
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst || echo "warning: Unable to change group of $dst!"; fi
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst || exit 1; fi
else
# Install a file...
dstfile="`basename $dst`"
# Check the destination file - for libraries just use the "-x" option
# to strip...
case "$dstfile" in
*.a | *.dylib | *.sl | *.sl.* | *.so | *.so.*)
stripopt="-x"
;;
*)
stripopt=""
;;
esac
# Make a temp file name in the proper directory.
dsttmp="$dstdir/#inst.$$#"
# Move or copy the file name to the temp name
$doit $instcmd $src $dsttmp || exit 1
# Update permissions and strip as needed, then move to the final name.
# If the chmod, strip, rm, or mv commands fail, remove the installed
# file...
if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripopt "$dsttmp" || echo "warning: Unable to strip $dst!"; fi
if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp" || echo "warning: Unable to change owner of $dst!"; fi
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp" || echo "warning: Unable to change group of $dst!"; fi
trap "rm -f ${dsttmp}" 0 &&
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; fi &&
$doit $rmcmd -f "$dstdir/$dstfile" &&
$doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
fi
exit 0

View File

@ -10,10 +10,6 @@
// (<https://github.com/kokke/tiny-AES-c>) // (<https://github.com/kokke/tiny-AES-c>)
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -21,7 +17,7 @@
// Local types... // Local types...
// //
typedef uint8_t state_t[4][4]; // 4x4 AES state table typedef uint8_t state_t[4][4]; // 4x4 AES state table @private@
// //

View File

@ -7,10 +7,6 @@
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"

View File

@ -1,16 +1,12 @@
// //
// Common support functions for pdfio. // Common support functions for pdfio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -38,6 +34,8 @@ _pdfioFileConsume(pdfio_file_t *pdf, // I - PDF file
else if (_pdfioFileSeek(pdf, (off_t)bytes, SEEK_CUR) < 0) else if (_pdfioFileSeek(pdf, (off_t)bytes, SEEK_CUR) < 0)
return (false); return (false);
PDFIO_DEBUG("_pdfioFileConsume: pos=%ld\n", (long)(pdf->bufpos + pdf->bufptr - pdf->buffer));
return (true); return (true);
} }
@ -373,9 +371,9 @@ _pdfioFileSeek(pdfio_file_t *pdf, // I - PDF file
if (whence != SEEK_END && offset >= pdf->bufpos && offset < (pdf->bufpos + pdf->bufend - pdf->buffer)) if (whence != SEEK_END && offset >= pdf->bufpos && offset < (pdf->bufpos + pdf->bufend - pdf->buffer))
{ {
// Yes, seek within existing buffer... // Yes, seek within existing buffer...
pdf->bufptr = pdf->buffer + offset - pdf->bufpos; pdf->bufptr = pdf->buffer + (offset - pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: Seek within buffer, bufpos=%ld.\n", (long)pdf->bufpos); PDFIO_DEBUG("_pdfioFileSeek: Seek within buffer, bufpos=%ld.\n", (long)pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p, bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufend); PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p(<%02X%02X...>), bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufptr[0] & 255, pdf->bufptr[1] & 255, pdf->bufend);
return (offset); return (offset);
} }

View File

@ -1,16 +1,12 @@
// //
// Content helper functions for PDFio. // Content helper functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
#include "pdfio-content.h" #include "pdfio-content.h"
#include "ttf.h" #include "ttf.h"
@ -489,8 +485,11 @@ pdfioContentMatrixRotate(
pdfio_stream_t *st, // I - Stream pdfio_stream_t *st, // I - Stream
double degrees) // I - Rotation angle in degrees counter-clockwise double degrees) // I - Rotation angle in degrees counter-clockwise
{ {
double dcos = cos(degrees / M_PI); // Cosine double dcos = cos(M_PI * degrees / 180.0);
double dsin = sin(degrees / M_PI); // Sine // Cosine
double dsin = sin(M_PI * degrees / 180.0);
// Sine
return (pdfioStreamPrintf(st, "%g %g %g %g 0 0 cm\n", dcos, -dsin, dsin, dcos)); return (pdfioStreamPrintf(st, "%g %g %g %g 0 0 cm\n", dcos, -dsin, dsin, dcos));
} }
@ -586,6 +585,17 @@ pdfioContentPathCurve23(
} }
//
// 'pdfioContentPathEnd()' - Clear the current path.
//
bool // O - `true` on success, `false` on failure
pdfioContentPathEnd(pdfio_stream_t *st) // I - Stream
{
return (pdfioStreamPuts(st, "n\n"));
}
// //
// 'pdfioContentPathLineTo()' - Add a straight line to the current path. // 'pdfioContentPathLineTo()' - Add a straight line to the current path.
// //
@ -656,6 +666,9 @@ pdfioContentSave(pdfio_stream_t *st) // I - Stream
// //
// 'pdfioContentSetDashPattern()' - Set the stroke pattern. // 'pdfioContentSetDashPattern()' - Set the stroke pattern.
// //
// This function sets the stroke pattern when drawing lines. If "on" and "off"
// are 0, a solid line is drawn.
//
bool // O - `true` on success, `false` on failure bool // O - `true` on success, `false` on failure
pdfioContentSetDashPattern( pdfioContentSetDashPattern(
@ -664,7 +677,12 @@ pdfioContentSetDashPattern(
double on, // I - On length double on, // I - On length
double off) // I - Off length double off) // I - Off length
{ {
return (pdfioStreamPrintf(st, "[%g %g] %g d\n", on, off, phase)); if (on <= 0.0 && off <= 0.0)
return (pdfioStreamPrintf(st, "[] %g d\n", phase));
else if (fabs(on - off) < 0.001)
return (pdfioStreamPrintf(st, "[%g] %g d\n", on, phase));
else
return (pdfioStreamPrintf(st, "[%g %g] %g d\n", on, off, phase));
} }
@ -1042,6 +1060,104 @@ pdfioContentTextEnd(pdfio_stream_t *st) // I - Stream
} }
//
// 'pdfioContentTextMeasure()' - Measure a text string and return its width.
//
// This function measures the given text string "s" and returns its width based
// on "size". The text string must always use the UTF-8 (Unicode) encoding but
// any control characters (such as newlines) are ignored.
//
double // O - Width
pdfioContentTextMeasure(
pdfio_obj_t *font, // I - Font object created by @link pdfioFileCreateFontObjFromFile@
const char *s, // I - UTF-8 string
double size) // I - Font size/height
{
const char *subtype; // Font sub-type
ttf_t *ttf = (ttf_t *)_pdfioObjGetExtension(font);
// TrueType font data
ttf_rect_t extents; // Text extents
int ch; // Unicode character
char temp[1024], // Temporary string
*tempptr; // Pointer into temporary string
if ((subtype = pdfioObjGetSubtype(font)) == NULL || strcmp(subtype, "Type0"))
{
// Map non-CP1282 characters to '?', everything else as-is...
tempptr = temp;
while (*s && tempptr < (temp + sizeof(temp) - 3))
{
if ((*s & 0xe0) == 0xc0)
{
// Two-byte UTF-8
ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
s += 2;
}
else if ((*s & 0xf0) == 0xe0)
{
// Three-byte UTF-8
ch = ((s[0] & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
s += 3;
}
else if ((*s & 0xf8) == 0xf0)
{
// Four-byte UTF-8
ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
s += 4;
}
else
{
ch = *s++;
}
if (ch > 255)
{
// Try mapping from Unicode to CP1252...
size_t i; // Looping var
for (i = 0; i < (sizeof(_pdfio_cp1252) / sizeof(_pdfio_cp1252[0])); i ++)
{
if (ch == _pdfio_cp1252[i])
break;
}
if (i >= (sizeof(_pdfio_cp1252) / sizeof(_pdfio_cp1252[0])))
ch = '?'; // Unsupported chars map to ?
}
if (ch < 128)
{
// ASCII
*tempptr++ = ch;
}
else if (ch < 2048)
{
// 2-byte UTF-8
*tempptr++ = 0xc0 | ((ch >> 6) & 0x1f);
*tempptr++ = 0x80 | (ch & 0x3f);
}
else
{
// 3-byte UTF-8
*tempptr++ = 0xe0 | ((ch >> 12) & 0x0f);
*tempptr++ = 0x80 | ((ch >> 6) & 0x3f);
*tempptr++ = 0x80 | (ch & 0x3f);
}
}
*tempptr = '\0';
s = temp;
}
ttfGetExtents(ttf, size, s, &extents);
return (extents.right - extents.left);
}
// //
// 'pdfioContentTextMoveLine()' - Move to the next line and offset. // 'pdfioContentTextMoveLine()' - Move to the next line and offset.
// //
@ -1071,7 +1187,21 @@ pdfioContentTextMoveTo(
// //
// 'pdfioContentTextNextLine()' - Move to the next line. // 'pdfioContentTextNewLine()' - Move to the next line.
//
bool // O - `true` on success, `false` on failure
pdfioContentTextNewLine(
pdfio_stream_t *st) // I - Stream
{
return (pdfioStreamPuts(st, "T*\n"));
}
//
// 'pdfioContentTextNextLine()' - Legacy function name preserved for binary compatibility.
//
// @private@
// //
bool // O - `true` on success, `false` on failure bool // O - `true` on success, `false` on failure
@ -1082,6 +1212,86 @@ pdfioContentTextNextLine(
} }
//
// 'pdfioContentTextNewLineShow()' - Move to the next line and show text.
//
// This function moves to the next line and then shows some text with optional
// word and character spacing in a PDF content stream. The "unicode" argument
// specifies that the current font maps to full Unicode. The "s" argument
// specifies a UTF-8 encoded string.
//
bool // O - `true` on success, `false` on failure
pdfioContentTextNewLineShow(
pdfio_stream_t *st, // I - Stream
double ws, // I - Word spacing or `0.0` for none
double cs, // I - Character spacing or `0.0` for none
bool unicode, // I - Unicode text?
const char *s) // I - String to show
{
bool newline = false; // New line?
char op; // Text operator
// Write word and/or character spacing as needed...
if (ws > 0.0 || cs > 0.0)
{
// Use " operator to show text with word and character spacing...
if (!pdfioStreamPrintf(st, "%g %g", ws, cs))
return (false);
op = '\"';
}
else
{
// Use ' operator to show text with the defaults...
op = '\'';
}
// Write the string...
if (!write_string(st, unicode, s, &newline))
return (false);
// Draw it...
if (newline)
return (pdfioStreamPrintf(st, "%c T*\n", op));
else
return (pdfioStreamPrintf(st, "%c\n", op));
}
//
// 'pdfioContentTextNewLineShowf()' - Show formatted text.
//
// This function moves to the next line and shows some formatted text with
// optional word and character spacing in a PDF content stream. The "unicode"
// argument specifies that the current font maps to full Unicode. The "format"
// argument specifies a UTF-8 encoded `printf`-style format string.
//
bool // O - `true` on success, `false` on failure
pdfioContentTextNewLineShowf(
pdfio_stream_t *st, // I - Stream
double ws, // I - Word spacing or `0.0` for none
double cs, // I - Character spacing or `0.0` for none
bool unicode, // I - Unicode text?
const char *format, // I - `printf`-style format string
...) // I - Additional arguments as needed
{
char buffer[8192]; // Text buffer
va_list ap; // Argument pointer
// Format the string...
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
// Show it...
return (pdfioContentTextNewLineShow(st, ws, cs, unicode, buffer));
}
// //
// 'pdfioContentTextShow()' - Show text. // 'pdfioContentTextShow()' - Show text.
// //
@ -1114,9 +1324,9 @@ pdfioContentTextShow(
// //
// 'pdfioContentTextShowf()' - Show formatted text. // 'pdfioContentTextShowf()' - Show formatted text.
// //
// This function shows some text in a PDF content stream. The "unicode" argument // This function shows some formatted text in a PDF content stream. The
// specifies that the current font maps to full Unicode. The "format" argument // "unicode" argument specifies that the current font maps to full Unicode.
// specifies a UTF-8 encoded `printf`-style format string. // The "format" argument specifies a UTF-8 encoded `printf`-style format string.
// //
bool bool
@ -1267,13 +1477,13 @@ pdfioFileCreateFontObjFromFile(
pdfio_dict_t *dict, // Font dictionary pdfio_dict_t *dict, // Font dictionary
*desc, // Font descriptor *desc, // Font descriptor
*file; // Font file dictionary *file; // Font file dictionary
pdfio_obj_t *obj, // Font object pdfio_obj_t *obj = NULL, // Font object
*desc_obj, // Font descriptor object *desc_obj, // Font descriptor object
*file_obj; // Font file object *file_obj; // Font file object
const char *basefont; // Base font name const char *basefont; // Base font name
pdfio_array_t *bbox; // Font bounding box array pdfio_array_t *bbox; // Font bounding box array
pdfio_stream_t *st; // Font stream pdfio_stream_t *st; // Font stream
int fd; // File int fd = -1; // File
unsigned char buffer[16384]; // Read buffer unsigned char buffer[16384]; // Read buffer
ssize_t bytes; // Bytes read ssize_t bytes; // Bytes read
@ -1302,48 +1512,32 @@ pdfioFileCreateFontObjFromFile(
// Create the font file dictionary and object... // Create the font file dictionary and object...
if ((file = pdfioDictCreate(pdf)) == NULL) if ((file = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
close(fd);
return (NULL);
}
pdfioDictSetName(file, "Filter", "FlateDecode"); pdfioDictSetName(file, "Filter", "FlateDecode");
if ((file_obj = pdfioFileCreateObj(pdf, file)) == NULL) if ((file_obj = pdfioFileCreateObj(pdf, file)) == NULL)
{ goto done;
ttfDelete(font);
close(fd);
return (NULL);
}
if ((st = pdfioObjCreateStream(file_obj, PDFIO_FILTER_FLATE)) == NULL) if ((st = pdfioObjCreateStream(file_obj, PDFIO_FILTER_FLATE)) == NULL)
{ goto done;
ttfDelete(font);
close(fd);
return (NULL);
}
while ((bytes = read(fd, buffer, sizeof(buffer))) > 0) while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
{ {
if (!pdfioStreamWrite(st, buffer, (size_t)bytes)) if (!pdfioStreamWrite(st, buffer, (size_t)bytes))
{ {
ttfDelete(font);
close(fd);
pdfioStreamClose(st); pdfioStreamClose(st);
return (NULL); goto done;
} }
} }
close(fd); close(fd);
fd = -1;
pdfioStreamClose(st); pdfioStreamClose(st);
// Create the font descriptor dictionary and object... // Create the font descriptor dictionary and object...
if ((bbox = pdfioArrayCreate(pdf)) == NULL) if ((bbox = pdfioArrayCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
ttfGetBounds(font, &bounds); ttfGetBounds(font, &bounds);
@ -1353,10 +1547,7 @@ pdfioFileCreateFontObjFromFile(
pdfioArrayAppendNumber(bbox, bounds.top); pdfioArrayAppendNumber(bbox, bounds.top);
if ((desc = pdfioDictCreate(pdf)) == NULL) if ((desc = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
basefont = pdfioStringCreate(pdf, ttfGetPostScriptName(font)); basefont = pdfioStringCreate(pdf, ttfGetPostScriptName(font));
@ -1375,59 +1566,70 @@ pdfioFileCreateFontObjFromFile(
pdfioDictSetNumber(desc, "StemV", ttfGetWeight(font) / 4 + 25); pdfioDictSetNumber(desc, "StemV", ttfGetWeight(font) / 4 + 25);
if ((desc_obj = pdfioFileCreateObj(pdf, desc)) == NULL) if ((desc_obj = pdfioFileCreateObj(pdf, desc)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
pdfioObjClose(desc_obj); pdfioObjClose(desc_obj);
if (unicode) if (unicode)
{ {
// Unicode (CID) font... // Unicode (CID) font...
pdfio_dict_t *cid2gid; // CIDToGIDMap dictionary pdfio_dict_t *cid2gid, // CIDToGIDMap dictionary
pdfio_obj_t *cid2gid_obj; // CIDToGIDMap object *to_unicode; // ToUnicode dictionary
pdfio_obj_t *cid2gid_obj, // CIDToGIDMap object
*to_unicode_obj;// ToUnicode object
size_t i, // Looping var size_t i, // Looping var
start, // Start character
num_cmap; // Number of CMap entries num_cmap; // Number of CMap entries
const int *cmap; // CMap entries const int *cmap; // CMap entries
int min_glyph, // First glyph
max_glyph; // Last glyph
unsigned short glyphs[65536]; // Glyph to Unicode mapping
unsigned char *bufptr, // Pointer into buffer unsigned char *bufptr, // Pointer into buffer
*bufend; // End of buffer *bufend; // End of buffer
pdfio_dict_t *type2; // CIDFontType2 font dictionary pdfio_dict_t *type2; // CIDFontType2 font dictionary
pdfio_obj_t *type2_obj; // CIDFontType2 font object pdfio_obj_t *type2_obj; // CIDFontType2 font object
pdfio_array_t *descendants; // Decendant font list pdfio_array_t *descendants; // Decendant font list
pdfio_dict_t *sidict; // CIDSystemInfo dictionary pdfio_dict_t *sidict; // CIDSystemInfo dictionary
pdfio_array_t *w_array, // Width array
*temp_array; // Temporary width sub-array
int w0, w1; // Widths
// Create a CIDSystemInfo mapping to Adobe UCS2 v0 (Unicode)
if ((sidict = pdfioDictCreate(pdf)) == NULL)
goto done;
pdfioDictSetString(sidict, "Registry", "Adobe");
pdfioDictSetString(sidict, "Ordering", "Identity");
pdfioDictSetNumber(sidict, "Supplement", 0);
// Create a CIDToGIDMap object for the Unicode font... // Create a CIDToGIDMap object for the Unicode font...
if ((cid2gid = pdfioDictCreate(pdf)) == NULL) if ((cid2gid = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
#ifndef DEBUG #ifndef DEBUG
pdfioDictSetName(cid2gid, "Filter", "FlateDecode"); pdfioDictSetName(cid2gid, "Filter", "FlateDecode");
#endif // !DEBUG #endif // !DEBUG
if ((cid2gid_obj = pdfioFileCreateObj(pdf, cid2gid)) == NULL) if ((cid2gid_obj = pdfioFileCreateObj(pdf, cid2gid)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
#ifdef DEBUG #ifdef DEBUG
if ((st = pdfioObjCreateStream(cid2gid_obj, PDFIO_FILTER_NONE)) == NULL) if ((st = pdfioObjCreateStream(cid2gid_obj, PDFIO_FILTER_NONE)) == NULL)
#else #else
if ((st = pdfioObjCreateStream(cid2gid_obj, PDFIO_FILTER_FLATE)) == NULL) if ((st = pdfioObjCreateStream(cid2gid_obj, PDFIO_FILTER_FLATE)) == NULL)
#endif // DEBUG #endif // DEBUG
{ goto done;
ttfDelete(font);
return (NULL);
}
cmap = ttfGetCMap(font, &num_cmap); cmap = ttfGetCMap(font, &num_cmap);
min_glyph = 65536;
max_glyph = 0;
memset(glyphs, 0, sizeof(glyphs));
PDFIO_DEBUG("pdfioFileCreateFontObjFromFile: num_cmap=%u\n", (unsigned)num_cmap);
for (i = 0, bufptr = buffer, bufend = buffer + sizeof(buffer); i < num_cmap; i ++) for (i = 0, bufptr = buffer, bufend = buffer + sizeof(buffer); i < num_cmap; i ++)
{ {
PDFIO_DEBUG("pdfioFileCreateFontObjFromFile: cmap[%u]=%d\n", (unsigned)i, cmap[i]);
if (cmap[i] < 0) if (cmap[i] < 0)
{ {
// Map undefined glyph to .notdef... // Map undefined glyph to .notdef...
@ -1439,6 +1641,12 @@ pdfioFileCreateFontObjFromFile(
// Map to specified glyph... // Map to specified glyph...
*bufptr++ = (unsigned char)(cmap[i] >> 8); *bufptr++ = (unsigned char)(cmap[i] >> 8);
*bufptr++ = (unsigned char)(cmap[i] & 255); *bufptr++ = (unsigned char)(cmap[i] & 255);
glyphs[cmap[i]] = i;
if (cmap[i] < min_glyph)
min_glyph = cmap[i];
if (cmap[i] > max_glyph)
max_glyph = cmap[i];
} }
if (bufptr >= bufend) if (bufptr >= bufend)
@ -1447,8 +1655,7 @@ pdfioFileCreateFontObjFromFile(
if (!pdfioStreamWrite(st, buffer, (size_t)(bufptr - buffer))) if (!pdfioStreamWrite(st, buffer, (size_t)(bufptr - buffer)))
{ {
pdfioStreamClose(st); pdfioStreamClose(st);
ttfDelete(font); goto done;
return (NULL);
} }
bufptr = buffer; bufptr = buffer;
@ -1461,30 +1668,102 @@ pdfioFileCreateFontObjFromFile(
if (!pdfioStreamWrite(st, buffer, (size_t)(bufptr - buffer))) if (!pdfioStreamWrite(st, buffer, (size_t)(bufptr - buffer)))
{ {
pdfioStreamClose(st); pdfioStreamClose(st);
ttfDelete(font); goto done;
return (NULL);
} }
} }
pdfioStreamClose(st); pdfioStreamClose(st);
// ToUnicode mapping object
to_unicode = pdfioDictCreate(pdf);
pdfioDictSetName(to_unicode, "Type", "CMap");
pdfioDictSetName(to_unicode, "CMapName", "Adobe-Identity-UCS2");
pdfioDictSetDict(to_unicode, "CIDSystemInfo", sidict);
#ifndef DEBUG
pdfioDictSetName(to_unicode, "Filter", "FlateDecode");
#endif // !DEBUG
if ((to_unicode_obj = pdfioFileCreateObj(pdf, to_unicode)) == NULL)
goto done;
#ifdef DEBUG
if ((st = pdfioObjCreateStream(to_unicode_obj, PDFIO_FILTER_NONE)) == NULL)
#else
if ((st = pdfioObjCreateStream(to_unicode_obj, PDFIO_FILTER_FLATE)) == NULL)
#endif // DEBUG
goto done;
pdfioStreamPuts(st,
"stream\n"
"/CIDInit /ProcSet findresource begin\n"
"12 dict begin\n"
"begincmap\n"
"/CIDSystemInfo<<\n"
"/Registry (Adobe)\n"
"/Ordering (UCS2)\n"
"/Supplement 0\n"
">> def\n"
"/CMapName /Adobe-Identity-UCS2 def\n"
"/CMapType 2 def\n"
"1 begincodespacerange\n"
"<0000> <FFFF>\n"
"endcodespacerange\n"
"1 beginbfrange\n"
"<0000> <FFFF> <0000>\n"
"endbfrange\n"
"endcmap\n"
"CMapName currentdict /CMap defineresource pop\n"
"end\n"
"end\n");
pdfioStreamClose(st);
// Create a CIDFontType2 dictionary for the Unicode font... // Create a CIDFontType2 dictionary for the Unicode font...
if ((type2 = pdfioDictCreate(pdf)) == NULL) if ((type2 = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
if ((sidict = pdfioDictCreate(pdf)) == NULL) // Width array
{ if ((w_array = pdfioArrayCreate(pdf)) == NULL)
ttfDelete(font); goto done;
return (NULL);
}
// CIDSystemInfo mapping to Adobe UCS2 v0 (Unicode) for (start = 0, w0 = ttfGetWidth(font, 0), i = 1; i < 65536; start = i, w0 = w1, i ++)
pdfioDictSetString(sidict, "Registry", "Adobe"); {
pdfioDictSetString(sidict, "Ordering", "Identity"); while (i < 65536 && (w1 = ttfGetWidth(font, i)) == w0)
pdfioDictSetNumber(sidict, "Supplement", 0); i ++;
if ((i - start) > 1)
{
// Encode a repeating sequence...
pdfioArrayAppendNumber(w_array, start);
pdfioArrayAppendNumber(w_array, i - 1);
pdfioArrayAppendNumber(w_array, w0);
}
else
{
// Encode a non-repeating sequence...
pdfioArrayAppendNumber(w_array, start);
if ((temp_array = pdfioArrayCreate(pdf)) == NULL)
goto done;
pdfioArrayAppendNumber(temp_array, w0);
for (w0 = w1, i ++; i < 65536; w0 = w1, i ++)
{
if ((w1 = ttfGetWidth(font, i)) == w0 && i < 65535)
break;
pdfioArrayAppendNumber(temp_array, w0);
}
if (i == 65536)
pdfioArrayAppendNumber(temp_array, w0);
else
i --;
pdfioArrayAppendArray(w_array, temp_array);
}
}
// Then the dictionary for the CID base font... // Then the dictionary for the CID base font...
pdfioDictSetName(type2, "Type", "Font"); pdfioDictSetName(type2, "Type", "Font");
@ -1493,56 +1772,41 @@ pdfioFileCreateFontObjFromFile(
pdfioDictSetDict(type2, "CIDSystemInfo", sidict); pdfioDictSetDict(type2, "CIDSystemInfo", sidict);
pdfioDictSetObj(type2, "CIDToGIDMap", cid2gid_obj); pdfioDictSetObj(type2, "CIDToGIDMap", cid2gid_obj);
pdfioDictSetObj(type2, "FontDescriptor", desc_obj); pdfioDictSetObj(type2, "FontDescriptor", desc_obj);
pdfioDictSetArray(type2, "W", w_array);
if ((type2_obj = pdfioFileCreateObj(pdf, type2)) == NULL) if ((type2_obj = pdfioFileCreateObj(pdf, type2)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
pdfioObjClose(type2_obj); pdfioObjClose(type2_obj);
// Create a Type 0 font object... // Create a Type 0 font object...
if ((descendants = pdfioArrayCreate(pdf)) == NULL) if ((descendants = pdfioArrayCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
pdfioArrayAppendObj(descendants, type2_obj); pdfioArrayAppendObj(descendants, type2_obj);
if ((dict = pdfioDictCreate(pdf)) == NULL) if ((dict = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
pdfioDictSetName(dict, "Type", "Font"); pdfioDictSetName(dict, "Type", "Font");
pdfioDictSetName(dict, "Subtype", "Type0"); pdfioDictSetName(dict, "Subtype", "Type0");
pdfioDictSetName(dict, "BaseFont", basefont); pdfioDictSetName(dict, "BaseFont", basefont);
pdfioDictSetArray(dict, "DescendantFonts", descendants); pdfioDictSetArray(dict, "DescendantFonts", descendants);
pdfioDictSetName(dict, "Encoding", "Identity-H"); pdfioDictSetName(dict, "Encoding", "Identity-H");
pdfioDictSetObj(dict, "ToUnicode", to_unicode_obj);
if ((obj = pdfioFileCreateObj(pdf, dict)) == NULL) if ((obj = pdfioFileCreateObj(pdf, dict)) != NULL)
return (NULL); pdfioObjClose(obj);
pdfioObjClose(obj);
} }
else else
{ {
// Simple (CP1282 or custom encoding) 8-bit font... // Simple (CP1282 or custom encoding) 8-bit font...
if (ttfGetMaxChar(font) >= 255 && !pdf->cp1252_obj && !create_cp1252(pdf)) if (ttfGetMaxChar(font) >= 255 && !pdf->cp1252_obj && !create_cp1252(pdf))
{ goto done;
ttfDelete(font);
return (NULL);
}
// Create a TrueType font object... // Create a TrueType font object...
if ((dict = pdfioDictCreate(pdf)) == NULL) if ((dict = pdfioDictCreate(pdf)) == NULL)
{ goto done;
ttfDelete(font);
return (NULL);
}
pdfioDictSetName(dict, "Type", "Font"); pdfioDictSetName(dict, "Type", "Font");
pdfioDictSetName(dict, "Subtype", "TrueType"); pdfioDictSetName(dict, "Subtype", "TrueType");
@ -1552,16 +1816,16 @@ pdfioFileCreateFontObjFromFile(
pdfioDictSetObj(dict, "FontDescriptor", desc_obj); pdfioDictSetObj(dict, "FontDescriptor", desc_obj);
if ((obj = pdfioFileCreateObj(pdf, dict)) == NULL) if ((obj = pdfioFileCreateObj(pdf, dict)) != NULL)
{ pdfioObjClose(obj);
ttfDelete(font);
return (NULL);
}
pdfioObjClose(obj);
} }
ttfDelete(font); done:
if (fd >= 0)
close(fd);
_pdfioObjSetExtension(obj, font, (_pdfio_extfree_t)ttfDelete);
return (obj); return (obj);
} }

View File

@ -1,7 +1,7 @@
// //
// Public content header file for PDFio. // Public content header file for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -9,18 +9,7 @@
#ifndef PDFIO_CONTENT_H #ifndef PDFIO_CONTENT_H
# define PDFIO_CONTENT_H # define PDFIO_CONTENT_H
//
// Include necessary headers...
//
# include "pdfio.h" # include "pdfio.h"
//
// C++ magic...
//
# ifdef __cplusplus # ifdef __cplusplus
extern "C" { extern "C" {
# endif // __cplusplus # endif // __cplusplus
@ -91,6 +80,7 @@ extern bool pdfioContentPathClose(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentPathCurve(pdfio_stream_t *st, double x1, double y1, double x2, double y2, double x3, double y3) _PDFIO_PUBLIC; extern bool pdfioContentPathCurve(pdfio_stream_t *st, double x1, double y1, double x2, double y2, double x3, double y3) _PDFIO_PUBLIC;
extern bool pdfioContentPathCurve13(pdfio_stream_t *st, double x1, double y1, double x3, double y3) _PDFIO_PUBLIC; extern bool pdfioContentPathCurve13(pdfio_stream_t *st, double x1, double y1, double x3, double y3) _PDFIO_PUBLIC;
extern bool pdfioContentPathCurve23(pdfio_stream_t *st, double x2, double y2, double x3, double y3) _PDFIO_PUBLIC; extern bool pdfioContentPathCurve23(pdfio_stream_t *st, double x2, double y2, double x3, double y3) _PDFIO_PUBLIC;
extern bool pdfioContentPathEnd(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentPathLineTo(pdfio_stream_t *st, double x, double y) _PDFIO_PUBLIC; extern bool pdfioContentPathLineTo(pdfio_stream_t *st, double x, double y) _PDFIO_PUBLIC;
extern bool pdfioContentPathMoveTo(pdfio_stream_t *st, double x, double y) _PDFIO_PUBLIC; extern bool pdfioContentPathMoveTo(pdfio_stream_t *st, double x, double y) _PDFIO_PUBLIC;
extern bool pdfioContentPathRect(pdfio_stream_t *st, double x, double y, double width, double height) _PDFIO_PUBLIC; extern bool pdfioContentPathRect(pdfio_stream_t *st, double x, double y, double width, double height) _PDFIO_PUBLIC;
@ -125,8 +115,12 @@ extern bool pdfioContentSetTextXScaling(pdfio_stream_t *st, double percent) _PD
extern bool pdfioContentStroke(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentStroke(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextBegin(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextBegin(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextEnd(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextEnd(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern double pdfioContentTextMeasure(pdfio_obj_t *font, const char *s, double size) _PDFIO_PUBLIC;
extern bool pdfioContentTextMoveLine(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC; extern bool pdfioContentTextMoveLine(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC;
extern bool pdfioContentTextMoveTo(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC; extern bool pdfioContentTextMoveTo(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC;
extern bool pdfioContentTextNewLine(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextNewLineShow(pdfio_stream_t *st, double ws, double cs, bool unicode, const char *s) _PDFIO_PUBLIC;
extern bool pdfioContentTextNewLineShowf(pdfio_stream_t *st, double ws, double cs, bool unicode, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(5,6);
extern bool pdfioContentTextNextLine(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextNextLine(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextShow(pdfio_stream_t *st, bool unicode, const char *s) _PDFIO_PUBLIC; extern bool pdfioContentTextShow(pdfio_stream_t *st, bool unicode, const char *s) _PDFIO_PUBLIC;
extern bool pdfioContentTextShowf(pdfio_stream_t *st, bool unicode, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(3,4); extern bool pdfioContentTextShowf(pdfio_stream_t *st, bool unicode, const char *format, ...) _PDFIO_PUBLIC _PDFIO_FORMAT(3,4);
@ -150,10 +144,6 @@ extern bool pdfioPageDictAddFont(pdfio_dict_t *dict, const char *name, pdfio_ob
extern bool pdfioPageDictAddImage(pdfio_dict_t *dict, const char *name, pdfio_obj_t *obj) _PDFIO_PUBLIC; extern bool pdfioPageDictAddImage(pdfio_dict_t *dict, const char *name, pdfio_obj_t *obj) _PDFIO_PUBLIC;
//
// C++ magic...
//
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif // __cplusplus # endif // __cplusplus

View File

@ -1,16 +1,12 @@
// //
// Cryptographic support functions for PDFio. // Cryptographic support functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
#if _WIN32 #if _WIN32
# include <windows.h> # include <windows.h>
@ -446,10 +442,18 @@ _pdfio_crypto_cb_t // O - Decryption callback or `NULL` for none
// Initialize the RC4 context using 40 bits of the digest... // Initialize the RC4 context using 40 bits of the digest...
_pdfioCryptoRC4Init(&ctx->rc4, digest, 5); _pdfioCryptoRC4Init(&ctx->rc4, digest, 5);
*ivlen = 0;
return ((_pdfio_crypto_cb_t)_pdfioCryptoRC4Crypt); return ((_pdfio_crypto_cb_t)_pdfioCryptoRC4Crypt);
case PDFIO_ENCRYPTION_RC4_128 :
case PDFIO_ENCRYPTION_AES_128 : case PDFIO_ENCRYPTION_AES_128 :
if (*ivlen < 16)
{
*ivlen = 0;
_pdfioFileError(pdf, "Value too short for AES encryption.");
return (NULL);
}
case PDFIO_ENCRYPTION_RC4_128 :
// Copy the key data for the MD5 hash. // Copy the key data for the MD5 hash.
memcpy(data, pdf->file_key, sizeof(pdf->file_key)); memcpy(data, pdf->file_key, sizeof(pdf->file_key));
data[16] = (uint8_t)obj->number; data[16] = (uint8_t)obj->number;
@ -663,7 +667,12 @@ _pdfioCryptoUnlock(
length = 128; length = 128;
} }
} }
// TODO: Implement AES-256 - V6 R6 else if (version == 6 && revision == 6)
{
// TODO: Implement AES-256 - V6 R6
pdf->encryption = PDFIO_ENCRYPTION_AES_256;
length = 256;
}
PDFIO_DEBUG("_pdfioCryptoUnlock: encryption=%d, length=%d\n", pdf->encryption, length); PDFIO_DEBUG("_pdfioCryptoUnlock: encryption=%d, length=%d\n", pdf->encryption, length);
@ -788,6 +797,8 @@ _pdfioCryptoUnlock(
else else
{ {
// TODO: Implement AES-256 security handler // TODO: Implement AES-256 security handler
_pdfioFileError(pdf, "Unable to unlock AES-256 encrypted file at this time.");
return (false);
} }
// If we get here we need to try another password... // If we get here we need to try another password...

View File

@ -1,16 +1,12 @@
// //
// PDF dictionary functions for PDFio. // PDF dictionary functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -464,6 +460,47 @@ _pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary
} }
//
// 'pdfioDictIterateKeys()' - Iterate the keys in a dictionary.
//
// This function iterates the keys in a dictionary, calling the supplied
// function "cb":
//
// ```
// bool
// my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data)
// {
// ... "key" contains the dictionary key ...
// ... return true to continue or false to stop ...
// }
// ```
//
// The iteration continues as long as the callback returns `true` or all keys
// have been iterated.
//
void
pdfioDictIterateKeys(
pdfio_dict_t *dict, // I - Dictionary
pdfio_dict_cb_t cb, // I - Callback function
void *cb_data) // I - Callback data
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
// Range check input...
if (!dict || !cb)
return;
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
if (!(cb)(dict, pair->key, cb_data))
break;
}
}
// //
// '_pdfioDictRead()' - Read a dictionary from a PDF file. // '_pdfioDictRead()' - Read a dictionary from a PDF file.
// //
@ -500,8 +537,15 @@ _pdfioDictRead(pdfio_file_t *pdf, // I - PDF file
_pdfioFileError(pdf, "Invalid dictionary contents."); _pdfioFileError(pdf, "Invalid dictionary contents.");
break; break;
} }
else if (_pdfioDictGetValue(dict, key + 1))
{
_pdfioFileError(pdf, "Duplicate dictionary key '%s'.", key + 1);
return (NULL);
}
// Then get the next value... // Then get the next value...
PDFIO_DEBUG("_pdfioDictRead: Reading value for '%s'.\n", key + 1);
if (!_pdfioValueRead(pdf, obj, tb, &value, depth)) if (!_pdfioValueRead(pdf, obj, tb, &value, depth))
{ {
_pdfioFileError(pdf, "Missing value for dictionary key."); _pdfioFileError(pdf, "Missing value for dictionary key.");
@ -707,7 +751,7 @@ pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary
bool // O - `true` on success, `false` on failure bool // O - `true` on success, `false` on failure
pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key const char *key, // I - Key
double value) // I - Value double value) // I - Value
{ {
_pdfio_value_t temp; // New value _pdfio_value_t temp; // New value
@ -891,9 +935,9 @@ _pdfioDictSetValue(
#ifdef DEBUG #ifdef DEBUG
PDFIO_DEBUG("_pdfioDictSetValue(%p): %lu pairs\n", (void *)dict, (unsigned long)dict->num_pairs); PDFIO_DEBUG("_pdfioDictSetValue(%p): %lu pairs\n", (void *)dict, (unsigned long)dict->num_pairs);
PDFIO_DEBUG("_pdfioDictSetValue(%p): ", (void *)dict); // PDFIO_DEBUG("_pdfioDictSetValue(%p): ", (void *)dict);
PDFIO_DEBUG_DICT(dict); // PDFIO_DEBUG_DICT(dict);
PDFIO_DEBUG("\n"); // PDFIO_DEBUG("\n");
#endif // DEBUG #endif // DEBUG
return (true); return (true);

View File

@ -1,16 +1,12 @@
// //
// PDF file functions for PDFio. // PDF file functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
#ifndef O_BINARY #ifndef O_BINARY
# define O_BINARY 0 # define O_BINARY 0
@ -24,6 +20,7 @@
static pdfio_obj_t *add_obj(pdfio_file_t *pdf, size_t number, unsigned short generation, off_t offset); 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 int compare_objmaps(_pdfio_objmap_t *a, _pdfio_objmap_t *b);
static int compare_objs(pdfio_obj_t **a, pdfio_obj_t **b); static int compare_objs(pdfio_obj_t **a, pdfio_obj_t **b);
static const char *get_info_string(pdfio_file_t *pdf, const char *key);
static bool load_obj_stream(pdfio_obj_t *obj); 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_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 load_xref(pdfio_file_t *pdf, off_t xref_offset, pdfio_password_cb_t password_cb, void *password_data);
@ -264,46 +261,26 @@ pdfioFileCreate(
// Write a standard PDF header... // Write a standard PDF header...
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version)) if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
{ goto error;
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
}
// Create the pages object... // Create the pages object...
if ((dict = pdfioDictCreate(pdf)) == NULL) if ((dict = pdfioDictCreate(pdf)) == NULL)
{ goto error;
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
}
pdfioDictSetName(dict, "Type", "Pages"); pdfioDictSetName(dict, "Type", "Pages");
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL) if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
{ goto error;
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
}
// Create the info object... // Create the info object...
if ((info_dict = pdfioDictCreate(pdf)) == NULL) if ((info_dict = pdfioDictCreate(pdf)) == NULL)
{ goto error;
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
}
pdfioDictSetDate(info_dict, "CreationDate", time(NULL)); pdfioDictSetDate(info_dict, "CreationDate", time(NULL));
pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION); pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION);
if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL) if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL)
{ goto error;
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
}
// Create random file ID values... // Create random file ID values...
_pdfioCryptoMakeRandom(id_value, sizeof(id_value)); _pdfioCryptoMakeRandom(id_value, sizeof(id_value));
@ -315,6 +292,15 @@ pdfioFileCreate(
} }
return (pdf); return (pdf);
// Common error handling code...
error:
pdfioFileClose(pdf);
unlink(filename);
return (NULL);
} }
@ -344,6 +330,32 @@ pdfioFileCreateArrayObj(
} }
//
// 'pdfioFileCreateNumberObj()' - Create a new object in a PDF file containing a number.
//
// This function creates a new object with a number value in a PDF file.
// You must call @link pdfioObjClose@ to write the object to the file.
//
pdfio_obj_t * // O - New object
pdfioFileCreateNumberObj(
pdfio_file_t *pdf, // I - PDF file
double number) // I - Number value
{
_pdfio_value_t value; // Object value
// Range check input...
if (!pdf)
return (NULL);
value.type = PDFIO_VALTYPE_NUMBER;
value.value.number = number;
return (_pdfioFileCreateObj(pdf, NULL, &value));
}
// //
// 'pdfioFileCreateObj()' - Create a new object in a PDF file. // 'pdfioFileCreateObj()' - Create a new object in a PDF file.
// //
@ -535,41 +547,26 @@ pdfioFileCreateOutput(
// Write a standard PDF header... // Write a standard PDF header...
if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version)) if (!_pdfioFilePrintf(pdf, "%%PDF-%s\n%%\342\343\317\323\n", version))
{ goto error;
pdfioFileClose(pdf);
return (NULL);
}
// Create the pages object... // Create the pages object...
if ((dict = pdfioDictCreate(pdf)) == NULL) if ((dict = pdfioDictCreate(pdf)) == NULL)
{ goto error;
pdfioFileClose(pdf);
return (NULL);
}
pdfioDictSetName(dict, "Type", "Pages"); pdfioDictSetName(dict, "Type", "Pages");
if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL) if ((pdf->pages_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
{ goto error;
pdfioFileClose(pdf);
return (NULL);
}
// Create the info object... // Create the info object...
if ((info_dict = pdfioDictCreate(pdf)) == NULL) if ((info_dict = pdfioDictCreate(pdf)) == NULL)
{ goto error;
pdfioFileClose(pdf);
return (NULL);
}
pdfioDictSetDate(info_dict, "CreationDate", time(NULL)); pdfioDictSetDate(info_dict, "CreationDate", time(NULL));
pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION); pdfioDictSetString(info_dict, "Producer", "pdfio/" PDFIO_VERSION);
if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL) if ((pdf->info_obj = pdfioFileCreateObj(pdf, info_dict)) == NULL)
{ goto error;
pdfioFileClose(pdf);
return (NULL);
}
// Create random file ID values... // Create random file ID values...
_pdfioCryptoMakeRandom(id_value, sizeof(id_value)); _pdfioCryptoMakeRandom(id_value, sizeof(id_value));
@ -581,6 +578,13 @@ pdfioFileCreateOutput(
} }
return (pdf); return (pdf);
// Common error handling code...
error:
pdfioFileClose(pdf);
return (NULL);
} }
@ -657,6 +661,220 @@ pdfioFileCreatePage(pdfio_file_t *pdf, // I - PDF file
} }
//
// 'pdfioFileCreateStringObj()' - Create a new object in a PDF file containing a string.
//
// This function creates a new object with a string value in a PDF file.
// You must call @link pdfioObjClose@ to write the object to the file.
//
pdfio_obj_t * // O - New object
pdfioFileCreateStringObj(
pdfio_file_t *pdf, // I - PDF file
const char *string) // I - String
{
_pdfio_value_t value; // Object value
// Range check input...
if (!pdf)
return (NULL);
value.type = PDFIO_VALTYPE_STRING;
value.value.string = string;
return (_pdfioFileCreateObj(pdf, NULL, &value));
}
//
// 'pdfioFileCreateTemporary()' - Create a temporary PDF file.
//
// This function creates a PDF file with a unique filename in the current
// temporary directory. The temporary file is stored in the string "buffer" an
// will have a ".pdf" extension. Otherwise, this function works the same as
// the @link pdfioFileCreate@ function.
//
// @since PDFio v1.1@
//
pdfio_file_t *
pdfioFileCreateTemporary(
char *buffer, // I - Filename buffer
size_t bufsize, // I - Size of filename buffer
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
{
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
const char *tmpdir; // Temporary directory
#if _WIN32 || defined(__APPLE__)
char tmppath[256]; // Temporary directory path
#endif // _WIN32 || __APPLE__
unsigned tmpnum; // Temporary filename number
// Range check input...
if (!buffer || bufsize < 32)
{
if (buffer)
*buffer = '\0';
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...
#if _WIN32
if ((tmpdir = getenv("TEMP")) == NULL)
{
GetTempPathA(sizeof(tmppath), tmppath);
tmpdir = tmppath;
}
#elif defined(__APPLE__)
if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK))
tmpdir = NULL;
if (!tmpdir)
{
// Grab the per-process temporary directory for sandboxed apps...
# ifdef _CS_DARWIN_USER_TEMP_DIR
if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath)))
tmpdir = tmppath;
else
# endif // _CS_DARWIN_USER_TEMP_DIR
tmpdir = "/private/tmp";
}
#else
if ((tmpdir = getenv("TMPDIR")) == NULL || access(tmpdir, W_OK))
tmpdir = "/tmp";
#endif // _WIN32
for (i = 0; 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)
break;
}
pdf->filename = strdup(buffer);
if (i >= 1000)
{
_pdfioFileError(pdf, "Unable to create file - %s", strerror(errno));
free(pdf->filename);
free(pdf);
*buffer = '\0';
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)
{
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));
}
return (pdf);
// Common error handling code...
error:
pdfioFileClose(pdf);
unlink(buffer);
*buffer = '\0';
return (NULL);
}
// //
// '_pdfioFileFindMappedObj()' - Find a mapped object. // '_pdfioFileFindMappedObj()' - Find a mapped object.
// //
@ -723,7 +941,7 @@ pdfioFileFindObj(
const char * // O - Author or `NULL` for none const char * // O - Author or `NULL` for none
pdfioFileGetAuthor(pdfio_file_t *pdf) // I - PDF file pdfioFileGetAuthor(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Author") : NULL); return (get_info_string(pdf, "Author"));
} }
@ -735,7 +953,7 @@ time_t // O - Creation date or `0` for none
pdfioFileGetCreationDate( pdfioFileGetCreationDate(
pdfio_file_t *pdf) // I - PDF file pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetDate(pdf->info_obj->value.value.dict, "CreationDate") : 0); return (pdf && pdf->info_obj ? pdfioDictGetDate(pdfioObjGetDict(pdf->info_obj), "CreationDate") : 0);
} }
@ -746,7 +964,7 @@ pdfioFileGetCreationDate(
const char * // O - Creator string or `NULL` for none const char * // O - Creator string or `NULL` for none
pdfioFileGetCreator(pdfio_file_t *pdf) // I - PDF file pdfioFileGetCreator(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Creator") : NULL); return (get_info_string(pdf, "Creator"));
} }
@ -768,7 +986,7 @@ pdfioFileGetID(pdfio_file_t *pdf) // I - PDF file
const char * // O - Keywords string or `NULL` for none const char * // O - Keywords string or `NULL` for none
pdfioFileGetKeywords(pdfio_file_t *pdf) // I - PDF file pdfioFileGetKeywords(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Keywords") : NULL); return (get_info_string(pdf, "Keywords"));
} }
@ -872,7 +1090,7 @@ pdfioFileGetPermissions(
const char * // O - Producer string or `NULL` for none const char * // O - Producer string or `NULL` for none
pdfioFileGetProducer(pdfio_file_t *pdf) // I - PDF file pdfioFileGetProducer(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Producer") : NULL); return (get_info_string(pdf, "Producer"));
} }
@ -883,7 +1101,7 @@ pdfioFileGetProducer(pdfio_file_t *pdf) // I - PDF file
const char * // O - Subject or `NULL` for none const char * // O - Subject or `NULL` for none
pdfioFileGetSubject(pdfio_file_t *pdf) // I - PDF file pdfioFileGetSubject(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Subject") : NULL); return (get_info_string(pdf, "Subject"));
} }
@ -894,7 +1112,7 @@ pdfioFileGetSubject(pdfio_file_t *pdf) // I - PDF file
const char * // O - Title or `NULL` for none const char * // O - Title or `NULL` for none
pdfioFileGetTitle(pdfio_file_t *pdf) // I - PDF file pdfioFileGetTitle(pdfio_file_t *pdf) // I - PDF file
{ {
return (pdf && pdf->info_obj ? pdfioDictGetString(pdf->info_obj->value.value.dict, "Title") : NULL); return (get_info_string(pdf, "Title"));
} }
@ -1237,6 +1455,51 @@ compare_objs(pdfio_obj_t **a, // I - First object
} }
//
// 'get_info_string()' - Get a string value from the Info dictionary.
//
// This function also handles converting binary strings to C strings, which
// occur in encrypted PDF files.
//
static const char * // O - String or `NULL` if not found
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)
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
{
// Something else that is not a string...
return (NULL);
}
}
// //
// 'load_obj_stream()' - Load an object stream. // 'load_obj_stream()' - Load an object stream.
// //
@ -1299,6 +1562,7 @@ load_obj_stream(pdfio_obj_t *obj) // I - Object to load
// Skip offset // Skip offset
_pdfioTokenGet(&tb, buffer, sizeof(buffer)); _pdfioTokenGet(&tb, buffer, sizeof(buffer));
PDFIO_DEBUG("load_obj_stream: %ld at offset %s\n", (long)number, buffer);
} }
if (!buffer[0]) if (!buffer[0])
@ -1522,6 +1786,10 @@ load_xref(
return (false); return (false);
} }
PDFIO_DEBUG("load_xref: tb.bufptr=%p, tb.bufend=%p, tb.bufptr[0]=0x%02x, tb.bufptr[0]=0x%02x\n", tb.bufptr, tb.bufend, tb.bufptr[0], tb.bufptr[1]);
if (tb.bufptr && tb.bufptr < tb.bufend && (tb.bufptr[0] == 0x0d || tb.bufptr[0] == 0x0a))
tb.bufptr ++; // Skip trailing CR or LF after token
_pdfioTokenFlush(&tb); _pdfioTokenFlush(&tb);
obj->stream_offset = _pdfioFileTell(pdf); obj->stream_offset = _pdfioFileTell(pdf);
@ -1544,9 +1812,9 @@ load_xref(
w_2 = w[0]; w_2 = w[0];
w_3 = w[0] + w[1]; w_3 = w[0] + w[1];
if (w[1] == 0 || w[2] > 2 || w[0] > sizeof(buffer) || w[1] > sizeof(buffer) || w[2] > sizeof(buffer) || w_total > sizeof(buffer)) if (w[1] == 0 || w[2] > 4 || w[0] > sizeof(buffer) || w[1] > sizeof(buffer) || w[2] > sizeof(buffer) || w_total > sizeof(buffer))
{ {
_pdfioFileError(pdf, "Cross-reference stream has invalid W key."); _pdfioFileError(pdf, "Cross-reference stream has invalid W key [%u %u %u].", (unsigned)w[0], (unsigned)w[1], (unsigned)w[2]);
return (false); return (false);
} }
@ -1586,9 +1854,11 @@ load_xref(
} }
} }
// Offset
for (i = 1, offset = buffer[w_2]; i < w[1]; i ++) for (i = 1, offset = buffer[w_2]; i < w[1]; i ++)
offset = (offset << 8) | buffer[w_2 + i]; offset = (offset << 8) | buffer[w_2 + i];
// Generation number
switch (w[2]) switch (w[2])
{ {
default : default :
@ -1600,6 +1870,19 @@ load_xref(
case 2 : case 2 :
generation = (buffer[w_3] << 8) | buffer[w_3 + 1]; generation = (buffer[w_3] << 8) | buffer[w_3 + 1];
break; break;
case 3 :
// Issue #46: Stupid Microsoft PDF generator using 3 bytes to
// encode 16-bit generation numbers == 0 (probably a lazy coder
// stuffing things into an array of 64-bit unsigned integers)
generation = (buffer[w_3] << 16) | (buffer[w_3 + 1] << 8) | buffer[w_3 + 2];
if (generation > 65535)
generation = 65535;
break;
case 4 : // Even stupider :)
generation = (buffer[w_3] << 24) | (buffer[w_3 + 1] << 16) | (buffer[w_3 + 2] << 8) | buffer[w_3 + 3];
if (generation > 65535)
generation = 65535;
break;
} }
// Create a placeholder for the object in memory... // Create a placeholder for the object in memory...
@ -1631,8 +1914,18 @@ load_xref(
break; break;
} }
if (i >= num_sobjs && num_sobjs < (sizeof(sobjs) / sizeof(sobjs[0]))) if (i >= num_sobjs)
sobjs[num_sobjs ++] = (size_t)offset; {
if (num_sobjs < (sizeof(sobjs) / sizeof(sobjs[0])))
{
sobjs[num_sobjs ++] = (size_t)offset;
}
else
{
_pdfioFileError(pdf, "Too many object streams.");
return (false);
}
}
} }
else if (!current) else if (!current)
{ {
@ -1680,12 +1973,12 @@ load_xref(
} }
} }
} }
else if (!strcmp(line, "xref")) else if (!strncmp(line, "xref", 4) && (!line[4] || isspace(line[4] & 255)))
{ {
// Read the xref tables // Read the xref tables
while (_pdfioFileGets(pdf, line, sizeof(line))) while (_pdfioFileGets(pdf, line, sizeof(line)))
{ {
if (!strcmp(line, "trailer")) if (!strncmp(line, "trailer", 7) && (!line[7] || isspace(line[7] & 255)))
break; break;
else if (!line[0]) else if (!line[0])
continue; continue;
@ -1750,7 +2043,7 @@ load_xref(
} }
} }
if (strcmp(line, "trailer")) if (strncmp(line, "trailer", 7))
{ {
_pdfioFileError(pdf, "Missing trailer."); _pdfioFileError(pdf, "Missing trailer.");
return (false); return (false);
@ -1769,6 +2062,8 @@ load_xref(
return (false); return (false);
} }
PDFIO_DEBUG("load_xref: Got trailer dict.\n");
_pdfioTokenFlush(&tb); _pdfioTokenFlush(&tb);
if (!pdf->trailer_dict) if (!pdf->trailer_dict)

View File

@ -1,16 +1,12 @@
// //
// PDF object functions for PDFio. // PDF object functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -33,8 +29,14 @@ pdfioObjClose(pdfio_obj_t *obj) // I - Object
if (!obj) if (!obj)
return (false); return (false);
// Clear the current object pointer...
obj->pdf->current_obj = NULL;
if (obj->pdf->mode != _PDFIO_MODE_WRITE) if (obj->pdf->mode != _PDFIO_MODE_WRITE)
return (true); // Nothing to do when reading {
// Nothing to do when reading
return (true);
}
// Write what remains for the object... // Write what remains for the object...
if (!obj->offset) if (!obj->offset)
@ -165,6 +167,12 @@ pdfioObjCreateStream(
return (NULL); return (NULL);
} }
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Write the header... // Write the header...
if (!_pdfioDictGetValue(obj->value.value.dict, "Length")) if (!_pdfioDictGetValue(obj->value.value.dict, "Length"))
{ {
@ -193,7 +201,8 @@ pdfioObjCreateStream(
if (!_pdfioFilePuts(obj->pdf, "stream\n")) if (!_pdfioFilePuts(obj->pdf, "stream\n"))
return (NULL); return (NULL);
obj->stream_offset = _pdfioFileTell(obj->pdf); obj->stream_offset = _pdfioFileTell(obj->pdf);
obj->pdf->current_obj = obj;
// Return the new stream... // Return the new stream...
return (_pdfioStreamCreate(obj, length_obj, filter)); return (_pdfioStreamCreate(obj, length_obj, filter));
@ -208,8 +217,13 @@ void
_pdfioObjDelete(pdfio_obj_t *obj) // I - Object _pdfioObjDelete(pdfio_obj_t *obj) // I - Object
{ {
if (obj) if (obj)
{
pdfioStreamClose(obj->stream); pdfioStreamClose(obj->stream);
if (obj->datafree)
(obj->datafree)(obj->data);
}
free(obj); free(obj);
} }
@ -254,6 +268,17 @@ pdfioObjGetDict(pdfio_obj_t *obj) // I - Object
} }
//
// '_pdfioObjGetExtension()' - Get the extension pointer for an object.
//
void * // O - Extension data
_pdfioObjGetExtension(pdfio_obj_t *obj) // I - Object
{
return (obj->data);
}
// //
// 'pdfioObjGetGeneration()' - Get the object's generation number. // 'pdfioObjGetGeneration()' - Get the object's generation number.
// //
@ -407,6 +432,9 @@ _pdfioObjLoad(pdfio_obj_t *obj) // I - Object
} }
ptr += 3; ptr += 3;
while (*ptr && isspace(*ptr & 255))
ptr ++;
_pdfioFileConsume(obj->pdf, (size_t)(ptr - line)); _pdfioFileConsume(obj->pdf, (size_t)(ptr - line));
// Then grab the object value... // Then grab the object value...
@ -425,11 +453,15 @@ _pdfioObjLoad(pdfio_obj_t *obj) // I - Object
return (false); return (false);
} }
PDFIO_DEBUG("_pdfioObjLoad: tb.bufptr=%p, tb.bufend=%p, tb.bufptr[0]=0x%02x, tb.bufptr[0]=0x%02x\n", tb.bufptr, tb.bufend, tb.bufptr[0], tb.bufptr[1]);
if (tb.bufptr && tb.bufptr < tb.bufend && (tb.bufptr[0] == 0x0d || tb.bufptr[0] == 0x0a))
tb.bufptr ++; // Skip trailing CR or LF after token
_pdfioTokenFlush(&tb); _pdfioTokenFlush(&tb);
if (!strcmp(line, "stream")) if (!strcmp(line, "stream"))
{ {
// Yes, save its location... // Yes, this is an embedded stream so save its location...
obj->stream_offset = _pdfioFileTell(obj->pdf); obj->stream_offset = _pdfioFileTell(obj->pdf);
PDFIO_DEBUG("_pdfioObjLoad: stream_offset=%lu.\n", (unsigned long)obj->stream_offset); PDFIO_DEBUG("_pdfioObjLoad: stream_offset=%lu.\n", (unsigned long)obj->stream_offset);
} }
@ -454,6 +486,12 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
if (!obj) if (!obj)
return (NULL); return (NULL);
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Make sure we've loaded the object dictionary... // Make sure we've loaded the object dictionary...
if (!obj->value.type) if (!obj->value.type)
{ {
@ -466,10 +504,27 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
return (NULL); return (NULL);
// Open the stream... // Open the stream...
obj->pdf->current_obj = obj;
return (_pdfioStreamOpen(obj, decode)); return (_pdfioStreamOpen(obj, decode));
} }
//
// '_pdfioObjSetExtension()' - Set extension data for an object.
//
void
_pdfioObjSetExtension(
pdfio_obj_t *obj, // I - Object
void *data, // I - Data
_pdfio_extfree_t datafree) // I - Free function
{
obj->data = data;
obj->datafree = datafree;
}
// //
// 'write_obj_header()' - Write the object header... // 'write_obj_header()' - Write the object header...
// //

View File

@ -1,17 +1,20 @@
// //
// PDF page functions for PDFio. // PDF page functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2022 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
#include "pdfio-private.h"
// //
// Include necessary headers... // Local functions...
// //
#include "pdfio-private.h" static _pdfio_value_t *get_contents(pdfio_obj_t *page);
// //
@ -47,3 +50,74 @@ pdfioPageCopy(pdfio_file_t *pdf, // I - PDF file
else else
return (_pdfioFileAddPage(pdf, dstpage)); return (_pdfioFileAddPage(pdf, dstpage));
} }
//
// 'pdfioPageGetNumStreams()' - Get the number of content streams for a page object.
//
size_t // O - Number of streams
pdfioPageGetNumStreams(
pdfio_obj_t *page) // I - Page object
{
_pdfio_value_t *contents = get_contents(page);
// Contents value
if (!contents)
return (0);
else if (contents->type == PDFIO_VALTYPE_ARRAY)
return (pdfioArrayGetSize(contents->value.array));
else
return (1);
}
//
// 'pdfioPageOpenStream()' - Open a content stream for a page.
//
pdfio_stream_t * // O - Stream
pdfioPageOpenStream(
pdfio_obj_t *page, // I - Page object
size_t n, // I - Stream index (0-based)
bool decode) // I - `true` to decode/decompress stream
{
_pdfio_value_t *contents = get_contents(page);
// Contents value
if (!contents)
return (NULL);
else if (contents->type == PDFIO_VALTYPE_ARRAY && n < pdfioArrayGetSize(contents->value.array))
return (pdfioObjOpenStream(pdfioArrayGetObj(contents->value.array, n), decode));
else if (n)
return (NULL);
else
return (pdfioObjOpenStream(pdfioFileFindObj(page->pdf, contents->value.indirect.number), decode));
}
//
// 'get_contents()' - Get a page's Contents value.
//
static _pdfio_value_t * // O - Value or NULL on error
get_contents(pdfio_obj_t *page) // I - Page object
{
// Range check input...
if (!page)
return (NULL);
// Load the page object as needed...
if (page->value.type == PDFIO_VALTYPE_NONE)
{
if (!_pdfioObjLoad(page))
return (NULL);
}
if (page->value.type != PDFIO_VALTYPE_DICT)
return (NULL);
return (_pdfioDictGetValue(page->value.value.dict, "Contents"));
}

View File

@ -1,7 +1,7 @@
// //
// Private header file for PDFio. // Private header file for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -9,41 +9,22 @@
#ifndef PDFIO_PRIVATE_H #ifndef PDFIO_PRIVATE_H
# define PDFIO_PRIVATE_H # define PDFIO_PRIVATE_H
//
// Include necessary headers...
//
# ifdef _WIN32 # ifdef _WIN32
/* # define _CRT_SECURE_NO_WARNINGS // Disable bogus VS warnings/errors...
* Disable bogus VS warnings/errors...
*/
# define _CRT_SECURE_NO_WARNINGS
# endif // _WIN32 # endif // _WIN32
# include "pdfio.h" # include "pdfio.h"
# include <stdarg.h> # include <stdarg.h>
# include <stdint.h> # include <stdint.h>
# include <string.h> # include <string.h>
# include <ctype.h>
# include <errno.h> # include <errno.h>
# include <inttypes.h> # include <inttypes.h>
# include <fcntl.h> # include <fcntl.h>
# ifdef _WIN32 # ifdef _WIN32
# include <io.h> # include <io.h>
# include <direct.h> # include <direct.h>
# include <windows.h> // GetTempPathA
/* # define access _access // Map standard POSIX/C99 names
* Microsoft renames the POSIX functions to _name, and introduces
* a broken compatibility layer using the original names. As a result,
* random crashes can occur when, for example, strdup() allocates memory
* from a different heap than used by malloc() and free().
*
* To avoid moronic problems like this, we #define the POSIX function
* names to the corresponding non-standard Microsoft names.
*/
# define access _access
# define close _close # define close _close
# define fileno _fileno # define fileno _fileno
# define lseek _lseek # define lseek _lseek
@ -56,25 +37,18 @@
# define unlink _unlink # define unlink _unlink
# define vsnprintf _vsnprintf # define vsnprintf _vsnprintf
# define write _write # define write _write
# define F_OK 00 // POSIX parameters/flags
/*
* Map various parameters for POSIX...
*/
# define F_OK 00
# define W_OK 02 # define W_OK 02
# define R_OK 04 # define R_OK 04
# define O_RDONLY _O_RDONLY # define O_RDONLY _O_RDONLY // Map standard POSIX open flags
# define O_WRONLY _O_WRONLY # define O_WRONLY _O_WRONLY
# define O_CREAT _O_CREAT # define O_CREAT _O_CREAT
# define O_TRUNC _O_TRUNC # define O_TRUNC _O_TRUNC
# define O_BINARY _O_BINARY # define O_BINARY _O_BINARY
# else // !_WIN32 # else // !_WIN32
# include <unistd.h> # include <unistd.h>
# define O_BINARY 0 # define O_BINARY 0 // Map Windows-specific open flag
# endif // _WIN32 # endif // _WIN32
# include <string.h>
# include <ctype.h>
# include <zlib.h> # include <zlib.h>
@ -118,6 +92,9 @@
# define PDFIO_MAX_DEPTH 32 // Maximum nesting depth for values # define PDFIO_MAX_DEPTH 32 // Maximum nesting depth for values
typedef void (*_pdfio_extfree_t)(void *);
// Extension data free function
typedef enum _pdfio_mode_e // Read/write mode typedef enum _pdfio_mode_e // Read/write mode
{ {
_PDFIO_MODE_READ, // Read a PDF file _PDFIO_MODE_READ, // Read a PDF file
@ -289,7 +266,8 @@ struct _pdfio_file_s // PDF file structure
pdfio_dict_t **dicts; // Dictionaries pdfio_dict_t **dicts; // Dictionaries
size_t num_objs, // Number of objects size_t num_objs, // Number of objects
alloc_objs; // Allocated objects alloc_objs; // Allocated objects
pdfio_obj_t **objs; // Objects pdfio_obj_t **objs, // Objects
*current_obj; // Current object being written/read
size_t num_objmaps, // Number of object maps size_t num_objmaps, // Number of object maps
alloc_objmaps; // Allocated object maps alloc_objmaps; // Allocated object maps
_pdfio_objmap_t *objmaps; // Object maps _pdfio_objmap_t *objmaps; // Object maps
@ -312,6 +290,8 @@ struct _pdfio_obj_s // Object
size_t stream_length; // Length of stream, if any size_t stream_length; // Length of stream, if any
_pdfio_value_t value; // Dictionary/number/etc. value _pdfio_value_t value; // Dictionary/number/etc. value
pdfio_stream_t *stream; // Open stream, if any pdfio_stream_t *stream; // Open stream, if any
void *data; // Extension data, if any
_pdfio_extfree_t datafree; // Free callback for extension data
}; };
struct _pdfio_stream_s // Stream struct _pdfio_stream_s // Stream
@ -390,7 +370,9 @@ extern off_t _pdfioFileTell(pdfio_file_t *pdf) _PDFIO_INTERNAL;
extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) _PDFIO_INTERNAL; extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) _PDFIO_INTERNAL;
extern void _pdfioObjDelete(pdfio_obj_t *obj) _PDFIO_INTERNAL; extern void _pdfioObjDelete(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern void *_pdfioObjGetExtension(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern bool _pdfioObjLoad(pdfio_obj_t *obj) _PDFIO_INTERNAL; extern bool _pdfioObjLoad(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern void _pdfioObjSetExtension(pdfio_obj_t *obj, void *data, _pdfio_extfree_t datafree) _PDFIO_INTERNAL;
extern pdfio_stream_t *_pdfioStreamCreate(pdfio_obj_t *obj, pdfio_obj_t *length_obj, pdfio_filter_t compression) _PDFIO_INTERNAL; extern pdfio_stream_t *_pdfioStreamCreate(pdfio_obj_t *obj, pdfio_obj_t *length_obj, pdfio_filter_t compression) _PDFIO_INTERNAL;
extern pdfio_stream_t *_pdfioStreamOpen(pdfio_obj_t *obj, bool decode) _PDFIO_INTERNAL; extern pdfio_stream_t *_pdfioStreamOpen(pdfio_obj_t *obj, bool decode) _PDFIO_INTERNAL;
@ -410,4 +392,5 @@ extern void _pdfioValueDelete(_pdfio_value_t *v) _PDFIO_INTERNAL;
extern _pdfio_value_t *_pdfioValueRead(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_token_t *ts, _pdfio_value_t *v, size_t depth) _PDFIO_INTERNAL; extern _pdfio_value_t *_pdfioValueRead(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_token_t *ts, _pdfio_value_t *v, size_t depth) _PDFIO_INTERNAL;
extern bool _pdfioValueWrite(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_value_t *v, off_t *length) _PDFIO_INTERNAL; extern bool _pdfioValueWrite(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_value_t *v, off_t *length) _PDFIO_INTERNAL;
#endif // !PDFIO_PRIVATE_H #endif // !PDFIO_PRIVATE_H

View File

@ -1,7 +1,7 @@
// //
// SHA-256 functions for PDFio. // SHA-256 functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// Copyright © 2011 IETF Trust and the persons identified as authors of the // Copyright © 2011 IETF Trust and the persons identified as authors of the
// code. All rights reserved. // code. All rights reserved.
// //
@ -217,7 +217,7 @@ _pdfioCryptoSHA256Append(_pdfio_sha256_t *context, const uint8_t *message_array,
*/ */
void void
_pdfioCryptoSHA256Finish(_pdfio_sha256_t *context, _pdfioCryptoSHA256Finish(_pdfio_sha256_t *context,
uint8_t Message_Digest[SHA256HashSize]) uint8_t *Message_Digest)
{ {
SHA224_256ResultN(context, Message_Digest, SHA256HashSize); SHA224_256ResultN(context, Message_Digest, SHA256HashSize);
} }

View File

@ -1,16 +1,12 @@
// //
// PDF stream functions for PDFio. // PDF stream functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -174,6 +170,8 @@ pdfioStreamClose(pdfio_stream_t *st) // I - Stream
done: done:
st->pdf->current_obj = NULL;
free(st->prbuffer); free(st->prbuffer);
free(st->psbuffer); free(st->psbuffer);
free(st); free(st);
@ -364,6 +362,13 @@ pdfioStreamConsume(pdfio_stream_t *st, // I - Stream
// //
// 'pdfioStreamGetToken()' - Read a single PDF token from a stream. // 'pdfioStreamGetToken()' - Read a single PDF token from a stream.
// //
// This function reads a single PDF token from a stream. Operator tokens,
// boolean values, and numbers are returned as-is in the provided string buffer.
// String values start with the opening parenthesis ('(') but have all escaping
// resolved and the terminating parenthesis removed. Hexadecimal string values
// start with the opening angle bracket ('<') and have all whitespace and the
// terminating angle bracket removed.
//
bool // O - `true` on success, `false` on EOF bool // O - `true` on success, `false` on EOF
pdfioStreamGetToken( pdfioStreamGetToken(
@ -372,6 +377,7 @@ pdfioStreamGetToken(
size_t bufsize) // I - Size of string buffer size_t bufsize) // I - Size of string buffer
{ {
_pdfio_token_t tb; // Token buffer/stack _pdfio_token_t tb; // Token buffer/stack
bool ret; // Return value
// Range check input... // Range check input...
@ -381,7 +387,10 @@ pdfioStreamGetToken(
// Read using the token engine... // Read using the token engine...
_pdfioTokenInit(&tb, st->pdf, (_pdfio_tconsume_cb_t)pdfioStreamConsume, (_pdfio_tpeek_cb_t)pdfioStreamPeek, st); _pdfioTokenInit(&tb, st->pdf, (_pdfio_tconsume_cb_t)pdfioStreamConsume, (_pdfio_tpeek_cb_t)pdfioStreamPeek, st);
return (_pdfioTokenRead(&tb, buffer, bufsize)); ret = _pdfioTokenRead(&tb, buffer, bufsize);
_pdfioTokenFlush(&tb);
return (ret);
} }
@ -439,6 +448,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
return (NULL); return (NULL);
} }
PDFIO_DEBUG("_pdfioStreamOpen: ivlen=%d\n", (int)ivlen);
if (ivlen > 0) if (ivlen > 0)
_pdfioFileConsume(st->pdf, ivlen); _pdfioFileConsume(st->pdf, ivlen);
@ -451,11 +461,19 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
// Try to decode/decompress the contents of this object... // Try to decode/decompress the contents of this object...
const char *filter = pdfioDictGetName(dict, "Filter"); const char *filter = pdfioDictGetName(dict, "Filter");
// Filter value // Filter value
pdfio_array_t *fa = pdfioDictGetArray(dict, "Filter");
// Filter array
if (!filter && fa && pdfioArrayGetSize(fa) == 1)
{
// Support single-valued arrays...
filter = pdfioArrayGetName(fa, 0);
}
if (!filter) if (!filter)
{ {
// No single filter name, do we have a compound filter? // No single filter name, do we have a compound filter?
if (pdfioDictGetArray(dict, "Filter")) if (fa)
{ {
// TODO: Implement compound filters... // TODO: Implement compound filters...
_pdfioFileError(st->pdf, "Unsupported compound stream filter."); _pdfioFileError(st->pdf, "Unsupported compound stream filter.");
@ -546,6 +564,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
else else
st->predictor = _PDFIO_PREDICTOR_NONE; st->predictor = _PDFIO_PREDICTOR_NONE;
PDFIO_DEBUG("_pdfioStreamOpen: pos=%ld\n", (long)_pdfioFileTell(st->pdf));
if (sizeof(st->cbuffer) > st->remaining) if (sizeof(st->cbuffer) > st->remaining)
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining); rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining);
else else
@ -566,12 +585,6 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
st->flate.next_in = (Bytef *)st->cbuffer; st->flate.next_in = (Bytef *)st->cbuffer;
st->flate.avail_in = (uInt)rbytes; st->flate.avail_in = (uInt)rbytes;
if (st->cbuffer[0] == 0x0a)
{
st->flate.next_in ++; // Skip newline
st->flate.avail_in --;
}
PDFIO_DEBUG("_pdfioStreamOpen: avail_in=%u, cbuffer=<%02X%02X%02X%02X%02X%02X%02X%02X...>\n", st->flate.avail_in, st->cbuffer[0], st->cbuffer[1], st->cbuffer[2], st->cbuffer[3], st->cbuffer[4], st->cbuffer[5], st->cbuffer[6], st->cbuffer[7]); PDFIO_DEBUG("_pdfioStreamOpen: avail_in=%u, cbuffer=<%02X%02X%02X%02X%02X%02X%02X%02X...>\n", st->flate.avail_in, st->cbuffer[0], st->cbuffer[1], st->cbuffer[2], st->cbuffer[3], st->cbuffer[4], st->cbuffer[5], st->cbuffer[6], st->cbuffer[7]);
if ((status = inflateInit(&(st->flate))) != Z_OK) if ((status = inflateInit(&(st->flate))) != Z_OK)
@ -995,6 +1008,7 @@ stream_read(pdfio_stream_t *st, // I - Stream
size_t bytes) // I - Number of bytes to read size_t bytes) // I - Number of bytes to read
{ {
ssize_t rbytes; // Bytes read ssize_t rbytes; // Bytes read
uInt avail_in, avail_out; // Previous flate values
if (st->filter == PDFIO_FILTER_NONE) if (st->filter == PDFIO_FILTER_NONE)
@ -1047,11 +1061,19 @@ stream_read(pdfio_stream_t *st, // I - Stream
st->flate.next_out = (Bytef *)buffer; st->flate.next_out = (Bytef *)buffer;
st->flate.avail_out = (uInt)bytes; 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) if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
{ {
_pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status)); _pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status));
return (-1); 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); return (st->flate.next_out - (Bytef *)buffer);
} }
@ -1100,12 +1122,15 @@ stream_read(pdfio_stream_t *st, // I - Stream
st->flate.avail_in = (uInt)rbytes; st->flate.avail_in = (uInt)rbytes;
} }
avail_in = st->flate.avail_in;
avail_out = st->flate.avail_out;
if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK) if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
{ {
_pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status)); _pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status));
return (-1); return (-1);
} }
else if (status == Z_STREAM_END) else if (status == Z_STREAM_END || (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out))
break; break;
} }
@ -1167,12 +1192,15 @@ stream_read(pdfio_stream_t *st, // I - Stream
st->flate.avail_in = (uInt)rbytes; st->flate.avail_in = (uInt)rbytes;
} }
avail_in = st->flate.avail_in;
avail_out = st->flate.avail_out;
if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK) if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
{ {
_pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status)); _pdfioFileError(st->pdf, "Unable to decompress stream data: %s", zstrerror(status));
return (-1); return (-1);
} }
else if (status == Z_STREAM_END) else if (status == Z_STREAM_END || (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out))
break; break;
} }

View File

@ -7,10 +7,6 @@
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"

View File

@ -1,16 +1,12 @@
// //
// PDF token parsing functions for PDFio. // PDF token parsing functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -129,9 +125,20 @@ _pdfioTokenGet(_pdfio_token_t *tb, // I - Token buffer/stack
if (tb->num_tokens > 0) if (tb->num_tokens > 0)
{ {
// Yes, return it... // Yes, return it...
size_t len; // Length of token
tb->num_tokens --; tb->num_tokens --;
strncpy(buffer, tb->tokens[tb->num_tokens], bufsize - 1);
buffer[bufsize - 1] = '\0'; if ((len = strlen(tb->tokens[tb->num_tokens])) > (bufsize - 1))
{
// Value too large...
PDFIO_DEBUG("_pdfioTokenGet(tb=%p, buffer=%p, bufsize=%u): Token '%s' from stack too large.\n", tb, buffer, (unsigned)bufsize, tb->tokens[tb->num_tokens]);
*buffer = '\0';
return (false);
}
memcpy(buffer, tb->tokens[tb->num_tokens], len);
buffer[len] = '\0';
PDFIO_DEBUG("_pdfioTokenGet(tb=%p, buffer=%p, bufsize=%u): Popping '%s' from stack.\n", tb, buffer, (unsigned)bufsize, buffer); PDFIO_DEBUG("_pdfioTokenGet(tb=%p, buffer=%p, bufsize=%u): Popping '%s' from stack.\n", tb, buffer, (unsigned)bufsize, buffer);
@ -484,6 +491,13 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
*bufptr++ = (char)ch; *bufptr++ = (char)ch;
break; break;
} }
else if (ch == '>')
{
// Issue #46: Empty hex string from Microsoft PDF generator; treat as
// empty literal string...
*buffer = '(';
break;
}
else if (!isspace(ch & 255) && !isxdigit(ch & 255)) else if (!isspace(ch & 255) && !isxdigit(ch & 255))
{ {
_pdfioFileError(tb->pdf, "Syntax error: '<%c'", ch); _pdfioFileError(tb->pdf, "Syntax error: '<%c'", ch);
@ -536,7 +550,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
*bufptr = '\0'; *bufptr = '\0';
PDFIO_DEBUG("_pdfioTokenRead: Read '%s'.\n", buffer); // PDFIO_DEBUG("_pdfioTokenRead: Read '%s'.\n", buffer);
return (bufptr > buffer); return (bufptr > buffer);
} }
@ -573,6 +587,7 @@ get_char(_pdfio_token_t *tb) // I - Token buffer
tb->bufptr = tb->buffer; tb->bufptr = tb->buffer;
tb->bufend = tb->buffer + bytes; tb->bufend = tb->buffer + bytes;
#if 0
#ifdef DEBUG #ifdef DEBUG
unsigned char *ptr; // Pointer into buffer unsigned char *ptr; // Pointer into buffer
@ -586,6 +601,7 @@ get_char(_pdfio_token_t *tb) // I - Token buffer
} }
PDFIO_DEBUG("'\n"); PDFIO_DEBUG("'\n");
#endif // DEBUG #endif // DEBUG
#endif // 0
} }
// Return the next character... // Return the next character...

View File

@ -1,16 +1,12 @@
// //
// PDF value functions for PDFio. // PDF value functions for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -219,7 +215,6 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
PDFIO_DEBUG("_pdfioValueRead(pdf=%p, obj=%p, v=%p)\n", pdf, obj, v); PDFIO_DEBUG("_pdfioValueRead(pdf=%p, obj=%p, v=%p)\n", pdf, obj, v);
(void)obj; // TODO: Implement decryption
if (!_pdfioTokenGet(tb, token, sizeof(token))) if (!_pdfioTokenGet(tb, token, sizeof(token)))
return (NULL); return (NULL);
@ -284,6 +279,7 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
} }
} }
} }
if (token[i]) if (token[i])
{ {
// Just a string... // Just a string...
@ -367,6 +363,36 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
*dataptr++ = (unsigned char)d; *dataptr++ = (unsigned char)d;
} }
if (obj && pdf->encryption)
{
// Decrypt the string...
_pdfio_crypto_ctx_t ctx; // Decryption context
_pdfio_crypto_cb_t cb; // Decryption callback
size_t ivlen; // Number of initialization vector bytes
uint8_t temp[32768]; // Temporary buffer for decryption
size_t templen; // Number of actual data bytes
if (v->value.binary.datalen > (sizeof(temp) - 32))
{
_pdfioFileError(pdf, "Unable to read encrypted binary string - too long.");
return (false);
}
ivlen = v->value.binary.datalen;
if ((cb = _pdfioCryptoMakeReader(pdf, obj, &ctx, v->value.binary.data, &ivlen)) == NULL)
return (false);
templen = (cb)(&ctx, temp, v->value.binary.data + ivlen, v->value.binary.datalen - ivlen);
// Copy the decrypted string back to the value and adjust the length...
memcpy(v->value.binary.data, temp, templen);
if (pdf->encryption >= PDFIO_ENCRYPTION_AES_128)
v->value.binary.datalen = templen - temp[templen - 1];
else
v->value.binary.datalen = templen;
}
} }
else if (strchr("0123456789-+.", token[0]) != NULL) else if (strchr("0123456789-+.", token[0]) != NULL)
{ {

34
pdfio.h
View File

@ -1,7 +1,7 @@
// //
// Public header file for PDFio. // Public header file for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -9,27 +9,23 @@
#ifndef PDFIO_H #ifndef PDFIO_H
# define PDFIO_H # define PDFIO_H
//
// Include necessary headers...
//
# include <stdio.h> # include <stdio.h>
# include <stdlib.h> # include <stdlib.h>
# include <stdbool.h> # include <stdbool.h>
# include <sys/types.h> # include <sys/types.h>
# include <time.h> # include <time.h>
//
// C++ magic...
//
# ifdef __cplusplus # ifdef __cplusplus
extern "C" { extern "C" {
# endif // __cplusplus # endif // __cplusplus
//
// Version number...
//
# define PDFIO_VERSION "1.2.0"
// //
// Visibility and other annotations... // Visibility and other annotations...
// //
@ -37,9 +33,11 @@ extern "C" {
# if defined(__has_extension) || defined(__GNUC__) # if defined(__has_extension) || defined(__GNUC__)
# define _PDFIO_PUBLIC __attribute__ ((visibility("default"))) # define _PDFIO_PUBLIC __attribute__ ((visibility("default")))
# define _PDFIO_FORMAT(a,b) __attribute__ ((__format__(__printf__, a,b))) # define _PDFIO_FORMAT(a,b) __attribute__ ((__format__(__printf__, a,b)))
# define _PDFIO_DEPRECATED __attribute__ ((deprecated)) _PDFIO_PUBLIC
# else # else
# define _PDFIO_PUBLIC # define _PDFIO_PUBLIC
# define _PDFIO_FORMAT(a,b) # define _PDFIO_FORMAT(a,b)
# define _PDFIO_DEPRECATED
# endif // __has_extension || __GNUC__ # endif // __has_extension || __GNUC__
@ -55,6 +53,8 @@ typedef struct _pdfio_array_s pdfio_array_t;
// Array of PDF values // Array of PDF values
typedef struct _pdfio_dict_s pdfio_dict_t; typedef struct _pdfio_dict_s pdfio_dict_t;
// Key/value dictionary // Key/value dictionary
typedef bool (*pdfio_dict_cb_t)(pdfio_dict_t *dict, const char *key, void *cb_data);
// Dictionary iterator callback
typedef struct _pdfio_file_s pdfio_file_t; typedef struct _pdfio_file_s pdfio_file_t;
// PDF file // PDF file
typedef bool (*pdfio_error_cb_t)(pdfio_file_t *pdf, const char *message, void *data); typedef bool (*pdfio_error_cb_t)(pdfio_file_t *pdf, const char *message, void *data);
@ -65,7 +65,7 @@ typedef enum pdfio_encryption_e // PDF encryption modes
PDFIO_ENCRYPTION_RC4_40, // 40-bit RC4 encryption (PDF 1.3) PDFIO_ENCRYPTION_RC4_40, // 40-bit RC4 encryption (PDF 1.3)
PDFIO_ENCRYPTION_RC4_128, // 128-bit RC4 encryption (PDF 1.4) PDFIO_ENCRYPTION_RC4_128, // 128-bit RC4 encryption (PDF 1.4)
PDFIO_ENCRYPTION_AES_128, // 128-bit AES encryption (PDF 1.6) PDFIO_ENCRYPTION_AES_128, // 128-bit AES encryption (PDF 1.6)
PDFIO_ENCRYPTION_AES_256 // 256-bit AES encryption (PDF 2.0) PDFIO_ENCRYPTION_AES_256 // 256-bit AES encryption (PDF 2.0) @exclude all@
} pdfio_encryption_t; } pdfio_encryption_t;
typedef enum pdfio_filter_e // Compression/decompression filters for streams typedef enum pdfio_filter_e // Compression/decompression filters for streams
{ {
@ -165,6 +165,7 @@ extern pdfio_obj_t *pdfioDictGetObj(pdfio_dict_t *dict, const char *key) _PDFIO_
extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC; extern pdfio_rect_t *pdfioDictGetRect(pdfio_dict_t *dict, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC;
extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern const char *pdfioDictGetString(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC; extern pdfio_valtype_t pdfioDictGetType(pdfio_dict_t *dict, const char *key) _PDFIO_PUBLIC;
extern void pdfioDictIterateKeys(pdfio_dict_t *dict, pdfio_dict_cb_t cb, void *cb_data) _PDFIO_PUBLIC;
extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC; extern bool pdfioDictSetArray(pdfio_dict_t *dict, const char *key, pdfio_array_t *value) _PDFIO_PUBLIC;
extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC; extern bool pdfioDictSetBinary(pdfio_dict_t *dict, const char *key, const unsigned char *value, size_t valuelen) _PDFIO_PUBLIC;
extern bool pdfioDictSetBoolean(pdfio_dict_t *dict, const char *key, bool value) _PDFIO_PUBLIC; extern bool pdfioDictSetBoolean(pdfio_dict_t *dict, const char *key, bool value) _PDFIO_PUBLIC;
@ -181,10 +182,13 @@ extern bool pdfioDictSetStringf(pdfio_dict_t *dict, const char *key, const char
extern bool pdfioFileClose(pdfio_file_t *pdf) _PDFIO_PUBLIC; 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_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 *pdfioFileCreateArrayObj(pdfio_file_t *pdf, pdfio_array_t *array) _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_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; 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;
// TODO: Add number, array, string, etc. versions of pdfioFileCreateObject? // TODO: Add number, array, string, etc. versions of pdfioFileCreateObject?
extern pdfio_stream_t *pdfioFileCreatePage(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC; extern pdfio_stream_t *pdfioFileCreatePage(pdfio_file_t *pdf, pdfio_dict_t *dict) _PDFIO_PUBLIC;
extern pdfio_obj_t *pdfioFileCreateStringObj(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC;
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 pdfio_obj_t *pdfioFileFindObj(pdfio_file_t *pdf, size_t number) _PDFIO_PUBLIC;
extern const char *pdfioFileGetAuthor(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern const char *pdfioFileGetAuthor(pdfio_file_t *pdf) _PDFIO_PUBLIC;
extern time_t pdfioFileGetCreationDate(pdfio_file_t *pdf) _PDFIO_PUBLIC; extern time_t pdfioFileGetCreationDate(pdfio_file_t *pdf) _PDFIO_PUBLIC;
@ -240,10 +244,6 @@ extern char *pdfioStringCreate(pdfio_file_t *pdf, const char *s) _PDFIO_PUBLIC
extern char *pdfioStringCreatef(pdfio_file_t *pdf, const char *format, ...) _PDFIO_FORMAT(2,3) _PDFIO_PUBLIC; extern char *pdfioStringCreatef(pdfio_file_t *pdf, const char *format, ...) _PDFIO_FORMAT(2,3) _PDFIO_PUBLIC;
//
// C++ magic...
//
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif // __cplusplus # endif // __cplusplus

View File

@ -1,6 +1,13 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: pdfio Name: pdfio
Description: PDF read/write library Description: PDF read/write library
Version: @PDFIO_VERSION@
URL: https://www.msweet.org/pdfio URL: https://www.msweet.org/pdfio
Requires: zlib >= 1.0 Requires: @PKGCONFIG_REQUIRES@
Libs: -L${prefix}/lib -lpdfio -lm Libs: @PKGCONFIG_LIBS@
Cflags: -I${prefix}/include Libs.private: @PKGCONFIG_LIBS_PRIVATE@ -lm
Cflags: @PKGCONFIG_CFLAGS@

View File

@ -87,7 +87,7 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>PDFIO_VERSION="1.0.0";WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>
@ -101,7 +101,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>PDFIO_VERSION="1.0.0";WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>
@ -115,7 +115,7 @@
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>PDFIO_VERSION="1.0.0";_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>
@ -130,7 +130,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>PDFIO_VERSION="1.0.0";NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
</ClCompile> </ClCompile>
<Link> <Link>

View File

@ -372,7 +372,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_IDENTITY = "Apple Development";
COPY_PHASE_STRIP = NO; COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1.0.0; CURRENT_PROJECT_VERSION = 1.1.2;
DEBUG_INFORMATION_FORMAT = dwarf; DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES; ENABLE_TESTABILITY = YES;
@ -381,7 +381,6 @@
GCC_PREPROCESSOR_DEFINITIONS = ( GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)", "$(inherited)",
"DEBUG=1", "DEBUG=1",
"'PDFIO_VERSION=\"$(CURRENT_PROJECT_VERSION)\"'",
); );
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@ -397,7 +396,7 @@
GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES; GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14; MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES; MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES; ONLY_ACTIVE_ARCH = YES;
@ -450,7 +449,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_IDENTITY = "Apple Development";
COPY_PHASE_STRIP = NO; COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1.0.0; CURRENT_PROJECT_VERSION = 1.1.2;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_HARDENED_RUNTIME = YES; ENABLE_HARDENED_RUNTIME = YES;
ENABLE_NS_ASSERTIONS = NO; ENABLE_NS_ASSERTIONS = NO;
@ -459,7 +458,6 @@
GCC_NO_COMMON_BLOCKS = YES; GCC_NO_COMMON_BLOCKS = YES;
GCC_PREPROCESSOR_DEFINITIONS = ( GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)", "$(inherited)",
"'PDFIO_VERSION=\"$(CURRENT_PROJECT_VERSION)\"'",
); );
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@ -475,7 +473,7 @@
GCC_WARN_UNUSED_LABEL = YES; GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES; GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14; MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO; MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES; MTL_FAST_MATH = YES;
RUN_CLANG_STATIC_ANALYZER = YES; RUN_CLANG_STATIC_ANALYZER = YES;
@ -515,7 +513,7 @@
ENABLE_HARDENED_RUNTIME = YES; ENABLE_HARDENED_RUNTIME = YES;
GCC_DYNAMIC_NO_PIC = NO; GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0; GCC_OPTIMIZATION_LEVEL = 0;
MACOSX_DEPLOYMENT_TARGET = 10.14; MACOSX_DEPLOYMENT_TARGET = 11.0;
PRODUCT_BUNDLE_IDENTIFIER = org.msweet.testpdfio; PRODUCT_BUNDLE_IDENTIFIER = org.msweet.testpdfio;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";
@ -529,7 +527,7 @@
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = ""; DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES; ENABLE_HARDENED_RUNTIME = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14; MACOSX_DEPLOYMENT_TARGET = 11.0;
PRODUCT_BUNDLE_IDENTIFIER = org.msweet.testpdfio; PRODUCT_BUNDLE_IDENTIFIER = org.msweet.testpdfio;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = ""; PROVISIONING_PROFILE_SPECIFIER = "";

View File

@ -1,5 +1,5 @@
LIBRARY pdfio1 LIBRARY pdfio1
VERSION 1.0 VERSION 1.2
EXPORTS EXPORTS
_pdfioArrayDebug _pdfioArrayDebug
_pdfioArrayDelete _pdfioArrayDelete
@ -47,7 +47,9 @@ _pdfioFileSeek
_pdfioFileTell _pdfioFileTell
_pdfioFileWrite _pdfioFileWrite
_pdfioObjDelete _pdfioObjDelete
_pdfioObjGetExtension
_pdfioObjLoad _pdfioObjLoad
_pdfioObjSetExtension
_pdfioStreamCreate _pdfioStreamCreate
_pdfioStreamOpen _pdfioStreamOpen
_pdfioStringIsAllocated _pdfioStringIsAllocated
@ -101,6 +103,7 @@ pdfioContentPathClose
pdfioContentPathCurve pdfioContentPathCurve
pdfioContentPathCurve13 pdfioContentPathCurve13
pdfioContentPathCurve23 pdfioContentPathCurve23
pdfioContentPathEnd
pdfioContentPathLineTo pdfioContentPathLineTo
pdfioContentPathMoveTo pdfioContentPathMoveTo
pdfioContentPathRect pdfioContentPathRect
@ -135,8 +138,12 @@ pdfioContentSetTextXScaling
pdfioContentStroke pdfioContentStroke
pdfioContentTextBegin pdfioContentTextBegin
pdfioContentTextEnd pdfioContentTextEnd
pdfioContentTextMeasure
pdfioContentTextMoveLine pdfioContentTextMoveLine
pdfioContentTextMoveTo pdfioContentTextMoveTo
pdfioContentTextNewLine
pdfioContentTextNewLineShow
pdfioContentTextNewLineShowf
pdfioContentTextNextLine pdfioContentTextNextLine
pdfioContentTextShow pdfioContentTextShow
pdfioContentTextShowJustified pdfioContentTextShowJustified
@ -154,6 +161,7 @@ pdfioDictGetObj
pdfioDictGetRect pdfioDictGetRect
pdfioDictGetString pdfioDictGetString
pdfioDictGetType pdfioDictGetType
pdfioDictIterateKeys
pdfioDictSetArray pdfioDictSetArray
pdfioDictSetBinary pdfioDictSetBinary
pdfioDictSetBoolean pdfioDictSetBoolean
@ -174,9 +182,12 @@ pdfioFileCreateFontObjFromFile
pdfioFileCreateICCObjFromFile pdfioFileCreateICCObjFromFile
pdfioFileCreateImageObjFromData pdfioFileCreateImageObjFromData
pdfioFileCreateImageObjFromFile pdfioFileCreateImageObjFromFile
pdfioFileCreateNumberObj
pdfioFileCreateObj pdfioFileCreateObj
pdfioFileCreateOutput pdfioFileCreateOutput
pdfioFileCreatePage pdfioFileCreatePage
pdfioFileCreateStringObj
pdfioFileCreateTemporary
pdfioFileFindObj pdfioFileFindObj
pdfioFileGetAuthor pdfioFileGetAuthor
pdfioFileGetCreationDate pdfioFileGetCreationDate
@ -219,6 +230,8 @@ pdfioPageCopy
pdfioPageDictAddColorSpace pdfioPageDictAddColorSpace
pdfioPageDictAddFont pdfioPageDictAddFont
pdfioPageDictAddImage pdfioPageDictAddImage
pdfioPageGetNumStreams
pdfioPageOpenStream
pdfioStreamClose pdfioStreamClose
pdfioStreamConsume pdfioStreamConsume
pdfioStreamGetToken pdfioStreamGetToken

View File

@ -3,7 +3,7 @@
<metadata> <metadata>
<id>pdfio_native</id> <id>pdfio_native</id>
<title>PDFio Library for VS2019+</title> <title>PDFio Library for VS2019+</title>
<version>1.0.0</version> <version>1.2.0</version>
<authors>Michael R Sweet</authors> <authors>Michael R Sweet</authors>
<owners>michaelrsweet</owners> <owners>michaelrsweet</owners>
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl> <projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
@ -12,11 +12,11 @@
<readme>build/native/README.md</readme> <readme>build/native/README.md</readme>
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>PDFio Library for VS2019+</description> <description>PDFio Library for VS2019+</description>
<summary>PDFio is a simple C library for reading and writing PDF files. PDFio is licensed under the Apache License Version 2.0 with an exception to allow linking against GNU GPL2-only software.</summary> <summary>PDFio is a simple C library for reading and writing PDF files. PDFio is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GNU GPL2-only software.</summary>
<copyright>Copyright © 2019-2021 by Michael R Sweet</copyright> <copyright>Copyright © 2019-2023 by Michael R Sweet</copyright>
<tags>pdf file native</tags> <tags>pdf file native</tags>
<dependencies> <dependencies>
<dependency id="pdfio_native.redist" version="1.0.0" /> <dependency id="pdfio_native.redist" version="1.2.0" />
<dependency id="zlib_native.redist" version="1.2.11" /> <dependency id="zlib_native.redist" version="1.2.11" />
</dependencies> </dependencies>
</metadata> </metadata>

View File

@ -3,7 +3,7 @@
<metadata> <metadata>
<id>pdfio_native.redist</id> <id>pdfio_native.redist</id>
<title>PDFio Library for VS2019+</title> <title>PDFio Library for VS2019+</title>
<version>1.0.0</version> <version>1.2.0</version>
<authors>Michael R Sweet</authors> <authors>Michael R Sweet</authors>
<owners>michaelrsweet</owners> <owners>michaelrsweet</owners>
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl> <projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
@ -12,9 +12,12 @@
<readme>build/native/README.md</readme> <readme>build/native/README.md</readme>
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>PDFio Library for VS2019+</description> <description>PDFio Library for VS2019+</description>
<summary>PDFio is a simple C library for reading and writing PDF files. This package provides the redistributable content for the PDFio library. PDFio is licensed under the Apache License Version 2.0 with an exception to allow linking against GNU GPL2-only software.</summary> <summary>PDFio is a simple C library for reading and writing PDF files. This package provides the redistributable content for the PDFio library. PDFio is licensed under the Apache License Version 2.0 with an (optional) exception to allow linking against GNU GPL2-only software.</summary>
<copyright>Copyright © 2019-2021 by Michael R Sweet</copyright> <copyright>Copyright © 2019-2023 by Michael R Sweet</copyright>
<tags>pdf file native</tags> <tags>pdf file native</tags>
<dependencies>
<dependency id="zlib_native.redist" version="1.2.11" />
</dependencies>
</metadata> </metadata>
<files> <files>
<file src="doc\pdfio-128.png" target="build\native" /> <file src="doc\pdfio-128.png" target="build\native" />

95
pdfiototext.c Normal file
View File

@ -0,0 +1,95 @@
//
// PDF to text program for PDFio.
//
// Copyright © 2022 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
// Usage:
//
// ./pdfiototext FILENAME.pdf > FILENAME.txt
//
#include "pdfio.h"
#include <string.h>
//
// 'main()' - Main entry.
//
int // O - Exit status
main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments
{
pdfio_file_t *file; // PDF file
size_t i, j, // Looping vars
num_pages, // Number of pages
num_streams; // Number of streams for page
pdfio_obj_t *obj; // Current page object
pdfio_stream_t *st; // Current page content stream
char buffer[1024]; // String buffer
bool first; // First string token?
// Verify command-line arguments...
if (argc != 2)
{
puts("Usage: pdfiototext FILENAME.pdf > FILENAME.txt");
return (1);
}
// Open the PDF file...
if ((file = pdfioFileOpen(argv[1], NULL, NULL, NULL, NULL)) == NULL)
return (1);
// printf("%s: %u pages\n", argv[1], (unsigned)pdfioFileGetNumPages(file));
// Try grabbing content from all of the pages...
for (i = 0, num_pages = pdfioFileGetNumPages(file); i < num_pages; i ++)
{
if ((obj = pdfioFileGetPage(file, i)) == NULL)
continue;
num_streams = pdfioPageGetNumStreams(obj);
// printf("%s: page%u=%p, num_streams=%u\n", argv[1], (unsigned)i, obj, (unsigned)num_streams);
for (j = 0; j < num_streams; j ++)
{
if ((st = pdfioPageOpenStream(obj, j, true)) == NULL)
continue;
// printf("%s: page%u st%u=%p\n", argv[1], (unsigned)i, (unsigned)j, st);
first = true;
while (pdfioStreamGetToken(st, buffer, sizeof(buffer)))
{
if (buffer[0] == '(')
{
if (first)
first = false;
else
putchar(' ');
fputs(buffer + 1, stdout);
}
else if (!strcmp(buffer, "Td") || !strcmp(buffer, "TD") || !strcmp(buffer, "T*") || !strcmp(buffer, "\'") || !strcmp(buffer, "\""))
{
putchar('\n');
first = true;
}
}
if (!first)
putchar('\n');
pdfioStreamClose(st);
}
}
pdfioFileClose(file);
return (0);
}

View File

@ -1,7 +1,7 @@
// //
// Test program for PDFio. // Test program for PDFio.
// //
// Copyright © 2021 by Michael R Sweet. // Copyright © 2021-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -10,11 +10,7 @@
// //
// ./testpdfio // ./testpdfio
// //
// ./testpdfio FILENAME [OBJECT-NUMBER] [FILENAME [OBJECT-NUMBER]] ... // ./testpdfio [--verbose] FILENAME [OBJECT-NUMBER] [FILENAME [OBJECT-NUMBER]] ...
//
//
// Include necessary headers...
// //
#include "pdfio-private.h" #include "pdfio-private.h"
@ -34,6 +30,7 @@ static int do_test_file(const char *filename, int objnum, bool verbose);
static int do_unit_tests(void); 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 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); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
static bool iterate_cb(pdfio_dict_t *dict, const char *key, void *cb_data);
static ssize_t output_cb(int *fd, const void *buffer, size_t bytes); static ssize_t output_cb(int *fd, const void *buffer, size_t bytes);
static const char *password_cb(void *data, const char *filename); 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 int read_unit_file(const char *filename, size_t num_pages, size_t first_image, bool is_output);
@ -43,7 +40,7 @@ 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_alpha_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
static int write_color_patch(pdfio_stream_t *st, bool device); static int write_color_patch(pdfio_stream_t *st, bool device);
static int write_color_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font); static int write_color_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
static int write_font_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font, bool unicode); static int write_font_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font, const char *textfontfile, bool unicode);
static int write_header_footer(pdfio_stream_t *st, const char *title, int number); static int write_header_footer(pdfio_stream_t *st, const char *title, int number);
static pdfio_obj_t *write_image_object(pdfio_file_t *pdf, _pdfio_predictor_t predictor); static pdfio_obj_t *write_image_object(pdfio_file_t *pdf, _pdfio_predictor_t predictor);
static int write_images_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font); static int write_images_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
@ -405,7 +402,7 @@ do_test_file(const char *filename, // I - PDF filename
filter = pdfioDictGetName(dict, "Filter"); filter = pdfioDictGetName(dict, "Filter");
if ((st = pdfioObjOpenStream(obj, (filter && !strcmp(filter, "FlateDecode")) ? PDFIO_FILTER_FLATE : PDFIO_FILTER_NONE)) == NULL) if ((st = pdfioObjOpenStream(obj, filter && !strcmp(filter, "FlateDecode"))) == NULL)
{ {
_pdfioValueDebug(&obj->value, stdout); _pdfioValueDebug(&obj->value, stdout);
putchar('\n'); putchar('\n');
@ -454,7 +451,7 @@ do_test_file(const char *filename, // I - PDF filename
} }
} }
printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2); printf(" Page #%d (obj %d) is %gx%g.\n", (int)n + 1, (int)pdfioObjGetNumber(obj), media_box.x2, media_box.y2);
} }
} }
@ -506,6 +503,9 @@ do_unit_tests(void)
_pdfio_value_t value; // Value _pdfio_value_t value; // Value
size_t first_image, // First image object size_t first_image, // First image object
num_pages; // Number of pages written num_pages; // Number of pages written
char temppdf[1024]; // Temporary PDF file
pdfio_dict_t *dict; // Test dictionary
int count = 0; // Number of key/value pairs
static const char *complex_dict = // Complex dictionary value static const char *complex_dict = // Complex dictionary value
"<</Annots 5457 0 R/Contents 5469 0 R/CropBox[0 0 595.4 842]/Group 725 0 R" "<</Annots 5457 0 R/Contents 5469 0 R/CropBox[0 0 595.4 842]/Group 725 0 R"
"/MediaBox[0 0 595.4 842]/Parent 23513 0 R/Resources<</ColorSpace<<" "/MediaBox[0 0 595.4 842]/Parent 23513 0 R/Resources<</ColorSpace<<"
@ -967,6 +967,41 @@ do_unit_tests(void)
// TODO: Test for known values in this test file. // TODO: Test for known values in this test file.
// Test dictionary APIs
fputs("pdfioDictCreate: ", stdout);
if ((dict = pdfioDictCreate(inpdf)) != NULL)
{
puts("PASS");
fputs("pdfioDictSet*: ", stdout);
if (pdfioDictSetBoolean(dict, "Boolean", true) && pdfioDictSetName(dict, "Name", "Name") && pdfioDictSetNumber(dict, "Number", 42.0) && pdfioDictSetString(dict, "String", "String"))
{
puts("PASS");
}
else
{
puts("FAIL");
return (1);
}
fputs("pdfioDictIterateKeys: ", stdout);
pdfioDictIterateKeys(dict, iterate_cb, &count);
if (count == 4)
{
puts("PASS");
}
else
{
printf("FAIL (got %d, expected 4)\n", count);
return (1);
}
}
else
{
puts("FAIL");
return (1);
}
// Test the value parsers for edge cases... // Test the value parsers for edge cases...
fputs("_pdfioValueRead(complex_dict): ", stdout); fputs("_pdfioValueRead(complex_dict): ", stdout);
s = complex_dict; s = complex_dict;
@ -1107,6 +1142,18 @@ do_unit_tests(void)
if (read_unit_file("testpdfio-aesp.pdf", num_pages, first_image, false)) if (read_unit_file("testpdfio-aesp.pdf", num_pages, first_image, false))
return (1); return (1);
fputs("pdfioFileCreateTemporary: ", stdout);
if ((outpdf = pdfioFileCreateTemporary(temppdf, sizeof(temppdf), NULL, NULL, NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
printf("PASS (%s)\n", temppdf);
else
return (1);
if (write_unit_file(inpdf, outpdf, &num_pages, &first_image))
return (1);
if (read_unit_file(temppdf, num_pages, first_image, false))
return (1);
pdfioFileClose(inpdf); pdfioFileClose(inpdf);
return (0); return (0);
@ -1199,6 +1246,27 @@ error_cb(pdfio_file_t *pdf, // I - PDF file
} }
//
// 'iterate_cb()' - Test pdfioDictIterateKeys function.
//
static bool // O - `true` to continue, `false` to stop
iterate_cb(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
void *cb_data) // I - Callback data
{
int *count = (int *)cb_data; // Pointer to counter
if (!dict || !key || !cb_data)
return (false);
(*count)++;
return (true);
}
// //
// 'output_cb()' - Write output to a file. // 'output_cb()' - Write output to a file.
// //
@ -1238,6 +1306,7 @@ read_unit_file(const char *filename, // I - File to read
{ {
pdfio_file_t *pdf; // PDF file pdfio_file_t *pdf; // PDF file
size_t i; // Looping var size_t i; // Looping var
const char *s; // String
bool error = false; // Error callback data bool error = false; // Error callback data
@ -1248,6 +1317,87 @@ read_unit_file(const char *filename, // I - File to read
else else
return (1); return (1);
// Verify metadata...
fputs("pdfioFileGetAuthor: ", stdout);
if ((s = pdfioFileGetAuthor(pdf)) != NULL && !strcmp(s, "Michael R Sweet"))
{
puts("PASS");
}
else if (s)
{
printf("FAIL (got '%s', expected 'Michael R Sweet')\n", s);
return (1);
}
else
{
puts("FAIL (got NULL, expected 'Michael R Sweet')");
return (1);
}
fputs("pdfioFileGetCreator: ", stdout);
if ((s = pdfioFileGetCreator(pdf)) != NULL && !strcmp(s, "testpdfio"))
{
puts("PASS");
}
else if (s)
{
printf("FAIL (got '%s', expected 'testpdfio')\n", s);
return (1);
}
else
{
puts("FAIL (got NULL, expected 'testpdfio')");
return (1);
}
fputs("pdfioFileGetKeywords: ", stdout);
if ((s = pdfioFileGetKeywords(pdf)) != NULL && !strcmp(s, "one fish,two fish,red fish,blue fish"))
{
puts("PASS");
}
else if (s)
{
printf("FAIL (got '%s', expected 'one fish,two fish,red fish,blue fish')\n", s);
return (1);
}
else
{
puts("FAIL (got NULL, expected 'one fish,two fish,red fish,blue fish')");
return (1);
}
fputs("pdfioFileGetSubject: ", stdout);
if ((s = pdfioFileGetSubject(pdf)) != NULL && !strcmp(s, "Unit test document"))
{
puts("PASS");
}
else if (s)
{
printf("FAIL (got '%s', expected 'Unit test document')\n", s);
return (1);
}
else
{
puts("FAIL (got NULL, expected 'Unit test document')");
return (1);
}
fputs("pdfioFileGetTitle: ", stdout);
if ((s = pdfioFileGetTitle(pdf)) != NULL && !strcmp(s, "Test Document"))
{
puts("PASS");
}
else if (s)
{
printf("FAIL (got '%s', expected 'Test Document')\n", s);
return (1);
}
else
{
puts("FAIL (got NULL, expected 'Test Document')");
return (1);
}
// Verify the number of pages is the same... // Verify the number of pages is the same...
fputs("pdfioFileGetNumPages: ", stdout); fputs("pdfioFileGetNumPages: ", stdout);
if (num_pages == pdfioFileGetNumPages(pdf)) if (num_pages == pdfioFileGetNumPages(pdf))
@ -2075,14 +2225,20 @@ write_color_test(pdfio_file_t *pdf, // I - PDF file
// //
static int // O - 1 on failure, 0 on success static int // O - 1 on failure, 0 on success
write_font_test(pdfio_file_t *pdf, // I - PDF file write_font_test(
int number, // I - Page number pdfio_file_t *pdf, // I - PDF file
pdfio_obj_t *font, // I - Page number font int number, // I - Page number
bool unicode) // I - Use Unicode font? pdfio_obj_t *font, // I - Page number font
const char *textfontfile, // I - Text font file
bool unicode) // I - Use Unicode font?
{ {
pdfio_dict_t *dict; // Page dictionary pdfio_dict_t *dict; // Page dictionary
pdfio_stream_t *st; // Page contents stream pdfio_stream_t *st; // Page contents stream
pdfio_obj_t *opensans; // OpenSans-Regular font pdfio_obj_t *textfont; // Text font
char title[256], // Page title
textname[256], // Name of text font
*ptr; // Pointer into name
double width; // Text width
int i; // Looping var int i; // Looping var
static const char * const welcomes[] =// "Welcome" in many languages static const char * const welcomes[] =// "Welcome" in many languages
{ {
@ -2233,28 +2389,16 @@ write_font_test(pdfio_file_t *pdf, // I - PDF file
"Märr-ŋamathirri", "Märr-ŋamathirri",
"Ẹ ku abọ", "Ẹ ku abọ",
"Kíimak 'oolal", "Kíimak 'oolal",
"Ngiyakwemukela" "Ngiyakwemukela",
"いらっしゃいませ"
}; };
#if 0 printf("pdfioFileCreateFontObjFromFile(%s): ", textfontfile);
if (unicode) if ((textfont = pdfioFileCreateFontObjFromFile(pdf, textfontfile, unicode)) != NULL)
{ puts("PASS");
fputs("pdfioFileCreateFontObjFromFile(NotoSansJP-Regular.otf): ", stdout);
if ((opensans = pdfioFileCreateFontObjFromFile(pdf, "testfiles/NotoSansJP-Regular.otf", true)) != NULL)
puts("PASS");
else
return (1);
}
else else
#endif // 0 return (1);
{
fputs("pdfioFileCreateFontObjFromFile(OpenSans-Regular.ttf): ", stdout);
if ((opensans = pdfioFileCreateFontObjFromFile(pdf, "testfiles/OpenSans-Regular.ttf", unicode)) != NULL)
puts("PASS");
else
return (1);
}
fputs("pdfioDictCreate: ", stdout); fputs("pdfioDictCreate: ", stdout);
if ((dict = pdfioDictCreate(pdf)) != NULL) if ((dict = pdfioDictCreate(pdf)) != NULL)
@ -2269,7 +2413,7 @@ write_font_test(pdfio_file_t *pdf, // I - PDF file
return (1); return (1);
fputs("pdfioPageDictAddFont(F2): ", stdout); fputs("pdfioPageDictAddFont(F2): ", stdout);
if (pdfioPageDictAddFont(dict, "F2", opensans)) if (pdfioPageDictAddFont(dict, "F2", textfont))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -2281,7 +2425,21 @@ write_font_test(pdfio_file_t *pdf, // I - PDF file
else else
return (1); return (1);
if (write_header_footer(st, unicode ? "Unicode TrueType Font Test" : "CP1252 TrueType Font Test", number)) if ((ptr = strrchr(textfontfile, '/')) != NULL)
strncpy(textname, ptr + 1, sizeof(textname) - 1);
else
strncpy(textname, textfontfile, sizeof(textname) - 1);
textname[sizeof(textname) - 1] = '\0';
if ((ptr = strrchr(textname, '.')) != NULL)
*ptr = '\0';
if (unicode)
snprintf(title, sizeof(title), "Unicode %s Font Test", textname);
else
snprintf(title, sizeof(title), "CP1252 %s Font Test", textname);
if (write_header_footer(st, title, number))
goto error; goto error;
fputs("pdfioContentTextBegin(): ", stdout); fputs("pdfioContentTextBegin(): ", stdout);
@ -2302,8 +2460,8 @@ write_font_test(pdfio_file_t *pdf, // I - PDF file
else else
goto error; goto error;
fputs("pdfioContentTextMoveTo(36.0, 702.0): ", stdout); fputs("pdfioContentTextMoveTo(198.0, 702.0): ", stdout);
if (pdfioContentTextMoveTo(st, 36.0, 702.0)) if (pdfioContentTextMoveTo(st, 198.0, 702.0))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -2312,18 +2470,36 @@ write_font_test(pdfio_file_t *pdf, // I - PDF file
{ {
if (i > 0 && (i % 50) == 0) if (i > 0 && (i % 50) == 0)
{ {
fputs("pdfioContentTextMoveTo(200.0, 600.0): ", stdout); fputs("pdfioContentTextMoveTo(162.0, 600.0): ", stdout);
if (pdfioContentTextMoveTo(st, 200.0, 600.0)) if (pdfioContentTextMoveTo(st, 162.0, 600.0))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
} }
printf("pdfioContentTextMeasure(\"%s\"): ", welcomes[i]);
if ((width = pdfioContentTextMeasure(textfont, welcomes[i], 10.0)) >= 0.0)
puts("PASS");
else
return (1);
printf("pdfioContextTextMoveTo(%g, 0.0): ", -width);
if (pdfioContentTextMoveTo(st, -width, 0.0))
puts("PASS");
else
return (1);
printf("pdfioContentTextShowf(\"%s\"): ", welcomes[i]); printf("pdfioContentTextShowf(\"%s\"): ", welcomes[i]);
if (pdfioContentTextShowf(st, unicode, "%s\n", welcomes[i])) if (pdfioContentTextShowf(st, unicode, "%s\n", welcomes[i]))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
printf("pdfioContextTextMoveTo(%g, 0.0): ", width);
if (pdfioContentTextMoveTo(st, width, 0.0))
puts("PASS");
else
return (1);
} }
fputs("pdfioContentTextEnd(): ", stdout); fputs("pdfioContentTextEnd(): ", stdout);
@ -3167,13 +3343,13 @@ write_unit_file(
// Create some image objects... // Create some image objects...
fputs("pdfioFileCreateImageObjFromFile(\"testfiles/color.jpg\"): ", stdout); fputs("pdfioFileCreateImageObjFromFile(\"testfiles/color.jpg\"): ", stdout);
if ((color_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/color.jpg", true)) != NULL) if ((color_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/color.jpg", true)) != NULL)
puts("PASS"); printf("PASS (%u)\n", (unsigned)pdfioObjGetNumber(color_jpg));
else else
return (1); return (1);
fputs("pdfioFileCreateImageObjFromFile(\"testfiles/gray.jpg\"): ", stdout); fputs("pdfioFileCreateImageObjFromFile(\"testfiles/gray.jpg\"): ", stdout);
if ((gray_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/gray.jpg", true)) != NULL) if ((gray_jpg = pdfioFileCreateImageObjFromFile(outpdf, "testfiles/gray.jpg", true)) != NULL)
puts("PASS"); printf("PASS (%u)\n", (unsigned)pdfioObjGetNumber(gray_jpg));
else else
return (1); return (1);
@ -3236,14 +3412,17 @@ write_unit_file(
return (1); return (1);
// Test TrueType fonts... // Test TrueType fonts...
if (write_font_test(outpdf, 9, helvetica, false)) if (write_font_test(outpdf, 9, helvetica, "testfiles/OpenSans-Regular.ttf", false))
return (1); return (1);
if (write_font_test(outpdf, 10, helvetica, true)) if (write_font_test(outpdf, 10, helvetica, "testfiles/OpenSans-Regular.ttf", true))
return (1);
if (write_font_test(outpdf, 11, helvetica, "testfiles/NotoSansJP-Regular.otf", true))
return (1); return (1);
// Print this text file... // Print this text file...
if (write_text_test(outpdf, 11, helvetica, "README.md")) if (write_text_test(outpdf, 12, helvetica, "README.md"))
return (1); return (1);
fputs("pdfioFileGetNumPages: ", stdout); fputs("pdfioFileGetNumPages: ", stdout);

304
testttf.c Normal file
View File

@ -0,0 +1,304 @@
//
// Unit test program for TTF library
//
// https://github.com/michaelrsweet/ttf
//
// Copyright © 2018-2023 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
// Usage:
//
// ./testttf [FILENAME]
//
#include <stdio.h>
#include "ttf.h"
//
// Local functions...
//
static void error_cb(void *data, const char *message);
static int test_font(const char *filename);
//
// 'main()' - Main entry for unit tests.
//
int // O - Exit status
main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments
{
int i; // Looping var
int errors = 0; // Number of errors
if (argc > 1)
{
for (i = 1; i < argc; i ++)
errors += test_font(argv[i]);
}
else
{
// Test with the bundled TrueType files...
errors += test_font("testfiles/OpenSans-Bold.ttf");
errors += test_font("testfiles/OpenSans-Regular.ttf");
errors += test_font("testfiles/NotoSansJP-Regular.otf");
}
if (!errors)
puts("\nALL TESTS PASSED");
else
printf("\n%d TEST(S) FAILED\n", errors);
return (errors);
}
//
// 'error_cb()' - Error callback.
//
static void
error_cb(void *data, // I - User data (not used)
const char *message) // I - Message string
{
fprintf(stderr, "FAIL (%s)\n", message);
}
//
// 'test_font()' - Test a font file.
//
static int // O - Number of errors
test_font(const char *filename) // I - Font filename
{
int i, // Looping var
errors = 0; // Number of errors
ttf_t *font; // Font
const char *value; // Font (string) value
int intvalue; // Font (integer) value
float realvalue; // Font (real) value
ttf_rect_t bounds; // Bounds
ttf_rect_t extents; // Extents
size_t num_fonts; // Number of fonts
ttf_style_t style; // Font style
ttf_weight_t weight; // Font weight
static const char * const stretches[] =
{ // Font stretch strings
"TTF_STRETCH_NORMAL", // normal
"TTF_STRETCH_ULTRA_CONDENSED", // ultra-condensed
"TTF_STRETCH_EXTRA_CONDENSED", // extra-condensed
"TTF_STRETCH_CONDENSED", // condensed
"TTF_STRETCH_SEMI_CONDENSED", // semi-condensed
"TTF_STRETCH_SEMI_EXPANDED", // semi-expanded
"TTF_STRETCH_EXPANDED", // expanded
"TTF_STRETCH_EXTRA_EXPANDED", // extra-expanded
"TTF_STRETCH_ULTRA_EXPANDED" // ultra-expanded
};
static const char * const strings[] = // Test strings
{
"Hello, World!", // English
"مرحبا بالعالم!", // Arabic
"Bonjour le monde!", // French
"Γειά σου Κόσμε!", // Greek
"שלום עולם!", // Hebrew
"Привет мир!", // Russian
"こんにちは世界!" // Japanese
};
static const char * const styles[] = // Font style names
{
"TTF_STYLE_NORMAL",
"TTF_STYLE_ITALIC",
"TTF_STYLE_OBLIQUE"
};
printf("ttfCreate(\"%s\"): ", filename);
if ((font = ttfCreate(filename, 0, error_cb, NULL)) != NULL)
puts("PASS");
else
errors ++;
fputs("ttfGetAscent: ", stdout);
if ((intvalue = ttfGetAscent(font)) > 0)
{
printf("PASS (%d)\n", intvalue);
}
else
{
printf("FAIL (%d)\n", intvalue);
errors ++;
}
fputs("ttfGetBounds: ", stdout);
if (ttfGetBounds(font, &bounds))
{
printf("PASS (%g %g %g %g)\n", bounds.left, bounds.bottom, bounds.right, bounds.top);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetCapHeight: ", stdout);
if ((intvalue = ttfGetCapHeight(font)) > 0)
{
printf("PASS (%d)\n", intvalue);
}
else
{
printf("FAIL (%d)\n", intvalue);
errors ++;
}
fputs("ttfGetCopyright: ", stdout);
if ((value = ttfGetCopyright(font)) != NULL)
{
printf("PASS (%s)\n", value);
}
else
{
puts("WARNING (no copyright found)");
}
for (i = 0; i < (int)(sizeof(strings) / sizeof(strings[0])); i ++)
{
printf("ttfGetExtents(\"%s\"): ", strings[i]);
if (ttfGetExtents(font, 12.0f, strings[i], &extents))
{
printf("PASS (%.1f %.1f %.1f %.1f)\n", extents.left, extents.bottom, extents.right, extents.top);
}
else
{
puts("FAIL");
errors ++;
}
}
fputs("ttfGetFamily: ", stdout);
if ((value = ttfGetFamily(font)) != NULL)
{
printf("PASS (%s)\n", value);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetItalicAngle: ", stdout);
if ((realvalue = ttfGetItalicAngle(font)) >= -180.0 && realvalue <= 180.0)
{
printf("PASS (%g)\n", realvalue);
}
else
{
printf("FAIL (%g)\n", realvalue);
errors ++;
}
fputs("ttfGetNumFonts: ", stdout);
if ((num_fonts = ttfGetNumFonts(font)) > 0)
{
printf("PASS (%u)\n", (unsigned)num_fonts);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetPostScriptName: ", stdout);
if ((value = ttfGetPostScriptName(font)) != NULL)
{
printf("PASS (%s)\n", value);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetStretch: ", stdout);
if ((intvalue = (int)ttfGetStretch(font)) >= TTF_STRETCH_NORMAL && intvalue <= TTF_STRETCH_ULTRA_EXPANDED)
{
printf("PASS (%s)\n", stretches[intvalue]);
}
else
{
printf("FAIL (%d)\n", intvalue);
errors ++;
}
fputs("ttfGetStyle: ", stdout);
if ((style = ttfGetStyle(font)) >= TTF_STYLE_NORMAL && style <= TTF_STYLE_ITALIC)
{
printf("PASS (%s)\n", styles[style]);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetVersion: ", stdout);
if ((value = ttfGetVersion(font)) != NULL)
{
printf("PASS (%s)\n", value);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetWeight: ", stdout);
if ((weight = ttfGetWeight(font)) >= 0)
{
printf("PASS (%u)\n", (unsigned)weight);
}
else
{
puts("FAIL");
errors ++;
}
fputs("ttfGetWidth(' '): ", stdout);
if ((intvalue = ttfGetWidth(font, ' ')) > 0)
{
printf("PASS (%d)\n", intvalue);
}
else
{
printf("FAIL (%d)\n", intvalue);
errors ++;
}
fputs("ttfGetXHeight: ", stdout);
if ((intvalue = ttfGetXHeight(font)) > 0)
{
printf("PASS (%d)\n", intvalue);
}
else
{
printf("FAIL (%d)\n", intvalue);
errors ++;
}
fputs("ttfIsFixedPitch: ", stdout);
if (ttfIsFixedPitch(font))
puts("PASS (true)");
else
puts("PASS (false)");
ttfDelete(font);
return (errors);
}

61
ttf.c
View File

@ -3,16 +3,12 @@
// //
// https://github.com/michaelrsweet/ttf // https://github.com/michaelrsweet/ttf
// //
// Copyright © 2018-2021 by Michael R Sweet. // Copyright © 2018-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
// //
//
// Include necessary headers...
//
#ifdef _WIN32 #ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS # define _CRT_SECURE_NO_WARNINGS
#endif // _WIN32 #endif // _WIN32
@ -75,7 +71,7 @@ typedef __int64 ssize_t; // POSIX type not present on Windows...
// //
// DEBUG is typically defined for debug builds. TTF_DEBUG maps to printf when // DEBUG is typically defined for debug builds. TTF_DEBUG maps to fprintf when
// DEBUG is defined and is a no-op otherwise... // DEBUG is defined and is a no-op otherwise...
// //
@ -420,7 +416,7 @@ ttfCreate(const char *filename, // I - Filename
if (read_os_2(font, &os_2)) if (read_os_2(font, &os_2))
{ {
// Copy key values from OS/2 table... // Copy key values from OS/2 table...
static const ttf_stretch_t widths[] = static const ttf_stretch_t stretches[] =
{ {
TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed
TTF_STRETCH_EXTRA_CONDENSED, // extra-condensed TTF_STRETCH_EXTRA_CONDENSED, // extra-condensed
@ -433,8 +429,8 @@ ttfCreate(const char *filename, // I - Filename
TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded
}; };
if (os_2.usWidthClass >= 1 && os_2.usWidthClass <= (int)(sizeof(widths) / sizeof(widths[0]))) if (os_2.usWidthClass >= 1 && os_2.usWidthClass <= (int)(sizeof(stretches) / sizeof(stretches[0])))
font->stretch = widths[os_2.usWidthClass - 1]; font->stretch = stretches[os_2.usWidthClass - 1];
font->weight = (short)os_2.usWeightClass; font->weight = (short)os_2.usWeightClass;
font->cap_height = os_2.sCapHeight; font->cap_height = os_2.sCapHeight;
@ -452,7 +448,7 @@ ttfCreate(const char *filename, // I - Filename
font->cap_height = font->ascent; font->cap_height = font->ascent;
if (font->x_height == 0) if (font->x_height == 0)
font->x_height = 3 * font->ascent / 5; font->x_height = 3 * font->ascent / 5;
// Build a sparse glyph widths table... // Build a sparse glyph widths table...
font->min_char = -1; font->min_char = -1;
@ -481,6 +477,11 @@ ttfCreate(const char *filename, // I - Filename
else else
font->widths[bin][i & 255] = widths[glyph]; font->widths[bin][i & 255] = widths[glyph];
} }
#ifdef DEBUG
if (i >= ' ' && i < 127 && font->widths[0])
TTF_DEBUG("ttfCreate: width['%c']=%d(%d)\n", (char)i, font->widths[0][i].width, font->widths[0][i].left_bearing);
#endif // DEBUG
} }
// Cleanup and return the font... // Cleanup and return the font...
@ -737,17 +738,6 @@ ttfGetFamily(ttf_t *font) // I - Font
} }
//
// 'ttfIsFixedPitch()' - Determine whether a font is fixedpitch.
//
bool // O - `true` if fixed pitch, `false` otherwise
ttfIsFixedPitch(ttf_t *font) // I - Font
{
return (font ? font->is_fixed : false);
}
// //
// 'ttfGetItalicAngle()' - Get the italic angle. // 'ttfGetItalicAngle()' - Get the italic angle.
// //
@ -856,13 +846,14 @@ int // O - Width in 1000ths
ttfGetWidth(ttf_t *font, // I - Font ttfGetWidth(ttf_t *font, // I - Font
int ch) // I - Unicode character int ch) // I - Unicode character
{ {
int bin = ch >> 8; // Bin in widths array int bin = ch >> 8; // Bin in widths array
// Range check input... // Range check input...
if (!font || ch < ' ' || ch == 0x7f) if (!font || ch < ' ' || ch == 0x7f)
return (0); return (0);
else if (font->widths[bin])
if (font->widths[bin])
return ((int)(1000.0f * font->widths[bin][ch & 255].width / font->units)); return ((int)(1000.0f * font->widths[bin][ch & 255].width / font->units));
else if (font->widths[0]) // .notdef else if (font->widths[0]) // .notdef
return ((int)(1000.0f * font->widths[0][0].width / font->units)); return ((int)(1000.0f * font->widths[0][0].width / font->units));
@ -882,6 +873,17 @@ ttfGetXHeight(ttf_t *font) // I - Font
} }
//
// 'ttfIsFixedPitch()' - Determine whether a font is fixedpitch.
//
bool // O - `true` if fixed pitch, `false` otherwise
ttfIsFixedPitch(ttf_t *font) // I - Font
{
return (font ? font->is_fixed : false);
}
// //
// 'copy_name()' - Copy a name string from a font. // 'copy_name()' - Copy a name string from a font.
// //
@ -1280,7 +1282,7 @@ read_cmap(ttf_t *font) // I - Font
// Based on the end code of the segent table, allocate space for the // Based on the end code of the segent table, allocate space for the
// uncompressed cmap table... // uncompressed cmap table...
segCount --; // Last segment is not used (sigh) // segCount --; // Last segment is not used (sigh)
font->num_cmap = segments[segCount - 1].endCode + 1; 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));
@ -1305,17 +1307,18 @@ read_cmap(ttf_t *font) // I - Font
{ {
// Use an "obscure indexing trick" (words from the spec, not // Use an "obscure indexing trick" (words from the spec, not
// mine) to look up the glyph index... // mine) to look up the glyph index...
temp = segment->idRangeOffset / 2 + ch - segment->startCode + seg - segCount - 1; temp = segment->idRangeOffset / 2 - segCount + (ch - segment->startCode) + (segment - segments);
if (temp < 0 || temp >= numGlyphIdArray || !glyphIdArray[temp]) TTF_DEBUG("read_cmap: ch=%d, temp=%d\n", ch, temp);
if (temp < 0 || temp >= numGlyphIdArray)
glyph = -1; glyph = -1;
else else
glyph = ((glyphIdArray[temp] + segment->idDelta) & 65535) - 1; glyph = (glyphIdArray[temp] + segment->idDelta) & 65535;
} }
else else
{ {
// Just use idDelta to compute a glyph index... // Just use idDelta to compute a glyph index...
glyph = ((ch + segment->idDelta) & 65535) - 1; glyph = (ch + segment->idDelta) & 65535;
} }
cmapptr[ch] = glyph; cmapptr[ch] = glyph;
@ -1582,6 +1585,8 @@ read_hmtx(ttf_t *font, // I - Font
{ {
widths[i].width = (short)read_ushort(font); widths[i].width = (short)read_ushort(font);
widths[i].left_bearing = (short)read_short(font); widths[i].left_bearing = (short)read_short(font);
TTF_DEBUG("read_hmtx: widths[%d].width=%d, .left_bearing=%d\n", i, widths[i].width, widths[i].left_bearing);
} }
return (widths); return (widths);

13
ttf.h
View File

@ -3,7 +3,7 @@
// //
// https://github.com/michaelrsweet/ttf // https://github.com/michaelrsweet/ttf
// //
// Copyright © 2018-2021 by Michael R Sweet. // Copyright © 2018-2023 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -11,17 +11,11 @@
#ifndef TTF_H #ifndef TTF_H
# define TTF_H # define TTF_H
//
// Include necessary headers...
//
# include <stdbool.h> # include <stdbool.h>
# include <sys/types.h> # include <sys/types.h>
# ifdef __cplusplus # ifdef __cplusplus
extern "C" { extern "C" {
# endif // # endif // __cplusplus
// //
@ -89,8 +83,8 @@ extern ttf_t *ttfCreate(const char *filename, size_t idx, ttf_err_cb_t err_cb,
extern void ttfDelete(ttf_t *font); extern void ttfDelete(ttf_t *font);
extern int ttfGetAscent(ttf_t *font); extern int ttfGetAscent(ttf_t *font);
extern ttf_rect_t *ttfGetBounds(ttf_t *font, ttf_rect_t *bounds); extern ttf_rect_t *ttfGetBounds(ttf_t *font, ttf_rect_t *bounds);
extern int ttfGetCapHeight(ttf_t *font);
extern const int *ttfGetCMap(ttf_t *font, size_t *num_cmap); extern const int *ttfGetCMap(ttf_t *font, size_t *num_cmap);
extern int ttfGetCapHeight(ttf_t *font);
extern const char *ttfGetCopyright(ttf_t *font); extern const char *ttfGetCopyright(ttf_t *font);
extern int ttfGetDescent(ttf_t *font); extern int ttfGetDescent(ttf_t *font);
extern ttf_rect_t *ttfGetExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents); extern ttf_rect_t *ttfGetExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents);
@ -112,5 +106,4 @@ extern bool ttfIsFixedPitch(ttf_t *font);
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif // __cplusplus # endif // __cplusplus
#endif // !TTF_H #endif // !TTF_H