File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
@@ -1329,9 +1330,11 @@ export class Renderer3D extends Renderer {
13291330
13301331 push ( ) {
13311332 super . push ( )
1332- if ( this . states . textFont ?. font ) {
1333- this . textDrawingContext ( ) ?. save ( )
1333+ const saved = ! ! ( this . states . textFont ?. font ) ;
1334+ if ( saved ) {
1335+ this . textDrawingContext ( ) . save ( )
13341336 }
1337+ this . _textContextSavedStack . push ( saved ) ;
13351338 }
13361339
13371340 pop ( ...args ) {
@@ -1341,8 +1344,8 @@ export class Renderer3D extends Renderer {
13411344 ) {
13421345 this . _clearClip ( ) ;
13431346 }
1344- if ( this . states . textFont ?. font ) {
1345- this . textDrawingContext ( ) ? .restore ( )
1347+ if ( this . _textContextSavedStack . pop ( ) ) {
1348+ this . textDrawingContext ( ) . restore ( )
13461349 }
13471350 super . pop ( ...args ) ;
13481351 this . _applyStencilTestIfClipping ( ) ;
Original file line number Diff line number Diff line change @@ -3016,5 +3016,20 @@ suite('p5.RendererGL', function() {
30163016
30173017 expect ( widthAt20 ) . toBeGreaterThan ( widthAt12 ) ;
30183018 } ) ;
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+ } ) ;
30193034 } ) ;
30203035} ) ;
You can’t perform that action at this time.
0 commit comments