@@ -68,7 +68,32 @@ public void reset() {
68
68
this . _stateUpdateIndices . Clear ( ) ;
69
69
}
70
70
71
+ void restoreToCount ( int count ) {
72
+ count = this . _states . Count - count ;
73
+ while ( count > 0 ) {
74
+ this . addDrawCmd ( new DrawRestore ( ) ) ;
75
+ count -- ;
76
+ }
77
+ }
78
+
79
+ void restore ( ) {
80
+ var stateToRestore = this . _getState ( ) ;
81
+ this . _states . RemoveAt ( this . _states . Count - 1 ) ;
82
+ var state = this . _getState ( ) ;
83
+
84
+ if ( ! stateToRestore . saveLayer ) {
85
+ state . paintBounds = stateToRestore . paintBounds ;
86
+ }
87
+ else {
88
+ var paintBounds = stateToRestore . paintBounds . shift ( stateToRestore . layerOffset ) ;
89
+ paintBounds = state . xform . mapRect ( paintBounds ) ;
90
+ this . _addPaintBounds ( paintBounds ) ;
91
+ }
92
+ }
93
+
71
94
public Picture endRecording ( ) {
95
+ this . restoreToCount ( 1 ) ;
96
+
72
97
if ( this . _states . Count > 1 ) {
73
98
throw new Exception ( "unmatched save/restore commands" ) ;
74
99
}
@@ -103,20 +128,11 @@ public void addDrawCmd(DrawCmd drawCmd) {
103
128
}
104
129
105
130
case DrawRestore _: {
106
- var stateToRestore = this . _getState ( ) ;
107
- this . _states . RemoveAt ( this . _states . Count - 1 ) ;
108
- var state = this . _getState ( ) ;
109
-
110
- if ( ! stateToRestore . saveLayer ) {
111
- state . paintBounds = stateToRestore . paintBounds ;
131
+ //check for underflow
132
+ if ( this . _states . Count > 1 ) {
133
+ this . restore ( ) ;
134
+ this . _stateUpdateIndices . Add ( this . _drawCmds . Count - 1 ) ;
112
135
}
113
- else {
114
- var paintBounds = stateToRestore . paintBounds . shift ( stateToRestore . layerOffset ) ;
115
- paintBounds = state . xform . mapRect ( paintBounds ) ;
116
- this . _addPaintBounds ( paintBounds ) ;
117
- }
118
- this . _stateUpdateIndices . Add ( this . _drawCmds . Count - 1 ) ;
119
-
120
136
break ;
121
137
}
122
138
0 commit comments