r5452 joty - in /trunk/netsurf: desktop/save_pdf/pdf_plotters.c desktop/save_pdf/pdf_plotters.h gtk/gtk_gui.c

netsurf at semichrome.net netsurf at semichrome.net
Sun Sep 28 14:40:06 BST 2008


Author: joty
Date: Sun Sep 28 08:40:06 2008
New Revision: 5452

URL: http://source.netsurf-browser.org?rev=5452&view=rev
Log:
* desktop/save_pdf/pdf_plotters.c:
  - save_pdf(): constify path parameter and routine no longer frees it.
    Follow latter change in pdf_end(), nsgtk_PDF_set_pass() and nsgtk_PDF_no_pass()
  - pdf_printer: constify
  - last_clip_x0, last_clip_y0, last_clip_x1, last_clip_y1, in_text_mode, text_mode_request: make static
  - pdf_doc: free previous PDF document if previous save attempt failed.
  - set PDF Creator entry based on our user_agent_string().
* other minor changes.

Modified:
    trunk/netsurf/desktop/save_pdf/pdf_plotters.c
    trunk/netsurf/desktop/save_pdf/pdf_plotters.h
    trunk/netsurf/gtk/gtk_gui.c

Modified: trunk/netsurf/desktop/save_pdf/pdf_plotters.c
URL: http://source.netsurf-browser.org/trunk/netsurf/desktop/save_pdf/pdf_plotters.c?rev=5452&r1=5451&r2=5452&view=diff
==============================================================================
--- trunk/netsurf/desktop/save_pdf/pdf_plotters.c (original)
+++ trunk/netsurf/desktop/save_pdf/pdf_plotters.c Sun Sep 28 08:40:06 2008
@@ -32,9 +32,10 @@
 #include "desktop/print.h"
 #include "desktop/printer.h"
 #include "desktop/save_pdf/pdf_plotters.h"
+#include "image/bitmap.h"
 #include "utils/log.h"
 #include "utils/utils.h"
-#include "image/bitmap.h"
+#include "utils/useragent.h"
 
 #include "font_haru.h"
 
@@ -91,9 +92,9 @@
 
 /*Remeber if pdf_plot_clip was invoked for current page*/
 static bool page_clipped;
-int last_clip_x0, last_clip_y0, last_clip_x1, last_clip_y1;
-
-bool in_text_mode, text_mode_request;
+static int last_clip_x0, last_clip_y0, last_clip_x1, last_clip_y1;
+
+static bool in_text_mode, text_mode_request;
 
 static struct print_settings* settings;
 
@@ -116,7 +117,7 @@
 	false
 };
 
-struct printer pdf_printer= {
+const struct printer pdf_printer = {
 	&pdf_plotters,
 	pdf_begin,
 	pdf_next_page,
@@ -409,8 +410,9 @@
 	apply_clip_and_mode();
 
 	image = pdf_extract_image(bitmap, content);
-
-	if (image) {
+	if (!image)
+		return false;
+	else {
 		/*The position of the next tile*/
 		HPDF_REAL current_x, current_y ;
 		HPDF_REAL max_width, max_height;
@@ -425,21 +427,14 @@
 						current_x + x,
       						page_height-current_y - y - height,
       						width, height);
-
-		return true;
-	}
-	else
-		return false;
+	}
 
 	return true;
 }
 
 HPDF_Image pdf_extract_image(struct bitmap *bitmap, struct content *content)
 {
-	HPDF_Image image = NULL,smask;
-	char *img_buffer, *rgb_buffer, *alpha_buffer;
-	int img_width, img_height, img_rowstride;
-	int i, j;
+	HPDF_Image image = NULL;
 
 	if (content) {
 		/*Not sure if I don't have to check if downloading has been
@@ -450,7 +445,7 @@
 			/*Handle "embeddable" types of images*/
 			case CONTENT_JPEG:
  				image = HPDF_LoadJpegImageFromMem(pdf_doc,
- 						content->source_data,
+ 						(const HPDF_BYTE *)content->source_data,
  						content->source_size);
  				break;
 
@@ -458,7 +453,7 @@
 
 			case CONTENT_PNG:
 				image = HPDF_LoadPngImageFromMem(pdf_doc,
-						content->source_data,
+						(const HPDF_BYTE *)content->source_data,
 						content->total_size);
 				break;*/
 			default:
@@ -467,28 +462,28 @@
 	}
 
 	if (!image) {
+		HPDF_Image smask;
+		unsigned char *img_buffer, *rgb_buffer, *alpha_buffer;
+		int img_width, img_height, img_rowstride;
+		int i, j;
+
 		/*Handle pixmaps*/
 		img_buffer = bitmap_get_buffer(bitmap);
 		img_width = bitmap_get_width(bitmap);
 		img_height = bitmap_get_height(bitmap);
 		img_rowstride = bitmap_get_rowstride(bitmap);
 
-		rgb_buffer = (char*)malloc(3 * img_width * img_height);
-		if (rgb_buffer == NULL) {
+		rgb_buffer = (unsigned char *)malloc(3 * img_width * img_height);
+		alpha_buffer = (unsigned char *)malloc(img_width * img_height);
+		if (rgb_buffer == NULL || alpha_buffer == NULL) {
 			LOG(("Not enough memory to create RGB buffer"));
+			free(rgb_buffer);
+			free(alpha_buffer);
 			return NULL;
 		}
 
-		alpha_buffer = (char*)malloc(img_width * img_height);
-		if (alpha_buffer == NULL) {
-			LOG(("Not enough memory to create alpha buffer"));
-			free(rgb_buffer);
-			return NULL;
-		}
-
-
-		for (i = 0; i<img_height; i++)
-			for (j = 0 ; j<img_width ; j++) {
+		for (i = 0; i < img_height; i++)
+			for (j = 0; j < img_width; j++) {
 				rgb_buffer[((i * img_width) + j) * 3] =
 				  img_buffer[(i * img_rowstride) + (j * 4)];
 
@@ -502,23 +497,24 @@
 				  img_buffer[(i * img_rowstride) + (j * 4) + 3];
 			}
 
-			smask = HPDF_LoadRawImageFromMem(pdf_doc, alpha_buffer,
-							img_width, img_height,
-     							HPDF_CS_DEVICE_GRAY, 8);
-
-			image = HPDF_LoadRawImageFromMem(pdf_doc, rgb_buffer,
-							img_width, img_height,
-     							HPDF_CS_DEVICE_RGB, 8);
-
-			if (HPDF_Image_AddSMask(pdf_doc, image,smask) != HPDF_OK)
-				image = NULL;
-
-			free(rgb_buffer);
-			free(alpha_buffer);
+		smask = HPDF_LoadRawImageFromMem(pdf_doc, alpha_buffer,
+				img_width, img_height,
+     				HPDF_CS_DEVICE_GRAY, 8);
+
+		image = HPDF_LoadRawImageFromMem(pdf_doc, rgb_buffer,
+				img_width, img_height,
+     				HPDF_CS_DEVICE_RGB, 8);
+
+		if (HPDF_Image_AddSMask(pdf_doc, image, smask) != HPDF_OK)
+			image = NULL;
+
+		free(rgb_buffer);
+		free(alpha_buffer);
 	}
 
 	return image;
 }
+
 /**change the mode and clip only if it's necessary*/
 static void apply_clip_and_mode()
 {
@@ -667,8 +663,8 @@
  */
 bool pdf_begin(struct print_settings *print_settings)
 {
-	pdf_doc = NULL;
-
+	if (pdf_doc != NULL)
+		HPDF_Free(pdf_doc);
 	pdf_doc = HPDF_New(error_handler, NULL);
 	if (!pdf_doc) {
 		LOG(("Error creating pdf_doc"));
@@ -685,9 +681,9 @@
 
 	if (option_enable_PDF_compression)
 		HPDF_SetCompressionMode(pdf_doc, HPDF_COMP_ALL); /*Compression on*/
-
-	pdf_font = HPDF_GetFont (pdf_doc, "Times-Roman", "StandardEncoding");
-
+	HPDF_SetInfoAttr(pdf_doc, HPDF_INFO_CREATOR, user_agent_string());
+
+	pdf_font = NULL;
 	pdf_page = NULL;
 
 #ifdef PDF_DEBUG
@@ -733,7 +729,6 @@
 
 void pdf_end(void)
 {
-	char *path;
 #ifdef PDF_DEBUG
 	LOG(("pdf_end begins"));
 	if (pdf_page != NULL) {
@@ -745,22 +740,19 @@
 	}
 #endif
 
-	if (settings->output != NULL)
-		path = strdup(settings->output);
-	else
-		path = NULL;
-
 	/*Encryption on*/
 	if (option_enable_PDF_password)
-		PDF_Password(&owner_pass, &user_pass, path);
+		PDF_Password(&owner_pass, &user_pass,
+				(void *)settings->output);
 	else
-		save_pdf(path);
+		save_pdf(settings->output);
 #ifdef PDF_DEBUG
 	LOG(("pdf_end finishes"));
 #endif
 }
+
 /** saves the pdf optionally encrypting it before*/
-void save_pdf(char *path)
+void save_pdf(const char *path)
 {
 	bool success = false;
 
@@ -776,14 +768,13 @@
 			remove(path);
 		else
 			success = true;
-
-		free(path);
 	}
 
 	if (!success)
 		warn_user("Unable to save PDF file.", 0);
 
 	HPDF_Free(pdf_doc);
+	pdf_doc = NULL;
 }
 
 

Modified: trunk/netsurf/desktop/save_pdf/pdf_plotters.h
URL: http://source.netsurf-browser.org/trunk/netsurf/desktop/save_pdf/pdf_plotters.h?rev=5452&r1=5451&r2=5452&view=diff
==============================================================================
--- trunk/netsurf/desktop/save_pdf/pdf_plotters.h (original)
+++ trunk/netsurf/desktop/save_pdf/pdf_plotters.h Sun Sep 28 08:40:06 2008
@@ -24,12 +24,11 @@
 #define NETSURF_PDF_PLOTTERS_H
 
 #include "desktop/print.h"
-struct plotter_table;
 
-extern struct printer pdf_printer;
+extern const struct printer pdf_printer;
 
 /**Start plotting a pdf file*/
-bool pdf_begin(struct print_settings* settings);
+bool pdf_begin(struct print_settings *settings);
 
 /**Finish the current page and start a new one*/
 bool pdf_next_page(void);
@@ -39,6 +38,6 @@
 
 void pdf_set_scale(float s);
 
-void save_pdf(char *path);
+void save_pdf(const char *path);
 
 #endif /*NETSURF_PDF_PLOTTERS_H*/

Modified: trunk/netsurf/gtk/gtk_gui.c
URL: http://source.netsurf-browser.org/trunk/netsurf/gtk/gtk_gui.c?rev=5452&r1=5451&r2=5452&view=diff
==============================================================================
--- trunk/netsurf/gtk/gtk_gui.c (original)
+++ trunk/netsurf/gtk/gtk_gui.c Sun Sep 28 08:40:06 2008
@@ -705,6 +705,7 @@
 		g_object_unref(G_OBJECT(x));
 		
 		save_pdf(path);
+		free(path);
 	}
 	else {
 		gtk_label_set_text(GTK_LABEL(glade_xml_get_widget(x,
@@ -729,6 +730,7 @@
 	g_object_unref(G_OBJECT(x));
 	
 	save_pdf(path);
+	free(path);
 }
 #endif
 




More information about the netsurf-commits mailing list