DynaPDF Manual - Page 606

Previous Page 605   Index   Next Page 607

Function Reference
Page 606 of 839
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
record.
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
follows:
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;
double y1 = 0.0;
double x2 = Width; // Width is a parameter of the callback function
double y2 = 0.0;
// Transform the text matrix to user space
TCTM m = MulMatrix(m_GState.Matrix, *Matrix);
Transform(m, x1, y1); // Start point of the text record
 

Previous topic: Text Coordinates and Metrics, Font Size

Next topic: Character Spacing, Word Spacing