Skip to content

Commit

Permalink
various bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmMaze committed Sep 7, 2023
1 parent cfde1f6 commit 284f18b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
3 changes: 3 additions & 0 deletions launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def main():
print(f'branch: {appinfo.branch}')
print(f"Commit hash: {commit}")

APP_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(APP_DIR)

prepare_environment()

from utils.logger import setup_logging, logger as LOGGER
Expand Down
2 changes: 1 addition & 1 deletion modules/translators/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
def chs2cht(text: str) -> str:
global CHS2CHT_CONVERTER
if CHS2CHT_CONVERTER is None:
CHS2CHT_CONVERTER = opencc.OpenCC('s2t.json')
CHS2CHT_CONVERTER = opencc.OpenCC('s2t')

return CHS2CHT_CONVERTER.convert(text)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ docx2txt
piexif
keyboard
ordered-set
opencc
opencc-python-reimplemented
requests
Pillow
beautifulsoup4
Expand Down
4 changes: 3 additions & 1 deletion ui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,11 @@ def on_pagtrans_finished(self, page_index: int):
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
self.imgtrans_proj.save()

self.saveCurrentPage(False, False)

def on_savestate_changed(self, unsaved: bool):
save_state = self.tr('unsaved') if unsaved else self.tr('saved')
Expand Down
1 change: 0 additions & 1 deletion ui/module_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,6 @@ def finishImgtransPipeline(self):
and self.imgtrans_thread.translate_finished() \
and self.imgtrans_thread.inpaint_finished():
self.progress_msgbox.hide()
self.imgtrans_proj.save()
self.imgtrans_pipeline_finished.emit()

def setTranslator(self, translator: str = None):
Expand Down
32 changes: 17 additions & 15 deletions ui/scene_textlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ def __init__(self, doc: QTextDocument) -> None:
self.relayout_on_changed = True

# relative bottom/right
self.y_bottom = 0
self.x_right = 0
self.shrink_height = 0
self.shrink_width = 0

def setMaxSize(self, max_width: int, max_height: int, relayout=True):
self.max_height = max_height
Expand Down Expand Up @@ -247,7 +247,7 @@ def max_font_size(self, to_px=False) -> float:
return fs

def minSize(self):
return (self.y_bottom, self.x_right)
return (self.shrink_height, self.shrink_width)


class VerticalTextDocumentLayout(SceneTextLayout):
Expand Down Expand Up @@ -275,6 +275,8 @@ def reLayout(self):
self.layout_left = 0
self.line_spaces_lst = []
self.draw_shifted = 0
self.shrink_height = 0
self.shrink_width = 0
doc = self.document()
doc_margin = doc.documentMargin()
block = doc.firstBlock()
Expand Down Expand Up @@ -530,7 +532,7 @@ def layoutBlock(self, block: QTextBlock):
option.setWrapMode(QTextOption.WrapAnywhere)
tl.setTextOption(option)

y_bottom = 0
shrink_height = 0
while True:
line = tl.createLine()
if not line.isValid():
Expand Down Expand Up @@ -606,13 +608,13 @@ def layoutBlock(self, block: QTextBlock):
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
shrink_height = 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)
shrink_height = max(shrink_height, line_bottom)

line.setPosition(QPointF(x_offset, line_y_offset))
blk_char_yoffset.append([line_y_offset, line_bottom])
Expand All @@ -621,8 +623,8 @@ def layoutBlock(self, block: QTextBlock):
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.shrink_width = max(self.max_width - self.layout_left, self.shrink_width)
self.shrink_height = max(shrink_height, self.shrink_height)
self.x_offset_lst.append(x_offset)
self.y_offset_lst.append(blk_char_yoffset)
self.line_spaces_lst.append(blk_line_spaces)
Expand All @@ -646,15 +648,15 @@ def __init__(self, doc: QTextDocument):
def reLayout(self):
doc = self.document()
doc_margin = self.document().documentMargin()
self.y_bottom = 0
self.x_right = 0
self.shrink_height = 0
self.shrink_width = 0
block = doc.firstBlock()
while block.isValid():
self.layoutBlock(block)
block = block.next()

if len(self.y_offset_lst) > 0:
new_height = self.y_bottom - doc_margin
new_height = self.shrink_height - doc_margin
else:
new_height = doc_margin
if new_height > self.available_height:
Expand Down Expand Up @@ -726,7 +728,7 @@ def layoutBlock(self, block: QTextBlock):

line_idx = 0
tl.beginLayout()
x_right = 0
shrink_width = 0
while True:
line = tl.createLine()
if not line.isValid():
Expand All @@ -735,13 +737,13 @@ def layoutBlock(self, block: QTextBlock):
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() #????
shrink_width = max(tw, shrink_width)
self.shrink_height = max(idea_height + y_offset + line.descent(), self.shrink_height) #????
y_offset += idea_height * self.line_spacing
line_idx += 1
tl.endLayout()
self.y_offset_lst.append(y_offset)
self.x_right = x_right
self.shrink_width = max(shrink_width, self.shrink_width)
return 1

def draw(self, painter: QPainter, context: QAbstractTextDocumentLayout.PaintContext) -> None:
Expand Down
3 changes: 2 additions & 1 deletion ui/textitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def absBoundingRect(self, max_h=None, max_w=None, qrect=False) -> Union[List, QR
w = min(max_w, x1) - x
if qrect:
return QRectF(x, y, w, h)
return [int(x), int(y), int(w), int(h)]
return [math.ceil(x), math.ceil(y), math.ceil(w), math.ceil(h)]

def shape(self) -> QPainterPath:
path = QPainterPath()
Expand Down Expand Up @@ -984,3 +984,4 @@ def shrinkSize(self):
elif align_tr:
ml += extra_w
self.setRect(QRectF(ml, mt, mw, mh))
self.blk._bounding_rect = self.absBoundingRect()
9 changes: 7 additions & 2 deletions utils/text_processing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List, Tuple
import json
import os.path as osp
import os

HALF2FULL = {i: i + 0xFEE0 for i in range(0x21, 0x7F)}
HALF2FULL[0x20] = 0x3000
Expand Down Expand Up @@ -191,8 +192,12 @@ def seg_ch_pkg(text: str):
import pkuseg
except:
import spacy_pkuseg as pkuseg
if not osp.exists(pkuseg.config.pkuseg_home):
pkuseg.config.pkuseg_home = 'data/models/pkuseg'
PKUSEG_HOME = osp.join(osp.dirname(osp.dirname(osp.abspath(__file__))), 'data/models/pkuseg')
if not osp.exists(osp.join(PKUSEG_HOME, 'postag.zip')) or not osp.exists(osp.join(PKUSEG_HOME, 'spacy_ontonotes.zip')):
if osp.exists(osp.join(PKUSEG_HOME, 'postag')) and osp.exists(osp.join(PKUSEG_HOME, 'spacy_ontonotes')):
os.makedirs(osp.join(PKUSEG_HOME, 'spacy_ontonotes.zip'), exist_ok=True)
os.makedirs(osp.join(PKUSEG_HOME, 'postag.zip'), exist_ok=True)
pkuseg.config.pkuseg_home = PKUSEG_HOME
CHSEG = pkuseg.pkuseg(postag=True)

# pkuseg won't work with half-width punctuations
Expand Down

0 comments on commit 284f18b

Please sign in to comment.