Can cairo_surface_set_device_scale double‑scale when CTM already includes scaling?
0 reputation · 13 Jun 2021, 21:13 UTC
0 reputation · 13 Jun 2021, 21:13 UTC
To render a high‑DPI image surface in Cairo while avoiding unintended double scaling when the current transformation matrix (CTM) already applies a scale factor.
Device scale is a surface‑level property that maps logical user coordinates to device pixels. It composes with the CTM, so if the CTM contains a scaling transformation, the effective pixel density may be multiplied. The Cairo documentation does not explicitly state whether the device scale is applied before or after the CTM, and some backends (e.g., PDF, SVG) treat device scale differently because they are resolution‑independent.
Should applications query the display’s scale factor and set it on each image surface, or should the toolkit (GTK, Qt, etc.) handle scaling automatically? Mixing the two approaches can lead to conflicting scaling assumptions.
cairo_surface_set_device_scale() apply before the CTM scaling, potentially causing double scaling when the CTM already includes a scale factor?35275 reputation · 13 Jun 2021, 23:10 UTC
Yes. When cairo_surface_set_device_scale() is called after the current transformation matrix (CTM) already contains a scaling factor, the two scale factors compose multiplicatively. The overall mapping from logical user coordinates to device pixels becomes CTM_scale × device_scale. If both are set to 2×, the effective pixel density is 4×, which appears as double scaling relative to the intended display scaling.
Cairo applies transformations in this sequence: user space → current transformation matrix (CTM) → device space → device scale → pixel space. The device scale is a surface‑level multiplier that sits after the CTM. This means any scaling already present in the CTM will be multiplied by the device scale value you set.
1.0 (or omit the call) so the overall effect remains just the CTM scaling.cairo_surface_set_device_scale(), compensate by adjusting the CTM (e.g., remove the extra scale factor) so the product stays at the intended level.1.0, especially if the same surface will later be used for unscaled output.Which Cairo backend are you using? (e.g., image surface, PDF, SVG, PS) – the device scale’s effect on final output is backend‑specific, and the recommendation to compensate or adjust depends on this choice.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.