Skip to content

Commit

Permalink
fix font weight conversion between qt5 and qt6
Browse files Browse the repository at this point in the history
  • Loading branch information
dmMaze committed Aug 30, 2024
1 parent 5c8344e commit 2debb6d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
17 changes: 5 additions & 12 deletions utils/fontformat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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:
Expand All @@ -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):

Expand Down Expand Up @@ -57,13 +56,7 @@ def update_from_textblock(self, text_block: TextBlock):
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
Expand Down
29 changes: 29 additions & 0 deletions utils/textblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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}
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -102,6 +128,9 @@ def __post_init__(self):
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:
Expand Down

0 comments on commit 2debb6d

Please sign in to comment.