Exemplo n.º 1
0
    def create_pango_layout(self, text, *styles):
        layout = self.context.create_pango_layout()

        layout.set_text(text)
        layout.set_font_description(self.font)

        # We need to save some stuff for when we render; just stick extra properties on the
        # layout. We may want to move to a setup where we have "print objects" for layouts
        # or custom results.
        layout._left_margin = 0
        layout._right_margin = 0
        layout._paragraph_background = None

        attrs = pango.AttrList()
        for style in styles:
            spec = DEFAULT_STYLE.get_spec(style)
            spec.add_pango_attributes(attrs, start_index=0, end_index=0x7fffffff)

            # We are fudging pixels vs. points here
            if spec.left_margin:
                layout._left_margin = spec.left_margin
            if spec.left_margin:
                layout._right_margin = spec.right_margin
            if spec.paragraph_background:
                layout._paragraph_background = pango.Color(spec.paragraph_background)

        layout._width = self.context.get_width() - layout._left_margin - layout._right_margin
        layout.set_width(int(pango.SCALE * layout._width))

        layout.set_attributes(attrs)

        return layout
Exemplo n.º 2
0
    def append_chunk_text(self, chunk):
        text = self.worksheet.get_text(start_line=chunk.start, end_line=chunk.end - 1)

        if isinstance(chunk, CommentChunk):
            layout = self.create_pango_layout(text, 'comment')
        else:
            layout = self.create_pango_layout(text)

        if isinstance(chunk, StatementChunk):
            attrs = layout.get_attributes() # makes a copy
            index = 0

            # The complexity here is because Pango attributes encode positions by byte
            # index for UTF-8 encoded text while we store the tokenization in Unicode
            # character positions.
            for i in xrange(chunk.start, chunk.end):
                line = self.worksheet.get_line(i)
                offset = 0
                for token_type, start_offset, end_offset, _ in chunk.tokenized.get_tokens(i - chunk.start):
                    start_index = index + len(line[offset:start_offset].encode("UTF-8"))
                    end_index = start_index + len(line[start_offset:end_offset].encode("UTF-8"))
                    spec = DEFAULT_STYLE.get_spec(token_type)
                    if spec is not None:
                        spec.add_pango_attributes(attrs, start_index, end_index)
                    index = end_index
                    offset = end_offset
                index += len(line[offset:].encode("UTF-8"))
                index += 1 # newline

            layout.set_attributes(attrs) # set the copy back

        self.append_pango_layout(layout)
Exemplo n.º 3
0
    def append_chunk_text(self, chunk):
        text = self.worksheet.get_text(start_line=chunk.start,
                                       end_line=chunk.end - 1)

        if isinstance(chunk, CommentChunk):
            layout = self.create_pango_layout(text, 'comment')
        else:
            layout = self.create_pango_layout(text)

        if isinstance(chunk, StatementChunk):
            attrs = layout.get_attributes()  # makes a copy
            index = 0

            # The complexity here is because Pango attributes encode positions by byte
            # index for UTF-8 encoded text while we store the tokenization in Unicode
            # character positions.
            for i in xrange(chunk.start, chunk.end):
                line = self.worksheet.get_line(i)
                offset = 0
                for token_type, start_offset, end_offset, _ in chunk.tokenized.get_tokens(
                        i - chunk.start):
                    start_index = index + len(
                        line[offset:start_offset].encode("UTF-8"))
                    end_index = start_index + len(
                        line[start_offset:end_offset].encode("UTF-8"))
                    spec = DEFAULT_STYLE.get_spec(token_type)
                    if spec is not None:
                        spec.add_pango_attributes(attrs, start_index,
                                                  end_index)
                    index = end_index
                    offset = end_offset
                index += len(line[offset:].encode("UTF-8"))
                index += 1  # newline

            layout.set_attributes(attrs)  # set the copy back

        self.append_pango_layout(layout)
Exemplo n.º 4
0
    def create_pango_layout(self, text, *styles):
        layout = self.context.create_pango_layout()

        layout.set_text(text)
        layout.set_font_description(self.font)

        # We need to save some stuff for when we render; just stick extra properties on the
        # layout. We may want to move to a setup where we have "print objects" for layouts
        # or custom results.
        layout._left_margin = 0
        layout._right_margin = 0
        layout._paragraph_background = None

        attrs = pango.AttrList()
        for style in styles:
            spec = DEFAULT_STYLE.get_spec(style)
            spec.add_pango_attributes(attrs,
                                      start_index=0,
                                      end_index=0x7fffffff)

            # We are fudging pixels vs. points here
            if spec.left_margin:
                layout._left_margin = spec.left_margin
            if spec.left_margin:
                layout._right_margin = spec.right_margin
            if spec.paragraph_background:
                layout._paragraph_background = pango.Color(
                    spec.paragraph_background)

        layout._width = self.context.get_width(
        ) - layout._left_margin - layout._right_margin
        layout.set_width(int(pango.SCALE * layout._width))

        layout.set_attributes(attrs)

        return layout