fix load from buffer

This commit is contained in:
John Cupitt 2016-02-09 11:58:43 +00:00
parent 153886d2eb
commit 56e45545d3
3 changed files with 29 additions and 2 deletions

8
TODO
View File

@ -1,4 +1,10 @@
- test suite is failing for svg load from buffer, strange - make vips_foreign_load_svg_is_a_buffer() much faster
test for first line starts "<?xml"
and second line starts "<svg"
will this be too fragile? what about extra whitespace, case, DOS files etc
etc.
- gif loader? - gif loader?

View File

@ -356,6 +356,21 @@ typedef VipsForeignLoadSvgClass VipsForeignLoadSvgBufferClass;
G_DEFINE_TYPE( VipsForeignLoadSvgBuffer, vips_foreign_load_svg_buffer, G_DEFINE_TYPE( VipsForeignLoadSvgBuffer, vips_foreign_load_svg_buffer,
vips_foreign_load_svg_get_type() ); vips_foreign_load_svg_get_type() );
static gboolean
vips_foreign_load_svg_is_a_buffer( const void *buf, size_t len )
{
char *str = (char *) buf;
/* Ouch! So slow!! This can easily end up parsing the entire document.
*/
if( (page = rsvg_handle_new_from_data( buf, len, NULL )) ) {
g_object_unref( page );
return( 1 );
}
return( 0 );
}
static int static int
vips_foreign_load_svg_buffer_header( VipsForeignLoad *load ) vips_foreign_load_svg_buffer_header( VipsForeignLoad *load )
{ {
@ -380,6 +395,7 @@ vips_foreign_load_svg_buffer_class_init(
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS( class ); GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class; VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class; VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->set_property = vips_object_set_property; gobject_class->set_property = vips_object_set_property;
@ -388,6 +404,12 @@ vips_foreign_load_svg_buffer_class_init(
object_class->nickname = "svgload_buffer"; object_class->nickname = "svgload_buffer";
object_class->description = _( "load SVG with rsvg" ); object_class->description = _( "load SVG with rsvg" );
/* is_a() is not quick, it must parse the whole document ...
* lower the priority.
*/
foreign_class->priority = -50;
load_class->is_a_buffer = vips_foreign_load_svg_is_a_buffer;
load_class->header = vips_foreign_load_svg_buffer_header; load_class->header = vips_foreign_load_svg_buffer_header;
VIPS_ARG_BOXED( class, "buffer", 1, VIPS_ARG_BOXED( class, "buffer", 1,

View File

@ -371,7 +371,6 @@ class TestForeign(unittest.TestCase):
return return
def svg_valid(self, im): def svg_valid(self, im):
im.write_to_file("x.v")
a = im(10, 10) a = im(10, 10)
self.assertAlmostEqualObjects(a, [0, 0, 77, 255]) self.assertAlmostEqualObjects(a, [0, 0, 77, 255])
self.assertEqual(im.width, 360) self.assertEqual(im.width, 360)