2

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!

wolfv
  • 580

1 Answers1

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()
xubuntix
  • 5,580