Skip to content

Commit 48b9509

Browse files
authored
Merge branch 'dev-2.0' into refactor/strands-binary-logical-dedup
2 parents 324c592 + bbfd9ab commit 48b9509

8 files changed

Lines changed: 71 additions & 12 deletions

File tree

src/core/p5.Renderer3D.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export class Renderer3D extends Renderer {
160160

161161
// clipping
162162
this._clipDepths = [];
163+
this._textContextSavedStack = [];
163164
this._isClipApplied = false;
164165
this._stencilTestOn = false;
165166

@@ -1327,13 +1328,25 @@ export class Renderer3D extends Renderer {
13271328
return this;
13281329
}
13291330

1331+
push() {
1332+
super.push()
1333+
const saved = !!(this.states.textFont?.font);
1334+
if (saved) {
1335+
this.textDrawingContext().save()
1336+
}
1337+
this._textContextSavedStack.push(saved);
1338+
}
1339+
13301340
pop(...args) {
13311341
if (
13321342
this._clipDepths.length > 0 &&
13331343
this._pushPopDepth === this._clipDepths[this._clipDepths.length - 1]
13341344
) {
13351345
this._clearClip();
13361346
}
1347+
if (this._textContextSavedStack.pop()) {
1348+
this.textDrawingContext().restore()
1349+
}
13371350
super.pop(...args);
13381351
this._applyStencilTestIfClipping();
13391352
}

src/type/textCore.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,13 +1587,6 @@ function textCore(p5, fn) {
15871587
if (typeof weight === 'number') {
15881588
this.states.setValue('fontWeight', weight);
15891589
this._applyTextProperties();
1590-
1591-
// Safari works without weight set in the canvas style attribute, and actually
1592-
// has buggy behavior if it is present, using the wrong weight when drawing
1593-
// multiple times with different weights
1594-
if (!p5.prototype._isSafari()) {
1595-
this._setCanvasStyleProperty('font-variation-settings', `"wght" ${weight}`);
1596-
}
15971590
return;
15981591
}
15991592
// the getter

src/webgl/3d_primitives.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,33 +1792,38 @@ function primitives3D(p5, fn){
17921792
const prevMode = this.states.textureMode;
17931793
this.states.setValue('textureMode', constants.NORMAL);
17941794
const prevOrder = this.bezierOrder();
1795-
this.bezierOrder(2);
1795+
this.bezierOrder(3);
17961796
this.beginShape();
17971797
const addUVs = (x, y) => [x, y, 0, (x - x1)/width, (y - y1)/height];
1798+
const rr = 0.5523; // kappa: 4*(sqrt(2)-1)/3, handle ratio for cubic bezier circle approximation
17981799
if (tr !== 0) {
17991800
this.vertex(...addUVs(x2 - tr, y1));
1800-
this.bezierVertex(...addUVs(x2, y1));
1801+
this.bezierVertex(...addUVs(x2 - tr + tr * rr, y1));
1802+
this.bezierVertex(...addUVs(x2, y1 + tr - tr * rr));
18011803
this.bezierVertex(...addUVs(x2, y1 + tr));
18021804
} else {
18031805
this.vertex(...addUVs(x2, y1));
18041806
}
18051807
if (br !== 0) {
18061808
this.vertex(...addUVs(x2, y2 - br));
1807-
this.bezierVertex(...addUVs(x2, y2));
1809+
this.bezierVertex(...addUVs(x2, y2 - br + br * rr));
1810+
this.bezierVertex(...addUVs(x2 - br + rr * br, y2));
18081811
this.bezierVertex(...addUVs(x2 - br, y2));
18091812
} else {
18101813
this.vertex(...addUVs(x2, y2));
18111814
}
18121815
if (bl !== 0) {
18131816
this.vertex(...addUVs(x1 + bl, y2));
1814-
this.bezierVertex(...addUVs(x1, y2));
1817+
this.bezierVertex(...addUVs(x1 + bl - bl * rr, y2));
1818+
this.bezierVertex(...addUVs(x1, y2 - bl + bl * rr));
18151819
this.bezierVertex(...addUVs(x1, y2 - bl));
18161820
} else {
18171821
this.vertex(...addUVs(x1, y2));
18181822
}
18191823
if (tl !== 0) {
18201824
this.vertex(...addUVs(x1, y1 + tl));
1821-
this.bezierVertex(...addUVs(x1, y1));
1825+
this.bezierVertex(...addUVs(x1, y1 + tl - tl * rr));
1826+
this.bezierVertex(...addUVs(x1 + tl - tl * rr, y1));
18221827
this.bezierVertex(...addUVs(x1 + tl, y1));
18231828
} else {
18241829
this.vertex(...addUVs(x1, y1));

test/unit/visual/cases/webgl.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,17 @@ visualSuite('WebGL', function() {
15061506
});
15071507
});
15081508

1509+
visualSuite('2D Shapes', function() {
1510+
visualTest('rect() rounded into a circle', function(p5, screenshot) {
1511+
p5.createCanvas(50, 50, p5.WEBGL);
1512+
p5.background(255);
1513+
p5.noStroke();
1514+
p5.fill('red');
1515+
p5.rect(-20, -20, 40, 40, 20);
1516+
screenshot();
1517+
});
1518+
});
1519+
15091520
visualSuite('3D Primitives', function() {
15101521
visualTest('cylinder() renders correctly', function(p5, screenshot) {
15111522
p5.createCanvas(100, 100, p5.WEBGL);
612 Bytes
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
-53 Bytes
Loading

test/unit/webgl/p5.RendererGL.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,4 +2998,38 @@ suite('p5.RendererGL', function() {
29982998
expect(myp5.get(5, 5)).toEqual([255, 0, 0, 255]);
29992999
});
30003000
});
3001+
3002+
suite('fontWidth', function() {
3003+
test('respects textSize changes across push/pop', async function() {
3004+
myp5.createCanvas(100, 100, myp5.WEBGL);
3005+
const font = await myp5.loadFont('test/unit/assets/acmesa.ttf');
3006+
3007+
myp5.push();
3008+
myp5.textFont(font);
3009+
myp5.textSize(12);
3010+
myp5.push();
3011+
myp5.textSize(20);
3012+
const widthAt20 = myp5.fontWidth('X');
3013+
myp5.pop();
3014+
const widthAt12 = myp5.fontWidth('X');
3015+
myp5.pop();
3016+
3017+
expect(widthAt20).toBeGreaterThan(widthAt12);
3018+
});
3019+
3020+
test('fontWidth restores correctly when font is unset inside push/pop', async function() {
3021+
myp5.createCanvas(100, 100, myp5.WEBGL);
3022+
const font = await myp5.loadFont('test/unit/assets/acmesa.ttf');
3023+
myp5.textFont(font);
3024+
myp5.textSize(12);
3025+
myp5.push();
3026+
myp5.textFont('sans-serif'); // unset loaded font
3027+
myp5.pop();
3028+
// After pop, should be back to size 12 with loaded font
3029+
const widthAfterPop = myp5.fontWidth('X');
3030+
myp5.textSize(20);
3031+
const widthAt20 = myp5.fontWidth('X');
3032+
expect(widthAfterPop).toBeLessThan(widthAt20);
3033+
});
3034+
});
30013035
});

0 commit comments

Comments
 (0)