I have a kivy label with mixed font sizes. How can one correctly calculate the bounding boxes of certain parts of the text? I am currently using the kivy markup ref tag. This works well if the font sizes are not mixed within one line of text. If they are mixed, then the sizes are correct, but the positions are relative to the top of the line, which is incorrect for all but the maximal font size (for the current line).
Here is a minimal example that illustrates the problem:
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics import Rectangle, Color
from kivy.uix.label import Label
TEXT = 'Lorem [ref=r1]ipsum[/ref] dolor [ref=r2][size=30]sit[/size][/ref] amet, [ref=r3][size=5]consectetur[/size][/ref] adipiscing...'
class MyLabel(Label):
def show_boxes(self, *_):
print(self.refs)
for boxes in self.refs.values():
for box in boxes:
x = self.center_x - 0.5 * self.texture_size[0] + box[0]
y = self.center_y + 0.5 * self.texture_size[1] - box[3]
w = box[2] - box[0]
h = box[3] - box[1]
self.canvas.add(Color(rgba=[0,1,1,.5]))
self.canvas.add(Rectangle(pos=(x, y),size=(w, h)))
class MyApp(App):
def build(self):
self.l = MyLabel(text=TEXT, markup=True)
Clock.schedule_once(self.l.show_boxes)
return self.l
MyApp().run()
To me this does not look like the intended behavior. I guess I could go through the boxes and adjust the position, but for that I need the actual font geometry. Should I dive into the internals of text layout, or is there an easier way?
I tested this with kivy 1.11.1 and (the awesome and long awaited) kivy 2.0, python3.6.9, on linux.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…