Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am desperatly trying to find some working solution on how to modify the Letter Spacing in a GTK TextView.

I am using python with GTK 3.6 -- It seems that the solution is to somehow use Pango, but I don't understand how. So any working prototype would be greatly appreciated.

Thanks!

share|improve this question
add comment

1 Answer 1

Based on the documentation of GtkTextTags which are used in GtkTextBuffers, there should be a way to stretch or condense text just like underlining text or making it bold.

I have tried this (see example below), but it is not working for the stretching part. I have read somewhere, however, that the font has to support the stretch. So maybe, this is a problem of the font used.

Not really working example of how the documentation tells us how it should work:

#!/usr/bin/python

from gi.repository import Gtk
import pango

class TextViewWindow:
    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_default_size(400, 400)

        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()

        tag_table = self.textbuffer.get_tag_table()
        stretched_tag = self.textbuffer.create_tag("str", stretch=pango.STRETCH_EXPANDED)
        tag_table.add(stretched_tag)

        bold_tag = self.textbuffer.create_tag("bld", weight=pango.WEIGHT_BOLD)
        tag_table.add(bold_tag)

        underline_tag = self.textbuffer.create_tag("und", underline=pango.UNDERLINE_SINGLE)
        tag_table.add(underline_tag)


        self.textbuffer.set_text("Here is some text: normal.\n")
        sob, eob = self.textbuffer.get_bounds()
        self.textbuffer.insert_with_tags_by_name(eob, "Here is some text: expanded.\n", "str")
        sob, eob = self.textbuffer.get_bounds()
        self.textbuffer.insert_with_tags_by_name(eob, "Here is some text: bold.\n", "bld")
        sob, eob = self.textbuffer.get_bounds()
        self.textbuffer.insert_with_tags_by_name(eob, "Here is some text: underlined.\n", "und")


        self.window.add(self.textview)

        self.window.show_all()
        self.window.connect('destroy', lambda w: Gtk.main_quit())


def main():
    app = TextViewWindow()
    Gtk.main()

if __name__ == "__main__":
    main()
share|improve this answer
    
hi xubuntix, thanks for your detailed explanation. But I am searching specifically for letter spacing, something (it seems) only settable with Pango Attributes. The only thing similar I found: stackoverflow.com/questions/8783670/… (but I need it for a TextView, not a label) and here's the documentation for attributes: developer.gnome.org/pango/stable/… (but somehow, pango_attr_letter_spacing_new doesn't seem to exist in python -.-) –  wolfv Nov 8 '12 at 12:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.