From ecd7c7248b9a93163090bfe7512b2a53d8da6d95 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Sun, 16 Aug 2009 18:52:54 +0200 Subject: [PATCH] bugfixes --- lib/devices/pdf.c | 23 +++++++++++++++++++---- lib/gfxtools.c | 16 ++++++++++++++++ lib/gfxtools.h | 2 ++ spec/simpletext.spec.rb | 1 + spec/smalltext.pdf | Bin 2746 -> 2749 bytes spec/smalltext.py | 2 +- spec/spec_helper.rb | 16 ++++++++++++---- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/lib/devices/pdf.c b/lib/devices/pdf.c index 05eaac0..ba92ed0 100644 --- a/lib/devices/pdf.c +++ b/lib/devices/pdf.c @@ -24,6 +24,9 @@ #include #include #include +#include +#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) diff --git a/lib/gfxtools.c b/lib/gfxtools.c index 239ba6f..9975e6d 100644 --- a/lib/gfxtools.c +++ b/lib/gfxtools.c @@ -28,6 +28,7 @@ #include #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;tdata[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); +} + diff --git a/lib/gfxtools.h b/lib/gfxtools.h index c2566d3..163138b 100644 --- a/lib/gfxtools.h +++ b/lib/gfxtools.h @@ -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); diff --git a/spec/simpletext.spec.rb b/spec/simpletext.spec.rb index d5911ad..874ce59 100644 --- a/spec/simpletext.spec.rb +++ b/spec/simpletext.spec.rb @@ -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 diff --git a/spec/smalltext.pdf b/spec/smalltext.pdf index d7c7eeb57233d84016bf0499e45de3687449f710..7f2323fe7f5f60a4dd2225ed08f5a80ebc3442cf 100644 GIT binary patch delta 325 zcmdlbx>t0!IWmAPhv5w=jh^8|aO-+mfE||8SxIQ6mQZ|P; zLr=Ye+r&N6j3yIrRI-~Hnj4xJm~D<=6y*>#RWJYng**i=FvGye(9~pd6Q_E;CAy4( ziIE|On30h=rkI5hx|pG*xiN;Asf7i)n31so)ZWP&T&A(+CI&7pW`@R=M&{;j29Ac# yZWb1fW`-_CmafK5mgbgrHUw3~a@pB&6_+Fyl~fd^rg0e=o0@Z}s=E5SaRC5|kWw@N delta 274 zcmdlhx=VDzBrVffijfL-Ez>&>opyZqvZ+F$SjX{MMAMmzrY1%K7fe-8t6Ue*W7xV{ zVfMuR(u{@^Z&$J#8yOm#8X9blW)$UMG@4wd%xs_90)J(wu1QhZVxWEhp0~14w z$p<-wZA~y_jLa?3#SAUY4KTz^Elej1atSk