def size(self, cr): min_w = self.style("min-width") min_h = self.style("min-height") padding = self.style("padding") width, height = text_size(cr, self.text(), self.font(), self.width()) return ( max(min_w, width + padding[Padding.RIGHT] + padding[Padding.LEFT]), max(min_h, height + padding[Padding.TOP] + padding[Padding.BOTTOM]), )
def size(self, context: SizeContext): style = combined_style(context.style, self._inline_style) min_w = style["min-width"] min_h = style["min-height"] padding = style["padding"] width, height = text_size(context.cairo, self.text(), style, self.width()) # type: ignore[type-var] return ( max(min_w, width + padding[Padding.RIGHT] + padding[Padding.LEFT]), max(min_h, height + padding[Padding.TOP] + padding[Padding.BOTTOM]), )
def test_text_with_font_as_dict(cr): w, h = text_size( cr, "Example", { "font": "sans 10", "font-style": FontStyle.ITALIC, "font-weight": FontWeight.BOLD, "text-decoration": TextDecoration.UNDERLINE, }, ) assert w assert h
def test_text_with_font_as_dict_with_values_set_to_none(cr): w, h = text_size( cr, "Example", { "font": "sans bold 10", "font-style": None, "font-weight": None, "text-decoration": TextDecoration.NONE, }, ) assert w assert h
def test_text_with_font_as_dict_with_values_set_to_none(cr): w, h = text_size( cr, "Example", { "font-family": "sans", "font-size": 9.5, "font-style": None, "font-weight": FontWeight.BOLD, "text-decoration": TextDecoration.NONE, }, ) assert w assert h
def post_update(self, context, p1, p2): """ Update label placement for association's name and multiplicity label. p1 is the line end and p2 is the last but one point of the line. """ cr = context.cairo ofs = 5 name_dx = 0.0 name_dy = 0.0 mult_dx = 0.0 mult_dy = 0.0 dx = float(p2[0]) - float(p1[0]) dy = float(p2[1]) - float(p1[1]) def max_text_size(size1, size2): w1, h1 = size1 w2, h2 = size2 return (max(w1, w2), max(h1, h2)) name_w, name_h = max_text_size(text_size(cr, self._name, self.font), (10, 10)) mult_w, mult_h = max_text_size(text_size(cr, self._mult, self.font), (10, 10)) if dy == 0: rc = 1000.0 # quite a lot... else: rc = dx / dy abs_rc = abs(rc) h = dx > 0 # right side of the box v = dy > 0 # bottom side if abs_rc > 6: # horizontal line if h: name_dx = ofs name_dy = -ofs - name_h mult_dx = ofs mult_dy = ofs else: name_dx = -ofs - name_w name_dy = -ofs - name_h mult_dx = -ofs - mult_w mult_dy = ofs elif 0 <= abs_rc <= 0.2: # vertical line if v: name_dx = -ofs - name_w name_dy = ofs mult_dx = ofs mult_dy = ofs else: name_dx = -ofs - name_w name_dy = -ofs - name_h mult_dx = ofs mult_dy = -ofs - mult_h else: # Should both items be placed on the same side of the line? r = abs_rc < 1.0 # Find out alignment of text (depends on the direction of the line) align_left = h ^ r align_bottom = v ^ r if align_left: name_dx = ofs mult_dx = ofs else: name_dx = -ofs - name_w mult_dx = -ofs - mult_w if align_bottom: name_dy = -ofs - name_h mult_dy = -ofs - name_h - mult_h else: name_dy = ofs mult_dy = ofs + mult_h self._name_bounds = Rectangle(p1[0] + name_dx, p1[1] + name_dy, width=name_w, height=name_h) self._mult_bounds = Rectangle(p1[0] + mult_dx, p1[1] + mult_dy, width=mult_w, height=mult_h)
def test_text_with_font_as_string(cr): w, h = text_size(cr, "Example", "sans 10") assert w assert h
def test_text_with_just_font_as_dict(cr): w, h = text_size(cr, "Example", {"font": "sans 10"}) assert w assert h
def test_text_with_just_font_as_dict(cr): w, h = text_size(cr, "Example", {"font-family": "sans", "font-size": 10}) assert w assert h