Re: exporting textbuffer formatting




On Mar 15, 2006, at 4:44 AM, Fabrizio Lanza wrote:

 A couple of other questions.
I'm writing a filter that will load an HTML document in a TextView (this could be useful to the community also; I could not find such a thing yet).

A problem I met with the export filter, is when tags overlap. Example: there is a div align="center" which includes a couple of p align="right" or such. I dinamically built tags for the TextBuffer and applied them. Which tag will have precedence when 2 different styles/justifications/etc. are applied to the same piece of text? I noticed unpredicted behaviours. One time the second applied style had precedence, one time the first applied style had precedence.


Now the big question. To apply a tag I just give the tag name, and the start and end position in an iterator. How do I retrieve tag name, start and end position from a TextBuffer to build an HTML export filter? My Idea was to iterate through each char of the TextBuffer, and build a table with all the formatting contained in the TextBuffer. Then, I could easily re-build HTML tags. Does it work this way? Which functions should I use?

# find out which tags are in use at this location.
@list = $iter->get_tags ();

Unfortunately it doesn't appear that there's a way to find out the ranges associated with each tag, so you'd have to iterate over the buffer one character at a time fetching the tags, and detect the those tag ranges yourself.

here's a quick mockup just because i was curious. i'll leave turning this into html as an exercise for you. :-)


use strict;
use Gtk2 -init;
use Gtk2::Pango; # for constants

my $buffer = Gtk2::TextBuffer->new;
# create a tag that knows its name, in $tag->{name}
sub create_tag {
        my ($buffer, $name, @args) = @_;
        my $tag = $buffer->create_tag ($name, @args);
        $tag->{name} = $name;
        return $tag;
}
create_tag ($buffer,'strong', weight => PANGO_WEIGHT_BOLD);
create_tag ($buffer, 'emphasis', style => 'italic');
create_tag ($buffer, 'typewriter', family => 'monospace');

# syntactic sugar.
sub insert {
        my $buffer = shift;
        my $text = shift;
        $buffer->insert_with_tags_by_name ($buffer->get_end_iter, $text, @_);
}
insert ($buffer, "Here is some text.  ");
insert ($buffer, "Some pretty, pretty text.", 'emphasis');
insert ($buffer, "\n\n");
insert ($buffer, "And ", 'emphasis', 'strong');
insert ($buffer, "very pretty", 'strong');
insert ($buffer, " text it is.", 'typewriter');

{
        my $iter = $buffer->get_start_iter;
        while (!$iter->is_end) {
                my $char = $iter->get_char;
                my @tags = $iter->get_tags;
                print "  $char  ".join(",",map { $_->{name} } @tags)."\n";
                $iter->forward_char();
        }
}





--
muppet <scott at asofyet dot org>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]