I am writing a function that draws text on a canvas. The function supports alignment vertical and horizontal, and also text orientation. My problem is that can't calculate proper alignment when text is oriented. This is the header:
procedure drawText(canvas: TCanvas; pos: TPoint; Text: string;
FontName: TFontName; FontSize: integer; FontColor: TColor; Angle: integer;
Halign: THorizontalAlignement; Valign: TVerticalAlignement);
Halign
can be left, right or center, Valign
can be top, bottom or center.
Everything works well for a simple non oriented text with:
h := TextWidth(Text);
case Halign of
haLeft: // do nothing;
;
haRight: x := x - h;
haCenter: x := x - ( h div 2 );
end;
v := TextHeight(Text);
case Valign of
vaTop: // do nothing;
;
vaBottom: y := y - v;
vaCenter: y := y - ( v div 2 );
end;
Font.Orientation := Angle;
textOut(x, y, Text );
I have made many attempts to determine what goes where, and I have managed to position vertical text according to its alignment parameters, but the horizontal one was misplaced.
I know it is related to orientation and width and height, but i cannot figure out properly how to deal with it.
Example when calling the procedure for the horitontal rule:
drawText( bmp.canvas, point( x, viewOption.padding - DocumentRuleTextMargin),
inttoStr( x ), 'arial', 8, clBLack, 0, haCenter, vaBottom );
Calling the procedure for the Vertical rule (the one who is annoying): drawText( bmp.canvas, Point( x - CDocumentRuleTextMargin, y ), inttostr( y ), 'arial', 8, clBlack, 900, haCenter, vaBottom);
here is the result :
i tried to get rid of this by modifying the signs in the calculation of the y position of the procedure like this :
v := TextHeight(Text);
case Valign of
vaTop: // do nothing;
;
vaBottom: y := y + v;
vaCenter: y := y + ( v div 2 );
end;
and the result is better for vertical rule, while worst for horizontal one :