From cairo Wed Mar 20 12:03:06 2024 From: Emmanuele Bassi Date: Wed, 20 Mar 2024 12:03:06 +0000 To: cairo Subject: Re: Implementing HiDpi scaling Message-Id: X-MARC-Message: https://marc.info/?l=cairo&m=171093606929710 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--0000000000005601400614166079" --0000000000005601400614166079 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable The way "high DPI" support is implemented is typically this: - You retrieve the scaling factor from the windowing system; in the case of X11 you can deduce the scaling factor from the ratio between the width and the height of the target monitor: anything below 192 is generally considered "1" and anything above is generally considered "2". If you want to go into fractional scaling territory, you'll likely need some other mechanism, like a user configuration setting. - Once you have a scaling factor, you create a windowing system surface that has two units: 1. device pixels, which are the physical pixels on the monitor 2. logical pixels, which are the scaled pixels for drawing operations Your surfaces have to be created with the size in device pixels; then you call cairo_surface_set_device_scale() with the scaling factor you computed: https://www.cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surfa= ce-set-device-scale After that, every drawing operation must be in **logical** pixels, and the Cairo will automatically scale every coordinate and size appropriately. It is your responsibility to deal with device and logical coordinates; for instance, windowing system events will be in device coordinates, but if you're exposing their information to other code you will need to scale them appropriately. Ciao, Emmanuele. On Wed, 20 Mar 2024 at 11:47, Federico Angelilli wrote: > Hello, > Sorry for the late reply. > > I am using cairo for drawing and pango for the text. I am creating a > window with xcb and using cairo_xcb_surface. > > I can easily extract the dpi information from the xrandr api calls. Now > that I have this ratio, I have to use it somewhere to scale my canvas > appropriately. > > I will try using cairo_scale, and should I also scale the size of the > xcb_surface by the same ratio? > > Anyway my main concern about antialising is that since I am making layere= d > shapes (think buttons) the border and the background will not match up > pixel perfect (which is something that I have seen in other project using > cairo). > > Does what I described about scaling the surface makes sense or am I > misinterpreting cairo_scale? > > Regards > > Ps: > I will soon try and post the results > > > On March 18, 2024 1:46:54 PM GMT+01:00, Dov Grobgeld < > dov.grobgeld@gmail.com> wrote: > >> I think that you should describe in detail what you expect. >> >> In pure vector graphics a contour may cut through a pixel and partially >> obscure it, and it is then the responsibility of the graphics engine to >> create anti-aliased graphics by mixing the colors of the background and = the >> foreground. As a user you typically don't need to concern yourself with >> that as it is done automatically. >> >> However, e.g. for text this may cause "fuzzy" characters and the engine >> will then fallback to moving the contours and scale the font so that the >> glyphs fall on whole pixels. >> >> Note that the use of anti-aliased graphics is less needed, and less >> visible, for an HDPI device. >> >> In any case all that you will typically need is cairo_scale() as Uli >> Shlachter already described. >> >> Try it! >> >> Regards, >> >> >> On Sun, Mar 17, 2024 at 12:46=E2=80=AFPM Federico Angelilli >> wrote: >> >>> Hi >>> >>> Will cairo_scale make a pixel perfect image for non integer scales? To >>> my understanding the scale matrix will be multiplied to all the coordin= ates >>> scaling the coordinates appropriately. Should I also scale the whole >>> surface by that same factor? >>> >>> Regards >>> >>> >>> On March 16, 2024 6:27:56 PM GMT+01:00, Uli Schlachter >>> wrote: >>> >>>> Hi >>>> >>>> Am 14.03.24 um 16:34 schrieb fedang: >>>> >>>>> Hello all, >>>>> >>>>> I am writing a small X11 app using cairo as the drawing backend. I wa= s wondering if and how to implement >>>>> fractional scaling to accommodate high dpi screens. After searching o= nline I found almost nothing, so here I am. >>>>> >>>>> Can someone give me some pointers on what to do? I can get a scale fa= ctor for the screens (depending on the size) >>>>> but I have no idea how to actually use it in the draw code. >>>>> >>>> >>>> I'm not sure if I understand the question. You can use cairo_scale() t= o set a scale factor for all drawing. So I would say that you can just appl= y the scale factor with cairo_scale() before drawing. >>>> >>>> Of course, this assumes that you don't use cairo_identity_matrix() or = cairo_set_matrix() in your drawing. Instead, cairo_save() and cairo_restore= () should be used around temporary modifications of the transformations. >>>> >>>> Cheers, >>>> Uli >>>> >>>> --=20 https://www.bassi.io [@] ebassi [@gmail.com] --0000000000005601400614166079 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
The way "high DPI" support is implemented i= s typically this:

- You retrieve the scaling facto= r from the windowing system; in the case of X11 you can deduce the scaling = factor from the ratio between the width and the height of the target monito= r: anything below 192 is generally considered "1" and anything ab= ove is generally considered "2". If you want to go into fractiona= l scaling territory, you'll likely need some other mechanism, like a us= er configuration setting.
- Once you have a scaling factor, y= ou create a windowing system surface that has two units:
=C2=A0 1= . device pixels, which are the physical pixels on the monitor
=C2= =A0 2. logical pixels, which are the scaled pixels for drawing operations

Your surfaces have to be created with the size in d= evice pixels; then you call cairo_surface_set_device_scale() with the scali= ng factor you computed:


After that, every dr= awing operation must be in **logical** pixels, and the Cairo will automatic= ally scale every coordinate and size appropriately.

It is your responsibility to deal with device and logical coordinates; fo= r instance, windowing system events will be in device coordinates, but if y= ou're exposing their information to other code you will need to scale t= hem appropriately.

Ciao,
=C2=A0Emman= uele.

On Wed, 20 Mar 2024 at 11:47, Federico Angelilli <list@fedang.net> wrote:
Hello,Sorry for the late reply.

I am using cairo for drawing and pango f= or the text. I am creating a window with xcb and using cairo_xcb_surface.
I can easily extract the dpi information from the xrandr api calls. N= ow that I have this ratio, I have to use it somewhere to scale my canvas ap= propriately.

I will try using cairo_scale, and should I also scale t= he size of the xcb_surface by the same ratio?

Anyway my main concern= about antialising is that since I am making layered shapes (think buttons)= the border and the background will not match up pixel perfect (which is so= mething that I have seen in other project using cairo).

Does what I = described about scaling the surface makes sense or am I misinterpreting cai= ro_scale?

Regards

Ps:
I will soon try and post the results=


On March 18= , 2024 1:46:54 PM GMT+01:00, Dov Grobgeld <dov.grobgeld@gmail.com> wrote:
<= blockquote class=3D"gmail_quote" style=3D"margin:0pt 0pt 0pt 0.8ex;border-l= eft:1px solid rgb(204,204,204);padding-left:1ex">
I think that you should describe in detail what you exp= ect.

In pure vector graphics a contour may cut through= a pixel and partially obscure it, and it is then the responsibility of the= graphics engine to create anti-aliased graphics by mixing the colors of th= e background and the foreground. As a user you typically don't need to = concern yourself with that as it is done automatically.

<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ">However, e.g. for text this may cause "fuzzy" characters and th= e engine will then fallback to moving the contours and scale the font so th= at the glyphs fall on whole pixels.

Note that the use = of anti-aliased graphics is less needed, and less visible, for an HDPI devi= ce.

In any case all that you will typically need is ca= iro_scale() as Uli Shlachter already described.

T= ry it!

Regards,


On Sun, Mar 17, 2024= at 12:46=E2=80=AFPM Federico Angelilli <list@fedang.net> wrote:
Hi

Will = cairo_scale make a pixel perfect image for non integer scales? To my unders= tanding the scale matrix will be multiplied to all the coordinates scaling = the coordinates appropriately. Should I also scale the whole surface by tha= t same factor?

Regards


On March 16, 2024 6:27:56 PM GMT+01:00, Uli Schlachter <psychon@znc.in> wro= te:
Hi

Am 14.03.24 um 16:34 schrieb fedang:
Hel= lo all,

I am writing a small X11 app using cairo as the drawing back= end. I was wondering if and how to implement
fractional scaling to accom= modate high dpi screens. After searching online I found almost nothing, so = here I am.

Can someone give me some pointers on what to do? I can ge= t a scale factor for the screens (depending on the size)
but I have no i= dea how to actually use it in the draw code.

I'm not sure if I understand the question. You can use ca= iro_scale() to set a scale factor for all drawing. So I would say that you = can just apply the scale factor with cairo_scale() before drawing.

O= f course, this assumes that you don't use cairo_identity_matrix() or ca= iro_set_matrix() in your drawing. Instead, cairo_save() and cairo_restore()= should be used around temporary modifications of the transformations.
<= br>Cheers,
Uli


--
--0000000000005601400614166079--