17 Commits

Author SHA1 Message Date
6cb661f0f4 Cleanup changelog. 2024-08-21 08:25:11 -04:00
7e01451b18 Merge 0-character font fix from TTF. 2024-08-21 08:22:31 -04:00
138f3955d1 Add --password option to PDFio test program. 2024-08-19 17:12:16 -04:00
82844ad2ce Merge TTF v1.0.0 source files. 2024-08-19 16:59:00 -04:00
d7cce4dfbc Merge TTF v1.0.0 source files. 2024-08-19 16:58:38 -04:00
1cec42f399 Bump version to 1.3.2. 2024-08-09 10:55:32 -04:00
f3f70e7877 Merge some TTF sanity check fixes from the TTF project. 2024-08-09 10:54:28 -04:00
90923c3818 Update DLL exports. 2024-08-05 21:55:32 -04:00
986cc512cd Bump NuGet project versions. 2024-08-05 21:50:18 -04:00
c35ddbec00 Changelog 2024-08-05 21:49:26 -04:00
e4e1c39578 Merge commit from fork
Add range checking to TTF loader.
2024-08-05 21:47:48 -04:00
1d4f77cab1 Add examples to documentation (Issue #69) 2024-08-05 21:44:56 -04:00
b035130cde Merge pull request #68 from devnibo/master
Update documentation
2024-08-05 19:56:40 -04:00
d6d5813b04 Update changelog with CVE number. 2024-08-05 16:34:12 -04:00
6492f210cf Bump version and changelog. 2024-08-05 10:23:51 -04:00
207062a996 Add size limiting for num_cmap and nGlyphs. 2024-08-05 10:16:00 -04:00
7d37abb0df Update documentation 2024-07-07 16:35:56 +02:00
15 changed files with 593 additions and 84 deletions

View File

@ -2,8 +2,22 @@ Changes in PDFio
================ ================
v1.3.0 (June 28, 2024) v1.3.2 - YYYY-MM-DD
---------------------- -------------------
- Added some more sanity checks to the TrueType font reader.
v1.3.1 - 2024-08-05
-------------------
- CVE 2024-42358: Updated TrueType font reader to avoid large memory
allocations.
- Fixed some documentation errors and added examples (Issue #68, Issue #69)
v1.3.0 - 2024-06-28
-------------------
- Added `pdfioFileGetCatalog` API for accessing the root/catalog object of a - Added `pdfioFileGetCatalog` API for accessing the root/catalog object of a
PDF file (Issue #67) PDF file (Issue #67)
@ -13,8 +27,8 @@ v1.3.0 (June 28, 2024)
- Optimized string pool code. - Optimized string pool code.
v1.2.0 (January 24, 2024) v1.2.0 - 2024-01-24
------------------------- -------------------
- Now use autoconf to configure the PDFio sources (Issue #54) - Now use autoconf to configure the PDFio sources (Issue #54)
- Added `pdfioFileCreateNumberObj` and `pdfioFileCreateStringObj` functions - Added `pdfioFileCreateNumberObj` and `pdfioFileCreateStringObj` functions
@ -37,8 +51,8 @@ v1.2.0 (January 24, 2024)
65536 in the xref table (Issue #59) 65536 in the xref table (Issue #59)
v1.1.4 (December 3, 2023) v1.1.4 - 2023-12-03
------------------------- -------------------
- Fixed detection of encrypted strings that are too short (Issue #52) - Fixed detection of encrypted strings that are too short (Issue #52)
- Fixed a TrueType CMAP decoding bug. - Fixed a TrueType CMAP decoding bug.
@ -46,15 +60,15 @@ v1.1.4 (December 3, 2023)
- Added a ToUnicode map for Unicode text to support text copying. - Added a ToUnicode map for Unicode text to support text copying.
v1.1.3 (November 15, 2023) v1.1.3 - 2023-11-15
-------------------------- -------------------
- Fixed Unicode font support (Issue #16) - Fixed Unicode font support (Issue #16)
- Fixed missing initializer for 40-bit RC4 encryption (Issue #51) - Fixed missing initializer for 40-bit RC4 encryption (Issue #51)
v1.1.2 (October 10, 2023) v1.1.2 - 2023-10-10
------------------------- -------------------
- Updated `pdfioContentSetDashPattern` to support setting a solid (0 length) - Updated `pdfioContentSetDashPattern` to support setting a solid (0 length)
dash pattern (Issue #41) dash pattern (Issue #41)
@ -69,15 +83,15 @@ v1.1.2 (October 10, 2023)
(Issue #48) (Issue #48)
v1.1.1 (March 20, 2023) v1.1.1 - 2023-03-20
----------------------- -------------------
- CVE-2023-28428: Fixed a potential denial-of-service with corrupt PDF files. - CVE-2023-28428: Fixed a potential denial-of-service with corrupt PDF files.
- Fixed a few build issues. - Fixed a few build issues.
v1.1.0 (February 6, 2023) v1.1.0 - 2023-02-06
------------------------- -------------------
- CVE-2023-24808: Fixed a potential denial-of-service with corrupt PDF files. - CVE-2023-24808: Fixed a potential denial-of-service with corrupt PDF files.
- Added `pdfioFileCreateTemporary` function (Issue #29) - Added `pdfioFileCreateTemporary` function (Issue #29)
@ -91,28 +105,28 @@ v1.1.0 (February 6, 2023)
- Fixed `pdfioContentMatrixRotate` function. - Fixed `pdfioContentMatrixRotate` function.
v1.0.1 (March 2, 2022) v1.0.1 - 2022-03-02
---------------------- -------------------
- Added missing `pdfioPageGetNumStreams` and `pdfioPageOpenStream` functions. - Added missing `pdfioPageGetNumStreams` and `pdfioPageOpenStream` functions.
- Added demo pdfiototext utility. - Added demo pdfiototext utility.
- Fixed bug in `pdfioStreamGetToken`. - Fixed bug in `pdfioStreamGetToken`.
v1.0.0 (December 14, 2021) v1.0.0 - 2021-12-14
-------------------------- -------------------
- First stable release. - First stable release.
v1.0rc1 (November 30, 2021) v1.0rc1 - 2021-11-30
--------------------------- --------------------
- Fixed a few stack/buffer overflow bugs discovered via fuzzing. - Fixed a few stack/buffer overflow bugs discovered via fuzzing.
v1.0b2 (November 7, 2021) v1.0b2 - 2021-11-07
------------------------- -------------------
- Added `pdfioFileCreateOutput` API to support streaming output of PDF - Added `pdfioFileCreateOutput` API to support streaming output of PDF
(Issue #21) (Issue #21)
@ -123,7 +137,7 @@ v1.0b2 (November 7, 2021)
- Fixed some issues identified by a Coverity scan. - Fixed some issues identified by a Coverity scan.
v1.0b1 (August 30, 2021) v1.0b1 - 2021-08-30
------------------------ -------------------
- Initial release - Initial release

24
configure vendored
View File

@ -1,6 +1,6 @@
#! /bin/sh #! /bin/sh
# Guess values for system-dependent variables and create Makefiles. # Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.71 for pdfio 1.3.0. # Generated by GNU Autoconf 2.71 for pdfio 1.3.2.
# #
# Report bugs to <https://github.com/michaelrsweet/pdfio/issues>. # Report bugs to <https://github.com/michaelrsweet/pdfio/issues>.
# #
@ -610,8 +610,8 @@ MAKEFLAGS=
# Identity of this package. # Identity of this package.
PACKAGE_NAME='pdfio' PACKAGE_NAME='pdfio'
PACKAGE_TARNAME='pdfio' PACKAGE_TARNAME='pdfio'
PACKAGE_VERSION='1.3.0' PACKAGE_VERSION='1.3.2'
PACKAGE_STRING='pdfio 1.3.0' PACKAGE_STRING='pdfio 1.3.2'
PACKAGE_BUGREPORT='https://github.com/michaelrsweet/pdfio/issues' PACKAGE_BUGREPORT='https://github.com/michaelrsweet/pdfio/issues'
PACKAGE_URL='https://www.msweet.org/pdfio' PACKAGE_URL='https://www.msweet.org/pdfio'
@ -1293,7 +1293,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing. # 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. # This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF cat <<_ACEOF
\`configure' configures pdfio 1.3.0 to adapt to many kinds of systems. \`configure' configures pdfio 1.3.2 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]... Usage: $0 [OPTION]... [VAR=VALUE]...
@ -1359,7 +1359,7 @@ fi
if test -n "$ac_init_help"; then if test -n "$ac_init_help"; then
case $ac_init_help in case $ac_init_help in
short | recursive ) echo "Configuration of pdfio 1.3.0:";; short | recursive ) echo "Configuration of pdfio 1.3.2:";;
esac esac
cat <<\_ACEOF cat <<\_ACEOF
@ -1456,7 +1456,7 @@ fi
test -n "$ac_init_help" && exit $ac_status test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then if $ac_init_version; then
cat <<\_ACEOF cat <<\_ACEOF
pdfio configure 1.3.0 pdfio configure 1.3.2
generated by GNU Autoconf 2.71 generated by GNU Autoconf 2.71
Copyright (C) 2021 Free Software Foundation, Inc. Copyright (C) 2021 Free Software Foundation, Inc.
@ -1612,7 +1612,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake. running configure, to aid debugging if configure makes a mistake.
It was created by pdfio $as_me 1.3.0, which was It was created by pdfio $as_me 1.3.2, which was
generated by GNU Autoconf 2.71. Invocation command line was generated by GNU Autoconf 2.71. Invocation command line was
$ $0$ac_configure_args_raw $ $0$ac_configure_args_raw
@ -2368,9 +2368,9 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
PDFIO_VERSION="1.3.0" PDFIO_VERSION="1.3.2"
PDFIO_VERSION_MAJOR="`echo 1.3.0 | awk -F. '{print $1}'`" PDFIO_VERSION_MAJOR="`echo 1.3.2 | awk -F. '{print $1}'`"
PDFIO_VERSION_MINOR="`echo 1.3.0 | awk -F. '{printf("%d\n",$2);}'`" PDFIO_VERSION_MINOR="`echo 1.3.2 | awk -F. '{printf("%d\n",$2);}'`"
@ -4935,7 +4935,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
# report actual input values of CONFIG_FILES etc. instead of their # report actual input values of CONFIG_FILES etc. instead of their
# values after options handling. # values after options handling.
ac_log=" ac_log="
This file was extended by pdfio $as_me 1.3.0, which was This file was extended by pdfio $as_me 1.3.2, which was
generated by GNU Autoconf 2.71. Invocation command line was generated by GNU Autoconf 2.71. Invocation command line was
CONFIG_FILES = $CONFIG_FILES CONFIG_FILES = $CONFIG_FILES
@ -4991,7 +4991,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_config='$ac_cs_config_escaped' ac_cs_config='$ac_cs_config_escaped'
ac_cs_version="\\ ac_cs_version="\\
pdfio config.status 1.3.0 pdfio config.status 1.3.2
configured by $0, generated by GNU Autoconf 2.71, configured by $0, generated by GNU Autoconf 2.71,
with options \\"\$ac_cs_config\\" with options \\"\$ac_cs_config\\"

View File

@ -21,7 +21,7 @@ AC_PREREQ([2.70])
dnl Package name and version... dnl Package name and version...
AC_INIT([pdfio], [1.3.0], [https://github.com/michaelrsweet/pdfio/issues], [pdfio], [https://www.msweet.org/pdfio]) AC_INIT([pdfio], [1.3.2], [https://github.com/michaelrsweet/pdfio/issues], [pdfio], [https://www.msweet.org/pdfio])
PDFIO_VERSION="AC_PACKAGE_VERSION" PDFIO_VERSION="AC_PACKAGE_VERSION"
PDFIO_VERSION_MAJOR="`echo AC_PACKAGE_VERSION | awk -F. '{print $1}'`" PDFIO_VERSION_MAJOR="`echo AC_PACKAGE_VERSION | awk -F. '{print $1}'`"

View File

@ -1,4 +1,4 @@
.TH pdfio 3 "pdf read/write library" "2024-06-24" "pdf read/write library" .TH pdfio 3 "pdf read/write library" "2024-08-05" "pdf read/write library"
.SH NAME .SH NAME
pdfio \- pdf read/write library pdfio \- pdf read/write library
.SH Introduction .SH Introduction
@ -361,7 +361,7 @@ pdfioStreamWrite writes a buffer of data to the stream
.PP .PP
The PDF content helper functions provide additional functions for writing specific PDF page stream commands. The PDF content helper functions provide additional functions for writing specific PDF page stream commands.
.PP .PP
When you are done writing the stream, call pdfioStreamCLose to close both the stream and the object. When you are done writing the stream, call pdfioStreamClose to close both the stream and the object.
.SS PDF Content Helper Functions .SS PDF Content Helper Functions
.PP .PP
PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into five categories: PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into five categories:
@ -787,6 +787,125 @@ pdfioContentTextShowf draws a formatted string in a text block
pdfioContentTextShowJustified draws an array of literal strings with offsets between them pdfioContentTextShowJustified draws an array of literal strings with offsets between them
.SH Examples
.SS Read PDF Metadata
.PP
The following example function will open a PDF file and print the title, author, creation date, and number of pages:
.nf
#include <pdfio.h>
#include <time.h>
void
show_pdf_info(const char *filename)
{
pdfio_file_t *pdf;
time_t creation_date;
struct tm *creation_tm;
char creation_text[256];
// Open the PDF file with the default callbacks...
pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_cbdata*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
if (pdf == NULL)
return;
// Get the creation date and convert to a string...
creation_date = pdfioFileGetCreationDate(pdf);
creation_tm = localtime(&creation_date);
strftime(creation_text, sizeof(creation_text), "%c", &creation_tm);
// Print file information to stdout...
printf("%s:\\n", filename);
printf(" Title: %s\\n", pdfioFileGetTitle(pdf));
printf(" Author: %s\\n", pdfioFileGetAuthor(pdf));
printf(" Created On: %s\\n", creation_text);
printf(" Number Pages: %u\\n", (unsigned)pdfioFileGetNumPages(pdf));
// Close the PDF file...
pdfioFileClose(pdf);
}
.fi
.SS Create PDF File With Text and Image
.PP
The following example function will create a PDF file, embed a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with the text centered below:
.nf
#include <pdfio.h>
#include <pdfio\-content.h>
#include <string.h>
void
create_pdf_image_file(const char *pdfname, const char *imagename, const char *caption)
{
pdfio_file_t *pdf;
pdfio_obj_t *font;
pdfio_obj_t *image;
pdfio_dict_t *dict;
pdfio_stream_t *page;
double width, height;
double swidth, sheight;
double tx, ty;
// Create the PDF file...
pdf = pdfioFileCreate(pdfname, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
// Create a Courier base font for the caption
font = pdfioFileCreateFontObjFromBase(pdf, "Courier");
// Create an image object from the JPEG/PNG image file...
image = pdfioFileCreateImageObjFromFile(pdf, imagename, true);
// Create a page dictionary with the font and image...
dict = pdfioDictCreate(pdf);
pdfioPageDictAddFont(dict, "F1", font);
pdfioPageDictAddImage(dict, "IM1", image);
// Create the page and its content stream...
page = pdfioFileCreatePage(pdf, dict);
// Position and scale the image on the page...
width = pdfioImageGetWidth(image);
height = pdfioImageGetHeight(image);
// Default media_box is "universal" 595.28x792 points (8.27x11in or 210x279mm)
// Use margins of 36 points (0.5in or 12.7mm) with another 36 points for the
// caption underneath...
swidth = 595.28 \- 72.0;
sheight = swidth * height / width;
if (sheight > (792.0 \- 36.0 \- 72.0))
{
sheight = 792.0 \- 36.0 \- 72.0;
swidth = sheight * width / height;
}
tx = 0.5 * (595.28 \- swidth);
ty = 0.5 * (792 \- 36 \- sheight);
pdfioContentDrawImage(page, "IM1", tx, ty + 36.0, swidth, sheight);
// Draw the caption in black...
pdfioContentSetFillColorDeviceGray(page, 0.0);
// Compute the starting point for the text \- Courier is monospaced with a
// nominal width of 0.6 times the text height...
tx = 0.5 * (595.28 \- 18.0 * 0.6 * strlen(caption));
// Position and draw the caption underneath...
pdfioContentTextBegin(page);
pdfioContentSetTextFont(page, "F1", 18.0);
pdfioContentTextMoveTo(page, tx, ty);
pdfioContentTextShow(page, /*unicode*/false, caption);
pdfioContentTextEnd(page);
// Close the page stream and the PDF file...
pdfioStreamClose(page);
pdfioFileClose(pdf);
}
.fi
.SH ENUMERATIONS .SH ENUMERATIONS
.SS pdfio_cs_e .SS pdfio_cs_e

View File

@ -273,6 +273,10 @@ span.string {
<li><a href="#pdf-streams">PDF Streams</a></li> <li><a href="#pdf-streams">PDF Streams</a></li>
<li><a href="#pdf-content-helper-functions">PDF Content Helper Functions</a></li> <li><a href="#pdf-content-helper-functions">PDF Content Helper Functions</a></li>
</ul></li> </ul></li>
<li><a href="#examples">Examples</a><ul class="subcontents">
<li><a href="#read-pdf-metadata">Read PDF Metadata</a></li>
<li><a href="#create-pdf-file-with-text-and-image">Create PDF File With Text and Image</a></li>
</ul></li>
<li><a href="#FUNCTIONS">Functions</a><ul class="subcontents"> <li><a href="#FUNCTIONS">Functions</a><ul class="subcontents">
<li><a href="#pdfioArrayAppendArray">pdfioArrayAppendArray</a></li> <li><a href="#pdfioArrayAppendArray">pdfioArrayAppendArray</a></li>
<li><a href="#pdfioArrayAppendBinary">pdfioArrayAppendBinary</a></li> <li><a href="#pdfioArrayAppendBinary">pdfioArrayAppendBinary</a></li>
@ -709,7 +713,7 @@ pdfio_stream_t *st = pdfioFileCreatePage(pdf, dict);
</li> </li>
</ul> </ul>
<p>The <a href="#pdf-content-helper-functions">PDF content helper functions</a> provide additional functions for writing specific PDF page stream commands.</p> <p>The <a href="#pdf-content-helper-functions">PDF content helper functions</a> provide additional functions for writing specific PDF page stream commands.</p>
<p>When you are done writing the stream, call <a href="#pdfioStreamCLose"><code>pdfioStreamCLose</code></a> to close both the stream and the object.</p> <p>When you are done writing the stream, call <a href="#pdfioStreamClose"><code>pdfioStreamClose</code></a> to close both the stream and the object.</p>
<h3 class="title" id="pdf-content-helper-functions">PDF Content Helper Functions</h3> <h3 class="title" id="pdf-content-helper-functions">PDF Content Helper Functions</h3>
<p>PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into five categories:</p> <p>PDFio includes many helper functions for embedding or writing specific kinds of content to a PDF file. These functions can be roughly grouped into five categories:</p>
<ul> <ul>
@ -943,6 +947,119 @@ pdfio_obj_t *img = pdfioFileCreateImageObjFromFile(pdf, <span class="string">&qu
<li><p><a href="#pdfioContentTextShowJustified"><code>pdfioContentTextShowJustified</code></a> draws an array of literal strings with offsets between them</p> <li><p><a href="#pdfioContentTextShowJustified"><code>pdfioContentTextShowJustified</code></a> draws an array of literal strings with offsets between them</p>
</li> </li>
</ul> </ul>
<h2 class="title" id="examples">Examples</h2>
<h3 class="title" id="read-pdf-metadata">Read PDF Metadata</h3>
<p>The following example function will open a PDF file and print the title, author, creation date, and number of pages:</p>
<pre><code class="language-c"><span class="directive">#include &lt;pdfio.h&gt;</span>
<span class="directive">#include &lt;time.h&gt;</span>
<span class="reserved">void</span>
show_pdf_info(<span class="reserved">const</span> <span class="reserved">char</span> *filename)
{
pdfio_file_t *pdf;
time_t creation_date;
<span class="reserved">struct</span> tm *creation_tm;
<span class="reserved">char</span> creation_text[<span class="number">256</span>];
<span class="comment">// Open the PDF file with the default callbacks...</span>
pdf = pdfioFileOpen(filename, <span class="comment">/*password_cb*/</span>NULL, <span class="comment">/*password_cbdata*/</span>NULL, <span class="comment">/*error_cb*/</span>NULL, <span class="comment">/*error_cbdata*/</span>NULL);
<span class="reserved">if</span> (pdf == NULL)
<span class="reserved">return</span>;
<span class="comment">// Get the creation date and convert to a string...</span>
creation_date = pdfioFileGetCreationDate(pdf);
creation_tm = localtime(&amp;creation_date);
strftime(creation_text, <span class="reserved">sizeof</span>(creation_text), <span class="string">&quot;%c&quot;</span>, &amp;creation_tm);
<span class="comment">// Print file information to stdout...</span>
printf(<span class="string">&quot;%s:\n&quot;</span>, filename);
printf(<span class="string">&quot; Title: %s\n&quot;</span>, pdfioFileGetTitle(pdf));
printf(<span class="string">&quot; Author: %s\n&quot;</span>, pdfioFileGetAuthor(pdf));
printf(<span class="string">&quot; Created On: %s\n&quot;</span>, creation_text);
printf(<span class="string">&quot; Number Pages: %u\n&quot;</span>, (<span class="reserved">unsigned</span>)pdfioFileGetNumPages(pdf));
<span class="comment">// Close the PDF file...</span>
pdfioFileClose(pdf);
}
</code></pre>
<h3 class="title" id="create-pdf-file-with-text-and-image">Create PDF File With Text and Image</h3>
<p>The following example function will create a PDF file, embed a base font and the named JPEG or PNG image file, and then creates a page with the image centered on the page with the text centered below:</p>
<pre><code class="language-c"><span class="directive">#include &lt;pdfio.h&gt;</span>
<span class="directive">#include &lt;pdfio-content.h&gt;</span>
<span class="directive">#include &lt;string.h&gt;</span>
<span class="reserved">void</span>
create_pdf_image_file(<span class="reserved">const</span> <span class="reserved">char</span> *pdfname, <span class="reserved">const</span> <span class="reserved">char</span> *imagename, <span class="reserved">const</span> <span class="reserved">char</span> *caption)
{
pdfio_file_t *pdf;
pdfio_obj_t *font;
pdfio_obj_t *image;
pdfio_dict_t *dict;
pdfio_stream_t *page;
<span class="reserved">double</span> width, height;
<span class="reserved">double</span> swidth, sheight;
<span class="reserved">double</span> tx, ty;
<span class="comment">// Create the PDF file...</span>
pdf = pdfioFileCreate(pdfname, <span class="comment">/*version*/</span>NULL, <span class="comment">/*media_box*/</span>NULL, <span class="comment">/*crop_box*/</span>NULL, <span class="comment">/*error_cb*/</span>NULL, <span class="comment">/*error_cbdata*/</span>NULL);
<span class="comment">// Create a Courier base font for the caption</span>
font = pdfioFileCreateFontObjFromBase(pdf, <span class="string">&quot;Courier&quot;</span>);
<span class="comment">// Create an image object from the JPEG/PNG image file...</span>
image = pdfioFileCreateImageObjFromFile(pdf, imagename, <span class="reserved">true</span>);
<span class="comment">// Create a page dictionary with the font and image...</span>
dict = pdfioDictCreate(pdf);
pdfioPageDictAddFont(dict, <span class="string">&quot;F1&quot;</span>, font);
pdfioPageDictAddImage(dict, <span class="string">&quot;IM1&quot;</span>, image);
<span class="comment">// Create the page and its content stream...</span>
page = pdfioFileCreatePage(pdf, dict);
<span class="comment">// Position and scale the image on the page...</span>
width = pdfioImageGetWidth(image);
height = pdfioImageGetHeight(image);
<span class="comment">// Default media_box is &quot;universal&quot; 595.28x792 points (8.27x11in or 210x279mm)</span>
<span class="comment">// Use margins of 36 points (0.5in or 12.7mm) with another 36 points for the</span>
<span class="comment">// caption underneath...</span>
swidth = <span class="number">595.28</span> - <span class="number">72.0</span>;
sheight = swidth * height / width;
<span class="reserved">if</span> (sheight &gt; (<span class="number">792.0</span> - <span class="number">36.0</span> - <span class="number">72.0</span>))
{
sheight = <span class="number">792.0</span> - <span class="number">36.0</span> - <span class="number">72.0</span>;
swidth = sheight * width / height;
}
tx = <span class="number">0.5</span> * (<span class="number">595.28</span> - swidth);
ty = <span class="number">0.5</span> * (<span class="number">792</span> - <span class="number">36</span> - sheight);
pdfioContentDrawImage(page, <span class="string">&quot;IM1&quot;</span>, tx, ty + <span class="number">36.0</span>, swidth, sheight);
<span class="comment">// Draw the caption in black...</span>
pdfioContentSetFillColorDeviceGray(page, <span class="number">0.0</span>);
<span class="comment">// Compute the starting point for the text - Courier is monospaced with a</span>
<span class="comment">// nominal width of 0.6 times the text height...</span>
tx = <span class="number">0.5</span> * (<span class="number">595.28</span> - <span class="number">18.0</span> * <span class="number">0.6</span> * strlen(caption));
<span class="comment">// Position and draw the caption underneath...</span>
pdfioContentTextBegin(page);
pdfioContentSetTextFont(page, <span class="string">&quot;F1&quot;</span>, <span class="number">18.0</span>);
pdfioContentTextMoveTo(page, tx, ty);
pdfioContentTextShow(page, <span class="comment">/*unicode*/</span><span class="reserved">false</span>, caption);
pdfioContentTextEnd(page);
<span class="comment">// Close the page stream and the PDF file...</span>
pdfioStreamClose(page);
pdfioFileClose(pdf);
}
</code></pre>
<h2 class="title"><a id="FUNCTIONS">Functions</a></h2> <h2 class="title"><a id="FUNCTIONS">Functions</a></h2>
<h3 class="function"><a id="pdfioArrayAppendArray">pdfioArrayAppendArray</a></h3> <h3 class="function"><a id="pdfioArrayAppendArray">pdfioArrayAppendArray</a></h3>
<p class="description">Add an array value to an array.</p> <p class="description">Add an array value to an array.</p>
@ -3447,7 +3564,7 @@ array that was created using the
<tr><th>dict</th> <tr><th>dict</th>
<td class="description">Page dictionary</td></tr> <td class="description">Page dictionary</td></tr>
<tr><th>name</th> <tr><th>name</th>
<td class="description">Font name</td></tr> <td class="description">Font name; must not contain spaces</td></tr>
<tr><th>obj</th> <tr><th>obj</th>
<td class="description">Font object</td></tr> <td class="description">Font object</td></tr>
</tbody></table> </tbody></table>

View File

@ -345,7 +345,7 @@ to the stream:
The [PDF content helper functions](@) provide additional functions for writing The [PDF content helper functions](@) provide additional functions for writing
specific PDF page stream commands. specific PDF page stream commands.
When you are done writing the stream, call [`pdfioStreamCLose`](@@) to close When you are done writing the stream, call [`pdfioStreamClose`](@@) to close
both the stream and the object. both the stream and the object.
@ -586,3 +586,133 @@ escaping, as needed:
- [`pdfioContentTextShowf`](@@) draws a formatted string in a text block - [`pdfioContentTextShowf`](@@) draws a formatted string in a text block
- [`pdfioContentTextShowJustified`](@@) draws an array of literal strings with - [`pdfioContentTextShowJustified`](@@) draws an array of literal strings with
offsets between them offsets between them
Examples
========
Read PDF Metadata
-----------------
The following example function will open a PDF file and print the title, author,
creation date, and number of pages:
```c
#include <pdfio.h>
#include <time.h>
void
show_pdf_info(const char *filename)
{
pdfio_file_t *pdf;
time_t creation_date;
struct tm *creation_tm;
char creation_text[256];
// Open the PDF file with the default callbacks...
pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_cbdata*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
if (pdf == NULL)
return;
// Get the creation date and convert to a string...
creation_date = pdfioFileGetCreationDate(pdf);
creation_tm = localtime(&creation_date);
strftime(creation_text, sizeof(creation_text), "%c", &creation_tm);
// Print file information to stdout...
printf("%s:\n", filename);
printf(" Title: %s\n", pdfioFileGetTitle(pdf));
printf(" Author: %s\n", pdfioFileGetAuthor(pdf));
printf(" Created On: %s\n", creation_text);
printf(" Number Pages: %u\n", (unsigned)pdfioFileGetNumPages(pdf));
// Close the PDF file...
pdfioFileClose(pdf);
}
```
Create PDF File With Text and Image
-----------------------------------
The following example function will create a PDF file, embed a base font and the
named JPEG or PNG image file, and then creates a page with the image centered on
the page with the text centered below:
```c
#include <pdfio.h>
#include <pdfio-content.h>
#include <string.h>
void
create_pdf_image_file(const char *pdfname, const char *imagename, const char *caption)
{
pdfio_file_t *pdf;
pdfio_obj_t *font;
pdfio_obj_t *image;
pdfio_dict_t *dict;
pdfio_stream_t *page;
double width, height;
double swidth, sheight;
double tx, ty;
// Create the PDF file...
pdf = pdfioFileCreate(pdfname, /*version*/NULL, /*media_box*/NULL, /*crop_box*/NULL, /*error_cb*/NULL, /*error_cbdata*/NULL);
// Create a Courier base font for the caption
font = pdfioFileCreateFontObjFromBase(pdf, "Courier");
// Create an image object from the JPEG/PNG image file...
image = pdfioFileCreateImageObjFromFile(pdf, imagename, true);
// Create a page dictionary with the font and image...
dict = pdfioDictCreate(pdf);
pdfioPageDictAddFont(dict, "F1", font);
pdfioPageDictAddImage(dict, "IM1", image);
// Create the page and its content stream...
page = pdfioFileCreatePage(pdf, dict);
// Position and scale the image on the page...
width = pdfioImageGetWidth(image);
height = pdfioImageGetHeight(image);
// Default media_box is "universal" 595.28x792 points (8.27x11in or 210x279mm)
// Use margins of 36 points (0.5in or 12.7mm) with another 36 points for the
// caption underneath...
swidth = 595.28 - 72.0;
sheight = swidth * height / width;
if (sheight > (792.0 - 36.0 - 72.0))
{
sheight = 792.0 - 36.0 - 72.0;
swidth = sheight * width / height;
}
tx = 0.5 * (595.28 - swidth);
ty = 0.5 * (792 - 36 - sheight);
pdfioContentDrawImage(page, "IM1", tx, ty + 36.0, swidth, sheight);
// Draw the caption in black...
pdfioContentSetFillColorDeviceGray(page, 0.0);
// Compute the starting point for the text - Courier is monospaced with a
// nominal width of 0.6 times the text height...
tx = 0.5 * (595.28 - 18.0 * 0.6 * strlen(caption));
// Position and draw the caption underneath...
pdfioContentTextBegin(page);
pdfioContentSetTextFont(page, "F1", 18.0);
pdfioContentTextMoveTo(page, tx, ty);
pdfioContentTextShow(page, /*unicode*/false, caption);
pdfioContentTextEnd(page);
// Close the page stream and the PDF file...
pdfioStreamClose(page);
pdfioFileClose(pdf);
}
```

View File

@ -2349,7 +2349,7 @@ pdfioPageDictAddColorSpace(
bool // O - `true` on success, `false` on failure bool // O - `true` on success, `false` on failure
pdfioPageDictAddFont( pdfioPageDictAddFont(
pdfio_dict_t *dict, // I - Page dictionary pdfio_dict_t *dict, // I - Page dictionary
const char *name, // I - Font name const char *name, // I - Font name; must not contain spaces
pdfio_obj_t *obj) // I - Font object pdfio_obj_t *obj) // I - Font object
{ {
pdfio_dict_t *resources; // Resource dictionary pdfio_dict_t *resources; // Resource dictionary

View File

@ -23,7 +23,7 @@ extern "C" {
// Version number... // Version number...
// //
# define PDFIO_VERSION "1.3.0" # define PDFIO_VERSION "1.3.1"
// //

View File

@ -1,7 +1,8 @@
LIBRARY pdfio1 LIBRARY pdfio1
VERSION 1.2 VERSION 1.3
EXPORTS EXPORTS
_pdfioArrayDebug _pdfioArrayDebug
_pdfioArrayDecrypt
_pdfioArrayDelete _pdfioArrayDelete
_pdfioArrayGetValue _pdfioArrayGetValue
_pdfioArrayRead _pdfioArrayRead
@ -24,6 +25,7 @@ _pdfioCryptoSHA256Init
_pdfioCryptoUnlock _pdfioCryptoUnlock
_pdfioDictClear _pdfioDictClear
_pdfioDictDebug _pdfioDictDebug
_pdfioDictDecrypt
_pdfioDictDelete _pdfioDictDelete
_pdfioDictGetValue _pdfioDictGetValue
_pdfioDictRead _pdfioDictRead
@ -61,9 +63,12 @@ _pdfioTokenPush
_pdfioTokenRead _pdfioTokenRead
_pdfioValueCopy _pdfioValueCopy
_pdfioValueDebug _pdfioValueDebug
_pdfioValueDecrypt
_pdfioValueDelete _pdfioValueDelete
_pdfioValueRead _pdfioValueRead
_pdfioValueWrite _pdfioValueWrite
_pdfio_strtod
_pdfio_vsnprintf
pdfioArrayAppendArray pdfioArrayAppendArray
pdfioArrayAppendBinary pdfioArrayAppendBinary
pdfioArrayAppendBoolean pdfioArrayAppendBoolean
@ -190,6 +195,7 @@ pdfioFileCreateStringObj
pdfioFileCreateTemporary pdfioFileCreateTemporary
pdfioFileFindObj pdfioFileFindObj
pdfioFileGetAuthor pdfioFileGetAuthor
pdfioFileGetCatalog
pdfioFileGetCreationDate pdfioFileGetCreationDate
pdfioFileGetCreator pdfioFileGetCreator
pdfioFileGetID pdfioFileGetID

View File

@ -3,7 +3,7 @@
<metadata> <metadata>
<id>pdfio_native</id> <id>pdfio_native</id>
<title>PDFio Library for VS2019+</title> <title>PDFio Library for VS2019+</title>
<version>1.3.0</version> <version>1.3.2</version>
<authors>Michael R Sweet</authors> <authors>Michael R Sweet</authors>
<owners>michaelrsweet</owners> <owners>michaelrsweet</owners>
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl> <projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>
@ -16,7 +16,7 @@
<copyright>Copyright © 2019-2024 by Michael R Sweet</copyright> <copyright>Copyright © 2019-2024 by Michael R Sweet</copyright>
<tags>pdf file native</tags> <tags>pdf file native</tags>
<dependencies> <dependencies>
<dependency id="pdfio_native.redist" version="1.3.0" /> <dependency id="pdfio_native.redist" version="1.3.2" />
<dependency id="zlib_native.redist" version="1.2.11" /> <dependency id="zlib_native.redist" version="1.2.11" />
</dependencies> </dependencies>
</metadata> </metadata>

View File

@ -3,7 +3,7 @@
<metadata> <metadata>
<id>pdfio_native.redist</id> <id>pdfio_native.redist</id>
<title>PDFio Library for VS2019+</title> <title>PDFio Library for VS2019+</title>
<version>1.3.0</version> <version>1.3.2</version>
<authors>Michael R Sweet</authors> <authors>Michael R Sweet</authors>
<owners>michaelrsweet</owners> <owners>michaelrsweet</owners>
<projectUrl>https://github.com/michaelrsweet/pappl</projectUrl> <projectUrl>https://github.com/michaelrsweet/pappl</projectUrl>

View File

@ -27,7 +27,7 @@
// //
static int do_crypto_tests(void); static int do_crypto_tests(void);
static int do_test_file(const char *filename, int objnum, bool verbose); static int do_test_file(const char *filename, int objnum, const char *password, bool verbose);
static int do_unit_tests(void); static int do_unit_tests(void);
static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label); static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label);
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
@ -37,6 +37,7 @@ static const char *password_cb(void *data, const char *filename);
static int read_unit_file(const char *filename, size_t num_pages, size_t first_image, bool is_output); static int read_unit_file(const char *filename, size_t num_pages, size_t first_image, bool is_output);
static ssize_t token_consume_cb(const char **s, size_t bytes); static ssize_t token_consume_cb(const char **s, size_t bytes);
static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes); static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes);
static int usage(FILE *fp);
static int verify_image(pdfio_file_t *pdf, size_t number); static int verify_image(pdfio_file_t *pdf, size_t number);
static int write_alpha_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font); static int write_alpha_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
static int write_color_patch(pdfio_stream_t *st, bool device); static int write_color_patch(pdfio_stream_t *st, bool device);
@ -59,22 +60,33 @@ int // O - Exit status
main(int argc, // I - Number of command-line arguments main(int argc, // I - Number of command-line arguments
char *argv[]) // I - Command-line arguments char *argv[]) // I - Command-line arguments
{ {
int ret = 0; // Return value int ret = 0; // Return value
fprintf(stderr, "testpdfio: Test locale is \"%s\".\n", setlocale(LC_ALL, getenv("LANG")));
if (argc > 1) if (argc > 1)
{ {
int i; // Looping var int i; // Looping var
const char *password = NULL; // Password
bool verbose = false; // Be verbose? bool verbose = false; // Be verbose?
for (i = 1; i < argc; i ++) for (i = 1; i < argc; i ++)
{ {
if (!strcmp(argv[i], "--help")) if (!strcmp(argv[i], "--help"))
{ {
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]"); return (usage(stdout));
return (0); }
else if (!strcmp(argv[i], "--password"))
{
i ++;
if (i < argc)
{
password = argv[i];
}
else
{
fputs("testpdfio: Missing password after '--password'.\n", stderr);
return (usage(stderr));
}
} }
else if (!strcmp(argv[i], "--verbose")) else if (!strcmp(argv[i], "--verbose"))
{ {
@ -82,24 +94,27 @@ main(int argc, // I - Number of command-line arguments
} }
else if (argv[i][0] == '-') else if (argv[i][0] == '-')
{ {
printf("Unknown option '%s'.\n\n", argv[i]); fprintf(stderr, "testpdfio: Unknown option '%s'.\n", argv[i]);
puts("Usage: ./testpdfio [--help] [--verbose] [filename [objnum] ...]"); return (usage(stderr));
return (1);
} }
else if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255)) else if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255))
{ {
// filename.pdf object-number // filename.pdf object-number
if (do_test_file(argv[i], atoi(argv[i + 1]), verbose)) if (do_test_file(argv[i], atoi(argv[i + 1]), password, verbose))
ret = 1; ret = 1;
i ++; i ++;
} }
else if (do_test_file(argv[i], 0, verbose)) else if (do_test_file(argv[i], 0, password, verbose))
{
ret = 1; ret = 1;
}
} }
} }
else else
{ {
fprintf(stderr, "testpdfio: Test locale is \"%s\".\n", setlocale(LC_ALL, getenv("LANG")));
#if _WIN32 #if _WIN32
// Windows puts executables in Platform/Configuration subdirs... // Windows puts executables in Platform/Configuration subdirs...
if (!_access("../../testfiles", 0)) if (!_access("../../testfiles", 0))
@ -363,6 +378,7 @@ do_crypto_tests(void)
static int // O - Exit status static int // O - Exit status
do_test_file(const char *filename, // I - PDF filename do_test_file(const char *filename, // I - PDF filename
int objnum, // I - Object number to dump, if any int objnum, // I - Object number to dump, if any
const char *password, // I - Password for file
bool verbose) // I - Be verbose? bool verbose) // I - Be verbose?
{ {
bool error = false; // Have we shown an error yet? bool error = false; // Have we shown an error yet?
@ -381,7 +397,7 @@ do_test_file(const char *filename, // I - PDF filename
fflush(stdout); fflush(stdout);
} }
if ((pdf = pdfioFileOpen(filename, /*password_cb*/NULL, /*password_data*/NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((pdf = pdfioFileOpen(filename, password_cb, (void *)password, (pdfio_error_cb_t)error_cb, &error)) != NULL)
{ {
if (objnum) if (objnum)
{ {
@ -1559,6 +1575,23 @@ token_peek_cb(const char **s, // IO - Test string
} }
//
// 'usage()' - Show program usage.
//
static int // O - Exit status
usage(FILE *fp) // I - Output file
{
fputs("Usage: ./testpdfio [OPTIONS] [FILENAME [OBJNUM]] ...\n", fp);
fputs("Options:\n", fp);
fputs(" --help Show program help.\n", fp);
fputs(" --password PASSWORD Set PDF password.\n", fp);
fputs(" --verbose Be verbose.\n", fp);
return (fp != stdout);
}
// //
// 'verify_image()' - Verify an image object. // 'verify_image()' - Verify an image object.
// //

View File

@ -3,7 +3,7 @@
// //
// https://github.com/michaelrsweet/ttf // https://github.com/michaelrsweet/ttf
// //
// Copyright © 2018-2023 by Michael R Sweet. // Copyright © 2018-2024 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -120,6 +120,7 @@ test_font(const char *filename) // I - Font filename
printf("ttfCreate(\"%s\"): ", filename); printf("ttfCreate(\"%s\"): ", filename);
fflush(stdout);
if ((font = ttfCreate(filename, 0, error_cb, NULL)) != NULL) if ((font = ttfCreate(filename, 0, error_cb, NULL)) != NULL)
puts("PASS"); puts("PASS");
else else

119
ttf.c
View File

@ -3,7 +3,7 @@
// //
// https://github.com/michaelrsweet/ttf // https://github.com/michaelrsweet/ttf
// //
// Copyright © 2018-2023 by Michael R Sweet. // Copyright © 2018-2024 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -62,7 +62,7 @@
# define O_CREAT _O_CREAT # define O_CREAT _O_CREAT
# define O_TRUNC _O_TRUNC # define O_TRUNC _O_TRUNC
typedef __int64 ssize_t; // POSIX type not present on Windows... typedef __int64 ssize_t; // POSIX type not present on Windows... @private@
#else #else
# include <unistd.h> # include <unistd.h>
@ -99,6 +99,8 @@ typedef __int64 ssize_t; // POSIX type not present on Windows...
// //
#define TTF_FONT_MAX_CHAR 262144 // Maximum number of character values #define TTF_FONT_MAX_CHAR 262144 // Maximum number of character values
#define TTF_FONT_MAX_GROUPS 65536 // Maximum number of sub-groups
#define TTF_FONT_MAX_NAMES 16777216// Maximum size of names table we support
// //
@ -254,7 +256,7 @@ typedef struct _ttf_off_hhea_s // Horizontal header
{ {
short ascender, // Ascender short ascender, // Ascender
descender; // Descender descender; // Descender
int numberOfHMetrics; // Number of horizontal metrics unsigned short numberOfHMetrics; // Number of horizontal metrics
} _ttf_off_hhea_t; } _ttf_off_hhea_t;
typedef struct _ttf_off_os_2_s // OS/2 information typedef struct _ttf_off_os_2_s // OS/2 information
@ -297,7 +299,28 @@ static unsigned seek_table(ttf_t *font, unsigned tag, unsigned offset, bool requ
// //
// 'ttfCreate()' - Create a new font object for the named font family. // 'ttfCreate()' - Create a new font object for the named font file.
//
// This function creates a new font object for the named TrueType or OpenType
// font file or collection. The "filename" argument specifies the name of the
// file to read.
//
// The "idx" argument specifies the font to load from a collection - the first
// font is number `0`. Once created, you can call the @link ttfGetNumFonts@
// function to determine whether the loaded font file is a collection with more
// than one font.
//
// The "err_cb" and "err_data" arguments specify a callback function and data
// pointer for receiving error messages. If `NULL`, errors are sent to the
// `stderr` file. The callback function receives the data pointer and a text
// message string, for example:
//
// ```
// void my_err_cb(void *err_data, const char *message)
// {
// fprintf(stderr, "ERROR: %s\n", message);
// }
// ```
// //
ttf_t * // O - New font object ttf_t * // O - New font object
@ -550,6 +573,10 @@ ttfGetAscent(ttf_t *font) // I - Font
// //
// 'ttfGetBounds()' - Get the bounds of all characters in a font. // 'ttfGetBounds()' - Get the bounds of all characters in a font.
// //
// This function gets the bounds of all characters in a font. The "bounds"
// argument is a pointer to a `ttf_rect_t` structure that will be filled with
// the limits for characters in the font scaled to a 1000x1000 unit square.
//
ttf_rect_t * // O - Bounds or `NULL` on error ttf_rect_t * // O - Bounds or `NULL` on error
ttfGetBounds(ttf_t *font, // I - Font ttfGetBounds(ttf_t *font, // I - Font
@ -631,8 +658,11 @@ ttfGetDescent(ttf_t *font) // I - Font
// //
// 'ttfGetExtents()' - Get the extents of a UTF-8 string. // 'ttfGetExtents()' - Get the extents of a UTF-8 string.
// //
// This function computes the extents of a UTF-8 string when rendered using the // This function computes the extents of the UTF-8 string "s" when rendered
// specified font and size. // using the specified font "font" and size "size". The "extents" argument is
// a pointer to a `ttf_rect_t` structure that is filled with the extents of a
// simple rendering of the string with no kerning or rewriting applied. The
// values are scaled using the specified font size.
// //
ttf_rect_t * // O - Pointer to extents or `NULL` on error ttf_rect_t * // O - Pointer to extents or `NULL` on error
@ -1272,20 +1302,34 @@ read_cmap(ttf_t *font) // I - Font
for (i = 0; i < numGlyphIdArray; i ++) for (i = 0; i < numGlyphIdArray; i ++)
glyphIdArray[i] = read_ushort(font); glyphIdArray[i] = read_ushort(font);
#ifdef DEBUG for (i = 0, segment = segments; i < segCount; i ++, segment ++)
for (i = 0; i < segCount; i ++) {
TTF_DEBUG("read_cmap: segment[%d].startCode=%d, endCode=%d, idDelta=%d, idRangeOffset=%d\n", i, segments[i].startCode, segments[i].endCode, segments[i].idDelta, segments[i].idRangeOffset); TTF_DEBUG("read_cmap: segment[%d].startCode=%d, endCode=%d, idDelta=%d, idRangeOffset=%d\n", i, segment->startCode, segment->endCode, segment->idDelta, segment->idRangeOffset);
if (segment->startCode > segment->endCode)
{
errorf(font, "Bad cmap table segment %u to %u.", segments->startCode, segment->endCode);
return (false);
}
// Based on the end code of the segment table, allocate space for the
// uncompressed cmap table...
if (segment->endCode >= font->num_cmap)
font->num_cmap = segment->endCode + 1;
}
#ifdef DEBUG
for (i = 0; i < numGlyphIdArray; i ++) for (i = 0; i < numGlyphIdArray; i ++)
TTF_DEBUG("read_cmap: glyphIdArray[%d]=%d\n", i, glyphIdArray[i]); TTF_DEBUG("read_cmap: glyphIdArray[%d]=%d\n", i, glyphIdArray[i]);
#endif /* DEBUG */ #endif /* DEBUG */
// Based on the end code of the segent table, allocate space for the if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
// uncompressed cmap table... {
// segCount --; // Last segment is not used (sigh) errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
return (false);
}
font->num_cmap = segments[segCount - 1].endCode + 1; font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
if (!font->cmap) if (!font->cmap)
{ {
@ -1356,6 +1400,12 @@ read_cmap(ttf_t *font) // I - Font
TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups); TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups);
if (nGroups > TTF_FONT_MAX_GROUPS)
{
errorf(font, "Invalid cmap table with %u groups.", nGroups);
return (false);
}
if ((groups = (_ttf_off_cmap12_t *)calloc(nGroups, sizeof(_ttf_off_cmap12_t))) == NULL) if ((groups = (_ttf_off_cmap12_t *)calloc(nGroups, sizeof(_ttf_off_cmap12_t))) == NULL)
{ {
errorf(font, "Unable to allocate memory for cmap."); errorf(font, "Unable to allocate memory for cmap.");
@ -1369,6 +1419,12 @@ read_cmap(ttf_t *font) // I - Font
group->startGlyphID = read_ulong(font); group->startGlyphID = read_ulong(font);
TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, startGlyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->startGlyphID); TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, startGlyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->startGlyphID);
if (group->startCharCode > group->endCharCode)
{
errorf(font, "Bad cmap table segment %u to %u.", group->startCharCode, group->endCharCode);
return (false);
}
if (group->endCharCode >= font->num_cmap) if (group->endCharCode >= font->num_cmap)
font->num_cmap = group->endCharCode + 1; font->num_cmap = group->endCharCode + 1;
} }
@ -1376,6 +1432,13 @@ read_cmap(ttf_t *font) // I - Font
// Based on the end code of the segent table, allocate space for the // Based on the end code of the segent table, allocate space for the
// uncompressed cmap table... // uncompressed cmap table...
TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap); TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap);
if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
{
errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
return (false);
}
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int)); font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
if (!font->cmap) if (!font->cmap)
@ -1426,6 +1489,12 @@ read_cmap(ttf_t *font) // I - Font
TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups); TTF_DEBUG("read_cmap: nGroups=%u\n", nGroups);
if (nGroups > TTF_FONT_MAX_GROUPS)
{
errorf(font, "Invalid cmap table with %u groups.", nGroups);
return (false);
}
if ((groups = (_ttf_off_cmap13_t *)calloc(nGroups, sizeof(_ttf_off_cmap13_t))) == NULL) if ((groups = (_ttf_off_cmap13_t *)calloc(nGroups, sizeof(_ttf_off_cmap13_t))) == NULL)
{ {
errorf(font, "Unable to allocate memory for cmap."); errorf(font, "Unable to allocate memory for cmap.");
@ -1439,6 +1508,12 @@ read_cmap(ttf_t *font) // I - Font
group->glyphID = read_ulong(font); group->glyphID = read_ulong(font);
TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, glyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->glyphID); TTF_DEBUG("read_cmap: [%u] startCharCode=%u, endCharCode=%u, glyphID=%u\n", gidx, group->startCharCode, group->endCharCode, group->glyphID);
if (group->startCharCode > group->endCharCode)
{
errorf(font, "Bad cmap table segment %u to %u.", group->startCharCode, group->endCharCode);
return (false);
}
if (group->endCharCode >= font->num_cmap) if (group->endCharCode >= font->num_cmap)
font->num_cmap = group->endCharCode + 1; font->num_cmap = group->endCharCode + 1;
} }
@ -1446,6 +1521,13 @@ read_cmap(ttf_t *font) // I - Font
// Based on the end code of the segent table, allocate space for the // Based on the end code of the segent table, allocate space for the
// uncompressed cmap table... // uncompressed cmap table...
TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap); TTF_DEBUG("read_cmap: num_cmap=%u\n", (unsigned)font->num_cmap);
if (font->num_cmap == 0 || font->num_cmap > TTF_FONT_MAX_CHAR)
{
errorf(font, "Invalid cmap table with %u characters.", (unsigned)font->num_cmap);
return (false);
}
font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int)); font->cmap = cmapptr = (int *)malloc(font->num_cmap * sizeof(int));
if (!font->cmap) if (!font->cmap)
@ -1565,7 +1647,7 @@ read_hmtx(ttf_t *font, // I - Font
_ttf_off_hhea_t *hhea) // O - hhea table data _ttf_off_hhea_t *hhea) // O - hhea table data
{ {
unsigned length; // Length of hmtx table unsigned length; // Length of hmtx table
int i; // Looping var unsigned i; // Looping var
_ttf_metric_t *widths; // Glyph metrics array _ttf_metric_t *widths; // Glyph metrics array
@ -1644,8 +1726,15 @@ read_names(ttf_t *font) // I - Font
return (false); return (false);
font->names.storage_size = length - (unsigned)offset; font->names.storage_size = length - (unsigned)offset;
if (font->names.storage_size > TTF_FONT_MAX_NAMES)
{
errorf(font, "Name table too large - %u bytes.", (unsigned)font->names.storage_size);
return (false);
}
if ((font->names.storage = malloc(font->names.storage_size)) == NULL) if ((font->names.storage = malloc(font->names.storage_size)) == NULL)
return (false); return (false);
memset(font->names.storage, 'A', font->names.storage_size); memset(font->names.storage, 'A', font->names.storage_size);
for (i = font->names.num_names, name = font->names.names; i > 0; i --, name ++) for (i = font->names.num_names, name = font->names.names; i > 0; i --, name ++)

16
ttf.h
View File

@ -3,7 +3,7 @@
// //
// https://github.com/michaelrsweet/ttf // https://github.com/michaelrsweet/ttf
// //
// Copyright © 2018-2023 by Michael R Sweet. // Copyright © 2018-2024 by Michael R Sweet.
// //
// Licensed under Apache License v2.0. See the file "LICENSE" for more // Licensed under Apache License v2.0. See the file "LICENSE" for more
// information. // information.
@ -22,12 +22,12 @@ extern "C" {
// Types... // Types...
// //
typedef struct _ttf_s ttf_t; //// Font object typedef struct _ttf_s ttf_t; // Font object
typedef void (*ttf_err_cb_t)(void *data, const char *message); typedef void (*ttf_err_cb_t)(void *data, const char *message);
//// Font error callback // Font error callback
typedef enum ttf_stretch_e //// Font stretch typedef enum ttf_stretch_e // Font stretch
{ {
TTF_STRETCH_NORMAL, // normal TTF_STRETCH_NORMAL, // normal
TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed
@ -40,20 +40,20 @@ typedef enum ttf_stretch_e //// Font stretch
TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded
} ttf_stretch_t; } ttf_stretch_t;
typedef enum ttf_style_e //// Font style typedef enum ttf_style_e // Font style
{ {
TTF_STYLE_NORMAL, // Normal font TTF_STYLE_NORMAL, // Normal font
TTF_STYLE_ITALIC, // Italic font TTF_STYLE_ITALIC, // Italic font
TTF_STYLE_OBLIQUE // Oblique (angled) font TTF_STYLE_OBLIQUE // Oblique (angled) font
} ttf_style_t; } ttf_style_t;
typedef enum ttf_variant_e //// Font variant typedef enum ttf_variant_e // Font variant
{ {
TTF_VARIANT_NORMAL, // Normal font TTF_VARIANT_NORMAL, // Normal font
TTF_VARIANT_SMALL_CAPS // Font whose lowercase letters are small capitals TTF_VARIANT_SMALL_CAPS // Font whose lowercase letters are small capitals
} ttf_variant_t; } ttf_variant_t;
typedef enum ttf_weight_e //// Font weight typedef enum ttf_weight_e // Font weight
{ {
TTF_WEIGHT_100 = 100, // Weight 100 (Thin) TTF_WEIGHT_100 = 100, // Weight 100 (Thin)
TTF_WEIGHT_200 = 200, // Weight 200 (Extra/Ultra-Light) TTF_WEIGHT_200 = 200, // Weight 200 (Extra/Ultra-Light)
@ -66,7 +66,7 @@ typedef enum ttf_weight_e //// Font weight
TTF_WEIGHT_900 = 900 // Weight 900 (Black/Heavy) TTF_WEIGHT_900 = 900 // Weight 900 (Black/Heavy)
} ttf_weight_t; } ttf_weight_t;
typedef struct ttf_rect_s //// Bounding rectangle typedef struct ttf_rect_s // Bounding rectangle
{ {
float left; // Left offset float left; // Left offset
float top; // Top offset float top; // Top offset