Skip to content

Commit

Permalink
more Phoebus parity
Browse files Browse the repository at this point in the history
  • Loading branch information
keenanlang committed Feb 17, 2025
1 parent 714986d commit 7600413
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
24 changes: 24 additions & 0 deletions gestalt/Generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

DEFAULT_DPI = 96.0

size_cache = {}

class GestaltGenerator:
def get_font_height(font_name, font_size):
tk_root = tk.Tk()
Expand All @@ -19,6 +21,28 @@ def get_font_height(font_name, font_size):

return int(ascent + descent)

def get_size_for_height(font_name, height):
font_size = 1

if font_name not in size_cache:
size_cache[font_name] = {}

if height in size_cache[font_name]:
return size_cache[font_name][height]

while True:
estimated_size = GestaltGenerator.get_font_height(font_name, font_size)
needed_height = height * 0.75

font_size += 1

if (estimated_size > needed_height):
break

size_cache[font_name][height] = font_size - 1

return font_size - 1

def get_text_width(font_name, font_size, text):
tk_root = tk.Tk()
tk_root.withdraw()
Expand Down
6 changes: 3 additions & 3 deletions gestalt/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ def apply (self, generator, data={}):
"__parenty__" : int(geom["y"]),
"__parentwidth__" : int(geom["width"]) - int(margins["x"]) - int(margins["width"]) - 2 * border,
"__parentheight__" : int(geom["height"]) - int(margins["y"]) - int(margins["height"]) - 2 * border})

self.updateMacros(child_macros)

widget = child.apply(generator, data=child_macros)

if widget:
Expand Down Expand Up @@ -656,7 +656,7 @@ def apply (self, generator, data={}):
applied_node.name = self.name

applied_node = applied_node.apply(generator, data=data)

applied_node["geometry"]["x"] = applied_node["geometry"]["x"] + self["geometry"]["x"]
applied_node["geometry"]["y"] = applied_node["geometry"]["y"] + self["geometry"]["y"]

Expand Down
21 changes: 17 additions & 4 deletions gestalt/convert/phoebus/CSSDisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
from gestalt.Type import *

from gestalt.Node import GroupNode
from gestalt.convert.phoebus.CSSAnonymous import CSSAnonymous

class CSSDisplay(GroupNode):
class CSSDisplay(CSSAnonymous):
def __init__(self, layout={}):
super(CSSDisplay, self).__init__("")
super(CSSDisplay, self).__init__()

self.tocopy.append("content")

self.content = CSSAnonymous()

def place(self, child, x=None, y=None, keep_original=False):
self.content.place(child, x=x, y=y, keep_original=keep_original)

def writeCSS(self, filename):
self.form = screen.Screen(str(String(self.pop("title", ""))))

margins = Rect(self.pop("margins", "0x0x0x0"))

self.form.width( self["geometry"]["width"] )
self.form.height( self["geometry"]["height"] )
self.form.width(int(self.content["geometry"]["width"] + margins["x"] + margins["width"]))
self.form.height(int(self.content["geometry"]["height"] + margins["y"] + margins["height"]))

self.content["geometry"]["x"] = margins["x"]
self.content["geometry"]["y"] = margins["y"]

col = None

Expand All @@ -27,6 +38,8 @@ def writeCSS(self, filename):

self.form.background_color(col["red"], col["green"], col["blue"], col["alpha"])

self.content.write(self.form)

for child in self.children:
child.write(self.form)

Expand Down
10 changes: 5 additions & 5 deletions gestalt/convert/phoebus/CSSGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ def generateCSSFile(template, data, outputfile=""):
a_display.updateProperties(data)
else:
data.update({
"__parentx__" : a_display["geometry"]["x"],
"__parenty__" : a_display["geometry"]["y"],
"__parentwidth__" : a_display["geometry"]["width"],
"__parentheight__" : a_display["geometry"]["height"]})

"__parentx__" : a_display.content["geometry"]["x"],
"__parenty__" : a_display.content["geometry"]["y"],
"__parentwidth__" : a_display.content["geometry"]["width"],
"__parentheight__" : a_display.content["geometry"]["height"]})
a_display.place(item.apply(the_generator, data=data))


Expand Down
9 changes: 6 additions & 3 deletions gestalt/convert/phoebus/CSSWidget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from gestalt.Type import *

from gestalt.Node import GroupNode
from gestalt.Generator import GestaltGenerator

from phoebusgen import widget
from phoebusgen.widget import properties as _p
Expand Down Expand Up @@ -30,7 +31,7 @@ def get_pv(pvname):
class CSSWidget(GroupNode):
def __init__(self, classname, node=None, name=None, layout={}, macros={}):
super(CSSWidget, self).__init__(classname, name=name, node=node, layout=layout)
self.updateProperties(macros)
CSSWidget.updateProperties(self,macros)

if "alignment" in self:
data = str(Alignment(self.pop("alignment")))
Expand Down Expand Up @@ -216,9 +217,11 @@ def setFontParam(self, attribute, prefix, check_class=object):
return

getattr(self.widget, prefix + "font_family")(my_font["family"])


font_size = GestaltGenerator.get_size_for_height(my_font["family"], int(self["geometry"]["height"]))

if my_font["size"]:
getattr(self.widget, prefix + "font_size")(int(my_font["size"]))
getattr(self.widget, prefix + "font_size")(int(font_size))

if my_font["style"]:
getattr(self.widget, prefix + "font_style_" + my_font["style"].lower())()
Expand Down

0 comments on commit 7600413

Please sign in to comment.