@@ -48,6 +48,9 @@ mupdf_document () {
4848 return doc;
4949}
5050
51+ bool mupdf_renderer_rep::clip_active= false ;
52+ pdf_processor* mupdf_renderer_rep::clip_proc;
53+
5154/* *****************************************************************************
5255 * Fitz pixmaps
5356 ******************************************************************************/
@@ -219,7 +222,6 @@ mupdf_renderer_rep::begin (void* handle) {
219222 current_width= -1.0 ;
220223 cfn = " " ;
221224 in_text = false ;
222- clip_level = 0 ;
223225
224226 // outmost save of the graphics state
225227 proc->op_q (mupdf_context (), proc);
@@ -243,8 +245,11 @@ mupdf_renderer_rep::end () {
243245
244246 if (proc) {
245247 // reset set_clipping calls in order to have well formed PDF.
246- while (clip_level--)
247- proc->op_Q (mupdf_context (), proc);
248+ if (clip_active) {
249+ clip_proc->op_Q (mupdf_context (), clip_proc);
250+ clip_active= false ;
251+ clip_proc = NULL ;
252+ }
248253 // outmost restore for the graphics state (see begin_page)
249254 proc->op_Q (mupdf_context (), proc);
250255
@@ -328,25 +333,28 @@ mupdf_renderer_rep::set_clipping (SI x1, SI y1, SI x2, SI y2, bool restore) {
328333
329334 outer_round (x1, y1, x2, y2);
330335 if (restore) {
331- // debug_convert << "restore clipping\n";
332- if (clip_level > 0 ) {
333- proc->op_Q (mupdf_context (), proc);
334- clip_level--;
335- }
336336 cfn= " " ;
337337 }
338+ if (clip_proc != proc) {
339+ if (clip_proc != NULL ) clip_proc->op_Q (mupdf_context (), clip_proc);
340+ clip_proc = proc;
341+ clip_active= false ;
342+ }
343+ if (clip_active) {
344+ proc->op_Q (mupdf_context (), proc);
345+ }
338346 else {
339- // debug_convert << "set clipping\n";
340- proc->op_q (mupdf_context (), proc);
341- clip_level++;
342- float xx1= to_x (min (x1, x2));
343- float yy1= to_y (min (y1, y2));
344- float xx2= to_x (max (x1, x2));
345- float yy2= to_y (max (y1, y2));
346- proc->op_re (mupdf_context (), proc, xx1, yy1, xx2 - xx1, yy2 - yy1);
347- proc->op_W (mupdf_context (), proc);
348- proc->op_n (mupdf_context (), proc);
347+ clip_active= true ;
349348 }
349+
350+ proc->op_q (mupdf_context (), proc);
351+ float xx1= to_x (min (x1, x2));
352+ float yy1= to_y (min (y1, y2));
353+ float xx2= to_x (max (x1, x2));
354+ float yy2= to_y (max (y1, y2));
355+ proc->op_re (mupdf_context (), proc, xx1, yy1, xx2 - xx1, yy2 - yy1);
356+ proc->op_W (mupdf_context (), proc);
357+ proc->op_n (mupdf_context (), proc);
350358}
351359
352360/* *****************************************************************************
0 commit comments