Compare commits

...

4 Commits

Author SHA1 Message Date
Michael R Sweet
927452d1eb
Changelog and exports updates. 2023-12-04 21:22:13 -05:00
Michael R Sweet
f1ad982fd1
Update docos. 2023-12-04 21:21:18 -05:00
Michael R Sweet
c188cb8dad
Finish implementation of pdfioContentTextMeasure (Issue #17) 2023-12-04 21:20:51 -05:00
Michael R Sweet
4919783da5
Save work on string measure support (Issue #17) 2023-12-04 18:54:33 -05:00
10 changed files with 204 additions and 19 deletions

View File

@ -5,6 +5,7 @@ Changes in PDFio
v1.2.0 (Month DD, YYYY) v1.2.0 (Month DD, YYYY)
----------------------- -----------------------
- Added `pdfioContentTextMeasure` function (Issue #17)
- Now use autoconf to configure the PDFio sources (Issue #54) - Now use autoconf to configure the PDFio sources (Issue #54)

View File

@ -233,8 +233,7 @@ DOCFLAGS = \
doc: doc:
echo Generating documentation... echo Generating documentation...
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSION)" $(PUBHEADERS) $(PUBOBJS:.o=.c) --body doc/pdfio.md --coverimage doc/pdfio-512.png pdfio.xml >doc/pdfio.html codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSION)" $(PUBHEADERS) $(PUBOBJS:.o=.c) --body doc/pdfio.md --coverimage doc/pdfio-512.png pdfio.xml >doc/pdfio.html
codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSIONmc6809e codedoc $(DOCFLAGS) --title "PDFio Programming Manual v$(PDFIO_VERSION)" --body doc/pdfio.md --coverimage doc/pdfio-epub.png pdfio.xml --epub doc/pdfio.epub
)" --body doc/pdfio.md --coverimage doc/pdfio-epub.png pdfio.xml --epub doc/pdfio.epub
codedoc $(DOCFLAGS) --title "pdf read/write library" --man pdfio --section 3 --body doc/pdfio.md pdfio.xml >doc/pdfio.3 codedoc $(DOCFLAGS) --title "pdf read/write library" --man pdfio --section 3 --body doc/pdfio.md pdfio.xml >doc/pdfio.3
rm -f pdfio.xml rm -f pdfio.xml

View File

@ -1,4 +1,4 @@
.TH pdfio 3 "pdf read/write library" "2023-11-15" "pdf read/write library" .TH pdfio 3 "pdf read/write library" "2023-12-04" "pdf read/write library"
.SH NAME .SH NAME
pdfio \- pdf read/write library pdfio \- pdf read/write library
.SH Introduction .SH Introduction
@ -1774,6 +1774,20 @@ bool pdfioContentTextEnd (
pdfio_stream_t *st pdfio_stream_t *st
); );
.fi .fi
.SS pdfioContentTextMeasure
Measure a text string and return its width.
.PP
.nf
double pdfioContentTextMeasure (
pdfio_obj_t *font,
const char *s,
double size
);
.fi
.PP
This function measures the given text string "s" and returns its width based
on "size". The text string must always use the UTF-8 (Unicode) encoding but
any control characters (such as newlines) are ignored.
.SS pdfioContentTextMoveLine .SS pdfioContentTextMoveLine
Move to the next line and offset. Move to the next line and offset.
.PP .PP

View File

@ -1,13 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en-US"> <html lang="en-US">
<head> <head>
<title>PDFio Programming Manual v1.1.3</title> <title>PDFio Programming Manual v1.2.0</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="generator" content="codedoc v3.7"> <meta name="generator" content="codedoc v3.7">
<meta name="author" content="Michael R Sweet"> <meta name="author" content="Michael R Sweet">
<meta name="language" content="en-US"> <meta name="language" content="en-US">
<meta name="copyright" content="Copyright © 2021-2023 by Michael R Sweet"> <meta name="copyright" content="Copyright © 2021-2023 by Michael R Sweet">
<meta name="version" content="1.1.3"> <meta name="version" content="1.2.0">
<style type="text/css"><!-- <style type="text/css"><!--
body { body {
background: white; background: white;
@ -245,7 +245,7 @@ span.string {
<body> <body>
<div class="header"> <div class="header">
<p><img class="title" src="pdfio-512.png"></p> <p><img class="title" src="pdfio-512.png"></p>
<h1 class="title">PDFio Programming Manual v1.1.3</h1> <h1 class="title">PDFio Programming Manual v1.2.0</h1>
<p>Michael R Sweet</p> <p>Michael R Sweet</p>
<p>Copyright © 2021-2023 by Michael R Sweet</p> <p>Copyright © 2021-2023 by Michael R Sweet</p>
</div> </div>
@ -342,6 +342,7 @@ span.string {
<li><a href="#pdfioContentStroke">pdfioContentStroke</a></li> <li><a href="#pdfioContentStroke">pdfioContentStroke</a></li>
<li><a href="#pdfioContentTextBegin">pdfioContentTextBegin</a></li> <li><a href="#pdfioContentTextBegin">pdfioContentTextBegin</a></li>
<li><a href="#pdfioContentTextEnd">pdfioContentTextEnd</a></li> <li><a href="#pdfioContentTextEnd">pdfioContentTextEnd</a></li>
<li><a href="#pdfioContentTextMeasure">pdfioContentTextMeasure</a></li>
<li><a href="#pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></li> <li><a href="#pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></li>
<li><a href="#pdfioContentTextMoveTo">pdfioContentTextMoveTo</a></li> <li><a href="#pdfioContentTextMoveTo">pdfioContentTextMoveTo</a></li>
<li><a href="#pdfioContentTextNextLine">pdfioContentTextNextLine</a></li> <li><a href="#pdfioContentTextNextLine">pdfioContentTextNextLine</a></li>
@ -2005,6 +2006,25 @@ bool pdfioContentTextEnd(<a href="#pdfio_stream_t">pdfio_stream_t</a> *st);</p>
</tbody></table> </tbody></table>
<h4 class="returnvalue">Return Value</h4> <h4 class="returnvalue">Return Value</h4>
<p class="description"><code>true</code> on success, <code>false</code> on failure</p> <p class="description"><code>true</code> on success, <code>false</code> on failure</p>
<h3 class="function"><a id="pdfioContentTextMeasure">pdfioContentTextMeasure</a></h3>
<p class="description">Measure a text string and return its width.</p>
<p class="code">
double pdfioContentTextMeasure(<a href="#pdfio_obj_t">pdfio_obj_t</a> *font, const char *s, double size);</p>
<h4 class="parameters">Parameters</h4>
<table class="list"><tbody>
<tr><th>font</th>
<td class="description">Font object created by <a href="#pdfioFileCreateFontObjFromFile"><code>pdfioFileCreateFontObjFromFile</code></a></td></tr>
<tr><th>s</th>
<td class="description">UTF-8 string</td></tr>
<tr><th>size</th>
<td class="description">Font size/height</td></tr>
</tbody></table>
<h4 class="returnvalue">Return Value</h4>
<p class="description">Width</p>
<h4 class="discussion">Discussion</h4>
<p class="discussion">This function measures the given text string &quot;s&quot; and returns its width based
on &quot;size&quot;. The text string must always use the UTF-8 (Unicode) encoding but
any control characters (such as newlines) are ignored.</p>
<h3 class="function"><a id="pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></h3> <h3 class="function"><a id="pdfioContentTextMoveLine">pdfioContentTextMoveLine</a></h3>
<p class="description">Move to the next line and offset.</p> <p class="description">Move to the next line and offset.</p>
<p class="code"> <p class="code">

View File

@ -7,10 +7,6 @@
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
#include "pdfio-content.h" #include "pdfio-content.h"
#include "ttf.h" #include "ttf.h"
@ -1064,6 +1060,104 @@ pdfioContentTextEnd(pdfio_stream_t *st) // I - Stream
} }
//
// 'pdfioContentTextMeasure()' - Measure a text string and return its width.
//
// This function measures the given text string "s" and returns its width based
// on "size". The text string must always use the UTF-8 (Unicode) encoding but
// any control characters (such as newlines) are ignored.
//
double // O - Width
pdfioContentTextMeasure(
pdfio_obj_t *font, // I - Font object created by @link pdfioFileCreateFontObjFromFile@
const char *s, // I - UTF-8 string
double size) // I - Font size/height
{
const char *subtype; // Font sub-type
ttf_t *ttf = (ttf_t *)_pdfioObjGetExtension(font);
// TrueType font data
ttf_rect_t extents; // Text extents
int ch; // Unicode character
char temp[1024], // Temporary string
*tempptr; // Pointer into temporary string
if ((subtype = pdfioObjGetSubtype(font)) == NULL || strcmp(subtype, "Type0"))
{
// Map non-CP1282 characters to '?', everything else as-is...
tempptr = temp;
while (*s && tempptr < (temp + sizeof(temp) - 3))
{
if ((*s & 0xe0) == 0xc0)
{
// Two-byte UTF-8
ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
s += 2;
}
else if ((*s & 0xf0) == 0xe0)
{
// Three-byte UTF-8
ch = ((s[0] & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
s += 3;
}
else if ((*s & 0xf8) == 0xf0)
{
// Four-byte UTF-8
ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
s += 4;
}
else
{
ch = *s++;
}
if (ch > 255)
{
// Try mapping from Unicode to CP1252...
size_t i; // Looping var
for (i = 0; i < (sizeof(_pdfio_cp1252) / sizeof(_pdfio_cp1252[0])); i ++)
{
if (ch == _pdfio_cp1252[i])
break;
}
if (i >= (sizeof(_pdfio_cp1252) / sizeof(_pdfio_cp1252[0])))
ch = '?'; // Unsupported chars map to ?
}
if (ch < 128)
{
// ASCII
*tempptr++ = ch;
}
else if (ch < 2048)
{
// 2-byte UTF-8
*tempptr++ = 0xc0 | ((ch >> 6) & 0x1f);
*tempptr++ = 0x80 | (ch & 0x3f);
}
else
{
// 3-byte UTF-8
*tempptr++ = 0xe0 | ((ch >> 12) & 0x0f);
*tempptr++ = 0x80 | ((ch >> 6) & 0x3f);
*tempptr++ = 0x80 | (ch & 0x3f);
}
}
*tempptr = '\0';
s = temp;
}
ttfGetExtents(ttf, size, s, &extents);
return (extents.right - extents.left);
}
// //
// 'pdfioContentTextMoveLine()' - Move to the next line and offset. // 'pdfioContentTextMoveLine()' - Move to the next line and offset.
// //
@ -1637,7 +1731,7 @@ pdfioFileCreateFontObjFromFile(
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
ttfDelete(font); _pdfioObjSetExtension(obj, font, (_pdfio_extfree_t)ttfDelete);
return (obj); return (obj);
} }

View File

@ -115,6 +115,7 @@ extern bool pdfioContentSetTextXScaling(pdfio_stream_t *st, double percent) _PD
extern bool pdfioContentStroke(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentStroke(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextBegin(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextBegin(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern bool pdfioContentTextEnd(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextEnd(pdfio_stream_t *st) _PDFIO_PUBLIC;
extern double pdfioContentTextMeasure(pdfio_obj_t *font, const char *s, double size) _PDFIO_PUBLIC;
extern bool pdfioContentTextMoveLine(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC; extern bool pdfioContentTextMoveLine(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC;
extern bool pdfioContentTextMoveTo(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC; extern bool pdfioContentTextMoveTo(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC;
extern bool pdfioContentTextNextLine(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextNextLine(pdfio_stream_t *st) _PDFIO_PUBLIC;

View File

@ -7,10 +7,6 @@
// information. // information.
// //
//
// Include necessary headers...
//
#include "pdfio-private.h" #include "pdfio-private.h"
@ -221,8 +217,13 @@ void
_pdfioObjDelete(pdfio_obj_t *obj) // I - Object _pdfioObjDelete(pdfio_obj_t *obj) // I - Object
{ {
if (obj) if (obj)
{
pdfioStreamClose(obj->stream); pdfioStreamClose(obj->stream);
if (obj->datafree)
(obj->datafree)(obj->data);
}
free(obj); free(obj);
} }
@ -267,6 +268,17 @@ pdfioObjGetDict(pdfio_obj_t *obj) // I - Object
} }
//
// '_pdfioObjGetExtension()' - Get the extension pointer for an object.
//
void * // O - Extension data
_pdfioObjGetExtension(pdfio_obj_t *obj) // I - Object
{
return (obj->data);
}
// //
// 'pdfioObjGetGeneration()' - Get the object's generation number. // 'pdfioObjGetGeneration()' - Get the object's generation number.
// //
@ -498,6 +510,21 @@ pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
} }
//
// '_pdfioObjSetExtension()' - Set extension data for an object.
//
void
_pdfioObjSetExtension(
pdfio_obj_t *obj, // I - Object
void *data, // I - Data
_pdfio_extfree_t datafree) // I - Free function
{
obj->data = data;
obj->datafree = datafree;
}
// //
// 'write_obj_header()' - Write the object header... // 'write_obj_header()' - Write the object header...
// //

View File

@ -92,6 +92,9 @@
# define PDFIO_MAX_DEPTH 32 // Maximum nesting depth for values # define PDFIO_MAX_DEPTH 32 // Maximum nesting depth for values
typedef void (*_pdfio_extfree_t)(void *);
// Extension data free function
typedef enum _pdfio_mode_e // Read/write mode typedef enum _pdfio_mode_e // Read/write mode
{ {
_PDFIO_MODE_READ, // Read a PDF file _PDFIO_MODE_READ, // Read a PDF file
@ -287,6 +290,8 @@ struct _pdfio_obj_s // Object
size_t stream_length; // Length of stream, if any size_t stream_length; // Length of stream, if any
_pdfio_value_t value; // Dictionary/number/etc. value _pdfio_value_t value; // Dictionary/number/etc. value
pdfio_stream_t *stream; // Open stream, if any pdfio_stream_t *stream; // Open stream, if any
void *data; // Extension data, if any
_pdfio_extfree_t datafree; // Free callback for extension data
}; };
struct _pdfio_stream_s // Stream struct _pdfio_stream_s // Stream
@ -365,7 +370,9 @@ extern off_t _pdfioFileTell(pdfio_file_t *pdf) _PDFIO_INTERNAL;
extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) _PDFIO_INTERNAL; extern bool _pdfioFileWrite(pdfio_file_t *pdf, const void *buffer, size_t bytes) _PDFIO_INTERNAL;
extern void _pdfioObjDelete(pdfio_obj_t *obj) _PDFIO_INTERNAL; extern void _pdfioObjDelete(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern void *_pdfioObjGetExtension(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern bool _pdfioObjLoad(pdfio_obj_t *obj) _PDFIO_INTERNAL; extern bool _pdfioObjLoad(pdfio_obj_t *obj) _PDFIO_INTERNAL;
extern void _pdfioObjSetExtension(pdfio_obj_t *obj, void *data, _pdfio_extfree_t datafree) _PDFIO_INTERNAL;
extern pdfio_stream_t *_pdfioStreamCreate(pdfio_obj_t *obj, pdfio_obj_t *length_obj, pdfio_filter_t compression) _PDFIO_INTERNAL; extern pdfio_stream_t *_pdfioStreamCreate(pdfio_obj_t *obj, pdfio_obj_t *length_obj, pdfio_filter_t compression) _PDFIO_INTERNAL;
extern pdfio_stream_t *_pdfioStreamOpen(pdfio_obj_t *obj, bool decode) _PDFIO_INTERNAL; extern pdfio_stream_t *_pdfioStreamOpen(pdfio_obj_t *obj, bool decode) _PDFIO_INTERNAL;

View File

@ -47,7 +47,9 @@ _pdfioFileSeek
_pdfioFileTell _pdfioFileTell
_pdfioFileWrite _pdfioFileWrite
_pdfioObjDelete _pdfioObjDelete
_pdfioObjGetExtension
_pdfioObjLoad _pdfioObjLoad
_pdfioObjSetExtension
_pdfioStreamCreate _pdfioStreamCreate
_pdfioStreamOpen _pdfioStreamOpen
_pdfioStringIsAllocated _pdfioStringIsAllocated
@ -136,6 +138,7 @@ pdfioContentSetTextXScaling
pdfioContentStroke pdfioContentStroke
pdfioContentTextBegin pdfioContentTextBegin
pdfioContentTextEnd pdfioContentTextEnd
pdfioContentTextMeasure
pdfioContentTextMoveLine pdfioContentTextMoveLine
pdfioContentTextMoveTo pdfioContentTextMoveTo
pdfioContentTextNextLine pdfioContentTextNextLine

View File

@ -2242,6 +2242,7 @@ write_font_test(
char title[256], // Page title char title[256], // Page title
textname[256], // Name of text font textname[256], // Name of text font
*ptr; // Pointer into name *ptr; // Pointer into name
double width; // Text width
int i; // Looping var int i; // Looping var
static const char * const welcomes[] =// "Welcome" in many languages static const char * const welcomes[] =// "Welcome" in many languages
{ {
@ -2463,8 +2464,8 @@ write_font_test(
else else
goto error; goto error;
fputs("pdfioContentTextMoveTo(36.0, 702.0): ", stdout); fputs("pdfioContentTextMoveTo(198.0, 702.0): ", stdout);
if (pdfioContentTextMoveTo(st, 36.0, 702.0)) if (pdfioContentTextMoveTo(st, 198.0, 702.0))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -2473,18 +2474,36 @@ write_font_test(
{ {
if (i > 0 && (i % 50) == 0) if (i > 0 && (i % 50) == 0)
{ {
fputs("pdfioContentTextMoveTo(200.0, 600.0): ", stdout); fputs("pdfioContentTextMoveTo(162.0, 600.0): ", stdout);
if (pdfioContentTextMoveTo(st, 200.0, 600.0)) if (pdfioContentTextMoveTo(st, 162.0, 600.0))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
} }
printf("pdfioContentTextMeasure(\"%s\"): ", welcomes[i]);
if ((width = pdfioContentTextMeasure(textfont, welcomes[i], 10.0)) >= 0.0)
puts("PASS");
else
return (1);
printf("pdfioContextTextMoveTo(%g, 0.0): ", -width);
if (pdfioContentTextMoveTo(st, -width, 0.0))
puts("PASS");
else
return (1);
printf("pdfioContentTextShowf(\"%s\"): ", welcomes[i]); printf("pdfioContentTextShowf(\"%s\"): ", welcomes[i]);
if (pdfioContentTextShowf(st, unicode, "%s\n", welcomes[i])) if (pdfioContentTextShowf(st, unicode, "%s\n", welcomes[i]))
puts("PASS"); puts("PASS");
else else
return (1); return (1);
printf("pdfioContextTextMoveTo(%g, 0.0): ", width);
if (pdfioContentTextMoveTo(st, width, 0.0))
puts("PASS");
else
return (1);
} }
fputs("pdfioContentTextEnd(): ", stdout); fputs("pdfioContentTextEnd(): ", stdout);