Skip to content

Commit

Permalink
Save work on string measure support (Issue #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Dec 4, 2023
1 parent 8628175 commit 4919783
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
25 changes: 21 additions & 4 deletions pdfio-content.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
// information.
//

//
// Include necessary headers...
//

#include "pdfio-private.h"
#include "pdfio-content.h"
#include "ttf.h"
Expand Down Expand Up @@ -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.
//
Expand Down
1 change: 1 addition & 0 deletions pdfio-content.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 26 additions & 4 deletions pdfio-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
// information.
//

//
// Include necessary headers...
//

#include "pdfio-private.h"


Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -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...
//
Expand Down
4 changes: 4 additions & 0 deletions pdfio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4919783

Please sign in to comment.