Skip to content

Commit 5f09eb6

Browse files
authored
[201_41] 模拟OCR通信流程 (#2345)
1 parent 13c41b9 commit 5f09eb6

File tree

9 files changed

+36
-32
lines changed

9 files changed

+36
-32
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
\begin{document}
2-
\[ e^{i\pi} + 1 = 0 \]
2+
很抱歉,目前OCR功能仅为会员用户提供。\
3+
详情请见 \url{https://liiistem.cn/} 注册即送14天会员!
34
\end{document}

TeXmacs/plugins/account/progs/liii/ocr.scm

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@
6262
(when (url-exists? p)
6363
(string-load p)))
6464

65-
(tm-define (insert-tips p)
66-
(go-to p)
65+
(tm-define (insert-tips)
66+
(go-to (cursor-path))
6767
(go-to-next-node)
68+
(let* ((content (get-file-string (unix->url "$TEXMACS_PATH/plugins/account/data/ocr.md"))))
69+
(insert `(with "par-mode" "center" (document ,(utf8->cork content))))))
70+
71+
72+
(tm-define (insert-latex)
73+
(go-to (cursor-path))
6874
(go-to-next-node)
69-
(let* ((content (get-file-string (unix->url "$TEXMACS_PATH/plugins/account/data/ocr.md")))
70-
(latex-code (get-file-string (unix->url "$TEXMACS_PATH/plugins/account/data/ocr.tex")))
75+
(let* ((latex-code (get-file-string (unix->url "$TEXMACS_PATH/plugins/account/data/ocr.tex")))
7176
(parsed-latex (parse-latex latex-code))
7277
(texmacs-latex (latex->texmacs parsed-latex)))
73-
(insert `(with "par-mode" "center" (document ,(utf8->cork content))))
7478
(insert texmacs-latex)))
7579

7680
(tm-define (image-ocr-to-latex t)
@@ -85,14 +89,12 @@
8589
(binary-data (decode-base64 base64-str)))
8690
(string-save binary-data temp-name)
8791
(display* "Image has saved to " temp-name "\n"))))
88-
(sleep 5)
89-
(go-to (cursor-path))
90-
(insert `(with "par-mode" "center" (document ,(utf8->cork "很抱歉,目前OCR功能仅为会员用户提供。")))))
92+
(insert-latex))
9193

9294
; (get-image-extention (get-image t 0 #t)) 获取文件后缀,创建对应临时文件
9395
; (get-image t 0 #f) 获取 raw-data
9496

95-
(tm-define (create-temp-image t p)
97+
(tm-define (create-temp-image t)
9698
(let* ((extention
9799
(get-image-extention (get-image t 0 #t)))
98100
(temp-name
@@ -104,4 +106,4 @@
104106
(binary-data (decode-base64 base64-str)))
105107
(string-save binary-data temp-name)
106108
(display* "Image has saved to " temp-name "\n"))))
107-
(insert-tips p))
109+
(insert-latex))

TeXmacs/progs/generic/format-edit.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@
173173
(string=? (tree->string (tree-ref t 0)) "par-mode")
174174
(>= (tree-arity t) 3))
175175
(tree-set! t 1 align)
176-
(debug-message "std-warning" "Invalid tree structure for set-image-alignment" "\n")))
176+
(debug-message "std-warning" "Invalid tree structure for set-image-alignment\n")))
177177

178178
(tm-define (get-image-alignment t)
179179
(if (and (tree-is? t 'with)
180180
(tree-is? (tree-ref t 0) 'string)
181181
(string=? (tree->string (tree-ref t 0)) "par-mode")
182182
(>= (tree-arity t) 3))
183183
(tree->string (tree-ref t 1))
184-
(debug-message "std-warning" "Invalid tree structure for get-image-alignment" "\n")))
184+
(debug-message "std-warning" "Invalid tree structure for get-image-alignment\n")))
185185

186186
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
187187
;; Inserting and toggling with-like tags

devel/201_41.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
## 如何测试
44
打开201_41.tmu,进行以下测试项:
5-
+ 点击图片悬浮菜单的 OCR 按钮,查看是否在图片下方插入了一条 **很抱歉,目前OCR功能仅为会员用户提供**,以及一行数学公式。
5+
+ 点击图片悬浮菜单的 OCR 按钮,查看是否在图片下方插入了一条 **很抱歉,目前OCR功能仅为会员用户提供**
66
+ 查看日志中的提示 **Image has saved to /tmp/temp-currenttime.png**,其中 currenttime 为图片保存时的时间戳,每人各不相同,以控制台日志输出为准。前往目标目录,查看文件是否存在,以及是否能够正常打开。
7-
+ 在数学模式下使用选择性粘贴功能,对应快捷键 **Ctrl+Alt+v**,呼出菜单后选择OCR,点击确定,等待5秒后会插入一句提示信息
8-
+ 使用智能粘贴功能,对应快捷键 **Ctrl+Shift+v**,确保剪贴板最新的内容是一张图片,按下快捷键,等待5秒后会插入一句提示信息
7+
+ 在数学模式下使用选择性粘贴功能,对应快捷键 **Ctrl+Alt+v**,呼出菜单后选择OCR,点击确定,等待5秒后会插入一条 **很抱歉,目前OCR功能仅为会员用户提供**
8+
+ 使用智能粘贴功能,对应快捷键 **Ctrl+Shift+v**,确保剪贴板最新的内容是一张图片,按下快捷键,等待5秒后会插入一条 **很抱歉,目前OCR功能仅为会员用户提供**
9+
+ 使用 **插入图片****粘贴图片** 以外的方式,即除了 **Ctrl+Alt+v** 粘贴图片以及 **插入->图片->插入图片** 以外的任何方式向编辑器输入一张图片,这里建议的方法是直接将文件夹中的图片拖入 Mogan,这样会插入一个没有经过处理的,能够稳定弹出警告的图片。插入后将鼠标移入移出图片范围,查看控制台是否正常输出 **std-warning**,而不是 **too many arguments...**
10+
11+
## 2025/12/25 模拟OCR通信流程
12+
13+
### What
14+
15+
+ 商业版编写 demo 文件,跑通 OCR 识别全流程
16+
+ 进行了代码集成的尝试,待后续测试与改进
17+
+ 修改了插入文字的位置,新的插入位置为图片块内部,图片的下方
18+
+ 修复了负责控制图片对齐方式的函数的警告信息输出问题,使用正确的参数个数
919

1020
## 2025/12/24 模拟OCR识别
1121

src/Edit/Interface/edit_mouse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ edit_interface_rep::mouse_any (string type, SI x, SI y, int mods, time_t t,
605605
set_cursor_style ("pointing_hand");
606606
path with_path= path_up (current_path);
607607
tree with_tree= subtree (et, with_path);
608-
show_image_popup (current_path, with_tree, selr, magf, get_scroll_x (),
609-
get_scroll_y (), get_canvas_x ());
608+
show_image_popup (with_tree, selr, magf, get_scroll_x (), get_scroll_y (),
609+
get_canvas_x ());
610610
}
611611
else {
612612
set_cursor_style ("normal");

src/Plugins/Qt/QTMImagePopup.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ QTMImagePopup::QTMImagePopup (QWidget* parent, qt_simple_widget_rep* owner)
7777
else if (button == rightBtn)
7878
call ("set-image-alignment", current_tree, "right");
7979
else if (button == ocrBtn)
80-
call ("create-temp-image", current_tree, current_path);
80+
call ("create-temp-image", current_tree);
8181
current_align=
8282
as_string (call ("get-image-alignment", current_tree));
8383
});
@@ -108,11 +108,6 @@ QTMImagePopup::showImagePopup (rectangle selr, double magf, int scroll_x,
108108
show ();
109109
}
110110

111-
void
112-
QTMImagePopup::setImagePath (path p) {
113-
this->current_path= p;
114-
}
115-
116111
void
117112
QTMImagePopup::setImageTree (tree t) {
118113
this->current_tree= t;

src/Plugins/Qt/QTMImagePopup.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class QTMImagePopup : public QWidget {
3232
int cached_scroll_y; // 页面滚动位置y
3333
int cached_canvas_x;
3434
double cached_magf; // 缩放因子
35-
path current_path;
3635
tree current_tree;
3736
string current_align;
3837
QToolButton* leftBtn;
@@ -49,7 +48,6 @@ class QTMImagePopup : public QWidget {
4948
int canvas_x);
5049
void updatePosition ();
5150
void scrollBy (int x, int y);
52-
void setImagePath (path p);
5351
void setImageTree (tree t);
5452
void updateButtonStates ();
5553
void autoSize ();

src/Plugins/Qt/qt_simple_widget.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,10 @@ qt_simple_widget_rep::ensure_image_popup () {
760760
}
761761

762762
void
763-
qt_simple_widget_rep::show_image_popup (path current_path, tree current_tree,
764-
rectangle selr, double magf,
765-
int scroll_x, int scroll_y,
763+
qt_simple_widget_rep::show_image_popup (tree current_tree, rectangle selr,
764+
double magf, int scroll_x, int scroll_y,
766765
int canvas_x) {
767766
ensure_image_popup ();
768-
imagePopUp->setImagePath (current_path);
769767
imagePopUp->setImageTree (current_tree);
770768
imagePopUp->showImagePopup (selr, magf, scroll_x, scroll_y, canvas_x);
771769
}

src/Plugins/Qt/qt_simple_widget.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class qt_simple_widget_rep : public qt_widget_rep {
115115

116116
////////////////////// Image popup support
117117
void ensure_image_popup ();
118-
void show_image_popup (path current_path, tree current_tree, rectangle selr,
119-
double magf, int scroll_x, int scroll_y, int canvas_x);
118+
void show_image_popup (tree current_tree, rectangle selr, double magf,
119+
int scroll_x, int scroll_y, int canvas_x);
120120
void hide_image_popup ();
121121
void scroll_image_popup_by (SI x, SI y);
122122

0 commit comments

Comments
 (0)