summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorportix <none@none>2012-11-20 12:35:32 +0100
committerportix <none@none>2012-11-20 12:35:32 +0100
commit9cf59535dc1e9cc37237a7ddfefd164a444a2470 (patch)
tree6dfb6425e65d264702bc9009c552054ace57d7fe
parentbd66e5045613123ee067f829185e3c15353470d3 (diff)
downloaddwb-9cf59535dc1e9cc37237a7ddfefd164a444a2470.zip
Implementing wv.toPng; set base-uri to HOME in sp_load
-rw-r--r--api/dwb-js.740
-rw-r--r--api/jsapi.7.txt17
-rw-r--r--api/jsapi.txt25
-rw-r--r--src/scratchpad.c7
-rw-r--r--src/scripts.c86
5 files changed, 171 insertions, 4 deletions
diff --git a/api/dwb-js.7 b/api/dwb-js.7
index aed9715e..0994d865 100644
--- a/api/dwb-js.7
+++ b/api/dwb-js.7
@@ -2,12 +2,12 @@
.\" Title: dwb-js
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.77.1 <http://docbook.sf.net/>
-.\" Date: 11/19/2012
+.\" Date: 11/20/2012
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
-.TH "DWB\-JS" "7" "11/19/2012" "\ \&" "\ \&"
+.TH "DWB\-JS" "7" "11/20/2012" "\ \&" "\ \&"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
@@ -1296,6 +1296,42 @@ true if the uri is loaded
.nr an-break-flag 1
.br
.ps +1
+\fBNumber wv.toPng(String filename, [Number width, Number height], [Boolean keepAspect])\fR
+.RS 4
+.sp
+Renders a webview to a png file\&.
+.PP
+\fIfilename\fR
+.RS 4
+The filename for the png\&.
+.RE
+.PP
+\fIwidth\fR
+.RS 4
+The width of the png, if width is < 0 and height is > 0 the image has the same aspect ratio as the original webview, optional\&.
+.RE
+.PP
+\fIheight\fR
+.RS 4
+The height of the png, if height is < 0 and width is > 0 the image will have the same aspect ratio as the original webview, optional, mandatory if width is set\&.
+.RE
+.PP
+\fIkeepAspect\fR
+.RS 4
+Whether to keep the aspect ratio, if set to true the new image will have the same aspect ratio as the original webview, width and height are taken as maximum sizes and must both be > 0, optional\&.
+.RE
+.PP
+\fIreturns\fR
+.RS 4
+A cairo_status_t (0 on success) or \-1 if an error occured\&.
+.RE
+.RE
+.sp
+.it 1 an-trap
+.nr an-no-space-flag 1
+.nr an-break-flag 1
+.br
+.ps +1
\fBvoid wv.reload(void)\fR
.RS 4
.sp
diff --git a/api/jsapi.7.txt b/api/jsapi.7.txt
index 0bfa830f..10330aef 100644
--- a/api/jsapi.7.txt
+++ b/api/jsapi.7.txt
@@ -598,6 +598,23 @@ changes, return true to stop the emission, optional
_returns_;; true if the uri is loaded
****
+==== Number wv.toPng(String filename, [Number width, Number height], [Boolean keepAspect])
+****
+
+Renders a webview to a png file.
+
+_filename_;; The filename for the png.
+_width_;; The width of the png, if width is < 0 and height is > 0 the image has
+the same aspect ratio as the original webview, optional.
+_height_;; The height of the png, if height is < 0 and width is > 0 the image
+will have the same aspect ratio as the original webview, optional, mandatory
+if width is set.
+_keepAspect_;; Whether to keep the aspect ratio, if set to true the new
+image will have the same aspect ratio as the original webview, width and
+height are taken as maximum sizes and must both be > 0, optional.
+_returns_;; A cairo_status_t (0 on success) or -1 if an error occured.
+****
+
==== void wv.reload(void)
****
diff --git a/api/jsapi.txt b/api/jsapi.txt
index 67007c5f..7ea7f182 100644
--- a/api/jsapi.txt
+++ b/api/jsapi.txt
@@ -1175,6 +1175,31 @@ void wv.reload(void)
Reload a webview
****
+****
+[float]
+==== *toPng()* ====
+
+[source,javascript]
+----
+Number wv.toPng(String filename, [Number width, Number height], [Boolean keepAspect])
+----
+
+Renders a webview to a png file.
+
+_filename_;; The filename for the png.
+_width_;; The width of the png, if width is < 0 and height is > 0 the image has
+the same aspect ratio as the original webview, optional.
+_height_;; The height of the png, if height is < 0 and width is > 0 the image
+will have the same aspect ratio as the original webview, optional, mandatory
+if width is set.
+_keepAspect_;; Whether to keep the ascpect ratio, if the set to true the new
+image will have the same aspect ratio as the original webview, width and
+height are taken as maximum sizes and must both be > 0, optional.
+_returns_;; A cairo_status_t (0 on success) or -1 if an error occured.
+****
+
+
+
====
[float]
diff --git a/src/scratchpad.c b/src/scratchpad.c
index 00fc0974..5e1308d6 100644
--- a/src/scratchpad.c
+++ b/src/scratchpad.c
@@ -63,8 +63,11 @@ void
scratchpad_load(const char *text) {
if (g_str_has_prefix(text, "file://"))
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(g_scratchpad), text);
- else
- webkit_web_view_load_string(WEBKIT_WEB_VIEW(g_scratchpad), text, NULL, NULL, "scratchpad");
+ else {
+ char *basepath = g_strconcat("file://", g_getenv("HOME"), NULL);
+ webkit_web_view_load_string(WEBKIT_WEB_VIEW(g_scratchpad), text, NULL, NULL, basepath);
+ g_free(basepath);
+ }
}
void
diff --git a/src/scripts.c b/src/scripts.c
index 80894377..5d0aa418 100644
--- a/src/scripts.c
+++ b/src/scripts.c
@@ -93,6 +93,9 @@ static JSValueRef wv_set_title(JSContextRef ctx, JSObjectRef function, JSObjectR
static JSValueRef wv_history(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc);
static JSValueRef wv_reload(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc);
static JSValueRef wv_inject(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc);
+#if WEBKIT_CHECK_VERSION(1, 10, 0)
+static JSValueRef wv_to_png(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc);
+#endif
static JSStaticFunction default_functions[] = {
{ "connect", connect_object, kJSDefaultAttributes },
{ "disconnect", disconnect_object, kJSDefaultAttributes },
@@ -104,6 +107,9 @@ static JSStaticFunction wv_functions[] = {
{ "history", wv_history, kJSDefaultAttributes },
{ "reload", wv_reload, kJSDefaultAttributes },
{ "inject", wv_inject, kJSDefaultAttributes },
+#if WEBKIT_CHECK_VERSION(1, 10, 0)
+ { "toPng", wv_to_png, kJSDefaultAttributes },
+#endif
{ 0, 0, 0 },
};
static JSValueRef wv_get_main_frame(JSContextRef ctx, JSObjectRef object, JSStringRef js_name, JSValueRef* exception);
@@ -420,6 +426,86 @@ wv_inject(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc,
}
return JSValueMakeNull(ctx);
}/*}}}*/
+#if WEBKIT_CHECK_VERSION(1, 10, 0)
+static JSValueRef
+wv_to_png(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) {
+ WebKitWebView *wv;
+ cairo_status_t status = -1;
+ cairo_surface_t *sf;
+ char *filename;
+ if (argc < 1 || (wv = JSObjectGetPrivate(this)) == NULL || (JSValueIsNull(ctx, argv[0])) || (filename = js_value_to_char(ctx, argv[0], -1, NULL)) == NULL) {
+ return JSValueMakeNumber(ctx, status);
+ }
+ if (argc > 2) {
+ gboolean keep_aspect = false;
+ double width = JSValueToNumber(ctx, argv[1], exc);
+ double height = JSValueToNumber(ctx, argv[2], exc);
+ if (width != NAN && height != NAN) {
+ sf = webkit_web_view_get_snapshot(wv);
+
+ if (argc > 3 && JSValueIsBoolean(ctx, argv[3]))
+ keep_aspect = JSValueToBoolean(ctx, argv[3]);
+ if (keep_aspect && (width <= 0 || height <= 0))
+ return JSValueMakeNumber(ctx, status);
+
+
+ int w = cairo_image_surface_get_width(sf);
+ int h = cairo_image_surface_get_height(sf);
+
+ double aspect = (double)w/h;
+ double new_width = width;
+ double new_height = height;
+
+ if (width <= 0 || keep_aspect)
+ new_width = height * aspect;
+ if ((width > 0 && height <= 0) || keep_aspect)
+ new_height = width / aspect;
+ if (keep_aspect) {
+ if (new_width > width) {
+ new_width = width;
+ new_height = new_width / aspect;
+ }
+ else if (new_height > height) {
+ new_height = height;
+ new_width = new_height * aspect;
+ }
+ }
+
+ double sw, sh;
+ if (width <= 0 || height <= 0)
+ sw = sh = MIN(width / w, height / h);
+ else {
+ sw = width / w;
+ sh = height / h;
+ }
+
+ cairo_surface_t *scaled_surface = cairo_surface_create_similar_image(sf, CAIRO_FORMAT_RGB24, new_width, new_height);
+ cairo_t *cr = cairo_create(scaled_surface);
+
+ cairo_save(cr);
+ cairo_scale(cr, sw, sh);
+
+ cairo_set_source_surface(cr, sf, 0, 0);
+ cairo_paint(cr);
+ cairo_restore(cr);
+
+ cairo_destroy(cr);
+
+ status = cairo_surface_write_to_png(scaled_surface, filename);
+ cairo_surface_destroy(scaled_surface);
+ }
+ else
+ return JSValueMakeNumber(ctx, status);
+ }
+ else {
+ sf = webkit_web_view_get_snapshot(wv);
+ status = cairo_surface_write_to_png(sf, filename);
+ }
+ cairo_surface_destroy(sf);
+
+ return JSValueMakeNumber(ctx, status);
+}
+#endif
/* wv_get_main_frame {{{*/
static JSValueRef
wv_get_main_frame(JSContextRef ctx, JSObjectRef object, JSStringRef js_name, JSValueRef* exception) {