Skip to content

Commit

Permalink
dom: convert Font point size to pixel size in canvas rendering to tak…
Browse files Browse the repository at this point in the history
…e into account retina displays
  • Loading branch information
briansfrank committed Dec 30, 2024
1 parent fed3f87 commit fef33fe
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 406 deletions.
21 changes: 14 additions & 7 deletions src/dom/es/CanvasGraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ class CanvasGraphics extends sys.Obj {
{
const cx = canvas.peer.elem.getContext("2d");
const g = new CanvasGraphics();
if (!canvas.peer.__inited)
{
// first time thru scale by half a pixel to avoid blurry lines
canvas.peer.__inited = true;
cx.translate(0.5, 0.5);
}
g.cx = cx;
cb(g);
}
Expand Down Expand Up @@ -88,8 +82,13 @@ class CanvasGraphics extends sys.Obj {
{
if (it === undefined) return this.#font;

// convert the font point size to a pixel size CSS string; we assume
// rendering using devicePixelRatio for crisp fonts on retina displays
const dpr = window.devicePixelRatio || 1;
const str = it.toPxSizeCss(dpr);

this.#font = it;
this.cx.font = it.toStr();
this.cx.font = str;
}

// FontMetrics metrics()
Expand Down Expand Up @@ -156,6 +155,14 @@ class CanvasGraphics extends sys.Obj {
return this;
}

// This clipRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)
clipRoundRect(x, y, w, h, wArc, hArc)
{
this.pathRoundRect(x, y, w, h, wArc, hArc);
this.cx.clip();
return this;
}

// generate path for a rounded rectangle
pathRoundRect(x, y, w, h, wArc, hArc)
{
Expand Down
2 changes: 1 addition & 1 deletion src/dom/es/WinPeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class WinPeer extends sys.Obj {

devicePixelRatio(self)
{
return sys.Float.make(this.win.devicePixelRatio);
return sys.Float.make(this.win.devicePixelRatio || 1);
}

#parent;
Expand Down
Loading

0 comments on commit fef33fe

Please sign in to comment.