Recent

Author Topic: [SOLVED] Canvas2D does not scale text position  (Read 3579 times)

toslan

  • New Member
  • *
  • Posts: 10
[SOLVED] Canvas2D does not scale text position
« on: March 10, 2018, 03:11:57 pm »
I assume that scale(factor) method should be the simplest way to resize the whole canvas. In fact it is not true when it contains paths and text at the same time.
The problem was mentioned in https://forum.lazarus.freepascal.org/index.php/topic,36284.0.html but still not solved. Here is another example.
Code: Pascal  [Select][+][-]
  1. procedure txt();
  2. begin
  3.   with bmp.Canvas2D do begin
  4.     fillStyle ('rgb(0,0,0)');
  5.     fontEmHeight:=50;
  6.     FillText('Text',50,50);
  7.   end;
  8. end;
  9.  
  10. procedure rct();
  11. begin
  12.   with bmp.Canvas2D do begin
  13.     fillStyle ('rgb(255,0,0)');
  14.     FillRect(50,50,50,50);
  15.   end;
  16. end;
  17.  
  18. procedure TForm2.FormPaint(Sender: TObject);
  19. var c:single;
  20. begin
  21.   bmp.SetSize(ClientWidth,ClientHeight);
  22.   bmp.Fill(BGRA(210,210,210));
  23.     with bmp.Canvas2D do begin
  24.       rct;
  25.       txt;
  26.     end;
  27.   bmp.Draw(Canvas,0,0);
  28. end;
  29.  
  30. procedure TForm2.FormShow(Sender: TObject);
  31. begin
  32.   bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,BGRA(210,210,210));
  33.   bmp.fontRenderer:=TBGRAVectorizedFontRenderer.Create;
  34.   bmp.Canvas2D.save;
  35. end;
  36.  
The result is shown on the 1st picture and is quite expected.
Next I scaled the canvas with a factor of 2 before placing rect and text on it:
Code: Pascal  [Select][+][-]
  1. procedure TForm2.FormPaint(Sender: TObject);
  2. var c:single;
  3. begin
  4.   bmp.SetSize(ClientWidth,ClientHeight);
  5.   bmp.Fill(BGRA(210,210,210));
  6.     with bmp.Canvas2D do begin
  7.       scale(2);
  8.       rct;
  9.       txt;
  10.     end;
  11.   bmp.Draw(Canvas,0,0);
  12. end;
  13.  
The red square is changed correctly: the size is doubled and the starting point is moved accordingly (x and y are doubled).
The text size is doubled as well. However its staring point is not changed. As a result the whole picture is distorted.
Is it a bug or deliberate behavior?
« Last Edit: April 02, 2018, 02:45:55 pm by toslan »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Canvas2D does not scale text position
« Reply #1 on: March 11, 2018, 09:53:55 pm »
Hi, please report it here (a link to this post is enough)
https://github.com/bgrabitmap/bgrabitmap/issues

toslan

  • New Member
  • *
  • Posts: 10
Re: Canvas2D does not scale text position
« Reply #2 on: March 11, 2018, 10:39:20 pm »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Canvas2D does not scale text position
« Reply #3 on: March 11, 2018, 10:49:03 pm »
Thanks. I received the notification. Is @circular the one that mantains BGRABitmap, he sure is notified also. For me is like encrypted code to see the BGRA sources.

toslan

  • New Member
  • *
  • Posts: 10
Re: Canvas2D does not scale text position
« Reply #4 on: March 12, 2018, 09:07:58 am »
From the code it seems that scaling transformation should somehow touch x and y coordinates in TBGRACanvas2D.Text() method. But it does not. Hope @circular clarify. 

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: Canvas2D does not scale text position
« Reply #5 on: March 31, 2018, 09:05:19 pm »
Hi. I applied a fix on the dev-bgrabitmap branch.

I would say it solves the problem. What do you think?
Conscience is the debugger of the mind

toslan

  • New Member
  • *
  • Posts: 10
Re: Canvas2D does not scale text position
« Reply #6 on: April 02, 2018, 02:45:05 pm »
Hi circular.
Now it works as expected. Thank you.

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: [SOLVED] Canvas2D does not scale text position
« Reply #7 on: April 02, 2018, 04:56:32 pm »
You're welcome.  :)
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018