def split_line(width, x, max_x, line, lines, fontid=0): words = line.split(" ") line = "" for word in words: c = (word if not line else " " + word) x_dx = x + blf.dimensions(fontid, line+c)[0] if (x_dx) > width: x_dx = blf.dimensions(fontid, line)[0] if not line: # one word is longer than the width line, x, max_x = split_word( width, x, max_x, word, lines, fontid) else: lines.append(line) line = word x = 0 else: line += c max_x = max(x_dx, max_x) if line: lines.append(line) return max_x
def draw_text(x_pos, y_pos, display_text, rgb, fsize, right=False): gap = 12 font_id = 0 blf.size(font_id, fsize, 72) # height of one line mwidth, mheight = blf.dimensions(font_id, "Tp") # uses high/low letters # split lines mylines = display_text.split("|") idx = len(mylines) - 1 maxwidth = 0 maxheight = len(mylines) * mheight # ------------------- # Draw all lines # ------------------- for line in mylines: text_width, text_height = blf.dimensions(font_id, line) if right is True: newx = x_pos - text_width - gap else: newx = x_pos # calculate new Y position new_y = y_pos + (mheight * idx) # Draw blf.position(font_id, newx, new_y, 0) bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3]) blf.draw(font_id, " " + line) # sub line idx -= 1 # saves max width if maxwidth < text_width: maxwidth = text_width return maxwidth, maxheight
def draw_textbox(text, origin, textsize=6, textcolor=colors['white'], backgroundcolor=colors['background'], offset=Vector((0.0, 0.0)), linewidth=2, hborder=3, vborder=4, rightalign=False, indicator_line=True): blf.size(0, textsize, 150) width = blf.dimensions(0, text)[0] height = blf.dimensions(0, text)[1] if rightalign: origin = origin + Vector((-width - 2 * hborder, 0)) + offset else: origin = origin + offset points = ((origin + Vector((-hborder, -vborder * 1.5)), origin + Vector( (width + hborder, -vborder * 1.5)), origin + Vector( (width + hborder, height + vborder)), origin + Vector( (-hborder, height + vborder)))) draw_2dpolygon(points, fillcolor=backgroundcolor, linecolor=textcolor, linewidth=linewidth) draw_text(text, position=origin, size=textsize, color=textcolor)
def draw_callback_px(self, context): wm = context.window_manager sc = context.scene if not wm.polycount_run: return font_size = sc.polycount_font_size pos_x, pos_y = get_display_location(context) # draw text in the 3d-view # ======================== blf.size(0, sc.polycount_font_size, 72) r, g, b = sc.polycount_font_color bgl.glColor3f(r, g, b) view3dId = get_space_id(context.space_data) visible_tri_count = PolyCountOperator.visible_triangle_count.get(view3dId, -1) if visible_tri_count > -1: text = "All: " + format(visible_tri_count, 'd') + "triangles" text1height = blf.dimensions(0, text)[1] blf.position(0, pos_x, pos_y - text1height, 0) blf.draw(0, text) selection_tri_count = PolyCountOperator.selection_triangle_count.get(view3dId, -1); if selection_tri_count > 0: text = "Selection: " + format(selection_tri_count, ',d') + " triangles" text2height = blf.dimensions(0, text)[1] blf.position(0, pos_x, pos_y - text1height - text2height - 5, 0) blf.draw(0, text)
def draw_callback_px(self, context): for n, area in enumerate(self.areas): if context.area.x == area.x and context.area.y == area.y: #then this is where our mouse is font_id = 0 # XXX, need to find out how best to get this. height = context.region.height width = context.region.width dims = blf.dimensions(0, self.messages[n]) #blf.position(font_id, 10, height - 10 - dims[1], 0) #top left blf.position(font_id, width - 10 - 2 * dims[0], 10 + dims[1] / 2, 0) blf.size(font_id, 20, 72) blf.draw(font_id, self.messages[n]) if (self.mouse_raw[0] > area.x and self.mouse_raw[0] < area.x + area.width) and \ (self.mouse_raw[1] > area.y and self.mouse_raw[1] < area.y + area.height): #label the mouse dims = blf.dimensions(0, 'MOUSE %i' % n) x = self.mouse_region_coord[0] - .5 * dims[0] y = self.mouse_region_coord[1] + dims[1] blf.position(font_id, x, y, 0) blf.draw(font_id, 'MOUSE %i' % n) if len(self.area_data[n]): N = len(self.areas) color = (n / N, .5 * n / N, (1 - n / N), 1) draw_3d_points_specific_region(context.region, context.space_data.region_3d, self.area_data[n], color, 3)
def draw_callback_px(tmp, self, context): region = context.region draw_bg(region) xt = int(region.width / 2.0) if self._mode == 0: mode = "Difference" elif self._mode == 1: mode = "Union" else: mode = "Slice" text = "Mode (M): {0}".format(mode) # Big font font_id = 0 create_font(font_id, 25) # Small font s_font_id = 1 create_font(s_font_id, 10) draw_text(text, xt - blf.dimensions(font_id, text)[0] / 2, 60, font_id) text_info = "Apply: Space | Finish: Esc" draw_text(text_info, xt - blf.dimensions(s_font_id, text_info)[0] / 2, 30, s_font_id)
def draw_callback_px(self, context): for n, area in enumerate(self.areas): if context.area.x == area.x and context.area.y == area.y: #then this is where our mouse is font_id = 0 # XXX, need to find out how best to get this. height = context.region.height width = context.region.width dims = blf.dimensions(0, self.messages[n]) #blf.position(font_id, 10, height - 10 - dims[1], 0) #top left blf.position(font_id, width - 10 - 2* dims[0], 10 + dims[1]/2, 0) blf.size(font_id, 20, 72) blf.draw(font_id, self.messages[n]) if (self.mouse_raw[0] > area.x and self.mouse_raw[0] < area.x + area.width) and \ (self.mouse_raw[1] > area.y and self.mouse_raw[1] < area.y + area.height): #label the mouse dims = blf.dimensions(0,'MOUSE %i' %n) x = self.mouse_region_coord[0] - .5 * dims[0] y = self.mouse_region_coord[1] + dims[1] blf.position(font_id,x,y,0) blf.draw(font_id,'MOUSE %i' % n) if len(self.area_data[n]): N = len(self.areas) color = (n/N, .5*n/N, (1-n/N), 1) draw_3d_points_specific_region(context.region, context.space_data.region_3d, self.area_data[n], color, 3)
def draw_callback(self, context): if context.region.as_pointer() != self.region: return glsettings = vagl.GLSettings(context) glsettings.push() cursor_2d = project(context.region, context.region_data, self.get_cursor_location(context)) unit_settings = context.scene.unit_settings text_lines = [] for i in range(3): if unit_settings.system == 'NONE': text = '{:.5f}'.format(self.cursor_location[i]) else: text = bpy.utils.units.to_string( unit_settings.system, 'LENGTH', self.cursor_location[i], 5, split_unit=unit_settings.use_separate ) text_lines.append(text) col = context.user_preferences.themes['Default'].view_3d.space.text_hi bgl.glColor3f(*col) blf.size(0, 12, context.user_preferences.system.dpi) tw_max = max(blf.dimensions(0, t)[0] for t in text_lines) if 0: p = [cursor_2d[0] - tw_max - 5, cursor_2d[1] - 40, 0] else: p = [self.mco[0] - tw_max - 5, self.mco[1] - 40, 0] th = blf.dimensions(0, '-.0123456789')[1] lf = th * 1.2 for i, text in enumerate(text_lines): tw, _ = blf.dimensions(0, text) blf.position(0, p[0] + tw_max - tw, p[1], p[2]) vagl.blf_draw(0, text) p[1] -= lf if 0: p = [cursor_2d[0] + 5, cursor_2d[1] - 40, 0] else: p = [self.mco[0] + 5, self.mco[1] - 40, 0] if self.use_snap: blf.position(0, *p) vagl.blf_draw(0, 'Snap') p[1] -= lf if self.use_precision: blf.position(0, *p) vagl.blf_draw(0, 'Precision') p[1] -= lf if self.use_depth: blf.position(0, *p) vagl.blf_draw(0, 'Depth') p[1] -= lf glsettings.pop()
def __click_char(self, x, y, btn): if btn != events.LEFTMOUSE: return offsetx = 0 newOffset = False for i in range(self.__renderable_right() - 1): if not newOffset: if self.masked: thisW, thisH = blf.dimensions(self.fid, "*") nextW, nextH = blf.dimensions(self.fid, "*") else: thisW, thisH = blf.dimensions(self.fid, self.text[i]) nextW, nextH = blf.dimensions(self.fid, self.text[i + 1]) fx = self.bounds[0] + (offsetx + (nextW + 1) / 2) if x <= fx + self.margin: self.caretx = i newOffset = True offsetx += thisW + self.charSpacing else: break if not newOffset: self.caretx = len(self.text)
def draw_text(self, font_size, font_color, context): font_id = 0 create_font(font_id, font_size, font_color) text = str(self.key_input) # default left dock xpos_text = 12 ypos_text = 30 if context.scene.h_dock == "1": # right dock text_extent = blf.dimensions(font_id, text) xpos_text = context.region.width - text_extent[0] - 28 elif context.scene.h_dock == "2": # center dock text_extent = blf.dimensions(font_id, text) xpos_text = (context.region.width - text_extent[0]) / 2.0 elif context.scene.h_dock == "3": offset_buttons = 120 if context.scene.show_buttons == False: offset_buttons = 70 ox = context.scene.cursor_offset_x oy = context.scene.cursor_offset_y + offset_buttons text_extent = blf.dimensions(font_id, text) xpos_text = self.mouse_input.mouse_x - (text_extent[0] / 2.0) + ox ypos_text = self.mouse_input.mouse_y - oy draw_text(text, xpos_text, ypos_text, font_id)
def draw_string(self, color1, color2, left, bottom, text, max_option, divide = 1): """ Draw the text like 'option : key' or just 'option' """ font_id = 0 ui_scale = bpy.context.preferences.system.ui_scale blf.enable(font_id,blf.SHADOW) blf.shadow(font_id, 0, 0.0, 0.0, 0.0, 1.0) blf.shadow_offset(font_id,2,-2) line_height = (blf.dimensions(font_id, "gM")[1] * 1.45) y_offset = 5 # Test if the text is a list formatted like : ('option', 'key') if isinstance(text,list): spacer_text = " : " spacer_width = blf.dimensions(font_id, spacer_text)[0] for string in text: blf.position(font_id, (left), (bottom + y_offset), 0) blf.color(font_id, *color1) blf.draw(font_id, string[0]) blf.position(font_id, (left + max_option), (bottom + y_offset), 0) blf.draw(font_id, spacer_text) blf.color(font_id, *color2) blf.position(font_id, (left + max_option + spacer_width), (bottom + y_offset), 0) blf.draw(font_id, string[1]) y_offset += line_height else: # The text is formatted like : ('option') blf.position(font_id, left, (bottom + y_offset), 0) blf.color(font_id, *color1) blf.draw(font_id, text) y_offset += line_height blf.disable(font_id,blf.SHADOW)
def draw(self): area_height = self.get_area_height() self.shader.bind() color = self._color text_color = self._text_color # pressed if self.__state == 1: color = self._select_color # hover elif self.__state == 2: color = self._hover_color # Draw background self.shader.uniform_float("color", self._bg_color) bgl.glEnable(bgl.GL_BLEND) self.batch_bg.draw(self.shader) # Draw slider self.shader.uniform_float("color", color) self.batch_slider.draw(self.shader) bgl.glDisable(bgl.GL_BLEND) # Draw value text sFormat = "{:0." + str(self._decimals) + "f}" blf.size(0, self._text_size, 72) sValue = sFormat.format(self.__slider_value) size = blf.dimensions(0, sValue) blf.position(0, self.__slider_pos + 1 + self.x_screen - size[0] / 2.0, area_height - self.y_screen + self.__slider_offset_y, 0) blf.draw(0, sValue) # Draw min and max if self._show_min_max: sMin = sFormat.format(self._min) size = blf.dimensions(0, sMin) blf.position(0, self.x_screen - size[0] / 2.0, area_height - self.height - self.y_screen, 0) blf.draw(0, sMin) sMax = sFormat.format(self._max) size = blf.dimensions(0, sMax) r, g, b, a = self._text_color blf.color(0, r, g, b, a) blf.position(0, self.x_screen + self.width - size[0] / 2.0, area_height - self.height - self.y_screen, 0) blf.draw(0, sMax)
def draw_callback_px(self, op, context): if context.region.width != self.width_region: self.width_region = context.region.width self.draw_util.create_batches(context, self.mouse_input) refresh_after_sec = 3.0 font_color = context.scene.font_color font_size = 28 # set color for buttons self.draw_util.set_color_buttons(context) # Draw the mouse buttons self.draw_util.draw_buttons(self.mouse_input.is_left, self.mouse_input.is_middle, self.mouse_input.is_right) # draw the text for events current_time = time.time() time_diff_keys = current_time - self.key_input.timestamp if (time_diff_keys < refresh_after_sec): font_id = 0 create_font(font_id, font_size, font_color) text = str(self.key_input) # default left dock xpos_text = 12 ypos_text = 30 if context.scene.h_dock == "1": # right dock text_extent = blf.dimensions(font_id, text) xpos_text = context.region.width - text_extent[0] - 28 elif context.scene.h_dock == "2": # center dock text_extent = blf.dimensions(font_id, text) xpos_text = (context.region.width - text_extent[0]) / 2.0 elif context.scene.h_dock == "3": ox = context.scene.cursor_offset_x oy = context.scene.cursor_offset_y text_extent = blf.dimensions(font_id, text) xpos_text = self.mouse_input.mouse_x - (text_extent[0] / 2.0) + ox ypos_text = self.mouse_input.mouse_y - 120 - oy draw_text(text, xpos_text, ypos_text, font_id) else: self.key_input.clear() self.mouse_input.clear()
def set_font_size(self, fontsize, force=False): fontsize, fontsize_scaled = int(fontsize), int(int(fontsize) * self._dpi_mult) if not force and fontsize_scaled == self.fontsize_scaled: return self.fontsize fontsize_prev = self.fontsize fontsize_scaled_prev = self.fontsize_scaled self.fontsize = fontsize self.fontsize_scaled = fontsize_scaled blf.size(self.font_id, fontsize_scaled, 72) #self._sysdpi) # cache away useful details about font (line height, line base) key = (self.fontsize_scaled) if key not in self.line_cache: dprint('Caching new scaled font size:', key) all_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()`~[}{]/?=+\\|-_\'",<.>' all_caps = all_chars.upper() self.line_cache[key] = { 'line height': round(blf.dimensions(self.font_id, all_chars)[1] + self.scale(4)), 'line base': round(blf.dimensions(self.font_id, all_caps)[1]), } info = self.line_cache[key] self.line_height = info['line height'] self.line_base = info['line base'] return fontsize_prev
def parse(self): """parsing of text, \n gives newline""" self.words = [] paragraphs = self.text.splitlines() self.max_line_width = 0 for p in paragraphs: self.words.extend(p.split(" ")) self.words.append("\n") self.lines = [] oldline = "" for word in self.words: if word == "\n": self.lines.append(oldline) w = blf.dimensions(self.font, oldline) self.max_line_width = max(self.max_line_width, w[0]) oldline = "" else: if self.max_width > 0: newline = oldline + word + " " w = blf.dimensions(self.font, newline) if w[0] > self.max_width: self.lines.append(oldline) w = blf.dimensions(self.font, oldline) self.max_line_width = max(self.max_line_width, w[0]) oldline = word + " " else: oldline = newline else: oldline = oldline + word + " "
def text_draw_callback(self, op, context): # Force Stop if self.is_handler_list_empty(): self.unregister_handler() return settings = get_main_settings() # TESTING # self.inc_counter() camera = settings.get_camera(settings.current_headnum, settings.current_camnum) # Draw text if camera is not None and len(self.message) > 0: region = context.region text = "{0} [{1}]".format(self.message[0], camera.get_image_name()) subtext = "{} | {}".format(self.message[1], settings.opnum) xt = int(region.width / 2.0) blf.size(0, 24, 72) blf.position(0, xt - blf.dimensions(0, text)[0] / 2, 60, 0) blf.draw(0, text) blf.size(0, 20, 72) blf.position(0, xt - blf.dimensions(0, subtext)[0] / 2, 30, 1) blf.draw(0, subtext) # Text is on screen
def split_line(width, x, max_x, line, lines, fontid=0): words = line.split(" ") line = "" for word in words: c = (word if not line else " " + word) x_dx = x + blf.dimensions(fontid, line + c)[0] if (x_dx) > width: x_dx = blf.dimensions(fontid, line)[0] if not line: # one word is longer than the width line, x, max_x = split_word(width, x, max_x, word, lines, fontid) else: lines.append(line) line = word x = 0 else: line += c max_x = max(x_dx, max_x) if line: lines.append(line) return max_x
def __click_char(self, x, y, btn): if btn != events.LEFTMOUSE: return offsetx = 0 newOffset = False for i in range(self.__renderable_right()-1): if not newOffset: if self.masked: thisW, thisH = blf.dimensions(self.fid, "*") nextW, nextH = blf.dimensions(self.fid, "*") else: thisW, thisH = blf.dimensions(self.fid, self.text[i]) nextW, nextH = blf.dimensions(self.fid, self.text[i + 1]) fx = self.bounds[0] + (offsetx + (nextW+1) / 2) if x <= fx+self.margin: self.caretx = i newOffset = True offsetx += thisW + self.charSpacing else: break if not newOffset: self.caretx = len(self.text)
def drawText(text, x, y, font=0, align="LEFT", verticalAlignment="BASELINE", size=12, color=(1, 1, 1, 1)): text = str(text) blf.size(font, size, int(dpi)) glColor4f(*color) if align == "LEFT" and verticalAlignment == "BASELINE": blf.position(font, x, y, 0) else: width, height = blf.dimensions(font, text) newX, newY = x, y if align == "RIGHT": newX -= width elif align == "CENTER": newX -= width / 2 if verticalAlignment == "CENTER": newY -= blf.dimensions(font, "x")[1] * 0.75 blf.position(font, newX, newY, 0) blf.draw(font, text)
def draw_text(x_pos, y_pos, display_text, rgb, fsize, i_props, anchor_co, endpoint_co): font_id = 0 blf.size(font_id, fsize, 72) #- height of one line mwidth, mheight = blf.dimensions(font_id, "Tp") # uses high/low letters origional_x = x_pos origional_y = y_pos # split lines mylines = display_text.split("|") maxwidth = 0 maxheight = len(mylines) * mheight #---------- Draw all lines-+ for index, line in enumerate(mylines): text_width, text_height = blf.dimensions(font_id, line) y_pos -= mheight * index x_pos -= text_width * 0.5 if not check_overlap_2d_point(anchor_co, endpoint_co): #if vertical line text placement if anchor_co[0] == endpoint_co[0]: if i_props.v_line_text_placement == 'LEFT': x_pos -= text_width * 0.5 + i_props.line_to_text_pad * 2 elif i_props.v_line_text_placement == 'RIGHT': x_pos += text_width * 0.5 #horizontal line text placement if anchor_co[1] == endpoint_co[1]: if i_props.h_line_text_placement == 'TOP': y_pos += i_props.line_to_text_pad elif i_props.h_line_text_placement == 'BOTTOM': y_pos -= text_height + i_props.line_to_text_pad blf.position(font_id, x_pos + i_props.gl_text_x, y_pos + i_props.gl_text_y, 0) else: blf.position(font_id, x_pos, y_pos, 0) bgl.glColor4f(rgb[0], rgb[1], rgb[2], rgb[3]) blf.draw(font_id, " " + line) # saves max width if maxwidth < text_width: maxwidth = text_width #Reset original position for multiple lines x_pos = origional_x y_pos = origional_y return maxwidth, maxheight
def draw_cursor(self, ctx): first_part = self.text[self.cursor_pos] last_part = self.text[self.cursor_pos:] blf.size(self.font_id, self.font_size, self.font_dpi) text_first_w, text_first_h = blf.dimensions(self.font_id, first_part) text_last_w, text_last_h = blf.dimensions(self.font_id, last_part) bounds = self.bounds()
def h_draw_text(fid, text, bounds, color, margin=0, font_size=16, text_align=0, vertical_align=0, shadow=False, clip=True): text = str(text) width = render.getWindowWidth() height = render.getWindowHeight() #bgl.glColor4f(*(1, 0, 0, 1)) #h_draw_quad_wire(bounds) if clip: h_clip_begin(bounds) blf.size(fid, font_size, 72) if shadow: blf.enable(fid, blf.SHADOW) blf.shadow(fid, 3, 0.0, 0.0, 0.0, 1.0) blf.shadow_offset(fid, 0, -1) else: blf.disable(fid, blf.SHADOW) bgl.glPushMatrix() # Fix upside-down text =) w, h = blf.dimensions(fid, text) bgl.glTranslated(0.0, h, 0.0) bgl.glScalef(1.0, -1.0, 1.0) bgl.glColor4f(*color) texts = text.split("\n") yn = 0 if vertical_align == 0: yn = margin elif vertical_align == 1: yn = bounds[3]/2-(h*len(texts))/2 elif vertical_align == 2: yn = (bounds[3]-(h*len(texts)))-margin for i in range(len(texts)): texts[i] = texts[i].replace("\t", " ") wl, hl = blf.dimensions(fid, texts[i]) xn = 0 if text_align == 0: xn = margin elif text_align == 1: xn = bounds[2]/2-wl/2 elif text_align == 2: xn = (bounds[2]-wl)-margin blf.position(fid, bounds[0]+xn, -bounds[1]-(i*h)-yn, 1) blf.draw(fid, texts[i]) bgl.glScalef(1.0, 1.0, 1.0) bgl.glPopMatrix() if clip: h_clip_end()
def update_selection(self): left = self.fd + blf.dimensions(self.label.fontid, self.text[:self.slice[0]])[0] right = self.fd + blf.dimensions(self.label.fontid, self.text[:self.slice[1]])[0] self.highlight.position = [left, 1] self.highlight.size = [right - left, self.frame.size[1] * .8] if self.slice_direction in [0, -1]: self.cursor.position = [left, 1] else: self.cursor.position = [right, 1] self.cursor.size = [2, self.frame.size[1] * .8]
def draw_tooltip(self, context, shader, message): addon_prefs = context.preferences.addons[__package__].preferences font_id = 0 line_height = 11 * scale_factor() text_color = addon_prefs.qcd_ogl_widget_tooltip_text blf.size(font_id, int(line_height), 72) blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) lines = message.split("\n") longest = [0, ""] num_lines = len(lines) for line in lines: w, _ = blf.dimensions(font_id, line) if w > longest[0]: longest[0] = w longest[1] = line w, h = blf.dimensions(font_id, longest[1]) line_spacer = 1 * scale_factor() padding = 4 * scale_factor() # draw background tooltip = { "vert": self.mouse_pos, "width": w + spacer() * 2, "height": (line_height * num_lines + line_spacer * num_lines) + padding * 3, "value": None } x = tooltip["vert"][0] - spacer() * 2 y = tooltip["vert"][1] + tooltip["height"] + round(5 * scale_factor()) tooltip["vert"] = (x, y) account_for_view_bounds(tooltip) outline_color = addon_prefs.qcd_ogl_widget_tooltip_outline background_color = addon_prefs.qcd_ogl_widget_tooltip_inner draw_rounded_rect(tooltip, shader, outline_color[:] + (1, ), outline=True) draw_rounded_rect(tooltip, shader, background_color) line_pos = padding + line_height # draw text for num, line in enumerate(lines): x = tooltip["vert"][0] + spacer() y = tooltip["vert"][1] - line_pos blf.position(font_id, x, y, 0) blf.draw(font_id, line) line_pos += line_height + line_spacer
def text(self, value): blf.size(self.fontid, self.pt_size, 72) size = [blf.dimensions(self.fontid, value)[0], blf.dimensions(self.fontid, 'Mj')[0]] if not (self.options & BGUI_NO_NORMALIZE): size[0] /= self.parent.size[0] size[1] /= self.parent.size[1] self._update_position(size, self._base_pos) self._text = value
def draw_text(text: str, font: int, size: int, x: int, y: int, horizontal: str, vertical: str, shadow: bool): ''' Draw two aligned columns of text. Args: text: List of tuples of two strings. font: ID returned by blf.load or 0. size: How large the font should be. x, y: Pixel on the active area. horizontal: 'LEFT', 'CENTER', or 'RIGHT'. vertical: 'BOTTOM', 'CENTER', or 'TOP'. shadow: Whether to add a dropshadow. ''' blf.size(font, size, 72) widths_l = [blf.dimensions(font, line[0])[0] for line in text] widths_r = [blf.dimensions(font, line[1])[0] for line in text] height = blf.dimensions(font, 'A')[1] * 2 space = height * 0.5 y += height * 0.25 if horizontal == 'LEFT': x += max(widths_l) + space elif horizontal == 'RIGHT': x -= max(widths_r) + space if vertical == 'BOTTOM': y += height * len(text) elif vertical == 'CENTER': y += height * len(text) * 0.5 if shadow: blf.enable(font, blf.SHADOW) blf.shadow(font, 5, 0, 0, 0, 1) blf.shadow_offset(font, 1, -1) for index, line in enumerate(text): offset = (index + 2) * height left, right = line width = widths_l[index] blf.position(font, x - width - space, y - offset, 0) blf.draw(font, left) blf.position(font, x + space, y - offset, 0) blf.draw(font, right) if shadow: blf.disable(font, blf.SHADOW)
def wrap_line(txt_line, width): ''' takes a string, returns a list of strings, corresponding to wrapped text of the specified pixel width, given current BLF settings ''' if blf.dimensions(0, txt_line)[0] < useful_width: #TODO fil return [txt_line] txt = txt_line #TODO Clean this words = txt.split(' ') new_lines = [] current_line = [] cur_line_len = 0 for i, wrd in enumerate(words): word_width = blf.dimensions(0, wrd)[0] if word_width >= useful_width and cur_line_len == 0: crp_wrd = crop_word(wrd, useful_width - cur_line_len) if len(current_line): new_lines.append(' '.join(current_line) + ' ' + crp_wrd) else: new_lines.append(crp_wrd) current_line = [] cur_line_len = 0 continue elif cur_line_len + word_width < useful_width: current_line.append(wrd) cur_line_len += word_width if i < len(words) - 1: cur_line_len += spc_size[0] else: new_lines.append(' '.join(current_line)) if new_lines[0].startswith(self.hang_indent): current_line = [' ' + wrd] cur_line_len = word_width + 2 * spc_width else: current_line = [wrd] cur_line_len = word_width if i < len(words) - 1: #meaning still words to go cur_line_len += spc_size[0] if i == len(words) - 1 and len(current_line): new_lines.append(' '.join(current_line)) return new_lines
def draw_item_in_rectangle(self, item, rectangle): glColor4f(*self.text_color) blf.size(self.font, self.font_size, int(getDpi())) if item.alignment == "LEFT": x = rectangle.left + self.padding if item.alignment == "CENTER": x = rectangle.center.x - blf.dimensions(self.font, item.text)[0] / 2 x += item.offset offset = blf.dimensions(self.font, "i")[1] / 2 blf.position(self.font, x, rectangle.center.y - offset, 0) blf.draw(self.font, item.text)
def drawTextArray(text, corner, pos_x, pos_y): vieportInfoProps = bpy.context.scene.vieportInfo font_id = 0 height = bpy.context.region.height width = bpy.context.region.width txt_width = [] list_line_width = [] blf.size(font_id, vieportInfoProps.text_font_size, 72) x_offset = 0 y_offset = 0 line_height = (blf.dimensions(font_id, "M")[1] * 1.45) x = 0 y = 0 for command in text: if len(command) == 2: Text, Color = command text_width, text_height = blf.dimensions(font_id, Text) txt_width.append(text_width) if corner == '1' or corner == '3': x = pos_x else: if txt_width: for label, value in zip(txt_width[0::2], txt_width[1::2]): l_width = label + value list_line_width.append(l_width) x = width - (max(list_line_width) + pos_x) if corner == '1' or corner == '2': y = height - pos_y else: line_count = text.count("Carriage return") y = pos_y + (line_height*line_count) for command in text: if len(command) == 2: Text, Color = command bgl.glColor3f(*Color) text_width, text_height = blf.dimensions(font_id, Text) blf.position(font_id, (x + x_offset), (y + y_offset), 0) blf.draw(font_id, Text) x_offset += text_width else: x_offset = 0 y_offset -= line_height
def draw_textbox( text, origin, textsize=6, textcolor=colors['white'], backgroundcolor=colors['background'], offset=Vector((0.0, 0.0)), linewidth=2, hborder=3, vborder=4, rightalign=False, indicator_line=True, ): """ Args: text: origin: textsize: (Default value = 6) textcolor: (Default value = colors['white']) backgroundcolor: (Default value = colors['background']) offset: (Default value = Vector((0.0) 0.0)): linewidth: (Default value = 2) hborder: (Default value = 3) vborder: (Default value = 4) rightalign: (Default value = False) indicator_line: (Default value = True) Returns: """ blf.size(0, textsize, 150) width = blf.dimensions(0, text)[0] height = blf.dimensions(0, text)[1] if rightalign: origin = origin + Vector((-width - 2 * hborder, 0)) + offset else: origin = origin + offset points = ( origin + Vector((-hborder, -vborder * 1.5)), origin + Vector((width + hborder, -vborder * 1.5)), origin + Vector((width + hborder, height + vborder)), origin + Vector((-hborder, height + vborder)), ) draw_2dpolygon(points, fillcolor=backgroundcolor, linecolor=textcolor, linewidth=linewidth) draw_text(text, position=origin, size=textsize, color=textcolor)
def onscreen_options(self, x, y): gamma = self.gamma_correction color_white = gamma((0.95, 0.95, 0.95, 1.0)) color_grey = gamma((0.67, 0.67, 0.67, 1.0)) fontid = 1 blf.size(fontid, self.prefs.view_font_size_option, 72) font_w_1, font_h = blf.dimensions(fontid, self.option_col_1_max) font_w_2, _ = blf.dimensions(fontid, self.option_col_2_max) font_row_height = font_h * 1.5 for option, hotkey, value, prop_type in self.option_list: y -= font_row_height x_ofst = x blf.position(fontid, x, y, 0.0) blf.color(fontid, *color_white) blf.draw(fontid, option) x_ofst += font_w_1 + 20 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, *color_grey) blf.draw(fontid, hotkey) if prop_type is self.TYPE_BOOL: x_ofst += font_w_2 + 10 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, *color_white) blf.draw(fontid, ":") x_ofst += 20 blf.position(fontid, x_ofst, y, 0.0) if getattr(self, value): blf.color(fontid, *gamma((0.3, 1.0, 0.3, 1.0))) blf.draw(fontid, "ON") else: blf.color(fontid, *gamma((1.0, 0.3, 0.3, 1.0))) blf.draw(fontid, "OFF") else: if getattr(self, value): x_ofst += font_w_2 + 10 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, *color_white) blf.draw(fontid, ":") x_ofst += 20 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, *gamma((0.9, 0.9, 0.0, 1.0))) blf.draw(fontid, "PROCESSING...")
def draw(self): if not self.visible: return PControl.new.draw(self) if self.theme == None: h_draw_quad_b(self.bounds, self.backColor, 2) h_clip_begin(self.bounds, padding=[1, 1, 1, 1]) else: t = self.theme["panel_down"] h_draw_9patch_skin(t, self.bounds) h_clip_begin(self.bounds, padding=t["padding"]) offx = 0 for i in range(len(self.text)): charw, charh = blf.dimensions(self.fid, self.text[i]) if offx+charw > self.bounds[2]: break ## Uncomment these lines to show each char collision box # #bnds = [self.bounds[0]+(offx + (charw+1) / 2), self.bounds[1], charw, self.bounds[3]] #bgl.glColor4f(*(0.0, 0.0, 1.0, 1.0)) #h_draw_quad_wire(bnds) if not self.masked: h_draw_text(self.fid, self.text[i], [self.bounds[0]+offx, self.bounds[1], charw, self.bounds[3]], self.foreColor, margin=self.margin, font_size=self.fontSize, text_align=0, vertical_align=2, shadow=self.shadow, clip=False) else: charw, charh = blf.dimensions(self.fid, "*") h_draw_text(self.fid, "*", [self.bounds[0]+offx, self.bounds[1], charw, self.bounds[3]], self.foreColor, margin=self.margin, font_size=self.fontSize, text_align=0, vertical_align=2, shadow=self.shadow, clip=False) offx += charw + self.charSpacing # h_draw_text(self.fid, "cx: %d, ln: %d, lt: %d" % (self.caretx, len(self.text), (self.caretx < len(self.text))), [10, 200, 100, 22], self.foreColor, margin=0, font_size=14, text_align=0, vertical_align=2) if self.focused and self.blink and not self.readOnly: offx = 0 txt = self.text+" " for i in range(len(txt)): charw, charh = blf.dimensions(self.fid, txt[i]) if self.masked: charw, charh = blf.dimensions(self.fid, "*") if i == self.caretx: cx = self.bounds[0]+offx-3 if cx < self.bounds[0]+self.bounds[2]: h_draw_text(self.fid, "|", [cx, self.bounds[1]-2, 1, self.bounds[3]], self.foreColor, margin=self.margin, font_size=self.fontSize, text_align=0, vertical_align=2, shadow=False, clip=False) #break offx += charw + self.charSpacing h_clip_end()
def drawTextArray(text, corner, pos_x, pos_y): vieportInfoProps = bpy.context.scene.vieportInfo font_id = 0 height = bpy.context.region.height width = bpy.context.region.width txt_width = [] list_line_width = [] blf.size(font_id, vieportInfoProps.text_font_size, 72) x_offset = 0 y_offset = 0 line_height = (blf.dimensions(font_id, "M")[1] * 1.45) x = 0 y = 0 for command in text: if len(command) == 2: Text, Color = command text_width, text_height = blf.dimensions(font_id, Text) txt_width.append(text_width) if corner == '1' or corner == '3': x = pos_x else: if txt_width: for label, value in zip(txt_width[0::2], txt_width[1::2]): l_width = label + value list_line_width.append(l_width) x = width - (max(list_line_width) + pos_x) if corner == '1' or corner == '2': y = height - pos_y else: line_count = text.count("Carriage return") y = pos_y + (line_height * line_count) for command in text: if len(command) == 2: Text, Color = command bgl.glColor3f(*Color) text_width, text_height = blf.dimensions(font_id, Text) blf.position(font_id, (x + x_offset), (y + y_offset), 0) blf.draw(font_id, Text) x_offset += text_width else: x_offset = 0 y_offset -= line_height
def wrap_line(txt_line,width): ''' takes a string, returns a list of strings, corresponding to wrapped text of the specified pixel width, given current BLF settings ''' if blf.dimensions(0,txt_line)[0] < useful_width: #TODO fil return [txt_line] txt = txt_line #TODO Clean this words = txt.split(' ') new_lines = [] current_line = [] cur_line_len = 0 for i,wrd in enumerate(words): word_width = blf.dimensions(0, wrd)[0] if word_width >= useful_width and cur_line_len == 0: crp_wrd = crop_word(wrd, useful_width - cur_line_len) if len(current_line): new_lines.append(' '.join(current_line) + ' ' + crp_wrd) else: new_lines.append(crp_wrd) current_line = [] cur_line_len = 0 continue elif cur_line_len + word_width < useful_width: current_line.append(wrd) cur_line_len += word_width if i < len(words)-1: cur_line_len += spc_size[0] else: new_lines.append(' '.join(current_line)) if new_lines[0].startswith(self.hang_indent): current_line = [' ' + wrd] cur_line_len = word_width + 2 * spc_width else: current_line = [wrd] cur_line_len = word_width if i < len(words)-1: #meaning still words to go cur_line_len += spc_size[0] if i == len(words) - 1 and len(current_line): new_lines.append(' '.join(current_line)) return new_lines
def gen_text_position(self, string, is_mono=False): """描画位置のY座標とboxの幅・高さを返すジェネレータ。 sendメソッドでは (string, is_mono) か string を渡す。 一行しかない場合、boxの高さはwidget_unitで固定される。 Noneを渡すと終了。 :param string: 改行文字を含む場合は複数行と見做し、 返り値のY座標は最初の行の位置を表す。 :type string: str: :param is_mono: 固定幅文字を使うなら真。 :type is_mono: bool :return: (描画位置のY座標(boxの上端を基準にして負の方向に進む), boxの幅, boxの高さ) :rtype tuple """ y = 0 width = height = 0 widget_unit = self.widget_unit while True: if is_mono: font_id = self.prefs.font_id_mono else: font_id = self.prefs.font_id th = blf.dimensions(font_id, CALC_TEXT_HEIGHT)[1] if string is not None: for line in string.split("\n"): if y == 0: h = widget_unit / 2 + th / 2 else: if is_mono: h = self.max_glyph_height_mono else: h = self.max_glyph_height y -= h height += h width = max(width, blf.dimensions(font_id, line)[0]) h_under = widget_unit / 2 - th / 2 if y == 0: # 最初の呼び出しでstringが空文字の場合 recv = (yield 0, widget_unit, widget_unit) else: recv = (yield y, width + widget_unit, height + h_under) if recv is None: string, is_mono = None, False elif isinstance(recv, str): string, is_mono = recv, False else: string, is_mono = recv
def calc_text(self): biggestdimensionX=biggestdimensionY=0 for it in self.menu_items: #find the biggest word in the menu and base the size of all buttons on that word blf.size(0, self.text_size, self.text_dpi) dimension = blf.dimensions(0, it.id) if dimension[0]>biggestdimensionX: biggestdimensionX = dimension[0] if dimension[1]>biggestdimensionY: biggestdimensionY = dimension[1] self.biggest_dim_x = biggestdimensionX self.biggest_dim_y = biggestdimensionY self.dimension_radio = blf.dimensions(0, "Off")[0]
def draw_callback_2d(self, op, context): # Draw text to indicate that draw mode is active region = context.region text = "- Draw mode active -" subtext = "Esc : Close | Enter : Create" xt = int(region.width / 2.0) blf.size(0, 24, 72) blf.position(0, xt - blf.dimensions(0, text)[0] / 2, 60, 0) blf.draw(0, text) blf.size(1, 20, 72) blf.position(1, xt - blf.dimensions(0, subtext)[0] / 2, 30, 1) blf.draw(1, subtext)
def calc_text(self): biggestdimensionX = biggestdimensionY = 0 for it in self.menu_items: #find the biggest word in the menu and base the size of all buttons on that word blf.size(0, self.text_size, self.text_dpi) dimension = blf.dimensions(0, it.id) if dimension[0] > biggestdimensionX: biggestdimensionX = dimension[0] if dimension[1] > biggestdimensionY: biggestdimensionY = dimension[1] self.biggest_dim_x = biggestdimensionX self.biggest_dim_y = biggestdimensionY self.dimension_radio = blf.dimensions(0, "Off")[0]
def draw_callback_px(self, context): font_id = 0 # XXX, need to find out how best to get this. # draw some text y = context.region.height dims = blf.dimensions(0, 'A') blf.position(font_id, 10, y - 20 - dims[1], 0) blf.size(font_id, 20, 72) if context.area.x == self.area_align.x: blf.draw(font_id, "Align: "+ self.align_msg) points = [self.obj_align.matrix_world * p for p in self.align_points] color = (1,0,0,1) else: blf.draw(font_id, "Base: " + self.base_msg) points = [self.obj_align.matrix_world * p for p in self.base_points] color = (0,1,0,1) draw_3d_points_revised(context, points, color, 4) for i, vec in enumerate(points): ind = str(i) draw_3d_text(context, font_id, ind, vec)
def draw_callback_px(self, context): #print("callback_px") # Maybe print a nice red circle in some corner bgl.glPushAttrib(bgl.GL_CLIENT_ALL_ATTRIB_BITS) FONT_RGBA = (0.8, 0.1, 0.1, 0.5) bgl.glColor4f(*FONT_RGBA) font_size = 11 DPI = 72 blf.size(0, font_size, DPI) msg = "Logging..." msg_w,msg_h = blf.dimensions(0, msg) pos_x = context.region.width - msg_w pos_y = font_size / 2 blf.position(0, pos_x, pos_y, 0) #blf.position(0, 10, 10, 0) blf.draw(0, msg) bgl.glPopAttrib() pass
def fit_box_width_to_text_lines(self): ''' ''' max_width = max([blf.dimensions(0,line)[0] for line in self.text_lines]) if max_width < self.width - 2*self.border: self.width = max_width + 2*self.border
def draw_index(rgb, rgb2, index, vec, text=''): vec_4d = perspective_matrix * vec.to_4d() if vec_4d.w <= 0.0: return x = region_mid_width + region_mid_width * (vec_4d.x / vec_4d.w) y = region_mid_height + region_mid_height * (vec_4d.y / vec_4d.w) if text: index = str(text[0]) else: index = str(index) if draw_bg: polyline = get_points(index) ''' draw polygon ''' bgl.glColor4f(*rgb2) bgl.glBegin(bgl.GL_POLYGON) for pointx, pointy in polyline: bgl.glVertex2f(pointx+x, pointy+y) bgl.glEnd() ''' draw text ''' txt_width, txt_height = blf.dimensions(0, index) bgl.glColor4f(*rgb) blf.position(0, x - (txt_width / 2), y - (txt_height / 2), 0) blf.draw(0, index)
def draw_text(text, position, align="LEFT"): """Draw 12pt white text at position (3D-Vector) aligned to the left""" pos = to_screen_coord(position) if pos is None: # we cannot draw because the point is not on the screen return # [PART VI] # BRIDGEKEEPER: Hee hee heh. Stop! What... is your name? # ARTHUR: It is 'Arthur', King of the Britons. # BRIDGEKEEPER: What... is your quest? # ARTHUR: To draw text on the screen. font_id = 0 size = 12 color = (1.0, 1.0, 1.0, 1.0) bgl.glColor4f(*color) blf.size(font_id, size, 72) if align == "CENTER": # get length of text, place text .5 of length to the left of the coord. text_width, text_height = blf.dimensions(font_id, text) pos.x -= text_width / 2 blf.position(font_id, pos.x, pos.y, 0) # BRIDGEKEEPER: What... is the OpenGL command for drawing text? # ARTHUR: What do you mean? Blender's bfl module can do that for me! # BRIDGEKEEPER: Huh? I-- I didn't know that. Auuuuuuuugh! blf.draw(font_id, text)
def getBoundingBox(current_width, current_height, new_text): w, h = blf.dimensions(0, new_text) if w > current_width: current_width = w current_height += h return (current_width, current_height)
def update_label(self): y_screen_flip = self.get_area_height() - self.y_screen size = blf.dimensions(0, self._label) self._label_width = size[0] + 12 # bottom left, top left, top right, bottom right vertices_outline = ( (self.x_screen, y_screen_flip), (self.x_screen + self.width + self._label_width, y_screen_flip), (self.x_screen + self.width + self._label_width, y_screen_flip - self.height), (self.x_screen, y_screen_flip - self.height)) self.batch_outline = batch_for_shader(self.shader, 'LINE_LOOP', {"pos" : vertices_outline}) indices = ((0, 1, 2), (2, 3, 1)) lb_x = self.x_screen + self.width # bottom left, top left, top right, bottom right vertices_label_bg = ( (lb_x, y_screen_flip), (lb_x + self._label_width, y_screen_flip), (lb_x, y_screen_flip - self.height), (lb_x + self._label_width, y_screen_flip - self.height)) self.batch_label_bg = batch_for_shader(self.shader, 'TRIS', {"pos" : vertices_label_bg}, indices=indices)
def _draw_text(self, text, line): blf.size(self._font_id, 100, 100) dimensions = blf.dimensions(self._font_id, text) scale = 90 * self._width / dimensions[0] blf.size(self._font_id, 100, int(scale)) blf.position(self._font_id, 0.05 * self._width, (line + 1) * self._height/6.0, 0) blf.draw(self._font_id, text)
def drawLines(self): baseLineOffset = blf.dimensions(font, "V")[1] glColor4f(0, 0, 0, 1) for i, line in enumerate(self.lines): blf.position(font, self.boundary.left, self.boundary.top - i * self.lineHeight - baseLineOffset, 0) blf.draw(font, line)
def draw(self): super().draw() area_height = self.get_area_height() # Draw text self.draw_text(area_height) self.shader.bind() self.shader.uniform_float("color", self._carret_color) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(2) self.batch_carret.draw(self.shader) if self.has_label: self.shader.uniform_float("color", self._label_color) bgl.glLineWidth(1) self.batch_outline.draw(self.shader) self.batch_label_bg.draw(self.shader) size = blf.dimensions(0, self._label) textpos_y = area_height - self.y_screen - (self.height + size[1]) / 2.0 blf.position(0, self.x_screen + self.width + (self._label_width / 2.0) - (size[0] / 2.0), textpos_y + 1, 0) r, g, b, a = self._label_text_color blf.color(0, r, g, b, a) blf.draw(0, self._label)
def getBoundingBox(current_width, current_height, new_text): w,h = blf.dimensions(0,new_text) if w > current_width: current_width = w current_height += h return(current_width, current_height)
def draw_callback_px(self, context): if context.region.id != self.region_id: return posx = 70 posy1 = 30 posy2 = 50 text_interval = 5 font_id = 0 blf.size(font_id, 11, context.user_preferences.system.dpi) bgl.glEnable(bgl.GL_BLEND) if self.changing_mgtype: bgl.glColor4f(1.0, 1.0, 0.0, 1.0) else: bgl.glColor4f(1.0, 1.0, 1.0, 1.0) # draw origin bgl.glLineWidth(2) radius = path_threthold radius2 = radius + 5 x, y, z = self.path[0] bgl.glBegin(bgl.GL_LINES) for i in range(4): r = math.pi / 2 * i + math.pi / 4 bgl.glVertex2f(x + radius * math.cos(r), y + radius * math.sin(r)) bgl.glVertex2f(x + radius2 * math.cos(r), y + radius2 * math.sin(r)) bgl.glEnd() bgl.glLineWidth(1) # draw lines bgl.glEnable(bgl.GL_LINE_STIPPLE) bgl.glLineStipple(1, int(0b101010101010101)) # (factor, pattern) bgl.glBegin(bgl.GL_LINE_STRIP) for v in self.path: bgl.glVertex2f(v[0], v[1]) bgl.glEnd() bgl.glLineStipple(1, 1) bgl.glDisable(bgl.GL_LINE_STIPPLE) # draw txt if self.action or self.mgitem: x = posx for txt in self.action: blf.position(font_id, x, posy1, 0) blf.draw(font_id, txt) text_width, text_height = blf.dimensions(font_id, txt) x += text_width + text_interval if self.mgitem: blf.position(font_id, posx, posy2, 0) blf.draw(font_id, self.mgitem.name) else: #blf.position(font_id, posx, posy2, 0) #blf.draw(font_id, '[Mouse Gesture]') pass # restore opengl defaults bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) blf.size(0, 11, context.user_preferences.system.dpi)
def onscreen_options(self, x, y): fontid = 1 blf.size(fontid, self.prefs.view_font_size_option, 72) font_w_1, font_h = blf.dimensions(fontid, self.option_col_1_max) font_w_2, _ = blf.dimensions(fontid, self.option_col_2_max) font_row_height = font_h * 1.5 for option, hotkey, value, prop_type in self.option_list: y -= font_row_height x_ofst = x blf.position(fontid, x, y, 0.0) blf.color(fontid, 0.97, 0.97, 0.97, 1.0) blf.draw(fontid, option) x_ofst += font_w_1 + 20 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, 0.7, 0.7, 0.7, 1.0) blf.draw(fontid, hotkey) if prop_type == self.TYPE_BOOL: x_ofst += font_w_2 + 10 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, 0.97, 0.97, 0.97, 1.0) blf.draw(fontid, ":") x_ofst += 20 blf.position(fontid, x_ofst, y, 0.0) if getattr(self, value): blf.color(fontid, 0.3, 1.0, 0.3, 1.0) blf.draw(fontid, "ON") else: blf.color(fontid, 1.0, 0.3, 0.3, 1.0) blf.draw(fontid, "OFF") else: if getattr(self, value): x_ofst += font_w_2 + 10 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, 0.97, 0.97, 0.97, 1.0) blf.draw(fontid, ":") x_ofst += 20 blf.position(fontid, x_ofst, y, 0.0) blf.color(fontid, 0.9, 0.9, 0.0, 1.0) blf.draw(fontid, "PROCESSING...")
def drawText(text, x, y, font = 0, align = "LEFT", verticalAlignment = "BASELINE", size = 12, color = (1, 1, 1, 1)): text = str(text) blf.size(font, size, int(dpi)) glColor4f(*color) if align == "LEFT" and verticalAlignment == "BASELINE": blf.position(font, x, y, 0) else: width, height = blf.dimensions(font, text) newX, newY = x, y if align == "RIGHT": newX -= width elif align == "CENTER": newX -= width / 2 if verticalAlignment == "CENTER": newY -= blf.dimensions(font, "x")[1] * 0.75 blf.position(font, newX, newY, 0) blf.draw(font, text)
def split_word(width, x, max_x, word, lines, fontid=0): line = "" for c in word: x_dx = x + blf.dimensions(fontid, line+c)[0] if (x_dx) > width: x_dx = blf.dimensions(fontid, line)[0] lines.append(line) line = c x = 0 else: line += c max_x = max(x_dx, max_x) return line, x, max_x
def drawLines(self): offset = blf.dimensions(font, "Vg")[1] textBoundary = self.boundary.getInsetRectangle(self.padding) glColor4f(0, 0, 0, 1) for i, line in enumerate(self.lines): blf.position(font, textBoundary.left, textBoundary.top - i * self.lineHeight - offset, 0) blf.draw(font, line)
def draw_gems(self, context, ratio_w=1, ratio_h=1): from_scene_scale = unit.Scale(context).from_scene view_normal = Vector((0.0, 0.0, 1.0)) @ self.region_3d.view_matrix angle_thold = pi / 1.8 fontid = 0 blf.size(fontid, self.prefs.view_font_size_gem_size, 72) blf.color(fontid, 0.0, 0.0, 0.0, 1.0) shader = gpu.shader.from_builtin("2D_UNIFORM_COLOR") depsgraph = context.depsgraph for dup in depsgraph.object_instances: if dup.is_instance: ob = dup.instance_object.original else: ob = dup.object.original if "gem" not in ob or (self.use_select and not ob.select_get()): continue shader.bind() ob_stone = ob["gem"]["stone"] ob_cut = ob["gem"]["cut"] ob_size = tuple(round(x, 2) for x in from_scene_scale(ob.dimensions, batch=True)) for stone, cut, size, size_fmt, color in self.gems_raw: if ob_stone == stone and ob_cut == cut and ob_size == size: shader.uniform_float("color", color) break me = ob.to_mesh(depsgraph, True) me.transform(dup.matrix_world) verts = me.vertices for poly in me.polygons: if view_normal.angle(poly.normal) < angle_thold: cos = [ loc_3d_to_2d(self.region, self.region_3d, verts[v].co, ratio_w, ratio_h) for v in poly.vertices ] batch = batch_for_shader(shader, "TRI_FAN", {"pos": cos}) batch.draw(shader) bpy.data.meshes.remove(me) # Size # ----------------------------- ob_loc = dup.matrix_world.translation.to_tuple() loc_x, loc_y = loc_3d_to_2d(self.region, self.region_3d, ob_loc, ratio_w, ratio_h) dim_x, dim_y = blf.dimensions(fontid, size_fmt) blf.position(fontid, loc_x - dim_x / 2, loc_y - dim_y / 2, 0.0) blf.draw(fontid, size_fmt)