Skip to content

Commit

Permalink
add font weight conversion for qt 6
Browse files Browse the repository at this point in the history
  • Loading branch information
dmMaze committed Aug 30, 2024
1 parent 1c37545 commit 5c8344e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ui/fontformat_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def ffmt_change_weight(param_name: str, values: str, act_ffmt: FontFormat, is_gl
@font_formating()
def ffmt_change_bold(param_name: str, values: str, act_ffmt: FontFormat, is_global: bool, blkitems: List[TextBlkItem] = None, **kwargs):
set_kwargs = global_default_set_kwargs if is_global else local_default_set_kwargs
values = [QFont.Bold if value else QFont.Normal for value in values]
values = [QFont.Weight.Bold if value else QFont.Weight.Normal for value in values]
# ffmt_change_weight('weight', values, act_ffmt, is_global, blkitems, **kwargs)
for blkitem, value in zip(blkitems, values):
blkitem.setFontWeight(value, **set_kwargs)
Expand Down
12 changes: 4 additions & 8 deletions ui/textitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def get_fontformat(self) -> FontFormat:
weight = font.weight()
# https://doc.qt.io/qt-5/qfont.html#Weight-enum, 50 is normal
if weight == 0:
weight = 50
weight = QFont.Weight.Normal

return FontFormat(
font.family(),
Expand Down Expand Up @@ -833,13 +833,9 @@ def setLetterSpacing(self, value: float, repaint_background: bool = True, set_se

def setFontColor(self, value: Tuple, repaint_background: bool = False, set_selected: bool = False, restore_cursor: bool = False, force=False):
cursor, after_kwargs = self._before_set_ffmt(set_selected, restore_cursor)
if not self.document().isEmpty():
fraghtml = cursor.selection().toHtml()
cursor.insertHtml(set_html_color(fraghtml, value))
else:
cfmt = cursor.charFormat()
cfmt.setForeground(QColor(*value))
self.set_cursor_cfmt(cursor, cfmt, True)
cfmt = cursor.charFormat()
cfmt.setForeground(QColor(*value))
self.set_cursor_cfmt(cursor, cfmt, True)
self._after_set_ffmt(cursor, repaint_background=repaint_background, restore_cursor=restore_cursor, **after_kwargs)

def setStrokeColor(self, scolor, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions utils/fontformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ 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 @@ -52,6 +56,15 @@ def update_from_textblock(self, text_block: TextBlock):
self.shadow_color = text_block.shadow_color
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]

def update_textblock_format(self, blk: TextBlock):
blk.default_stroke_width = self.stroke_width
blk.line_spacing = self.line_spacing
Expand Down

0 comments on commit 5c8344e

Please sign in to comment.