def create_attribute(self, attribute, start, end, value): if attribute == 'bold': return Pango.AttrWeight(value, start, end) elif attribute == 'italic': return Pango.AttrStyle(value, start, end) elif attribute == 'underline': return Pango.AttrUnderline(value, start, end)
def setup_default_tags(self): self.italics = self.get_tags_from_attrs(None, None, [Pango.AttrStyle('italic')])[0] self.bold = self.get_tags_from_attrs(None, None, [Pango.AttrWeight('bold')])[0] self.underline = self.get_tags_from_attrs( None, None, [Pango.AttrUnderline('single')])[0]
def __init__(self, lbl, is_active=False): gtk.ImageMenuItem.__init__(self) self.label = gtk.Label(lbl) if is_active: atrlist = Pango.AttrList() atrlist.insert(Pango.AttrWeight(Pango.WEIGHT_BOLD, 0, 50)) self.label.set_attributes(atrlist) self.label.set_alignment(0, 0) self.add(self.label) self.label.show()
def load(self, node): self.index = int(node.getAttribute("cursor")) if node.hasAttribute("selection_end"): self.end_index = int(node.getAttribute("selection_end")) else: self.end_index = self.index tmp = node.getAttribute("ul-coords") self.ul = utils.parse_coords(tmp) tmp = node.getAttribute("lr-coords") self.lr = utils.parse_coords(tmp) self.identity = int(node.getAttribute("identity")) try: tmp = node.getAttribute("background-color") #self.background_color = Gdk.color_parse(tmp) tmp = node.getAttribute("foreground-color") #self.foreground_color = Gdk.color_parse(tmp) except ValueError: pass if node.hasAttribute("edit"): self.editing = True else: self.editing = False self.end_index = self.index self.am_selected = node.hasAttribute("current_root") self.am_primary = node.hasAttribute("primary_root") for n in node.childNodes: if n.nodeType == n.TEXT_NODE: self.text = n.data elif n.nodeName == "Extended": self.extended_buffer.load(n) elif n.nodeName == "attribute": attrType = n.getAttribute("type") start = int(n.getAttribute("start")) end = int(n.getAttribute("end")) if attrType == "bold": attr = Pango.AttrWeight(Pango.WEIGHT_BOLD, start, end) elif attrType == "italics": attr = Pango.AttrStyle(Pango.STYLE_ITALIC, start, end) elif attrType == "underline": attr = Pango.AttrUnderline(Pango.UNDERLINE_SINGLE, start, end) elif attrType == "font": font_name = str(n.getAttribute("value")) Pango_font = Pango.FontDescription(font_name) attr = Pango.AttrFontDesc() #self.attributes.change(attr) else: print("Unknown: " + n.nodeName) self.rebuild_byte_table() self.recalc_edges()
def attrs_changed(self): bold = False italics = False underline = False Pango_font = None del self.attrlist self.attrlist = self.attributes.copy() if self.preedit: ins_text = self.preedit[0] ins_style = self.preedit[1] if self.index == len(self.text): show_text = self.text + ins_text elif self.index == 0: show_text = ins_text + self.text else: split1 = self.text[:self.index] split2 = self.text[self.index:] show_text = split1 + ins_text + split2 self.attrlist.splice(ins_style, self.index, len(ins_text)) else: show_text = self.text #print(help(self.attributes)) it = wrap_attriterator(self.attributes) for attrs, (start, end) in it: found = False if self.index == self.end_index: if start <= self.index and end > self.index: found = True elif self.index < self.end_index: if start > self.end_index: break elif start <= self.index and \ end >= self.end_index: # We got a winner! found = True else: # i.e. self.index > self.end_index if start > self.index: break elif start <= self.end_index and \ end >= self.index: # We got another winner! found = True if found: for x in attrs: if x.type == Pango.ATTR_WEIGHT and \ x.value == Pango.WEIGHT_BOLD: bold = True elif x.type == Pango.ATTR_STYLE and \ x.value == Pango.STYLE_ITALIC: italics = True elif x.type == Pango.ATTR_UNDERLINE and \ x.value == Pango.UNDERLINE_SINGLE: underline = True elif x.type == Pango.ATTR_FONT_DESC: Pango_font = x.desc to_add = [] if bold: to_add.append( Pango.AttrWeight(Pango.WEIGHT_BOLD, self.index, self.index)) if italics: to_add.append( Pango.AttrStyle(Pango.STYLE_ITALIC, self.index, self.index)) if underline: to_add.append( Pango.AttrUnderline(Pango.UNDERLINE_SINGLE, self.index, self.index)) if Pango_font: to_add.append( Pango.AttrFontDesc(Pango_font, self.index, self.index)) for x in self.current_attrs: if x.type == Pango.ATTR_WEIGHT and x.value == Pango.WEIGHT_BOLD: bold = True to_add.append(x) if x.type == Pango.ATTR_STYLE and x.value == Pango.STYLE_ITALIC: italics = True to_add.append(x) if x.type == Pango.ATTR_UNDERLINE and x.value == Pango.UNDERLINE_SINGLE: underline = True to_add.append(x) if x.type == Pango.ATTR_FONT_DESC: Pango_font = x.desc to_add.append(x) del self.current_attrs self.current_attrs = to_add self.emit("update-attrs", bold, italics, underline, Pango_font) return show_text
def setup_default_tags(self): self.italics = self.get_tags_from_attrs(None, None, [pango.AttrStyle('italic')])[0] self.bold = self.get_tags_from_attrs(None, None, [pango.AttrWeight('bold')])[0]
def get_calculated_dimensions(self, context, rect): angle = 2 * math.pi * self._rotation / 360.0 if self._context == None: label = Gtk.Label() self._context = label.create_pango_context() pango_context = self._context attrs = Pango.AttrList() attrs.insert(Pango.AttrWeight(self._weight, 0, len(self._text))) attrs.insert(Pango.AttrStyle(self._slant, 0, len(self._text))) attrs.insert(Pango.AttrUnderline(self._underline, 0, len(self._text))) if self._size != None: attrs.insert(Pango.AttrSize(1000 * self._size, 0, len(self._text))) if self._layout == None: self._layout = Pango.Layout(pango_context) layout = self._layout layout.set_text(self._text) layout.set_attributes(attrs) #find out where to draw the layout and calculate the maximum width width = rect.width if self._anchor in [ANCHOR_BOTTOM_LEFT, ANCHOR_TOP_LEFT, ANCHOR_LEFT_CENTER]: width = rect.width - self._position[0] elif self._anchor in [ANCHOR_BOTTOM_RIGHT, ANCHOR_TOP_RIGHT, ANCHOR_RIGHT_CENTER]: width = self._position[0] text_width, text_height = layout.get_pixel_size() width = width * math.cos(angle) width = min(width, self._max_width) if self._wrap: layout.set_wrap(Pango.WrapMode.WORD_CHAR) layout.set_width(int(1000 * width)) x, y = get_text_pos(layout, self._position, self._anchor, angle) if not self._fixed: #Find already drawn labels that would intersect with the current one #and adjust position to avoid intersection. text_width, text_height = layout.get_pixel_size() real_width = abs(text_width * math.cos(angle)) + abs(text_height * math.sin(angle)) real_height = abs(text_height * math.cos(angle)) + abs(text_width * math.sin(angle)) other_labels = get_registered_labels() this_rect = (int(x), int(y), int(real_width), int(real_height)) for label in other_labels: label_rect = label.get_allocation() intersection = this_rect.intersect(label_rect) if intersection.width == 0 and intersection.height == 0: continue y_diff = 0 if label_rect.y <= y and label_rect.y + label_rect.height >= y: y_diff = y - label_rect.y + label_rect.height elif label_rect.y > y and label_rect.y < y + real_height: y_diff = label_rect.y - real_height - y y += y_diff #calculate the dimensions text_width, text_height = layout.get_pixel_size() real_width = abs(text_width * math.cos(angle)) + abs(text_height * math.sin(angle)) real_height = abs(text_height * math.cos(angle)) + abs(text_width * math.sin(angle)) return real_width, real_height