Advertisement
kidino

convert svg

Sep 15th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         var svg_image = document.getElementsByTagName('image');
  2.         var svg_count = 0;
  3.         var svg_count_done = 0;
  4.        
  5.         if ( svg_image.length > 0 ) {
  6.             svg_finish = 0;
  7.             svg_count_done = 0;
  8.             for(var si = 0; si < svg_image.length; si++ ) {
  9.                 var svg_image_href = svg_image[si].getAttribute('xlink:href');
  10.                 console.log( svg_image_href );
  11.                 if ( (svg_image_href == '') || (svg_image_href == null) || (svg_image_href == undefined)) {
  12.                     svg_image_href = svg_image[si].getAttribute('href');
  13.                 }
  14.                
  15.                 var new_image_href = resolvePath(svg_image_href, opf.manifest[opf.spine[ this_chapter ]].href );
  16.                 var img_ext = new_image_href.substring( new_image_href.lastIndexOf('.') + 1 );
  17.                 var this_image = svg_image[si];
  18.                
  19.                 $.ajax({
  20.                   url: new_image_href,
  21.                   beforeSend: function ( xhr ) { xhr.overrideMimeType("text/plain; charset=x-user-defined"); }
  22.                 })
  23.                 .done(function ( data2 ) {  
  24.                     svg_count_done++;
  25.  
  26.                     var image_el = document.createElement("IMG");
  27.                     var img_data = do_decrypt( book_key, data2, 'binary');
  28.                     image_el.setAttribute('class','svg-image');
  29.                     svg_count++;
  30.                     image_el.setAttribute('id','svg'+svg_count);
  31.  
  32.                     image_el.setAttribute("src", 'data:image/'+img_ext+';base64,'+img_data );
  33.                     console.log('svg_length:'+img_data.length);
  34.                    
  35.                     var this_parent = this_image.parentNode; // the SVG
  36.                     var parent_parent = this_parent.parentNode; // SVG's parent
  37.                    
  38.                     parent_parent.insertBefore(image_el, this_parent);
  39.                     $('img#svg'+svg_count).load(function(){
  40.                         if ( $(this).width() > (colwidth) ) {
  41.                             var ori_width = $(this).width();
  42.                             var ori_height = $(this).height();
  43.                             var resize_ratio = (colwidth) / ori_width;
  44.                            
  45.                             $(this).width( colwidth );
  46.                             $(this).height( ori_height * resize_ratio );
  47.                         }
  48.                        
  49.                         if ( $(this).height() > (colheight) ) {
  50.                             var ori_width = $(this).width();
  51.                             var ori_height = $(this).height();
  52.                             var resize_ratio = (colheight) / ori_height;
  53.                            
  54.                             $(this).height( colheight );
  55.                             $(this).width( ori_width * resize_ratio );
  56.                         }
  57.                     });
  58.                  
  59.                   delete img_data;
  60.                   delete image_el;
  61.                   delete this_parent;
  62.                   delete parent_parent;
  63.                 })
  64.                 .fail(function(){
  65.                     svg_count_done++;
  66.                 })
  67.                 .always(function(){
  68.                     console.log('svg_count_done:'+svg_count_done +'  svg_image.length:'+svg_image.length);
  69.                     if ( svg_count_done == svg_image.length ) {
  70.                         svg_finish = 1;
  71.                         $('svg').hide();
  72.                     }
  73.                 });
  74.             }          
  75.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement