bugfixes
authorMatthias Kramm <kramm@quiss.org>
Sun, 16 Aug 2009 16:52:54 +0000 (18:52 +0200)
committerMatthias Kramm <kramm@quiss.org>
Sun, 16 Aug 2009 16:52:54 +0000 (18:52 +0200)
lib/devices/pdf.c
lib/gfxtools.c
lib/gfxtools.h
spec/simpletext.spec.rb
spec/smalltext.pdf
spec/smalltext.py
spec/spec_helper.rb

index 05eaac0..ba92ed0 100644 (file)
@@ -24,6 +24,9 @@
 #include <unistd.h>
 #include <memory.h>
 #include <pdflib.h>
+#include <math.h>
+#include "../os.h"
+#include "../jpeg.h"
 #include "../types.h"
 #include "../mem.h"
 #include "../gfxdevice.h"
@@ -45,7 +48,7 @@ void pdf_startpage(gfxdevice_t*dev, int width, int height)
     internal_t*i = (internal_t*)dev->internal;
 
     if(!i->tempfile) {
-       i->tempfile = strdup("tmp.pdf");
+       i->tempfile = strdup(mktempname(0));
        PDF_open_file(i->p, i->tempfile);
        PDF_set_parameter(i->p, "usercoordinates", "true");
        PDF_set_parameter(i->p, "topdown", "true");
@@ -91,7 +94,7 @@ void pdf_startclip(gfxdevice_t*dev, gfxline_t*line)
     if(mkline(line, i->p))
        PDF_clip(i->p);
     else   
-       ; // TODO: not sure about this
+       ; // TODO: strictly speaking, an empty clip clears everything
 
 }
 void pdf_endclip(gfxdevice_t*dev)
@@ -127,8 +130,20 @@ void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t
 {
     internal_t*i = (internal_t*)dev->internal;
 
-    //PDFLIB_API int PDFLIB_CALL
-    //PDF_load_image(i->pPDF *p, const char *imagetype, const char *filename, int len, const char *optlist);
+    double l1 = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01)*img->width;
+    double l2 = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11)*img->height;
+    double r = atan2(matrix->m01, matrix->m00);
+
+    /* fit_image needs the lower left corner of the image */
+    double x = matrix->tx + matrix->m10*img->height;
+    double y = matrix->ty + matrix->m11*img->height;
+
+    char*tempfile = mktempname(0);
+    char options[80];
+    sprintf(options, "boxsize {%f %f} fitmethod meet rotate %f", l1, l2, r*180/M_PI);
+    gfximage_save_jpeg(img, tempfile, 99);
+    int imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
+    PDF_fit_image(i->p, imgid, x, y, options);
 }
 
 void pdf_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
index 239ba6f..9975e6d 100644 (file)
@@ -28,6 +28,7 @@
 #include <assert.h>
 #include "gfxtools.h"
 #include "gfxfont.h"
+#include "jpeg.h"
 
 typedef struct _linedraw_internal
 {
@@ -987,3 +988,18 @@ void gfxline_dump(gfxline_t*line, FILE*fi, char*prefix)
     }
 }
 
+void gfximage_save_jpeg(gfximage_t*img, char*filename, int quality)
+{
+    unsigned char*data = malloc(img->width*img->height*3);
+    int t;
+    int size = img->width*img->height;
+    int s = 0;
+    for(t=0;t<size;t++) {
+       data[s+0] = img->data[t].r;
+       data[s+1] = img->data[t].g;
+       data[s+2] = img->data[t].b;
+       s+=3;
+    }
+    jpeg_save(data, img->width, img->height, quality, filename);
+}
+
index c2566d3..163138b 100644 (file)
@@ -85,6 +85,8 @@ gfxfont_t*gfxfontlist_findfont(gfxfontlist_t*list, char*id);
 char gfxfontlist_hasfont(gfxfontlist_t*list, gfxfont_t*font);
 void gfxfontlist_free(gfxfontlist_t*list, char deletefonts);
 
+void gfximage_save_jpeg(gfximage_t*img, char*filename, int quality);
+
 gfxbbox_t* gfxline_isrectangle(gfxline_t*_l);
 
 gfxline_t*gfxline_makerectangle(double x1, double y1, double x2, double y2);
index d5911ad..874ce59 100644 (file)
@@ -8,5 +8,6 @@ describe "pdf conversion" do
         pixel_at(97,164).should_be_the_same_as pixel_at(208,256)
         area_at(215,80,333,156).should_be_plain_colored
         area_at(46,126,180,158).should_not_be_plain_colored
+        area_at(38,118,170,152).should_contain_text 'Hello World'
     end
 end
index d7c7eeb..7f2323f 100644 (file)
Binary files a/spec/smalltext.pdf and b/spec/smalltext.pdf differ
index a0ac9df..45fa2d7 100644 (file)
@@ -10,7 +10,7 @@ PDF_begin_page(p, 612, 200)
 font = PDF_load_font(p, "Helvetica", "host", "")
 
 PDF_setfont(p, font, 6.0)
-PDF_set_text_pos(p, 40, 100);PDF_show(p, "|")
+PDF_set_text_pos(p, 40, 100);PDF_show(p, "'")
 PDF_set_text_pos(p, 80, 100);PDF_show(p, "i")
 PDF_set_text_pos(p, 120, 100);PDF_show(p, "l")
 PDF_set_text_pos(p, 160, 100);PDF_show(p, "-")
index 1ee0b39..cf9c971 100644 (file)
@@ -91,6 +91,14 @@ class Pixel
        "(#{@x},#{@y})"
     end
 end
+
+$tempfiles = []
+Kernel.at_exit do
+    $tempfiles.each do |file|
+       `rm -f #{file}`
+    end
+end
+
 class DocFile
     def initialize(filename, page)
        @filename = filename
@@ -99,6 +107,7 @@ class DocFile
     def convert()
        return if @swfname
        @swfname = @filename.gsub(/.pdf$/i,"")+".swf"
+       $tempfiles += [@swfname]
        `pdfinfo #{@filename}` =~ /Page size:\s*([0-9]+) x ([0-9]+) pts/
        width,height = $1,$2
        dpi = (72.0 * 612 / width.to_i).to_i
@@ -115,15 +124,14 @@ class DocFile
            raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname)
            @img = Magick::Image.read(@pngname).first
        ensure
-           `rm -f #{@swfname}`
            `rm -f #{@pngname}`
        end
     end
     def get_text(x1,y1,x2,y2)
        self.convert()
-       puts "swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}"
-       puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
-       `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
+       #puts "swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}"
+       #puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
+       `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`.chomp
     end
     def get_area(x1,y1,x2,y2)
        self.render()