def _required_size_for_cells(cells: List[List[AutosizeTableCell]], base_font_size: float) -> Tuple[int, int]: required_width = 0 required_height = 0 label = None last_font_size = -1 for row in cells: row_height = 0 sumw = sum([cell.layout_weight for cell in row]) weight_ratios = [sumw / cell.layout_weight for cell in row] for cell, wrat in zip(row, weight_ratios): fsize = base_font_size * cell.relative_font_size # Updating an existing label's font size doesn't affect its computed # size, so we have to recreate it if the font size changes. if label is None or last_font_size != fsize: debug(f'New label: {fsize}') pad = fsize * cell.relative_padding label = CoreLabel(font_size=fsize, padding=pad) last_font_size = fsize label.text = cell.text label.refresh() size = label.texture.size required_width = max(required_width, size[0] * wrat) row_height = max(row_height, size[1]) required_height += row_height return (required_width, required_height)
def btn_left(self, flayout, text_input): if text_input.text != "": str2 = text_input.text float = FloatLayout() float.pos_hint = {'x': 0, 'y': 0} my_label = CoreLabel() my_label.text = str2 my_label.refresh() newsize = my_label.size mybtn = Button(text=str2, size_hint_y=None, size_hint_x=None, size=(newsize[0] + 10, newsize[1] + 10), pos_hint={ 'x': 0, 'y': 0 }) mybtn.font_size = 12 mybtn.font_name = 'Arial' mybtn.border = 0, 0, 0, 0 mybtn.background_normal = 'gr.png' float.add_widget(mybtn) flayout.add_widget(float) text_input.text = ""
def redraw(self): self.canvas.clear() with self.canvas: Color(*COL_FG) total_ticks = int(self.total_axis_space / self.space_btw_marks) for i in range(total_ticks): if i % 5 == 0: # Long Mark mark = self.mark_length else: # Short Mark mark = self.mark_length / 2 Line(points=[ self.fwidth - mark, i * self.space_btw_marks, self.fwidth, i * self.space_btw_marks, self.fwidth, (i + 1) * self.space_btw_marks ]) i = int(self.total_axis_space / self.space_btw_marks) Line(points=[ self.fwidth - self.mark_length, i * self.space_btw_marks, self.fwidth, i * self.space_btw_marks ]) for i in range(int(total_ticks / 5) + 1): label = CoreLabel() label.text = "{:.1f}".format(0.1 * i) label.refresh() label_texture = label.texture texture_size = list(label_texture.size) Rectangle(texture=label_texture, size=[s * 0.9 for s in texture_size], pos=(0, i * 5 * self.space_btw_marks - 2))
def label_size(text: str, font_size: float) -> Tuple[int, int]: label = CoreLabel(text=text, font_size=font_size) label.refresh() r1 = label.texture.size label.text = text + text label.refresh() r2 = label.texture.size return r1 + r2
def get_text_width(text, font, root=None): logging.info("get_text_width: %s %s" % (text, font)) label = CoreLabel() label.text = text label.refresh() return label.content_width
def banner_and_background(Screen): with Screen.canvas: Color(0.4, 0.4, 0.4, 0.3) Rectangle(pos=root.pos, size=(screenx, screeny)) Color(0.2, 0.2, 0.2, 0.7) Rectangle(pos=(150, 500), size=(500, 100)) label = Label() label.font_size = '36sp' label.pos = (350, 500) label.text = "AttenBuddy-Desktop" Screen.add_widget(label)
def get_text_width_height_descent(self, s, prop, ismath): '''This method is needed specifically to calculate text positioning in the canvas. Matplotlib needs the size to calculate the points according to their layout ''' if ismath: ftimage, depth = self.mathtext_parser.parse(s, self.dpi, prop) w = ftimage.get_width() h = ftimage.get_height() return w, h, depth font = resource_find(prop.get_name() + ".ttf") if font is None: plot_text = CoreLabel(font_size=prop.get_size_in_points()) else: plot_text = CoreLabel(font_size=prop.get_size_in_points(), font_name=prop.get_name()) plot_text.text = six.text_type("{}".format(s)) plot_text.refresh() return plot_text.texture.size[0], plot_text.texture.size[1], 1
def spawn_text(self, text, position, size=0.1, orientation="y", color=(1, 1, 1, 1)): my_label = CoreLabel(font_size=int(200 * size)) my_label.text = text my_label.refresh() aspect = float(my_label.texture.size[0]) / my_label.texture.size[1] quad = Quad(Material(map=my_label.texture, color=color[:3], transparency=color[3]), size * aspect, size, orientation=orientation) quad.label = my_label self.spawn(quad, position)
def draw(self): with self.canvas: line_kwargs = { 'points': [], 'width': 1, } self.color = Color(rgba=(1, 0, 0, 1.)) self.time_lines = [ Line(**line_kwargs) for i in range(self.TIME_INTERVAL_COUNT + 1) ] self.hours_core_labels = [] self.hours_rect_labels = [] for hour in range(24 + 1): label = CoreLabel() label.text = "%02d" % hour label.refresh() self.hours_core_labels.append(label) self.hours_rect_labels.append( Rectangle(size=label.size, texture=label.texture))
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): '''Render text that is displayed in the canvas. The position x, y is given in matplotlib coordinates. A `GraphicsContextKivy` is given to render according to the text properties such as color, size, etc. An angle is given to change the orientation of the text when needed. If the text is a math expression it will be rendered using a MathText parser. ''' if mtext: transform = mtext.get_transform() ax, ay = transform.transform_point(mtext.get_position()) angle_rad = mtext.get_rotation() * np.pi / 180. dir_vert = np.array([np.sin(angle_rad), np.cos(angle_rad)]) if mtext.get_rotation_mode() == "anchor": # if anchor mode, rotation is undone first v_offset = np.dot(dir_vert, [(x - ax), (y - ay)]) ax = ax + v_offset * dir_vert[0] ay = ay + v_offset * dir_vert[1] w, h, d = self.get_text_width_height_descent(s, prop, ismath) ha, va = mtext.get_ha(), mtext.get_va() if ha == "center": ax -= w / 2 elif ha == "right": ax -= w if va == "top": ay -= h elif va == "center": ay -= h / 2 if mtext.get_rotation_mode() != "anchor": # if not anchor mode, rotation is undone last v_offset = np.dot(dir_vert, [(x - ax), (y - ay)]) ax = ax + v_offset * dir_vert[0] ay = ay + v_offset * dir_vert[1] x, y = ax, ay x += self.widget.x y += self.widget.y if ismath: self.draw_mathtext(gc, x, y, s, prop, angle) else: font = resource_find(prop.get_name() + ".ttf") if font is None: plot_text = CoreLabel(font_size=prop.get_size_in_points()) else: plot_text = CoreLabel(font_size=prop.get_size_in_points(), font_name=prop.get_name()) plot_text.text = six.text_type("{}".format(s)) if prop.get_style() == 'italic': plot_text.italic = True if weight_as_number(prop.get_weight()) > 500: plot_text.bold = True plot_text.refresh() with self.widget.canvas: if isinstance(angle, float): PushMatrix() Rotate(angle=angle, origin=(int(x), int(y))) Rectangle(pos=(int(x), int(y)), texture=plot_text.texture, size=plot_text.texture.size) PopMatrix() else: Rectangle(pos=(int(x), int(y)), texture=plot_text.texture, size=plot_text.texture.size)
def label(i, j, text): label = Label() label.font_size = '18sp' label.pos = (i, j) label.text = text return label
def subhead(Screen, text, i, j): label = Label() label.font_size = '30sp' label.pos = (i, j) label.text = text Screen.add_widget(label)
def get_texture(text): l = CoreLabel() l.text = str(text) l.refresh() return l.texture