From 16a75aebc1ce2bedb63ed33268f596b7174aff19 Mon Sep 17 00:00:00 2001 From: keenanlang Date: Mon, 29 Jul 2024 15:17:24 -0500 Subject: [PATCH] adjust for generation on different DPI screens --- gestalt/Generator.py | 35 +++++++++++++++++++++++ gestalt/Node.py | 17 +++-------- gestalt/convert/phoebus/CSSTabbedGroup.py | 27 +++-------------- gestalt/convert/qt/QtTabbedGroup.py | 6 ++++ 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/gestalt/Generator.py b/gestalt/Generator.py index 75e2691..7a84126 100644 --- a/gestalt/Generator.py +++ b/gestalt/Generator.py @@ -1,4 +1,39 @@ +import tkinter as tk +import tkinter.font as tkfont + +DEFAULT_DPI = 96.0 + class GestaltGenerator: + def get_font_height(font_name, font_size): + tk_root = tk.Tk() + tk_root.withdraw() + + tk_font = tkfont.Font(family=font_name, size=font_size) + + dpi_scale = DEFAULT_DPI / tk_root.winfo_fpixels("1i") + + ascent = round(tk_font.metrics("ascent") * dpi_scale) + descent = round(tk_font.metrics("descent") * dpi_scale) + + tk_root.destroy() + + return int(ascent + descent) + + def get_text_width(font_name, font_size, text): + tk_root = tk.Tk() + tk_root.withdraw() + + tk_font = tkfont.Font(family=font_name, size=font_size) + + dpi_scale = DEFAULT_DPI / tk_root.winfo_fpixels("1i") + + output = tk_font.measure(text) * dpi_scale + + tk_root.destroy() + + return output + + def generateWidget(self, original, macros={}): pass diff --git a/gestalt/Node.py b/gestalt/Node.py index f482656..7151b97 100644 --- a/gestalt/Node.py +++ b/gestalt/Node.py @@ -4,13 +4,10 @@ import pprint import string +from gestalt.Generator import GestaltGenerator from gestalt.Datasheet import * from gestalt.Type import * -import tkinter as tk -import tkinter.font as tkfont - - class Node(object): def __init__(self, classname, name=None, node=None, layout={}, loc=None): self.classname = classname @@ -289,16 +286,10 @@ def apply(self, generator, data={}): self.log("Generating Tabbed Group") output = generator.generateTabbedGroup(self, macros=data) - tk_root = tk.Tk() - tk_root.withdraw() - the_font = output["font"] - tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"])) - - tab_bar_height = tk_font.metrics("linespace") + 4 + int(output["offset"]) - - tk_root.destroy() + tab_bar_height = GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"])) + tab_bar_height += 4 + int(output["offset"]) border_size = int(output["border-width"]) @@ -680,7 +671,7 @@ def apply (self, generator, data={}): class RelatedDisplayNode(Node): - def __init__(self, name=None, layout={}, loc=None): + def __init__(self, name=None, layout={}, loc=None): self.links = layout.pop("links", []) super(RelatedDisplayNode, self).__init__("RelatedDisplay", name=name, layout=layout, loc=loc) diff --git a/gestalt/convert/phoebus/CSSTabbedGroup.py b/gestalt/convert/phoebus/CSSTabbedGroup.py index 7b247c6..b71e13f 100644 --- a/gestalt/convert/phoebus/CSSTabbedGroup.py +++ b/gestalt/convert/phoebus/CSSTabbedGroup.py @@ -1,14 +1,11 @@ from phoebusgen import screen +from gestalt.Generator import GestaltGenerator from gestalt.Type import * from gestalt.convert.phoebus.CSSWidget import CSSWidget from gestalt.convert.phoebus.CSSGroup import CSSGroup -import tkinter as tk -import tkinter.font as tkfont - - class CSSTabbedGroup(CSSWidget): def __init__(self, node=None, macros={}): @@ -34,18 +31,10 @@ def __init__(self, node=None, macros={}): the_font = self.font.val() - # Use Tkinter to determine height and width - tk_root = tk.Tk() - tk_root.withdraw() - - tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"])) - - self.tab_height = tk_font.metrics("linespace") + 4 + self.tab_height = GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"])) + 4 self.content_offset = self.content_offset + self.tab_height self.index = 0 - - tk_root.destroy() def write(self, screen): self.pop("font") @@ -65,16 +54,8 @@ def write(self, screen): def place(self, child, x=None, y=None, keep_original=False): the_font = self.font.val() - # Use Tkinter to determine height and width - tk_root = tk.Tk() - tk_root.withdraw() - - tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"])) - - tab_width = tk_font.measure(child.name) + 10 - - tk_root.destroy() - + tab_width = GestaltGenerator.get_text_width(the_font["family"], int(the_font["size"]), child.,name) + 10 + next_tab = CSSWidget("ActionButton", name=child.name + "_tab") next_tab["geometry"] = Rect("{x}x{y}x{wid}x{hei}".format(x=self.tab_offset, y=0, wid=tab_width, hei=self.tab_height)) next_tab["font"] = self.font diff --git a/gestalt/convert/qt/QtTabbedGroup.py b/gestalt/convert/qt/QtTabbedGroup.py index 65dc417..b8efb46 100644 --- a/gestalt/convert/qt/QtTabbedGroup.py +++ b/gestalt/convert/qt/QtTabbedGroup.py @@ -1,3 +1,4 @@ +from gestalt.Generator import GestaltGenerator from gestalt.Type import * from gestalt.convert.qt.QtWidget import QtWidget @@ -20,6 +21,8 @@ def write(self, tree): """.format( inset=int(tab_offset) ) + the_font = self["font"].val() + tab_color = self.pop("tab-color") font_color = self.pop("foreground") tab_padding = self.pop("padding") @@ -35,6 +38,8 @@ def write(self, tree): padding-bottom: 2px; padding-left: 5px; padding-right: 5px; + + height: {tab_height}px; background: rgba({br},{bg},{bb},{ba}); color: rgba({tr},{tg},{tb},{ta}); @@ -49,6 +54,7 @@ def write(self, tree): """.format( margin = int(tab_padding), offset = int(content_offset), + tab_height = 9 + GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"])), br = int(tab_color["red"]), bg = int(tab_color["green"]), bb = int(tab_color["blue"]),