Xojo Conferences

DynaPDF Manual - Page 535

Previous Page 534   Index   Next Page 536

Function Reference
Page 535 of 750
Text Coordinates and Metrics
As mentioned earlier text coordinates are defined in text space. The transformation from text space
to user space is achieved by multiplying the text matrix with the one of the current graphics state.
However, the graphics state contains several text related parameters which require some further
explanation. The following sub-clauses describe in detail in which coordinate space these parameters
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

Previous topic: Helper Functions

Next topic: Word Spacing