Commit 2debb6d5 authored by dmMaze's avatar dmMaze
Browse files

fix font weight conversion between qt5 and qt6

parent 5c8344e5
Loading
Loading
Loading
Loading
+5 −12
Original line number Diff line number Diff line
from typing import Union
import re

from . import shared
from .structures import Tuple, Union, List, Dict, Config, field, nested_dataclass
from .textblock import TextBlock
from .textblock import TextBlock, fix_fontweight_qt


def pt2px(pt) -> float:
@@ -10,10 +13,6 @@ def px2pt(px) -> float:
    return px / shared.LDPI * 72.


fontweight_qt5_to_qt6 = {0: 100, 12: 200, 25: 300, 50: 400, 57: 500, 63: 600, 75: 700, 81: 800, 87: 900}
fontweight_qt6_to_qt5 = {100: 0, 200: 12, 300: 25, 400: 50, 500: 57, 600: 63, 700: 75, 800: 81, 900: 87}


@nested_dataclass
class FontFormat(Config):

@@ -57,13 +56,7 @@ class FontFormat(Config):
        self.shadow_offset = text_block.shadow_offset

    def __post_init__(self):
        if self.weight is not None:
            if shared.FLAG_QT6 and self.weight < 100:
                if self.weight in fontweight_qt5_to_qt6:
                    self.weight = fontweight_qt5_to_qt6[self.weight]
            if not shared.FLAG_QT6 and self.weight >= 100:
                if self.weight in fontweight_qt6_to_qt5:
                    self.weight = fontweight_qt6_to_qt5[self.weight]
        self.weight = fix_fontweight_qt(self.weight)

    def update_textblock_format(self, blk: TextBlock):
        blk.default_stroke_width = self.stroke_width
+29 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import re
from .imgproc_utils import union_area, xywh2xyxypoly, rotate_polygons, color_difference
from .structures import Tuple, Union, List, Dict, Config, field, nested_dataclass
from .split_text_region import split_textblock as split_text_region
from . import shared

LANG_LIST = ['eng', 'ja', 'unknown']
LANGCLS2IDX = {'eng': 0, 'ja': 1, 'unknown': 2}
@@ -50,6 +51,31 @@ def sort_pnts(pts: np.ndarray):
        return pts_sorted, is_vertical


fontweight_qt5_to_qt6 = {0: 100, 12: 200, 25: 300, 50: 400, 57: 500, 63: 600, 75: 700, 81: 800, 87: 900}
fontweight_qt6_to_qt5 = {100: 0, 200: 12, 300: 25, 400: 50, 500: 57, 600: 63, 700: 75, 800: 81, 900: 87}

fontweight_pattern = re.compile(r'font-weight:(\d+)', re.DOTALL)

def fix_fontweight_qt(weight: Union[str, int]):

    def _fix_html_fntweight(matched):
        weight = int(matched.group(1))
        return f'font-weight:{fix_fontweight_qt(weight)}'

    if weight is None:
        return None
    if isinstance(weight, int):
        if shared.FLAG_QT6 and weight < 100:
            if weight in fontweight_qt5_to_qt6:
                weight = fontweight_qt5_to_qt6[weight]
        if not shared.FLAG_QT6 and weight >= 100:
            if weight in fontweight_qt6_to_qt5:
                weight = fontweight_qt6_to_qt5[weight]
    if isinstance(weight, str):
        weight = fontweight_pattern.sub(lambda matched: _fix_html_fntweight(matched), weight)
    return weight


@nested_dataclass
class TextBlock:
    xyxy: List = field(default_factory = lambda: [0, 0, 0, 0])
@@ -102,6 +128,9 @@ class TextBlock:
            self.vec = np.array(self.vec, np.float32)
        if self.src_is_vertical is None:
            self.src_is_vertical = self.vertical
        self.font_weight = fix_fontweight_qt(self.font_weight)
        if self.rich_text:
            self.rich_text = fix_fontweight_qt(self.rich_text)

        da = self.deprecated_attributes
        if len(da) > 0: