G::O::I based bindings and problem passing char array ref



Hello,

A few months ago I updated the Poppler module to use
Glib::Object::Introspection to generate the bindings. I just realized
yesterday that this broke the "new_from_data()" constructor. I realized
this as I was wrapping librsvg 2.0 in a similar fashion
(https://github.com/jvolkening/p5-Image-Rsvg) and came across the
problem of passing in a Perl scalar when the underlying library wanted
a char array pointer. I borrowed the "_unpack_unless_array_ref()"
function from Gtk3 in that instance and it solved the problem. However,
it doesn't seem to be working in the same way for the Poppler bindings.
I get a "PDF document is damaged" error from the underlying libs
regardless of what I try. For now I hacked it to use an intermediate
file on disk but I'd like to get it working properly.

I thought I'd ask for advice here first as I figured this group would
have the most experience dealing with G::O::I-based bindings. The
previous bindings written directly in XS (not by me) worked without
doing anything fancy that I can see:

    hPopplerDocument*
    new_from_data( class , data , length )
            char * class;
            char * data;
            int * length;
    PREINIT:
            PopplerDocument *document;
    CODE:
            g_type_init();
            Newz(0, RETVAL, 1, hPopplerDocument );
            document = poppler_document_new_from_data( data, length,
                 NULL, NULL ); if( document == NULL )
            { croak("poppler_document_new_from_data failed"); }
            RETVAL->handle = document;
    OUTPUT:
            RETVAL

Below is the override I added to the module with the
non-working part commented out and the hack below it. I didn't
include the "_unpack_unless_array_ref()" code below but it was taken
verbatim from Gtk3. The C method expects a char array pointer, the data
length, and an optional password (ignored for now) as parameters. If
anyone has any advice or suggestions they would be greatly appreciated. 

Regards,
Jeremy


    sub Poppler::Document::new_from_data {

        my ($class, $data) = @_;

        #-----------------------------
        # this is how it should be done, but can't get it to work yet
        #-----------------------------

        #$data = _unpack_unless_array_ref( $data );
        #my $len = scalar(@$data);

        #return Glib::Object::Introspection->invoke (
            #$_POPPLER_BASENAME, 'Document', 'new_from_data',
            #$class, $data, $len
        #);

        #-----------------------------
        # this is an ugly hack to make things work
        #-----------------------------

        use File::Temp;
        my $tmp = File::Temp->new( UNLINK => 1 );
        print {$tmp} $data;
        close $tmp;
        return Poppler::Document->new_from_file("$tmp", $pwd);

    }


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