Save work on string measure support (Issue #17)

This commit is contained in:
Michael R Sweet 2023-12-04 18:54:33 -05:00
parent 86281750e5
commit 4919783da5
No known key found for this signature in database
GPG Key ID: BE67C75EC81F3244
4 changed files with 52 additions and 8 deletions

View File

@ -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.
//

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 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;

View File

@ -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...
//

View File

@ -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;