diff --git a/pdfio-content.c b/pdfio-content.c index c3edd1e..7c591b9 100644 --- a/pdfio-content.c +++ b/pdfio-content.c @@ -7,10 +7,6 @@ // information. // -// -// Include necessary headers... -// - #include "pdfio-private.h" #include "pdfio-content.h" #include "ttf.h" @@ -1064,6 +1060,27 @@ pdfioContentTextEnd(pdfio_stream_t *st) // I - Stream } +// +// 'pdfioContextTextMeasure()' - Measure a text string and return its width. +// + +double // O - Width +pdfioContextTextMeasure( + pdfio_obj_t *font, // I - Font object created by @link pdfioFileCreateFontObjFromFile@ + const char *s, // I - UTF-8 string + double size) // I - Font size/height +{ + ttf_t *ttf = (ttf_t *)_pdfioObjGetExtension(font); + // TrueType font data + ttf_rect_t extents; // Text extents + + + ttfGetExtents(ttf, size, s, &extents); + + return (extents.right - extents.left); +} + + // // 'pdfioContentTextMoveLine()' - Move to the next line and offset. // diff --git a/pdfio-content.h b/pdfio-content.h index f5f11ac..deb9341 100644 --- a/pdfio-content.h +++ b/pdfio-content.h @@ -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 pdfioContentTextBegin(pdfio_stream_t *st) _PDFIO_PUBLIC; extern bool pdfioContentTextEnd(pdfio_stream_t *st) _PDFIO_PUBLIC; +extern double pdfioContextTextMeasure(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 pdfioContentTextMoveTo(pdfio_stream_t *st, double tx, double ty) _PDFIO_PUBLIC; extern bool pdfioContentTextNextLine(pdfio_stream_t *st) _PDFIO_PUBLIC; diff --git a/pdfio-object.c b/pdfio-object.c index fe10c56..baa62f3 100644 --- a/pdfio-object.c +++ b/pdfio-object.c @@ -7,10 +7,6 @@ // information. // -// -// Include necessary headers... -// - #include "pdfio-private.h" @@ -267,6 +263,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. // @@ -498,6 +505,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 + void (*datafree)(void *)) // I - Free function +{ + obj->data = data; + obj->datafree = datafree; +} + + // // 'write_obj_header()' - Write the object header... // diff --git a/pdfio-private.h b/pdfio-private.h index 67b7ba6..92b002b 100644 --- a/pdfio-private.h +++ b/pdfio-private.h @@ -287,6 +287,8 @@ 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 + void *data; // Extension data, if any + void (*datafree)(void *); // Free callback for extension data }; struct _pdfio_stream_s // Stream @@ -365,7 +367,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 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 void _pdfioObjSetExtension(pdfio_obj_t *obj, void *data, void (*datafree)(void *)) _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;