1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Webpage plug-in.
* Copyright (C) 2011 Mukund Sivaraman <muks@banu.com>.
* Portions are copyright of the author of the
* file-open-location-dialog.c code.
*/
#include "config.h"
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <webkit2/webkit2.h>
#include "libgimp/stdplugins-intl.h"
#define PLUG_IN_PROC "plug-in-web-page"
#define PLUG_IN_BINARY "web-page"
#define PLUG_IN_ROLE "gimp-web-page"
#define MAX_URL_LEN 2048
typedef struct
{
GimpImage *image;
GError *error;
} WebpageResult;
typedef struct _Webpage Webpage;
typedef struct _WebpageClass WebpageClass;
struct _Webpage
{
GimpPlugIn parent_instance;
};
struct _WebpageClass
{
GimpPlugInClass parent_class;
};
#define WEBPAGE_TYPE (webpage_get_type ())
#define WEBPAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), WEBPAGE_TYPE, Webpage))
GType webpage_get_type (void) G_GNUC_CONST;
static GList * webpage_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * webpage_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * webpage_run (GimpProcedure *procedure,
GimpProcedureConfig *config,
gpointer run_data);
static gboolean webpage_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config);
static GimpImage * webpage_capture (GimpProcedureConfig *config,
GError **error);
G_DEFINE_TYPE (Webpage, webpage, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (WEBPAGE_TYPE)
DEFINE_STD_SET_I18N
static void
webpage_class_init (WebpageClass *klass)
{
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = webpage_query_procedures;
plug_in_class->create_procedure = webpage_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
webpage_init (Webpage *webpage)
{
}
static GList *
webpage_query_procedures (GimpPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (PLUG_IN_PROC));
}
static GimpProcedure *
webpage_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
if (! strcmp (name, PLUG_IN_PROC))
{
procedure = gimp_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN,
webpage_run, NULL, NULL);
gimp_procedure_set_menu_label (procedure, _("From _Webpage..."));
gimp_procedure_add_menu_path (procedure, "<Image>/File/Create");
gimp_procedure_set_documentation (procedure,
_("Create an image of a webpage"),
"The plug-in allows you to take a "
"screenshot of a webpage.",
name);
gimp_procedure_set_attribution (procedure,
"Mukund Sivaraman <muks@banu.com>",
"2011",
"2011");
gimp_procedure_add_enum_argument (procedure, "run-mode",
"Run mode",
"The run mode",
GIMP_TYPE_RUN_MODE,
GIMP_RUN_INTERACTIVE,
G_PARAM_READWRITE);
gimp_procedure_add_string_argument (procedure, "url",
_("Enter location (_URI)"),
_("URL of the webpage to screenshot"),
"https://www.gimp.org/",
G_PARAM_READWRITE);
gimp_procedure_add_int_argument (procedure, "width",
_("_Width (pixels)"),
_("The width of the screenshot (in pixels)"),
100, GIMP_MAX_IMAGE_SIZE, 1024,
G_PARAM_READWRITE);
gimp_procedure_add_int_argument (procedure, "font-size",
_("_Font size"),
_("The font size to use in the page (in pt)"),
1, 1000, 12,
G_PARAM_READWRITE);
gimp_procedure_add_image_return_value (procedure, "image",
"Image",
"The output image",
FALSE,
G_PARAM_READWRITE);
}
return procedure;
}
static GimpValueArray *
webpage_run (GimpProcedure *procedure,
GimpProcedureConfig *config,
gpointer run_data)
{
GimpValueArray *return_vals;
GimpRunMode run_mode;
GimpImage *image;
GError *error = NULL;
g_object_get (config, "run-mode", &run_mode, NULL);
if (run_mode == GIMP_RUN_INTERACTIVE &&
! webpage_dialog (procedure, config))
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CANCEL,
NULL);
image = webpage_capture (config, &error);
if (! image)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
if (run_mode == GIMP_RUN_INTERACTIVE)
gimp_display_new (image);
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static gboolean
webpage_dialog (GimpProcedure *procedure,
GimpProcedureConfig *config)
{
GtkWidget *dialog;
GtkListStore *store;
gboolean run;
gimp_ui_init (PLUG_IN_BINARY);
dialog = gimp_procedure_dialog_new (procedure,
GIMP_PROCEDURE_CONFIG (config),
_("Create from webpage"));
gimp_procedure_dialog_set_ok_label (GIMP_PROCEDURE_DIALOG (dialog),
_("Cre_ate"));
store = gimp_int_store_new (_("Huge"), 16,
_("Large"), 14,
C_("web-page", "Default"), 12,
_("Small"), 10,
_("Tiny"), 8,
NULL);
gimp_procedure_dialog_get_int_combo (GIMP_PROCEDURE_DIALOG (dialog),
"font-size", GIMP_INT_STORE (store));
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog),
"url", "width", "font-size", NULL);
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
gtk_widget_destroy (dialog);
return run;
}
static void
notify_progress_cb (WebKitWebView *view,
GParamSpec *pspec,
gpointer user_data)
{
static gdouble old_progress = 0.0;
gdouble progress;
progress = webkit_web_view_get_estimated_load_progress (view);
if ((progress - old_progress) > 0.01)
{
gimp_progress_update (progress);
old_progress = progress;
}
}
static gboolean
load_failed_cb (WebKitWebView *view,
WebKitLoadEvent event,
gchar *uri,
gpointer web_error,
gpointer user_data)
{
GError **error = user_data;
*error = g_error_copy ((GError *) web_error);
gtk_main_quit ();
return TRUE;
}
static void
snapshot_ready (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
WebpageResult *retval = user_data;
WebKitWebView *view = WEBKIT_WEB_VIEW (source_object);
cairo_surface_t *surface;
gimp_progress_pulse ();
surface = webkit_web_view_get_snapshot_finish (view, result,
&retval->error);
gimp_progress_pulse ();
if (surface)
{
gint width;
gint height;
GimpLayer *layer;
width = cairo_image_surface_get_width (surface);
height = cairo_image_surface_get_height (surface);
retval->image = gimp_image_new (width, height, GIMP_RGB);
gimp_image_undo_disable (retval->image);
layer = gimp_layer_new_from_surface (retval->image, _("Webpage"),
surface,
0.25, 1.0);
gimp_image_insert_layer (retval->image, layer, NULL, 0);
gimp_image_undo_enable (retval->image);
cairo_surface_destroy (surface);
}
gtk_main_quit ();
}
static void
load_changed_cb (WebKitWebView *view,
WebKitLoadEvent event,
gpointer user_data)
{
if (event == WEBKIT_LOAD_FINISHED)
gtk_main_quit ();
}
static GimpImage *
webpage_capture (GimpProcedureConfig *config,
GError **error)
{
gchar *scheme;
GtkWidget *window;
GtkWidget *view;
WebKitSettings *settings;
char *ua;
gchar *url;
gint width;
gint font_size;
WebpageResult result;
g_object_get (config,
"url", &url,
"width", &width,
"font-size", &font_size,
NULL);
if (! url || strlen (url) == 0)
{
g_set_error (error, 0, 0, _("No URL was specified"));
return NULL;
}
scheme = g_uri_parse_scheme (url);
if (! scheme)
{
gchar *scheme_url;
/* If we were not given a well-formed URL, make one. */
scheme_url = g_strconcat ("https://", url, NULL);
g_free (url);
url = scheme_url;
}
g_free (scheme);
if (width < 32)
{
g_warning ("Width '%d' is too small. Clamped to 32.", width);
width = 32;
}
else if (width > 8192)
{
g_warning ("Width '%d' is too large. Clamped to 8192.", width);
width = 8192;
}
window = gtk_offscreen_window_new ();
gtk_widget_show (window);
view = webkit_web_view_new ();
gtk_widget_show (view);
gtk_widget_set_vexpand (view, TRUE);
gtk_widget_set_size_request (view, width, -1);
gtk_container_add (GTK_CONTAINER (window), view);
/* Append "GIMP/<GIMP_VERSION>" to the user agent string */
settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view));
ua = g_strdup_printf ("%s GIMP/%s",
webkit_settings_get_user_agent (settings),
GIMP_VERSION);
webkit_settings_set_user_agent (settings, ua);
g_free (ua);
/* Set font size */
webkit_settings_set_default_font_size (settings, font_size);
g_signal_connect (view, "notify::estimated-load-progress",
G_CALLBACK (notify_progress_cb),
window);
g_signal_connect (view, "load-failed",
G_CALLBACK (load_failed_cb),
error);
g_signal_connect (view, "load-changed",
G_CALLBACK (load_changed_cb),
window);
gimp_progress_init_printf (_("Downloading webpage '%s'"), url);
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (view), url);
gtk_main ();
result.error = NULL;
result.image = NULL;
if (*error == NULL)
{
gimp_progress_init_printf (_("Transferring webpage image for '%s'"), url);
gimp_progress_pulse ();
webkit_web_view_get_snapshot (WEBKIT_WEB_VIEW (view),
WEBKIT_SNAPSHOT_REGION_FULL_DOCUMENT,
WEBKIT_SNAPSHOT_OPTIONS_NONE,
NULL,
snapshot_ready,
&result);
gtk_main ();
if (result.error != NULL)
g_propagate_error (error, result.error);
}
gtk_widget_destroy (window);
gimp_progress_update (1.0);
return result.image;
}
|