Recent

Author Topic: Retina / High DPI TBitmap  (Read 6829 times)

ps

  • Full Member
  • ***
  • Posts: 136
    • CSS
Retina / High DPI TBitmap
« on: July 03, 2016, 08:45:40 am »
How to handle high DPI TBitmap for MacOS X?

I have problem with TBitmap see attachment.
Background is rendered on TBitmap with size of component. Text is painted with Canvas.TextOut(X,Y, 'Caption') and it's well rendered (except italics style). TBitmap size is same as component (TBitmap.Width := Self.Width) but in real it's half "real" width and height and result is uggly output. Target is Cocoa (under Windows target is all OK and TBitmap has real width and height 1px=1px).

« Last Edit: July 03, 2016, 08:47:32 am by ps »
Small simple CSS/box model implementation: https://github.com/pst2d/csscontrols/tree/dev

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Retina / High DPI TBitmap
« Reply #1 on: December 13, 2017, 09:35:32 pm »
The same happens with BGRABitmap. So I see that this needs to be fixed for all kind of bitmaps. BGRABitmap uses TBitmap AFAIK.

Edit: Did not find a bug report, so I did one https://bugs.freepascal.org/view.php?id=32816
« Last Edit: December 13, 2017, 09:45:59 pm by lainz »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Retina / High DPI TBitmap
« Reply #2 on: June 13, 2020, 04:19:22 pm »
The bitmap needs to be rendered with the real resolution and drawn stretched.
- You can determine the scaling factor with the function GetCanvasScaleFactor of the control on which you want to draw.
- Then draw the bitmap with StretchDraw
See: https://wiki.lazarus.freepascal.org/Cocoa_DPI

With BGRABitmap, you can render the stretched bitmap like this:
Code: Pascal  [Select][+][-]
  1. uses BGRABitmap, BGRABitmapTypes;
  2.  
  3. { TForm1 }
  4.  
  5. procedure TForm1.FormPaint(Sender: TObject);
  6. var
  7.   bmp: TBGRABitmap;
  8. begin
  9.   bmp := TBGRABitmap.Create(
  10.     round(ClientWidth*GetCanvasScaleFactor),
  11.     round(ClientHeight*GetCanvasScaleFactor)
  12.     );
  13.   with bmp do
  14.   begin
  15.     FillRect(0,0,bmp.Width,bmp.Height,clRed);
  16.     DrawLineAntialias(0,0,bmp.Width,bmp.Height,clYellow,false);
  17.   end;
  18.   bmp.Draw(Canvas, rect(0,0,ClientWidth,ClientHeight), true);
  19.   bmp.Free;
  20. end;
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Retina / High DPI TBitmap
« Reply #3 on: June 13, 2020, 05:41:43 pm »
Thanks.  :)

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: Retina / High DPI TBitmap
« Reply #4 on: June 13, 2020, 06:44:56 pm »
You're welcome.

In BGRAControls, I have added Retina handling for BGRAVirtualScreen, BGRAGraphicControl and SVGViewer. Do the change make sense to you?

Note it will need dev branch of BGRABitmap.
Conscience is the debugger of the mind

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Retina / High DPI TBitmap
« Reply #5 on: June 13, 2020, 06:50:54 pm »
Hi, well I don't have a mac with retina to test, so I can't say, but I trust you did the changes very well  :)

 

TinyPortal © 2005-2018