Skip to content

Commit 39e79c2

Browse files
xjl12da-liii
authored andcommitted
!466 [205_6] MuPDF:修复裁剪区域设置异常
1 parent f0cd898 commit 39e79c2

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

devel/205_6.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 205_6 MuPDF:修复裁剪区域设置异常
2+
## 如何测试
3+
启用MuPDF后编译、运行,菜单栏点击插入->图片->绘制图形,可以正常绘制各种图形
4+
5+
## 2025/07/21
6+
### What
7+
问题根源在于,在绘制图形过程中,两个不同的`mupdf_renderer_rep`对象频繁、交替调用`set_clipping`函数,造成MuPDF内部图形状态管理异常,进而造成页面显示异常和崩溃。
8+
1. 重写`mupdf_renderer_rep::set_clipping()`函数,重点解决不同`mupdf_renderer_rep`对象交替调用时,MuPDF图形状态能够统一。
9+
2. 引入类静态变量`clip_active``clip_proc`统一管理裁剪设置的状态。
10+
- `clip_active`指示裁剪区域是否已被设置,在`mupdf_renderer_rep::end`中,裁剪设置会还原。
11+
- `clip_proc`保存上一次调用`set_clipping`函数的`mupdf_renderer_rep`对象中的MuPDF图形状态(`pdf_processor* proc`)地址,确保交替调用时,能够将上一次保存的图形状态顺利还原。
12+
13+
### Why
14+
在启用MuPDF渲染器后,无法正常绘制图形,在绘制图形界面容易崩溃。

src/Plugins/MuPDF/mupdf_renderer.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/******************************************************************************

src/Plugins/MuPDF/mupdf_renderer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class mupdf_renderer_rep : public basic_renderer_rep {
3535
color fg, bg;
3636
SI lw;
3737
double current_width;
38-
int clip_level;
3938

4039
// pencil pen;
4140
// brush bgb, fgb;
@@ -45,6 +44,9 @@ class mupdf_renderer_rep : public basic_renderer_rep {
4544
string cfn;
4645
float fsize;
4746

47+
static bool clip_active;
48+
static pdf_processor* clip_proc;
49+
4850
// geometry
4951

5052
double to_x (SI x) {

0 commit comments

Comments
 (0)