I am trying to draw text in the center of a box using SKIA and the values I'm getting for the measurement don't seem to be right.
The image on the left is pure Skia -- the image on the right is an autosizing Tlabel set to use the same font/size etc. and which is then centered by its reported dimensions. As you can see it is centered correctly while the skia one isn't .
procedure TForm41.SkPaintBox1Draw(ASender: TObject; const ACanvas: ISkCanvas;
const ADest: TRectF; const AOpacity: Single);
var
Paint:ISKPaint;
LFont1:ISkFont;
LPaint1:ISkPaint;
LBlob1,LBlob2:ISkTextBlob;
LTypeface:ISkTypeface;
TargetBoundsRect:TRectF;
LocalScale:TPointF;
TextToDraw:String;
begin
LPaint1 := TSkPaint.Create;
LPaint1.AntiAlias := True;
LPaint1.SetColorF(TAlphaColorF.Create(TALphaColorRec.Blue));
//draw frame around the destination rectangles
ACanvas.DrawLine(ADest.TopLeft,PointF(0,ADest.Height),LPaint1);
ACanvas.DrawLine(ADest.TopLeft,PointF(ADest.Width,ADest.Top),LPaint1);
ACanvas.DrawLine(ADest.TopLeft,PointF(0,ADest.Height),LPaint1);
ACanvas.DrawLine(PointF(ADest.Left,ADest.Height),ADest.BottomRight,LPaint1);
ACanvas.DrawLine(PointF(ADest.Width,ADest.Height),PointF(ADest.Width,ADest.Top),LPaint1);
//create the font setup
LTypeface := TSkTypeface.MakeFromName('Sans',tskFontStyle.Bold);
LFont1 := TSkFont.Create(LTypeface, 18, 1);
LFont1.Edging := TSkFontEdging.AntiAlias;
TargetBoundsRect:=ADest;
TextToDraw:='Skia Rules';
if not (ASender = SkPaintBox2) then
begin
//if we are on the first box then lets measure the text using skia code
LFont1.MeasureText(TextToDraw,TargetBoundsRect,LPaint1);
LBlob1 := TSkTextBlob.Make(TextToDraw, LFont1);
//draw the text centered based on its reported dimensions
ACanvas.DrawTextBlob(LBlob1, (ADest.Width-TargetBoundsRect.Width)/2,(ADest.Height
- TargetBoundsRect.Height)/2, LPaint1);
end;
//make a smaller font
LFont1 := TSkFont.Create(LTypeface, 12, 1);
LFont1.Edging := TSkFontEdging.AntiAlias;
if ASender = SkPaintBox2 then
begin
//Since we are on the second box then we want to center the label based on its reported size
Label1.SetBounds(ADest.Left+(ADest.Width-Label1.Width)/2,
ADest.Top+(ADest.Height-Label1.Height)/2,Label1.Width,Label1.Height);
//report its bounds
LBlob2 := TSkTextBlob.Make('Reported Bounds: '+FloatToStr(Label1.width)+'w vs '+
FloatToStr(Label1.Height)+'h', LFont1)
end
else //report the Skia TExt's bounds
LBlob2 := TSkTextBlob.Make('Reported Bounds: '+FloatToStr(TargetBoundsRect.width)+'w vs '+
FloatToStr(TargetBoundsRect.Height)+'h', LFont1);
ACanvas.DrawTextBlob(LBlob2, 2,(ADest.Height-18), LPaint1);
end;