1 /* GTK - The GIMP Toolkit
2 * gtkpapersize.c: Paper Size
3 * Copyright (C) 2006, Red Hat, Inc.
4 * Copyright © 2006, 2007 Christian Persch
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
30 #include "gtkpapersize.h"
31 #include "gtkprintutils.h"
32 #include "gtkprintoperation.h" /* for GtkPrintError */
35 #include "paper_names_offsets.c"
39 * SECTION:gtkpapersize
40 * @Short_description: Support for named paper sizes
41 * @Title: GtkPaperSize
42 * @See_also:#GtkPageSetup
44 * GtkPaperSize handles paper sizes. It uses the standard called
45 * <ulink url="http://www.pwg.org/standards.html">"PWG 5101.1-2002 PWG: Standard for Media Standardized Names"</ulink>
46 * to name the paper sizes (and to get the data for the page sizes).
47 * In addition to standard paper sizes, GtkPaperSize allows to
48 * construct custom paper sizes with arbitrary dimensions.
50 * The #GtkPaperSize object stores not only the dimensions (width
51 * and height) of a paper size and its name, it also provides
52 * default <link linkend="print-margins">print margins</link>.
54 * Printing support has been added in GTK+ 2.10.
60 const PaperInfo *info;
62 /* If these are not set we fall back to info */
67 gdouble width, height; /* Stored in mm */
71 G_DEFINE_BOXED_TYPE (GtkPaperSize, gtk_paper_size,
75 static const PaperInfo *
76 lookup_paper_info (const gchar *name)
79 int upper = G_N_ELEMENTS (standard_names_offsets) - 1;
85 mid = (lower + upper) / 2;
86 cmp = strcmp (name, paper_names + standard_names_offsets[mid].name);
92 return &standard_names_offsets[mid];
94 while (lower <= upper);
100 parse_media_size (const gchar *size,
106 double short_dim, long_dim;
110 short_dim = g_ascii_strtod (p, &e);
112 if (p == e || *e != 'x')
115 p = e + 1; /* Skip x */
117 long_dim = g_ascii_strtod (p, &e);
124 if (strcmp (p, "in") == 0)
126 short_dim = short_dim * MM_PER_INCH;
127 long_dim = long_dim * MM_PER_INCH;
129 else if (strcmp (p, "mm") != 0)
133 *width_mm = short_dim;
135 *height_mm = long_dim;
141 parse_full_media_size_name (const gchar *full_name,
147 const char *end_of_name;
150 media-size-self-describing-name =
151 ( class-in "_" size-name "_" short-dim "x" long-dim "in" ) |
152 ( class-mm "_" size-name "_" short-dim "x" long-dim "mm" )
153 class-in = "custom" | "na" | "asme" | "roc" | "oe"
154 class-mm = "custom" | "iso" | "jis" | "jpn" | "prc" | "om"
155 size-name = ( lowalpha | digit ) *( lowalpha | digit | "-" )
158 dim = integer-part [fraction-part] | "0" fraction-part
159 integer-part = non-zero-digit *digit
160 fraction-part = "." *digit non-zero-digit
161 lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
162 "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
163 "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
164 non-zero-digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
165 digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
168 p = strchr (full_name, '_');
182 if (!parse_media_size (p, width_mm, height_mm))
186 *name = g_strndup (full_name, end_of_name - full_name);
191 static GtkPaperSize *
192 gtk_paper_size_new_from_info (const PaperInfo *info)
196 size = g_slice_new0 (GtkPaperSize);
198 size->width = info->width;
199 size->height = info->height;
205 * gtk_paper_size_new:
206 * @name: (allow-none): a paper size name, or %NULL
208 * Creates a new #GtkPaperSize object by parsing a
209 * <ulink url="ftp://ftp.pwg.org/pub/pwg/candidates/cs-pwgmsn10-20020226-5101.1.pdf">PWG 5101.1-2002</ulink>
212 * If @name is %NULL, the default paper size is returned,
213 * see gtk_paper_size_get_default().
215 * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
221 gtk_paper_size_new (const gchar *name)
225 double width, height;
226 const PaperInfo *info;
229 name = gtk_paper_size_get_default ();
231 if (parse_full_media_size_name (name, &short_name, &width, &height))
233 info = lookup_paper_info (short_name);
234 if (info != NULL && info->width == width && info->height == height)
236 size = gtk_paper_size_new_from_info (info);
241 size = g_slice_new0 (GtkPaperSize);
244 size->height = height;
245 size->name = short_name;
246 size->display_name = g_strdup (short_name);
247 if (strncmp (short_name, "custom", 6) == 0)
248 size->is_custom = TRUE;
253 info = lookup_paper_info (name);
255 size = gtk_paper_size_new_from_info (info);
258 g_warning ("Unknown paper size %s\n", name);
259 size = g_slice_new0 (GtkPaperSize);
260 size->name = g_strdup (name);
261 size->display_name = g_strdup (name);
262 /* Default to A4 size */
272 * gtk_paper_size_new_from_ppd:
273 * @ppd_name: a PPD paper name
274 * @ppd_display_name: the corresponding human-readable name
275 * @width: the paper width, in points
276 * @height: the paper height in points
278 * Creates a new #GtkPaperSize object by using
281 * If @ppd_name is not a recognized PPD paper name,
282 * @ppd_display_name, @width and @height are used to
283 * construct a custom #GtkPaperSize object.
285 * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
291 gtk_paper_size_new_from_ppd (const gchar *ppd_name,
292 const gchar *ppd_display_name,
297 const char *lookup_ppd_name;
302 lookup_ppd_name = ppd_name;
305 /* Strip out Traverse suffix in matching. */
306 if (g_str_has_suffix (ppd_name, ".Transverse"))
308 lookup_ppd_name = freeme =
309 g_strndup (ppd_name, strlen (ppd_name) - strlen (".Transverse"));
312 for (i = 0; i < G_N_ELEMENTS(standard_names_offsets); i++)
314 if (standard_names_offsets[i].ppd_name != -1 &&
315 strcmp (paper_names + standard_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
317 size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
322 for (i = 0; i < G_N_ELEMENTS(extra_ppd_names_offsets); i++)
324 if (strcmp (paper_names + extra_ppd_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
326 size = gtk_paper_size_new (paper_names + extra_ppd_names_offsets[i].standard_name);
331 name = g_strconcat ("ppd_", ppd_name, NULL);
332 size = gtk_paper_size_new_custom (name, ppd_display_name, width, height, GTK_UNIT_POINTS);
337 if (size->info == NULL ||
338 size->info->ppd_name == -1 ||
339 strcmp (paper_names + size->info->ppd_name, ppd_name) != 0)
340 size->ppd_name = g_strdup (ppd_name);
348 * gtk_paper_size_new_custom:
349 * @name: the paper name
350 * @display_name: the human-readable name
351 * @width: the paper width, in units of @unit
352 * @height: the paper height, in units of @unit
353 * @unit: the unit for @width and @height
355 * Creates a new #GtkPaperSize object with the
358 * Return value: a new #GtkPaperSize object, use gtk_paper_size_free()
364 gtk_paper_size_new_custom (const gchar *name,
365 const gchar *display_name,
371 g_return_val_if_fail (name != NULL, NULL);
372 g_return_val_if_fail (unit != GTK_UNIT_PIXEL, NULL);
374 size = g_slice_new0 (GtkPaperSize);
376 size->name = g_strdup (name);
377 size->display_name = g_strdup (display_name);
378 size->is_custom = TRUE;
380 size->width = _gtk_print_convert_to_mm (width, unit);
381 size->height = _gtk_print_convert_to_mm (height, unit);
387 * gtk_paper_size_copy:
388 * @other: a #GtkPaperSize
390 * Copies an existing #GtkPaperSize.
392 * Return value: a copy of @other
397 gtk_paper_size_copy (GtkPaperSize *other)
401 size = g_slice_new0 (GtkPaperSize);
403 size->info = other->info;
405 size->name = g_strdup (other->name);
406 if (other->display_name)
407 size->display_name = g_strdup (other->display_name);
409 size->ppd_name = g_strdup (other->ppd_name);
411 size->width = other->width;
412 size->height = other->height;
413 size->is_custom = other->is_custom;
419 * gtk_paper_size_free:
420 * @size: a #GtkPaperSize
422 * Free the given #GtkPaperSize object.
427 gtk_paper_size_free (GtkPaperSize *size)
430 g_free (size->display_name);
431 g_free (size->ppd_name);
433 g_slice_free (GtkPaperSize, size);
437 * gtk_paper_size_is_equal:
438 * @size1: a #GtkPaperSize object
439 * @size2: another #GtkPaperSize object
441 * Compares two #GtkPaperSize objects.
443 * Return value: %TRUE, if @size1 and @size2
444 * represent the same paper size
449 gtk_paper_size_is_equal (GtkPaperSize *size1,
452 if (size1->info != NULL && size2->info != NULL)
453 return size1->info == size2->info;
455 return strcmp (gtk_paper_size_get_name (size1),
456 gtk_paper_size_get_name (size2)) == 0;
459 GList * _gtk_load_custom_papers (void);
462 * gtk_paper_size_get_paper_sizes:
463 * @include_custom: whether to include custom paper sizes
464 * as defined in the page setup dialog
466 * Creates a list of known paper sizes.
468 * Return value: (element-type GtkPaperSize) (transfer full): a newly allocated list of newly
469 * allocated #GtkPaperSize objects
474 gtk_paper_size_get_paper_sizes (gboolean include_custom)
478 #ifdef G_OS_UNIX /* _gtk_load_custom_papers() only on Unix so far */
481 GList *page_setups, *l;
483 page_setups = _gtk_load_custom_papers ();
484 for (l = page_setups; l != NULL; l = l->next)
486 GtkPageSetup *setup = (GtkPageSetup *) l->data;
489 size = gtk_page_setup_get_paper_size (setup);
490 list = g_list_prepend (list, gtk_paper_size_copy (size));
493 g_list_foreach (page_setups, (GFunc) g_object_unref, NULL);
494 g_list_free (page_setups);
497 for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); ++i)
501 size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
502 list = g_list_prepend (list, size);
505 return g_list_reverse (list);
510 * gtk_paper_size_get_name:
511 * @size: a #GtkPaperSize object
513 * Gets the name of the #GtkPaperSize.
515 * Return value: the name of @size
520 gtk_paper_size_get_name (GtkPaperSize *size)
524 g_assert (size->info != NULL);
525 return paper_names + size->info->name;
529 * gtk_paper_size_get_display_name:
530 * @size: a #GtkPaperSize object
532 * Gets the human-readable name of the #GtkPaperSize.
534 * Return value: the human-readable name of @size
539 gtk_paper_size_get_display_name (GtkPaperSize *size)
541 const gchar *display_name;
543 if (size->display_name)
544 return size->display_name;
546 g_assert (size->info != NULL);
548 display_name = paper_names + size->info->display_name;
549 return g_dpgettext2 (GETTEXT_PACKAGE, "paper size", display_name);
553 * gtk_paper_size_get_ppd_name:
554 * @size: a #GtkPaperSize object
556 * Gets the PPD name of the #GtkPaperSize, which
559 * Return value: the PPD name of @size
564 gtk_paper_size_get_ppd_name (GtkPaperSize *size)
567 return size->ppd_name;
569 return paper_names + size->info->ppd_name;
574 * gtk_paper_size_get_width:
575 * @size: a #GtkPaperSize object
576 * @unit: the unit for the return value
578 * Gets the paper width of the #GtkPaperSize, in
581 * Return value: the paper width
586 gtk_paper_size_get_width (GtkPaperSize *size,
589 return _gtk_print_convert_from_mm (size->width, unit);
593 * gtk_paper_size_get_height:
594 * @size: a #GtkPaperSize object
595 * @unit: the unit for the return value
597 * Gets the paper height of the #GtkPaperSize, in
600 * Return value: the paper height
605 gtk_paper_size_get_height (GtkPaperSize *size,
608 return _gtk_print_convert_from_mm (size->height, unit);
612 * gtk_paper_size_is_custom:
613 * @size: a #GtkPaperSize object
615 * Returns %TRUE if @size is not a standard paper size.
617 * Return value: whether @size is a custom paper size.
620 gtk_paper_size_is_custom (GtkPaperSize *size)
622 return size->is_custom;
626 * gtk_paper_size_set_size:
627 * @size: a custom #GtkPaperSize object
628 * @width: the new width in units of @unit
629 * @height: the new height in units of @unit
630 * @unit: the unit for @width and @height
632 * Changes the dimensions of a @size to @width x @height.
637 gtk_paper_size_set_size (GtkPaperSize *size,
642 g_return_if_fail (size != NULL);
643 g_return_if_fail (size->is_custom);
645 size->width = _gtk_print_convert_to_mm (width, unit);
646 size->height = _gtk_print_convert_to_mm (height, unit);
649 #define NL_PAPER_GET(x) \
650 ((union { char *string; unsigned int word; })nl_langinfo(x)).word
653 * gtk_paper_size_get_default:
655 * Returns the name of the default paper size, which
656 * depends on the current locale.
658 * Return value: the name of the default paper size. The string
659 * is owned by GTK+ and should not be modified.
664 gtk_paper_size_get_default (void)
666 char *locale, *freeme = NULL;
667 const char *paper_size;
669 #if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
671 int width = NL_PAPER_GET (_NL_PAPER_WIDTH);
672 int height = NL_PAPER_GET (_NL_PAPER_HEIGHT);
674 if (width == 210 && height == 297)
675 return GTK_PAPER_NAME_A4;
677 if (width == 216 && height == 279)
678 return GTK_PAPER_NAME_LETTER;
683 freeme = locale = g_win32_getlocale ();
684 #elif defined(LC_PAPER)
685 locale = setlocale(LC_PAPER, NULL);
687 locale = setlocale(LC_MESSAGES, NULL);
691 return GTK_PAPER_NAME_A4;
694 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/territory_language_information.html
696 if (g_regex_match_simple("[^_.@]{2,3}_(BZ|CA|CL|CO|CR|GT|MX|NI|PA|PH|PR|SV|US|VE)",
697 locale, G_REGEX_ANCHORED, G_REGEX_MATCH_ANCHORED))
698 paper_size = GTK_PAPER_NAME_LETTER;
700 paper_size = GTK_PAPER_NAME_A4;
706 /* These get the default margins used for the paper size. Its
707 * larger than most printers margins, so that it will be within
708 * the imageble area on any printer.
710 * I've taken the actual values used from the OSX page setup dialog.
711 * I'm not sure exactly where they got these values for, but might
712 * correspond to this (from ghostscript docs):
714 * All DeskJets have 0.5 inches (1.27cm) of unprintable bottom margin,
715 * due to the mechanical arrangement used to grab the paper. Side margins
716 * are approximately 0.25 inches (0.64cm) for U.S. letter paper, and 0.15
717 * inches (0.38cm) for A4.
721 * gtk_paper_size_get_default_top_margin:
722 * @size: a #GtkPaperSize object
723 * @unit: the unit for the return value
725 * Gets the default top margin for the #GtkPaperSize.
727 * Return value: the default top margin
732 gtk_paper_size_get_default_top_margin (GtkPaperSize *size,
737 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
738 return _gtk_print_convert_from_mm (margin, unit);
742 * gtk_paper_size_get_default_bottom_margin:
743 * @size: a #GtkPaperSize object
744 * @unit: the unit for the return value
746 * Gets the default bottom margin for the #GtkPaperSize.
748 * Return value: the default bottom margin
753 gtk_paper_size_get_default_bottom_margin (GtkPaperSize *size,
759 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
761 name = gtk_paper_size_get_name (size);
762 if (strcmp (name, "na_letter") == 0 ||
763 strcmp (name, "na_legal") == 0 ||
764 strcmp (name, "iso_a4") == 0)
765 margin = _gtk_print_convert_to_mm (0.56, GTK_UNIT_INCH);
767 return _gtk_print_convert_from_mm (margin, unit);
771 * gtk_paper_size_get_default_left_margin:
772 * @size: a #GtkPaperSize object
773 * @unit: the unit for the return value
775 * Gets the default left margin for the #GtkPaperSize.
777 * Return value: the default left margin
782 gtk_paper_size_get_default_left_margin (GtkPaperSize *size,
787 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
788 return _gtk_print_convert_from_mm (margin, unit);
792 * gtk_paper_size_get_default_right_margin:
793 * @size: a #GtkPaperSize object
794 * @unit: the unit for the return value
796 * Gets the default right margin for the #GtkPaperSize.
798 * Return value: the default right margin
803 gtk_paper_size_get_default_right_margin (GtkPaperSize *size,
808 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
809 return _gtk_print_convert_from_mm (margin, unit);
813 * gtk_paper_size_new_from_key_file:
814 * @key_file: the #GKeyFile to retrieve the papersize from
815 * @group_name: the name ofthe group in the key file to read,
816 * or %NULL to read the first group
817 * @error: (allow-none): return location for an error, or %NULL
819 * Reads a paper size from the group @group_name in the key file
822 * Returns: a new #GtkPaperSize object with the restored
823 * paper size, or %NULL if an error occurred
828 gtk_paper_size_new_from_key_file (GKeyFile *key_file,
829 const gchar *group_name,
832 GtkPaperSize *paper_size = NULL;
834 gchar *ppd_name = NULL;
835 gchar *display_name = NULL;
836 gchar *freeme = NULL;
837 gdouble width, height;
840 g_return_val_if_fail (key_file != NULL, NULL);
843 group_name = freeme = g_key_file_get_start_group (key_file);
844 if (!group_name || !g_key_file_has_group (key_file, group_name))
846 g_set_error_literal (error,
848 GTK_PRINT_ERROR_INVALID_FILE,
849 _("Not a valid page setup file"));
853 #define GET_DOUBLE(kf, group, name, v) \
854 v = g_key_file_get_double (kf, group, name, &err); \
857 g_propagate_error (error, err);\
861 GET_DOUBLE (key_file, group_name, "Width", width);
862 GET_DOUBLE (key_file, group_name, "Height", height);
866 name = g_key_file_get_string (key_file, group_name,
868 ppd_name = g_key_file_get_string (key_file, group_name,
870 display_name = g_key_file_get_string (key_file, group_name,
871 "DisplayName", NULL);
872 /* Fallback for old ~/.gtk-custom-paper entries */
874 display_name = g_strdup (name);
876 if (ppd_name != NULL)
877 paper_size = gtk_paper_size_new_from_ppd (ppd_name,
879 _gtk_print_convert_from_mm (width, GTK_UNIT_POINTS),
880 _gtk_print_convert_from_mm (height, GTK_UNIT_POINTS));
881 else if (name != NULL)
882 paper_size = gtk_paper_size_new_custom (name, display_name,
883 width, height, GTK_UNIT_MM);
886 g_set_error_literal (error,
888 GTK_PRINT_ERROR_INVALID_FILE,
889 _("Not a valid page setup file"));
893 g_assert (paper_size != NULL);
898 g_free (display_name);
905 * gtk_paper_size_to_key_file:
906 * @size: a #GtkPaperSize
907 * @key_file: the #GKeyFile to save the paper size to
908 * @group_name: the group to add the settings to in @key_file
910 * This function adds the paper size from @size to @key_file.
915 gtk_paper_size_to_key_file (GtkPaperSize *size,
917 const gchar *group_name)
919 const char *name, *ppd_name, *display_name;
921 g_return_if_fail (size != NULL);
922 g_return_if_fail (key_file != NULL);
924 name = gtk_paper_size_get_name (size);
925 display_name = gtk_paper_size_get_display_name (size);
926 ppd_name = gtk_paper_size_get_ppd_name (size);
928 if (ppd_name != NULL)
929 g_key_file_set_string (key_file, group_name,
930 "PPDName", ppd_name);
932 g_key_file_set_string (key_file, group_name,
936 g_key_file_set_string (key_file, group_name,
937 "DisplayName", display_name);
939 g_key_file_set_double (key_file, group_name,
940 "Width", gtk_paper_size_get_width (size, GTK_UNIT_MM));
941 g_key_file_set_double (key_file, group_name,
942 "Height", gtk_paper_size_get_height (size, GTK_UNIT_MM));