Loading ui/mainwindow.py +4 −0 Original line number Diff line number Diff line Loading @@ -943,6 +943,10 @@ class MainWindow(FramelessWindow): self.canvas.updateCanvas() self.st_manager.updateSceneTextitems() if not pcfg.module.enable_detect: for blkitem in self.st_manager.textblk_item_list: blkitem.shrinkSize() self.saveCurrentPage(False, False) if page_index + 1 == self.imgtrans_proj.num_pages: self.st_manager.auto_textlayout_flag = False Loading ui/scene_textlayout.py +19 −0 Original line number Diff line number Diff line Loading @@ -156,6 +156,10 @@ class SceneTextLayout(QAbstractTextDocumentLayout): self.relayout_on_changed = True # relative bottom/right self.y_bottom = 0 self.x_right = 0 def setMaxSize(self, max_width: int, max_height: int, relayout=True): self.max_height = max_height self.max_width = max_width Loading Loading @@ -242,6 +246,10 @@ class SceneTextLayout(QAbstractTextDocumentLayout): fs = pt2px(fs) return fs def minSize(self): return (self.y_bottom, self.x_right) class VerticalTextDocumentLayout(SceneTextLayout): def __init__(self, doc: QTextDocument): Loading Loading @@ -522,6 +530,7 @@ class VerticalTextDocumentLayout(SceneTextLayout): option.setWrapMode(QTextOption.WrapAnywhere) tl.setTextOption(option) y_bottom = 0 while True: line = tl.createLine() if not line.isValid(): Loading Loading @@ -597,11 +606,13 @@ class VerticalTextDocumentLayout(SceneTextLayout): for _ in range(num_rspaces): char_yoffset_lst.append(min(char_yoffset_lst[-1] + space_w, available_height)) line_bottom = char_yoffset_lst[-1] y_bottom = available_height else: char_yoffset_lst.append(char_bottom) for _ in range(num_rspaces): char_yoffset_lst.append(min(char_yoffset_lst[-1] + space_w, available_height)) line_bottom = char_yoffset_lst[-1] y_bottom = max(y_bottom, line_bottom) line.setPosition(QPointF(x_offset, line_y_offset)) blk_char_yoffset.append([line_y_offset, line_bottom]) Loading @@ -610,6 +621,8 @@ class VerticalTextDocumentLayout(SceneTextLayout): tl.endLayout() self.layout_left = x_offset - self.draw_shifted self.x_right = self.max_width - self.layout_left self.y_bottom = y_bottom self.x_offset_lst.append(x_offset) self.y_offset_lst.append(blk_char_yoffset) self.line_spaces_lst.append(blk_line_spaces) Loading @@ -623,6 +636,7 @@ class VerticalTextDocumentLayout(SceneTextLayout): self.reLayout() class HorizontalTextDocumentLayout(SceneTextLayout): def __init__(self, doc: QTextDocument): Loading @@ -633,6 +647,7 @@ class HorizontalTextDocumentLayout(SceneTextLayout): doc = self.document() doc_margin = self.document().documentMargin() self.y_bottom = 0 self.x_right = 0 block = doc.firstBlock() while block.isValid(): self.layoutBlock(block) Loading Loading @@ -711,6 +726,7 @@ class HorizontalTextDocumentLayout(SceneTextLayout): line_idx = 0 tl.beginLayout() x_right = 0 while True: line = tl.createLine() if not line.isValid(): Loading @@ -718,11 +734,14 @@ class HorizontalTextDocumentLayout(SceneTextLayout): line.setLeadingIncluded(False) line.setLineWidth(self.available_width) line.setPosition(QPointF(doc_margin, y_offset)) tw = line.naturalTextWidth() x_right = max(tw, x_right) self.y_bottom = idea_height + y_offset + line.descent() #???? y_offset += idea_height * self.line_spacing line_idx += 1 tl.endLayout() self.y_offset_lst.append(y_offset) self.x_right = x_right return 1 def draw(self, painter: QPainter, context: QAbstractTextDocumentLayout.PaintContext) -> None: Loading ui/scenetext_manager.py +7 −2 Original line number Diff line number Diff line Loading @@ -699,7 +699,7 @@ class SceneTextManager(QObject): self.canvas.push_undo_command(ResetAngleCommand(selected_blks, self.txtblkShapeControl)) def layout_textblk(self, blkitem: TextBlkItem, text: str = None, mask: np.ndarray = None, bounding_rect: List = None, region_rect: List = None): old_br = blkitem.absBoundingRect() img = self.imgtrans_proj.img_array if img is None: return Loading Loading @@ -839,11 +839,15 @@ class SceneTextManager(QObject): if scale != 1 and not fmt.alignment == 0: xywh = (np.array(xywh, np.float64) * scale).astype(np.int32).tolist() if fmt.alignment == 0: if fmt.alignment == 0 or fmt.alignment == 2: x_shift = (scale - 1) * xywh[2] // 2 + xywh[0] * scale y_shift = (scale - 1) * xywh[3] // 2 + xywh[1] * scale xywh[0] = int(abs_centroid[0] * scale) + x_shift xywh[1] = int(abs_centroid[1] * scale) + y_shift if fmt.alignment == 2: ex_w, ex_h = xywh[2] - old_br[2], xywh[3] - old_br[3] if ex_w > 0: xywh[0] -= ex_w if restore_charfmts: char_fmts = blkitem.get_char_fmts() Loading @@ -854,6 +858,7 @@ class SceneTextManager(QObject): self.pairwidget_list[blkitem.idx].e_trans.setPlainText(new_text) if restore_charfmts: self.restore_charfmts(blkitem, text, new_text, char_fmts) blkitem.shrinkSize() def restore_charfmts(self, blkitem: TextBlkItem, text: str, new_text: str, char_fmts: List[QTextCharFormat]): cursor = blkitem.textCursor() Loading ui/textedit_commands.py +1 −1 Original line number Diff line number Diff line Loading @@ -184,7 +184,7 @@ class AutoLayoutCommand(QUndoCommand): self.new_html_lst = [] for item in items: self.new_html_lst.append(item.toHtml()) self.new_rect_lst.append(item.absBoundingRect()) self.new_rect_lst.append(item.absBoundingRect(qrect=True)) self.counter = 0 def redo(self): Loading ui/textitem.py +29 −0 Original line number Diff line number Diff line Loading @@ -955,3 +955,32 @@ class TextBlkItem(QGraphicsTextItem): cursor = QTextCursor(self.document()) cursor.select(QTextCursor.SelectionType.Document) cursor.insertText(text) def shrinkSize(self): mh, mw = self.layout.minSize() if mh == 0 or mw == 0: return br = self.absBoundingRect(qrect=True) align_tl = align_tr = align_c = False br_w, br_h = br.width(), br.height() if br_w > mw or br_h > mh: if self.is_vertical: align_tr = True else: alignment = self.alignment() if alignment == Qt.AlignmentFlag.AlignLeft: align_tl = True elif alignment == Qt.AlignmentFlag.AlignRight: align_tr = True else: align_c = True ml, mt = br.left(), br.top() extra_w, extra_h = br_w - mw, br_h - mh extra_w = max(0, extra_w) extra_h = max(0, extra_h) if align_c: ml += extra_w / 2 mt += extra_h / 2 elif align_tr: ml += extra_w self.setRect(QRectF(ml, mt, mw, mh)) Loading
ui/mainwindow.py +4 −0 Original line number Diff line number Diff line Loading @@ -943,6 +943,10 @@ class MainWindow(FramelessWindow): self.canvas.updateCanvas() self.st_manager.updateSceneTextitems() if not pcfg.module.enable_detect: for blkitem in self.st_manager.textblk_item_list: blkitem.shrinkSize() self.saveCurrentPage(False, False) if page_index + 1 == self.imgtrans_proj.num_pages: self.st_manager.auto_textlayout_flag = False Loading
ui/scene_textlayout.py +19 −0 Original line number Diff line number Diff line Loading @@ -156,6 +156,10 @@ class SceneTextLayout(QAbstractTextDocumentLayout): self.relayout_on_changed = True # relative bottom/right self.y_bottom = 0 self.x_right = 0 def setMaxSize(self, max_width: int, max_height: int, relayout=True): self.max_height = max_height self.max_width = max_width Loading Loading @@ -242,6 +246,10 @@ class SceneTextLayout(QAbstractTextDocumentLayout): fs = pt2px(fs) return fs def minSize(self): return (self.y_bottom, self.x_right) class VerticalTextDocumentLayout(SceneTextLayout): def __init__(self, doc: QTextDocument): Loading Loading @@ -522,6 +530,7 @@ class VerticalTextDocumentLayout(SceneTextLayout): option.setWrapMode(QTextOption.WrapAnywhere) tl.setTextOption(option) y_bottom = 0 while True: line = tl.createLine() if not line.isValid(): Loading Loading @@ -597,11 +606,13 @@ class VerticalTextDocumentLayout(SceneTextLayout): for _ in range(num_rspaces): char_yoffset_lst.append(min(char_yoffset_lst[-1] + space_w, available_height)) line_bottom = char_yoffset_lst[-1] y_bottom = available_height else: char_yoffset_lst.append(char_bottom) for _ in range(num_rspaces): char_yoffset_lst.append(min(char_yoffset_lst[-1] + space_w, available_height)) line_bottom = char_yoffset_lst[-1] y_bottom = max(y_bottom, line_bottom) line.setPosition(QPointF(x_offset, line_y_offset)) blk_char_yoffset.append([line_y_offset, line_bottom]) Loading @@ -610,6 +621,8 @@ class VerticalTextDocumentLayout(SceneTextLayout): tl.endLayout() self.layout_left = x_offset - self.draw_shifted self.x_right = self.max_width - self.layout_left self.y_bottom = y_bottom self.x_offset_lst.append(x_offset) self.y_offset_lst.append(blk_char_yoffset) self.line_spaces_lst.append(blk_line_spaces) Loading @@ -623,6 +636,7 @@ class VerticalTextDocumentLayout(SceneTextLayout): self.reLayout() class HorizontalTextDocumentLayout(SceneTextLayout): def __init__(self, doc: QTextDocument): Loading @@ -633,6 +647,7 @@ class HorizontalTextDocumentLayout(SceneTextLayout): doc = self.document() doc_margin = self.document().documentMargin() self.y_bottom = 0 self.x_right = 0 block = doc.firstBlock() while block.isValid(): self.layoutBlock(block) Loading Loading @@ -711,6 +726,7 @@ class HorizontalTextDocumentLayout(SceneTextLayout): line_idx = 0 tl.beginLayout() x_right = 0 while True: line = tl.createLine() if not line.isValid(): Loading @@ -718,11 +734,14 @@ class HorizontalTextDocumentLayout(SceneTextLayout): line.setLeadingIncluded(False) line.setLineWidth(self.available_width) line.setPosition(QPointF(doc_margin, y_offset)) tw = line.naturalTextWidth() x_right = max(tw, x_right) self.y_bottom = idea_height + y_offset + line.descent() #???? y_offset += idea_height * self.line_spacing line_idx += 1 tl.endLayout() self.y_offset_lst.append(y_offset) self.x_right = x_right return 1 def draw(self, painter: QPainter, context: QAbstractTextDocumentLayout.PaintContext) -> None: Loading
ui/scenetext_manager.py +7 −2 Original line number Diff line number Diff line Loading @@ -699,7 +699,7 @@ class SceneTextManager(QObject): self.canvas.push_undo_command(ResetAngleCommand(selected_blks, self.txtblkShapeControl)) def layout_textblk(self, blkitem: TextBlkItem, text: str = None, mask: np.ndarray = None, bounding_rect: List = None, region_rect: List = None): old_br = blkitem.absBoundingRect() img = self.imgtrans_proj.img_array if img is None: return Loading Loading @@ -839,11 +839,15 @@ class SceneTextManager(QObject): if scale != 1 and not fmt.alignment == 0: xywh = (np.array(xywh, np.float64) * scale).astype(np.int32).tolist() if fmt.alignment == 0: if fmt.alignment == 0 or fmt.alignment == 2: x_shift = (scale - 1) * xywh[2] // 2 + xywh[0] * scale y_shift = (scale - 1) * xywh[3] // 2 + xywh[1] * scale xywh[0] = int(abs_centroid[0] * scale) + x_shift xywh[1] = int(abs_centroid[1] * scale) + y_shift if fmt.alignment == 2: ex_w, ex_h = xywh[2] - old_br[2], xywh[3] - old_br[3] if ex_w > 0: xywh[0] -= ex_w if restore_charfmts: char_fmts = blkitem.get_char_fmts() Loading @@ -854,6 +858,7 @@ class SceneTextManager(QObject): self.pairwidget_list[blkitem.idx].e_trans.setPlainText(new_text) if restore_charfmts: self.restore_charfmts(blkitem, text, new_text, char_fmts) blkitem.shrinkSize() def restore_charfmts(self, blkitem: TextBlkItem, text: str, new_text: str, char_fmts: List[QTextCharFormat]): cursor = blkitem.textCursor() Loading
ui/textedit_commands.py +1 −1 Original line number Diff line number Diff line Loading @@ -184,7 +184,7 @@ class AutoLayoutCommand(QUndoCommand): self.new_html_lst = [] for item in items: self.new_html_lst.append(item.toHtml()) self.new_rect_lst.append(item.absBoundingRect()) self.new_rect_lst.append(item.absBoundingRect(qrect=True)) self.counter = 0 def redo(self): Loading
ui/textitem.py +29 −0 Original line number Diff line number Diff line Loading @@ -955,3 +955,32 @@ class TextBlkItem(QGraphicsTextItem): cursor = QTextCursor(self.document()) cursor.select(QTextCursor.SelectionType.Document) cursor.insertText(text) def shrinkSize(self): mh, mw = self.layout.minSize() if mh == 0 or mw == 0: return br = self.absBoundingRect(qrect=True) align_tl = align_tr = align_c = False br_w, br_h = br.width(), br.height() if br_w > mw or br_h > mh: if self.is_vertical: align_tr = True else: alignment = self.alignment() if alignment == Qt.AlignmentFlag.AlignLeft: align_tl = True elif alignment == Qt.AlignmentFlag.AlignRight: align_tr = True else: align_c = True ml, mt = br.left(), br.top() extra_w, extra_h = br_w - mw, br_h - mh extra_w = max(0, extra_w) extra_h = max(0, extra_h) if align_c: ml += extra_w / 2 mt += extra_h / 2 elif align_tr: ml += extra_w self.setRect(QRectF(ml, mt, mw, mh))