Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
932f237c3f | ||
|
|
09198056a5 | ||
|
|
1c9e675cf6 | ||
|
|
f16f0c10ed | ||
|
|
019f0e8003 |
12
.github/workflows/build.yml
vendored
@@ -13,9 +13,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout PDFio sources
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
submodules: recursive
|
||||
uses: actions/checkout@v5
|
||||
- name: Update Build Environment
|
||||
run: sudo apt-get update --fix-missing -y
|
||||
- name: Install Prerequisites
|
||||
@@ -39,9 +37,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout PDFio sources
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
submodules: recursive
|
||||
uses: actions/checkout@v5
|
||||
- name: Configure PDFio
|
||||
run: ./configure --enable-debug --enable-sanitizer --enable-maintainer
|
||||
- name: Build PDFio
|
||||
@@ -57,9 +53,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout PDFio sources
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
submodules: recursive
|
||||
uses: actions/checkout@v5
|
||||
- name: Setup MSBuild
|
||||
uses: microsoft/setup-msbuild@v2
|
||||
- name: Nuget Restore
|
||||
|
||||
2
.github/workflows/codeql.yml
vendored
@@ -24,7 +24,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout PDFio sources
|
||||
uses: actions/checkout@v6
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
|
||||
4
.github/workflows/coverity.yml
vendored
@@ -8,9 +8,7 @@ jobs:
|
||||
environment: Coverity
|
||||
steps:
|
||||
- name: Checkout PDFio sources
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
submodules: recursive
|
||||
uses: actions/checkout@v5
|
||||
- name: Update Build Environment
|
||||
run: sudo apt-get update --fix-missing -y
|
||||
- name: Install Prerequisites
|
||||
|
||||
1
.gitignore
vendored
@@ -25,4 +25,5 @@
|
||||
/pdfio-*.zip*
|
||||
/testpdfio
|
||||
/testpdfio-*.pdf
|
||||
/testttf
|
||||
/x64
|
||||
|
||||
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "ttf"]
|
||||
path = ttf
|
||||
url = https://github.com/michaelrsweet/ttf.git
|
||||
29
CHANGES.md
@@ -2,34 +2,6 @@ Changes in PDFio
|
||||
================
|
||||
|
||||
|
||||
v1.7.0 - YYYY-MM-DD
|
||||
-------------------
|
||||
|
||||
- Now use TTF 1.1 or later for font support.
|
||||
- Added support for basic compound stream filters for ASCII85Decode support
|
||||
(Issue #11)
|
||||
- Added support for LZWDecode filters (Issue #11)
|
||||
- Added support for writing object streams (Issue #101)
|
||||
- Added support for GIF files (Issue #145)
|
||||
- Added support for WebP files (Issue #144)
|
||||
- Added `pdfioPageGetXxx` functions to get values from the page dictionaries
|
||||
(Issue #150)
|
||||
- Fixed a buffer overflow in the (still not enabled) AES-256 code.
|
||||
|
||||
|
||||
v1.6.2 - YYYY-MM-DD
|
||||
-------------------
|
||||
|
||||
- Increased the maximum length of a single string to 128k (Issue #146)
|
||||
- Added missing range checks to `pdfioArrayCopy` and `pdfioDictCopy`.
|
||||
- Refactored PDF encryption code to fix unlocking with certain files.
|
||||
- Improved xref table loop detection (Issue #148)
|
||||
- Fixed xref reconstruction for objects lacking a `Type` value.
|
||||
- Fixed `pdfioPageOpenStream` for indirect `Contents` arrays.
|
||||
- Fixed an error propagation bug when reading too-long values (Issue #146)
|
||||
- Fixed a Clang warning.
|
||||
|
||||
|
||||
v1.6.1 - 2025-12-26
|
||||
-------------------
|
||||
|
||||
@@ -43,7 +15,6 @@ v1.6.1 - 2025-12-26
|
||||
- Fixed the generated pkg-config file.
|
||||
|
||||
|
||||
|
||||
v1.6.0 - 2025-10-06
|
||||
-------------------
|
||||
|
||||
|
||||
45
Makefile.in
@@ -1,7 +1,7 @@
|
||||
#
|
||||
# Makefile for PDFio.
|
||||
#
|
||||
# Copyright © 2021-2026 by Michael R Sweet.
|
||||
# Copyright © 2021-2025 by Michael R Sweet.
|
||||
#
|
||||
# Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
# information.
|
||||
@@ -69,8 +69,6 @@ top_srcdir = @top_srcdir@
|
||||
|
||||
BUILDROOT = $(DSTROOT)$(RPM_BUILD_ROOT)$(DESTDIR)
|
||||
|
||||
TTFDIR = @TTFDIR@
|
||||
|
||||
|
||||
# Build commands...
|
||||
.SUFFIXES: .c .h .o
|
||||
@@ -91,7 +89,6 @@ PUBOBJS = \
|
||||
pdfio-crypto.o \
|
||||
pdfio-dict.o \
|
||||
pdfio-file.o \
|
||||
pdfio-lzw.o \
|
||||
pdfio-md5.o \
|
||||
pdfio-object.o \
|
||||
pdfio-page.o \
|
||||
@@ -102,14 +99,17 @@ PUBOBJS = \
|
||||
pdfio-token.o \
|
||||
pdfio-value.o
|
||||
LIBOBJS = \
|
||||
$(PUBOBJS)
|
||||
$(PUBOBJS) \
|
||||
ttf.o
|
||||
OBJS = \
|
||||
$(LIBOBJS) \
|
||||
testpdfio.o
|
||||
testpdfio.o \
|
||||
testttf.o
|
||||
TARGETS = \
|
||||
$(LIBPDFIO) \
|
||||
$(LIBPDFIO_STATIC) \
|
||||
testpdfio
|
||||
testpdfio \
|
||||
testttf
|
||||
DOCFILES = \
|
||||
doc/pdfio.html \
|
||||
doc/pdfio-512.png \
|
||||
@@ -135,30 +135,16 @@ EXAMPLES = \
|
||||
|
||||
|
||||
# Make everything
|
||||
all:
|
||||
if test "x$(TTFDIR)" != x; then \
|
||||
echo Making all in $(TTFDIR)...; \
|
||||
(cd $(TTFDIR); $(MAKE) $(MFLAGS) all) || exit 1; \
|
||||
fi
|
||||
$(MAKE) $(MFLAGS) $(TARGETS)
|
||||
all: $(TARGETS)
|
||||
|
||||
|
||||
# Clean everything
|
||||
clean:
|
||||
if test "x$(TTFDIR)" != x; then \
|
||||
echo Cleaning in $(TTFDIR)...; \
|
||||
(cd $(TTFDIR); $(MAKE) $(MFLAGS) clean) || exit 1; \
|
||||
fi
|
||||
echo Cleaning build files...
|
||||
rm -f $(TARGETS) $(OBJS)
|
||||
|
||||
|
||||
# Install everything
|
||||
install: $(TARGETS)
|
||||
if test "x$(TTFDIR)" != x; then \
|
||||
echo Installing in $(TTFDIR)...; \
|
||||
(cd $(TTFDIR); $(MAKE) $(MFLAGS) install) || exit 1; \
|
||||
fi
|
||||
echo Installing header files to $(BUILDROOT)$(includedir)...
|
||||
$(INSTALL) -d -m 755 $(BUILDROOT)$(includedir)
|
||||
for file in $(PUBHEADERS); do \
|
||||
@@ -200,8 +186,9 @@ install: $(TARGETS)
|
||||
|
||||
|
||||
# Test everything
|
||||
test: testpdfio
|
||||
./testpdfio 2>test.log
|
||||
test: testpdfio testttf
|
||||
./testttf 2>test.log
|
||||
./testpdfio 2>>test.log
|
||||
LANG=fr_FR.UTF-8 ./testpdfio 2>>test.log
|
||||
|
||||
|
||||
@@ -245,10 +232,18 @@ testpdfio: testpdfio.o libpdfio.a
|
||||
$(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
|
||||
pdfio-content.o: pdfio-content.h ttf.h
|
||||
testpdfio.o: test.h
|
||||
testttf.o: ttf.h
|
||||
ttf.o: ttf.h
|
||||
|
||||
|
||||
# Make documentation using Codedoc <https://www.msweet.org/codedoc>
|
||||
|
||||
2
NOTICE
@@ -1,6 +1,6 @@
|
||||
PDFio - PDF Read/Write Library
|
||||
|
||||
Copyright © 2021-2026 by Michael R Sweet.
|
||||
Copyright © 2021-2025 by Michael R Sweet.
|
||||
|
||||
(Optional) Exceptions to the Apache 2.0 License:
|
||||
================================================
|
||||
|
||||
13
README.md
@@ -3,8 +3,8 @@ pdfio - PDF Read/Write Library
|
||||
|
||||

|
||||

|
||||
[](https://github.com/michaelrsweet/pdfio/actions/workflows/build.yml)
|
||||
[](https://scan.coverity.com/projects/michaelrsweet-pdfio)
|
||||
[](https://github.com/michaelrsweet/pdfio/actions/workflows/build.yml)
|
||||
[](https://scan.coverity.com/projects/michaelrsweet-pdfio)
|
||||
|
||||
PDFio is a simple C library for reading and writing PDF files. The primary
|
||||
goals of PDFio are:
|
||||
@@ -28,12 +28,7 @@ PDFio requires the following to build the software:
|
||||
|
||||
- A C99 compiler such as Clang, GCC, or MS Visual C
|
||||
- A POSIX-compliant `make` program
|
||||
- A POSIX-compliant `sh` program
|
||||
- libpng (<https://www.libpng.org/>) 1.6 or later for full PNG image support
|
||||
(optional)
|
||||
- libwebp (<https://developers.google.com/speed/webp>) 1.0 or later for WebP
|
||||
image support (optional)
|
||||
- ZLIB (<https://www.zlib.net/>) 1.1 or later
|
||||
- ZLIB (<https://www.zlib.net>) 1.1 or higher
|
||||
|
||||
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
|
||||
|
||||
@@ -94,7 +89,7 @@ generates a static library that will be installed under "/usr/local" with:
|
||||
Legal Stuff
|
||||
-----------
|
||||
|
||||
PDFio is Copyright © 2021-2026 by Michael R Sweet.
|
||||
PDFio is Copyright © 2021-2025 by Michael R Sweet.
|
||||
|
||||
This software is licensed under the Apache License Version 2.0 with an
|
||||
(optional) exception to allow linking against GPL2/LGPL2 software. See the
|
||||
|
||||
38
SECURITY.md
@@ -5,40 +5,12 @@ This file describes how security issues are reported and handled, and what the
|
||||
expectations are for security issues reported to this project.
|
||||
|
||||
|
||||
What is a Security Bug?
|
||||
-----------------------
|
||||
|
||||
Not every bug is a security bug.
|
||||
|
||||
Certain bugs that might be considered security bugs in a program, such as bugs
|
||||
that lead to a Denial of Service, are *not* considered security bugs simply
|
||||
because this project *does not provide a service*. Some might argue that, "my
|
||||
server uses this library and the bug in this library causes a denial of service
|
||||
for my server", however it is the responsibility of the *server* to protect
|
||||
against DoS attacks, not a subordinate library, because only the server knows
|
||||
what is an appropriate use of memory, CPU, time, and other resources.
|
||||
|
||||
Similarly, bugs caused by incorrect API usage such as passing `NULL` pointers
|
||||
where such pointers are not allowed, passing the wrong kinds of pointers or
|
||||
objects to an API, or using a private API are not security bugs because they
|
||||
are not caused by an attacker but by the developer.
|
||||
|
||||
Finally, bugs that only exist in unreleased (non-production) or inactive code
|
||||
are not security bugs because they do not affect ordinary users. See the
|
||||
[Supported Versions](#supported-versions) section below for more information
|
||||
about what versions of the project are covered by this security policy.
|
||||
|
||||
If the bug you've found falls into one of these three categories, please report
|
||||
the bug as an the ordinary project issue at
|
||||
<https://github.com/michaelrsweet/pdfio/issues>.
|
||||
|
||||
|
||||
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 causes the software to crash. Such defects should be
|
||||
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>.
|
||||
|
||||
@@ -46,6 +18,11 @@ 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
|
||||
----------------------
|
||||
@@ -91,9 +68,6 @@ example:
|
||||
1.0b2
|
||||
1.0rc1
|
||||
|
||||
Pre-release code in a Git branch ("master", "v1.6.x", etc.) is similarly *not*
|
||||
production release code.
|
||||
|
||||
|
||||
PGP Public Key
|
||||
--------------
|
||||
|
||||
269
configure
vendored
@@ -1,6 +1,6 @@
|
||||
#! /bin/sh
|
||||
# Guess values for system-dependent variables and create Makefiles.
|
||||
# Generated by GNU Autoconf 2.71 for pdfio 1.7.0.
|
||||
# Generated by GNU Autoconf 2.71 for pdfio 1.6.1.
|
||||
#
|
||||
# Report bugs to <https://github.com/michaelrsweet/pdfio/issues>.
|
||||
#
|
||||
@@ -610,8 +610,8 @@ MAKEFLAGS=
|
||||
# Identity of this package.
|
||||
PACKAGE_NAME='pdfio'
|
||||
PACKAGE_TARNAME='pdfio'
|
||||
PACKAGE_VERSION='1.7.0'
|
||||
PACKAGE_STRING='pdfio 1.7.0'
|
||||
PACKAGE_VERSION='1.6.1'
|
||||
PACKAGE_STRING='pdfio 1.6.1'
|
||||
PACKAGE_BUGREPORT='https://github.com/michaelrsweet/pdfio/issues'
|
||||
PACKAGE_URL='https://www.msweet.org/pdfio'
|
||||
|
||||
@@ -647,15 +647,12 @@ ac_includes_default="\
|
||||
#endif"
|
||||
|
||||
ac_header_c_list=
|
||||
enable_option_checking=no
|
||||
ac_subst_vars='LTLIBOBJS
|
||||
LIBOBJS
|
||||
WARNINGS
|
||||
CSFLAGS
|
||||
LIBPDFIO_STATIC
|
||||
LIBPDFIO
|
||||
TTFDIR
|
||||
subdirs
|
||||
PKGCONFIG_REQUIRES_PRIVATE
|
||||
PKGCONFIG_REQUIRES
|
||||
PKGCONFIG_LIBS_PRIVATE
|
||||
@@ -734,7 +731,6 @@ ac_subst_files=''
|
||||
ac_user_opts='
|
||||
enable_option_checking
|
||||
enable_libpng
|
||||
enable_libwebp
|
||||
enable_static
|
||||
enable_shared
|
||||
enable_debug
|
||||
@@ -751,7 +747,7 @@ CFLAGS
|
||||
LDFLAGS
|
||||
LIBS
|
||||
CPPFLAGS'
|
||||
ac_subdirs_all='ttf'
|
||||
|
||||
|
||||
# Initialize some variables set by options.
|
||||
ac_init_help=
|
||||
@@ -1299,7 +1295,7 @@ if test "$ac_init_help" = "long"; then
|
||||
# Omit some internal or obsolete options to make the list less imposing.
|
||||
# This message is too long to be a string in the A/UX 3.1 sh.
|
||||
cat <<_ACEOF
|
||||
\`configure' configures pdfio 1.7.0 to adapt to many kinds of systems.
|
||||
\`configure' configures pdfio 1.6.1 to adapt to many kinds of systems.
|
||||
|
||||
Usage: $0 [OPTION]... [VAR=VALUE]...
|
||||
|
||||
@@ -1365,7 +1361,7 @@ fi
|
||||
|
||||
if test -n "$ac_init_help"; then
|
||||
case $ac_init_help in
|
||||
short | recursive ) echo "Configuration of pdfio 1.7.0:";;
|
||||
short | recursive ) echo "Configuration of pdfio 1.6.1:";;
|
||||
esac
|
||||
cat <<\_ACEOF
|
||||
|
||||
@@ -1373,9 +1369,7 @@ Optional Features:
|
||||
--disable-option-checking ignore unrecognized --enable/--with options
|
||||
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
|
||||
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
|
||||
--disable-libpng use libpng for pdfioFileCreateImageObjFromFile,
|
||||
default=auto
|
||||
--disable-libwebp use libwebp for pdfioFileCreateImageObjFromFile,
|
||||
--enable-libpng use libpng for pdfioFileCreateImageObjFromFile,
|
||||
default=auto
|
||||
--disable-static do not install static library
|
||||
--enable-shared install shared library
|
||||
@@ -1466,7 +1460,7 @@ fi
|
||||
test -n "$ac_init_help" && exit $ac_status
|
||||
if $ac_init_version; then
|
||||
cat <<\_ACEOF
|
||||
pdfio configure 1.7.0
|
||||
pdfio configure 1.6.1
|
||||
generated by GNU Autoconf 2.71
|
||||
|
||||
Copyright (C) 2021 Free Software Foundation, Inc.
|
||||
@@ -1684,7 +1678,7 @@ cat >config.log <<_ACEOF
|
||||
This file contains any messages produced by compilers while
|
||||
running configure, to aid debugging if configure makes a mistake.
|
||||
|
||||
It was created by pdfio $as_me 1.7.0, which was
|
||||
It was created by pdfio $as_me 1.6.1, which was
|
||||
generated by GNU Autoconf 2.71. Invocation command line was
|
||||
|
||||
$ $0$ac_configure_args_raw
|
||||
@@ -2440,9 +2434,9 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
|
||||
|
||||
|
||||
|
||||
PDFIO_VERSION="1.7.0"
|
||||
PDFIO_VERSION_MAJOR="`echo 1.7.0 | awk -F. '{print $1}'`"
|
||||
PDFIO_VERSION_MINOR="`echo 1.7.0 | awk -F. '{printf("%d\n",$2);}'`"
|
||||
PDFIO_VERSION="1.6.1"
|
||||
PDFIO_VERSION_MAJOR="`echo 1.6.1 | awk -F. '{print $1}'`"
|
||||
PDFIO_VERSION_MINOR="`echo 1.6.1 | awk -F. '{printf("%d\n",$2);}'`"
|
||||
|
||||
|
||||
|
||||
@@ -4144,8 +4138,8 @@ fi
|
||||
PKGCONFIG_CFLAGS="-I\${includedir}"
|
||||
PKGCONFIG_LIBS="-L\${libdir} -lpdfio"
|
||||
PKGCONFIG_LIBS_PRIVATE="-lm"
|
||||
PKGCONFIG_REQUIRES=""
|
||||
PKGCONFIG_REQUIRES_PRIVATE="ttf"
|
||||
PKBCONFIG_REQUIRES=""
|
||||
PKGCONFIG_REQUIRES_PRIVATE=""
|
||||
|
||||
|
||||
|
||||
@@ -4153,35 +4147,6 @@ PKGCONFIG_REQUIRES_PRIVATE="ttf"
|
||||
|
||||
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for ttf library" >&5
|
||||
printf %s "checking for ttf library... " >&6; }
|
||||
|
||||
|
||||
if $PKGCONFIG --exists ttf
|
||||
then :
|
||||
|
||||
# Use installed TTF library...
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
|
||||
printf "%s\n" "yes" >&6; }
|
||||
CPPFLAGS="$CPPFLAGS $($PKGCONFIG --cflags ttf)"
|
||||
TTFDIR=""
|
||||
LIBS="$($PKGCONFIG --libs ttf) $LIBS"
|
||||
|
||||
else $as_nop
|
||||
|
||||
# Use embedded TTF library...
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no, using embedded version" >&5
|
||||
printf "%s\n" "no, using embedded version" >&6; }
|
||||
CPPFLAGS="$CPPFLAGS -Ittf"
|
||||
TTFDIR="ttf"
|
||||
LIBS="-Lttf \`PKG_CONFIG_PATH=ttf $PKGCONFIG --libs ttf\` $LIBS"
|
||||
subdirs="$subdirs ttf"
|
||||
|
||||
|
||||
fi
|
||||
|
||||
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for zlib via pkg-config" >&5
|
||||
printf %s "checking for zlib via pkg-config... " >&6; }
|
||||
if $PKGCONFIG --exists zlib
|
||||
@@ -4191,7 +4156,7 @@ then :
|
||||
printf "%s\n" "yes" >&6; }
|
||||
CPPFLAGS="$($PKGCONFIG --cflags zlib) $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs zlib) $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="$PKGCONFIG_REQUIRES_PRIVATE, zlib"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="zlib"
|
||||
|
||||
else $as_nop
|
||||
|
||||
@@ -4258,7 +4223,6 @@ fi
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# Check whether --enable-libpng was given.
|
||||
if test ${enable_libpng+y}
|
||||
then :
|
||||
@@ -4281,7 +4245,16 @@ printf "%s\n" "#define HAVE_LIBPNG 1" >>confdefs.h
|
||||
|
||||
CPPFLAGS="$($PKGCONFIG --cflags libpng16) -DHAVE_LIBPNG=1 $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs libpng16) -lz $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
if test "x$PKGCONFIG_REQUIRES_PRIVATE" = x
|
||||
then :
|
||||
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6"
|
||||
|
||||
else $as_nop
|
||||
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
|
||||
fi
|
||||
|
||||
else $as_nop
|
||||
|
||||
@@ -4304,51 +4277,6 @@ then :
|
||||
fi
|
||||
|
||||
|
||||
# Check whether --enable-libwebp was given.
|
||||
if test ${enable_libwebp+y}
|
||||
then :
|
||||
enableval=$enable_libwebp;
|
||||
fi
|
||||
|
||||
|
||||
if test "x$PKGCONFIG" != x -a x$enable_libwebp != xno
|
||||
then :
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for libwebp" >&5
|
||||
printf %s "checking for libwebp... " >&6; }
|
||||
if $PKGCONFIG --exists libwebp
|
||||
then :
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: yes" >&5
|
||||
printf "%s\n" "yes" >&6; };
|
||||
|
||||
printf "%s\n" "#define HAVE_LIBWEBP 1" >>confdefs.h
|
||||
|
||||
CPPFLAGS="$($PKGCONFIG --cflags libwebp) -DHAVE_LIBWEBP=1 $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs libwebp) -lz $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libwebp, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
|
||||
else $as_nop
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5
|
||||
printf "%s\n" "no" >&6; };
|
||||
if test x$enable_libwebp = xyes
|
||||
then :
|
||||
|
||||
as_fn_error $? "libwebp-dev required for --enable-libwebp." "$LINENO" 5
|
||||
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
elif test x$enable_libwebp = xyes
|
||||
then :
|
||||
|
||||
as_fn_error $? "libwebp-dev required for --enable-libwebp." "$LINENO" 5
|
||||
|
||||
fi
|
||||
|
||||
|
||||
# Check whether --enable-static was given.
|
||||
if test ${enable_static+y}
|
||||
then :
|
||||
@@ -5187,7 +5115,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
|
||||
# report actual input values of CONFIG_FILES etc. instead of their
|
||||
# values after options handling.
|
||||
ac_log="
|
||||
This file was extended by pdfio $as_me 1.7.0, which was
|
||||
This file was extended by pdfio $as_me 1.6.1, which was
|
||||
generated by GNU Autoconf 2.71. Invocation command line was
|
||||
|
||||
CONFIG_FILES = $CONFIG_FILES
|
||||
@@ -5243,7 +5171,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\
|
||||
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
|
||||
ac_cs_config='$ac_cs_config_escaped'
|
||||
ac_cs_version="\\
|
||||
pdfio config.status 1.7.0
|
||||
pdfio config.status 1.6.1
|
||||
configured by $0, generated by GNU Autoconf 2.71,
|
||||
with options \\"\$ac_cs_config\\"
|
||||
|
||||
@@ -5799,149 +5727,6 @@ if test "$no_create" != yes; then
|
||||
# would make configure fail if this is the last instruction.
|
||||
$ac_cs_success || as_fn_exit 1
|
||||
fi
|
||||
|
||||
#
|
||||
# CONFIG_SUBDIRS section.
|
||||
#
|
||||
if test "$no_recursion" != yes; then
|
||||
|
||||
# Remove --cache-file, --srcdir, and --disable-option-checking arguments
|
||||
# so they do not pile up.
|
||||
ac_sub_configure_args=
|
||||
ac_prev=
|
||||
eval "set x $ac_configure_args"
|
||||
shift
|
||||
for ac_arg
|
||||
do
|
||||
if test -n "$ac_prev"; then
|
||||
ac_prev=
|
||||
continue
|
||||
fi
|
||||
case $ac_arg in
|
||||
-cache-file | --cache-file | --cache-fil | --cache-fi \
|
||||
| --cache-f | --cache- | --cache | --cach | --cac | --ca | --c)
|
||||
ac_prev=cache_file ;;
|
||||
-cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \
|
||||
| --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* \
|
||||
| --c=*)
|
||||
;;
|
||||
--config-cache | -C)
|
||||
;;
|
||||
-srcdir | --srcdir | --srcdi | --srcd | --src | --sr)
|
||||
ac_prev=srcdir ;;
|
||||
-srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*)
|
||||
;;
|
||||
-prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
|
||||
ac_prev=prefix ;;
|
||||
-prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
|
||||
;;
|
||||
--disable-option-checking)
|
||||
;;
|
||||
*)
|
||||
case $ac_arg in
|
||||
*\'*) ac_arg=`printf "%s\n" "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;;
|
||||
esac
|
||||
as_fn_append ac_sub_configure_args " '$ac_arg'" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Always prepend --prefix to ensure using the same prefix
|
||||
# in subdir configurations.
|
||||
ac_arg="--prefix=$prefix"
|
||||
case $ac_arg in
|
||||
*\'*) ac_arg=`printf "%s\n" "$ac_arg" | sed "s/'/'\\\\\\\\''/g"` ;;
|
||||
esac
|
||||
ac_sub_configure_args="'$ac_arg' $ac_sub_configure_args"
|
||||
|
||||
# Pass --silent
|
||||
if test "$silent" = yes; then
|
||||
ac_sub_configure_args="--silent $ac_sub_configure_args"
|
||||
fi
|
||||
|
||||
# Always prepend --disable-option-checking to silence warnings, since
|
||||
# different subdirs can have different --enable and --with options.
|
||||
ac_sub_configure_args="--disable-option-checking $ac_sub_configure_args"
|
||||
|
||||
ac_popdir=`pwd`
|
||||
for ac_dir in : $subdirs; do test "x$ac_dir" = x: && continue
|
||||
|
||||
# Do not complain, so a configure script can configure whichever
|
||||
# parts of a large source tree are present.
|
||||
test -d "$srcdir/$ac_dir" || continue
|
||||
|
||||
ac_msg="=== configuring in $ac_dir (`pwd`/$ac_dir)"
|
||||
printf "%s\n" "$as_me:${as_lineno-$LINENO}: $ac_msg" >&5
|
||||
printf "%s\n" "$ac_msg" >&6
|
||||
as_dir="$ac_dir"; as_fn_mkdir_p
|
||||
ac_builddir=.
|
||||
|
||||
case "$ac_dir" in
|
||||
.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;;
|
||||
*)
|
||||
ac_dir_suffix=/`printf "%s\n" "$ac_dir" | sed 's|^\.[\\/]||'`
|
||||
# A ".." for each directory in $ac_dir_suffix.
|
||||
ac_top_builddir_sub=`printf "%s\n" "$ac_dir_suffix" | sed 's|/[^\\/]*|/..|g;s|/||'`
|
||||
case $ac_top_builddir_sub in
|
||||
"") ac_top_builddir_sub=. ac_top_build_prefix= ;;
|
||||
*) ac_top_build_prefix=$ac_top_builddir_sub/ ;;
|
||||
esac ;;
|
||||
esac
|
||||
ac_abs_top_builddir=$ac_pwd
|
||||
ac_abs_builddir=$ac_pwd$ac_dir_suffix
|
||||
# for backward compatibility:
|
||||
ac_top_builddir=$ac_top_build_prefix
|
||||
|
||||
case $srcdir in
|
||||
.) # We are building in place.
|
||||
ac_srcdir=.
|
||||
ac_top_srcdir=$ac_top_builddir_sub
|
||||
ac_abs_top_srcdir=$ac_pwd ;;
|
||||
[\\/]* | ?:[\\/]* ) # Absolute name.
|
||||
ac_srcdir=$srcdir$ac_dir_suffix;
|
||||
ac_top_srcdir=$srcdir
|
||||
ac_abs_top_srcdir=$srcdir ;;
|
||||
*) # Relative name.
|
||||
ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix
|
||||
ac_top_srcdir=$ac_top_build_prefix$srcdir
|
||||
ac_abs_top_srcdir=$ac_pwd/$srcdir ;;
|
||||
esac
|
||||
ac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix
|
||||
|
||||
|
||||
cd "$ac_dir"
|
||||
|
||||
# Check for configure.gnu first; this name is used for a wrapper for
|
||||
# Metaconfig's "Configure" on case-insensitive file systems.
|
||||
if test -f "$ac_srcdir/configure.gnu"; then
|
||||
ac_sub_configure=$ac_srcdir/configure.gnu
|
||||
elif test -f "$ac_srcdir/configure"; then
|
||||
ac_sub_configure=$ac_srcdir/configure
|
||||
else
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: no configuration information is in $ac_dir" >&5
|
||||
printf "%s\n" "$as_me: WARNING: no configuration information is in $ac_dir" >&2;}
|
||||
ac_sub_configure=
|
||||
fi
|
||||
|
||||
# The recursion is here.
|
||||
if test -n "$ac_sub_configure"; then
|
||||
# Make the cache file name correct relative to the subdirectory.
|
||||
case $cache_file in
|
||||
[\\/]* | ?:[\\/]* ) ac_sub_cache_file=$cache_file ;;
|
||||
*) # Relative name.
|
||||
ac_sub_cache_file=$ac_top_build_prefix$cache_file ;;
|
||||
esac
|
||||
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: running $SHELL $ac_sub_configure $ac_sub_configure_args --cache-file=$ac_sub_cache_file --srcdir=$ac_srcdir" >&5
|
||||
printf "%s\n" "$as_me: running $SHELL $ac_sub_configure $ac_sub_configure_args --cache-file=$ac_sub_cache_file --srcdir=$ac_srcdir" >&6;}
|
||||
# The eval makes quoting arguments work.
|
||||
eval "\$SHELL \"\$ac_sub_configure\" $ac_sub_configure_args \
|
||||
--cache-file=\"\$ac_sub_cache_file\" --srcdir=\"\$ac_srcdir\"" ||
|
||||
as_fn_error $? "$ac_sub_configure failed for $ac_dir" "$LINENO" 5
|
||||
fi
|
||||
|
||||
cd "$ac_popdir"
|
||||
done
|
||||
fi
|
||||
if test -n "$ac_unrecognized_opts" && test "$enable_option_checking" != no; then
|
||||
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5
|
||||
printf "%s\n" "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
|
||||
|
||||
60
configure.ac
@@ -1,7 +1,7 @@
|
||||
dnl
|
||||
dnl Configuration script for PDFio
|
||||
dnl
|
||||
dnl Copyright © 2023-2026 by Michael R Sweet
|
||||
dnl Copyright © 2023-2025 by Michael R Sweet
|
||||
dnl
|
||||
dnl Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
dnl information.
|
||||
@@ -21,7 +21,7 @@ AC_PREREQ([2.70])
|
||||
|
||||
|
||||
dnl Package name and version...
|
||||
AC_INIT([pdfio], [1.7.0], [https://github.com/michaelrsweet/pdfio/issues], [pdfio], [https://www.msweet.org/pdfio])
|
||||
AC_INIT([pdfio], [1.6.1], [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}'`"
|
||||
@@ -119,8 +119,8 @@ AC_PATH_TOOL([PKGCONFIG], [pkg-config])
|
||||
PKGCONFIG_CFLAGS="-I\${includedir}"
|
||||
PKGCONFIG_LIBS="-L\${libdir} -lpdfio"
|
||||
PKGCONFIG_LIBS_PRIVATE="-lm"
|
||||
PKGCONFIG_REQUIRES=""
|
||||
PKGCONFIG_REQUIRES_PRIVATE="ttf"
|
||||
PKBCONFIG_REQUIRES=""
|
||||
PKGCONFIG_REQUIRES_PRIVATE=""
|
||||
AC_SUBST([PKGCONFIG_CFLAGS])
|
||||
AC_SUBST([PKGCONFIG_LIBS])
|
||||
AC_SUBST([PKGCONFIG_LIBS_PRIVATE])
|
||||
@@ -128,32 +128,13 @@ AC_SUBST([PKGCONFIG_REQUIRES])
|
||||
AC_SUBST([PKGCONFIG_REQUIRES_PRIVATE])
|
||||
|
||||
|
||||
dnl TTF library for font support...
|
||||
AC_MSG_CHECKING([for ttf library])
|
||||
AS_IF([$PKGCONFIG --exists ttf], [
|
||||
# Use installed TTF library...
|
||||
AC_MSG_RESULT([yes])
|
||||
CPPFLAGS="$CPPFLAGS $($PKGCONFIG --cflags ttf)"
|
||||
TTFDIR=""
|
||||
LIBS="$($PKGCONFIG --libs ttf) $LIBS"
|
||||
], [
|
||||
# Use embedded TTF library...
|
||||
AC_MSG_RESULT([no, using embedded version])
|
||||
CPPFLAGS="$CPPFLAGS -Ittf"
|
||||
TTFDIR="ttf"
|
||||
LIBS="-Lttf \`PKG_CONFIG_PATH=ttf $PKGCONFIG --libs ttf\` $LIBS"
|
||||
AC_CONFIG_SUBDIRS([ttf])
|
||||
])
|
||||
AC_SUBST([TTFDIR])
|
||||
|
||||
|
||||
dnl ZLIB
|
||||
AC_MSG_CHECKING([for zlib via pkg-config])
|
||||
AS_IF([$PKGCONFIG --exists zlib], [
|
||||
AC_MSG_RESULT([yes])
|
||||
CPPFLAGS="$($PKGCONFIG --cflags zlib) $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs zlib) $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="$PKGCONFIG_REQUIRES_PRIVATE, zlib"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="zlib"
|
||||
],[
|
||||
AC_MSG_RESULT([no])
|
||||
AC_CHECK_HEADER([zlib.h])
|
||||
@@ -166,9 +147,8 @@ AS_IF([$PKGCONFIG --exists zlib], [
|
||||
PKGCONFIG_LIBS_PRIVATE="-lz $PKGCONFIG_LIBS_PRIVATE"
|
||||
])
|
||||
|
||||
|
||||
dnl libpng...
|
||||
AC_ARG_ENABLE([libpng], AS_HELP_STRING([--disable-libpng], [use libpng for pdfioFileCreateImageObjFromFile, default=auto]))
|
||||
AC_ARG_ENABLE([libpng], AS_HELP_STRING([--enable-libpng], [use libpng for pdfioFileCreateImageObjFromFile, default=auto]))
|
||||
|
||||
AS_IF([test "x$PKGCONFIG" != x -a x$enable_libpng != xno], [
|
||||
AC_MSG_CHECKING([for libpng-1.6.x])
|
||||
@@ -177,7 +157,11 @@ AS_IF([test "x$PKGCONFIG" != x -a x$enable_libpng != xno], [
|
||||
AC_DEFINE([HAVE_LIBPNG], 1, [Have PNG library?])
|
||||
CPPFLAGS="$($PKGCONFIG --cflags libpng16) -DHAVE_LIBPNG=1 $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs libpng16) -lz $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
AS_IF([test "x$PKGCONFIG_REQUIRES_PRIVATE" = x], [
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6"
|
||||
], [
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libpng >= 1.6, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
])
|
||||
], [
|
||||
AC_MSG_RESULT([no]);
|
||||
AS_IF([test x$enable_libpng = xyes], [
|
||||
@@ -189,28 +173,6 @@ AS_IF([test "x$PKGCONFIG" != x -a x$enable_libpng != xno], [
|
||||
])
|
||||
|
||||
|
||||
dnl libwebp...
|
||||
AC_ARG_ENABLE([libwebp], AS_HELP_STRING([--disable-libwebp], [use libwebp for pdfioFileCreateImageObjFromFile, default=auto]))
|
||||
|
||||
AS_IF([test "x$PKGCONFIG" != x -a x$enable_libwebp != xno], [
|
||||
AC_MSG_CHECKING([for libwebp])
|
||||
AS_IF([$PKGCONFIG --exists libwebp], [
|
||||
AC_MSG_RESULT([yes]);
|
||||
AC_DEFINE([HAVE_LIBWEBP], 1, [Have WebP library?])
|
||||
CPPFLAGS="$($PKGCONFIG --cflags libwebp) -DHAVE_LIBWEBP=1 $CPPFLAGS"
|
||||
LIBS="$($PKGCONFIG --libs libwebp) -lz $LIBS"
|
||||
PKGCONFIG_REQUIRES_PRIVATE="libwebp, $PKGCONFIG_REQUIRES_PRIVATE"
|
||||
], [
|
||||
AC_MSG_RESULT([no]);
|
||||
AS_IF([test x$enable_libwebp = xyes], [
|
||||
AC_MSG_ERROR([libwebp-dev required for --enable-libwebp.])
|
||||
])
|
||||
])
|
||||
], [test x$enable_libwebp = xyes], [
|
||||
AC_MSG_ERROR([libwebp-dev required for --enable-libwebp.])
|
||||
])
|
||||
|
||||
|
||||
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]))
|
||||
|
||||
220
doc/pdfio.3
@@ -1,4 +1,4 @@
|
||||
.TH pdfio 3 "pdf read/write library" "2026-01-18" "pdf read/write library"
|
||||
.TH pdfio 3 "pdf read/write library" "2025-10-05" "pdf read/write library"
|
||||
.SH NAME
|
||||
pdfio \- pdf read/write library
|
||||
.SH Introduction
|
||||
@@ -34,7 +34,7 @@ PDFio is
|
||||
.I not
|
||||
concerned with rendering or viewing a PDF file, although a PDF RIP or viewer could be written using it.
|
||||
.PP
|
||||
PDFio is Copyright \[co] 2021\-2026 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\-2025 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
|
||||
.PP
|
||||
PDFio requires the following to build the software:
|
||||
@@ -52,17 +52,11 @@ A POSIX\-compliant sh program
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
libpng (https://www.libpng.org/) 1.6 or later for full PNG image support (optional)
|
||||
ZLIB (https://www.zlib.net/) 1.0 or higher
|
||||
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
libwebp (https://developers.google.com/speed/webp) 1.0 or later for WebP image support (optional)
|
||||
|
||||
.IP \(bu 5
|
||||
.PP
|
||||
ZLIB (https://www.zlib.net/) 1.1 or later
|
||||
|
||||
|
||||
PDFio will also use libpng 1.6 or higher (https://www.libpng.org/) to provide enhanced PNG image support.
|
||||
.PP
|
||||
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
|
||||
.SS Installing PDFio
|
||||
@@ -367,28 +361,41 @@ Each PDF file contains one or more pages. The pdfioFileGetNumPages function retu
|
||||
}
|
||||
.fi
|
||||
.PP
|
||||
Each page is represented by a "page tree" object (what pdfioFileGetPage returns) that specifies information about the page and one or more "content" objects that contain the images, fonts, text, and graphics that appear on the page. Use the pdfioPageGetNumStreams and pdfioPageOpenStream functions to access the content streams for each page, pdfioObjGetDict to get the associated page object dictionary, and pdfioPageGetArray, pdfioPageGetBinary, pdfioPageGetBoolean, pdfioPageGetDate, pdfioPageGetDict, pdfioPageGetName, pdfioPageGetObj, pdfioPageGetRect, and pdfioPageGetString to get a value from the page object dictionary or its parents. For example, if you want to display the media and crop boxes for a given page:
|
||||
Each page is represented by a "page tree" object (what pdfioFileGetPage returns) that specifies information about the page and one or more "content" objects that contain the images, fonts, text, and graphics that appear on the page. Use the pdfioPageGetNumStreams and pdfioPageOpenStream functions to access the content streams for each page, and pdfioObjGetDict to get the associated page object dictionary. For example, if you want to display the media and crop boxes for a given page:
|
||||
.nf
|
||||
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
size_t count; // Number of pages
|
||||
pdfio_obj_t *page; // Current page
|
||||
pdfio_rect_t media_box; // MediaBox values
|
||||
pdfio_rect_t crop_box; // CropBox values
|
||||
pdfio_dict_t *dict; // Current page dictionary
|
||||
pdfio_array_t *media_box; // MediaBox array
|
||||
double media_values[4]; // MediaBox values
|
||||
pdfio_array_t *crop_box; // CropBox array
|
||||
double crop_values[4]; // CropBox values
|
||||
|
||||
// Iterate the pages in the PDF file
|
||||
for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
|
||||
{
|
||||
page = pdfioFileGetPage(pdf, i);
|
||||
dict = pdfioObjGetDict(page);
|
||||
|
||||
pdfioPageGetRect(page, "MediaBox", &media_box);
|
||||
pdfioPageGetRect(page, "CropBox", &crop_box);
|
||||
media_box = pdfioDictGetArray(dict, "MediaBox");
|
||||
media_values[0] = pdfioArrayGetNumber(media_box, 0);
|
||||
media_values[1] = pdfioArrayGetNumber(media_box, 1);
|
||||
media_values[2] = pdfioArrayGetNumber(media_box, 2);
|
||||
media_values[3] = pdfioArrayGetNumber(media_box, 3);
|
||||
|
||||
crop_box = pdfioDictGetArray(dict, "CropBox");
|
||||
crop_values[0] = pdfioArrayGetNumber(crop_box, 0);
|
||||
crop_values[1] = pdfioArrayGetNumber(crop_box, 1);
|
||||
crop_values[2] = pdfioArrayGetNumber(crop_box, 2);
|
||||
crop_values[3] = pdfioArrayGetNumber(crop_box, 3);
|
||||
|
||||
printf("Page %u: MediaBox=[%g %g %g %g], CropBox=[%g %g %g %g]\\n",
|
||||
(unsigned)(i + 1),
|
||||
media_box.x1, media_box.y1, media_box.x2, media_box.y2,
|
||||
crop_box.x1, crop_box.y1, crop_box.x2, crop_box.y2);
|
||||
media_values[0], media_values[1], media_values[2], media_values[3],
|
||||
crop_values[0], crop_values[1], crop_values[2], crop_values[3]);
|
||||
}
|
||||
.fi
|
||||
.PP
|
||||
@@ -777,13 +784,16 @@ will create an object for a 1024x1024 RGBA image in memory, using the default co
|
||||
.PP
|
||||
The "interpolate" argument specifies whether the colors in the image should be smoothed/interpolated when scaling. This is most useful for photographs but should be false for screenshot and barcode images.
|
||||
.PP
|
||||
If you have a GIF, JPEG, PNG, or WebP file, use the pdfioFileCreateImageObjFromFile function to copy the image into a PDF image object, for example:
|
||||
If you have a JPEG or PNG file, use the pdfioFileCreateImageObjFromFile function to copy the image into a PDF image object, for example:
|
||||
.nf
|
||||
|
||||
pdfio_file_t *pdf = pdfioFileCreate(...);
|
||||
pdfio_obj_t *img =
|
||||
pdfioFileCreateImageObjFromFile(pdf, "myphoto.jpg", /*interpolate*/true);
|
||||
.fi
|
||||
.PP
|
||||
Note: Currently pdfioFileCreateImageObjFromFile does not support 12 bit JPEG files or PNG files with an alpha channel.
|
||||
|
||||
.PP
|
||||
Page Dictionary Functions
|
||||
.PP
|
||||
@@ -1166,10 +1176,16 @@ The pdfioinfo.c example program opens a PDF file and prints the title, author, c
|
||||
for (cur = 0, prev = 0; cur < num_pages; cur ++)
|
||||
{
|
||||
// Find the MediaBox for this page in the page tree...
|
||||
page = pdfioFileGetPage(pdf, cur);
|
||||
for (page = pdfioFileGetPage(pdf, cur);
|
||||
page != NULL;
|
||||
page = pdfioDictGetObj(page_dict, "Parent"))
|
||||
{
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
page_dict = pdfioObjGetDict(page);
|
||||
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
pdfioPageGetRect(page, "MediaBox", &cur_box);
|
||||
if (pdfioDictGetRect(page_dict, "MediaBox", &cur_box))
|
||||
break;
|
||||
}
|
||||
|
||||
// If this MediaBox is different from the previous one, show the range of
|
||||
// pages that have that size...
|
||||
@@ -1442,7 +1458,7 @@ Then we loop through the differences array, keeping track of the current index w
|
||||
.fi
|
||||
.SS Create a PDF File With Text and an Image
|
||||
.PP
|
||||
The image2pdf.c example code creates a PDF file containing a GIF, JPEG, or PNG image file and optional caption on a single page. The create_pdf_image_file function creates the PDF file, embeds a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with any text centered below:
|
||||
The image2pdf.c example code creates a PDF file containing a JPEG or PNG image file and optional caption on a single page. The create_pdf_image_file function creates the PDF file, embeds a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with any text centered below:
|
||||
.nf
|
||||
|
||||
#include <pdfio.h>
|
||||
@@ -2539,7 +2555,7 @@ ASCIIHexDecode filter (reading only)
|
||||
.TP 5
|
||||
PDFIO_FILTER_CCITTFAX
|
||||
.br
|
||||
CCITTFaxDecode filter (reading only)
|
||||
CCITTFaxDecode filter
|
||||
.TP 5
|
||||
PDFIO_FILTER_CRYPT
|
||||
.br
|
||||
@@ -2555,7 +2571,7 @@ FlateDecode filter
|
||||
.TP 5
|
||||
PDFIO_FILTER_JBIG2
|
||||
.br
|
||||
JBIG2Decode filter (reading only)
|
||||
JBIG2Decode filter
|
||||
.TP 5
|
||||
PDFIO_FILTER_JPX
|
||||
.br
|
||||
@@ -4193,17 +4209,15 @@ pdfio_obj_t * pdfioFileCreateImageObjFromFile (
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function creates an image object in a PDF file from a GIF, JPEG, PNG, or
|
||||
WebP file. The "filename" parameter specifies the name of the GIF, JPEG,
|
||||
PNG, or WebP file, while the "interpolate" parameter specifies whether to
|
||||
interpolate when scaling the image on the page.
|
||||
This function creates an image object in a PDF file from a JPEG or PNG file.
|
||||
The "filename" parameter specifies the name of the JPEG or PNG file, while
|
||||
the "interpolate" parameter specifies whether to interpolate when scaling the
|
||||
image on the page.
|
||||
.PP
|
||||
.IP 5
|
||||
Note: PNG files containing transparency cannot be used when producing
|
||||
.IP 5
|
||||
PDF/A files. Files containing animation yield the final frame of the
|
||||
.IP 5
|
||||
animation.
|
||||
PDF/A files.
|
||||
.SS pdfioFileCreateNameObj
|
||||
Create a new object in a PDF file containing a name.
|
||||
.PP
|
||||
@@ -4910,91 +4924,6 @@ bool pdfioPageDictAddImage (
|
||||
pdfio_obj_t *obj
|
||||
);
|
||||
.fi
|
||||
.SS pdfioPageGetArray
|
||||
Get an array value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_array_t * pdfioPageGetArray (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up an array value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetBinary
|
||||
Get a binary value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
unsigned char * pdfioPageGetBinary (
|
||||
pdfio_obj_t *page,
|
||||
const char *key,
|
||||
size_t *length
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a binary value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetBoolean
|
||||
Get a boolean value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
bool pdfioPageGetBoolean (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a boolean value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetDate
|
||||
Get a date value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
time_t pdfioPageGetDate (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a date value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetDict
|
||||
Get a dictionary value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_dict_t * pdfioPageGetDict (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a dictionary value in the page dictionary, either in
|
||||
the specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetName
|
||||
Get a name value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
const char * pdfioPageGetName (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a name value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetNumStreams
|
||||
Get the number of content streams for a page object.
|
||||
.PP
|
||||
@@ -5003,63 +4932,6 @@ size_t pdfioPageGetNumStreams (
|
||||
pdfio_obj_t *page
|
||||
);
|
||||
.fi
|
||||
.SS pdfioPageGetNumber
|
||||
Get a number value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
double pdfioPageGetNumber (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a number value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetObj
|
||||
Get an indirect object value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_obj_t * pdfioPageGetObj (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up an indirect object value in the page dictionary,
|
||||
either in the specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetRect
|
||||
Get a rectangle value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
pdfio_rect_t * pdfioPageGetRect (
|
||||
pdfio_obj_t *page,
|
||||
const char *key,
|
||||
pdfio_rect_t *rect
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a rectangle value in the page dictionary, either in
|
||||
the specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageGetString
|
||||
Get a string value from the page dictionary.
|
||||
.PP
|
||||
.nf
|
||||
const char * pdfioPageGetString (
|
||||
pdfio_obj_t *page,
|
||||
const char *key
|
||||
);
|
||||
.fi
|
||||
.PP
|
||||
This function looks up a string value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
|
||||
.SS pdfioPageOpenStream
|
||||
Open a content stream for a page.
|
||||
.PP
|
||||
|
||||
292
doc/pdfio.html
@@ -1,13 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<title>PDFio Programming Manual v1.7.0</title>
|
||||
<title>PDFio Programming Manual v1.6.0</title>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
|
||||
<meta name="generator" content="codedoc v3.8">
|
||||
<meta name="author" content="Michael R Sweet">
|
||||
<meta name="language" content="en-US">
|
||||
<meta name="copyright" content="Copyright © 2021-2025 by Michael R Sweet">
|
||||
<meta name="version" content="1.7.0">
|
||||
<meta name="version" content="1.6.0">
|
||||
<style type="text/css"><!--
|
||||
body {
|
||||
background: white;
|
||||
@@ -92,16 +92,13 @@ blockquote :first-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
p code, li code, p.code, pre, ul.code li {
|
||||
background: rgba(127,127,127,0.25);
|
||||
border: thin dotted gray;
|
||||
font-family: monospace;
|
||||
hyphens: manual;
|
||||
-webkit-hyphens: manual;
|
||||
}
|
||||
p code, li code {
|
||||
padding: 0 5px;
|
||||
}
|
||||
p.code, pre, ul.code li {
|
||||
background: rgba(127,127,127,0.25);
|
||||
border: thin dotted gray;
|
||||
padding: 10px;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
@@ -220,21 +217,6 @@ span.string {
|
||||
a:link:hover, a:visited:hover, a:active {
|
||||
color: #f06;
|
||||
}
|
||||
span.comment {
|
||||
color: #7c7;
|
||||
}
|
||||
span.directive {
|
||||
color: red;
|
||||
}
|
||||
span.number {
|
||||
color: #c64;
|
||||
}
|
||||
span.reserved {
|
||||
color: #77f;
|
||||
}
|
||||
span.string {
|
||||
color: #f7f;
|
||||
}
|
||||
}
|
||||
/* Show contents on left side in web browser */
|
||||
@media screen and (min-width: 800px) {
|
||||
@@ -269,7 +251,7 @@ span.string {
|
||||
<body>
|
||||
<div class="header">
|
||||
<p><img class="title" src="pdfio-512.png"></p>
|
||||
<h1 class="title">PDFio Programming Manual v1.7.0</h1>
|
||||
<h1 class="title">PDFio Programming Manual v1.6.0</h1>
|
||||
<p>Michael R Sweet</p>
|
||||
<p>Copyright © 2021-2025 by Michael R Sweet</p>
|
||||
</div>
|
||||
@@ -482,17 +464,7 @@ span.string {
|
||||
<li><a href="#pdfioPageDictAddColorSpace">pdfioPageDictAddColorSpace</a></li>
|
||||
<li><a href="#pdfioPageDictAddFont">pdfioPageDictAddFont</a></li>
|
||||
<li><a href="#pdfioPageDictAddImage">pdfioPageDictAddImage</a></li>
|
||||
<li><a href="#pdfioPageGetArray">pdfioPageGetArray</a></li>
|
||||
<li><a href="#pdfioPageGetBinary">pdfioPageGetBinary</a></li>
|
||||
<li><a href="#pdfioPageGetBoolean">pdfioPageGetBoolean</a></li>
|
||||
<li><a href="#pdfioPageGetDate">pdfioPageGetDate</a></li>
|
||||
<li><a href="#pdfioPageGetDict">pdfioPageGetDict</a></li>
|
||||
<li><a href="#pdfioPageGetName">pdfioPageGetName</a></li>
|
||||
<li><a href="#pdfioPageGetNumStreams">pdfioPageGetNumStreams</a></li>
|
||||
<li><a href="#pdfioPageGetNumber">pdfioPageGetNumber</a></li>
|
||||
<li><a href="#pdfioPageGetObj">pdfioPageGetObj</a></li>
|
||||
<li><a href="#pdfioPageGetRect">pdfioPageGetRect</a></li>
|
||||
<li><a href="#pdfioPageGetString">pdfioPageGetString</a></li>
|
||||
<li><a href="#pdfioPageOpenStream">pdfioPageOpenStream</a></li>
|
||||
<li><a href="#pdfioStreamClose">pdfioStreamClose</a></li>
|
||||
<li><a href="#pdfioStreamConsume">pdfioStreamConsume</a></li>
|
||||
@@ -560,7 +532,7 @@ span.string {
|
||||
</li>
|
||||
</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 Copyright © 2021-2026 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.</p>
|
||||
<p>PDFio is Copyright © 2021-2025 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.</p>
|
||||
<h3 class="title" id="requirements">Requirements</h3>
|
||||
<p>PDFio requires the following to build the software:</p>
|
||||
<ul>
|
||||
@@ -570,13 +542,10 @@ span.string {
|
||||
</li>
|
||||
<li><p>A POSIX-compliant <code>sh</code> program</p>
|
||||
</li>
|
||||
<li><p>libpng (<a href="https://www.libpng.org/">https://www.libpng.org/</a>) 1.6 or later for full PNG image support (optional)</p>
|
||||
</li>
|
||||
<li><p>libwebp (<a href="https://developers.google.com/speed/webp">https://developers.google.com/speed/webp</a>) 1.0 or later for WebP image support (optional)</p>
|
||||
</li>
|
||||
<li><p>ZLIB (<a href="https://www.zlib.net/">https://www.zlib.net/</a>) 1.1 or later</p>
|
||||
<li><p>ZLIB (<a href="https://www.zlib.net/">https://www.zlib.net/</a>) 1.0 or higher</p>
|
||||
</li>
|
||||
</ul>
|
||||
<p>PDFio will also use libpng 1.6 or higher (<a href="https://www.libpng.org/">https://www.libpng.org/</a>) to provide enhanced PNG image support.</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>
|
||||
<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>
|
||||
@@ -795,26 +764,39 @@ pdfio_obj_t *page; <span class="comment">// Current page</span>
|
||||
<span class="comment">// do something with page</span>
|
||||
}
|
||||
</code></pre>
|
||||
<p>Each page is represented by a "page tree" object (what <a href="#pdfioFileGetPage"><code>pdfioFileGetPage</code></a> 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 <a href="#pdfioPageGetNumStreams"><code>pdfioPageGetNumStreams</code></a> and <a href="#pdfioPageOpenStream"><code>pdfioPageOpenStream</code></a> functions to access the content streams for each page, <a href="#pdfioObjGetDict"><code>pdfioObjGetDict</code></a> to get the associated page object dictionary, and <a href="#pdfioPageGetArray"><code>pdfioPageGetArray</code></a>, <a href="#pdfioPageGetBinary"><code>pdfioPageGetBinary</code></a>, <a href="#pdfioPageGetBoolean"><code>pdfioPageGetBoolean</code></a>, <a href="#pdfioPageGetDate"><code>pdfioPageGetDate</code></a>, <a href="#pdfioPageGetDict"><code>pdfioPageGetDict</code></a>, <a href="#pdfioPageGetName"><code>pdfioPageGetName</code></a>, <a href="#pdfioPageGetObj"><code>pdfioPageGetObj</code></a>, <a href="#pdfioPageGetRect"><code>pdfioPageGetRect</code></a>, and <a href="#pdfioPageGetString"><code>pdfioPageGetString</code></a> to get a value from the page object dictionary or its parents. For example, if you want to display the media and crop boxes for a given page:</p>
|
||||
<p>Each page is represented by a "page tree" object (what <a href="#pdfioFileGetPage"><code>pdfioFileGetPage</code></a> 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 <a href="#pdfioPageGetNumStreams"><code>pdfioPageGetNumStreams</code></a> and <a href="#pdfioPageOpenStream"><code>pdfioPageOpenStream</code></a> functions to access the content streams for each page, and <a href="#pdfioObjGetDict"><code>pdfioObjGetDict</code></a> to get the associated page object dictionary. For example, if you want to display the media and crop boxes for a given page:</p>
|
||||
<pre><code class="language-c">pdfio_file_t *pdf; <span class="comment">// PDF file</span>
|
||||
size_t i; <span class="comment">// Looping var</span>
|
||||
size_t count; <span class="comment">// Number of pages</span>
|
||||
pdfio_obj_t *page; <span class="comment">// Current page</span>
|
||||
pdfio_rect_t media_box; <span class="comment">// MediaBox values</span>
|
||||
pdfio_rect_t crop_box; <span class="comment">// CropBox values</span>
|
||||
pdfio_dict_t *dict; <span class="comment">// Current page dictionary</span>
|
||||
pdfio_array_t *media_box; <span class="comment">// MediaBox array</span>
|
||||
<span class="reserved">double</span> media_values[<span class="number">4</span>]; <span class="comment">// MediaBox values</span>
|
||||
pdfio_array_t *crop_box; <span class="comment">// CropBox array</span>
|
||||
<span class="reserved">double</span> crop_values[<span class="number">4</span>]; <span class="comment">// CropBox values</span>
|
||||
|
||||
<span class="comment">// Iterate the pages in the PDF file</span>
|
||||
<span class="reserved">for</span> (i = <span class="number">0</span>, count = pdfioFileGetNumPages(pdf); i < count; i ++)
|
||||
{
|
||||
page = pdfioFileGetPage(pdf, i);
|
||||
dict = pdfioObjGetDict(page);
|
||||
|
||||
pdfioPageGetRect(page, <span class="string">"MediaBox"</span>, &media_box);
|
||||
pdfioPageGetRect(page, <span class="string">"CropBox"</span>, &crop_box);
|
||||
media_box = pdfioDictGetArray(dict, <span class="string">"MediaBox"</span>);
|
||||
media_values[<span class="number">0</span>] = pdfioArrayGetNumber(media_box, <span class="number">0</span>);
|
||||
media_values[<span class="number">1</span>] = pdfioArrayGetNumber(media_box, <span class="number">1</span>);
|
||||
media_values[<span class="number">2</span>] = pdfioArrayGetNumber(media_box, <span class="number">2</span>);
|
||||
media_values[<span class="number">3</span>] = pdfioArrayGetNumber(media_box, <span class="number">3</span>);
|
||||
|
||||
crop_box = pdfioDictGetArray(dict, <span class="string">"CropBox"</span>);
|
||||
crop_values[<span class="number">0</span>] = pdfioArrayGetNumber(crop_box, <span class="number">0</span>);
|
||||
crop_values[<span class="number">1</span>] = pdfioArrayGetNumber(crop_box, <span class="number">1</span>);
|
||||
crop_values[<span class="number">2</span>] = pdfioArrayGetNumber(crop_box, <span class="number">2</span>);
|
||||
crop_values[<span class="number">3</span>] = pdfioArrayGetNumber(crop_box, <span class="number">3</span>);
|
||||
|
||||
printf(<span class="string">"Page %u: MediaBox=[%g %g %g %g], CropBox=[%g %g %g %g]\n"</span>,
|
||||
(<span class="reserved">unsigned</span>)(i + <span class="number">1</span>),
|
||||
media_box.x1, media_box.y1, media_box.x2, media_box.y2,
|
||||
crop_box.x1, crop_box.y1, crop_box.x2, crop_box.y2);
|
||||
media_values[<span class="number">0</span>], media_values[<span class="number">1</span>], media_values[<span class="number">2</span>], media_values[<span class="number">3</span>],
|
||||
crop_values[<span class="number">0</span>], crop_values[<span class="number">1</span>], crop_values[<span class="number">2</span>], crop_values[<span class="number">3</span>]);
|
||||
}
|
||||
</code></pre>
|
||||
<p>Page object dictionaries have several (mostly optional) key/value pairs, including:</p>
|
||||
@@ -1049,11 +1031,14 @@ pdfio_obj_t *img =
|
||||
<span class="comment">/*alpha*/</span><span class="reserved">true</span>, <span class="comment">/*interpolate*/</span><span class="reserved">false</span>);
|
||||
</code></pre>
|
||||
<p>The "interpolate" argument specifies whether the colors in the image should be smoothed/interpolated when scaling. This is most useful for photographs but should be <code>false</code> for screenshot and barcode images.</p>
|
||||
<p>If you have a GIF, JPEG, PNG, or WebP file, use the <a href="#pdfioFileCreateImageObjFromFile"><code>pdfioFileCreateImageObjFromFile</code></a> function to copy the image into a PDF image object, for example:</p>
|
||||
<p>If you have a JPEG or PNG file, use the <a href="#pdfioFileCreateImageObjFromFile"><code>pdfioFileCreateImageObjFromFile</code></a> function to copy the image into a PDF image object, for example:</p>
|
||||
<pre><code class="language-c">pdfio_file_t *pdf = pdfioFileCreate(...);
|
||||
pdfio_obj_t *img =
|
||||
pdfioFileCreateImageObjFromFile(pdf, <span class="string">"myphoto.jpg"</span>, <span class="comment">/*interpolate*/</span><span class="reserved">true</span>);
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>Note: Currently <code>pdfioFileCreateImageObjFromFile</code> does not support 12 bit JPEG files or PNG files with an alpha channel.</p>
|
||||
</blockquote>
|
||||
<h4 id="page-dictionary-functions">Page Dictionary Functions</h4>
|
||||
<p>PDF pages each have an associated dictionary to specify the images, fonts, and color spaces used by the page. PDFio provides functions to add these resources to the dictionary:</p>
|
||||
<ul>
|
||||
@@ -1309,10 +1294,16 @@ main(<span class="reserved">int</span> argc, <span clas
|
||||
<span class="reserved">for</span> (cur = <span class="number">0</span>, prev = <span class="number">0</span>; cur < num_pages; cur ++)
|
||||
{
|
||||
<span class="comment">// Find the MediaBox for this page in the page tree...</span>
|
||||
page = pdfioFileGetPage(pdf, cur);
|
||||
<span class="reserved">for</span> (page = pdfioFileGetPage(pdf, cur);
|
||||
page != NULL;
|
||||
page = pdfioDictGetObj(page_dict, <span class="string">"Parent"</span>))
|
||||
{
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = <span class="number">0.0</span>;
|
||||
page_dict = pdfioObjGetDict(page);
|
||||
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = <span class="number">0.0</span>;
|
||||
pdfioPageGetRect(page, <span class="string">"MediaBox"</span>, &cur_box);
|
||||
<span class="reserved">if</span> (pdfioDictGetRect(page_dict, <span class="string">"MediaBox"</span>, &cur_box))
|
||||
<span class="reserved">break</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// If this MediaBox is different from the previous one, show the range of</span>
|
||||
<span class="comment">// pages that have that size...</span>
|
||||
@@ -1551,7 +1542,7 @@ load_encoding(
|
||||
}
|
||||
</code></pre>
|
||||
<h3 class="title" id="create-a-pdf-file-with-text-and-an-image">Create a PDF File With Text and an Image</h3>
|
||||
<p>The <code>image2pdf.c</code> example code creates a PDF file containing a GIF, JPEG, or PNG image file and optional caption on a single page. The <code>create_pdf_image_file</code> function creates the PDF file, embeds a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with any text centered below:</p>
|
||||
<p>The <code>image2pdf.c</code> example code creates a PDF file containing a JPEG or PNG image file and optional caption on a single page. The <code>create_pdf_image_file</code> function creates the PDF file, embeds a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with any text centered below:</p>
|
||||
<pre><code class="language-c"><span class="directive">#include <pdfio.h></span>
|
||||
<span class="directive">#include <pdfio-content.h></span>
|
||||
<span class="directive">#include <string.h></span>
|
||||
@@ -4483,15 +4474,14 @@ files do not support alpha-based transparency.</blockquote>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Object</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function creates an image object in a PDF file from a GIF, JPEG, PNG, or
|
||||
WebP file. The "filename" parameter specifies the name of the GIF, JPEG,
|
||||
PNG, or WebP file, while the "interpolate" parameter specifies whether to
|
||||
interpolate when scaling the image on the page.<br>
|
||||
<p class="discussion">This function creates an image object in a PDF file from a JPEG or PNG file.
|
||||
The "filename" parameter specifies the name of the JPEG or PNG file, while
|
||||
the "interpolate" parameter specifies whether to interpolate when scaling the
|
||||
image on the page.<br>
|
||||
<br>
|
||||
</p><blockquote>
|
||||
Note: PNG files containing transparency cannot be used when producing
|
||||
PDF/A files. Files containing animation yield the final frame of the
|
||||
animation.</blockquote>
|
||||
PDF/A files.</blockquote>
|
||||
<h3 class="function"><span class="info"> PDFio v1.4 </span><a id="pdfioFileCreateNameObj">pdfioFileCreateNameObj</a></h3>
|
||||
<p class="description">Create a new object in a PDF file containing a name.</p>
|
||||
<p class="code">
|
||||
@@ -5419,116 +5409,6 @@ array that was created using the
|
||||
</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"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetArray">pdfioPageGetArray</a></h3>
|
||||
<p class="description">Get an array value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<a href="#pdfio_array_t">pdfio_array_t</a> *pdfioPageGetArray(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Array or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up an array value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetBinary">pdfioPageGetBinary</a></h3>
|
||||
<p class="description">Get a binary value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<span class="reserved">unsigned</span> <span class="reserved">char</span> *pdfioPageGetBinary(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key, size_t *length);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
<tr><th>length</th>
|
||||
<td class="description">Length of value</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Pointer to binary data or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a binary value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetBoolean">pdfioPageGetBoolean</a></h3>
|
||||
<p class="description">Get a boolean value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<span class="reserved">bool</span> pdfioPageGetBoolean(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Boolean value or <code>false</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a boolean value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetDate">pdfioPageGetDate</a></h3>
|
||||
<p class="description">Get a date value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
time_t pdfioPageGetDate(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Date/time or <code>0</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a date value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetDict">pdfioPageGetDict</a></h3>
|
||||
<p class="description">Get a dictionary value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<a href="#pdfio_dict_t">pdfio_dict_t</a> *pdfioPageGetDict(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Dictionary or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a dictionary value in the page dictionary, either in
|
||||
the specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetName">pdfioPageGetName</a></h3>
|
||||
<p class="description">Get a name value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *pdfioPageGetName(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Name string or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a name value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</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">
|
||||
@@ -5540,80 +5420,6 @@ size_t pdfioPageGetNumStreams(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page);</p>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Number of streams</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetNumber">pdfioPageGetNumber</a></h3>
|
||||
<p class="description">Get a number value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<span class="reserved">double</span> pdfioPageGetNumber(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Number value or <code>0.0</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a number value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetObj">pdfioPageGetObj</a></h3>
|
||||
<p class="description">Get an indirect object value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<a href="#pdfio_obj_t">pdfio_obj_t</a> *pdfioPageGetObj(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Object or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up an indirect object value in the page dictionary,
|
||||
either in the specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetRect">pdfioPageGetRect</a></h3>
|
||||
<p class="description">Get a rectangle value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<a href="#pdfio_rect_t">pdfio_rect_t</a> *pdfioPageGetRect(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key, <a href="#pdfio_rect_t">pdfio_rect_t</a> *rect);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
<tr><th>rect</th>
|
||||
<td class="description">Rectangle</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">Rectangle or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a rectangle value in the page dictionary, either in
|
||||
the specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><span class="info"> PDFio 1.7 </span><a id="pdfioPageGetString">pdfioPageGetString</a></h3>
|
||||
<p class="description">Get a string value from the page dictionary.</p>
|
||||
<p class="code">
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *pdfioPageGetString(<a href="#pdfio_obj_t">pdfio_obj_t</a> *page, <span class="reserved">const</span> <span class="reserved">char</span> *key);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>page</th>
|
||||
<td class="description">Page object</td></tr>
|
||||
<tr><th>key</th>
|
||||
<td class="description">Dictionary key</td></tr>
|
||||
</tbody></table>
|
||||
<h4 class="returnvalue">Return Value</h4>
|
||||
<p class="description">String value or <code>NULL</code> if none</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">This function looks up a string value in the page dictionary, either in the
|
||||
specified object or one of its parents.
|
||||
|
||||
</p>
|
||||
<h3 class="function"><a id="pdfioPageOpenStream">pdfioPageOpenStream</a></h3>
|
||||
<p class="description">Open a content stream for a page.</p>
|
||||
<p class="code">
|
||||
@@ -5953,11 +5759,11 @@ typedef enum <a href="#pdfio_valtype_e">pdfio_valtype_e</a> pdfio_valtype_t;
|
||||
<table class="list"><tbody>
|
||||
<tr><th>PDFIO_FILTER_ASCII85 </th><td class="description">ASCII85Decode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_ASCIIHEX </th><td class="description">ASCIIHexDecode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_CCITTFAX </th><td class="description">CCITTFaxDecode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_CCITTFAX </th><td class="description">CCITTFaxDecode filter</td></tr>
|
||||
<tr><th>PDFIO_FILTER_CRYPT </th><td class="description">Encryption filter</td></tr>
|
||||
<tr><th>PDFIO_FILTER_DCT </th><td class="description">DCTDecode (JPEG) filter</td></tr>
|
||||
<tr><th>PDFIO_FILTER_FLATE </th><td class="description">FlateDecode filter</td></tr>
|
||||
<tr><th>PDFIO_FILTER_JBIG2 </th><td class="description">JBIG2Decode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_JBIG2 </th><td class="description">JBIG2Decode filter</td></tr>
|
||||
<tr><th>PDFIO_FILTER_JPX </th><td class="description">JPXDecode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_LZW </th><td class="description">LZWDecode filter (reading only)</td></tr>
|
||||
<tr><th>PDFIO_FILTER_NONE </th><td class="description">No filter</td></tr>
|
||||
|
||||
69
doc/pdfio.md
@@ -15,7 +15,7 @@ goals of PDFio are:
|
||||
PDFio is *not* concerned with rendering or viewing a PDF file, although a PDF
|
||||
RIP or viewer could be written using it.
|
||||
|
||||
PDFio is Copyright © 2021-2026 by Michael R Sweet and is licensed under the
|
||||
PDFio is Copyright © 2021-2025 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.
|
||||
|
||||
@@ -28,11 +28,10 @@ PDFio requires the following to build the software:
|
||||
- A C99 compiler such as Clang, GCC, or MS Visual C
|
||||
- A POSIX-compliant `make` program
|
||||
- A POSIX-compliant `sh` program
|
||||
- libpng (<https://www.libpng.org/>) 1.6 or later for full PNG image support
|
||||
(optional)
|
||||
- libwebp (<https://developers.google.com/speed/webp>) 1.0 or later for WebP
|
||||
image support (optional)
|
||||
- ZLIB (<https://www.zlib.net/>) 1.1 or later
|
||||
- ZLIB (<https://www.zlib.net/>) 1.0 or higher
|
||||
|
||||
PDFio will also use libpng 1.6 or higher (<https://www.libpng.org/>) to provide
|
||||
enhanced PNG image support.
|
||||
|
||||
IDE files for Xcode (macOS/iOS) and Visual Studio (Windows) are also provided.
|
||||
|
||||
@@ -388,35 +387,43 @@ 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, [`pdfioObjGetDict`](@@)
|
||||
to get the associated page object dictionary, and [`pdfioPageGetArray`](@@),
|
||||
[`pdfioPageGetBinary`](@@), [`pdfioPageGetBoolean`](@@),
|
||||
[`pdfioPageGetDate`](@@), [`pdfioPageGetDict`](@@), [`pdfioPageGetName`](@@),
|
||||
[`pdfioPageGetObj`](@@), [`pdfioPageGetRect`](@@), and
|
||||
[`pdfioPageGetString`](@@) to get a value from the page object dictionary or its
|
||||
parents. For example, if you want to display the media and crop boxes for a
|
||||
given page:
|
||||
functions to access the content streams for each page, and
|
||||
[`pdfioObjGetDict`](@@) to get the associated page object dictionary. For
|
||||
example, if you want to display the media and crop boxes for a given page:
|
||||
|
||||
```c
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
size_t count; // Number of pages
|
||||
pdfio_obj_t *page; // Current page
|
||||
pdfio_rect_t media_box; // MediaBox values
|
||||
pdfio_rect_t crop_box; // CropBox values
|
||||
pdfio_dict_t *dict; // Current page dictionary
|
||||
pdfio_array_t *media_box; // MediaBox array
|
||||
double media_values[4]; // MediaBox values
|
||||
pdfio_array_t *crop_box; // CropBox array
|
||||
double crop_values[4]; // CropBox values
|
||||
|
||||
// Iterate the pages in the PDF file
|
||||
for (i = 0, count = pdfioFileGetNumPages(pdf); i < count; i ++)
|
||||
{
|
||||
page = pdfioFileGetPage(pdf, i);
|
||||
dict = pdfioObjGetDict(page);
|
||||
|
||||
pdfioPageGetRect(page, "MediaBox", &media_box);
|
||||
pdfioPageGetRect(page, "CropBox", &crop_box);
|
||||
media_box = pdfioDictGetArray(dict, "MediaBox");
|
||||
media_values[0] = pdfioArrayGetNumber(media_box, 0);
|
||||
media_values[1] = pdfioArrayGetNumber(media_box, 1);
|
||||
media_values[2] = pdfioArrayGetNumber(media_box, 2);
|
||||
media_values[3] = pdfioArrayGetNumber(media_box, 3);
|
||||
|
||||
crop_box = pdfioDictGetArray(dict, "CropBox");
|
||||
crop_values[0] = pdfioArrayGetNumber(crop_box, 0);
|
||||
crop_values[1] = pdfioArrayGetNumber(crop_box, 1);
|
||||
crop_values[2] = pdfioArrayGetNumber(crop_box, 2);
|
||||
crop_values[3] = pdfioArrayGetNumber(crop_box, 3);
|
||||
|
||||
printf("Page %u: MediaBox=[%g %g %g %g], CropBox=[%g %g %g %g]\n",
|
||||
(unsigned)(i + 1),
|
||||
media_box.x1, media_box.y1, media_box.x2, media_box.y2,
|
||||
crop_box.x1, crop_box.y1, crop_box.x2, crop_box.y2);
|
||||
media_values[0], media_values[1], media_values[2], media_values[3],
|
||||
crop_values[0], crop_values[1], crop_values[2], crop_values[3]);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -753,9 +760,8 @@ The "interpolate" argument specifies whether the colors in the image should be
|
||||
smoothed/interpolated when scaling. This is most useful for photographs but
|
||||
should be `false` for screenshot and barcode images.
|
||||
|
||||
If you have a GIF, JPEG, PNG, or WebP file, use the
|
||||
[`pdfioFileCreateImageObjFromFile`](@@) function to copy the image into a PDF
|
||||
image object, for example:
|
||||
If you have a JPEG or PNG file, use the [`pdfioFileCreateImageObjFromFile`](@@)
|
||||
function to copy the image into a PDF image object, for example:
|
||||
|
||||
```c
|
||||
pdfio_file_t *pdf = pdfioFileCreate(...);
|
||||
@@ -763,6 +769,9 @@ pdfio_obj_t *img =
|
||||
pdfioFileCreateImageObjFromFile(pdf, "myphoto.jpg", /*interpolate*/true);
|
||||
```
|
||||
|
||||
> Note: Currently `pdfioFileCreateImageObjFromFile` does not support 12 bit JPEG
|
||||
> files or PNG files with an alpha channel.
|
||||
|
||||
|
||||
### Page Dictionary Functions
|
||||
|
||||
@@ -1020,10 +1029,16 @@ main(int argc, // I - Number of command-line arguments
|
||||
for (cur = 0, prev = 0; cur < num_pages; cur ++)
|
||||
{
|
||||
// Find the MediaBox for this page in the page tree...
|
||||
page = pdfioFileGetPage(pdf, cur);
|
||||
for (page = pdfioFileGetPage(pdf, cur);
|
||||
page != NULL;
|
||||
page = pdfioDictGetObj(page_dict, "Parent"))
|
||||
{
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
page_dict = pdfioObjGetDict(page);
|
||||
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
pdfioPageGetRect(page, "MediaBox", &cur_box);
|
||||
if (pdfioDictGetRect(page_dict, "MediaBox", &cur_box))
|
||||
break;
|
||||
}
|
||||
|
||||
// If this MediaBox is different from the previous one, show the range of
|
||||
// pages that have that size...
|
||||
@@ -1327,7 +1342,7 @@ Unicode glyph for the current index:
|
||||
Create a PDF File With Text and an Image
|
||||
----------------------------------------
|
||||
|
||||
The `image2pdf.c` example code creates a PDF file containing a GIF, JPEG, or PNG
|
||||
The `image2pdf.c` example code creates a PDF file containing a JPEG or PNG
|
||||
image file and optional caption on a single page. The `create_pdf_image_file`
|
||||
function creates the PDF file, embeds a base font and the named JPEG or PNG
|
||||
image file, and then creates a page with the image centered on the page with any
|
||||
|
||||
@@ -13,10 +13,6 @@
|
||||
|
||||
#include <pdfio.h>
|
||||
#include <pdfio-content.h>
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
# include <fcntl.h>
|
||||
#endif // _WIN32
|
||||
|
||||
|
||||
//
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#include <math.h>
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
# include <fcntl.h>
|
||||
#else
|
||||
# include <unistd.h>
|
||||
#endif // _WIN32
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF metadata example for PDFio.
|
||||
//
|
||||
// Copyright © 2023-2026 by Michael R Sweet.
|
||||
// Copyright © 2023-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -113,10 +113,16 @@ main(int argc, // I - Number of command-line arguments
|
||||
for (cur = 0, prev = 0; cur < num_pages; cur ++)
|
||||
{
|
||||
// Find the MediaBox for this page in the page tree...
|
||||
page = pdfioFileGetPage(pdf, cur);
|
||||
for (page = pdfioFileGetPage(pdf, cur);
|
||||
page != NULL;
|
||||
page = pdfioDictGetObj(page_dict, "Parent"))
|
||||
{
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
page_dict = pdfioObjGetDict(page);
|
||||
|
||||
cur_box.x1 = cur_box.x2 = cur_box.y1 = cur_box.y2 = 0.0;
|
||||
pdfioPageGetRect(page, "MediaBox", &cur_box);
|
||||
if (pdfioDictGetRect(page_dict, "MediaBox", &cur_box))
|
||||
break;
|
||||
}
|
||||
|
||||
// If this MediaBox is different from the previous one, show the range of
|
||||
// pages that have that size...
|
||||
|
||||
41
makesrcdist
@@ -7,10 +7,6 @@
|
||||
# ./makesrcdist [--snapshot] VERSION
|
||||
#
|
||||
|
||||
# Save the current directory...
|
||||
basedir="$(pwd)"
|
||||
|
||||
|
||||
# Support "--snapshot" option...
|
||||
if test "$1" == "--snapshot"; then
|
||||
shift
|
||||
@@ -19,21 +15,18 @@ else
|
||||
snapshot=0
|
||||
fi
|
||||
|
||||
|
||||
# Get the release version...
|
||||
# Get version...
|
||||
if test $# != 1; then
|
||||
echo "Usage: ./makesrcdist [--snapshot] VERSION"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
status=0
|
||||
version=$1
|
||||
version_major=$(echo $1 | awk -F. '{print $1}')
|
||||
version_minor=$(echo $1 | awk -F. '{print $2}')
|
||||
|
||||
|
||||
# Check that version number has been updated everywhere...
|
||||
status=0
|
||||
|
||||
if test $(grep AC_INIT configure.ac | awk '{print $2}') != "[$version],"; then
|
||||
echo "Still need to update AC_INIT version in 'configure.ac'."
|
||||
status=1
|
||||
@@ -85,32 +78,18 @@ if test $status = 1; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
# Tag release...
|
||||
if test $snapshot = 0; then
|
||||
echo "Creating tag v$version for release..."
|
||||
echo Creating tag for release...
|
||||
git tag -m "Tag $version" v$version
|
||||
git push origin v$version
|
||||
fi
|
||||
|
||||
# Make source archives...
|
||||
echo Creating pdfio-$version.tar.gz...
|
||||
git archive --format tar --prefix=pdfio-$version/ HEAD | gzip -v9 >pdfio-$version.tar.gz
|
||||
gpg --detach-sign pdfio-$version.tar.gz
|
||||
|
||||
# Make and sign source archives...
|
||||
echo "Exporting $version..."
|
||||
rm -rf $TMPDIR/pdfio-$version
|
||||
mkdir $TMPDIR/pdfio-$version
|
||||
git archive --format tar HEAD | (cd $TMPDIR/pdfio-$version; tar xf -)
|
||||
(cd ttf; git archive --prefix=ttf/ HEAD) | (cd $TMPDIR/pdfio-$version; tar xf -)
|
||||
cd $TMPDIR
|
||||
|
||||
echo "Creating pdfio-$version.tar.gz..."
|
||||
tar cf - pdfio-$version | gzip -v9 >"$basedir/pdfio-$version.tar.gz"
|
||||
gpg --detach-sign "$basedir/pdfio-$version.tar.gz"
|
||||
|
||||
echo "Creating pdfio-$version.zip..."
|
||||
zip -r "$basedir/pdfio-$version.zip" pdfio-$version
|
||||
gpg --detach-sign "$basedir/pdfio-$version.zip"
|
||||
|
||||
|
||||
# Clean up...
|
||||
echo "Removing temporary files..."
|
||||
rm -rf pdfio-$version
|
||||
echo Creating pdfio-$version.zip...
|
||||
git archive --format zip --prefix=pdfio-$version/ HEAD >pdfio-$version.zip
|
||||
gpg --detach-sign pdfio-$version.zip
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// AES functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -117,7 +117,7 @@ _pdfioCryptoAESInit(
|
||||
memcpy(ctx->round_key, key, keylen);
|
||||
|
||||
// All other round keys are found from the previous round keys.
|
||||
for (rkptr0 = ctx->round_key, rkptr = rkptr0 + keylen, rkend = rkptr0 + 16 * ctx->round_size + 16, i = nwords; rkptr < rkend; i ++)
|
||||
for (rkptr0 = ctx->round_key, rkptr = rkptr0 + keylen, rkend = rkptr + 16 * ctx->round_size, i = nwords; rkptr < rkend; i ++)
|
||||
{
|
||||
if ((i % nwords) == 0)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF array functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2024 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -264,10 +264,6 @@ pdfioArrayCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
PDFIO_DEBUG("pdfioArrayCopy(pdf=%p, a=%p(%p))\n", (void *)pdf, (void *)a, a ? (void *)a->pdf : NULL);
|
||||
|
||||
// Range check input...
|
||||
if (!pdf || !a)
|
||||
return (NULL);
|
||||
|
||||
// Create the new array...
|
||||
if ((na = pdfioArrayCreate(pdf)) == NULL)
|
||||
return (NULL);
|
||||
@@ -670,29 +666,27 @@ pdfioArrayRemove(pdfio_array_t *a, // I - Array
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` otherwise
|
||||
_pdfioArrayWrite(
|
||||
_pdfio_printf_t cb, // I - Printf callback function
|
||||
void *cbdata, // I - Printf callback data
|
||||
pdfio_obj_t *obj, // I - Object, if any
|
||||
pdfio_array_t *a) // I - Array
|
||||
_pdfioArrayWrite(pdfio_array_t *a, // I - Array
|
||||
pdfio_obj_t *obj) // I - Object, if any
|
||||
{
|
||||
pdfio_file_t *pdf = a->pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
_pdfio_value_t *v; // Current value
|
||||
|
||||
|
||||
// Arrays are surrounded by square brackets ([ ... ])
|
||||
if (!(cb)(cbdata, "["))
|
||||
if (!_pdfioFilePuts(pdf, "["))
|
||||
return (false);
|
||||
|
||||
// Write each value...
|
||||
for (i = a->num_values, v = a->values; i > 0; i --, v ++)
|
||||
{
|
||||
if (!_pdfioValueWrite(cb, cbdata, obj, v, NULL))
|
||||
if (!_pdfioValueWrite(pdf, obj, v, NULL))
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Closing bracket...
|
||||
return ((cb)(cbdata, "]"));
|
||||
return (_pdfioFilePuts(pdf, "]"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
636
pdfio-content.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Content helper functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -12,13 +12,9 @@
|
||||
#include "pdfio-base-font-widths.h"
|
||||
#include "pdfio-cgats001-compat.h"
|
||||
#include "ttf.h"
|
||||
#include <sys/stat.h>
|
||||
#ifdef HAVE_LIBPNG
|
||||
# include <png.h>
|
||||
#endif // HAVE_LIBPNG
|
||||
#ifdef HAVE_LIBWEBP
|
||||
# include <webp/decode.h>
|
||||
#endif // HAVE_LIBWEBP
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846264338327950288
|
||||
@@ -29,10 +25,6 @@
|
||||
// Local constants...
|
||||
//
|
||||
|
||||
#define _PDFIO_GIF_COLORBITS 0x07 // Color bits
|
||||
#define _PDFIO_GIF_INTERLACE 0x40 // Interlace flag
|
||||
#define _PDFIO_GIF_COLORMAP 0x80 // Colormap present flag
|
||||
|
||||
#define _PDFIO_JPEG_SOF0 0xc0 // Start of frame (0)
|
||||
#define _PDFIO_JPEG_SOF1 0xc1 // Start of frame (1)
|
||||
#define _PDFIO_JPEG_SOF2 0xc2 // Start of frame (2)
|
||||
@@ -83,8 +75,6 @@
|
||||
// Local types...
|
||||
//
|
||||
|
||||
typedef uint8_t _pdfio_gif_cmap_t[256][3];
|
||||
|
||||
typedef pdfio_obj_t *(*_pdfio_image_func_t)(pdfio_dict_t *dict, int fd);
|
||||
|
||||
|
||||
@@ -92,30 +82,19 @@ typedef pdfio_obj_t *(*_pdfio_image_func_t)(pdfio_dict_t *dict, int fd);
|
||||
// Local functions...
|
||||
//
|
||||
|
||||
static pdfio_obj_t *copy_gif(pdfio_dict_t *dict, int fd);
|
||||
static pdfio_obj_t *copy_jpeg(pdfio_dict_t *dict, int fd);
|
||||
static pdfio_obj_t *copy_png(pdfio_dict_t *dict, int fd);
|
||||
#ifdef HAVE_LIBWEBP
|
||||
static pdfio_obj_t *copy_webp(pdfio_dict_t *dict, int fd);
|
||||
#endif // HAVE_LIBWEBP
|
||||
static bool create_cp1252(pdfio_file_t *pdf);
|
||||
static pdfio_obj_t *create_font(pdfio_obj_t *file_obj, ttf_t *font, bool unicode);
|
||||
static pdfio_obj_t *create_image(pdfio_dict_t *dict, const unsigned char *data, size_t width, size_t height, size_t depth, size_t num_colors, bool alpha);
|
||||
|
||||
static ssize_t gif_get_block(pdfio_file_t *pdf, int fd, uint8_t *buffer, size_t bufsize);
|
||||
static bool gif_read_image(pdfio_file_t *pdf, int fd, uint8_t *data, size_t width, size_t height, int *ncolors, _pdfio_gif_cmap_t cmap);
|
||||
|
||||
#ifdef HAVE_LIBPNG
|
||||
static void png_error_func(png_structp pp, png_const_charp message);
|
||||
static void png_read_func(png_structp png_ptr, png_bytep data, size_t length);
|
||||
#endif // HAVE_LIBPNG
|
||||
|
||||
static void ttf_error_cb(pdfio_file_t *pdf, const char *message);
|
||||
|
||||
#ifndef HAVE_LIBPNG
|
||||
static unsigned update_png_crc(unsigned crc, const unsigned char *buffer, size_t length);
|
||||
#endif // !HAVE_LIBPNG
|
||||
|
||||
static bool write_array(pdfio_stream_t *st, pdfio_array_t *a);
|
||||
static bool write_dict(pdfio_stream_t *st, pdfio_dict_t *dict);
|
||||
static bool write_string(pdfio_stream_t *st, bool unicode, const char *s, bool *newline);
|
||||
@@ -2148,14 +2127,13 @@ pdfioFileCreateImageObjFromData(
|
||||
//
|
||||
// 'pdfioFileCreateImageObjFromFile()' - Add an image object to a PDF file from a file.
|
||||
//
|
||||
// This function creates an image object in a PDF file from a GIF, JPEG, PNG, or
|
||||
// WebP file. The "filename" parameter specifies the name of the GIF, JPEG,
|
||||
// PNG, or WebP file, while the "interpolate" parameter specifies whether to
|
||||
// interpolate when scaling the image on the page.
|
||||
// This function creates an image object in a PDF file from a JPEG or PNG file.
|
||||
// The "filename" parameter specifies the name of the JPEG or PNG file, while
|
||||
// the "interpolate" parameter specifies whether to interpolate when scaling the
|
||||
// image on the page.
|
||||
//
|
||||
// > Note: PNG files containing transparency cannot be used when producing
|
||||
// > PDF/A files. Files containing animation yield the final frame of the
|
||||
// > animation.
|
||||
// > PDF/A files.
|
||||
//
|
||||
|
||||
pdfio_obj_t * // O - Object
|
||||
@@ -2196,28 +2174,16 @@ pdfioFileCreateImageObjFromFile(
|
||||
|
||||
PDFIO_DEBUG("pdfioFileCreateImageObjFromFile: buffer=<%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X>\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], buffer[15], buffer[16], buffer[17], buffer[18], buffer[19], buffer[20], buffer[21], buffer[22], buffer[23], buffer[24], buffer[25], buffer[26], buffer[27], buffer[28], buffer[29], buffer[30], buffer[31]);
|
||||
|
||||
if (!memcmp(buffer, "GIF87a", 6) || !memcmp(buffer, "GIF89a", 6))
|
||||
{
|
||||
// GIF image...
|
||||
copy_func = copy_gif;
|
||||
}
|
||||
else if (!memcmp(buffer, "\211PNG\015\012\032\012\000\000\000\015IHDR", 16))
|
||||
if (!memcmp(buffer, "\211PNG\015\012\032\012\000\000\000\015IHDR", 16))
|
||||
{
|
||||
// PNG image...
|
||||
copy_func = copy_png;
|
||||
}
|
||||
else if (!memcmp(buffer, "\377\330\377", 3))
|
||||
{
|
||||
// JPEG image...
|
||||
// JPEG image...
|
||||
copy_func = copy_jpeg;
|
||||
}
|
||||
#ifdef HAVE_LIBWEBP
|
||||
else if (!memcmp(buffer, "RIFF", 4) && !memcmp(buffer + 8, "WEBP", 4))
|
||||
{
|
||||
// WebP image
|
||||
copy_func = copy_webp;
|
||||
}
|
||||
#endif // HAVE_LIBWEBP
|
||||
else
|
||||
{
|
||||
// Something else that isn't supported...
|
||||
@@ -2490,169 +2456,6 @@ pdfioPageDictAddImage(
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 'copy_gif()' - Copy a GIF image file.
|
||||
*/
|
||||
|
||||
static pdfio_obj_t * // O - Image object or `NULL` on error
|
||||
copy_gif(pdfio_dict_t *dict, // I - Image dictionary
|
||||
int fd) // I - File to read from
|
||||
{
|
||||
pdfio_obj_t *image_obj = NULL;
|
||||
// Image object
|
||||
uint8_t header[13]; // Image header
|
||||
_pdfio_gif_cmap_t cmap; // Colormap
|
||||
int ncolors, // Number of colors/bits per pixel
|
||||
transparent; // Transparent color index
|
||||
size_t width, // Width of image
|
||||
height; // Height of image
|
||||
uint8_t *data = NULL; // Image data
|
||||
size_t datasize; // Size of image data
|
||||
uint8_t desc, // Descriptor header
|
||||
extdesc; // Extension descriptor
|
||||
uint8_t block[256]; // Extension block
|
||||
ssize_t blocklen; // Length of block
|
||||
pdfio_dict_t *decode; // Decode parameters
|
||||
|
||||
|
||||
// Read the header; we already know it is a GIF file...
|
||||
if (read(fd, header, sizeof(header)) < sizeof(header))
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to read GIF header: %s", strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("copy_gif: header=<%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X>\n", header[0], header[1], header[2], header[3], header[4], header[5], header[6], header[7], header[8], header[9], header[10], header[11], header[12]);
|
||||
|
||||
width = (header[7] << 8) | header[6];
|
||||
height = (header[9] << 8) | header[8];
|
||||
ncolors = 2 << (header[10] & _PDFIO_GIF_COLORBITS);
|
||||
|
||||
PDFIO_DEBUG("copy_gif: width=%u, height=%u, ncolors=%d\n", (unsigned)width, (unsigned)height, ncolors);
|
||||
|
||||
if (width < 1 || width > 16384 || height < 1 || height > 16384)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unsupported image dimensions %ux%ux%d.", (unsigned)width, (unsigned)height, ncolors);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (header[10] & _PDFIO_GIF_COLORMAP)
|
||||
{
|
||||
// Read colormap after the file header...
|
||||
PDFIO_DEBUG("copy_gif: Have colormap.\n");
|
||||
if (read(fd, cmap, ncolors * 3) < (ncolors * 3))
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to read GIF colormap: %s", strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make a grayscale colormap for the number of colors...
|
||||
PDFIO_DEBUG("copy_gif: Using default grayscale colormap.\n");
|
||||
for (int i = 0; i < ncolors; i ++)
|
||||
cmap[i][0] = cmap[i][1] = cmap[i][2] = 255 * i / (ncolors - 1);
|
||||
}
|
||||
|
||||
#if DEBUG > 1
|
||||
for (int i = 0; i < ncolors; i ++)
|
||||
PDFIO_DEBUG("copy_gif: cmap[%d]={%4d,%4d,%4d}\n", i, cmap[i][0], cmap[i][1], cmap[i][2]);
|
||||
#endif // DEBUG > 1
|
||||
|
||||
transparent = -1;
|
||||
|
||||
// Allocate memory for the image and clear it to the background color...
|
||||
datasize = width * height;
|
||||
|
||||
if ((data = malloc(datasize)) == NULL)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to allocate memory for GIF image: %s", strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
memset(data, header[11], datasize);
|
||||
|
||||
// Read the image...
|
||||
while (read(fd, &desc, 1) > 0)
|
||||
{
|
||||
PDFIO_DEBUG("copy_gif: desc=%02X('%c')\n", desc, desc);
|
||||
|
||||
switch (desc)
|
||||
{
|
||||
case ';' : // End of image
|
||||
pdfioDictSetNumber(dict, "Width", width);
|
||||
pdfioDictSetNumber(dict, "Height", height);
|
||||
pdfioDictSetNumber(dict, "BitsPerComponent", 8);
|
||||
pdfioDictSetArray(dict, "ColorSpace", pdfioArrayCreateColorFromPalette(dict->pdf, ncolors, cmap[0]));
|
||||
|
||||
if (transparent >= 0)
|
||||
{
|
||||
pdfio_array_t *mask = pdfioArrayCreate(dict->pdf);
|
||||
pdfioArrayAppendNumber(mask, transparent);
|
||||
pdfioArrayAppendNumber(mask, transparent);
|
||||
|
||||
pdfioDictSetArray(dict, "Mask", mask);
|
||||
}
|
||||
|
||||
if ((decode = pdfioDictCreate(dict->pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to create decode parameters for WebP image.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
pdfioDictSetNumber(decode, "BitsPerComponent", 8);
|
||||
pdfioDictSetNumber(decode, "Colors", 1);
|
||||
pdfioDictSetNumber(decode, "Columns", width);
|
||||
pdfioDictSetNumber(decode, "Predictor", _PDFIO_PREDICTOR_PNG_AUTO);
|
||||
pdfioDictSetDict(dict, "DecodeParms", decode);
|
||||
pdfioDictSetName(dict, "Filter", "FlateDecode");
|
||||
|
||||
image_obj = create_image(dict, data, width, height, 8, 1, /*alpha*/false);
|
||||
goto done;
|
||||
|
||||
case '!' : // Extension record
|
||||
if (read(fd, &extdesc, 1) < 1)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to read extension descriptor.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (extdesc == 0xf9) // Graphic Control Extension
|
||||
{
|
||||
if ((blocklen = gif_get_block(dict->pdf, fd, block, sizeof(block))) < 4)
|
||||
goto done;
|
||||
|
||||
if (block[0] & 1) // Get transparent color index
|
||||
{
|
||||
transparent = block[3];
|
||||
PDFIO_DEBUG("copy_gif: transparent=%d\n", transparent);
|
||||
}
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
}
|
||||
while ((blocklen = gif_get_block(dict->pdf, fd, block, sizeof(block))) > 0);
|
||||
|
||||
if (blocklen < 0)
|
||||
goto done;
|
||||
break;
|
||||
|
||||
case ',' : // Image data
|
||||
if (!gif_read_image(dict->pdf, fd, data, width, height, &ncolors, cmap))
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
|
||||
free(data);
|
||||
|
||||
return (image_obj);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'copy_jpeg()' - Copy a JPEG image.
|
||||
//
|
||||
@@ -3481,86 +3284,6 @@ copy_png(pdfio_dict_t *dict, // I - Dictionary
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_LIBWEBP
|
||||
//
|
||||
// 'copy_webp()' - Copy a WebP image.
|
||||
//
|
||||
|
||||
static pdfio_obj_t * // O - Image object
|
||||
copy_webp(pdfio_dict_t *dict, // I - Image dictionary
|
||||
int fd) // I - Image file
|
||||
{
|
||||
struct stat finfo; // Image file information
|
||||
uint8_t *fdata = NULL; // Image file data
|
||||
int width, // Width of image
|
||||
height; // Height of image
|
||||
uint8_t *pixels = NULL; // Image pixel (RGBA) data
|
||||
pdfio_obj_t *obj = NULL; // Image object
|
||||
pdfio_dict_t *decode; // Stream decode parameters
|
||||
|
||||
|
||||
if (fstat(fd, &finfo))
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to get WebP file information: %s", strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (finfo.st_size > (1024 * 1024))
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "WebP image is too large.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if ((fdata = malloc((size_t)finfo.st_size)) == NULL)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to allocate memory for WebP file: %s", strerror(errno));
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (read(fd, fdata, (size_t)finfo.st_size) < (ssize_t)finfo.st_size)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to read WebP file.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
if ((pixels = WebPDecodeRGBA(fdata, (size_t)finfo.st_size, &width, &height)) == NULL)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Invalid WebP file.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
pdfioDictSetNumber(dict, "Width", width);
|
||||
pdfioDictSetNumber(dict, "Height", height);
|
||||
pdfioDictSetNumber(dict, "BitsPerComponent", 8);
|
||||
pdfioDictSetArray(dict, "ColorSpace", pdfioArrayCreateColorFromStandard(dict->pdf, /*num_colors*/3, PDFIO_CS_SRGB));
|
||||
pdfioDictSetName(dict, "Filter", "FlateDecode");
|
||||
|
||||
if ((decode = pdfioDictCreate(dict->pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(dict->pdf, "Unable to create decode parameters for WebP image.");
|
||||
goto done;
|
||||
}
|
||||
|
||||
pdfioDictSetNumber(decode, "BitsPerComponent", 8);
|
||||
pdfioDictSetNumber(decode, "Colors", 3);
|
||||
pdfioDictSetNumber(decode, "Columns", width);
|
||||
pdfioDictSetNumber(decode, "Predictor", _PDFIO_PREDICTOR_PNG_AUTO);
|
||||
pdfioDictSetDict(dict, "DecodeParms", decode);
|
||||
|
||||
obj = create_image(dict, pixels, width, height, /*depth*/8, /*num_colors*/3, /*alpha*/true);
|
||||
|
||||
done:
|
||||
|
||||
free(fdata);
|
||||
|
||||
if (pixels)
|
||||
WebPFree(pixels);
|
||||
|
||||
return (obj);
|
||||
}
|
||||
#endif // HAVE_LIBWEBP
|
||||
|
||||
|
||||
//
|
||||
// 'create_cp1252()' - Create the CP1252 font encoding object.
|
||||
//
|
||||
@@ -3976,80 +3699,66 @@ create_image(
|
||||
// Generate a mask image, as needed...
|
||||
if (alpha)
|
||||
{
|
||||
// See if the image mask is actually needed...
|
||||
size_t remaining = width * height; // Remaining pixels
|
||||
|
||||
for (dataptr = data + bpp; remaining > 0; remaining --, dataptr += bpp + bpc)
|
||||
// Create the image mask dictionary...
|
||||
if ((mask_dict = pdfioDictCopy(pdf, dict)) == NULL)
|
||||
{
|
||||
if (*dataptr < 255)
|
||||
break;
|
||||
if (bpc == 2 && dataptr[1] < 255)
|
||||
break;
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (remaining > 0)
|
||||
// Transparency masks are always grayscale...
|
||||
pdfioDictSetName(mask_dict, "ColorSpace", "DeviceGray");
|
||||
|
||||
// Set the automatic PNG predictor to optimize compression...
|
||||
if ((decode = pdfioDictCreate(pdf)) == NULL)
|
||||
{
|
||||
// Create the image mask dictionary...
|
||||
if ((mask_dict = pdfioDictCopy(pdf, dict)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Transparency masks are always grayscale...
|
||||
pdfioDictSetName(mask_dict, "ColorSpace", "DeviceGray");
|
||||
|
||||
// Set the automatic PNG predictor to optimize compression...
|
||||
if ((decode = pdfioDictCreate(pdf)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdfioDictSetNumber(decode, "BitsPerComponent", (double)depth);
|
||||
pdfioDictSetNumber(decode, "Colors", 1);
|
||||
pdfioDictSetNumber(decode, "Columns", (double)width);
|
||||
pdfioDictSetNumber(decode, "Predictor", _PDFIO_PREDICTOR_PNG_AUTO);
|
||||
pdfioDictSetDict(mask_dict, "DecodeParms", decode);
|
||||
|
||||
// Create the mask object and write the mask image...
|
||||
if ((mask_obj = pdfioFileCreateObj(pdf, mask_dict)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((st = pdfioObjCreateStream(mask_obj, PDFIO_FILTER_FLATE)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
pdfioObjClose(mask_obj);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
for (y = height, dataptr = data + bpp; y > 0; y --)
|
||||
{
|
||||
if (bpc == 1)
|
||||
{
|
||||
for (x = width, lineptr = line; x > 0; x --, dataptr += bpp)
|
||||
*lineptr++ = *dataptr++;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (x = width, lineptr = line; x > 0; x --, dataptr += bpp)
|
||||
{
|
||||
*lineptr++ = *dataptr++;
|
||||
*lineptr++ = *dataptr++;
|
||||
}
|
||||
}
|
||||
|
||||
pdfioStreamWrite(st, line, width * bpc);
|
||||
}
|
||||
|
||||
pdfioStreamClose(st);
|
||||
|
||||
// Use the transparency mask...
|
||||
pdfioDictSetObj(dict, "SMask", mask_obj);
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
pdfioDictSetNumber(decode, "BitsPerComponent", (double)depth);
|
||||
pdfioDictSetNumber(decode, "Colors", 1);
|
||||
pdfioDictSetNumber(decode, "Columns", (double)width);
|
||||
pdfioDictSetNumber(decode, "Predictor", _PDFIO_PREDICTOR_PNG_AUTO);
|
||||
pdfioDictSetDict(mask_dict, "DecodeParms", decode);
|
||||
|
||||
// Create the mask object and write the mask image...
|
||||
if ((mask_obj = pdfioFileCreateObj(pdf, mask_dict)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if ((st = pdfioObjCreateStream(mask_obj, PDFIO_FILTER_FLATE)) == NULL)
|
||||
{
|
||||
free(line);
|
||||
pdfioObjClose(mask_obj);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
for (y = height, dataptr = data + bpp; y > 0; y --)
|
||||
{
|
||||
if (bpc == 1)
|
||||
{
|
||||
for (x = width, lineptr = line; x > 0; x --, dataptr += bpp)
|
||||
*lineptr++ = *dataptr++;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (x = width, lineptr = line; x > 0; x --, dataptr += bpp)
|
||||
{
|
||||
*lineptr++ = *dataptr++;
|
||||
*lineptr++ = *dataptr++;
|
||||
}
|
||||
}
|
||||
|
||||
pdfioStreamWrite(st, line, width * bpc);
|
||||
}
|
||||
|
||||
pdfioStreamClose(st);
|
||||
|
||||
// Use the transparency mask...
|
||||
pdfioDictSetObj(dict, "SMask", mask_obj);
|
||||
}
|
||||
|
||||
// Set the automatic PNG predictor to optimize compression...
|
||||
@@ -4110,225 +3819,6 @@ create_image(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'gif_get_block()' - Read a GIF data block...
|
||||
//
|
||||
|
||||
static ssize_t // O - Number characters read
|
||||
gif_get_block(pdfio_file_t *pdf, // I - PDF file
|
||||
int fd, // I - File to read from
|
||||
uint8_t *buffer, // I - Input buffer (at least 256 bytes)
|
||||
size_t bufsize) // I - Size of buffer
|
||||
{
|
||||
uint8_t count; // Number of bytes to read
|
||||
|
||||
|
||||
// Make sure the buffer is sufficiently large...
|
||||
if (bufsize < 256)
|
||||
{
|
||||
_pdfioFileError(pdf, "GIF block buffer too small.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
// Read the count byte followed by the data from the file...
|
||||
if (read(fd, &count, 1) < 1)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read GIF block length.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("gif_get_block: count=%u\n", count);
|
||||
|
||||
if (count > 0 && read(fd, buffer, count) < count)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read GIF block data.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'gif_read_image()' - Read a GIF image stream...
|
||||
//
|
||||
|
||||
static bool // I - `true` on success, `false` on failure
|
||||
gif_read_image(
|
||||
pdfio_file_t *pdf, // I - PDF file
|
||||
int fd, // I - Input file
|
||||
uint8_t *data, // I - Indexed image data
|
||||
size_t width, // I - Width of image
|
||||
size_t height, // I - Height of image
|
||||
int *ncolors, // IO - Number of colors
|
||||
_pdfio_gif_cmap_t cmap) // I - Colormap
|
||||
{
|
||||
bool ret = false; // Return value
|
||||
uint8_t code_size; // Code size
|
||||
_pdfio_lzw_t *lzw; // LZW decompressor state
|
||||
uint8_t block[256]; // Data block from GIF file
|
||||
ssize_t blocklen; // Length of data block
|
||||
uint8_t pixels[1024], // Temporary pixel buffer
|
||||
*pixptr = pixels, // Pointer into pixel buffer
|
||||
*pixend = pixels; // End of pixel buffer
|
||||
uint8_t *dataptr; // Current pixel in image
|
||||
uint8_t iheader[9]; // Image header
|
||||
size_t ileft, // Left position
|
||||
itop, // Top position
|
||||
iwidth, // Width
|
||||
iheight; // Height
|
||||
bool interlace; // Interlaced image?
|
||||
size_t xpos = 0, // Current X position
|
||||
ypos = 0, // Current Y position
|
||||
pass = 0, // Current pass
|
||||
remaining; // Remaining pixels
|
||||
bool saw_endblock = false; // Have we seen the 0-length block?
|
||||
static size_t xpasses[4] = { 8, 8, 4, 2 },
|
||||
ypasses[5] = { 0, 4, 2, 1, 0 };
|
||||
|
||||
|
||||
if (read(fd, iheader, sizeof(iheader)) < sizeof(iheader))
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read GIF image header.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("gif_read_image: iheader=<%02X%02X%02X%02X%02X%02X%02X%02X%02X>\n", iheader[0], iheader[1], iheader[2], iheader[3], iheader[4], iheader[5], iheader[6], iheader[7], iheader[8]);
|
||||
|
||||
ileft = iheader[0] | (iheader[1] << 8);
|
||||
itop = iheader[2] | (iheader[3] << 8);
|
||||
iwidth = iheader[4] | (iheader[5] << 8);
|
||||
iheight = iheader[6] | (iheader[7] << 8);
|
||||
interlace = (iheader[8] & _PDFIO_GIF_INTERLACE) != 0;
|
||||
|
||||
PDFIO_DEBUG("gif_read_image: ileft=%u, itop=%u, iwidth=%u, iheight=%u, interlace=%s\n", (unsigned)ileft, (unsigned)itop, (unsigned)iwidth, (unsigned)iheight, interlace ? "true" : "false");
|
||||
|
||||
if ((ileft + iwidth) > width || (itop + iheight) > height)
|
||||
{
|
||||
_pdfioFileError(pdf, "Invalid GIF image %ux%u offset %ux%u.", (unsigned)iwidth, (unsigned)iheight, (unsigned)ileft, (unsigned)itop);
|
||||
return (false);
|
||||
}
|
||||
|
||||
remaining = iwidth * iheight;
|
||||
iwidth += ileft;
|
||||
iheight += itop;
|
||||
|
||||
if (iheader[8] & _PDFIO_GIF_COLORMAP)
|
||||
{
|
||||
// Local image colormap...
|
||||
int ncolors2 = 2 << (iheader[8] & _PDFIO_GIF_COLORBITS);
|
||||
|
||||
PDFIO_DEBUG("gif_read_image: Colormap with %d colors.\n", ncolors2);
|
||||
|
||||
if (read(fd, cmap, 3 * ncolors2) < (3 * ncolors2))
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read GIF image colormap.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (ncolors2 > *ncolors)
|
||||
*ncolors = ncolors2;
|
||||
}
|
||||
|
||||
xpos = ileft;
|
||||
ypos = itop;
|
||||
pass = 0;
|
||||
code_size = 0;
|
||||
|
||||
if (read(fd, &code_size, 1) < 1 || code_size < 2 || code_size > 12)
|
||||
{
|
||||
_pdfioFileError(pdf, "Invalid code size %u in GIF file.", code_size);
|
||||
return (false);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("gif_read_image: code_size=%u\n", code_size);
|
||||
|
||||
if ((lzw = _pdfioLZWCreate(code_size, /*early*/0, /*reversed*/true)) == NULL)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to create GIF decompressor for code size %u.", code_size);
|
||||
return (false);
|
||||
}
|
||||
|
||||
// TODO: Support 1-bit and 4-bit per output data; current code is 8-bit only
|
||||
dataptr = data + ypos * width + xpos;
|
||||
|
||||
while (remaining > 0)
|
||||
{
|
||||
// Fill the pixel buffer as needed...
|
||||
while (pixptr >= pixend)
|
||||
{
|
||||
// Fill input buffer as needed...
|
||||
if (lzw->avail_in == 0 && !saw_endblock)
|
||||
{
|
||||
blocklen = gif_get_block(pdf, fd, block, sizeof(block));
|
||||
saw_endblock = blocklen <= 0;
|
||||
|
||||
if (blocklen < 0)
|
||||
goto done;
|
||||
|
||||
lzw->avail_in = (size_t)blocklen;
|
||||
lzw->next_in = block;
|
||||
}
|
||||
|
||||
lzw->avail_out = sizeof(pixels);
|
||||
lzw->next_out = pixels;
|
||||
|
||||
if (!_pdfioLZWInflate(lzw))
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to decompress GIF image: %s", lzw->error);
|
||||
goto done;
|
||||
}
|
||||
|
||||
pixptr = pixels;
|
||||
pixend = pixels + sizeof(pixels) - lzw->avail_out;
|
||||
}
|
||||
|
||||
// Add this pixel and advance to the next one...
|
||||
*dataptr++ = *pixptr++;
|
||||
remaining --;
|
||||
xpos ++;
|
||||
|
||||
if (xpos >= iwidth)
|
||||
{
|
||||
// New line...
|
||||
xpos = ileft;
|
||||
|
||||
if (interlace)
|
||||
{
|
||||
// Skip lines when the image is interlaced...
|
||||
ypos += xpasses[pass];
|
||||
|
||||
if (ypos >= iheight)
|
||||
{
|
||||
pass ++;
|
||||
ypos = ypasses[pass] + itop;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No interlacing, move to next line...
|
||||
ypos ++;
|
||||
}
|
||||
|
||||
dataptr = data + ypos * width + xpos;
|
||||
}
|
||||
}
|
||||
|
||||
ret = true;
|
||||
|
||||
done:
|
||||
|
||||
// Read any remaining blocks...
|
||||
while (!saw_endblock)
|
||||
saw_endblock = gif_get_block(pdf, fd, block, sizeof(block)) <= 0;
|
||||
|
||||
// Free the LZW decoder state and return...
|
||||
_pdfioLZWDelete(lzw);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_LIBPNG
|
||||
//
|
||||
// 'png_error_func()' - PNG error message function.
|
||||
|
||||
233
pdfio-crypto.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Cryptographic support functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -96,14 +96,12 @@ static uint8_t pdf_passpad[32] = // Padding for passwords
|
||||
// Local functions...
|
||||
//
|
||||
|
||||
static void decrypt_ou_key(pdfio_encryption_t encryption, const uint8_t *file_key, size_t file_keylen, uint8_t ou_key[32]);
|
||||
static void encrypt_ou_key(pdfio_encryption_t encryption, const uint8_t *file_key, size_t file_keylen, uint8_t ou_key[32]);
|
||||
static void make_file_key(pdfio_encryption_t encryption, size_t file_keylen, pdfio_permission_t permissions, const unsigned char *file_id, size_t file_idlen, const uint8_t *user_pad, const uint8_t *owner_key, bool encrypt_metadata, uint8_t file_key[16]);
|
||||
static void make_owner_key(pdfio_encryption_t encryption, size_t file_keylen, const uint8_t *owner_pad, const uint8_t *user_pad, uint8_t owner_key[32]);
|
||||
static void make_password_key(pdfio_encryption_t encryption, size_t file_keylen, const uint8_t *pad, uint8_t *key);
|
||||
static void decrypt_user_key(pdfio_encryption_t encryption, const uint8_t *file_key, uint8_t user_key[32]);
|
||||
static void encrypt_user_key(pdfio_encryption_t encryption, const uint8_t *file_key, uint8_t user_key[32]);
|
||||
static void make_file_key(pdfio_encryption_t encryption, pdfio_permission_t permissions, const unsigned char *file_id, size_t file_idlen, const uint8_t *user_pad, const uint8_t *owner_key, bool encrypt_metadata, uint8_t file_key[16]);
|
||||
static void make_owner_key(pdfio_encryption_t encryption, const uint8_t *owner_pad, const uint8_t *user_pad, uint8_t owner_key[32]);
|
||||
static void make_user_key(const unsigned char *file_id, size_t file_idlen, uint8_t user_key[32]);
|
||||
static void pad_password(const char *password, uint8_t pad[32]);
|
||||
static bool test_password(pdfio_encryption_t encryption, size_t file_keylen, pdfio_permission_t permisions,const unsigned char *file_id, size_t file_idlen, const uint8_t *user_pad, const uint8_t *user_key, const uint8_t *owner_key, bool encrypt_metadata, uint8_t file_key[16]);
|
||||
|
||||
|
||||
//
|
||||
@@ -141,7 +139,6 @@ _pdfioCryptoLock(
|
||||
case PDFIO_ENCRYPTION_AES_128 :
|
||||
// Create the 128-bit encryption keys...
|
||||
pad_password(user_password, user_pad);
|
||||
pdf->file_keylen = 16;
|
||||
|
||||
if (!owner_password && user_password && *user_password)
|
||||
{
|
||||
@@ -155,17 +152,18 @@ _pdfioCryptoLock(
|
||||
}
|
||||
|
||||
// Compute the owner key...
|
||||
make_owner_key(encryption, pdf->file_keylen, owner_pad, user_pad, pdf->owner_key);
|
||||
make_owner_key(encryption, owner_pad, user_pad, pdf->owner_key);
|
||||
pdf->owner_keylen = 32;
|
||||
|
||||
// Generate the encryption key
|
||||
file_id = pdfioArrayGetBinary(pdf->id_array, 0, &file_idlen);
|
||||
|
||||
make_file_key(encryption, pdf->file_keylen, permissions, file_id, file_idlen, user_pad, pdf->owner_key, pdf->encrypt_metadata, pdf->file_key);
|
||||
make_file_key(encryption, permissions, file_id, file_idlen, user_pad, pdf->owner_key, pdf->encrypt_metadata, pdf->file_key);
|
||||
pdf->file_keylen = 16;
|
||||
|
||||
// Generate the user key...
|
||||
make_user_key(file_id, file_idlen, pdf->user_key);
|
||||
encrypt_ou_key(encryption, pdf->file_key, pdf->file_keylen, pdf->user_key);
|
||||
encrypt_user_key(encryption, pdf->file_key, pdf->user_key);
|
||||
pdf->user_keylen = 32;
|
||||
|
||||
// Save everything in the dictionary...
|
||||
@@ -693,11 +691,6 @@ _pdfioCryptoUnlock(
|
||||
_pdfioFileError(pdf, "Unsupported encryption V%d R%d.", version, revision);
|
||||
return (false);
|
||||
}
|
||||
else if (length < 40 || length > 128)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unsupported key length %d.", length);
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Grab the remaining values we need to unlock the PDF...
|
||||
pdf->file_keylen = (size_t)(length / 8);
|
||||
@@ -740,7 +733,7 @@ _pdfioCryptoUnlock(
|
||||
return (false);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: user_key[%d]=<%02X%02X%02X%02X...%02X%02X%02X%02X>\n", (int)user_keylen, user_key[0], user_key[1], user_key[2], user_key[3], user_key[28], user_key[29], user_key[30], user_key[31]);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: user_key[%d]=%02X%02X%02X%02X...%02X%02X%02X%02X\n", (int)user_keylen, user_key[0], user_key[1], user_key[2], user_key[3], user_key[28], user_key[29], user_key[30], user_key[31]);
|
||||
|
||||
memcpy(pdf->user_key, user_key, user_keylen);
|
||||
pdf->user_keylen = user_keylen;
|
||||
@@ -751,9 +744,6 @@ _pdfioCryptoUnlock(
|
||||
return (false);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: P=%d\n", pdf->permissions);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: file_id(%d)=<%02X%02X%02X%02X...%02X%02X%02X%02X>\n", (int)file_idlen, file_id[0], file_id[1], file_id[2], file_id[3], file_id[12], file_id[13], file_id[14], file_id[15]);
|
||||
|
||||
// Generate a base hash from known values...
|
||||
_pdfioCryptoMD5Init(&md5);
|
||||
_pdfioCryptoMD5Append(&md5, pdf_passpad, 32);
|
||||
@@ -765,34 +755,56 @@ _pdfioCryptoUnlock(
|
||||
{
|
||||
if (pdf->encryption <= PDFIO_ENCRYPTION_AES_128)
|
||||
{
|
||||
// RC4 and AES-128 encryption...
|
||||
uint8_t pad[32], // Padded password
|
||||
padkey[16], // Password key
|
||||
file_key[16]; // File key
|
||||
file_key[16], // File key
|
||||
user_pad[32], // Padded user password
|
||||
own_user_key[32], // Calculated user key
|
||||
pdf_user_key[32]; // Decrypted user key
|
||||
|
||||
// Pad the supplied password, if any...
|
||||
pad_password(password, pad);
|
||||
|
||||
// Test the user password...
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Trying <%02X%02X%02X%02X...%02X%02X%02X%02X>\n", pad[0], pad[1], pad[2], pad[3], pad[28], pad[29], pad[30], pad[31]);
|
||||
// Generate keys to see if things match...
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Trying %02X%02X%02X%02X...%02X%02X%02X%02X\n", pad[0], pad[1], pad[2], pad[3], pad[28], pad[29], pad[30], pad[31]);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: P=%d\n", pdf->permissions);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Fid(%d)=%02X%02X%02X%02X...%02X%02X%02X%02X\n", (int)file_idlen, file_id[0], file_id[1], file_id[2], file_id[3], file_id[12], file_id[13], file_id[14], file_id[15]);
|
||||
|
||||
if (test_password(pdf->encryption, pdf->file_keylen, pdf->permissions, file_id, file_idlen, pad, pdf->user_key, pdf->owner_key, pdf->encrypt_metadata, file_key))
|
||||
make_owner_key(pdf->encryption, pad, pdf->owner_key, user_pad);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Upad=%02X%02X%02X%02X...%02X%02X%02X%02X\n", user_pad[0], user_pad[1], user_pad[2], user_pad[3], user_pad[28], user_pad[29], user_pad[30], user_pad[31]);
|
||||
|
||||
make_file_key(pdf->encryption, pdf->permissions, file_id, file_idlen, user_pad, pdf->owner_key, pdf->encrypt_metadata, file_key);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Fown=%02X%02X%02X%02X...%02X%02X%02X%02X\n", file_key[0], file_key[1], file_key[2], file_key[3], file_key[12], file_key[13], file_key[14], file_key[15]);
|
||||
|
||||
make_user_key(file_id, file_idlen, own_user_key);
|
||||
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: U=%02X%02X%02X%02X...%02X%02X%02X%02X\n", pdf->user_key[0], pdf->user_key[1], pdf->user_key[2], pdf->user_key[3], pdf->user_key[28], pdf->user_key[29], pdf->user_key[30], pdf->user_key[31]);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Uown=%02X%02X%02X%02X...%02X%02X%02X%02X\n", own_user_key[0], own_user_key[1], own_user_key[2], own_user_key[3], own_user_key[28], own_user_key[29], own_user_key[30], own_user_key[31]);
|
||||
|
||||
if (!memcmp(own_user_key, pdf->user_key, sizeof(own_user_key)))
|
||||
{
|
||||
memcpy(pdf->file_key, file_key, pdf->file_keylen);
|
||||
// Matches!
|
||||
memcpy(pdf->file_key, file_key, sizeof(pdf->file_key));
|
||||
memcpy(pdf->password, pad, sizeof(pdf->password));
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
// Test the owner password...
|
||||
make_password_key(pdf->encryption, pdf->file_keylen, pad, padkey);
|
||||
// Not the owner password, try the user password...
|
||||
make_file_key(pdf->encryption, pdf->permissions, file_id, file_idlen, pad, pdf->owner_key, pdf->encrypt_metadata, file_key);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Fuse=%02X%02X%02X%02X...%02X%02X%02X%02X\n", file_key[0], file_key[1], file_key[2], file_key[3], file_key[12], file_key[13], file_key[14], file_key[15]);
|
||||
|
||||
memcpy(pad, pdf->owner_key, sizeof(pad));
|
||||
decrypt_ou_key(pdf->encryption, padkey, pdf->file_keylen, pad);
|
||||
make_user_key(file_id, file_idlen, own_user_key);
|
||||
|
||||
if (test_password(pdf->encryption, pdf->file_keylen, pdf->permissions, file_id, file_idlen, pad, pdf->user_key, pdf->owner_key, pdf->encrypt_metadata, file_key))
|
||||
memcpy(pdf_user_key, pdf->user_key, sizeof(pdf_user_key));
|
||||
decrypt_user_key(pdf->encryption, file_key, pdf_user_key);
|
||||
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Uuse=%02X%02X%02X%02X...%02X%02X%02X%02X\n", user_key[0], user_key[1], user_key[2], user_key[3], user_key[28], user_key[29], user_key[30], user_key[31]);
|
||||
PDFIO_DEBUG("_pdfioCryptoUnlock: Updf=%02X%02X%02X%02X...%02X%02X%02X%02X\n", pdf_user_key[0], pdf_user_key[1], pdf_user_key[2], pdf_user_key[3], pdf_user_key[28], pdf_user_key[29], pdf_user_key[30], pdf_user_key[31]);
|
||||
|
||||
if (!memcmp(pad, pdf_user_key, 32) || !memcmp(own_user_key, pdf_user_key, 16))
|
||||
{
|
||||
memcpy(pdf->file_key, file_key, pdf->file_keylen);
|
||||
// Matches!
|
||||
memcpy(pdf->file_key, file_key, sizeof(pdf->file_key));
|
||||
memcpy(pdf->password, pad, sizeof(pdf->password));
|
||||
|
||||
return (true);
|
||||
@@ -820,15 +832,14 @@ _pdfioCryptoUnlock(
|
||||
|
||||
|
||||
//
|
||||
// 'decrypt_ou_key()' - Decrypt the user key.
|
||||
// 'decrypt_user_key()' - Decrypt the user key.
|
||||
//
|
||||
|
||||
static void
|
||||
decrypt_ou_key(
|
||||
decrypt_user_key(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
const uint8_t *file_key, // I - File encryption key
|
||||
size_t file_keylen, // I - Length of file encryption key
|
||||
uint8_t ou_key[32]) // IO - Owner/User key
|
||||
uint8_t user_key[32]) // IO - User key
|
||||
{
|
||||
size_t i, j; // Looping vars
|
||||
_pdfio_rc4_t rc4; // RC4 encryption context
|
||||
@@ -838,38 +849,38 @@ decrypt_ou_key(
|
||||
{
|
||||
// Encrypt the result once...
|
||||
_pdfioCryptoRC4Init(&rc4, file_key, 5);
|
||||
_pdfioCryptoRC4Crypt(&rc4, ou_key, ou_key, 32);
|
||||
_pdfioCryptoRC4Crypt(&rc4, user_key, user_key, 32);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Encrypt the result 20 times...
|
||||
uint8_t key[16]; // Current encryption key
|
||||
|
||||
for (i = 20; i > 0;)
|
||||
for (i = 19; i > 0; i --)
|
||||
{
|
||||
// XOR each byte in the key with the loop counter...
|
||||
i --;
|
||||
|
||||
for (j = 0; j < file_keylen; j ++)
|
||||
for (j = 0; j < 16; j ++)
|
||||
key[j] = (uint8_t)(file_key[j] ^ i);
|
||||
|
||||
_pdfioCryptoRC4Init(&rc4, key, file_keylen);
|
||||
_pdfioCryptoRC4Crypt(&rc4, ou_key, ou_key, 32);
|
||||
_pdfioCryptoRC4Init(&rc4, key, 16);
|
||||
_pdfioCryptoRC4Crypt(&rc4, user_key, user_key, 32);
|
||||
}
|
||||
|
||||
_pdfioCryptoRC4Init(&rc4, file_key, 16);
|
||||
_pdfioCryptoRC4Crypt(&rc4, user_key, user_key, 32);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'encrypt_ou_key()' - Encrypt the owner/user key.
|
||||
// 'encrypt_user_key()' - Encrypt the user key.
|
||||
//
|
||||
|
||||
static void
|
||||
encrypt_ou_key(
|
||||
encrypt_user_key(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
const uint8_t *file_key, // I - File encryption key
|
||||
size_t file_keylen, // I - Length of file encryption key
|
||||
uint8_t ou_key[32]) // IO - Owner/User key
|
||||
uint8_t user_key[32]) // IO - User key
|
||||
{
|
||||
size_t i, j; // Looping vars
|
||||
_pdfio_rc4_t rc4; // RC4 encryption context
|
||||
@@ -879,7 +890,7 @@ encrypt_ou_key(
|
||||
{
|
||||
// Encrypt the result once...
|
||||
_pdfioCryptoRC4Init(&rc4, file_key, 5);
|
||||
_pdfioCryptoRC4Crypt(&rc4, ou_key, ou_key, 32);
|
||||
_pdfioCryptoRC4Crypt(&rc4, user_key, user_key, 32);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -889,11 +900,11 @@ encrypt_ou_key(
|
||||
for (i = 0; i < 20; i ++)
|
||||
{
|
||||
// XOR each byte in the key with the loop counter...
|
||||
for (j = 0; j < file_keylen; j ++)
|
||||
for (j = 0; j < 16; j ++)
|
||||
key[j] = (uint8_t)(file_key[j] ^ i);
|
||||
|
||||
_pdfioCryptoRC4Init(&rc4, key, file_keylen);
|
||||
_pdfioCryptoRC4Crypt(&rc4, ou_key, ou_key, 32);
|
||||
_pdfioCryptoRC4Init(&rc4, key, 16);
|
||||
_pdfioCryptoRC4Crypt(&rc4, user_key, user_key, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -906,7 +917,6 @@ encrypt_ou_key(
|
||||
static void
|
||||
make_file_key(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
size_t file_keylen, // I - Encryption key length
|
||||
pdfio_permission_t permissions, // I - File permissions
|
||||
const unsigned char *file_id, // I - File ID value
|
||||
size_t file_idlen, // I - Length of file ID
|
||||
@@ -952,14 +962,14 @@ make_file_key(
|
||||
for (i = 0; i < 50; i ++)
|
||||
{
|
||||
_pdfioCryptoMD5Init(&md5);
|
||||
_pdfioCryptoMD5Append(&md5, digest, file_keylen);
|
||||
_pdfioCryptoMD5Append(&md5, digest, 16);
|
||||
_pdfioCryptoMD5Finish(&md5, digest);
|
||||
}
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("make_file_key: file_key[16]=<%02X%02X%02X%02X...%02X%02X%02X%02X>\n", digest[0], digest[1], digest[2], digest[3], digest[12], digest[13], digest[14], digest[15]);
|
||||
|
||||
memcpy(file_key, digest, file_keylen);
|
||||
memcpy(file_key, digest, 16);
|
||||
}
|
||||
|
||||
|
||||
@@ -970,57 +980,57 @@ make_file_key(
|
||||
static void
|
||||
make_owner_key(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
size_t file_keylen, // I - Length of encryption key
|
||||
const uint8_t *owner_pad, // I - Padded owner password
|
||||
const uint8_t *user_pad, // I - Padded user password
|
||||
uint8_t owner_key[32]) // O - Owner key value
|
||||
{
|
||||
uint8_t key[16]; // Base encryption key
|
||||
size_t i, j; // Looping vars
|
||||
_pdfio_md5_t md5; // MD5 context
|
||||
uint8_t digest[16]; // 128-bit MD5 digest
|
||||
_pdfio_rc4_t rc4; // RC4 encryption context
|
||||
|
||||
|
||||
// Hash the owner password...
|
||||
make_password_key(encryption, file_keylen, owner_pad, key);
|
||||
|
||||
// Copy and encrypt the padded user password...
|
||||
memcpy(owner_key, user_pad, 32);
|
||||
decrypt_ou_key(encryption, key, file_keylen, owner_key);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'make_password_key()' - Make a password key.
|
||||
//
|
||||
|
||||
static void
|
||||
make_password_key(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
size_t file_keylen, // I - Length of encryption key
|
||||
const uint8_t *pad, // I - Padded password
|
||||
uint8_t *key) // O - Key data
|
||||
{
|
||||
size_t i; // Looping var
|
||||
_pdfio_md5_t md5; // MD5 context
|
||||
uint8_t digest[16]; // 128-bit MD5 digest
|
||||
|
||||
|
||||
// Hash the padded password...
|
||||
_pdfioCryptoMD5Init(&md5);
|
||||
_pdfioCryptoMD5Append(&md5, pad, 32);
|
||||
_pdfioCryptoMD5Append(&md5, owner_pad, 32);
|
||||
_pdfioCryptoMD5Finish(&md5, digest);
|
||||
|
||||
if (encryption != PDFIO_ENCRYPTION_RC4_40)
|
||||
{
|
||||
// Hash the hash another 50 times...
|
||||
for (i = 0; i < 50; i ++)
|
||||
{
|
||||
_pdfioCryptoMD5Init(&md5);
|
||||
_pdfioCryptoMD5Append(&md5, digest, file_keylen);
|
||||
_pdfioCryptoMD5Append(&md5, digest, 16);
|
||||
_pdfioCryptoMD5Finish(&md5, digest);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the key portion of the hashed password to the key...
|
||||
memcpy(key, digest, file_keylen);
|
||||
// Copy and encrypt the padded user password...
|
||||
memcpy(owner_key, user_pad, 32);
|
||||
|
||||
if (encryption == PDFIO_ENCRYPTION_RC4_40)
|
||||
{
|
||||
// Encrypt once...
|
||||
_pdfioCryptoRC4Init(&rc4, digest, 5);
|
||||
_pdfioCryptoRC4Crypt(&rc4, owner_key, owner_key, 32);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Encrypt 20 times...
|
||||
uint8_t encrypt_key[16]; // RC4 encryption key
|
||||
|
||||
for (i = 20; i > 0;)
|
||||
{
|
||||
// XOR each byte in the digest with the loop counter to make a key...
|
||||
i --;
|
||||
|
||||
for (j = 0; j < sizeof(encrypt_key); j ++)
|
||||
encrypt_key[j] = (uint8_t)(digest[j] ^ i);
|
||||
|
||||
_pdfioCryptoRC4Init(&rc4, encrypt_key, sizeof(encrypt_key));
|
||||
_pdfioCryptoRC4Crypt(&rc4, owner_key, owner_key, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1075,52 +1085,3 @@ pad_password(const char *password, // I - Password string or `NULL`
|
||||
if (len < 32)
|
||||
memcpy(pad + len, pdf_passpad, 32 - len);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'test_password()' - Test a user password...
|
||||
//
|
||||
|
||||
static bool // O - `true` if password is correct, `false` otherwise
|
||||
test_password(
|
||||
pdfio_encryption_t encryption, // I - Type of encryption
|
||||
size_t file_keylen, // I - Length of encryption key
|
||||
pdfio_permission_t permissions, // I - File permissions
|
||||
const unsigned char *file_id, // I - File ID value
|
||||
size_t file_idlen, // I - Length of file ID
|
||||
const uint8_t *user_pad, // I - Padded user password
|
||||
const uint8_t *user_key, // I - User key
|
||||
const uint8_t *owner_key, // I - Owner key
|
||||
bool encrypt_metadata,
|
||||
// I - Encrypt metadata?
|
||||
uint8_t file_key[16]) // O - Encryption key
|
||||
{
|
||||
uint8_t pdf_user_key[32]; // Decrypted user key
|
||||
|
||||
|
||||
// Make the file key...
|
||||
make_file_key(encryption, file_keylen, permissions, file_id, file_idlen, user_pad, owner_key, encrypt_metadata, file_key);
|
||||
|
||||
// Decrypt the user key using the file key...
|
||||
memcpy(pdf_user_key, user_key, sizeof(pdf_user_key));
|
||||
decrypt_ou_key(encryption, file_key, file_keylen, pdf_user_key);
|
||||
PDFIO_DEBUG("test_password: pdf_user_key[32]=<%02X%02X%02X%02X...%02X%02X%02X%02X>\n", pdf_user_key[0], pdf_user_key[1], pdf_user_key[2], pdf_user_key[3], pdf_user_key[28], pdf_user_key[29], pdf_user_key[30], pdf_user_key[31]);
|
||||
|
||||
if (encryption == PDFIO_ENCRYPTION_RC4_40 && !memcmp(pdf_user_key, pdf_passpad, 32))
|
||||
{
|
||||
// 40-bit encryption just compares the eecrypted user key matches...
|
||||
return (true);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 128-bit encryption needs to match the calculated user key...
|
||||
uint8_t own_user_key[32]; // Calculated user key
|
||||
|
||||
// Calculate what the user key should be...
|
||||
make_user_key(file_id, file_idlen, own_user_key);
|
||||
PDFIO_DEBUG("test_password: own_user_key[32]=<%02X%02X%02X%02X...%02X%02X%02X%02X>\n", own_user_key[0], own_user_key[1], own_user_key[2], own_user_key[3], own_user_key[28], own_user_key[29], own_user_key[30], own_user_key[31]);
|
||||
|
||||
// Return whether they match...
|
||||
return (!memcmp(pdf_user_key, own_user_key, 16));
|
||||
}
|
||||
}
|
||||
|
||||
50
pdfio-dict.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF dictionary functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -79,10 +79,6 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
PDFIO_DEBUG("pdfioDictCopy(pdf=%p, dict=%p(%p))\n", (void *)pdf, (void *)dict, dict ? (void *)dict->pdf : NULL);
|
||||
|
||||
// Range check input...
|
||||
if (!pdf || !dict)
|
||||
return (NULL);
|
||||
|
||||
// Create the new dictionary...
|
||||
if ((ndict = pdfioDictCreate(pdf)) == NULL)
|
||||
return (NULL);
|
||||
@@ -96,8 +92,6 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
// Copy and add each of the source dictionary's key/value pairs...
|
||||
for (i = dict->num_pairs, p = dict->pairs; i > 0; i --, p ++)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioDictCopy: key=\"%s\", value.type=%d\n", p->key, p->value.type);
|
||||
|
||||
if (!strcmp(p->key, "Length") && p->value.type == PDFIO_VALTYPE_INDIRECT && dict->pdf != pdf)
|
||||
{
|
||||
// Don't use indirect stream lengths for copied objects...
|
||||
@@ -108,28 +102,15 @@ pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
if (lenobj)
|
||||
{
|
||||
if (lenobj->value.type == PDFIO_VALTYPE_NONE)
|
||||
{
|
||||
if (!_pdfioObjLoad(lenobj))
|
||||
{
|
||||
PDFIO_DEBUG("pdfioDictCopy: Unable to copy value of \"%s\", returning NULL.\n", p->key);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
_pdfioObjLoad(lenobj);
|
||||
|
||||
v.value.number = lenobj->value.value.number;
|
||||
}
|
||||
else
|
||||
{
|
||||
v.value.number = 0.0;
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("pdfioDictCopy: Length is %.0f.\n", v.value.number);
|
||||
}
|
||||
else if (!_pdfioValueCopy(pdf, &v, dict->pdf, &p->value))
|
||||
{
|
||||
PDFIO_DEBUG("pdfioDictCopy: Unable to copy value of \"%s\", returning NULL.\n", p->key);
|
||||
return (NULL); // Let pdfioFileClose do the cleanup...
|
||||
}
|
||||
|
||||
if (_pdfioStringIsAllocated(dict->pdf, p->key))
|
||||
key = pdfioStringCreate(pdf, p->key);
|
||||
@@ -669,7 +650,8 @@ _pdfioDictRead(pdfio_file_t *pdf, // I - PDF file
|
||||
}
|
||||
else if (_pdfioDictGetValue(dict, key + 1))
|
||||
{
|
||||
// Issue 118: Discard duplicate key/value pairs...
|
||||
// Issue 118: Discard duplicate key/value pairs, in the future this will
|
||||
// be a warning message...
|
||||
_pdfioValueDelete(&value);
|
||||
if (_pdfioFileError(pdf, "WARNING: Discarding value for duplicate dictionary key '%s'.", key + 1))
|
||||
continue;
|
||||
@@ -1073,13 +1055,11 @@ _pdfioDictSetValue(
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` on failure
|
||||
_pdfioDictWrite(
|
||||
_pdfio_printf_t cb, // I - Printf callback function
|
||||
void *cbdata, // I - Printf callback data
|
||||
pdfio_obj_t *obj, // I - Object, if any
|
||||
pdfio_dict_t *dict, // I - Dictionary
|
||||
off_t *length) // I - Offset to length value
|
||||
_pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary
|
||||
pdfio_obj_t *obj, // I - Object, if any
|
||||
off_t *length) // I - Offset to length value
|
||||
{
|
||||
pdfio_file_t *pdf = dict->pdf; // PDF file
|
||||
size_t i; // Looping var
|
||||
_pdfio_pair_t *pair; // Current key/value pair
|
||||
|
||||
@@ -1088,30 +1068,28 @@ _pdfioDictWrite(
|
||||
*length = 0;
|
||||
|
||||
// Dictionaries are bounded by "<<" and ">>"...
|
||||
if (!(cb)(cbdata, "<<"))
|
||||
if (!_pdfioFilePuts(pdf, "<<"))
|
||||
return (false);
|
||||
|
||||
// Write all of the key/value pairs...
|
||||
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
|
||||
{
|
||||
if (!(cb)(cbdata, "%N", pair->key))
|
||||
if (!_pdfioFilePrintf(pdf, "%N", pair->key))
|
||||
return (false);
|
||||
|
||||
if (length && !strcmp(pair->key, "Length") && pair->value.type == PDFIO_VALTYPE_NUMBER && pair->value.value.number <= 0.0)
|
||||
{
|
||||
// Writing an object dictionary with an undefined length
|
||||
*length = _pdfioFileTell(dict->pdf) + 1;
|
||||
if (!(cb)(cbdata, " 9999999999"))
|
||||
*length = _pdfioFileTell(pdf) + 1;
|
||||
if (!_pdfioFilePuts(pdf, " 9999999999"))
|
||||
return (false);
|
||||
}
|
||||
else if (!_pdfioValueWrite(cb, cbdata, obj, &pair->value, NULL))
|
||||
{
|
||||
else if (!_pdfioValueWrite(pdf, obj, &pair->value, NULL))
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
// Close it up...
|
||||
return ((cb)(cbdata, ">>"));
|
||||
return (_pdfioFilePuts(pdf, ">>"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
371
pdfio-file.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF file functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -29,7 +29,6 @@ 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 repair_xref(pdfio_file_t *pdf, pdfio_password_cb_t password_cb, void *password_data);
|
||||
static bool write_metadata(pdfio_file_t *pdf);
|
||||
static bool write_objstm(pdfio_file_t *pdf);
|
||||
static bool write_pages(pdfio_file_t *pdf);
|
||||
static bool write_trailer(pdfio_file_t *pdf);
|
||||
|
||||
@@ -132,7 +131,7 @@ pdfioFileClose(pdfio_file_t *pdf) // I - PDF file
|
||||
pdfioFileAddOutputIntent(pdf, /*subtype*/"GTS_PDFA1", /*condition*/"CMYK", /*cond_id*/"CGATS001", /*reg_name*/NULL, /*info*/"CMYK Printing", /*profile*/NULL);
|
||||
|
||||
// Close and write out the last bits...
|
||||
if (write_metadata(pdf) && pdfioObjClose(pdf->info_obj) && write_objstm(pdf) && write_pages(pdf) && pdfioObjClose(pdf->root_obj) && write_trailer(pdf))
|
||||
if (write_metadata(pdf) && pdfioObjClose(pdf->info_obj) && write_pages(pdf) && pdfioObjClose(pdf->root_obj) && write_trailer(pdf))
|
||||
ret = _pdfioFileFlush(pdf);
|
||||
}
|
||||
|
||||
@@ -1715,15 +1714,6 @@ create_common(
|
||||
if ((pdf->root_obj = pdfioFileCreateObj(pdf, dict)) == NULL)
|
||||
goto error;
|
||||
|
||||
// For PDF 1.5 and later, create a (compressed) object stream for value objects...
|
||||
if (strcmp(file_version, "1.5") >= 0 && !pdf->output_cb)
|
||||
{
|
||||
if ((pdf->objstm_dict = pdfioDictCreate(pdf)) == NULL)
|
||||
goto error;
|
||||
|
||||
pdfioDictSetName(pdf->objstm_dict, "Type", "ObjStm");
|
||||
}
|
||||
|
||||
// Create random file ID values...
|
||||
_pdfioCryptoMakeRandom(id_value, sizeof(id_value));
|
||||
|
||||
@@ -2016,9 +2006,6 @@ load_xref(
|
||||
_pdfio_token_t tb; // Token buffer/stack
|
||||
off_t line_offset; // Offset to start of line
|
||||
pdfio_obj_t *pages_obj; // Pages object
|
||||
size_t num_xrefs = 1; // Number of xref offsets
|
||||
off_t xrefs[100] = { xref_offset };
|
||||
// xref offsets
|
||||
|
||||
|
||||
while (!done)
|
||||
@@ -2484,31 +2471,13 @@ load_xref(
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
else if (new_offset == xref_offset)
|
||||
{
|
||||
// See if we've seen this xref table before...
|
||||
size_t i; // Looping var
|
||||
|
||||
for (i = 0; i < num_xrefs; i ++)
|
||||
{
|
||||
if (new_offset == xrefs[i])
|
||||
{
|
||||
// Yes, error out...
|
||||
_pdfioFileError(pdf, "Recursive xref table.");
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
// No, save it...
|
||||
if (i >= (sizeof(xrefs) / sizeof(xrefs[0])))
|
||||
{
|
||||
// Too many xref tables...
|
||||
_pdfioFileError(pdf, "Too many xref tables.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
xrefs[num_xrefs ++] = xref_offset = new_offset;
|
||||
_pdfioFileError(pdf, "Recursive xref table.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
xref_offset = new_offset;
|
||||
}
|
||||
|
||||
// Once we have all of the xref tables loaded, get the important objects and
|
||||
@@ -2631,19 +2600,19 @@ repair_xref(
|
||||
|
||||
_pdfioTokenFlush(&tb);
|
||||
|
||||
if (!strcmp(line, "stream"))
|
||||
if (type && !strcmp(line, "stream"))
|
||||
{
|
||||
// Possible object or XRef stream...
|
||||
obj->stream_offset = _pdfioFileTell(pdf);
|
||||
|
||||
if (type && !strcmp(type, "ObjStm") && num_sobjs < (sizeof(sobjs) / sizeof(sobjs[0])))
|
||||
if (!strcmp(type, "ObjStm") && num_sobjs < (sizeof(sobjs) / sizeof(sobjs[0])))
|
||||
{
|
||||
PDFIO_DEBUG("repair_xref: Object stream...\n");
|
||||
sobjs[num_sobjs] = obj;
|
||||
num_sobjs ++;
|
||||
}
|
||||
|
||||
if (type && !strcmp(type, "XRef") && !pdf->trailer_dict)
|
||||
if (!strcmp(type, "XRef") && !pdf->trailer_dict)
|
||||
{
|
||||
// Save the trailer dictionary...
|
||||
pdfio_obj_t *encrypt_obj;
|
||||
@@ -2884,86 +2853,6 @@ write_metadata(pdfio_file_t *pdf) // I - PDF file
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'write_objstm()' - Write an object stream.
|
||||
//
|
||||
|
||||
static bool // O - `true` on success, `false` on failure
|
||||
write_objstm(pdfio_file_t *pdf) // I - PDF file
|
||||
{
|
||||
bool ret = false; // Return value
|
||||
size_t i; // Looping var
|
||||
pdfio_obj_t *obj; // Current object
|
||||
pdfio_stream_t *st = NULL; // Output stream
|
||||
_pdfio_strbuf_t *bptr = NULL; // String buffer
|
||||
size_t length; // Length of stream objects
|
||||
|
||||
|
||||
// If we aren't creating object streams, return now...
|
||||
if (!pdf->objstm_dict || pdf->num_objstm == 0)
|
||||
return (true);
|
||||
|
||||
// Create a string buffer to hold the list of objects in the stream...
|
||||
if (!_pdfioStringAllocBuffer(pdf, &bptr))
|
||||
return (false);
|
||||
|
||||
// Loop through the array of objects and
|
||||
for (i = 0, length = 0; i < pdf->num_objs; i ++)
|
||||
{
|
||||
// Skip non-stream objects...
|
||||
obj = pdf->objs[i];
|
||||
|
||||
if (!obj->objstm_data)
|
||||
continue;
|
||||
|
||||
// Add this object to the initial list of objects and offsets for the stream...
|
||||
_pdfioStringPrintf(bptr, "%lu %lu\n", (unsigned long)obj->number, (unsigned long)length);
|
||||
|
||||
length += obj->objstm_datalen;
|
||||
}
|
||||
|
||||
// Now create the object stream with the collected data...
|
||||
pdfioDictSetNumber(pdf->objstm_dict, "First", bptr->bufptr - bptr->buffer);
|
||||
pdfioDictSetNumber(pdf->objstm_dict, "N", pdf->num_objstm);
|
||||
pdfioDictSetName(pdf->objstm_dict, "Filter", "FlateDecode");
|
||||
|
||||
if ((pdf->objstm_obj = pdfioFileCreateObj(pdf, pdf->objstm_dict)) == NULL)
|
||||
goto done;
|
||||
|
||||
if ((st = pdfioObjCreateStream(pdf->objstm_obj, PDFIO_FILTER_FLATE)) == NULL)
|
||||
{
|
||||
pdfioObjClose(pdf->objstm_obj);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!pdfioStreamWrite(st, bptr->buffer, (size_t)(bptr->bufptr - bptr->buffer)))
|
||||
goto done;
|
||||
|
||||
for (i = 0; i < pdf->num_objs; i ++)
|
||||
{
|
||||
// Skip non-stream objects...
|
||||
obj = pdf->objs[i];
|
||||
|
||||
if (!obj->objstm_data)
|
||||
continue;
|
||||
|
||||
// Write the stream data...
|
||||
if (!pdfioStreamWrite(st, obj->objstm_data, obj->objstm_datalen))
|
||||
goto done;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
|
||||
done:
|
||||
|
||||
pdfioStreamClose(st);
|
||||
|
||||
_pdfioStringFreeBuffer(pdf, bptr->buffer);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'write_pages()' - Write the PDF pages objects.
|
||||
//
|
||||
@@ -3013,38 +2902,32 @@ write_trailer(pdfio_file_t *pdf) // I - PDF file
|
||||
pdfio_array_t *w_array; // W array
|
||||
pdfio_obj_t *xref_obj; // Object
|
||||
pdfio_stream_t *xref_st; // Stream
|
||||
int offsize, // Size of object offsets
|
||||
idxsize; // Size of object stream indexes
|
||||
unsigned char buffer[20]; // Buffer entry
|
||||
int offsize; // Size of object offsets
|
||||
unsigned char buffer[10]; // Buffer entry
|
||||
pdfio_encryption_t encryption; // PDF encryption mode
|
||||
|
||||
// Disable encryption while we write the xref stream...
|
||||
encryption = pdf->encryption;
|
||||
pdf->encryption = PDFIO_ENCRYPTION_NONE;
|
||||
|
||||
// Figure out how many bytes are needed for the object offsets
|
||||
if (xref_offset < 0xff && pdf->num_objs < 0xff)
|
||||
// Figure out how many bytes are needed for the object numbers
|
||||
if (xref_offset < 0xff)
|
||||
offsize = 1;
|
||||
else if (xref_offset < 0xffff && pdf->num_objs < 0xffff)
|
||||
else if (xref_offset < 0xffff)
|
||||
offsize = 2;
|
||||
else if (xref_offset < 0xffffff && pdf->num_objs < 0xffffff)
|
||||
else if (xref_offset < 0xffffff)
|
||||
offsize = 3;
|
||||
else if (xref_offset < 0xffffffff && pdf->num_objs < 0xffffffff)
|
||||
else if (xref_offset < 0xffffffff)
|
||||
offsize = 4;
|
||||
else if (xref_offset < 0xffffffffff && pdf->num_objs < 0xffffffffff)
|
||||
else if (xref_offset < 0xffffffffff)
|
||||
offsize = 5;
|
||||
else if (xref_offset < 0xffffffffffff && pdf->num_objs < 0xffffffffffff)
|
||||
else if (xref_offset < 0xffffffffffff)
|
||||
offsize = 6;
|
||||
else if (xref_offset < 0xffffffffffffff && pdf->num_objs < 0xffffffffffffff)
|
||||
else if (xref_offset < 0xffffffffffffff)
|
||||
offsize = 7;
|
||||
else
|
||||
offsize = 8;
|
||||
|
||||
if (pdf->num_objstm <= 0x100)
|
||||
idxsize = 1;
|
||||
else // pdfioObjClose limits number of object stream values to 64k
|
||||
idxsize = 2;
|
||||
|
||||
// Create the object...
|
||||
if ((w_array = pdfioArrayCreate(pdf)) == NULL)
|
||||
{
|
||||
@@ -3055,7 +2938,7 @@ write_trailer(pdfio_file_t *pdf) // I - PDF file
|
||||
|
||||
pdfioArrayAppendNumber(w_array, 1);
|
||||
pdfioArrayAppendNumber(w_array, offsize);
|
||||
pdfioArrayAppendNumber(w_array, idxsize);
|
||||
pdfioArrayAppendNumber(w_array, 1);
|
||||
|
||||
if ((xref_dict = pdfioDictCreate(pdf)) == NULL)
|
||||
{
|
||||
@@ -3096,166 +2979,74 @@ write_trailer(pdfio_file_t *pdf) // I - PDF file
|
||||
pdfioStreamWrite(xref_st, buffer, (size_t)offsize + 2);
|
||||
|
||||
// Then write the "allocated" objects...
|
||||
buffer[0] = 1;
|
||||
|
||||
for (i = 0; i < pdf->num_objs; i ++)
|
||||
{
|
||||
obj = pdf->objs[i]; // Current object
|
||||
|
||||
if (obj->objstm_data)
|
||||
switch (offsize)
|
||||
{
|
||||
// Stream object
|
||||
size_t number = pdf->objstm_obj->number;
|
||||
// Object stream object number
|
||||
|
||||
buffer[0] = 2;
|
||||
|
||||
switch (offsize)
|
||||
{
|
||||
case 1 :
|
||||
buffer[1] = number & 255;
|
||||
break;
|
||||
case 2 :
|
||||
buffer[1] = (number >> 8) & 255;
|
||||
buffer[2] = number & 255;
|
||||
break;
|
||||
case 3 :
|
||||
buffer[1] = (number >> 16) & 255;
|
||||
buffer[2] = (number >> 8) & 255;
|
||||
buffer[3] = number & 255;
|
||||
break;
|
||||
case 4 :
|
||||
buffer[1] = (number >> 24) & 255;
|
||||
buffer[2] = (number >> 16) & 255;
|
||||
buffer[3] = (number >> 8) & 255;
|
||||
buffer[4] = number & 255;
|
||||
break;
|
||||
case 5 :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (number >> 32) & 255;
|
||||
case 1 :
|
||||
buffer[1] = obj->offset & 255;
|
||||
break;
|
||||
case 2 :
|
||||
buffer[1] = (obj->offset >> 8) & 255;
|
||||
buffer[2] = obj->offset & 255;
|
||||
break;
|
||||
case 3 :
|
||||
buffer[1] = (obj->offset >> 16) & 255;
|
||||
buffer[2] = (obj->offset >> 8) & 255;
|
||||
buffer[3] = obj->offset & 255;
|
||||
break;
|
||||
#ifdef _WIN32
|
||||
default :
|
||||
#endif // _WIN32
|
||||
buffer[2] = (number >> 24) & 255;
|
||||
buffer[3] = (number >> 16) & 255;
|
||||
buffer[4] = (number >> 8) & 255;
|
||||
buffer[5] = number & 255;
|
||||
break;
|
||||
case 6 :
|
||||
case 4 :
|
||||
buffer[1] = (obj->offset >> 24) & 255;
|
||||
buffer[2] = (obj->offset >> 16) & 255;
|
||||
buffer[3] = (obj->offset >> 8) & 255;
|
||||
buffer[4] = obj->offset & 255;
|
||||
break;
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (number >> 40) & 255;
|
||||
buffer[2] = (number >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[3] = (number >> 24) & 255;
|
||||
buffer[4] = (number >> 16) & 255;
|
||||
buffer[5] = (number >> 8) & 255;
|
||||
buffer[6] = number & 255;
|
||||
break;
|
||||
case 7 :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (number >> 48) & 255;
|
||||
buffer[2] = (number >> 40) & 255;
|
||||
buffer[3] = (number >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[4] = (number >> 24) & 255;
|
||||
buffer[5] = (number >> 16) & 255;
|
||||
buffer[6] = (number >> 8) & 255;
|
||||
buffer[7] = number & 255;
|
||||
break;
|
||||
default :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (number >> 56) & 255;
|
||||
buffer[2] = (number >> 48) & 255;
|
||||
buffer[3] = (number >> 40) & 255;
|
||||
buffer[4] = (number >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[5] = (number >> 24) & 255;
|
||||
buffer[6] = (number >> 16) & 255;
|
||||
buffer[7] = (number >> 8) & 255;
|
||||
buffer[8] = number & 255;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (idxsize)
|
||||
{
|
||||
case 1 :
|
||||
buffer[1 + offsize] = obj->objstm_number & 255;
|
||||
break;
|
||||
case 2 :
|
||||
default :
|
||||
buffer[1 + offsize] = (obj->objstm_number >> 8) & 255;
|
||||
buffer[2 + offsize] = obj->objstm_number & 255;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Regular object
|
||||
buffer[0] = 1;
|
||||
memset(buffer + 1 + offsize, 0, idxsize);
|
||||
|
||||
switch (offsize)
|
||||
{
|
||||
case 1 :
|
||||
buffer[1] = obj->offset & 255;
|
||||
break;
|
||||
case 2 :
|
||||
buffer[1] = (obj->offset >> 8) & 255;
|
||||
buffer[2] = obj->offset & 255;
|
||||
break;
|
||||
case 3 :
|
||||
buffer[1] = (obj->offset >> 16) & 255;
|
||||
buffer[2] = (obj->offset >> 8) & 255;
|
||||
buffer[3] = obj->offset & 255;
|
||||
break;
|
||||
case 4 :
|
||||
buffer[1] = (obj->offset >> 24) & 255;
|
||||
buffer[2] = (obj->offset >> 16) & 255;
|
||||
buffer[3] = (obj->offset >> 8) & 255;
|
||||
buffer[4] = obj->offset & 255;
|
||||
break;
|
||||
case 5 :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (obj->offset >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[2] = (obj->offset >> 24) & 255;
|
||||
buffer[3] = (obj->offset >> 16) & 255;
|
||||
buffer[4] = (obj->offset >> 8) & 255;
|
||||
buffer[5] = obj->offset & 255;
|
||||
break;
|
||||
case 6 :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (obj->offset >> 40) & 255;
|
||||
buffer[2] = (obj->offset >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[3] = (obj->offset >> 24) & 255;
|
||||
buffer[4] = (obj->offset >> 16) & 255;
|
||||
buffer[5] = (obj->offset >> 8) & 255;
|
||||
buffer[6] = obj->offset & 255;
|
||||
break;
|
||||
case 7 :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (obj->offset >> 48) & 255;
|
||||
buffer[2] = (obj->offset >> 40) & 255;
|
||||
buffer[3] = (obj->offset >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[4] = (obj->offset >> 24) & 255;
|
||||
buffer[5] = (obj->offset >> 16) & 255;
|
||||
buffer[6] = (obj->offset >> 8) & 255;
|
||||
buffer[7] = obj->offset & 255;
|
||||
break;
|
||||
default :
|
||||
#ifndef _WIN32 // Windows off_t is 32-bits?!?
|
||||
buffer[1] = (obj->offset >> 56) & 255;
|
||||
buffer[2] = (obj->offset >> 48) & 255;
|
||||
buffer[3] = (obj->offset >> 40) & 255;
|
||||
buffer[4] = (obj->offset >> 32) & 255;
|
||||
#endif // _WIN32
|
||||
buffer[5] = (obj->offset >> 24) & 255;
|
||||
buffer[6] = (obj->offset >> 16) & 255;
|
||||
buffer[7] = (obj->offset >> 8) & 255;
|
||||
buffer[8] = obj->offset & 255;
|
||||
break;
|
||||
}
|
||||
case 5 :
|
||||
buffer[1] = (obj->offset >> 32) & 255;
|
||||
buffer[2] = (obj->offset >> 24) & 255;
|
||||
buffer[3] = (obj->offset >> 16) & 255;
|
||||
buffer[4] = (obj->offset >> 8) & 255;
|
||||
buffer[5] = obj->offset & 255;
|
||||
break;
|
||||
case 6 :
|
||||
buffer[1] = (obj->offset >> 40) & 255;
|
||||
buffer[2] = (obj->offset >> 32) & 255;
|
||||
buffer[3] = (obj->offset >> 24) & 255;
|
||||
buffer[4] = (obj->offset >> 16) & 255;
|
||||
buffer[5] = (obj->offset >> 8) & 255;
|
||||
buffer[6] = obj->offset & 255;
|
||||
break;
|
||||
case 7 :
|
||||
buffer[1] = (obj->offset >> 48) & 255;
|
||||
buffer[2] = (obj->offset >> 40) & 255;
|
||||
buffer[3] = (obj->offset >> 32) & 255;
|
||||
buffer[4] = (obj->offset >> 24) & 255;
|
||||
buffer[5] = (obj->offset >> 16) & 255;
|
||||
buffer[6] = (obj->offset >> 8) & 255;
|
||||
buffer[7] = obj->offset & 255;
|
||||
break;
|
||||
default :
|
||||
buffer[1] = (obj->offset >> 56) & 255;
|
||||
buffer[2] = (obj->offset >> 48) & 255;
|
||||
buffer[3] = (obj->offset >> 40) & 255;
|
||||
buffer[4] = (obj->offset >> 32) & 255;
|
||||
buffer[5] = (obj->offset >> 24) & 255;
|
||||
buffer[6] = (obj->offset >> 16) & 255;
|
||||
buffer[7] = (obj->offset >> 8) & 255;
|
||||
buffer[8] = obj->offset & 255;
|
||||
break;
|
||||
#endif // !_WIN32
|
||||
}
|
||||
|
||||
if (!pdfioStreamWrite(xref_st, buffer, (size_t)(1 + offsize + idxsize)))
|
||||
if (!pdfioStreamWrite(xref_st, buffer, (size_t)offsize + 2))
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to write cross-reference table.");
|
||||
ret = false;
|
||||
@@ -3312,7 +3103,7 @@ write_trailer(pdfio_file_t *pdf) // I - PDF file
|
||||
pdfioDictSetObj(pdf->trailer_dict, "Root", pdf->root_obj);
|
||||
pdfioDictSetNumber(pdf->trailer_dict, "Size", (double)(pdf->num_objs + 1));
|
||||
|
||||
if (!_pdfioDictWrite((_pdfio_printf_t)_pdfioFilePrintf, pdf, /*obj*/NULL, pdf->trailer_dict, /*length*/NULL))
|
||||
if (!_pdfioDictWrite(pdf->trailer_dict, NULL, NULL))
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to write trailer.");
|
||||
ret = false;
|
||||
|
||||
338
pdfio-lzw.c
@@ -1,338 +0,0 @@
|
||||
//
|
||||
// LZW decoding functions for PDFio.
|
||||
//
|
||||
// This code is used to support (legacy) PDF object streams using the LZWDecode
|
||||
// filter as well as when embedding (legacy) GIF images. None of this is public
|
||||
// API and we only support reading (decoding) since FlateDecode is superior in
|
||||
// every way.
|
||||
//
|
||||
// Copyright © 2026 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
|
||||
#include "pdfio-private.h"
|
||||
|
||||
|
||||
//
|
||||
// Local functions...
|
||||
//
|
||||
|
||||
static void lzw_clear(_pdfio_lzw_t *lzw);
|
||||
static int lzw_get_code(_pdfio_lzw_t *lzw);
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioLZWCreate()' - Create a LZW decompressor.
|
||||
//
|
||||
|
||||
_pdfio_lzw_t * // O - LZW state
|
||||
_pdfioLZWCreate(int code_size, // I - Data code size in bits (typically 8 for PDF, 2-8 for GIF)
|
||||
int early, // I - Number of early codes
|
||||
bool reversed) // I - Reversed (GIF) LZW bit encoding?
|
||||
{
|
||||
_pdfio_lzw_t *lzw; // LZW state
|
||||
|
||||
|
||||
if ((lzw = (_pdfio_lzw_t *)calloc(1, sizeof(_pdfio_lzw_t))) != NULL)
|
||||
{
|
||||
lzw->def_code_size = code_size + 1;
|
||||
lzw->clear_code = (short)(1 << code_size);
|
||||
lzw->eod_code = lzw->clear_code + 1;
|
||||
lzw->early = early;
|
||||
lzw->reversed = reversed;
|
||||
|
||||
lzw_clear(lzw);
|
||||
}
|
||||
|
||||
return (lzw);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioLZWDelete()' - Delete a LZW decompressor.
|
||||
//
|
||||
|
||||
void
|
||||
_pdfioLZWDelete(_pdfio_lzw_t *lzw) // I - LZW state
|
||||
{
|
||||
free(lzw);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioLZWInflate()' - Decompress pending input data.
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` on error
|
||||
_pdfioLZWInflate(_pdfio_lzw_t *lzw) // I - LZW state
|
||||
{
|
||||
int cur_code, // Current code
|
||||
in_code; // Input code
|
||||
|
||||
|
||||
// Stop if we already saw the "end of data" code...
|
||||
if (lzw->saw_eod)
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: EOD, returning false.\n");
|
||||
lzw->error = "End of data.";
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Copy pending compressed data to the output buffer...
|
||||
while (lzw->stptr > lzw->stack && lzw->avail_out > 0)
|
||||
{
|
||||
*(lzw->next_out++) = *(--lzw->stptr);
|
||||
lzw->avail_out --;
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Unrolled value %d, stptr=%p(%ld), avail_out=%u\n", *(lzw->stptr), (void *)lzw->stptr, lzw->stptr - lzw->stack, (unsigned)lzw->avail_out);
|
||||
}
|
||||
|
||||
// Loop as long as we have room in the output buffer and data in the input
|
||||
// buffer...
|
||||
while (lzw->avail_out > 0)
|
||||
{
|
||||
if ((in_code = lzw_get_code(lzw)) < 0)
|
||||
{
|
||||
// Out of data, stop now...
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Out of data.\n");
|
||||
break;
|
||||
}
|
||||
else if (in_code == lzw->clear_code)
|
||||
{
|
||||
// Clear the compression tables and reset...
|
||||
lzw_clear(lzw);
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Clear.\n");
|
||||
continue;
|
||||
}
|
||||
else if (in_code == lzw->eod_code)
|
||||
{
|
||||
// End of data...
|
||||
lzw->saw_eod = true;
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: EOD.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// If we get this far we have something to write to the output buffer and/or
|
||||
// stack...
|
||||
if (lzw->first_code == 0xffff)
|
||||
{
|
||||
// First code...
|
||||
lzw->first_code = lzw->old_code = in_code;
|
||||
*(lzw->next_out++) = in_code;
|
||||
lzw->avail_out --;
|
||||
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: first_code=%d.\n", in_code);
|
||||
continue;
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: in_code=%d, old_code=%d.\n", in_code, lzw->old_code);
|
||||
|
||||
cur_code = in_code;
|
||||
|
||||
if (cur_code >= lzw->next_code)
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: New cur_code=%d, next_code=%d\n", cur_code, lzw->next_code);
|
||||
*(lzw->stptr++) = lzw->first_code;
|
||||
cur_code = lzw->old_code;
|
||||
}
|
||||
|
||||
while (cur_code >= lzw->clear_code)
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: cur_code=%d (%d,%d)\n", cur_code, lzw->table[cur_code].prefix_code, lzw->table[cur_code].suffix);
|
||||
|
||||
// Protect against overflow/loops...
|
||||
if (lzw->stptr >= (lzw->stack + sizeof(lzw->stack) / sizeof(lzw->stack[0])))
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Stack overflow, returning false.\n");
|
||||
lzw->error = "Output overflow.";
|
||||
return (false);
|
||||
}
|
||||
|
||||
// Add this character to the output stack and move to the next character
|
||||
// in the sequence...
|
||||
*(lzw->stptr++) = lzw->table[cur_code].suffix;
|
||||
|
||||
if (cur_code == lzw->table[cur_code].prefix_code)
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Table loop on code %d, returning false.\n", cur_code);
|
||||
lzw->error = "Table loop detected.";
|
||||
return (false);
|
||||
}
|
||||
|
||||
cur_code = lzw->table[cur_code].prefix_code;
|
||||
}
|
||||
|
||||
if (lzw->stptr >= (lzw->stack + sizeof(lzw->stack) / sizeof(lzw->stack[0])))
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Stack overflow, returning false.\n");
|
||||
lzw->error = "Output overflow.";
|
||||
return (false);
|
||||
}
|
||||
|
||||
*(lzw->stptr++) = lzw->first_code = lzw->table[cur_code].suffix;
|
||||
|
||||
if ((cur_code = lzw->next_code) < 4096)
|
||||
{
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Adding code %d (%d,%d), next_size_code=%d\n", cur_code, lzw->old_code, lzw->first_code, lzw->next_size_code);
|
||||
|
||||
lzw->table[cur_code].prefix_code = lzw->old_code;
|
||||
lzw->table[cur_code].suffix = lzw->first_code;
|
||||
lzw->next_code ++;
|
||||
|
||||
if (lzw->next_code >= lzw->next_size_code && lzw->cur_code_size < 12)
|
||||
{
|
||||
lzw->cur_code_size ++;
|
||||
lzw->next_size_code = (1 << lzw->cur_code_size) - lzw->early;
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Increased code size to %u, next_size_code=%u\n", lzw->cur_code_size, lzw->next_size_code);
|
||||
}
|
||||
}
|
||||
|
||||
lzw->old_code = (uint16_t)in_code;
|
||||
|
||||
while (lzw->stptr > lzw->stack && lzw->avail_out > 0)
|
||||
{
|
||||
*(lzw->next_out++) = *(--lzw->stptr);
|
||||
lzw->avail_out --;
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Unrolled value %d, stptr=%p(%ld), avail_out=%u\n", *(lzw->stptr), (void *)lzw->stptr, lzw->stptr - lzw->stack, (unsigned)lzw->avail_out);
|
||||
}
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfioLZWInflate: Returning true, avail_in=%u, avail_out=%u.\n", (unsigned)lzw->avail_in, (unsigned)lzw->avail_out);
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'lzw_clear()' - Clear the compression table.
|
||||
//
|
||||
|
||||
static void
|
||||
lzw_clear(_pdfio_lzw_t *lzw) // I - LZW state
|
||||
{
|
||||
uint16_t i; // Looping var
|
||||
|
||||
|
||||
lzw->cur_code_size = lzw->def_code_size;
|
||||
lzw->next_code = lzw->clear_code + 2;
|
||||
lzw->next_size_code = (1 << lzw->cur_code_size) - lzw->early;
|
||||
lzw->first_code = 0xffff;
|
||||
lzw->old_code = 0xffff;
|
||||
|
||||
memset(lzw->table, 0, sizeof(lzw->table));
|
||||
|
||||
for (i = 0; i < lzw->clear_code; i ++)
|
||||
lzw->table[i].suffix = i;
|
||||
|
||||
lzw->stptr = lzw->stack;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'lzw_get_code()' - Get a code from the input buffer.
|
||||
//
|
||||
|
||||
static int // O - Code or -1 if there is not enough data available
|
||||
lzw_get_code(_pdfio_lzw_t *lzw) // I - LZW state
|
||||
{
|
||||
uint16_t code, // Code
|
||||
in_bit; // Bit offset in buffer
|
||||
uint8_t bits, // Bits in current byte
|
||||
boff, // Bit offset in current byte
|
||||
byte, // Current byte
|
||||
remaining; // Remaining bits for code
|
||||
static uint8_t mask[8] = // Value mask
|
||||
{
|
||||
0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
|
||||
};
|
||||
static uint8_t rbits[8] = // Right-to-left bit masks
|
||||
{
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
|
||||
};
|
||||
|
||||
|
||||
// Fill input bytes as needed...
|
||||
if ((lzw->in_bit + lzw->cur_code_size) > lzw->in_bits)
|
||||
{
|
||||
uint16_t in_used = lzw->in_bits / 8,
|
||||
// Number of input bytes
|
||||
in_offset = lzw->in_bit / 8,
|
||||
// Offset to current input
|
||||
in_add; // Number of bytes to "read"
|
||||
|
||||
|
||||
if (lzw->avail_in == 0)
|
||||
{
|
||||
// No more data
|
||||
PDFIO_DEBUG("lzw_get_code: No data, returning -1.\n");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (in_offset > 0)
|
||||
{
|
||||
// Make room in the input buffer
|
||||
memmove(lzw->in_bytes, lzw->in_bytes + in_offset, in_used - in_offset);
|
||||
in_used -= in_offset;
|
||||
lzw->in_bit &= 7;
|
||||
}
|
||||
|
||||
if ((in_add = sizeof(lzw->in_bytes) - in_used) > lzw->avail_in)
|
||||
in_add = lzw->avail_in;
|
||||
|
||||
memcpy(lzw->in_bytes + in_used, lzw->next_in, in_add);
|
||||
lzw->next_in += in_add;
|
||||
lzw->avail_in -= in_add;
|
||||
lzw->in_bits = 8 * (in_used + in_add);
|
||||
|
||||
if ((lzw->in_bit + lzw->cur_code_size) > lzw->in_bits)
|
||||
{
|
||||
// Not enough data
|
||||
PDFIO_DEBUG("lzw_get_code: Not enough data, returning -1.\n");
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("lzw_get_code: in_bit=%u, in_bits=%u, in_bytes=<...%02X%02X%02X...>, cur_code_size=%u\n", lzw->in_bit, lzw->in_bits, lzw->in_bytes[lzw->in_bit / 8], lzw->in_bytes[lzw->in_bit / 8 + 1], lzw->in_bytes[lzw->in_bit / 8 + 2], lzw->cur_code_size);
|
||||
|
||||
// Now extract the code from the buffer...
|
||||
if (lzw->reversed)
|
||||
{
|
||||
// Insane GIF-style right-to-left bit encoding...
|
||||
for (code = 0, in_bit = lzw->in_bit + lzw->cur_code_size - 1, remaining = lzw->cur_code_size; remaining > 0; in_bit --, remaining --)
|
||||
{
|
||||
code = (code << 1) | ((lzw->in_bytes[in_bit / 8] & rbits[in_bit & 7]) != 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TIFF/PDF-style left-to-right bit encoding...
|
||||
for (code = 0, in_bit = lzw->in_bit, remaining = lzw->cur_code_size; remaining > 0; in_bit += bits, remaining -= bits)
|
||||
{
|
||||
// See how many bits we can extract from the current byte...
|
||||
boff = (in_bit & 7);
|
||||
byte = lzw->in_bytes[in_bit / 8];
|
||||
bits = 8 - boff;
|
||||
if (bits > remaining)
|
||||
bits = remaining;
|
||||
|
||||
// Get those bits
|
||||
if (bits == 8) // Full byte from buffer
|
||||
code = (code << 8) | byte;
|
||||
else // Partial byte from buffer
|
||||
code = (code << bits) | ((byte >> (8 - bits - boff)) & mask[bits]);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the position in the input buffer and return the code...
|
||||
lzw->in_bit += lzw->cur_code_size;
|
||||
|
||||
#ifdef DEBUG
|
||||
if (code >= 0x20 && code < 0x7f)
|
||||
PDFIO_DEBUG("lzw_get_code: Returning %u('%c').\n", code, code);
|
||||
else
|
||||
PDFIO_DEBUG("lzw_get_code: Returning %u.\n", code);
|
||||
#endif // DEBUG
|
||||
|
||||
return ((int)code);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF object functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -32,43 +32,8 @@ pdfioObjClose(pdfio_obj_t *obj) // I - Object
|
||||
}
|
||||
|
||||
// Write what remains for the object...
|
||||
if (!obj->offset && !obj->objstm_data)
|
||||
if (!obj->offset)
|
||||
{
|
||||
// Are we using object streams and is this object eligible?
|
||||
if (obj->pdf->objstm_dict && !obj->pdf->objstm_obj && obj != obj->pdf->encrypt_obj && obj->pdf->num_objstm < 0x10000 && (obj->value.type == PDFIO_VALTYPE_ARRAY || obj->value.type == PDFIO_VALTYPE_DICT))
|
||||
{
|
||||
// Add this to the object stream...
|
||||
_pdfio_strbuf_t *bptr; // String buffer
|
||||
|
||||
if (!_pdfioStringAllocBuffer(obj->pdf, &bptr))
|
||||
goto regular_obj;
|
||||
|
||||
if (!_pdfioValueWrite((_pdfio_printf_t)_pdfioStringPrintf, bptr, /*obj*/NULL, &(obj->value), /*length*/NULL))
|
||||
{
|
||||
_pdfioStringFreeBuffer(obj->pdf, bptr->buffer);
|
||||
goto regular_obj;
|
||||
}
|
||||
|
||||
_pdfioStringPrintf(bptr, "\n");
|
||||
|
||||
if ((obj->objstm_data = pdfioStringCreate(obj->pdf, bptr->buffer)) == NULL)
|
||||
{
|
||||
_pdfioStringFreeBuffer(obj->pdf, bptr->buffer);
|
||||
goto regular_obj;
|
||||
}
|
||||
|
||||
// If we got this far then we have the value string and can assign an
|
||||
// object number, which is all that is required for now...
|
||||
obj->objstm_datalen = (size_t)(bptr->bufptr - bptr->buffer);
|
||||
obj->objstm_number = obj->pdf->num_objstm;
|
||||
obj->pdf->num_objstm ++;
|
||||
|
||||
_pdfioStringFreeBuffer(obj->pdf, bptr->buffer);
|
||||
return (true);
|
||||
}
|
||||
|
||||
regular_obj:
|
||||
|
||||
// Write the object value
|
||||
if (!_pdfioObjWriteHeader(obj))
|
||||
return (false);
|
||||
@@ -104,7 +69,7 @@ pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
ssize_t bytes; // Bytes read
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioObjCopy(pdf=%p, srcobj=%p(%u,%p))\n", (void *)pdf, (void *)srcobj, srcobj ? (unsigned)srcobj->number : 0, srcobj ? (void *)srcobj->pdf : NULL);
|
||||
PDFIO_DEBUG("pdfioObjCopy(pdf=%p, srcobj=%p(%p))\n", (void *)pdf, (void *)srcobj, srcobj ? (void *)srcobj->pdf : NULL);
|
||||
|
||||
// Range check input
|
||||
if (!pdf || !srcobj)
|
||||
@@ -112,10 +77,7 @@ pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
// Load the object value if needed...
|
||||
if (srcobj->value.type == PDFIO_VALTYPE_NONE)
|
||||
{
|
||||
if (!_pdfioObjLoad(srcobj))
|
||||
return (NULL);
|
||||
}
|
||||
_pdfioObjLoad(srcobj);
|
||||
|
||||
// See if we have already mapped this object...
|
||||
if ((dstobj = _pdfioFileFindMappedObj(pdf, srcobj->pdf, srcobj->number)) != NULL)
|
||||
@@ -582,8 +544,6 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
|
||||
pdfio_stream_t *st; // Stream
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioObjOpenStream(obj=%p(%lu), decode=%s)\n", (void *)obj, obj ? (unsigned long)obj->number : 0, decode ? "true" : "false");
|
||||
|
||||
// Range check input...
|
||||
if (!obj)
|
||||
return (NULL);
|
||||
@@ -603,10 +563,7 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
|
||||
|
||||
// No stream if there is no dict or offset to a stream...
|
||||
if (obj->value.type != PDFIO_VALTYPE_DICT || !obj->stream_offset)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioObjOpenStream: value.type=%d, stream_offset=%ld\n", obj->value.type, (long)obj->stream_offset);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Open the stream...
|
||||
if ((st = _pdfioStreamOpen(obj, decode)) != NULL)
|
||||
@@ -643,7 +600,7 @@ _pdfioObjWriteHeader(pdfio_obj_t *obj) // I - Object
|
||||
if (!_pdfioFilePrintf(obj->pdf, "%lu %u obj\n", (unsigned long)obj->number, obj->generation))
|
||||
return (false);
|
||||
|
||||
if (!_pdfioValueWrite((_pdfio_printf_t)_pdfioFilePrintf, obj->pdf, obj, &obj->value, &obj->length_offset))
|
||||
if (!_pdfioValueWrite(obj->pdf, obj, &obj->value, &obj->length_offset))
|
||||
return (false);
|
||||
|
||||
return (_pdfioFilePuts(obj->pdf, "\n"));
|
||||
|
||||
332
pdfio-page.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF page functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2022 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -15,7 +15,6 @@
|
||||
//
|
||||
|
||||
static _pdfio_value_t *get_contents(pdfio_obj_t *page);
|
||||
static _pdfio_value_t *get_page_value(pdfio_obj_t *page, const char *key);
|
||||
|
||||
|
||||
//
|
||||
@@ -53,158 +52,6 @@ pdfioPageCopy(pdfio_file_t *pdf, // I - PDF file
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetArray()' - Get an array value from the page dictionary.
|
||||
//
|
||||
// This function looks up an array value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
pdfio_array_t * // O - Array or `NULL` if none
|
||||
pdfioPageGetArray(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_ARRAY)
|
||||
return (v->value.array);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetBinary()' - Get a binary value from the page dictionary.
|
||||
//
|
||||
// This function looks up a binary value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
unsigned char * // O - Pointer to binary data or `NULL` if none
|
||||
pdfioPageGetBinary(pdfio_obj_t *page, // I - Page object
|
||||
const char *key, // I - Dictionary key
|
||||
size_t *length) // O - Length of value
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_BINARY)
|
||||
{
|
||||
if (length)
|
||||
*length = v->value.binary.datalen;
|
||||
|
||||
return (v->value.binary.data);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetBoolean()' - Get a boolean value from the page dictionary.
|
||||
//
|
||||
// This function looks up a boolean value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
bool // O - Boolean value or `false` if none
|
||||
pdfioPageGetBoolean(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_ARRAY)
|
||||
return (v->value.boolean);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetDate()' - Get a date value from the page dictionary.
|
||||
//
|
||||
// This function looks up a date value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
time_t // O - Date/time or `0` if none
|
||||
pdfioPageGetDate(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_DATE)
|
||||
return (v->value.date);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetDict()' - Get a dictionary value from the page dictionary.
|
||||
//
|
||||
// This function looks up a dictionary value in the page dictionary, either in
|
||||
// the specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
pdfio_dict_t * // O - Dictionary or `NULL` if none
|
||||
pdfioPageGetDict(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_DICT)
|
||||
return (v->value.dict);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetName()' - Get a name value from the page dictionary.
|
||||
//
|
||||
// This function looks up a name value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
const char * // O - Name string or `NULL` if none
|
||||
pdfioPageGetName(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_NAME)
|
||||
return (v->value.name);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetNumStreams()' - Get the number of content streams for a page object.
|
||||
//
|
||||
@@ -226,112 +73,6 @@ pdfioPageGetNumStreams(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetNumber()' - Get a number value from the page dictionary.
|
||||
//
|
||||
// This function looks up a number value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
double // O - Number value or `0.0` if none
|
||||
pdfioPageGetNumber(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_NUMBER)
|
||||
return (v->value.number);
|
||||
else
|
||||
return (0.0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetObj()' - Get an indirect object value from the page dictionary.
|
||||
//
|
||||
// This function looks up an indirect object value in the page dictionary,
|
||||
// either in the specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
pdfio_obj_t * // O - Object or `NULL` if none
|
||||
pdfioPageGetObj(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_INDIRECT)
|
||||
return (pdfioFileFindObj(page->pdf, v->value.indirect.number));
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetRect()' - Get a rectangle value from the page dictionary.
|
||||
//
|
||||
// This function looks up a rectangle value in the page dictionary, either in
|
||||
// the specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
pdfio_rect_t * // O - Rectangle or `NULL` if none
|
||||
pdfioPageGetRect(pdfio_obj_t *page, // I - Page object
|
||||
const char *key, // I - Dictionary key
|
||||
pdfio_rect_t *rect) // O - Rectangle
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_ARRAY && pdfioArrayGetSize(v->value.array) == 4)
|
||||
{
|
||||
rect->x1 = pdfioArrayGetNumber(v->value.array, 0);
|
||||
rect->y1 = pdfioArrayGetNumber(v->value.array, 1);
|
||||
rect->x2 = pdfioArrayGetNumber(v->value.array, 2);
|
||||
rect->y2 = pdfioArrayGetNumber(v->value.array, 3);
|
||||
|
||||
return (rect);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageGetString()' - Get a string value from the page dictionary.
|
||||
//
|
||||
// This function looks up a string value in the page dictionary, either in the
|
||||
// specified object or one of its parents.
|
||||
//
|
||||
// @since PDFio 1.7@
|
||||
//
|
||||
|
||||
const char * // O - String value or `NULL` if none
|
||||
pdfioPageGetString(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = get_page_value(page, key);
|
||||
// Dictionary value
|
||||
|
||||
|
||||
if (v && v->type == PDFIO_VALTYPE_STRING)
|
||||
return (v->value.string);
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'pdfioPageOpenStream()' - Open a content stream for a page.
|
||||
//
|
||||
@@ -346,28 +87,14 @@ pdfioPageOpenStream(
|
||||
// Contents value
|
||||
|
||||
|
||||
PDFIO_DEBUG("pdfioPageOpenStream(page=%p(%lu), n=%lu, decode=%s)\n", (void *)page, page ? (unsigned long)page->number : 0, (unsigned long)n, decode ? "true" : "false");
|
||||
|
||||
if (!contents)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioPageOpenStream: No contents.\n");
|
||||
return (NULL);
|
||||
}
|
||||
else if (contents->type == PDFIO_VALTYPE_ARRAY && n < pdfioArrayGetSize(contents->value.array))
|
||||
{
|
||||
PDFIO_DEBUG("pdfioPageOpenStream: Contents is array, opening numbered content stream.\n");
|
||||
return (pdfioObjOpenStream(pdfioArrayGetObj(contents->value.array, n), decode));
|
||||
}
|
||||
else if (n)
|
||||
{
|
||||
PDFIO_DEBUG("pdfioPageOpenStream: Numbered stream does not exist.\n");
|
||||
return (NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
PDFIO_DEBUG("pdfioPageOpenStream: Opening single content stream %d.\n", (int)contents->value.indirect.number);
|
||||
return (pdfioObjOpenStream(pdfioFileFindObj(page->pdf, contents->value.indirect.number), decode));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -378,10 +105,6 @@ pdfioPageOpenStream(
|
||||
static _pdfio_value_t * // O - Value or NULL on error
|
||||
get_contents(pdfio_obj_t *page) // I - Page object
|
||||
{
|
||||
_pdfio_value_t *contents; // Contents value
|
||||
pdfio_obj_t *obj; // Contents object
|
||||
|
||||
|
||||
// Range check input...
|
||||
if (!page)
|
||||
return (NULL);
|
||||
@@ -396,56 +119,5 @@ get_contents(pdfio_obj_t *page) // I - Page object
|
||||
if (page->value.type != PDFIO_VALTYPE_DICT)
|
||||
return (NULL);
|
||||
|
||||
contents = _pdfioDictGetValue(page->value.value.dict, "Contents");
|
||||
|
||||
if (contents->type == PDFIO_VALTYPE_INDIRECT)
|
||||
{
|
||||
// See if the indirect object is a stream or an array of indirect object
|
||||
// references...
|
||||
if ((obj = pdfioFileFindObj(page->pdf, contents->value.indirect.number)) != NULL)
|
||||
{
|
||||
if (obj->value.type == PDFIO_VALTYPE_NONE)
|
||||
{
|
||||
if (!_pdfioObjLoad(obj))
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (obj->value.type == PDFIO_VALTYPE_ARRAY)
|
||||
contents = &(obj->value);
|
||||
}
|
||||
}
|
||||
|
||||
return (contents);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'get_page_value()' - Get a page dictionary value, including parents.
|
||||
//
|
||||
|
||||
static _pdfio_value_t * // O - Dictionary value or `NULL` if none
|
||||
get_page_value(pdfio_obj_t *page, // I - Page object
|
||||
const char *key) // I - Dictionary key
|
||||
{
|
||||
_pdfio_value_t *v = NULL; // Dictionary value
|
||||
|
||||
|
||||
while (page != NULL)
|
||||
{
|
||||
// Load the page object as needed...
|
||||
if (page->value.type == PDFIO_VALTYPE_NONE && !_pdfioObjLoad(page))
|
||||
break;
|
||||
|
||||
// If there isn't a dictionary for the object, stop...
|
||||
if (page->value.type != PDFIO_VALTYPE_DICT)
|
||||
break;
|
||||
|
||||
// Lookup the key...
|
||||
if ((v = _pdfioDictGetValue(page->value.value.dict, key)) != NULL)
|
||||
break;
|
||||
|
||||
page = pdfioDictGetObj(page->value.value.dict, "Parent");
|
||||
}
|
||||
|
||||
return (v);
|
||||
return (_pdfioDictGetValue(page->value.value.dict, "Contents"));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Private header file for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -94,12 +94,10 @@
|
||||
//
|
||||
|
||||
# define PDFIO_MAX_DEPTH 32 // Maximum nesting depth for values
|
||||
# define PDFIO_MAX_STRING 131072 // Maximum length of string
|
||||
# define PDFIO_MAX_STRING 65536 // Maximum length of string
|
||||
|
||||
typedef void (*_pdfio_extfree_t)(void *p);
|
||||
typedef void (*_pdfio_extfree_t)(void *);
|
||||
// Extension data free function
|
||||
typedef bool (*_pdfio_printf_t)(void *data, const char *format, ...);
|
||||
// "printf" function
|
||||
|
||||
typedef enum _pdfio_mode_e // Read/write mode
|
||||
{
|
||||
@@ -178,7 +176,7 @@ typedef struct _pdfio_value_s // Value structure
|
||||
typedef struct _pdfio_aes_s // AES encryption state
|
||||
{
|
||||
size_t round_size; // Size of round key
|
||||
uint8_t round_key[256], // Round key
|
||||
uint8_t round_key[240], // Round key
|
||||
iv[16]; // Initialization vector
|
||||
} _pdfio_aes_t;
|
||||
|
||||
@@ -213,38 +211,6 @@ typedef union _pdfio_crypto_ctx_u // Cryptographic contexts
|
||||
} _pdfio_crypto_ctx_t;
|
||||
typedef size_t (*_pdfio_crypto_cb_t)(_pdfio_crypto_ctx_t *ctx, uint8_t *outbuffer, const uint8_t *inbuffer, size_t len);
|
||||
|
||||
typedef struct _pdfio_lzws_s // LZW string table
|
||||
{
|
||||
uint16_t prefix_code, // Prefix code
|
||||
suffix; // Suffix (character)
|
||||
} _pdfio_lzws_t;
|
||||
|
||||
typedef struct _pdfio_lzw_s // LZW state
|
||||
{
|
||||
uint8_t *next_in; // Next input byte
|
||||
size_t avail_in; // Available input bytes
|
||||
uint8_t in_bytes[256]; // Current input bytes
|
||||
uint16_t in_bit, // Current input bit
|
||||
in_bits; // Total input bits
|
||||
uint8_t *next_out; // Next output byte
|
||||
size_t avail_out; // Available output bytes
|
||||
uint8_t cur_code_size, // Current code size
|
||||
def_code_size, // Initial/default code size
|
||||
early; // Early code change offset
|
||||
bool reversed; // Reversed bit encoding?
|
||||
uint16_t clear_code, // Clear code
|
||||
eod_code, // End code
|
||||
next_code, // Next code to be used
|
||||
next_size_code, // Code where we need to increase the code size
|
||||
first_code, // First code in sequence
|
||||
old_code, // Previous code in sequence
|
||||
stack[8192], // Output stack
|
||||
*stptr; // Current stack pointer
|
||||
_pdfio_lzws_t table[4096]; // String table
|
||||
bool saw_eod; // Saw end-of-data code?
|
||||
const char *error; // Error, if any
|
||||
} _pdfio_lzw_t;
|
||||
|
||||
struct _pdfio_array_s
|
||||
{
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
@@ -277,11 +243,9 @@ typedef struct _pdfio_objmap_s // PDF object map
|
||||
typedef struct _pdfio_strbuf_s // PDF string buffer
|
||||
{
|
||||
struct _pdfio_strbuf_s *next; // Next string buffer
|
||||
pdfio_file_t *pdf; // PDF file
|
||||
bool bufused; // Is this string buffer being used?
|
||||
char buffer[PDFIO_MAX_STRING + 32],
|
||||
char buffer[PDFIO_MAX_STRING + 32];
|
||||
// String buffer
|
||||
*bufptr; // Pointer into buffer
|
||||
} _pdfio_strbuf_t;
|
||||
|
||||
struct _pdfio_file_s // PDF file structure
|
||||
@@ -323,10 +287,7 @@ struct _pdfio_file_s // PDF file structure
|
||||
pdfio_dict_t *encrypt_dict; // De/Encryption dictionary
|
||||
pdfio_obj_t *cgats001_obj, // CGATS001 ICC profile object
|
||||
*cp1252_obj, // CP1252 font encoding object
|
||||
*unicode_obj, // Unicode font encoding object
|
||||
*objstm_obj; // Object stream object
|
||||
pdfio_dict_t *objstm_dict; // Object stream dictionary
|
||||
size_t num_objstm; // Number of objects in object stream
|
||||
*unicode_obj; // Unicode font encoding object
|
||||
pdfio_array_t *id_array; // ID array
|
||||
bool encrypt_metadata; // Encrypt metadata?
|
||||
pdfio_dict_t *markinfo; // MarkInfo dictionary, if any
|
||||
@@ -366,9 +327,6 @@ struct _pdfio_obj_s // Object
|
||||
size_t stream_length; // Length of stream, if any
|
||||
_pdfio_value_t value; // Dictionary/number/etc. value
|
||||
pdfio_stream_t *stream; // Open stream, if any
|
||||
size_t objstm_number; // Object stream number
|
||||
char *objstm_data; // Object stream data, if any
|
||||
size_t objstm_datalen; // Object stream data length
|
||||
void *data; // Extension data, if any
|
||||
_pdfio_extfree_t datafree; // Free callback for extension data
|
||||
};
|
||||
@@ -383,20 +341,12 @@ struct _pdfio_stream_s // Stream
|
||||
char buffer[8192], // Read/write buffer
|
||||
*bufptr, // Current position in buffer
|
||||
*bufend; // End of buffer
|
||||
size_t a85size; // Size of ASCII85Decode buffer
|
||||
char *a85buffer, // ASCII85Decode buffer, if any
|
||||
*a85bufptr, // Pointer into ASCII85Decode buffer
|
||||
*a85bufend, // End of data in ASCII85Decode buffer
|
||||
a85decode[4], // Current block of decoded characters
|
||||
*a85decptr, // Pointer into decoded characters
|
||||
*a85decend; // Last decoded character
|
||||
z_stream flate; // Flate filter state
|
||||
_pdfio_lzw_t *lzw; // LZW filter state
|
||||
_pdfio_predictor_t predictor; // Predictor function, if any
|
||||
size_t pbpixel, // Size of a pixel in bytes
|
||||
pbsize, // Predictor buffer size, if any
|
||||
cbsize; // Compressed data buffer size
|
||||
uint8_t *cbuffer, // Compressed data buffer
|
||||
unsigned char *cbuffer, // Compressed data buffer
|
||||
*prbuffer, // Raw buffer (previous line), as needed
|
||||
*psbuffer; // PNG filter buffer, as needed
|
||||
_pdfio_crypto_cb_t crypto_cb; // Encryption/descryption callback, if any
|
||||
@@ -418,7 +368,7 @@ extern void _pdfioArrayDebug(pdfio_array_t *a, FILE *fp) _PDFIO_INTERNAL;
|
||||
extern void _pdfioArrayDelete(pdfio_array_t *a) _PDFIO_INTERNAL;
|
||||
extern _pdfio_value_t *_pdfioArrayGetValue(pdfio_array_t *a, size_t n) _PDFIO_INTERNAL;
|
||||
extern pdfio_array_t *_pdfioArrayRead(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_token_t *ts, size_t depth) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioArrayWrite(_pdfio_printf_t cb, void *cbdata, pdfio_obj_t *obj, pdfio_array_t *a) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioArrayWrite(pdfio_array_t *a, pdfio_obj_t *obj) _PDFIO_INTERNAL;
|
||||
|
||||
extern void _pdfioCryptoAESInit(_pdfio_aes_t *ctx, const uint8_t *key, size_t keylen, const uint8_t *iv) _PDFIO_INTERNAL;
|
||||
extern size_t _pdfioCryptoAESDecrypt(_pdfio_aes_t *ctx, uint8_t *outbuffer, const uint8_t *inbuffer, size_t len) _PDFIO_INTERNAL;
|
||||
@@ -443,7 +393,7 @@ extern void _pdfioDictDelete(pdfio_dict_t *dict) _PDFIO_INTERNAL;
|
||||
extern _pdfio_value_t *_pdfioDictGetValue(pdfio_dict_t *dict, const char *key) _PDFIO_INTERNAL;
|
||||
extern pdfio_dict_t *_pdfioDictRead(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_token_t *ts, size_t depth) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioDictSetValue(pdfio_dict_t *dict, const char *key, _pdfio_value_t *value) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioDictWrite(_pdfio_printf_t cb, void *cbdata, pdfio_obj_t *obj, pdfio_dict_t *dict, off_t *length) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioDictWrite(pdfio_dict_t *dict, pdfio_obj_t *obj, off_t *length) _PDFIO_INTERNAL;
|
||||
|
||||
extern bool _pdfioFileAddMappedObj(pdfio_file_t *pdf, pdfio_obj_t *dst_obj, pdfio_obj_t *src_obj) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioFileAddPage(pdfio_file_t *pdf, pdfio_obj_t *obj) _PDFIO_INTERNAL;
|
||||
@@ -463,10 +413,6 @@ extern off_t _pdfioFileSeek(pdfio_file_t *pdf, off_t offset, int whence) _PDFIO
|
||||
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 _pdfio_lzw_t *_pdfioLZWCreate(int def_code_size, int early, bool reversed) _PDFIO_INTERNAL;
|
||||
extern void _pdfioLZWDelete(_pdfio_lzw_t *lzw) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioLZWInflate(_pdfio_lzw_t *lzw) _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;
|
||||
@@ -476,10 +422,9 @@ extern bool _pdfioObjWriteHeader(pdfio_obj_t *obj) _PDFIO_INTERNAL;
|
||||
extern pdfio_stream_t *_pdfioStreamCreate(pdfio_obj_t *obj, pdfio_obj_t *length_obj, size_t cbsize, pdfio_filter_t compression) _PDFIO_INTERNAL;
|
||||
extern pdfio_stream_t *_pdfioStreamOpen(pdfio_obj_t *obj, bool decode) _PDFIO_INTERNAL;
|
||||
|
||||
extern char *_pdfioStringAllocBuffer(pdfio_file_t *pdf, _pdfio_strbuf_t **bptr);
|
||||
extern char *_pdfioStringAllocBuffer(pdfio_file_t *pdf);
|
||||
extern void _pdfioStringFreeBuffer(pdfio_file_t *pdf, char *buffer);
|
||||
extern bool _pdfioStringIsAllocated(pdfio_file_t *pdf, const char *s) _PDFIO_INTERNAL;
|
||||
extern bool _pdfioStringPrintf(_pdfio_strbuf_t *bptr, const char *format, ...) _PDFIO_INTERNAL;
|
||||
|
||||
extern void _pdfioTokenClear(_pdfio_token_t *tb) _PDFIO_INTERNAL;
|
||||
extern void _pdfioTokenFlush(_pdfio_token_t *tb) _PDFIO_INTERNAL;
|
||||
@@ -493,7 +438,7 @@ extern bool _pdfioValueDecrypt(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_valu
|
||||
extern void _pdfioValueDebug(_pdfio_value_t *v, FILE *fp) _PDFIO_INTERNAL;
|
||||
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 bool _pdfioValueWrite(_pdfio_printf_t cb, void *cbdata, 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
|
||||
|
||||
523
pdfio-stream.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF stream functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -14,8 +14,6 @@
|
||||
// Local functions...
|
||||
//
|
||||
|
||||
static ssize_t stream_get_bytes(pdfio_stream_t *st, void *buffer, size_t bytes);
|
||||
static ssize_t stream_inflate(pdfio_stream_t *st, uint8_t *buffer, size_t bytes, bool exactly);
|
||||
static unsigned char stream_paeth(unsigned char a, unsigned char b, unsigned char c);
|
||||
static ssize_t stream_read(pdfio_stream_t *st, char *buffer, size_t bytes);
|
||||
static bool stream_write(pdfio_stream_t *st, const void *buffer, size_t bytes);
|
||||
@@ -41,8 +39,6 @@ pdfioStreamClose(pdfio_stream_t *st) // I - Stream
|
||||
{
|
||||
if (st->filter == PDFIO_FILTER_FLATE)
|
||||
inflateEnd(&(st->flate));
|
||||
else if (st->filter == PDFIO_FILTER_LZW)
|
||||
_pdfioLZWDelete(st->lzw);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -176,7 +172,6 @@ pdfioStreamClose(pdfio_stream_t *st) // I - Stream
|
||||
|
||||
st->pdf->current_obj = NULL;
|
||||
|
||||
free(st->a85buffer);
|
||||
free(st->cbuffer);
|
||||
free(st->prbuffer);
|
||||
free(st->psbuffer);
|
||||
@@ -484,34 +479,10 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
pdfio_array_t *fa = pdfioDictGetArray(dict, "Filter");
|
||||
// Filter array
|
||||
|
||||
if (!filter && fa)
|
||||
if (!filter && fa && pdfioArrayGetSize(fa) == 1)
|
||||
{
|
||||
const char *filter0 = pdfioArrayGetName(fa, 0);
|
||||
// First filter
|
||||
|
||||
if (pdfioArrayGetSize(fa) == 1)
|
||||
{
|
||||
// Support single-valued arrays...
|
||||
filter = filter0;
|
||||
}
|
||||
else if (pdfioArrayGetSize(fa) == 2 && filter0 && !strcmp(filter0, "ASCII85Decode"))
|
||||
{
|
||||
// Support ASCII85Decode + something else
|
||||
st->a85size = 5200; // Enough for 4k of decoded data
|
||||
|
||||
// Allocate the ASCII85Decode buffer...
|
||||
if ((st->a85buffer = malloc(st->a85size)) == NULL)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to allocate ASCII85Decode buffer.");
|
||||
goto error;
|
||||
}
|
||||
|
||||
st->a85bufptr = st->a85buffer;
|
||||
st->a85bufend = st->a85buffer;
|
||||
|
||||
// Get the second filter...
|
||||
filter = pdfioArrayGetName(fa, 1);
|
||||
}
|
||||
// Support single-valued arrays...
|
||||
filter = pdfioArrayGetName(fa, 0);
|
||||
}
|
||||
|
||||
if (!filter)
|
||||
@@ -519,6 +490,7 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
// No single filter name, do we have a compound filter?
|
||||
if (fa)
|
||||
{
|
||||
// TODO: Implement compound filters...
|
||||
_pdfioFileError(st->pdf, "Unsupported compound stream filter.");
|
||||
goto error;
|
||||
}
|
||||
@@ -526,9 +498,9 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
// No filter, read as-is...
|
||||
st->filter = PDFIO_FILTER_NONE;
|
||||
}
|
||||
else if (!strcmp(filter, "FlateDecode") || !strcmp(filter, "LZWDecode"))
|
||||
else if (!strcmp(filter, "FlateDecode"))
|
||||
{
|
||||
// Flate or LZW compression
|
||||
// Flate compression
|
||||
pdfio_dict_t *params = pdfioDictGetDict(dict, "DecodeParms");
|
||||
// Decoding parameters
|
||||
int bpc = (int)pdfioDictGetNumber(params, "BitsPerComponent");
|
||||
@@ -539,11 +511,12 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
// Number of columns
|
||||
int predictor = (int)pdfioDictGetNumber(params, "Predictor");
|
||||
// Predictory value, if any
|
||||
int status; // ZLIB status
|
||||
ssize_t rbytes; // Bytes read
|
||||
|
||||
PDFIO_DEBUG("_pdfioStreamOpen: %s - BitsPerComponent=%d, Colors=%d, Columns=%d, Predictor=%d\n", filter, bpc, colors, columns, predictor);
|
||||
PDFIO_DEBUG("_pdfioStreamOpen: FlateDecode - BitsPerComponent=%d, Colors=%d, Columns=%d, Predictor=%d\n", bpc, colors, columns, predictor);
|
||||
|
||||
st->filter = !strcmp(filter, "FlateDecode") ? PDFIO_FILTER_FLATE : PDFIO_FILTER_LZW;
|
||||
st->filter = PDFIO_FILTER_FLATE;
|
||||
|
||||
if (bpc == 0)
|
||||
{
|
||||
@@ -610,57 +583,42 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
st->cbsize = 4096;
|
||||
if ((st->cbuffer = malloc(st->cbsize)) == NULL)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to allocate %lu bytes for FlateDecode decompression buffer.", (unsigned long)st->cbsize);
|
||||
_pdfioFileError(st->pdf, "Unable to allocate %lu bytes for Flate compression buffer.", (unsigned long)st->cbsize);
|
||||
goto error;
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("_pdfioStreamOpen: pos=%ld\n", (long)_pdfioFileTell(st->pdf));
|
||||
if ((rbytes = stream_get_bytes(st, st->cbuffer, st->cbsize)) <= 0)
|
||||
if (st->cbsize > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->cbsize);
|
||||
|
||||
if (rbytes <= 0)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to read bytes for stream.");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (st->filter == PDFIO_FILTER_FLATE)
|
||||
if (st->crypto_cb)
|
||||
rbytes = (ssize_t)(st->crypto_cb)(&st->crypto_ctx, st->cbuffer, st->cbuffer, (size_t)rbytes);
|
||||
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
st->flate.avail_in = (uInt)rbytes;
|
||||
|
||||
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)
|
||||
{
|
||||
// Flate decompression...
|
||||
int status; // ZLIB status
|
||||
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
st->flate.avail_in = (uInt)rbytes;
|
||||
|
||||
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)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to start FlateDecode filter: %s", zstrerror(status));
|
||||
goto error;
|
||||
}
|
||||
_pdfioFileError(st->pdf, "Unable to start Flate filter: %s", zstrerror(status));
|
||||
goto error;
|
||||
}
|
||||
else
|
||||
{
|
||||
// LZW decompression...
|
||||
int early = 1;
|
||||
|
||||
if (pdfioDictGetType(params, "EarlyChange") == PDFIO_VALTYPE_NUMBER)
|
||||
{
|
||||
early = (int)pdfioDictGetNumber(params, "EarlyChange");
|
||||
if (early < 0 || early > 100)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Bad EarlyChange value %d for LZWDecode filter.", early);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
if ((st->lzw = _pdfioLZWCreate(/*code_size*/8, early, /*reversed*/false)) == NULL)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to initialize LZWDecode filter: %s", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
st->lzw->next_in = st->cbuffer;
|
||||
st->lzw->avail_in = (size_t)rbytes;
|
||||
}
|
||||
st->remaining -= st->flate.avail_in;
|
||||
}
|
||||
else if (!strcmp(filter, "LZWDecode"))
|
||||
{
|
||||
// LZW compression
|
||||
st->filter = PDFIO_FILTER_LZW;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -680,13 +638,12 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
||||
// If we get here something went wrong...
|
||||
error:
|
||||
|
||||
free(st->a85buffer);
|
||||
free(st->cbuffer);
|
||||
free(st->prbuffer);
|
||||
free(st->psbuffer);
|
||||
free(st);
|
||||
|
||||
return (NULL);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1054,271 +1011,6 @@ pdfioStreamWrite(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'stream_get_bytes()' - Read and decrypt raw or ASCII85Decode-encoded data.
|
||||
//
|
||||
|
||||
static ssize_t // O - Bytes read or `-1` on error
|
||||
stream_get_bytes(
|
||||
pdfio_stream_t *st, // I - Stream
|
||||
void *buffer, // I - Buffer
|
||||
size_t bytes) // I - Maximum number of bytes to read
|
||||
{
|
||||
ssize_t rbytes; // Bytes read
|
||||
|
||||
|
||||
if (st->a85buffer)
|
||||
{
|
||||
// Decode through the ASCII85Decode buffer...
|
||||
char *bufptr = (char *)buffer; // Pointer into read buffer
|
||||
|
||||
rbytes = 0;
|
||||
|
||||
// Read as much as we can...
|
||||
while (bytes > 0 && (st->a85bufptr < st->a85bufend || st->remaining > 0))
|
||||
{
|
||||
unsigned count, // Number of ASCII85 chars
|
||||
a85val; // ASCII85 "chunk" value
|
||||
char *a85bufptr; // Local copy of buffer pointer
|
||||
size_t declen; // Decoded length
|
||||
|
||||
|
||||
// First use up any remaining decoded chars...
|
||||
if (st->a85decptr)
|
||||
{
|
||||
declen = (size_t)(st->a85decend - st->a85decptr);
|
||||
|
||||
if (bytes >= declen)
|
||||
{
|
||||
memcpy(bufptr, st->a85decptr, declen);
|
||||
bufptr += declen;
|
||||
rbytes += (ssize_t)declen;
|
||||
st->a85decptr = NULL;
|
||||
bytes -= declen;
|
||||
|
||||
if (bytes == 0)
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(bufptr, st->a85decptr, bytes);
|
||||
rbytes += (ssize_t)bytes;
|
||||
st->a85decptr += bytes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((st->a85bufend - st->a85bufptr) < 5 && st->remaining > 0)
|
||||
{
|
||||
// Fill the ASCII85Decode buffer...
|
||||
ssize_t a85bytes = st->a85bufend - st->a85bufptr;
|
||||
// Bytes in the ASCII85Decode buffer
|
||||
size_t a85remaining = st->a85size - (size_t)a85bytes;
|
||||
// Remaining capacity in the buffer
|
||||
|
||||
// First move any remaining bytes to the front of the buffer...
|
||||
if (a85bytes > 0)
|
||||
memmove(st->a85buffer, st->a85bufptr, (size_t)a85bytes);
|
||||
|
||||
st->a85bufptr = st->a85buffer;
|
||||
st->a85bufend = st->a85buffer + a85bytes;
|
||||
|
||||
// Then read more data from the file...
|
||||
if (a85remaining > st->remaining)
|
||||
a85bytes = _pdfioFileRead(st->pdf, st->a85bufend, st->remaining);
|
||||
else
|
||||
a85bytes = _pdfioFileRead(st->pdf, st->a85bufend, a85remaining);
|
||||
|
||||
if (a85bytes > 0)
|
||||
{
|
||||
st->remaining -= (size_t)a85bytes;
|
||||
|
||||
if (st->crypto_cb)
|
||||
(st->crypto_cb)(&st->crypto_ctx, (uint8_t *)st->a85bufend, (uint8_t *)st->a85bufend, (size_t)a85bytes);
|
||||
|
||||
st->a85bufend += a85bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Read error in stream.");
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the next chunk...
|
||||
for (a85bufptr = st->a85bufptr, a85val = 0, count = 0; a85bufptr < st->a85bufend && count < 5; a85bufptr ++)
|
||||
{
|
||||
char a85ch = *a85bufptr; // Current character
|
||||
|
||||
if (a85ch >= '!' && a85ch <= 'u')
|
||||
{
|
||||
// Valid ASCII85Decode character...
|
||||
a85val = a85val * 85 + a85ch - '!';
|
||||
count ++;
|
||||
}
|
||||
else if (a85ch == 'z' && count == 0)
|
||||
{
|
||||
// 'z' == 0's
|
||||
a85val = 0;
|
||||
count = 5;
|
||||
|
||||
a85bufptr++;
|
||||
}
|
||||
else if (a85ch == '~')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (!isspace(a85ch & 255))
|
||||
{
|
||||
// Invalid ASCII85Decode character...
|
||||
_pdfioFileError(st->pdf, "Invalid ASCII85Decode character '%c' in stream.", a85ch);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
st->a85bufptr = a85bufptr;
|
||||
|
||||
if (*a85bufptr == '~')
|
||||
break;
|
||||
|
||||
if (count < 2)
|
||||
{
|
||||
// Need at least 2 characters to decode a single byte...
|
||||
_pdfioFileError(st->pdf, "Invalid ASCII85Decode sequence in stream.");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
declen = count - 1;
|
||||
|
||||
// Add rounds to properly align the decoded value...
|
||||
while (count < 5)
|
||||
{
|
||||
a85val = a85val * 85 + 84;
|
||||
count ++;
|
||||
}
|
||||
|
||||
// Copy the bytes to the decode buffer...
|
||||
st->a85decode[0] = (char)(a85val >> 24);
|
||||
st->a85decode[1] = (char)((a85val >> 16) & 255);
|
||||
st->a85decode[2] = (char)((a85val >> 8) & 255);
|
||||
st->a85decode[3] = (char)(a85val & 255);
|
||||
|
||||
st->a85decptr = st->a85decode;
|
||||
st->a85decend = st->a85decode + declen;
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("stream_get_bytes: Returning %ld ASCII85 bytes for stream.\n", (long)rbytes);
|
||||
|
||||
return (rbytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Limit reads to the length of the stream...
|
||||
if (bytes > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, buffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, buffer, bytes);
|
||||
|
||||
if (rbytes > 0)
|
||||
{
|
||||
st->remaining -= (size_t)rbytes;
|
||||
|
||||
if (st->crypto_cb)
|
||||
(st->crypto_cb)(&st->crypto_ctx, (uint8_t *)buffer, (uint8_t *)buffer, (size_t)rbytes);
|
||||
}
|
||||
|
||||
PDFIO_DEBUG("stream_get_bytes: Returning %ld raw bytes for stream.\n", (long)rbytes);
|
||||
|
||||
return (rbytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'stream_inflate()' - Decompress bytes from a stream (Flate or LZW) into the specified buffer.
|
||||
//
|
||||
|
||||
static ssize_t
|
||||
stream_inflate(pdfio_stream_t *st, // I - Stream
|
||||
uint8_t *buffer, // I - Output buffer
|
||||
size_t bytes, // I - Number of bytes
|
||||
bool exactly) // I - Require exactly the number of bytes
|
||||
{
|
||||
ssize_t rbytes; // Bytes read
|
||||
|
||||
|
||||
// Setup decompression to the output buffer...
|
||||
if (st->filter == PDFIO_FILTER_FLATE)
|
||||
{
|
||||
st->flate.next_out = (Bytef *)buffer;
|
||||
st->flate.avail_out = (uInt)bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
st->lzw->next_out = buffer;
|
||||
st->lzw->avail_out = bytes;
|
||||
}
|
||||
|
||||
// Loop to get the bytes...
|
||||
do
|
||||
{
|
||||
if (st->filter == PDFIO_FILTER_FLATE)
|
||||
{
|
||||
// Flate decompress
|
||||
int status; // Status of decompression
|
||||
|
||||
PDFIO_DEBUG("stream_inflate: avail_in=%u, avail_out=%u\n", st->flate.avail_in, st->flate.avail_out);
|
||||
|
||||
if (st->flate.avail_in == 0)
|
||||
{
|
||||
// Read more from the file...
|
||||
if ((rbytes = stream_get_bytes(st, st->cbuffer, st->cbsize)) <= 0)
|
||||
return (-1); // End of file...
|
||||
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
st->flate.avail_in = (uInt)rbytes;
|
||||
}
|
||||
|
||||
if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
|
||||
{
|
||||
PDFIO_DEBUG("stream_inflate: inflate() returned %d\n", status);
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
bytes = (size_t)st->flate.avail_out;
|
||||
}
|
||||
else
|
||||
{
|
||||
// LZW decompress
|
||||
if (st->lzw->avail_in == 0)
|
||||
{
|
||||
// Read more from the file...
|
||||
if ((rbytes = stream_get_bytes(st, st->cbuffer, st->cbsize)) <= 0)
|
||||
return (-1); // End of file...
|
||||
|
||||
st->lzw->next_in = st->cbuffer;
|
||||
st->lzw->avail_in = (size_t)rbytes;
|
||||
}
|
||||
|
||||
if (!_pdfioLZWInflate(st->lzw) && !st->lzw->saw_eod)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, st->lzw->error);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
bytes = st->lzw->avail_out;
|
||||
}
|
||||
}
|
||||
while (bytes > 0 && exactly);
|
||||
|
||||
if (st->filter == PDFIO_FILTER_FLATE)
|
||||
return (st->flate.next_out - (Bytef *)buffer);
|
||||
else
|
||||
return (st->lzw->next_out - (uint8_t *)buffer);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'stream_paeth()' - PaethPredictor function for PNG decompression filter.
|
||||
//
|
||||
@@ -1346,20 +1038,67 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
char *buffer, // I - Buffer
|
||||
size_t bytes) // I - Number of bytes to read
|
||||
{
|
||||
ssize_t rbytes; // Bytes read
|
||||
uInt avail_in, avail_out; // Previous flate values
|
||||
|
||||
|
||||
if (st->filter == PDFIO_FILTER_NONE)
|
||||
{
|
||||
// No filtering...
|
||||
return (stream_get_bytes(st, buffer, bytes));
|
||||
// No filtering, but limit reads to the length of the stream...
|
||||
if (bytes > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, buffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, buffer, bytes);
|
||||
|
||||
if (rbytes > 0)
|
||||
{
|
||||
st->remaining -= (size_t)rbytes;
|
||||
|
||||
if (st->crypto_cb)
|
||||
(st->crypto_cb)(&st->crypto_ctx, (uint8_t *)buffer, (uint8_t *)buffer, (size_t)rbytes);
|
||||
}
|
||||
|
||||
return (rbytes);
|
||||
}
|
||||
else if (st->filter == PDFIO_FILTER_FLATE || st->filter == PDFIO_FILTER_LZW)
|
||||
else if (st->filter == PDFIO_FILTER_FLATE)
|
||||
{
|
||||
// Flate or LZW compression...
|
||||
// Deflate compression...
|
||||
int status; // Status of decompression
|
||||
|
||||
if (st->predictor == _PDFIO_PREDICTOR_NONE)
|
||||
{
|
||||
// Decompress into the buffer...
|
||||
PDFIO_DEBUG("stream_read: No predictor.\n");
|
||||
|
||||
return (stream_inflate(st, (uint8_t *)buffer, bytes, /*exactly*/false));
|
||||
if (st->flate.avail_in == 0)
|
||||
{
|
||||
// Read more from the file...
|
||||
if (st->cbsize > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->cbsize);
|
||||
|
||||
if (rbytes <= 0)
|
||||
return (-1); // End of file...
|
||||
|
||||
if (st->crypto_cb)
|
||||
rbytes = (ssize_t)(st->crypto_cb)(&st->crypto_ctx, st->cbuffer, st->cbuffer, (size_t)rbytes);
|
||||
|
||||
st->remaining -= (size_t)rbytes;
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
st->flate.avail_in = (uInt)rbytes;
|
||||
}
|
||||
|
||||
st->flate.next_out = (Bytef *)buffer;
|
||||
st->flate.avail_out = (uInt)bytes;
|
||||
|
||||
if ((status = inflate(&(st->flate), Z_NO_FLUSH)) < Z_OK)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status));
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (st->flate.next_out - (Bytef *)buffer);
|
||||
}
|
||||
else if (st->predictor == _PDFIO_PREDICTOR_TIFF2)
|
||||
{
|
||||
@@ -1367,9 +1106,9 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
// Size of pixel in bytes
|
||||
remaining = st->pbsize;
|
||||
// Remaining bytes
|
||||
uint8_t *bufptr = (uint8_t *)buffer,
|
||||
unsigned char *bufptr = (unsigned char *)buffer,
|
||||
// Pointer into buffer
|
||||
*bufsecond = (uint8_t *)buffer + pbpixel,
|
||||
*bufsecond = (unsigned char *)buffer + pbpixel,
|
||||
// Pointer to second pixel in buffer
|
||||
*sptr = st->psbuffer;
|
||||
// Current (raw) line
|
||||
@@ -1382,7 +1121,43 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (stream_inflate(st, sptr, st->pbsize, /*exactly*/true) < 0)
|
||||
st->flate.next_out = (Bytef *)sptr;
|
||||
st->flate.avail_out = (uInt)st->pbsize;
|
||||
|
||||
while (st->flate.avail_out > 0)
|
||||
{
|
||||
if (st->flate.avail_in == 0)
|
||||
{
|
||||
// Read more from the file...
|
||||
if (st->cbsize > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->cbsize);
|
||||
|
||||
if (rbytes <= 0)
|
||||
return (-1); // End of file...
|
||||
|
||||
if (st->crypto_cb)
|
||||
rbytes = (ssize_t)(st->crypto_cb)(&st->crypto_ctx, st->cbuffer, st->cbuffer, (size_t)rbytes);
|
||||
|
||||
st->remaining -= (size_t)rbytes;
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
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)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status));
|
||||
return (-1);
|
||||
}
|
||||
else if (status == Z_STREAM_END || (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out))
|
||||
break;
|
||||
}
|
||||
|
||||
if (st->flate.avail_out > 0)
|
||||
return (-1); // Early end of stream
|
||||
|
||||
for (; bufptr < bufsecond; remaining --, sptr ++)
|
||||
@@ -1399,9 +1174,9 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
// Size of pixel in bytes
|
||||
remaining = st->pbsize - 1;
|
||||
// Remaining bytes
|
||||
uint8_t *bufptr = (uint8_t *)buffer,
|
||||
unsigned char *bufptr = (unsigned char *)buffer,
|
||||
// Pointer into buffer
|
||||
*bufsecond = (uint8_t *)buffer + pbpixel,
|
||||
*bufsecond = (unsigned char *)buffer + pbpixel,
|
||||
// Pointer to second pixel in buffer
|
||||
*sptr = st->psbuffer + 1,
|
||||
// Current (raw) line
|
||||
@@ -1416,10 +1191,46 @@ stream_read(pdfio_stream_t *st, // I - Stream
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (stream_inflate(st, sptr - 1, st->pbsize, /*exactly*/true) < 0)
|
||||
st->flate.next_out = (Bytef *)sptr - 1;
|
||||
st->flate.avail_out = (uInt)st->pbsize;
|
||||
|
||||
while (st->flate.avail_out > 0)
|
||||
{
|
||||
if (st->flate.avail_in == 0)
|
||||
{
|
||||
// Read more from the file...
|
||||
if (st->cbsize > st->remaining)
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->remaining);
|
||||
else
|
||||
rbytes = _pdfioFileRead(st->pdf, st->cbuffer, st->cbsize);
|
||||
|
||||
if (rbytes <= 0)
|
||||
return (-1); // End of file...
|
||||
|
||||
if (st->crypto_cb)
|
||||
rbytes = (ssize_t)(st->crypto_cb)(&st->crypto_ctx, st->cbuffer, st->cbuffer, (size_t)rbytes);
|
||||
|
||||
st->remaining -= (size_t)rbytes;
|
||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||
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)
|
||||
{
|
||||
_pdfioFileError(st->pdf, "Unable to decompress stream data for object %ld: %s", (long)st->obj->number, zstrerror(status));
|
||||
return (-1);
|
||||
}
|
||||
else if (status == Z_STREAM_END || (avail_in == st->flate.avail_in && avail_out == st->flate.avail_out))
|
||||
break;
|
||||
}
|
||||
|
||||
if (st->flate.avail_out > 0)
|
||||
{
|
||||
// Early end of stream
|
||||
PDFIO_DEBUG("stream_read: Early EOF (remaining=%u).\n", (unsigned)st->remaining);
|
||||
PDFIO_DEBUG("stream_read: Early EOF (remaining=%u, avail_in=%d, avail_out=%d, data_type=%d, next_in=<%02X%02X%02X%02X...>).\n", (unsigned)st->remaining, st->flate.avail_in, st->flate.avail_out, st->flate.data_type, st->flate.next_in[0], st->flate.next_in[1], st->flate.next_in[2], st->flate.next_in[3]);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
@@ -1522,6 +1333,8 @@ stream_write(pdfio_stream_t *st, // I - Stream
|
||||
outbytes = cbytes;
|
||||
}
|
||||
|
||||
// fprintf(stderr, "stream_write: bytes=%u, outbytes=%u\n", (unsigned)bytes, (unsigned)outbytes);
|
||||
|
||||
if (!_pdfioFileWrite(st->pdf, st->cbuffer, outbytes))
|
||||
return (false);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF string functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -674,8 +674,7 @@ _pdfio_vsnprintf(pdfio_file_t *pdf, // I - PDF file
|
||||
|
||||
char * // O - Buffer or `NULL` on error
|
||||
_pdfioStringAllocBuffer(
|
||||
pdfio_file_t *pdf, // I - PDF file
|
||||
_pdfio_strbuf_t **bptr) // O - String buffer pointer
|
||||
pdfio_file_t *pdf) // I - PDF file
|
||||
{
|
||||
_pdfio_strbuf_t *current; // Current string buffer
|
||||
|
||||
@@ -684,34 +683,21 @@ _pdfioStringAllocBuffer(
|
||||
for (current = pdf->strbuffers; current; current = current->next)
|
||||
{
|
||||
if (!current->bufused)
|
||||
goto done;
|
||||
{
|
||||
current->bufused = true;
|
||||
return (current->buffer);
|
||||
}
|
||||
}
|
||||
|
||||
// Didn't find one, allocate a new one...
|
||||
if ((current = calloc(1, sizeof(_pdfio_strbuf_t))) == NULL)
|
||||
{
|
||||
if (bptr)
|
||||
*bptr = NULL;
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
// Add to the linked list of string buffers...
|
||||
current->pdf = pdf;
|
||||
current->next = pdf->strbuffers;
|
||||
pdf->strbuffers = current;
|
||||
|
||||
// Claim and return the free string buffer...
|
||||
done:
|
||||
|
||||
current->next = pdf->strbuffers;
|
||||
current->bufused = true;
|
||||
|
||||
if (bptr)
|
||||
{
|
||||
*bptr = current;
|
||||
current->buffer[0] = '\0';
|
||||
current->bufptr = current->buffer;
|
||||
}
|
||||
pdf->strbuffers = current;
|
||||
|
||||
return (current->buffer);
|
||||
}
|
||||
@@ -871,36 +857,6 @@ _pdfioStringIsAllocated(
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// '_pdfioStringPrintf()' - Append a formatted string to a string buffer.
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` on failure
|
||||
_pdfioStringPrintf(
|
||||
_pdfio_strbuf_t *bptr, // I - String buffer
|
||||
const char *format, // I - Format string
|
||||
...) // I - Additional arguments as needed
|
||||
{
|
||||
va_list ap; // Pointer to additional arguments
|
||||
size_t remaining; // Remaining bytes
|
||||
ssize_t bytes; // Formatted bytes
|
||||
|
||||
|
||||
// Format the string in the buffer...
|
||||
va_start(ap, format);
|
||||
|
||||
remaining = sizeof(bptr->buffer) - (size_t)(bptr->bufptr - bptr->buffer);
|
||||
bytes = _pdfio_vsnprintf(bptr->pdf, bptr->bufptr, remaining, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
// Advance the current position in the buffer and return.
|
||||
bptr->bufptr += strlen(bptr->bufptr);
|
||||
|
||||
return (bytes < (ssize_t)remaining && bytes >= 0);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// 'find_string()' - Find an element in the array.
|
||||
//
|
||||
|
||||
@@ -397,7 +397,7 @@ _pdfioTokenRead(_pdfio_token_t *tb, // I - Token buffer/stack
|
||||
{
|
||||
// UTF-16 string, convert to UTF-8...
|
||||
PDFIO_DEBUG("_pdfioTokenRead: Converting string to UTF-8.\n", stderr);
|
||||
_pdfio_utf16cpy(buffer + 1, (unsigned char *)buffer + 1, (size_t)(bufptr - buffer - 1), bufsize - 1);
|
||||
_pdfio_utf16cpy(buffer + 1, (unsigned char *)buffer + 1, bufptr - buffer - 1, bufsize - 1);
|
||||
|
||||
PDFIO_DEBUG("_pdfioTokenRead: Read '%s'.\n", buffer);
|
||||
return (true);
|
||||
|
||||
111
pdfio-value.c
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// PDF value functions for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -76,8 +76,7 @@ _pdfioValueCopy(pdfio_file_t *pdfdst, // I - Destination PDF file
|
||||
return (NULL);
|
||||
|
||||
case PDFIO_VALTYPE_ARRAY :
|
||||
if ((vdst->value.array = pdfioArrayCopy(pdfdst, vsrc->value.array)) == NULL)
|
||||
return (NULL);
|
||||
vdst->value.array = pdfioArrayCopy(pdfdst, vsrc->value.array);
|
||||
break;
|
||||
|
||||
case PDFIO_VALTYPE_BINARY :
|
||||
@@ -98,14 +97,12 @@ _pdfioValueCopy(pdfio_file_t *pdfdst, // I - Destination PDF file
|
||||
return (vdst);
|
||||
|
||||
case PDFIO_VALTYPE_DICT :
|
||||
if ((vdst->value.dict = pdfioDictCopy(pdfdst, vsrc->value.dict)) == NULL)
|
||||
return (NULL);
|
||||
vdst->value.dict = pdfioDictCopy(pdfdst, vsrc->value.dict);
|
||||
break;
|
||||
|
||||
case PDFIO_VALTYPE_NAME :
|
||||
case PDFIO_VALTYPE_STRING :
|
||||
if ((vdst->value.name = pdfioStringCreate(pdfdst, vsrc->value.name)) == NULL)
|
||||
return (NULL);
|
||||
vdst->value.name = pdfioStringCreate(pdfdst, vsrc->value.name);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -160,7 +157,7 @@ _pdfioValueDecrypt(pdfio_file_t *pdf, // I - PDF file
|
||||
_pdfioFileError(pdf, "Unable to read encrypted binary string - too long.");
|
||||
return (false);
|
||||
}
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf, /*bptr*/NULL)) == NULL)
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read encrypted binary string - out of memory.");
|
||||
return (false);
|
||||
@@ -191,7 +188,7 @@ _pdfioValueDecrypt(pdfio_file_t *pdf, // I - PDF file
|
||||
_pdfioFileError(pdf, "Unable to read encrypted string - too long.");
|
||||
return (false);
|
||||
}
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf, /*bptr*/NULL)) == NULL)
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(pdf, "Unable to read encrypted binary string - out of memory.");
|
||||
return (false);
|
||||
@@ -341,7 +338,7 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
|
||||
size_t depth) // I - Depth of value
|
||||
{
|
||||
_pdfio_value_t *ret = NULL; // Return value
|
||||
char *token = _pdfioStringAllocBuffer(pdf, /*bptr*/NULL);
|
||||
char *token = _pdfioStringAllocBuffer(pdf);
|
||||
// Token buffer
|
||||
time_t timeval; // Date/time value
|
||||
#ifdef DEBUG
|
||||
@@ -603,12 +600,10 @@ _pdfioValueRead(pdfio_file_t *pdf, // I - PDF file
|
||||
//
|
||||
|
||||
bool // O - `true` on success, `false` on failure
|
||||
_pdfioValueWrite(
|
||||
_pdfio_printf_t cb, // I - Printf callback function
|
||||
void *cbdata, // I - Printf callback data
|
||||
pdfio_obj_t *obj, // I - Object, if any
|
||||
_pdfio_value_t *v, // I - Value
|
||||
off_t *length) // O - Offset to /Length value, if any
|
||||
_pdfioValueWrite(pdfio_file_t *pdf, // I - PDF file
|
||||
pdfio_obj_t *obj, // I - Object, if any
|
||||
_pdfio_value_t *v, // I - Value
|
||||
off_t *length)// O - Offset to /Length value, if any
|
||||
{
|
||||
switch (v->type)
|
||||
{
|
||||
@@ -616,7 +611,7 @@ _pdfioValueWrite(
|
||||
return (false);
|
||||
|
||||
case PDFIO_VALTYPE_ARRAY :
|
||||
return (_pdfioArrayWrite(cb, cbdata, obj, v->value.array));
|
||||
return (_pdfioArrayWrite(v->value.array, obj));
|
||||
|
||||
case PDFIO_VALTYPE_BINARY :
|
||||
{
|
||||
@@ -626,26 +621,26 @@ _pdfioValueWrite(
|
||||
bool ret = false; // Return value
|
||||
|
||||
|
||||
if (obj && obj->pdf->encryption)
|
||||
if (obj && pdf->encryption)
|
||||
{
|
||||
// Write encrypted string...
|
||||
_pdfio_crypto_ctx_t ctx; // Encryption context
|
||||
_pdfio_crypto_cb_t ccb; // Encryption callback
|
||||
_pdfio_crypto_cb_t cb; // Encryption callback
|
||||
size_t ivlen; // Number of initialization vector bytes
|
||||
|
||||
if (v->value.binary.datalen > PDFIO_MAX_STRING)
|
||||
{
|
||||
_pdfioFileError(obj->pdf, "Unable to write encrypted binary string - too long.");
|
||||
_pdfioFileError(pdf, "Unable to write encrypted binary string - too long.");
|
||||
return (false);
|
||||
}
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(obj->pdf, /*bptr*/NULL)) == NULL)
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(obj->pdf, "Unable to write encrypted binary string - out of memory.");
|
||||
_pdfioFileError(pdf, "Unable to write encrypted binary string - out of memory.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
ccb = _pdfioCryptoMakeWriter(obj->pdf, obj, &ctx, temp, &ivlen);
|
||||
databytes = (ccb)(&ctx, temp + ivlen, v->value.binary.data, v->value.binary.datalen) + ivlen;
|
||||
cb = _pdfioCryptoMakeWriter(pdf, obj, &ctx, temp, &ivlen);
|
||||
databytes = (cb)(&ctx, temp + ivlen, v->value.binary.data, v->value.binary.datalen) + ivlen;
|
||||
dataptr = temp;
|
||||
}
|
||||
else
|
||||
@@ -654,33 +649,33 @@ _pdfioValueWrite(
|
||||
databytes = v->value.binary.datalen;
|
||||
}
|
||||
|
||||
if (!(cb)(cbdata, "<"))
|
||||
if (!_pdfioFilePuts(pdf, "<"))
|
||||
goto bindone;
|
||||
|
||||
for (; databytes > 1; databytes -= 2, dataptr += 2)
|
||||
{
|
||||
if (!(cb)(cbdata, "%02X%02X", dataptr[0], dataptr[1]))
|
||||
if (!_pdfioFilePrintf(pdf, "%02X%02X", dataptr[0], dataptr[1]))
|
||||
goto bindone;
|
||||
}
|
||||
|
||||
if (databytes > 0 && !(cb)(cbdata, "%02X", dataptr[0]))
|
||||
if (databytes > 0 && !_pdfioFilePrintf(pdf, "%02X", dataptr[0]))
|
||||
goto bindone;
|
||||
|
||||
ret = (cb)(cbdata, ">");
|
||||
ret = _pdfioFilePuts(pdf, ">");
|
||||
|
||||
bindone:
|
||||
|
||||
if (temp)
|
||||
_pdfioStringFreeBuffer(obj->pdf, (char *)temp);
|
||||
_pdfioStringFreeBuffer(pdf, (char *)temp);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
case PDFIO_VALTYPE_BOOLEAN :
|
||||
if (v->value.boolean)
|
||||
return ((cb)(cbdata, " true"));
|
||||
return (_pdfioFilePuts(pdf, " true"));
|
||||
else
|
||||
return ((cb)(cbdata, " false"));
|
||||
return (_pdfioFilePuts(pdf, " false"));
|
||||
|
||||
case PDFIO_VALTYPE_DATE :
|
||||
{
|
||||
@@ -695,64 +690,64 @@ _pdfioValueWrite(
|
||||
|
||||
snprintf(datestr, sizeof(datestr), "D:%04d%02d%02d%02d%02d%02dZ", date.tm_year + 1900, date.tm_mon + 1, date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
|
||||
|
||||
if (obj && obj->pdf->encryption)
|
||||
if (obj && pdf->encryption)
|
||||
{
|
||||
// Write encrypted string...
|
||||
uint8_t temp[64], // Encrypted bytes
|
||||
*tempptr; // Pointer into encrypted bytes
|
||||
_pdfio_crypto_ctx_t ctx; // Encryption context
|
||||
_pdfio_crypto_cb_t ccb; // Encryption callback
|
||||
_pdfio_crypto_cb_t cb; // Encryption callback
|
||||
size_t len = strlen(datestr),
|
||||
// Length of value
|
||||
ivlen, // Number of initialization vector bytes
|
||||
tempbytes; // Number of output bytes
|
||||
|
||||
ccb = _pdfioCryptoMakeWriter(obj->pdf, obj, &ctx, temp, &ivlen);
|
||||
tempbytes = (ccb)(&ctx, temp + ivlen, (const uint8_t *)datestr, len) + ivlen;
|
||||
cb = _pdfioCryptoMakeWriter(pdf, obj, &ctx, temp, &ivlen);
|
||||
tempbytes = (cb)(&ctx, temp + ivlen, (const uint8_t *)datestr, len) + ivlen;
|
||||
|
||||
if (!(cb)(cbdata, "<"))
|
||||
if (!_pdfioFilePuts(pdf, "<"))
|
||||
return (false);
|
||||
|
||||
for (tempptr = temp; tempbytes > 1; tempbytes -= 2, tempptr += 2)
|
||||
{
|
||||
if (!(cb)(cbdata, "%02X%02X", tempptr[0], tempptr[1]))
|
||||
if (!_pdfioFilePrintf(pdf, "%02X%02X", tempptr[0], tempptr[1]))
|
||||
return (false);
|
||||
}
|
||||
|
||||
if (tempbytes > 0)
|
||||
return ((cb)(cbdata, "%02X>", *tempptr));
|
||||
return (_pdfioFilePrintf(pdf, "%02X>", *tempptr));
|
||||
else
|
||||
return ((cb)(cbdata, ">"));
|
||||
return (_pdfioFilePuts(pdf, ">"));
|
||||
}
|
||||
else
|
||||
{
|
||||
return ((cb)(cbdata, "%S", datestr));
|
||||
return (_pdfioFilePrintf(pdf, "%S", datestr));
|
||||
}
|
||||
}
|
||||
|
||||
case PDFIO_VALTYPE_DICT :
|
||||
return (_pdfioDictWrite(cb, cbdata, obj, v->value.dict, length));
|
||||
return (_pdfioDictWrite(v->value.dict, obj, length));
|
||||
|
||||
case PDFIO_VALTYPE_INDIRECT :
|
||||
return ((cb)(cbdata, " %lu %u R", (unsigned long)v->value.indirect.number, v->value.indirect.generation));
|
||||
return (_pdfioFilePrintf(pdf, " %lu %u R", (unsigned long)v->value.indirect.number, v->value.indirect.generation));
|
||||
|
||||
case PDFIO_VALTYPE_NAME :
|
||||
return ((cb)(cbdata, "%N", v->value.name));
|
||||
return (_pdfioFilePrintf(pdf, "%N", v->value.name));
|
||||
|
||||
case PDFIO_VALTYPE_NULL :
|
||||
return ((cb)(cbdata, " null"));
|
||||
return (_pdfioFilePuts(pdf, " null"));
|
||||
|
||||
case PDFIO_VALTYPE_NUMBER :
|
||||
return ((cb)(cbdata, " %.6f", v->value.number));
|
||||
return (_pdfioFilePrintf(pdf, " %.6f", v->value.number));
|
||||
|
||||
case PDFIO_VALTYPE_STRING :
|
||||
if (obj && obj->pdf->encryption)
|
||||
if (obj && pdf->encryption)
|
||||
{
|
||||
// Write encrypted string...
|
||||
uint8_t *temp = NULL, // Encrypted bytes
|
||||
*tempptr; // Pointer into encrypted bytes
|
||||
_pdfio_crypto_ctx_t ctx; // Encryption context
|
||||
_pdfio_crypto_cb_t ccb; // Encryption callback
|
||||
_pdfio_crypto_cb_t cb; // Encryption callback
|
||||
size_t len = strlen(v->value.string),
|
||||
// Length of value
|
||||
ivlen, // Number of initialization vector bytes
|
||||
@@ -761,42 +756,42 @@ _pdfioValueWrite(
|
||||
|
||||
if (len > PDFIO_MAX_STRING)
|
||||
{
|
||||
_pdfioFileError(obj->pdf, "Unable to write encrypted string - too long.");
|
||||
_pdfioFileError(pdf, "Unable to write encrypted string - too long.");
|
||||
return (false);
|
||||
}
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(obj->pdf, /*bptr*/NULL)) == NULL)
|
||||
else if ((temp = (uint8_t *)_pdfioStringAllocBuffer(pdf)) == NULL)
|
||||
{
|
||||
_pdfioFileError(obj->pdf, "Unable to write encrypted string - out of memory.");
|
||||
_pdfioFileError(pdf, "Unable to write encrypted string - out of memory.");
|
||||
return (false);
|
||||
}
|
||||
|
||||
ccb = _pdfioCryptoMakeWriter(obj->pdf, obj, &ctx, temp, &ivlen);
|
||||
tempbytes = (ccb)(&ctx, temp + ivlen, (const uint8_t *)v->value.string, len) + ivlen;
|
||||
cb = _pdfioCryptoMakeWriter(pdf, obj, &ctx, temp, &ivlen);
|
||||
tempbytes = (cb)(&ctx, temp + ivlen, (const uint8_t *)v->value.string, len) + ivlen;
|
||||
|
||||
if (!(cb)(cbdata, "<"))
|
||||
if (!_pdfioFilePuts(pdf, "<"))
|
||||
goto strdone;
|
||||
|
||||
for (tempptr = temp; tempbytes > 1; tempbytes -= 2, tempptr += 2)
|
||||
{
|
||||
if (!(cb)(cbdata, "%02X%02X", tempptr[0], tempptr[1]))
|
||||
if (!_pdfioFilePrintf(pdf, "%02X%02X", tempptr[0], tempptr[1]))
|
||||
goto strdone;
|
||||
}
|
||||
|
||||
if (tempbytes > 0 && !(cb)(cbdata, "%02X", *tempptr))
|
||||
if (tempbytes > 0 && !_pdfioFilePrintf(pdf, "%02X", *tempptr))
|
||||
goto strdone;
|
||||
|
||||
ret = (cb)(cbdata, ">");
|
||||
ret = _pdfioFilePuts(pdf, ">");
|
||||
|
||||
strdone :
|
||||
|
||||
_pdfioStringFreeBuffer(obj->pdf, (char *)temp);
|
||||
_pdfioStringFreeBuffer(pdf, (char *)temp);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Write unencrypted string...
|
||||
return ((cb)(cbdata, "%S", v->value.string));
|
||||
return (_pdfioFilePrintf(pdf, "%S", v->value.string));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
pdfio.h
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Public header file for PDFio.
|
||||
//
|
||||
// Copyright © 2021-2026 by Michael R Sweet.
|
||||
// Copyright © 2021-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
@@ -23,9 +23,9 @@ extern "C" {
|
||||
// Version numbers...
|
||||
//
|
||||
|
||||
# define PDFIO_VERSION "1.7.0"
|
||||
# define PDFIO_VERSION "1.6.1"
|
||||
# define PDFIO_VERSION_MAJOR 1
|
||||
# define PDFIO_VERSION_MINOR 7
|
||||
# define PDFIO_VERSION_MINOR 6
|
||||
|
||||
|
||||
//
|
||||
@@ -72,11 +72,11 @@ typedef enum pdfio_filter_e // Compression/decompression filters for streams
|
||||
PDFIO_FILTER_NONE, // No filter
|
||||
PDFIO_FILTER_ASCIIHEX, // ASCIIHexDecode filter (reading only)
|
||||
PDFIO_FILTER_ASCII85, // ASCII85Decode filter (reading only)
|
||||
PDFIO_FILTER_CCITTFAX, // CCITTFaxDecode filter (reading only)
|
||||
PDFIO_FILTER_CCITTFAX, // CCITTFaxDecode filter
|
||||
PDFIO_FILTER_CRYPT, // Encryption filter
|
||||
PDFIO_FILTER_DCT, // DCTDecode (JPEG) filter
|
||||
PDFIO_FILTER_FLATE, // FlateDecode filter
|
||||
PDFIO_FILTER_JBIG2, // JBIG2Decode filter (reading only)
|
||||
PDFIO_FILTER_JBIG2, // JBIG2Decode filter
|
||||
PDFIO_FILTER_JPX, // JPXDecode filter (reading only)
|
||||
PDFIO_FILTER_LZW, // LZWDecode filter (reading only)
|
||||
PDFIO_FILTER_RUNLENGTH, // RunLengthDecode filter (reading only)
|
||||
@@ -238,17 +238,7 @@ extern const char *pdfioObjGetType(pdfio_obj_t *obj) _PDFIO_PUBLIC;
|
||||
extern pdfio_stream_t *pdfioObjOpenStream(pdfio_obj_t *obj, bool decode) _PDFIO_PUBLIC;
|
||||
|
||||
extern bool pdfioPageCopy(pdfio_file_t *pdf, pdfio_obj_t *srcpage) _PDFIO_PUBLIC;
|
||||
extern pdfio_array_t *pdfioPageGetArray(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern unsigned char *pdfioPageGetBinary(pdfio_obj_t *page, const char *key, size_t *length) _PDFIO_PUBLIC;
|
||||
extern bool pdfioPageGetBoolean(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern time_t pdfioDictPageDate(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_dict_t *pdfioPageGetDict(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioPageGetName(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern size_t pdfioPageGetNumStreams(pdfio_obj_t *page) _PDFIO_PUBLIC;
|
||||
extern double pdfioPageGetNumber(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_obj_t *pdfioPageGetObj(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_rect_t *pdfioPageGetRect(pdfio_obj_t *page, const char *key, pdfio_rect_t *rect) _PDFIO_PUBLIC;
|
||||
extern const char *pdfioPageGetString(pdfio_obj_t *page, const char *key) _PDFIO_PUBLIC;
|
||||
extern pdfio_stream_t *pdfioPageOpenStream(pdfio_obj_t *page, size_t n, bool decode) _PDFIO_PUBLIC;
|
||||
|
||||
extern bool pdfioStreamClose(pdfio_stream_t *st) _PDFIO_PUBLIC;
|
||||
|
||||
@@ -87,7 +87,6 @@
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>ttf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
@@ -102,7 +101,6 @@
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>ttf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
@@ -117,7 +115,6 @@
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>ttf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>HAVE_LIBPNG;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
@@ -133,7 +130,6 @@
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>ttf;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>HAVE_LIBPNG;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
@@ -149,8 +145,7 @@
|
||||
<ClInclude Include="pdfio-content.h" />
|
||||
<ClInclude Include="pdfio-private.h" />
|
||||
<ClInclude Include="pdfio.h" />
|
||||
<ClInclude Include="ttf\ttf.h" />
|
||||
<ClInclude Include="ttf\ttf-private.h" />
|
||||
<ClInclude Include="ttf.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="pdfio-aes.c" />
|
||||
@@ -160,7 +155,6 @@
|
||||
<ClCompile Include="pdfio-crypto.c" />
|
||||
<ClCompile Include="pdfio-dict.c" />
|
||||
<ClCompile Include="pdfio-file.c" />
|
||||
<ClCompile Include="pdfio-lzw.c" />
|
||||
<ClCompile Include="pdfio-md5.c" />
|
||||
<ClCompile Include="pdfio-object.c" />
|
||||
<ClCompile Include="pdfio-page.c" />
|
||||
@@ -170,8 +164,7 @@
|
||||
<ClCompile Include="pdfio-string.c" />
|
||||
<ClCompile Include="pdfio-token.c" />
|
||||
<ClCompile Include="pdfio-value.c" />
|
||||
<ClCompile Include="ttf\ttf-cache.c" />
|
||||
<ClCompile Include="ttf\ttf-file.c" />
|
||||
<ClCompile Include="ttf.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -22,10 +22,8 @@
|
||||
273440CD263D727800FBFD63 /* pdfio-page.c in Sources */ = {isa = PBXBuildFile; fileRef = 273440C2263D727800FBFD63 /* pdfio-page.c */; };
|
||||
273440D8263D72E100FBFD63 /* testpdfio.c in Sources */ = {isa = PBXBuildFile; fileRef = 273440D7263D72E100FBFD63 /* testpdfio.c */; };
|
||||
273440E4263DD7EA00FBFD63 /* pdfio-token.c in Sources */ = {isa = PBXBuildFile; fileRef = 273440E3263DD7EA00FBFD63 /* pdfio-token.c */; };
|
||||
2741C9A22F05872C002D93F2 /* ttf-cache.c in Sources */ = {isa = PBXBuildFile; fileRef = 2741C99F2F05872C002D93F2 /* ttf-cache.c */; };
|
||||
2741C9A32F05872C002D93F2 /* ttf-file.c in Sources */ = {isa = PBXBuildFile; fileRef = 2741C9A02F05872C002D93F2 /* ttf-file.c */; };
|
||||
2741C9A42F05872C002D93F2 /* ttf-private.h in Headers */ = {isa = PBXBuildFile; fileRef = 2741C9A12F05872C002D93F2 /* ttf-private.h */; };
|
||||
2741C9A52F05872C002D93F2 /* ttf.h in Headers */ = {isa = PBXBuildFile; fileRef = 2741C99E2F05872C002D93F2 /* ttf.h */; };
|
||||
279E1035267D043B00D3A349 /* ttf.h in Headers */ = {isa = PBXBuildFile; fileRef = 279E1033267D043B00D3A349 /* ttf.h */; };
|
||||
279E1036267D043B00D3A349 /* ttf.c in Sources */ = {isa = PBXBuildFile; fileRef = 279E1034267D043B00D3A349 /* ttf.c */; };
|
||||
279E103B267D04E600D3A349 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 279E103A267D04E600D3A349 /* libz.tbd */; };
|
||||
27CF90442711DFFE00E50FE4 /* pdfio-aes.c in Sources */ = {isa = PBXBuildFile; fileRef = 27CF90432711DFFE00E50FE4 /* pdfio-aes.c */; };
|
||||
27ECBD8926419DAB0025312A /* libpdfio.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 273440B0263D6FE200FBFD63 /* libpdfio.a */; };
|
||||
@@ -84,10 +82,8 @@
|
||||
273440E1263D73A300FBFD63 /* pdfio.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = pdfio.html; path = doc/pdfio.html; sourceTree = "<group>"; };
|
||||
273440E2263D73A300FBFD63 /* pdfio.3 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = pdfio.3; path = doc/pdfio.3; sourceTree = "<group>"; };
|
||||
273440E3263DD7EA00FBFD63 /* pdfio-token.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "pdfio-token.c"; sourceTree = "<group>"; };
|
||||
2741C99E2F05872C002D93F2 /* ttf.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ttf.h; path = ttf/ttf.h; sourceTree = "<group>"; };
|
||||
2741C99F2F05872C002D93F2 /* ttf-cache.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = "ttf-cache.c"; path = "ttf/ttf-cache.c"; sourceTree = "<group>"; };
|
||||
2741C9A02F05872C002D93F2 /* ttf-file.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = "ttf-file.c"; path = "ttf/ttf-file.c"; sourceTree = "<group>"; };
|
||||
2741C9A12F05872C002D93F2 /* ttf-private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "ttf-private.h"; path = "ttf/ttf-private.h"; sourceTree = "<group>"; };
|
||||
279E1033267D043B00D3A349 /* ttf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ttf.h; sourceTree = "<group>"; };
|
||||
279E1034267D043B00D3A349 /* ttf.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ttf.c; sourceTree = "<group>"; };
|
||||
279E103A267D04E600D3A349 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
|
||||
27CF90432711DFFE00E50FE4 /* pdfio-aes.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "pdfio-aes.c"; sourceTree = "<group>"; };
|
||||
27F2F05D2710BE92008ECD36 /* pdfio-md5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "pdfio-md5.c"; sourceTree = "<group>"; };
|
||||
@@ -183,10 +179,8 @@
|
||||
273440B9263D727800FBFD63 /* pdfio-string.c */,
|
||||
273440E3263DD7EA00FBFD63 /* pdfio-token.c */,
|
||||
273440C0263D727800FBFD63 /* pdfio-value.c */,
|
||||
2741C99E2F05872C002D93F2 /* ttf.h */,
|
||||
2741C99F2F05872C002D93F2 /* ttf-cache.c */,
|
||||
2741C9A02F05872C002D93F2 /* ttf-file.c */,
|
||||
2741C9A12F05872C002D93F2 /* ttf-private.h */,
|
||||
279E1034267D043B00D3A349 /* ttf.c */,
|
||||
279E1033267D043B00D3A349 /* ttf.h */,
|
||||
);
|
||||
name = Library;
|
||||
sourceTree = "<group>";
|
||||
@@ -215,11 +209,10 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
27FCBDE42D19F9B300485EEE /* pdfio-base-font-widths.h in Headers */,
|
||||
2741C9A42F05872C002D93F2 /* ttf-private.h in Headers */,
|
||||
2741C9A52F05872C002D93F2 /* ttf.h in Headers */,
|
||||
273440CC263D727800FBFD63 /* pdfio.h in Headers */,
|
||||
271EA706265B2B1000ACDD39 /* pdfio-content.h in Headers */,
|
||||
273440C3263D727800FBFD63 /* pdfio-private.h in Headers */,
|
||||
279E1035267D043B00D3A349 /* ttf.h in Headers */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -302,6 +295,7 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
279E1036267D043B00D3A349 /* ttf.c in Sources */,
|
||||
273440C9263D727800FBFD63 /* pdfio-dict.c in Sources */,
|
||||
273440C8263D727800FBFD63 /* pdfio-file.c in Sources */,
|
||||
273440CB263D727800FBFD63 /* pdfio-value.c in Sources */,
|
||||
@@ -309,8 +303,6 @@
|
||||
273440CD263D727800FBFD63 /* pdfio-page.c in Sources */,
|
||||
27F2F0622710BE92008ECD36 /* pdfio-crypto.c in Sources */,
|
||||
27F2F0642711243D008ECD36 /* pdfio-sha256.c in Sources */,
|
||||
2741C9A22F05872C002D93F2 /* ttf-cache.c in Sources */,
|
||||
2741C9A32F05872C002D93F2 /* ttf-file.c in Sources */,
|
||||
273440C5263D727800FBFD63 /* pdfio-array.c in Sources */,
|
||||
273440E4263DD7EA00FBFD63 /* pdfio-token.c in Sources */,
|
||||
273440C7263D727800FBFD63 /* pdfio-object.c in Sources */,
|
||||
@@ -368,7 +360,7 @@
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_IMPLICIT_FALLTHROUGH = NO;
|
||||
CLANG_WARN_IMPLICIT_FALLTHROUGH = YES;
|
||||
CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
@@ -406,7 +398,7 @@
|
||||
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
|
||||
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = NO;
|
||||
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
|
||||
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
|
||||
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
@@ -468,7 +460,7 @@
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_IMPLICIT_FALLTHROUGH = NO;
|
||||
CLANG_WARN_IMPLICIT_FALLTHROUGH = YES;
|
||||
CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
@@ -505,7 +497,7 @@
|
||||
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
|
||||
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = NO;
|
||||
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
|
||||
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
|
||||
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
|
||||
15
pdfio1.def
@@ -1,5 +1,5 @@
|
||||
LIBRARY pdfio1
|
||||
VERSION 1.7
|
||||
VERSION 1.6
|
||||
EXPORTS
|
||||
_pdfio_strlcpy
|
||||
_pdfio_strtod
|
||||
@@ -51,9 +51,6 @@ _pdfioFileRead
|
||||
_pdfioFileSeek
|
||||
_pdfioFileTell
|
||||
_pdfioFileWrite
|
||||
_pdfioLZWCreate
|
||||
_pdfioLZWDelete
|
||||
_pdfioLZWInflate
|
||||
_pdfioObjDelete
|
||||
_pdfioObjGetExtension
|
||||
_pdfioObjLoad
|
||||
@@ -258,17 +255,7 @@ pdfioPageCopy
|
||||
pdfioPageDictAddColorSpace
|
||||
pdfioPageDictAddFont
|
||||
pdfioPageDictAddImage
|
||||
pdfioPageGetArray
|
||||
pdfioPageGetBinary
|
||||
pdfioPageGetBoolean
|
||||
pdfioPageGetDate
|
||||
pdfioPageGetDict
|
||||
pdfioPageGetName
|
||||
pdfioPageGetNumber
|
||||
pdfioPageGetNumStreams
|
||||
pdfioPageGetObj
|
||||
pdfioPageGetRect
|
||||
pdfioPageGetString
|
||||
pdfioPageOpenStream
|
||||
pdfioStreamClose
|
||||
pdfioStreamConsume
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<metadata>
|
||||
<id>pdfio_native</id>
|
||||
<title>PDFio Library for VS2019+</title>
|
||||
<version>1.7.0</version>
|
||||
<version>1.6.1</version>
|
||||
<authors>Michael R Sweet</authors>
|
||||
<owners>michaelrsweet</owners>
|
||||
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
|
||||
@@ -16,7 +16,7 @@
|
||||
<copyright>Copyright © 2019-2025 by Michael R Sweet</copyright>
|
||||
<tags>pdf file native</tags>
|
||||
<dependencies>
|
||||
<dependency id="pdfio_native.redist" version="1.7.0" />
|
||||
<dependency id="pdfio_native.redist" version="1.6.1" />
|
||||
<dependency id="libpng_native.redist" version="1.6.30" />
|
||||
<dependency id="zlib_native.redist" version="1.2.11" />
|
||||
</dependencies>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<metadata>
|
||||
<id>pdfio_native.redist</id>
|
||||
<title>PDFio Library for VS2019+</title>
|
||||
<version>1.7.0</version>
|
||||
<version>1.6.1</version>
|
||||
<authors>Michael R Sweet</authors>
|
||||
<owners>michaelrsweet</owners>
|
||||
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#
|
||||
# Script to test PDFio against a directory of PDF files.
|
||||
#
|
||||
# Copyright © 2025-2026 by Michael R Sweet.
|
||||
# Copyright © 2025 by Michael R Sweet.
|
||||
#
|
||||
# Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
# information.
|
||||
@@ -17,28 +17,14 @@ if test $# = 0; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
|
||||
ac_n=-n
|
||||
ac_c=
|
||||
else
|
||||
ac_n=
|
||||
ac_c='\c'
|
||||
fi
|
||||
|
||||
for file in $(find "$@" -name \*.pdf -print); do
|
||||
# Run testpdfio to test loading the file...
|
||||
echo $ac_n "\r$file: $ac_c"
|
||||
|
||||
./testpdfio --verbose $file >/dev/null 2>$file.log
|
||||
|
||||
./testpdfio $file >$file.log 2>&1
|
||||
if test $? = 0; then
|
||||
# Passed
|
||||
echo PASS
|
||||
rm -f $file.log
|
||||
else
|
||||
# Failed, preserve log and write to stdout...
|
||||
echo FAIL
|
||||
cat $file.log
|
||||
echo ""
|
||||
# Failed, preserve log and write filename to stdout...
|
||||
echo $file
|
||||
fi
|
||||
done
|
||||
|
||||
10
test.h
@@ -96,16 +96,6 @@ static int test_progress; // Current progress
|
||||
static char test_title[1024] = ""; // Current test title
|
||||
|
||||
|
||||
// Add printf syntax checking on supported compilers...
|
||||
#if defined(__has_extension) || defined(__GNUC__)
|
||||
# define TEST_FORMAT(a,b) __attribute__ ((__format__(__printf__,a,b)))
|
||||
static inline void testBegin(const char *title, ...) TEST_FORMAT(1,2);
|
||||
static inline void testEndMessage(bool pass, const char *message, ...) TEST_FORMAT(2,3);
|
||||
static inline void testError(const char *error, ...) TEST_FORMAT(1,2);
|
||||
static inline void testMessage(const char *error, ...) TEST_FORMAT(1,2);
|
||||
#endif // __has_extension || __GNUC__
|
||||
|
||||
|
||||
// Start a test
|
||||
static inline void
|
||||
testBegin(const char *title, ...) // I - printf-style title string
|
||||
|
||||
|
Before Width: | Height: | Size: 245 KiB |
|
Before Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 88 KiB |
|
Before Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 8.6 KiB |
|
Before Width: | Height: | Size: 7.4 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 9.1 KiB |
|
Before Width: | Height: | Size: 8.1 KiB |
|
Before Width: | Height: | Size: 9.5 KiB |
955
testpdfio.c
396
testttf.c
Normal file
@@ -0,0 +1,396 @@
|
||||
//
|
||||
// Unit test program for TTF library
|
||||
//
|
||||
// https://github.com/michaelrsweet/ttf
|
||||
//
|
||||
// Copyright © 2018-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// ./testttf [FILENAME]
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.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
|
||||
struct stat fileinfo; // Font file information
|
||||
FILE *fp = NULL; // File pointer
|
||||
void *data = NULL; // Memory buffer for font file
|
||||
const char *value; // Font (string) value
|
||||
int intvalue; // Font (integer) value
|
||||
float realvalue; // Font (real) value
|
||||
char psname[1024]; // Postscript font name
|
||||
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);
|
||||
fflush(stdout);
|
||||
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);
|
||||
|
||||
strncpy(psname, value, sizeof(psname) - 1);
|
||||
psname[sizeof(psname) - 1] = '\0';
|
||||
}
|
||||
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);
|
||||
font = NULL;
|
||||
|
||||
// Now copy the font to memory and open it that way...
|
||||
printf("fopen(\"%s\", \"rb\"): ", filename);
|
||||
if ((fp = fopen(filename, "rb")) == NULL)
|
||||
{
|
||||
printf("FAIL (%s)\n", strerror(errno));
|
||||
errors ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("PASS (%d)\n", fileno(fp));
|
||||
printf("fstat(%d): ", fileno(fp));
|
||||
if (fstat(fileno(fp), &fileinfo))
|
||||
{
|
||||
printf("FAIL (%s)\n", strerror(errno));
|
||||
errors ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("PASS (%lu bytes)\n", (unsigned long)fileinfo.st_size);
|
||||
|
||||
fputs("malloc(): ", stdout);
|
||||
if ((data = malloc((size_t)fileinfo.st_size)) == NULL)
|
||||
{
|
||||
printf("FAIL (%s)\n", strerror(errno));
|
||||
errors ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("PASS");
|
||||
fputs("fread(): ", stdout);
|
||||
if (fread(data, (size_t)fileinfo.st_size, 1, fp) != 1)
|
||||
{
|
||||
printf("FAIL (%s)\n", strerror(errno));
|
||||
errors ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("PASS");
|
||||
fputs("ttfCreateData(): ", stdout);
|
||||
if ((font = ttfCreateData(data, (size_t)fileinfo.st_size, /*idx*/0, error_cb, /*err_data*/NULL)) == NULL)
|
||||
{
|
||||
puts("FAIL");
|
||||
errors ++;
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("PASS");
|
||||
|
||||
fputs("ttfGetPostScriptName: ", stdout);
|
||||
if ((value = ttfGetPostScriptName(font)) != NULL)
|
||||
{
|
||||
if (!strcmp(value, psname))
|
||||
{
|
||||
printf("PASS (%s)\n", value);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("FAIL (got \"%s\", expected \"%s\")\n", value, psname);
|
||||
errors ++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
puts("FAIL");
|
||||
errors ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
|
||||
free(data);
|
||||
|
||||
ttfDelete(font);
|
||||
}
|
||||
|
||||
return (errors);
|
||||
}
|
||||
1
ttf
111
ttf.h
Normal file
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// Header file for TTF library
|
||||
//
|
||||
// https://github.com/michaelrsweet/ttf
|
||||
//
|
||||
// Copyright © 2018-2025 by Michael R Sweet.
|
||||
//
|
||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||
// information.
|
||||
//
|
||||
|
||||
#ifndef TTF_H
|
||||
# define TTF_H
|
||||
# include <stddef.h>
|
||||
# include <stdbool.h>
|
||||
# include <sys/types.h>
|
||||
# ifdef __cplusplus
|
||||
extern "C" {
|
||||
# endif // __cplusplus
|
||||
|
||||
|
||||
//
|
||||
// Types...
|
||||
//
|
||||
|
||||
typedef struct _ttf_s ttf_t; // Font object
|
||||
|
||||
typedef void (*ttf_err_cb_t)(void *data, const char *message);
|
||||
// Font error callback
|
||||
|
||||
typedef enum ttf_stretch_e // Font stretch
|
||||
{
|
||||
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
|
||||
} ttf_stretch_t;
|
||||
|
||||
typedef enum ttf_style_e // Font style
|
||||
{
|
||||
TTF_STYLE_NORMAL, // Normal font
|
||||
TTF_STYLE_ITALIC, // Italic font
|
||||
TTF_STYLE_OBLIQUE // Oblique (angled) font
|
||||
} ttf_style_t;
|
||||
|
||||
typedef enum ttf_variant_e // Font variant
|
||||
{
|
||||
TTF_VARIANT_NORMAL, // Normal font
|
||||
TTF_VARIANT_SMALL_CAPS // Font whose lowercase letters are small capitals
|
||||
} ttf_variant_t;
|
||||
|
||||
typedef enum ttf_weight_e // Font weight
|
||||
{
|
||||
TTF_WEIGHT_100 = 100, // Weight 100 (Thin)
|
||||
TTF_WEIGHT_200 = 200, // Weight 200 (Extra/Ultra-Light)
|
||||
TTF_WEIGHT_300 = 300, // Weight 300 (Light)
|
||||
TTF_WEIGHT_400 = 400, // Weight 400 (Normal/Regular)
|
||||
TTF_WEIGHT_500 = 500, // Weight 500 (Medium)
|
||||
TTF_WEIGHT_600 = 600, // Weight 600 (Semi/Demi-Bold)
|
||||
TTF_WEIGHT_700 = 700, // Weight 700 (Bold)
|
||||
TTF_WEIGHT_800 = 800, // Weight 800 (Extra/Ultra-Bold)
|
||||
TTF_WEIGHT_900 = 900 // Weight 900 (Black/Heavy)
|
||||
} ttf_weight_t;
|
||||
|
||||
typedef struct ttf_rect_s // Bounding rectangle
|
||||
{
|
||||
float left; // Left offset
|
||||
float top; // Top offset
|
||||
float right; // Right offset
|
||||
float bottom; // Bottom offset
|
||||
} ttf_rect_t;
|
||||
|
||||
|
||||
//
|
||||
// Functions...
|
||||
//
|
||||
|
||||
extern ttf_t *ttfCreate(const char *filename, size_t idx, ttf_err_cb_t err_cb, void *err_data);
|
||||
extern ttf_t *ttfCreateData(const void *data, size_t data_size, size_t idx, ttf_err_cb_t err_cb, void *err_data);
|
||||
extern void ttfDelete(ttf_t *font);
|
||||
extern int ttfGetAscent(ttf_t *font);
|
||||
extern ttf_rect_t *ttfGetBounds(ttf_t *font, ttf_rect_t *bounds);
|
||||
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 int ttfGetDescent(ttf_t *font);
|
||||
extern ttf_rect_t *ttfGetExtents(ttf_t *font, float size, const char *s, ttf_rect_t *extents);
|
||||
extern const char *ttfGetFamily(ttf_t *font);
|
||||
extern float ttfGetItalicAngle(ttf_t *font);
|
||||
extern int ttfGetMaxChar(ttf_t *font);
|
||||
extern int ttfGetMinChar(ttf_t *font);
|
||||
extern size_t ttfGetNumFonts(ttf_t *font);
|
||||
extern const char *ttfGetPostScriptName(ttf_t *font);
|
||||
extern ttf_stretch_t ttfGetStretch(ttf_t *font);
|
||||
extern ttf_style_t ttfGetStyle(ttf_t *font);
|
||||
extern const char *ttfGetVersion(ttf_t *font);
|
||||
extern int ttfGetWidth(ttf_t *font, int ch);
|
||||
extern ttf_weight_t ttfGetWeight(ttf_t *font);
|
||||
extern int ttfGetXHeight(ttf_t *font);
|
||||
extern bool ttfIsFixedPitch(ttf_t *font);
|
||||
|
||||
|
||||
# ifdef __cplusplus
|
||||
}
|
||||
# endif // __cplusplus
|
||||
#endif // !TTF_H
|
||||