file-pdf-load.c 54.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2
 *
3
 * file-pdf-load.c - PDF file loader
4
 *
5 6
 * Copyright (C) 2005 Nathan Summers
 *
7 8
 * Some code in render_page_to_surface() borrowed from
 * poppler.git/glib/poppler-page.cc.
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13 14 15 16 17 18 19 20
 * (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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 23 24 25 26 27 28 29 30
 */

#include "config.h"

#include <string.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

31
#undef GTK_DISABLE_SINGLE_INCLUDES
32
#include <poppler.h>
33
#define GTK_DISABLE_SINGLE_INCLUDES
34 35 36

#include "libgimp/stdplugins-intl.h"

37

38 39
#define LOAD_PROC       "file-pdf-load"
#define LOAD_THUMB_PROC "file-pdf-load-thumb"
40
#define PLUG_IN_BINARY  "file-pdf-load"
41
#define PLUG_IN_ROLE    "gimp-file-pdf-load"
42

43 44
#define THUMBNAIL_SIZE  128

45

46 47 48
/* Structs for the load dialog */
typedef struct
{
49 50
  GimpPageSelectorTarget target;
  gdouble                resolution;
51
  gboolean               antialias;
52 53 54 55
} PdfLoadVals;

static PdfLoadVals loadvals =
{
56
  GIMP_PAGE_SELECTOR_TARGET_LAYERS,
57 58
  100.00,  /* 100 dpi   */
  TRUE
59 60 61 62 63 64 65 66 67 68
};

typedef struct
{
  gint  n_pages;
  gint *pages;
} PdfSelectedPages;

/* Declare local functions */
static void              query             (void);
69 70 71 72 73 74 75 76 77 78 79
static void              run               (const gchar            *name,
                                            gint                    nparams,
                                            const GimpParam        *param,
                                            gint                   *nreturn_vals,
                                            GimpParam             **return_vals);

static gint32            load_image        (PopplerDocument        *doc,
                                            const gchar            *filename,
                                            GimpRunMode             run_mode,
                                            GimpPageSelectorTarget  target,
                                            guint32                 resolution,
80
                                            gboolean                antialias,
81 82
                                            PdfSelectedPages       *pages);

83
static GimpPDBStatusType load_dialog       (PopplerDocument        *doc,
84 85
                                            PdfSelectedPages       *pages);

86 87
static PopplerDocument * open_document     (const gchar            *filename,
                                            GError                **error);
88

89
static cairo_surface_t * get_thumb_surface (PopplerDocument        *doc,
90 91 92
                                            gint                    page,
                                            gint                    preferred_size);

93 94 95 96 97 98 99 100 101 102
static GdkPixbuf *       get_thumb_pixbuf  (PopplerDocument        *doc,
                                            gint                    page,
                                            gint                    preferred_size);

static gint32            layer_from_surface (gint32                  image,
					     const gchar            *layer_name,
					     gint                    position,
					     cairo_surface_t        *surface,
					     gdouble                 progress_start,
					     gdouble                 progress_scale);
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/**
 ** the following was formerly part of
 ** gimpresolutionentry.h and gimpresolutionentry.c,
 ** moved here because this is the only thing that uses
 ** it, and it is undesirable to maintain all that api.
 ** Most unused functions have been removed.
 **/
#define GIMP_TYPE_RESOLUTION_ENTRY            (gimp_resolution_entry_get_type ())
#define GIMP_RESOLUTION_ENTRY(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntry))
#define GIMP_RESOLUTION_ENTRY_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntryClass))
#define GIMP_IS_RESOLUTION_ENTRY(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GIMP_TYPE_RESOLUTION_ENTRY))
#define GIMP_IS_RESOLUTION_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_RESOLUTION_ENTRY))
#define GIMP_RESOLUTION_ENTRY_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_RESOLUTION_ENTRY, GimpResolutionEntryClass))


Sven Neumann's avatar
Sven Neumann committed
119
typedef struct _GimpResolutionEntry       GimpResolutionEntry;
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
typedef struct _GimpResolutionEntryClass  GimpResolutionEntryClass;

typedef struct _GimpResolutionEntryField  GimpResolutionEntryField;

struct _GimpResolutionEntryField
{
  GimpResolutionEntry      *gre;
  GimpResolutionEntryField *corresponding;

  gboolean       size;

  GtkWidget     *label;

  guint          changed_signal;

  GtkObject     *adjustment;
  GtkWidget     *spinbutton;

  gdouble        phy_size;

  gdouble        value;
  gdouble        min_value;
  gdouble        max_value;

  gint           stop_recursion;
};


struct _GimpResolutionEntry
{
150
  GtkTable                  parent_instance;
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

  GimpUnit                  size_unit;
  GimpUnit                  unit;

  GtkWidget                *unitmenu;
  GtkWidget                *chainbutton;

  GimpResolutionEntryField  width;
  GimpResolutionEntryField  height;
  GimpResolutionEntryField  x;
  GimpResolutionEntryField  y;

};

struct _GimpResolutionEntryClass
{
  GtkTableClass  parent_class;

  void (* value_changed)  (GimpResolutionEntry *gse);
  void (* refval_changed) (GimpResolutionEntry *gse);
  void (* unit_changed)   (GimpResolutionEntry *gse);
};


GType       gimp_resolution_entry_get_type (void) G_GNUC_CONST;

GtkWidget * gimp_resolution_entry_new          (const gchar   *width_label,
                                                gdouble        width,
                                                const gchar   *height_label,
                                                gdouble        height,
                                                GimpUnit       size_unit,
182 183 184
                                                const gchar   *res_label,
                                                gdouble        initial_res,
                                                GimpUnit       initial_unit);
185 186 187 188 189 190 191

GtkWidget * gimp_resolution_entry_attach_label (GimpResolutionEntry *gre,
                                                const gchar         *text,
                                                gint                 row,
                                                gint                 column,
                                                gfloat               alignment);

192
gdouble     gimp_resolution_entry_get_x_in_dpi (GimpResolutionEntry *gre);
193

194
gdouble     gimp_resolution_entry_get_y_in_dpi (GimpResolutionEntry *gre);
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


/* signal callback convenience functions */
void        gimp_resolution_entry_update_x_in_dpi (GimpResolutionEntry *gre,
                                                   gpointer             data);

void        gimp_resolution_entry_update_y_in_dpi (GimpResolutionEntry *gre,
                                                   gpointer             data);


enum
{
  WIDTH_CHANGED,
  HEIGHT_CHANGED,
  X_CHANGED,
  Y_CHANGED,
  UNIT_CHANGED,
  LAST_SIGNAL
};

static void   gimp_resolution_entry_class_init      (GimpResolutionEntryClass *class);
static void   gimp_resolution_entry_init            (GimpResolutionEntry      *gre);

static void   gimp_resolution_entry_update_value    (GimpResolutionEntryField *gref,
                                                     gdouble              value);
static void   gimp_resolution_entry_value_callback  (GtkWidget           *widget,
                                                     gpointer             data);
static void   gimp_resolution_entry_update_unit     (GimpResolutionEntry *gre,
                                                     GimpUnit             unit);
static void   gimp_resolution_entry_unit_callback   (GtkWidget           *widget,
                                                     GimpResolutionEntry *gre);

static void   gimp_resolution_entry_field_init (GimpResolutionEntry      *gre,
                                                GimpResolutionEntryField *gref,
                                                GimpResolutionEntryField *corresponding,
                                                guint                     changed_signal,
                                                gdouble                   initial_val,
                                                GimpUnit                  initial_unit,
                                                gboolean                  size,
                                                gint                      spinbutton_width);

236 237 238
static void   gimp_resolution_entry_format_label (GimpResolutionEntry *gre,
                                                  GtkWidget           *label,
                                                  gdouble              size);
239 240 241 242 243

/**
 ** end of gimpresolutionentry stuff
 ** the actual code can be found at the end of this file
 **/
244
const GimpPlugInInfo PLUG_IN_INFO =
245 246 247 248 249 250 251 252 253 254 255 256 257
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
};


MAIN ()

static void
query (void)
{
258
  static const GimpParamDef load_args[] =
259
  {
260
    { GIMP_PDB_INT32,     "run-mode",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }"     },
261
    { GIMP_PDB_STRING,    "filename",     "The name of the file to load"     },
262 263
    { GIMP_PDB_STRING,    "raw-filename", "The name entered"                 }
    /* XXX: Nice to have API at some point, but needs work
264
    { GIMP_PDB_INT32,     "resolution",   "Resolution to rasterize to (dpi)" },
265
    { GIMP_PDB_INT32,     "antialiasing", "Use anti-aliasing" },
266
    { GIMP_PDB_INT32,     "n-pages",      "Number of pages to load (0 for all)" },
267
    { GIMP_PDB_INT32ARRAY,"pages",        "The pages to load"                }
268
    */
269
  };
270

271
  static const GimpParamDef load_return_vals[] =
272
  {
273
    { GIMP_PDB_IMAGE,     "image",        "Output image" }
274 275
  };

276
  static const GimpParamDef thumb_args[] =
277
  {
278 279
    { GIMP_PDB_STRING,    "filename",     "The name of the file to load"  },
    { GIMP_PDB_INT32,     "thumb-size",   "Preferred thumbnail size"      }
280 281
  };

282
  static const GimpParamDef thumb_return_vals[] =
283 284
  {
    { GIMP_PDB_IMAGE,  "image",        "Thumbnail image"               },
285
    { GIMP_PDB_INT32,  "image-width",  "Width of full-sized image"     },
286 287 288
    { GIMP_PDB_INT32,  "image-height", "Height of full-sized image"    },
    { GIMP_PDB_INT32,  "image-type",   "Image type"                    },
    { GIMP_PDB_INT32,  "num-layers",   "Number of pages"               }
289 290
  };

291
  gimp_install_procedure (LOAD_PROC,
292
                          "Load file in PDF format",
293 294
                          "Loads files in Adobe's Portable Document Format. "
                          "PDF is designed to be easily processed by a variety "
295
                          "of different platforms, and is a distant cousin of "
296
                          "PostScript.",
297 298 299 300 301 302 303 304 305 306
                          "Nathan Summers",
                          "Nathan Summers",
                          "2005",
                          N_("Portable Document Format"),
                          NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (load_args),
                          G_N_ELEMENTS (load_return_vals),
                          load_args, load_return_vals);

307 308
  gimp_register_file_handler_mime (LOAD_PROC, "application/pdf");
  gimp_register_magic_load_handler (LOAD_PROC,
309 310 311 312
                                    "pdf",
                                    "",
                                    "0, string,%PDF-");

313
  gimp_install_procedure (LOAD_THUMB_PROC,
314 315 316 317 318 319 320 321 322 323 324 325 326 327
                          "Loads a preview from a PDF file.",
                          "Loads a small preview of the first page of the PDF "
                          "format file. Uses the embedded thumbnail if "
                          "present.",
                          "Nathan Summers",
                          "Nathan Summers",
                          "2005",
                          NULL,
                          NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (thumb_args),
                          G_N_ELEMENTS (thumb_return_vals),
                          thumb_args, thumb_return_vals);

328
  gimp_register_thumbnail_loader (LOAD_PROC, LOAD_THUMB_PROC);
329 330 331 332 333 334 335 336 337
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
338
  static GimpParam  values[6];
339
  GimpRunMode       run_mode;
340 341 342
  GimpPDBStatusType status   = GIMP_PDB_SUCCESS;
  gint32            image_ID = -1;
  PopplerDocument  *doc      = NULL;
343
  GError           *error    = NULL;
344 345 346

  run_mode = param[0].data.d_int32;

347
  INIT_I18N ();
348 349 350 351 352 353 354

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;

355
  if (strcmp (name, LOAD_PROC) == 0)
356
    {
357
      PdfSelectedPages pages = { 0, NULL };
358

359 360 361 362
      switch (run_mode)
        {
        case GIMP_RUN_INTERACTIVE:
          /* Possibly retrieve last settings */
363
          gimp_get_data (LOAD_PROC, &loadvals);
364

365
          doc = open_document (param[1].data.d_string, &error);
366 367 368 369 370 371 372

          if (!doc)
            {
              status = GIMP_PDB_EXECUTION_ERROR;
              break;
            }

373 374
          status = load_dialog (doc, &pages);
          if (status == GIMP_PDB_SUCCESS)
375
            gimp_set_data (LOAD_PROC, &loadvals, sizeof(loadvals));
376 377
          break;

David Odin's avatar
David Odin committed
378
        case GIMP_RUN_WITH_LAST_VALS:
379
          /* FIXME: implement last vals mode */
380 381
          status = GIMP_PDB_EXECUTION_ERROR;
          break;
382 383

        case GIMP_RUN_NONINTERACTIVE:
384
          doc = open_document (param[1].data.d_string, &error);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

          if (doc)
            {
              PopplerPage *test_page = poppler_document_get_page (doc, 0);

              if (test_page)
                {
                  pages.n_pages = 1;
                  pages.pages = g_new (gint, 1);
                  pages.pages[0] = 0;

                  g_object_unref (test_page);
                }
              else
                {
                  status = GIMP_PDB_EXECUTION_ERROR;
                  g_object_unref (doc);
                }
            }
          else
405 406 407
            {
              status = GIMP_PDB_EXECUTION_ERROR;
            }
408
          break;
409 410 411
        }

      if (status == GIMP_PDB_SUCCESS)
412 413
        {
          image_ID = load_image (doc, param[1].data.d_string,
414 415
                                 run_mode,
                                 loadvals.target,
416
                                 loadvals.resolution,
417
                                 loadvals.antialias,
418
                                 &pages);
419 420 421 422 423 424 425 426 427 428 429 430

          if (image_ID != -1)
            {
              *nreturn_vals = 2;
              values[1].type         = GIMP_PDB_IMAGE;
              values[1].data.d_image = image_ID;
            }
          else
            {
              status = GIMP_PDB_EXECUTION_ERROR;
            }
        }
431 432

      if (doc)
433
        g_object_unref (doc);
434

435
      g_free (pages.pages);
436
    }
437
  else if (strcmp (name, LOAD_THUMB_PROC) == 0)
438 439 440 441 442 443 444
    {
      if (nparams < 2)
        {
          status = GIMP_PDB_CALLING_ERROR;
        }
      else
        {
445 446 447 448 449 450
          gdouble          width     = 0;
          gdouble          height    = 0;
          gdouble          scale;
          gint32           image     = -1;
          gint             num_pages = 0;
          cairo_surface_t *surface   = NULL;
451 452

          /* Possibly retrieve last settings */
453
          gimp_get_data (LOAD_PROC, &loadvals);
454

455
          doc = open_document (param[0].data.d_string, &error);
456 457 458

          if (doc)
            {
459
              PopplerPage *page = poppler_document_get_page (doc, 0);
460

461 462 463
              if (page)
                {
                  poppler_page_get_size (page, &width, &height);
464

465
                  g_object_unref (page);
466
                }
467

468 469
              num_pages = poppler_document_get_n_pages (doc);

470
              surface = get_thumb_surface (doc, 0, param[1].data.d_int32);
471

472
              g_object_unref (doc);
473 474
            }

475
          if (surface)
476
            {
477 478
              image = gimp_image_new (cairo_image_surface_get_width (surface),
                                      cairo_image_surface_get_height (surface),
479
                                      GIMP_RGB);
480

481
              gimp_image_undo_disable (image);
482

483 484
              layer_from_surface (image, "thumbnail", 0, surface, 0.0, 1.0);
              cairo_surface_destroy (surface);
485

486 487
              gimp_image_undo_enable (image);
              gimp_image_clean_all (image);
488 489
            }

490
          scale = loadvals.resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);
491 492 493 494 495 496

          width  *= scale;
          height *= scale;

          if (image != -1)
            {
497
              *nreturn_vals = 6;
498 499

              values[1].type         = GIMP_PDB_IMAGE;
David Odin's avatar
David Odin committed
500 501 502 503 504
              values[1].data.d_image = image;
              values[2].type         = GIMP_PDB_INT32;
              values[2].data.d_int32 = width;
              values[3].type         = GIMP_PDB_INT32;
              values[3].data.d_int32 = height;
505 506 507 508
              values[4].type         = GIMP_PDB_INT32;
              values[4].data.d_int32 = GIMP_RGB_IMAGE;
              values[5].type         = GIMP_PDB_INT32;
              values[5].data.d_int32 = num_pages;
509 510 511 512 513 514
            }
          else
            {
              status = GIMP_PDB_EXECUTION_ERROR;
            }
        }
515

516 517 518 519 520 521
    }
  else
    {
      status = GIMP_PDB_CALLING_ERROR;
    }

522
  if (status != GIMP_PDB_SUCCESS && error)
523 524 525 526 527 528
    {
      *nreturn_vals = 2;
      values[1].type          = GIMP_PDB_STRING;
      values[1].data.d_string = error->message;
    }

529 530 531 532
  values[0].data.d_status = status;
}

static PopplerDocument*
533 534
open_document (const gchar  *filename,
               GError      **load_error)
535 536
{
  PopplerDocument *doc;
537
  GMappedFile     *mapped_file;
538
  GError          *error = NULL;
539

540
  mapped_file = g_mapped_file_new (filename, FALSE, &error);
541

542
  if (! mapped_file)
543
    {
544
      g_set_error (load_error, 0, 0,
545
                   _("Could not load '%s': %s"),
546 547
                   gimp_filename_to_utf8 (filename), error->message);
      g_error_free (error);
548 549
      return NULL;
    }
550

551 552 553 554
  doc = poppler_document_new_from_data (g_mapped_file_get_contents (mapped_file),
                                        g_mapped_file_get_length (mapped_file),
                                        NULL,
                                        &error);
555

556 557 558 559
  /* We can't g_mapped_file_unref(mapped_file) as apparently doc has
   * references to data in there. No big deal, this is just a
   * short-lived plug-in.
   */
560

561
  if (! doc)
562
    {
563
      g_set_error (load_error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
564
                   _("Could not load '%s': %s"),
565 566 567
                   gimp_filename_to_utf8 (filename),
                   error->message);
      g_error_free (error);
568 569 570 571 572 573
      return NULL;
    }

  return doc;
}

574 575 576 577 578 579
/* FIXME: Remove this someday when we depend fully on GTK+ >= 3 */

#if (!GTK_CHECK_VERSION (3, 0, 0))

static cairo_format_t
gdk_cairo_format_for_content (cairo_content_t content)
580
{
581 582 583 584 585 586 587 588 589 590 591
  switch (content)
    {
    case CAIRO_CONTENT_COLOR:
      return CAIRO_FORMAT_RGB24;
    case CAIRO_CONTENT_ALPHA:
      return CAIRO_FORMAT_A8;
    case CAIRO_CONTENT_COLOR_ALPHA:
    default:
      return CAIRO_FORMAT_ARGB32;
    }
}
592

593 594 595 596 597 598 599 600 601 602
static cairo_surface_t *
gdk_cairo_surface_coerce_to_image (cairo_surface_t *surface,
                                   cairo_content_t  content,
                                   int              src_x,
                                   int              src_y,
                                   int              width,
                                   int              height)
{
  cairo_surface_t *copy;
  cairo_t *cr;
603

604 605 606 607 608 609 610 611 612 613 614
  copy = cairo_image_surface_create (gdk_cairo_format_for_content (content),
                                     width,
                                     height);

  cr = cairo_create (copy);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  cairo_set_source_surface (cr, surface, -src_x, -src_y);
  cairo_paint (cr);
  cairo_destroy (cr);

  return copy;
615 616
}

617
static void
618 619 620 621 622 623 624 625
convert_alpha (guchar *dest_data,
               int     dest_stride,
               guchar *src_data,
               int     src_stride,
               int     src_x,
               int     src_y,
               int     width,
               int     height)
626 627 628
{
  int x, y;

629
  src_data += src_stride * src_y + src_x * 4;
630

631 632
  for (y = 0; y < height; y++) {
    guint32 *src = (guint32 *) src_data;
633

634 635
    for (x = 0; x < width; x++) {
      guint alpha = src[x] >> 24;
636

637 638 639 640 641 642 643
      if (alpha == 0)
        {
          dest_data[x * 4 + 0] = 0;
          dest_data[x * 4 + 1] = 0;
          dest_data[x * 4 + 2] = 0;
        }
      else
644
        {
645 646 647 648 649 650
          dest_data[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
          dest_data[x * 4 + 1] = (((src[x] & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
          dest_data[x * 4 + 2] = (((src[x] & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
        }
      dest_data[x * 4 + 3] = alpha;
    }
651

652 653 654 655
    src_data += src_stride;
    dest_data += dest_stride;
  }
}
656

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
static void
convert_no_alpha (guchar *dest_data,
                  int     dest_stride,
                  guchar *src_data,
                  int     src_stride,
                  int     src_x,
                  int     src_y,
                  int     width,
                  int     height)
{
  int x, y;

  src_data += src_stride * src_y + src_x * 4;

  for (y = 0; y < height; y++) {
    guint32 *src = (guint32 *) src_data;

    for (x = 0; x < width; x++) {
      dest_data[x * 3 + 0] = src[x] >> 16;
      dest_data[x * 3 + 1] = src[x] >>  8;
      dest_data[x * 3 + 2] = src[x];
678
    }
679 680 681 682

    src_data += src_stride;
    dest_data += dest_stride;
  }
683 684
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
/**
 * gdk_pixbuf_get_from_surface:
 * @surface: surface to copy from
 * @src_x: Source X coordinate within @surface
 * @src_y: Source Y coordinate within @surface
 * @width: Width in pixels of region to get
 * @height: Height in pixels of region to get
 *
 * Transfers image data from a #cairo_surface_t and converts it to an RGB(A)
 * representation inside a #GdkPixbuf. This allows you to efficiently read
 * individual pixels from cairo surfaces. For #GdkWindows, use
 * gdk_pixbuf_get_from_window() instead.
 *
 * This function will create an RGB pixbuf with 8 bits per channel.
 * The pixbuf will contain an alpha channel if the @surface contains one.
 *
 * Return value: (transfer full): A newly-created pixbuf with a reference
 *     count of 1, or %NULL on error
 */
704
static GdkPixbuf *
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
gdk_pixbuf_get_from_surface  (cairo_surface_t *surface,
                              gint             src_x,
                              gint             src_y,
                              gint             width,
                              gint             height)
{
  cairo_content_t content;
  GdkPixbuf *dest;

  /* General sanity checks */
  g_return_val_if_fail (surface != NULL, NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
  dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                         !!(content & CAIRO_CONTENT_ALPHA),
                         8,
                         width, height);

  surface = gdk_cairo_surface_coerce_to_image (surface, content,
                                               src_x, src_y,
                                               width, height);
  cairo_surface_flush (surface);
  if (cairo_surface_status (surface) || dest == NULL)
    {
      cairo_surface_destroy (surface);
      return NULL;
    }

  if (gdk_pixbuf_get_has_alpha (dest))
    convert_alpha (gdk_pixbuf_get_pixels (dest),
                   gdk_pixbuf_get_rowstride (dest),
                   cairo_image_surface_get_data (surface),
                   cairo_image_surface_get_stride (surface),
                   0, 0,
                   width, height);
  else
    convert_no_alpha (gdk_pixbuf_get_pixels (dest),
                      gdk_pixbuf_get_rowstride (dest),
                      cairo_image_surface_get_data (surface),
                      cairo_image_surface_get_stride (surface),
                      0, 0,
                      width, height);

  cairo_surface_destroy (surface);
  return dest;
}

#endif

static gint32
layer_from_surface (gint32           image,
                    const gchar     *layer_name,
                    gint             position,
                    cairo_surface_t *surface,
                    gdouble          progress_start,
                    gdouble          progress_scale)
{
  gint32 layer = gimp_layer_new_from_surface (image, layer_name, surface,
                                              progress_start,
                                              progress_start + progress_scale);

  gimp_image_insert_layer (image, layer, -1, position);

  return layer;
}

static cairo_surface_t *
render_page_to_surface (PopplerPage *page,
                        int          width,
                        int          height,
776 777
                        double       scale,
                        gboolean     antialias)
778 779 780 781 782 783 784 785 786 787 788 789 790
{
  cairo_surface_t *surface;
  cairo_t *cr;

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
  cr = cairo_create (surface);

  cairo_save (cr);
  cairo_translate (cr, 0.0, 0.0);

  if (scale != 1.0)
    cairo_scale (cr, scale, scale);

791 792
  if (! antialias)
    {
793
      cairo_font_options_t *options = cairo_font_options_create ();
794 795 796 797 798

      cairo_get_font_options (cr, options);
      cairo_font_options_set_antialias (options, CAIRO_ANTIALIAS_NONE);
      cairo_set_font_options (cr, options);
      cairo_font_options_destroy (options);
799 800

      cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
801 802
    }

803 804 805 806 807 808 809 810
  poppler_page_render (page, cr);
  cairo_restore (cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_paint (cr);

  cairo_destroy (cr);
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

  return surface;
}

#if 0

/* This is currently unused, but we'll have it here in case the military
   wants it. */

static GdkPixbuf *
render_page_to_pixbuf (PopplerPage *page,
                       int          width,
                       int          height,
                       double       scale)
{
  GdkPixbuf *pixbuf;
  cairo_surface_t *surface;

  surface = render_page_to_surface (page, width, height, scale);
  pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
                                        cairo_image_surface_get_width (surface),
                                        cairo_image_surface_get_height (surface));
833 834 835 836 837
  cairo_surface_destroy (surface);

  return pixbuf;
}

838 839
#endif

840
static gint32
841 842 843 844 845
load_image (PopplerDocument        *doc,
            const gchar            *filename,
            GimpRunMode             run_mode,
            GimpPageSelectorTarget  target,
            guint32                 resolution,
846
            gboolean                antialias,
847
            PdfSelectedPages       *pages)
848
{
849 850 851 852 853 854 855 856
  gint32   image_ID = 0;
  gint32  *images   = NULL;
  gint     i;
  gdouble  scale;
  gdouble  doc_progress = 0;

  if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
    images = g_new0 (gint32, pages->n_pages);
857

858
  gimp_progress_init_printf (_("Opening '%s'"),
859
                             gimp_filename_to_utf8 (filename));
860 861 862 863 864 865

  scale = resolution / gimp_unit_get_factor (GIMP_UNIT_POINT);

  /* read the file */

  for (i = 0; i < pages->n_pages; i++)
866
    {
867 868 869 870
      PopplerPage *page;
      gchar       *page_label;
      gdouble      page_width;
      gdouble      page_height;
871

872 873 874
      cairo_surface_t *surface;
      gint             width;
      gint             height;
875 876

      page = poppler_document_get_page (doc, pages->pages[i]);
877

878 879 880
      poppler_page_get_size (page, &page_width, &page_height);
      width  = page_width  * scale;
      height = page_height * scale;
881

882 883 884
      g_object_get (G_OBJECT (page), "label", &page_label, NULL);

      if (! image_ID)
885
        {
886 887
          gchar *name;

888 889
          image_ID = gimp_image_new (width, height, GIMP_RGB);
          gimp_image_undo_disable (image_ID);
890

891 892 893 894 895 896
          if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
            name = g_strdup_printf (_("%s-%s"), filename, page_label);
          else
            name = g_strdup_printf (_("%s-pages"), filename);

          gimp_image_set_filename (image_ID, name);
897
          g_free (name);
898

899
          gimp_image_set_resolution (image_ID, resolution, resolution);
900 901
        }

902
      surface = render_page_to_surface (page, width, height, scale, antialias);
903

904 905
      layer_from_surface (image_ID, page_label, i, surface,
                          doc_progress, 1.0 / pages->n_pages);
906 907

      g_free (page_label);
908
      cairo_surface_destroy (surface);
909

910 911
      doc_progress = (double) (i + 1) / pages->n_pages;
      gimp_progress_update (doc_progress);
912 913 914 915 916 917 918 919 920 921

      if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
        {
          images[i] = image_ID;

          gimp_image_undo_enable (image_ID);
          gimp_image_clean_all (image_ID);

          image_ID = 0;
        }
922 923
    }
  gimp_progress_update (1.0);
924

925 926 927 928 929 930 931
  if (image_ID)
    {
      gimp_image_undo_enable (image_ID);
      gimp_image_clean_all (image_ID);
    }

  if (target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
932
    {
933 934 935 936 937 938 939 940 941 942 943 944
      if (run_mode != GIMP_RUN_NONINTERACTIVE)
        {
          /* Display images in reverse order.  The last will be
           * displayed by GIMP itself
           */
          for (i = pages->n_pages - 1; i > 0; i--)
            gimp_display_new (images[i]);
        }

      image_ID = images[0];

      g_free (images);
945 946
    }

947
  return image_ID;
948 949
}

950 951 952 953
static cairo_surface_t *
get_thumb_surface (PopplerDocument *doc,
                   gint             page_num,
                   gint             preferred_size)
954 955
{
  PopplerPage *page;
956
  cairo_surface_t *surface;
957 958 959 960 961

  page = poppler_document_get_page (doc, page_num);

  if (! page)
    return NULL;
962

963
  surface = poppler_page_get_thumbnail (page);
964

965
  if (! surface)
966
    {
967 968 969
      gdouble width;
      gdouble height;
      gdouble scale;
970 971 972

      poppler_page_get_size (page, &width, &height);

973
      scale = (gdouble) preferred_size / MAX (width, height);
974 975 976 977

      width  *= scale;
      height *= scale;

978
      surface = render_page_to_surface (page, width, height, scale, TRUE);
979 980
    }

981
  g_object_unref (page);
982

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
  return surface;
}

static GdkPixbuf *
get_thumb_pixbuf (PopplerDocument *doc,
                  gint             page_num,
                  gint             preferred_size)
{
  cairo_surface_t *surface;
  GdkPixbuf *pixbuf;

  surface = get_thumb_surface (doc, page_num, preferred_size);
  pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0,
                                        cairo_image_surface_get_width (surface),
                                        cairo_image_surface_get_height (surface));
  cairo_surface_destroy (surface);

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
  return pixbuf;
}

typedef struct
{
  PopplerDocument  *document;
  GimpPageSelector *selector;
  gboolean          stop_thumbnailing;
} ThreadData;

typedef struct
{
  GimpPageSelector *selector;
  gint              page_no;
  GdkPixbuf        *pixbuf;
} IdleData;

static gboolean
idle_set_thumbnail (gpointer data)
{
  IdleData *idle_data = data;

  gimp_page_selector_set_page_thumbnail (idle_data->selector,
                                         idle_data->page_no,
                                         idle_data->pixbuf);
  g_object_unref (idle_data->pixbuf);
  g_free (idle_data);

  return FALSE;
}

static gpointer
thumbnail_thread (gpointer data)
{
  ThreadData  *thread_data = data;
  gint         n_pages;
  gint         i;

  n_pages = poppler_document_get_n_pages (thread_data->document);

  for (i = 0; i < n_pages; i++)
    {
      IdleData *idle_data = g_new0 (IdleData, 1);

      idle_data->selector = thread_data->selector;
      idle_data->page_no  = i;

      /* FIXME get preferred size from somewhere? */
1048 1049
      idle_data->pixbuf = get_thumb_pixbuf (thread_data->document, i,
                                            THUMBNAIL_SIZE);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

      g_idle_add (idle_set_thumbnail, idle_data);

      if (thread_data->stop_thumbnailing)
        break;
    }

  return NULL;
}

1060
static GimpPDBStatusType
1061 1062 1063
load_dialog (PopplerDocument  *doc,
             PdfSelectedPages *pages)
{
1064 1065
  GtkWidget  *dialog;
  GtkWidget  *vbox;
1066
  GtkWidget  *title;
1067
  GtkWidget  *selector;
1068
  GtkWidget  *resolution;
1069
  GtkWidget  *antialias;
1070
  GtkWidget  *hbox;
1071

1072 1073 1074 1075 1076 1077 1078 1079
  ThreadData  thread_data;
  GThread    *thread;

  gint        i;
  gint        n_pages;

  gdouble     width;
  gdouble     height;
1080

1081
  gboolean    run;
1082

1083
  gimp_ui_init (PLUG_IN_BINARY, FALSE);
1084

1085
  dialog = gimp_dialog_new (_("Import from PDF"), PLUG_IN_ROLE,
1086
                            NULL, 0,
1087
                            gimp_standard_help_func, LOAD_PROC,
1088 1089

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1090
                            _("_Import"),     GTK_RESPONSE_OK,
1091 1092 1093 1094 1095 1096 1097 1098

                            NULL);

  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

1099 1100
  gimp_window_set_transient (GTK_WINDOW (dialog));

1101
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1102
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1103 1104
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                      vbox, TRUE, TRUE, 0);
1105 1106
  gtk_widget_show (vbox);

1107
  /* Title */
1108
  title = gimp_prop_label_new (G_OBJECT (doc), "title");
1109
  gtk_label_set_ellipsize (GTK_LABEL (title), PANGO_ELLIPSIZE_END);
1110 1111
  gtk_box_pack_start (GTK_BOX (vbox), title, FALSE, FALSE, 0);
  gtk_widget_show (title);
1112

1113 1114
  /* Page Selector */
  selector = gimp_page_selector_new ();
1115
  gtk_widget_set_size_request (selector, 380, 360);
1116
  gtk_box_pack_start (GTK_BOX (vbox), selector, TRUE, TRUE, 0);
1117 1118
  gtk_widget_show (selector);

1119
  n_pages = poppler_document_get_n_pages (doc);
1120 1121

  if (n_pages <= 0)
1122 1123 1124 1125
    {
      g_message (_("Error getting number of pages from the given PDF file."));
      return GIMP_PDB_EXECUTION_ERROR;
    }
1126

1127
  gimp_page_selector_set_n_pages (GIMP_PAGE_SELECTOR (selector), n_pages);
1128 1129
  gimp_page_selector_set_target (GIMP_PAGE_SELECTOR (selector),
                                 loadvals.target);
1130

1131
  for (i = 0; i < n_pages; i++)
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    {
      PopplerPage     *page;
      gchar           *label;

      page = poppler_document_get_page (doc, i);
      g_object_get (G_OBJECT (page), "label", &label, NULL);

      gimp_page_selector_set_page_label (GIMP_PAGE_SELECTOR (selector), i,
                                         label);

1142 1143
      if (i == 0)
        poppler_page_get_size (page, &width, &height);
1144

1145 1146 1147 1148
      g_object_unref (page);
      g_free (label);
    }

1149 1150 1151 1152
  g_signal_connect_swapped (selector, "activate",
                            G_CALLBACK (gtk_window_activate_default),
                            dialog);

1153 1154 1155 1156 1157 1158 1159 1160
  thread_data.document          = doc;
  thread_data.selector          = GIMP_PAGE_SELECTOR (selector);
  thread_data.stop_thumbnailing = FALSE;

  thread = g_thread_create (thumbnail_thread, &thread_data, TRUE, NULL);

  /* Resolution */

1161
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1162
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1163 1164
  gtk_widget_show (hbox);

1165 1166 1167
  resolution = gimp_resolution_entry_new (_("_Width (pixels):"), width,
                                          _("_Height (pixels):"), height,
                                          GIMP_UNIT_POINT,
1168 1169
                                          _("_Resolution:"),
                                          loadvals.resolution, GIMP_UNIT_INCH);
1170

1171 1172
  gtk_box_pack_start (GTK_BOX (hbox), resolution, FALSE, FALSE, 0);
  gtk_widget_show (resolution);
1173

1174 1175 1176
  g_signal_connect (resolution, "x-changed",
                    G_CALLBACK (gimp_resolution_entry_update_x_in_dpi),
                    &loadvals.resolution);
1177

1178
  /* Antialiasing*/
1179
  antialias = gtk_check_button_new_with_mnemonic (_("Use _Anti-aliasing"));
1180 1181 1182 1183 1184 1185 1186
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (antialias), loadvals.antialias);
  gtk_box_pack_start (GTK_BOX (vbox), antialias, FALSE, FALSE, 0);

  g_signal_connect (antialias, "toggled",
                    G_CALLBACK (gimp_toggle_button_update), &loadvals.antialias);
  gtk_widget_show (antialias);

1187 1188 1189 1190 1191
  /* Setup done; display the dialog */
  gtk_widget_show (dialog);

  /* run the dialog */
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
1192

1193 1194 1195
  loadvals.target =
    gimp_page_selector_get_target (GIMP_PAGE_SELECTOR (selector));

1196
  pages->pages =
1197 1198 1199 1200 1201 1202 1203 1204
    gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
                                           &pages->n_pages);

  /* select all if none selected */
  if (pages->n_pages == 0)
    {
      gimp_page_selector_select_all (GIMP_PAGE_SELECTOR (selector));

1205
      pages->pages =
1206 1207 1208
        gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
                                               &pages->n_pages);
    }
1209

1210 1211 1212 1213
  /* cleanup */
  thread_data.stop_thumbnailing = TRUE;
  g_thread_join (thread);

1214
  return run ? GIMP_PDB_SUCCESS : GIMP_PDB_CANCEL;
1215
}
1216 1217 1218 1219


/**
 ** code for GimpResolutionEntry widget, formerly in libgimpwidgets
1220 1221 1222
 **/

static guint gimp_resolution_entry_signals[LAST_SIGNAL] = { 0 };
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

static GtkTableClass *parent_class = NULL;


GType
gimp_resolution_entry_get_type (void)
{
  static GType gre_type = 0;

  if (! gre_type)
    {
      const GTypeInfo gre_info =
      {
        sizeof (GimpResolutionEntryClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_resolution_entry_class_init,
        NULL,                /* class_finalize */
        NULL,                /* class_data     */
        sizeof (GimpResolutionEntry),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_resolution_entry_init,
      };

      gre_type = g_type_register_static (GTK_TYPE_TABLE,
                                         "GimpResolutionEntry",
                                         &gre_info, 0);
    }

  return gre_type;
}

static void
gimp_resolution_entry_class_init (GimpResolutionEntryClass *klass)
{
  parent_class = g_type_class_peek_parent (klass);

  gimp_resolution_entry_signals[HEIGHT_CHANGED] =
    g_signal_new ("height-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  gimp_resolution_entry_signals[WIDTH_CHANGED] =
    g_signal_new ("width-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  gimp_resolution_entry_signals[X_CHANGED] =
    g_signal_new ("x-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpResolutionEntryClass, value_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  gimp_resolution_entry_signals[Y_CHANGED] =
    g_signal_new ("y-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpResolutionEntryClass, refval_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  gimp_resolution_entry_signals[UNIT_CHANGED] =
    g_signal_new ("unit-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpResolutionEntryClass, unit_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

1305 1306 1307
  klass->value_changed  = NULL;
  klass->refval_changed = NULL;
  klass->unit_changed   = NULL;
1308 1309 1310 1311 1312
}

static void
gimp_resolution_entry_init (GimpResolutionEntry *gre)
{
1313 1314
  gre->unitmenu = NULL;
  gre->unit     = GIMP_UNIT_INCH;
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

  gtk_table_set_col_spacings (GTK_TABLE (gre), 4);
  gtk_table_set_row_spacings (GTK_TABLE (gre), 2);
}

static void
gimp_resolution_entry_field_init (GimpResolutionEntry      *gre,
                                  GimpResolutionEntryField *gref,
                                  GimpResolutionEntryField *corresponding,
                                  guint                     changed_signal,
                                  gdouble                   initial_val,
                                  GimpUnit                  initial_unit,
                                  gboolean                  size,
                                  gint                      spinbutton_width)
{
  gint digits;

  g_return_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre));

  gref->gre               = gre;
  gref->corresponding     = corresponding;
  gref->changed_signal    = gimp_resolution_entry_signals[changed_signal];

  if (size)
    {
      gref->value         = initial_val /
                            gimp_unit_get_factor (initial_unit) *
                            corresponding->value *
                            gimp_unit_get_factor (gre->unit);

      gref->phy_size      = initial_val /
                            gimp_unit_get_factor (initial_unit);
    }
  else
1349 1350 1351
    {
      gref->value         = initial_val;
    }
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

  gref->min_value         = GIMP_MIN_RESOLUTION;
  gref->max_value         = GIMP_MAX_RESOLUTION;
  gref->adjustment        = NULL;

  gref->stop_recursion    = 0;

  gref->size              = size;

  if (size)
    {
      gref->label = g_object_new (GTK_TYPE_LABEL,
                                  "xalign", 0.0,
                                  "yalign", 0.5,
                                  NULL);
      gimp_label_set_attributes (GTK_LABEL (gref->label),
                                 PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC,
                                 -1);

      gimp_resolution_entry_format_label (gre, gref->label, gref->phy_size);
    }

1374
  digits = size ? 0 : MIN (gimp_unit_get_digits (initial_unit), 5) + 1;
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

  gref->spinbutton = gimp_spin_button_new (&gref->adjustment,
                                            gref->value,
                                            gref->min_value,
                                            gref->max_value,
                                            1.0, 10.0, 0.0,
                                            1.0,
                                            digits);


  if (spinbutton_width > 0)
    {
      if (spinbutton_width < 17)
        gtk_entry_set_width_chars (GTK_ENTRY (gref->spinbutton),
                                   spinbutton_width);
      else
        gtk_widget_set_size_request (gref->spinbutton,
                                     spinbutton_width, -1);
    }
}

/**
 * gimp_resolution_entry_new:
 * @width_label:       Optional label for the width control.
 * @width:             Width of the item, specified in terms of @size_unit.
 * @height_label:      Optional label for the height control.
 * @height:            Height of the item, specified in terms of @size_unit.
 * @size_unit:         Unit used to specify the width and height.
1403 1404
 * @res_label:         Optional label for the resolution entry.
 * @initial_res:       The initial resolution.
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
 * @initial_unit:      The initial unit.
 *
 * Creates a new #GimpResolutionEntry widget.
 *
 * The #GimpResolutionEntry is derived from #GtkTable and will have
 * an empty border of one cell width on each side plus an empty column left
 * of the #GimpUnitMenu to allow the caller to add labels or other widgets.
 *
 * A #GimpChainButton is displayed if independent is set to %TRUE.
 *
 * Returns: A pointer to the new #GimpResolutionEntry widget.
 **/
GtkWidget *
gimp_resolution_entry_new (const gchar *width_label,
                           gdouble      width,
                           const gchar *height_label,
                           gdouble      height,
                           GimpUnit     size_unit,
1423 1424 1425
                           const gchar *res_label,
                           gdouble      initial_res,
                           GimpUnit     initial_unit)
1426 1427
{
  GimpResolutionEntry *gre;
1428
  GtkTreeModel        *model;
1429 1430 1431

  gre = g_object_new (GIMP_TYPE_RESOLUTION_ENTRY, NULL);

1432
  gre->unit = initial_unit;
1433

1434
  gtk_table_resize (GTK_TABLE (gre), 4, 4);
1435 1436 1437 1438

  gimp_resolution_entry_field_init (gre, &gre->x,
                                    &gre->width,
                                    X_CHANGED,
1439 1440
                                    initial_res, initial_unit,
                                    FALSE, 0);
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

  gtk_table_attach_defaults (GTK_TABLE (gre), gre->x.spinbutton,
                             1, 2,
                             3, 4);

  g_signal_connect (gre->x.adjustment, "value-changed",
                    G_CALLBACK (gimp_resolution_entry_value_callback),
                    &gre->x);

  gtk_widget_show (gre->x.spinbutton);

1452 1453 1454 1455
  gre->unitmenu = gimp_unit_combo_box_new ();
  model = gtk_combo_box_get_model (GTK_COMBO_BOX (gre->unitmenu));
  gimp_unit_store_set_has_pixels (GIMP_UNIT_STORE (model), FALSE);
  gimp_unit_store_set_has_percent (GIMP_UNIT_STORE (model), FALSE);
1456 1457 1458 1459
  g_object_set (model,
                "short-format", _("pixels/%a"),
                "long-format",  _("pixels/%a"),
                NULL);
1460 1461 1462
  gimp_unit_combo_box_set_active (GIMP_UNIT_COMBO_BOX (gre->unitmenu),
                                  initial_unit);

1463
  gtk_table_attach (GTK_TABLE (gre), gre->unitmenu,
1464
                    3, 4, 3, 4,
1465
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
1466
  g_signal_connect (gre->unitmenu, "changed",
1467 1468 1469 1470 1471 1472 1473 1474
                    G_CALLBACK (gimp_resolution_entry_unit_callback),
                    gre);
  gtk_widget_show (gre->unitmenu);

  gimp_resolution_entry_field_init (gre, &gre->width,
                                    &gre->x,
                                    WIDTH_CHANGED,
                                    width, size_unit,
1475
                                    TRUE, 0);
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

  gtk_table_attach_defaults (GTK_TABLE (gre), gre->width.spinbutton,
                             1, 2,
                             1, 2);

  gtk_table_attach_defaults (GTK_TABLE (gre), gre->width.label,
                             3, 4,
                             1, 2);

  g_signal_connect (gre->width.adjustment, "value-changed",
                    G_CALLBACK (gimp_resolution_entry_value_callback),
                    &gre->width);

  gtk_widget_show (gre->width.spinbutton);
  gtk_widget_show (gre->width.label);

1492
  gimp_resolution_entry_field_init (gre, &gre->height, &gre->x,
1493 1494
                                    HEIGHT_CHANGED,
                                    height, size_unit,
1495
                                    TRUE, 0);
1496 1497

  gtk_table_attach_defaults (GTK_TABLE (gre), gre->height.spinbutton,
1498
                             1, 2, 2, 3);
1499 1500

  gtk_table_attach_defaults (GTK_TABLE (gre), gre->height.label,
1501
                             3, 4, 2, 3);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

  g_signal_connect (gre->height.adjustment, "value-changed",
                    G_CALLBACK (gimp_resolution_entry_value_callback),
                    &gre->height);

  gtk_widget_show (gre->height.spinbutton);
  gtk_widget_show (gre->height.label);

  if (width_label)
    gimp_resolution_entry_attach_label (gre, width_label,  1, 0, 0.0);

  if (height_label)
    gimp_resolution_entry_attach_label (gre, height_label, 2, 0, 0.0);

1516 1517
  if (res_label)
    gimp_resolution_entry_attach_label (gre, res_label,    3, 0, 0.0);
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

  return GTK_WIDGET (gre);
}

/**
 * gimp_resolution_entry_attach_label:
 * @gre:       The #GimpResolutionEntry you want to add a label to.
 * @text:      The text of the label.
 * @row:       The row where the label will be attached.
 * @column:    The column where the label will be attached.
 * @alignment: The horizontal alignment of the label.
 *
 * Attaches a #GtkLabel to the #GimpResolutionEntry (which is a #GtkTable).
 *
 * Returns: A pointer to the new #GtkLabel widget.
 **/
GtkWidget *
gimp_resolution_entry_attach_label (GimpResolutionEntry *gre,
                                    const gchar         *text,
                                    gint                 row,
                                    gint                 column,
                                    gfloat               alignment)
{
  GtkWidget *label;

  g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), NULL);
  g_return_val_if_fail (text != NULL, NULL);

  label = gtk_label_new_with_mnemonic (text);

  if (column == 0)
    {
1550 1551
      GList *children;
      GList *list;
1552

1553 1554 1555
      children = gtk_container_get_children (GTK_CONTAINER (gre));

      for (list = children; list; list = g_list_next (list))
1556
        {
1557 1558 1559 1560 1561 1562 1563 1564
          GtkWidget *child = list->data;
          gint       left_attach;
          gint       top_attach;

          gtk_container_child_get (GTK_CONTAINER (gre), child,
                                   "left-attach", &left_attach,
                                   "top-attach",  &top_attach,
                                   NULL);
1565

1566
          if (left_attach == 1 && top_attach == row)
1567
            {
1568
              gtk_label_set_mnemonic_widget (GTK_LABEL (label), child);
1569 1570 1571
              break;
            }
        }
1572 1573

      g_list_free (children);
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    }

  gtk_misc_set_alignment (GTK_MISC (label), alignment, 0.5);

  gtk_table_attach (GTK_TABLE (gre), label, column, column+1, row, row+1,
                    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  gtk_widget_show (label);

  return label;
}

/**
 * gimp_resolution_entry_get_x_in_dpi;
 * @gre:   The #GimpResolutionEntry you want to know the resolution of.
 *
 * Returns the X resolution of the #GimpResolutionEntry in pixels per inch.
 **/
gdouble
gimp_resolution_entry_get_x_in_dpi (GimpResolutionEntry *gre)
{
  g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), 0);

1596 1597
  /* dots_in_one_unit * units_in_one_inch -> dpi */
  return gre->x.value * gimp_unit_get_factor (gre->unit);
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
}

/**
 * gimp_resolution_entry_get_y_in_dpi;
 * @gre:   The #GimpResolutionEntry you want to know the resolution of.
 *
 * Returns the Y resolution of the #GimpResolutionEntry in pixels per inch.
 **/
gdouble
gimp_resolution_entry_get_y_in_dpi (GimpResolutionEntry *gre)
{
  g_return_val_if_fail (GIMP_IS_RESOLUTION_ENTRY (gre), 0);

1611
  return gre->y.value * gimp_unit_get_factor (gre->unit);
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
}


static void
gimp_resolution_entry_update_value (GimpResolutionEntryField *gref,
                                    gdouble                   value)
{
  if (gref->stop_recursion > 0)
    return;

  gref->value = value;

  gref->stop_recursion++;

  if (gref->size)
    gimp_resolution_entry_update_value (gref->corresponding,
                                        gref->value /
                                          gref->phy_size /
                                          gimp_unit_get_factor (gref->gre->unit));
  else
    {
      gdouble factor = gimp_unit_get_factor (gref->gre->unit);

1635 1636 1637 1638 1639 1640 1641 1642 1643
      gimp_resolution_entry_update_value (&gref->gre->width,
                                          gref->value *
                                          gref->gre->width.phy_size *
                                          factor);

      gimp_resolution_entry_update_value (&gref->gre->height,
                                          gref->value *
                                          gref->gre->height.phy_size *
                                          factor);
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
    }

  gtk_adjustment_set_value (GTK_ADJUSTMENT (gref->adjustment), value);

  gref->stop_recursion--;

  g_signal_emit (gref->gre, gref->changed_signal, 0);
}

static void
gimp_resolution_entry_value_callback (GtkWidget *widget,
1655
                                      gpointer   data)
1656
{
1657 1658
  GimpResolutionEntryField *gref = (GimpResolutionEntryField *) data;
  gdouble                   new_value;
William Skaggs's avatar