Browse Source

Fix some bugs in wkhtmltoimage, apply websettings, save output to stdout, use feedback

Jakob Truelsen 15 years ago
parent
commit
9780a15372

+ 3 - 3
src/image/arguments.cc

@@ -34,9 +34,9 @@ CommandLineParser::CommandLineParser(wkhtmltopdf::settings::Global & s):
 	addarg("crop-y",0,"Set y coordinate for croping", new IntSetter(s.crop.top,"int"));
 	addarg("crop-w",0,"Set width for croping", new IntSetter(s.crop.width,"int"));
 	addarg("crop-h",0,"Set height for croping", new IntSetter(s.crop.height,"int"));
-
-	extended(false);
-	qthack(false);
+	addarg("format",'f',"Output format", new QStrSetter(s.fmt, "format") );
+	extended(true);
+	qthack(true);
 
 #ifdef Q_WS_X11
 	addarg("use-xserver",0,"Use the X server (some plugins and other stuff might not work without X11)", new ConstSetter<bool>(s.useGraphics,true));

+ 2 - 2
src/image/commandlineparser.cc

@@ -109,9 +109,9 @@ void CommandLineParser::parseArguments(int argc, const char ** argv, bool final)
     settings.out="";
 	bool defaultMode=false;
 	for (int i=1; i < argc; ++i) {
-        if(i==argc-2 && argv[i][1]!='-'){ // the arg before last (in)
+        if(i==argc-2 && (argv[i][0] != '-' || argv[i][1] == '\0')) { // the arg before last (in)
             settings.in = argv[i];
-        } else if (i==argc-1 && argv[i][1]!='-'){ // the last arg (out)
+        } else if (i==argc-1 && (argv[i][0] != '-' || argv[i][1] == '\0')) { // the last arg (out)
             settings.out = argv[i];
 		} else {
 			parseArg(global, argc, argv, defaultMode, i, 0);

+ 1 - 1
src/image/image.pro

@@ -38,6 +38,6 @@ include(../shared/shared.pri)
 # Input
 HEADERS += imageconverter.hh imageconverter_p.hh settings.hh converter.hh
 SOURCES += wkhtmltoimage.cc arguments.cc commandlineparser.cc docparts.cc \
-           imageconverter.cc
+           imageconverter.cc settings.cc
 
 

+ 46 - 35
src/image/imageconverter.cc

@@ -49,9 +49,9 @@ void ImageConverterPrivate::beginConvert() {
 	error = false;
 	convertionDone = false;
 	progressString = "0%";
-	currentPhase=0;
 	loaderObject = loader.addResource(settings.in, settings.loadPage);
-	
+	updateWebSettings(loaderObject->page.settings(), settings.web);
+	currentPhase=0;
 	emit out. phaseChanged();
 	loadProgress(0);
 	loader.load();
@@ -63,13 +63,12 @@ void ImageConverterPrivate::clearResources() {
 }
 
 void ImageConverterPrivate::pagesLoaded(bool ok) {
-	//TODO theck ok
-
-	// if fmt is empty try to get it from file extension in out
-	if(settings.fmt==""){
-		QFileInfo fi(settings.out);
-        settings.fmt = fi.suffix();
+	if (errorCode == 0) errorCode = loader.httpErrorCode();
+	if (!ok) {
+		fail(); 
+		return;
 	}
+	
 	// check whether image format is supported (for writing)
 //	QImageWriter test;
 //	test.setFormat(settings.fmt);
@@ -80,47 +79,59 @@ void ImageConverterPrivate::pagesLoaded(bool ok) {
 //	}
 	// create webkit frame and load website
 	
-	// begin looping till loadFinished is encountered
-	//QEventLoop loop;
-	//QObject::connect(&page, SIGNAL(loadFinished(bool)), &loop, SLOT( quit( )));
-	//loop.exec( );
-	// paint image
-	//if(!settings.quiet)printf("downloading complete, converting...\n");
-
+	currentPhase=1;
+	emit out. phaseChanged();
+	loadProgress(0);
 	loaderObject->page.setViewportSize(loaderObject->page.mainFrame()->contentsSize());
-
-	
 	QImage image(loaderObject->page.viewportSize(), QImage::Format_ARGB32_Premultiplied);
 	QPainter painter(&image);
 
 	loaderObject->page.mainFrame()->render(&painter);
 	painter.end();
+	loadProgress(30);
 	// perform filter(s)
-	if(settings.crop.width!=-1 && settings.crop.height!=-1){
-		if(!settings.quiet)printf("cropping...\n");
-		// note: the returned image object is a copy; is this a memleak? does c++ have a GC?
+	if (settings.crop.width > 0 && settings.crop.height > 0) 
 		image=image.copy(settings.crop.left,settings.crop.top,settings.crop.width,settings.crop.height);
-	}
-	if(settings.scale.width!=-1 && settings.scale.height!=-1){
-		if(!settings.quiet)printf("scaling...\n");
+
+	loadProgress(50);
+	if (settings.scale.width > 0 && settings.scale.height > 0) {
 		// todo: perhaps get more user options to change aspect ration and scaling mode?
-		// note: the returned image object is a copy; is this a memleak? does c++ have a GC?
 		image=image.scaled(settings.scale.width,settings.scale.height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
 	}
+	loadProgress(80);
+
+
+	QFile file;
+	bool openOk;
 	// output image
-	if(settings.out!="-"){
-		// save to file
-		if(!settings.quiet)printf("saving image...\n");
-		QByteArray ba=settings.fmt.toLatin1();
-		if(!image.save(QString(settings.out),ba.data(),-1)){
-			emit out.error("could'nt save image");
-			fail();
-		} 
-	}else{
-		// save to stdout
-		// TODO: print to stdout
+	if (settings.out != "-" ) {
+		file.setFileName(settings.out);
+		openOk = file.open(QIODevice::WriteOnly);
+	} else {
+		openOk = file.open(stdout, QIODevice::WriteOnly);
+	}
+	if (!openOk) {
+		emit out.error("Could not write to output file");
+		fail();
 	}
 
+	// if fmt is empty try to get it from file extension in out
+	if(settings.fmt==""){
+		if (settings.out == "-")
+			settings.fmt = "jpg";
+		else {
+			QFileInfo fi(settings.out);
+			settings.fmt = fi.suffix();
+		}
+	}
+	QByteArray fmt=settings.fmt.toLatin1();
+	if (!image.save(&file,fmt.data(),-1)) {
+		emit out.error("Could not save image");
+		fail();
+	} 
+
+	loadProgress(100);
+
 	currentPhase = 2;
 	emit out.phaseChanged();
 	convertionDone = true;

+ 22 - 6
src/image/settings.cc

@@ -14,11 +14,27 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
-#include <settings.hh>
+#include "settings.hh"
+
+namespace wkhtmltopdf {
+namespace settings {
+
+CropSettings:: CropSettings():
+	left(0),
+	top(0),
+	width(-1),
+	height(-1) {}
+
+ScaleSettings::ScaleSettings():
+	width(-1),
+	height(-1) {}
 
 Global::Global():
-  quiet(false),
-  useGraphics(false),
-  in(""),
-  out(""),
-  fmt("") {}
+	quiet(false),
+	useGraphics(false),
+	in(""),
+	out(""),
+	fmt("") {}
+
+}
+}

+ 4 - 0
src/image/settings.hh

@@ -26,6 +26,7 @@ namespace settings {
 
 /*! \brief Settings for cropping image */
 struct CropSettings {
+	CropSettings();
 	//! Cropping left/x coord
 	int left;
 	//! Cropping top/y coord
@@ -38,6 +39,7 @@ struct CropSettings {
 
 /*! \brief Settings for scaling image */
 struct ScaleSettings {
+	ScaleSettings();
 	//! Scale width/w dime
 	int width;
 	//! Scale height/h dime
@@ -51,6 +53,8 @@ struct ScaleSettings {
     \sa CommandLineParser::parse()
 */
 struct Global {
+	Global();
+
 	//! Crop related settings
 	CropSettings crop;
 	//! Scale related settings

+ 13 - 20
src/image/wkhtmltoimage.cc

@@ -17,6 +17,7 @@
 #include "settings.hh"
 #include "utilities.hh"
 #include "imageconverter.hh"
+#include "progressfeedback.hh"
 #include <QApplication>
 
 int main(int argc, char** argv) {
@@ -24,32 +25,24 @@ int main(int argc, char** argv) {
 	wkhtmltopdf::settings::Global settings;
 	//Create a command line parser to parse commandline arguments
 	CommandLineParser parser(settings);
-	//Setup default values in settings
-	//parser.loadDefaults();
 	//Parse the arguments
 	parser.parseArguments(argc, (const char**)argv);
 
-	//Construct QApplication required for printing
-	QApplication a( argc, argv );
+
+	bool use_graphics=true;
+#if defined(Q_WS_X11) || defined(Q_WS_MACX)
+#ifdef __EXTENSIVE_WKHTMLTOPDF_QT_HACK__
+	use_graphics=settings.useGraphics;
+	if (!use_graphics) QApplication::setGraphicsSystem("raster");
+#endif
+#endif
+	QApplication a(argc, argv, use_graphics);
 	a.setStyle(new MyLooksStyle());
 
 	//Create the actual page converter to convert the pages
 	wkhtmltopdf::ImageConverter converter(settings);
 
-	if (!converter.convert()) return EXIT_FAILURE;
-
-	switch(converter.httpErrorCode()) {
-
-		case 401:
-			return 3;
-
-		case 404:
-			return 2;
-
-		case 0:
-			return EXIT_SUCCESS;
-
-		default:
-			return EXIT_FAILURE;
-	}
+	wkhtmltopdf::ProgressFeedback feedback(settings.quiet, converter);
+	bool success = converter.convert();
+	return handleError(success, converter.httpErrorCode());
 }

+ 0 - 1
src/shared/commandlineparserbase.cc

@@ -125,7 +125,6 @@ void CommandLineParserBase::parseArg(int sections, const int argc, const char **
 				usage(stderr, false);
 				exit(1);
 			}
-			qDebug() << k.value()->section << sections;
 		
 			if (!(k.value()->section & sections)) {
 				fprintf(stderr, "-%c specified in incorrect location\n\n", argv[c][j]);