Re: Gnome2::Print

* Cornel Ghiban <green neomedia ro>:

Ok.. I found the examples/* (they work very well), but I still can't use 
Gnome2::Print. Let's say I have a text, net very long - 30 lines for example,
how can I print this text?

If you are pretty sure that the lenght of each line is less than the
page width, you might just cycle through every line of the text and
        my $pc = $job->get_context;
        $pc->moveto(..., ...);

Otherwise, if you do not know the lenght of the lines (you are printing
the contents of a GtkTextBuffer, for instance), you'll have to check if
the next character/word is out of the page boundaries, and act on this


Remember: the minimum height between lines is calculated using:
        # $font is a Gnome2::Print::Font object
        # height('b') + height('p');
        $font_height = $font->get_ascender + $font->get_descender;

Those are values specified by each font.


If you have a buffer consisting of lines, here's some code:

sub render_job
        my $job = shift;
        my $conf = $job->get_config;
        my $pc = $job->get_context;
        my $buffer = [
                "Twinkle Twinkle",
                "Little Star",
                "Twinkle Twinkle",
                "Where You Are",
        my $font = Gnome2::Print::Font->find_closest("Sans Regular", 16);
        # line_space = max_font_size + 20%
        my $line_space = ($font->get_descender + $font->get_ascender) * 1.2;
        my ($width, $height) = $job->get_page_size;
        my $cur_x = 50;                 # 50px border left
        my $cur_y = $height - 100;      # 100px border bottom
        my $page_n = 1;
        # baginpage wants a string, not an integer; this is just
        # plain braindead, but fortunately we aren't using C...
        $pc->beginpage(sprintf("%d", $page_n++));
        $pc->moveto($cur_x, $cur_y);
        foreach (@$buffer)
                $cur_y -= $line_space;
                # force new page, just to show how does it work.
                if ($cur_y < 700)
                        $pc->beginpage(sprintf("%d", $page_n++));
                        $cur_y = $height - 100;
                $pc->moveto($cur_x, $cur_y);

[ just use replace the render_job sub inside examples/ to
test it ]


I'll try and come up with a better example that gathers the text froma
TextView widget and prints it.


Emmanuele Bassi (Zefram)                 [ ]
GnuPG Key fingerprint = 4DD0 C90D 4070 F071 5738  08BD 8ECC DB8F A432 0FF4

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