Commit 68871662 authored by dmMaze's avatar dmMaze
Browse files

support adaptive resize font

parent e1e57608
Loading
Loading
Loading
Loading
+21 −10
Original line number Diff line number Diff line
@@ -98,14 +98,21 @@ class ConfigBlock(Widget):
        sublock.pressed.connect(lambda idx0, idx1: self.sublock_pressed.emit(idx0, idx1))
        self.subblock_list.append(sublock)

    def addCombobox(self, sel: List[str], name: str, discription: str = None, vertical_layout: bool = False):
    def addCombobox(self, sel: List[str], name: str, discription: str = None, vertical_layout: bool = False, target_block: QWidget = None) -> ConfigComboBox:
        combox = ConfigComboBox()
        combox.addItems(sel)
        if target_block is None:
            sublock = ConfigSubBlock(combox, name, discription, vertical_layout=vertical_layout)
            sublock.layout().setAlignment(Qt.AlignmentFlag.AlignLeft)
            sublock.layout().setSpacing(20)
            self.addSublock(sublock)
        return combox
            return combox, sublock
        else:
            layout = target_block.layout()
            layout.addSpacing(20)
            layout.addWidget(ConfigTextLabel(name, CONFIG_FONTSIZE_CONTENT, QFont.Weight.Normal))
            layout.addWidget(combox)
            return combox, target_block

    def addBlockWidget(self, widget: Union[QWidget, QLayout], name: str = None, discription: str = None, vertical_layout: bool = False) -> ConfigSubBlock:
        sublock = ConfigSubBlock(widget, name, discription, vertical_layout)
@@ -115,6 +122,9 @@ class ConfigBlock(Widget):
    def addCheckBox(self, name: str, discription: str = None) -> QCheckBox:
        checkbox = QCheckBox()
        if discription is not None:
            font = checkbox.font()
            font.setPointSizeF(CONFIG_FONTSIZE_CONTENT * 0.8)
            checkbox.setFont(font)
            checkbox.setText(discription)
            vertical_layout = True
        else:
@@ -333,13 +343,14 @@ class ConfigPanel(Widget):
        dec_program_str = self.tr('decide by program')
        use_global_str = self.tr('use global setting')
        
        self.let_fntsize_combox = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font size'))
        self.let_fntsize_combox, letblk_0 = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font size'))
        self.let_fntsize_combox.currentIndexChanged.connect(self.on_fntsize_flag_changed)
        self.let_fntstroke_combox = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('stroke'))
        self.let_fntstroke_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('stroke'), target_block=letblk_0)
        self.let_fntstroke_combox.currentIndexChanged.connect(self.on_fntstroke_flag_changed)
        self.let_fntcolor_combox = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color'))
        self.let_fntcolor_combox, _ = generalConfigPanel.addCombobox([dec_program_str, use_global_str], self.tr('font & stroke color'))
        self.let_fntcolor_combox.currentIndexChanged.connect(self.on_fontcolor_flag_changed)
        self.let_autolayout_checker = generalConfigPanel.addCheckBox(self.tr('Auto layout'))
        self.let_autolayout_checker = generalConfigPanel.addCheckBox(self.tr('Auto layout'), 
                discription=self.tr('Split translation into multi-lines according to the extracted balloon region. The font size will be adaptively resized if it is set to \"decide by program.\"'))
        self.let_autolayout_checker.stateChanged.connect(self.on_autolayout_changed)
        self.let_uppercase_checker = generalConfigPanel.addCheckBox(self.tr('To uppercase'))
        self.let_uppercase_checker.stateChanged.connect(self.on_uppercase_changed)
+30 −13
Original line number Diff line number Diff line
@@ -469,17 +469,20 @@ class SceneTextManager(QObject):
        delimiter_len = text_size_func(delimiter)[0]

        font_scale_ratio = 1.0

        # 
        # if self.auto_textlayout_flag:
        #     if self.config.let_fntsize_flag == 0 and text:
        adaptive_fntsize = True
        if adaptive_fntsize:
            area_ratio = ballon_area / (w * h)
            ballon_area_thresh = 1.8
            downscale_constraint = 0.6
        resize_ratio = min(area_ratio / ballon_area_thresh, max(wl_list) / region_rect[2])
            # downscale the font size if textarea exceeds the balloon_area / ballon_area_thresh
            # or the longest word exceeds the region_width
            resize_ratio = np.clip(min(area_ratio / ballon_area_thresh, max(wl_list) / region_rect[2]), downscale_constraint, 1.0) 
            if resize_ratio < 1:
            resize_ratio = max(resize_ratio, downscale_constraint)
                new_font_size = blk_font.pointSizeF() * resize_ratio
            blkitem.textCursor().clearSelection()
            set_textblk_fontsize(blkitem, new_font_size)
                blk_font.setPointSizeF(new_font_size)
                wl_list = (np.array(wl_list, np.float64) * resize_ratio).astype(np.int32).tolist()
                line_height = int(line_height * resize_ratio)
@@ -488,6 +491,20 @@ class SceneTextManager(QObject):
        padding = pt2px(blk_font.pointSize()) // 4   # dummpy padding variable
        new_text, xywh = layout_text(mask, mask_xyxy, region_rect, words, wl_list, delimiter, delimiter_len, blkitem.blk.angle, line_height, fmt.alignment, fmt.vertical, padding)
        
        if adaptive_fntsize:
            downscale_constraint = 0.7
            post_resize_ratio = max(region_rect[2] / xywh[2], downscale_constraint)
            if post_resize_ratio < 1:
                resize_ratio *= post_resize_ratio
                cx, cy = xywh[0] + xywh[2] / 2, xywh[1] + xywh[3] / 2
                w, h = xywh[2] * post_resize_ratio, xywh[3] * post_resize_ratio
                xywh = [int(cx - w / 2), int(cy - h / 2), int(w), int(h)]
            if resize_ratio < 1:
                new_font_size = blkitem.font().pointSizeF() * resize_ratio
                blkitem.textCursor().clearSelection()
                set_textblk_fontsize(blkitem, new_font_size)


        scale = blkitem.scale()
        if scale != 1:
            xywh = (np.array(xywh, np.float64) * blkitem.scale()).astype(np.int32).tolist()