def recalc_text_edges(self): if (not hasattr(self, "layout")): return del self.layout show_text = self.attrs_changed() r, g, b = utils.selected_colors["fill"] r *= 65536 g *= 65536 b *= 65536 if self.index > self.end_index: bgsel = pango.AttrBackground(int(r), int(g), int(b), self.end_index, self.index) else: bgsel = pango.AttrBackground(int(r), int(g), int(b), self.index, self.end_index) self.attrlist.insert(bgsel) self.layout = pango.Layout(self.pango_context) self.layout.set_text(show_text) self.layout.set_attributes(self.attrlist) margin = utils.margin_required(utils.STYLE_NORMAL) text_w, text_h = self.layout.get_pixel_size() text_w += margin[0] + margin[2] text_h += margin[1] + margin[3] self.width = max(self.width, text_w) self.height = max(self.height, text_h) self.min_x = self.ul[0] + (self.width - text_w) / 2 + margin[0] self.min_y = self.ul[1] + (self.height - text_h) / 2 + margin[1] self.max_x = self.min_x + text_w self.max_y = self.min_y + text_h """
def _set_background(self, value, attrlist): if value not in common.COLOR_RGB: logging.getLogger(__name__).info('This color is not supported' + '=> %s' % value) color = common.COLOR_RGB.get(value, common.COLOR_RGB['black']) attrlist.change(pango.AttrBackground(color[0], color[1], color[2], 0, -1))
def __init__(self, attrs, unistr): super(PangoAttrList, self).__init__() if attrs == None: return offsets = [] offset = 0 for c in unistr: offsets.append(offset) offset += len(c.encode("utf8")) offsets.append(offset) for attr in attrs: pango_attr = None start_index = attr.start_index if attr.start_index >= 0 else 0 end_index = attr.end_index if attr.end_index >= 0 else 0 start_index = offsets[start_index] if start_index < len( offsets) else offsets[-1] end_index = offsets[end_index] if end_index < len( offsets) else offsets[-1] if attr.type == ibus.ATTR_TYPE_FOREGROUND: r = (attr.value & 0x00ff0000) >> 8 g = (attr.value & 0x0000ff00) b = (attr.value & 0x000000ff) << 8 pango_attr = pango.AttrForeground(r, g, b, start_index, end_index) elif attr.type == ibus.ATTR_TYPE_BACKGROUND: r = (attr.value & 0x00ff0000) >> 8 g = (attr.value & 0x0000ff00) b = (attr.value & 0x000000ff) << 8 pango_attr = pango.AttrBackground(r, g, b, start_index, end_index) elif attr.type == ibus.ATTR_TYPE_UNDERLINE: pango_attr = pango.AttrUnderline(int(attr.value), start_index, end_index) if pango_attr != None: self.insert(pango_attr)
def update_example(self, widget=None): start = 0 end = len(self.example) attributes = pango.AttrList() attributes.insert(pango.AttrForeground(self.text_color.red, self.text_color.green, self.text_color.blue, start, end)) attributes.insert(pango.AttrBackground(self.highlight_color.red, self.highlight_color.green, self.highlight_color.blue, start, end)) # Bold verification if self.bold_tg_button.get_active(): attributes.insert(pango.AttrWeight(pango.WEIGHT_HEAVY, start, end)) else: attributes.insert(pango.AttrWeight(pango.WEIGHT_NORMAL, start, end)) # Italic verification if self.italic_tg_button.get_active(): attributes.insert(pango.AttrStyle(pango.STYLE_ITALIC, start, end)) else: attributes.insert(pango.AttrStyle(pango.STYLE_NORMAL, start, end)) # Underline verification if self.underline_tg_button.get_active(): attributes.insert(pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end)) else: attributes.insert(pango.AttrUnderline(pango.UNDERLINE_NONE, start, end)) self.example_label.set_attributes(attributes)
def _set_attributes(self, bgcolor, fgcolor, size, weight): attr = pango.AttrList() bg_color = pango.AttrBackground(bgcolor[0], bgcolor[1], bgcolor[2], 0, -1) attr.insert(bg_color) size_attr = pango.AttrSize(size[0], size[1], size[2]) attr.insert(size_attr) weight_attr = pango.AttrWeight(weight[0], weight[1], weight[2]) attr.insert(weight_attr) fg_color = pango.AttrForeground(fgcolor[0], fgcolor[1], fgcolor[2], 0, 13) attr.insert(fg_color) return attr
def _new_attr(self, atype, colorcode, start, end): def get_gdk_color(ccolor): bg_color = self.get_style().base[gtk.STATE_NORMAL] return contrast.contrast_render_foreground_color(bg_color, ccolor) gcolor = get_gdk_color(colorcode) if atype == "fg": return pango.AttrForeground(gcolor.red, gcolor.green, gcolor.blue, start, end) elif atype == "bg": return pango.AttrBackground(gcolor.red, gcolor.green, gcolor.blue, start, end)
def recalc_edges(self): if not hasattr(self, 'layout'): return del self.layout show_text = self.attrs_changed() r, g, b = utils.selected_colors["fill"] r *= 65536 g *= 65536 b *= 65536 if self.index > self.end_index: bgsel = pango.AttrBackground(int(r), int(g), int(b), self.end_index, self.index) else: bgsel = pango.AttrBackground(int(r), int(g), int(b), self.index, self.end_index) self.attrlist.insert(bgsel) self.layout = pango.Layout(self.pango_context) self.layout.set_text(show_text) self.layout.set_attributes(self.attrlist) self.recalc_position()
def getLineAttrs(self, attr_array, start, end): attr_array_tmp = [] container = pango.AttrBackground(54321, 12345, 6553, start, end) attr_array_tmp.append(container) for attr in attr_array: if attr.end_index <= start: continue if attr.start_index >= end: continue attr_copy = attr.copy() if attr_copy.end_index > end: attr_copy.end_index = end if attr.start_index < start: attr_copy.start_index = start attr_array_tmp.append(attr_copy) return attr_array_tmp
def __init__(self, weh): super(GTKstrip, self).__init__() self.label = "GTKstrip" chars = "ESKISTLFÜNFZEHNZWANZIGDREIVIERTELTGNACHVORJMHALBQZWÖLFPZWEINSIEBENKDREIRHFÜNFELFNEUNVIERWACHTZEHNRSBSECHSFMUHR...." #chars = chars + "------------------------------------------------------------" #chars = chars + "------------------------------------------------------------" self.labels = [] self.colors = [] self.brightness = 180 self.weh = weh self.win = gtk.Window(gtk.WINDOW_TOPLEVEL) self.win.connect("delete_event", self.delete_event) self.win.connect("destroy", self.destroy) self.win.connect("key_press_event", self.key_press_event) table = gtk.Table(11, 11, True) x = 0 y = 0 for i in unicode(chars): label = gtk.Label(i) self.labels.append(label) self.colors.append(Color(0, 0, 0)) table.attach(label, x, x + 1, y, y + 1, FILL, FILL) x += 1 if x == 11: x = 0 y += 1 attrs = pango.AttrList() # attrs.insert(pango.AttrLanguage("de")) attrs.insert(pango.AttrForeground(0, 0, 0)) FONTSIZE = 15 attrs.insert(pango.AttrSize(FONTSIZE * 1000)) attrs.insert(pango.AttrBackground(0, 0, 0)) label.set_attributes(attrs) label.show() color = gtk.gdk.color_parse('#000000') self.win.modify_bg(gtk.STATE_NORMAL, color) self.win.add(table) self.win.show_all()
def set_attr(layout): attr = pango.AttrList() size = pango.AttrSize(int(11.5 * pango.SCALE), 0, -1) attr.insert(size) if customAttr and customAttr.attributes_dict: attributes_dict = customAttr.attributes_dict if attributes_dict.has_key("regex_include_match"): start, end = attributes_dict["regex_include_match"] pango_color = pango.AttrBackground(self.include_color.red, self.include_color.green, self.include_color.blue, start, end) attr.insert(pango_color) if attributes_dict.has_key("regex_exclude_match"): start, end = attributes_dict["regex_exclude_match"] pango_color = pango.AttrForeground(self.exclude_color.red, self.exclude_color.green, self.exclude_color.blue, start, end) attr.insert(pango_color) layout.set_attributes(attr)
def _generate_thumb_with_text(self): """ Generate the text on the pixbuf """ # This technique is used to compose image with text # create a pixmap with the data of the pixbuf then insert text # cf. pygtk FAQ How do I draw a text [or something else] on a gtk.gdk.pixbuf? for mor explication # (http://faq.pygtk.org/index.py?req=show&file=faq08.020.htp ) pixmap, mask = self._pixbuf_thumb.render_pixmap_and_mask() # graphic context and Drawarea any created to allow generation of pixbuf composition gc = pixmap.new_gc() area = gtk.DrawingArea() # create pango layout self.pangolayout = area.create_pango_layout("") # extract only the image name without extenstion text_buffer, ext = os.path.splitext(self._name) text_buffer = " " + text_buffer + " " self.pangolayout.set_text(text_buffer) # set text attributes attrs = pango.AttrList() attrs.insert(pango.AttrFamily(LCONST.THUMB_TEXT_FAMILY, 0, -1)) attrs.insert(pango.AttrStyle(pango.STYLE_ITALIC, 0, -1)) attrs.insert( pango.AttrForeground(LCONST.THUMB_TEXT_COLOR[0], LCONST.THUMB_TEXT_COLOR[1], LCONST.THUMB_TEXT_COLOR[2], 0, -1)) attrs.insert(pango.AttrBackground(0, 0, 0, 0, -1)) attrs.insert(pango.AttrSize(LCONST.THUMB_TEXT_SIZE, 0, -1)) self.pangolayout.set_attributes(attrs) # compute text layout position and set it on pixmap (w, h) = (self._pixbuf_thumb.get_width(), self._pixbuf_thumb.get_height()) (lw, lh) = self.pangolayout.get_pixel_size() pixmap.draw_layout(gc, (w - lw) // 2, (h - lh - 2), self.pangolayout) # function get_from_drawable gets the the pixbuf from the pixmap # no need to affect resuly to a new pisbuf: self._pixbuf_thumb is changed whe get_from_drawable is used self._pixbuf_thumb.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, -1, -1)
def set_candidates(self, candidates, focus_candidate=0, show_cursor=True): assert len(candidates) <= len(self.__labels) for i, (text, attrs) in enumerate(candidates): if i == focus_candidate and show_cursor: if attrs == None: attrs = pango.AttrList() color = self.__labels[i][1].style.base[gtk.STATE_SELECTED] end_index = len(text.encode("utf8")) attr = pango.AttrBackground(color.red, color.green, color.blue, 0, end_index) attrs.change(attr) color = self.__labels[i][1].style.text[gtk.STATE_SELECTED] attr = pango.AttrForeground(color.red, color.green, color.blue, 0, end_index) attrs.insert(attr) self.__labels[i][1].set_text(text) self.__labels[i][1].show() self.__labels[i][1].set_property("attributes", attrs) for w in self.__candidates[i]: w.show() for w in reduce(operator.add, self.__candidates[len(candidates):]): w.hide()