Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    def set_font(self, font_name):
        if not self.editing:
            return
        start, end = minmax(self.index, self.end_index)

        Pango_font = Pango.FontDescription(font_name)
        attr = Pango.AttrFontDesc(Pango_font, start, end)

        if start == end:
            self.undo.add_undo(
                UndoManager.UndoAction(self, UNDO_ADD_ATTR, self.undo_attr_cb,
                                       attr))
            self.current_attrs.append(attr)
        else:
            old_attrs = self.attributes.copy()
            self.attributes.change(attr)
            self.undo.add_undo(
                UndoManager.UndoAction(self, UNDO_ADD_ATTR_SELECTION,
                                       self.undo_attr_cb, old_attrs,
                                       self.attributes.copy()))
        self.recalc_edges()
Ejemplo n.º 3
0
 def _set_font(self, value, attrlist):
     attrlist.change(Pango.AttrFontDesc(
             Pango.FontDescription(value), 0, -1))
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
 def set_font_size(label, fontsize=48.0):
     siz_attr = Pango.AttrFontDesc(
         Pango.FontDescription.from_string(str(fontsize)), 0, -1)
     attrs = Pango.AttrList()
     attrs.insert(siz_attr)
     label.set_attributes(attrs)