Xojo Conferences

DynaPDF Manual - Page 550

Previous Page 549   Index   Next Page 551

Function Reference
Page 550 of 770
are defined, how they can be transformed to user space, and how text metrics like the text width
must be interpreted.
Font Size
The current font size, which is a parameter of the graphics state, is defined in text space. The visible
font size is measured in user space, so, the value of the graphics state is not directly usable.
The transformation to user space can only be done in the used text callback function because the
mapping from text space to user space depends on the text matrix which is only available in a text
The font size can be transformed to user space as follows:
// We must first transform the text matrix to user space. The text
// matrix is a parameter of the used text callback function. m_GState
// represents the current graphics state. The function GetScaleY() is
// described at Helper Functions.
TCTM m = MulMatrix(m_GState.Matrix, *Matrix);
double realFontSize = fabs(m_GS.FontSize * GetScaleY(m));
The above code is very simple but can be fairly optimized because the function GetScaleY() causes
some unnecessary overhead. When extracting text from a PDF file, the start point of the text must
always be calculated and the font size is always a positive value. The optimized code looks as
double x1 = 0.0;
double y1 = 0.0;
double x2 = 0.0;
double y2 = m_GState.FontSize;
// Transform the text matrix to user space
TCTM m = MulMatrix(m_GState.Matrix, *Matrix);
Transform(m, x1, y1); // Start point of the text record
Transform(m, x2, y2); // Second point to compute the font size
double realFontSize = CalcDistance(x1, y1, x2, y2);
The main difference to the first code is that the start point of the text record can be used for further
calculations and the real font size is already a positive value. Such optimizations are very important
especially when using more complex algorithms to construct text lines or logically sorted text output.
Text Width
The text width, which is provided in all text callback functions, is calculated in text space. The
provided text width includes already the graphics state parameters Character Spacing, Word
Spacing, and Text Scaling. However, these parameters are still required if the width of a sub string
must be computed. The parameters must be available in the graphics state including the
corresponding callback functions which set the parameters.
The real text width measured in user space can be calculated as follows:
double x1 = 0.0;

Previous topic: Text Coordinates and Metrics

Next topic: Character Spacing, Word Spacing