Commit f7dddae9 authored by birk's avatar birk
Browse files

avoid too wide texts

parent 9987b4b7
......@@ -186,36 +186,34 @@ class Screen(Window):
return not self.media and not self.__info_mode
@staticmethod
def find_text_size(text_, max_width_, max_height_=None, bold_=False):
def find_text_size(text_:str, max_width_:float, max_height_:float=None, bold_:bool=False):
"""
:param text_: String
:param max_width_: int
:param max_height_: int
:param bold_: Boolean
:return: float
"""
s = 50
if max_height_:
# take max_width_ as fixed and find text size for max_height_
while Screen.get_text_height(text_, s, max_width_) < max_height_:
s += 2
s += 1
while Screen.get_text_height(text_, s, max_width_) > max_height_:
s -= 2
s -= 1
# also check width
_, w = Screen.get_text_height_width(text_, s, max_width_)
if w > max_width_:
while w > max_width_:
s -= 1
_, w = Screen.get_text_height_width(text_, s, max_width_)
else:
# find text size
while Screen.get_text_width(text_, s, bold_) < max_width_:
s += 2
s += 1
while Screen.get_text_width(text_, s, bold_) > max_width_:
s -= 2
s -= 1
return s
@staticmethod
def get_text_width(text_, size_, bold_=False):
def get_text_width(text_:str, size_:float, bold_:bool=False):
"""
:param text_: String
:param size_: float
:param bold_: Boolean
:return: float
"""
l = Label(text_, font_name=Config.FONT, bold=bold_,
......@@ -226,12 +224,8 @@ class Screen(Window):
return r
@staticmethod
def get_text_height(text_, size_, width_):
def get_text_height(text_:str, size_:float, width_:float):
"""
:param text_: String
:param size_: float
:param width_: float
:param bold_: Boolean
:return: float
"""
d = Screen.get_formatted_text(text_, size_)
......@@ -240,6 +234,18 @@ class Screen(Window):
t.delete()
return r
@staticmethod
def get_text_height_width(text_:str, size_:float, width_:float):
"""
:return: float
"""
d = Screen.get_formatted_text(text_, size_)
t = InfoBox(d, 0, 0, width_)
h = t.content_height
w = t.content_width
t.delete()
return h, w
@staticmethod
def get_formatted_text(text_, size_, align_='left'):
d = pyglet.text.decode_text(text_)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment