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 size = g_slice_new0 (GtkPaperSize);
236 size->height = height;
237 size->name = short_name;
238 size->display_name = g_strdup (short_name);
239 if (strncmp (short_name, "custom", 6) == 0)
240 size->is_custom = TRUE;
244 info = lookup_paper_info (name);
246 size = gtk_paper_size_new_from_info (info);
249 g_warning ("Unknown paper size %s\n", name);
250 size = g_slice_new0 (GtkPaperSize);
251 size->name = g_strdup (name);
252 size->display_name = g_strdup (name);
253 /* Default to A4 size */
263 * gtk_paper_size_new_from_ppd:
264 * @ppd_name: a PPD paper name
265 * @ppd_display_name: the corresponding human-readable name
266 * @width: the paper width, in points
267 * @height: the paper height in points
269 * Creates a new #GtkPaperSize object by using
272 * If @ppd_name is not a recognized PPD paper name,
273 * @ppd_display_name, @width and @height are used to
274 * construct a custom #GtkPaperSize object.
276 * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
282 gtk_paper_size_new_from_ppd (const gchar *ppd_name,
283 const gchar *ppd_display_name,
288 const char *lookup_ppd_name;
293 lookup_ppd_name = ppd_name;
296 /* Strip out Traverse suffix in matching. */
297 if (g_str_has_suffix (ppd_name, ".Transverse"))
299 lookup_ppd_name = freeme =
300 g_strndup (ppd_name, strlen (ppd_name) - strlen (".Transverse"));
303 for (i = 0; i < G_N_ELEMENTS(standard_names_offsets); i++)
305 if (standard_names_offsets[i].ppd_name != -1 &&
306 strcmp (paper_names + standard_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
308 size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
313 for (i = 0; i < G_N_ELEMENTS(extra_ppd_names_offsets); i++)
315 if (strcmp (paper_names + extra_ppd_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
317 size = gtk_paper_size_new (paper_names + extra_ppd_names_offsets[i].standard_name);
322 name = g_strconcat ("ppd_", ppd_name, NULL);
323 size = gtk_paper_size_new_custom (name, ppd_display_name, width, height, GTK_UNIT_POINTS);
328 if (size->info == NULL ||
329 size->info->ppd_name == -1 ||
330 strcmp (paper_names + size->info->ppd_name, ppd_name) != 0)
331 size->ppd_name = g_strdup (ppd_name);
339 * gtk_paper_size_new_custom:
340 * @name: the paper name
341 * @display_name: the human-readable name
342 * @width: the paper width, in units of @unit
343 * @height: the paper height, in units of @unit
344 * @unit: the unit for @width and @height
346 * Creates a new #GtkPaperSize object with the
349 * Return value: a new #GtkPaperSize object, use gtk_paper_size_free()
355 gtk_paper_size_new_custom (const gchar *name,
356 const gchar *display_name,
362 g_return_val_if_fail (name != NULL, NULL);
363 g_return_val_if_fail (unit != GTK_UNIT_PIXEL, NULL);
365 size = g_slice_new0 (GtkPaperSize);
367 size->name = g_strdup (name);
368 size->display_name = g_strdup (display_name);
369 size->is_custom = TRUE;
371 size->width = _gtk_print_convert_to_mm (width, unit);
372 size->height = _gtk_print_convert_to_mm (height, unit);
378 * gtk_paper_size_copy:
379 * @other: a #GtkPaperSize
381 * Copies an existing #GtkPaperSize.
383 * Return value: a copy of @other
388 gtk_paper_size_copy (GtkPaperSize *other)
392 size = g_slice_new0 (GtkPaperSize);
394 size->info = other->info;
396 size->name = g_strdup (other->name);
397 if (other->display_name)
398 size->display_name = g_strdup (other->display_name);
400 size->ppd_name = g_strdup (other->ppd_name);
402 size->width = other->width;
403 size->height = other->height;
404 size->is_custom = other->is_custom;
410 * gtk_paper_size_free:
411 * @size: a #GtkPaperSize
413 * Free the given #GtkPaperSize object.
418 gtk_paper_size_free (GtkPaperSize *size)
421 g_free (size->display_name);
422 g_free (size->ppd_name);
424 g_slice_free (GtkPaperSize, size);
428 * gtk_paper_size_is_equal:
429 * @size1: a #GtkPaperSize object
430 * @size2: another #GtkPaperSize object
432 * Compares two #GtkPaperSize objects.
434 * Return value: %TRUE, if @size1 and @size2
435 * represent the same paper size
440 gtk_paper_size_is_equal (GtkPaperSize *size1,
443 if (size1->info != NULL && size2->info != NULL)
444 return size1->info == size2->info;
446 return strcmp (gtk_paper_size_get_name (size1),
447 gtk_paper_size_get_name (size2)) == 0;
450 GList * _gtk_load_custom_papers (void);
453 * gtk_paper_size_get_paper_sizes:
454 * @include_custom: whether to include custom paper sizes
455 * as defined in the page setup dialog
457 * Creates a list of known paper sizes.
459 * Return value: (element-type GtkPaperSize) (transfer full): a newly allocated list of newly
460 * allocated #GtkPaperSize objects
465 gtk_paper_size_get_paper_sizes (gboolean include_custom)
469 #ifdef G_OS_UNIX /* _gtk_load_custom_papers() only on Unix so far */
472 GList *page_setups, *l;
474 page_setups = _gtk_load_custom_papers ();
475 for (l = page_setups; l != NULL; l = l->next)
477 GtkPageSetup *setup = (GtkPageSetup *) l->data;
480 size = gtk_page_setup_get_paper_size (setup);
481 list = g_list_prepend (list, gtk_paper_size_copy (size));
484 g_list_foreach (page_setups, (GFunc) g_object_unref, NULL);
485 g_list_free (page_setups);
488 for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); ++i)
492 size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
493 list = g_list_prepend (list, size);
496 return g_list_reverse (list);
501 * gtk_paper_size_get_name:
502 * @size: a #GtkPaperSize object
504 * Gets the name of the #GtkPaperSize.
506 * Return value: the name of @size
510 G_CONST_RETURN gchar *
511 gtk_paper_size_get_name (GtkPaperSize *size)
515 g_assert (size->info != NULL);
516 return paper_names + size->info->name;
520 * gtk_paper_size_get_display_name:
521 * @size: a #GtkPaperSize object
523 * Gets the human-readable name of the #GtkPaperSize.
525 * Return value: the human-readable name of @size
529 G_CONST_RETURN gchar *
530 gtk_paper_size_get_display_name (GtkPaperSize *size)
532 const gchar *display_name;
534 if (size->display_name)
535 return size->display_name;
537 g_assert (size->info != NULL);
539 display_name = paper_names + size->info->display_name;
540 return g_dpgettext2 (GETTEXT_PACKAGE, "paper size", display_name);
544 * gtk_paper_size_get_ppd_name:
545 * @size: a #GtkPaperSize object
547 * Gets the PPD name of the #GtkPaperSize, which
550 * Return value: the PPD name of @size
554 G_CONST_RETURN gchar *
555 gtk_paper_size_get_ppd_name (GtkPaperSize *size)
558 return size->ppd_name;
560 return paper_names + size->info->ppd_name;
565 * gtk_paper_size_get_width:
566 * @size: a #GtkPaperSize object
567 * @unit: the unit for the return value
569 * Gets the paper width of the #GtkPaperSize, in
572 * Return value: the paper width
577 gtk_paper_size_get_width (GtkPaperSize *size,
580 return _gtk_print_convert_from_mm (size->width, unit);
584 * gtk_paper_size_get_height:
585 * @size: a #GtkPaperSize object
586 * @unit: the unit for the return value
588 * Gets the paper height of the #GtkPaperSize, in
591 * Return value: the paper height
596 gtk_paper_size_get_height (GtkPaperSize *size,
599 return _gtk_print_convert_from_mm (size->height, unit);
603 * gtk_paper_size_is_custom:
604 * @size: a #GtkPaperSize object
606 * Returns %TRUE if @size is not a standard paper size.
608 * Return value: whether @size is a custom paper size.
611 gtk_paper_size_is_custom (GtkPaperSize *size)
613 return size->is_custom;
617 * gtk_paper_size_set_size:
618 * @size: a custom #GtkPaperSize object
619 * @width: the new width in units of @unit
620 * @height: the new height in units of @unit
621 * @unit: the unit for @width and @height
623 * Changes the dimensions of a @size to @width x @height.
628 gtk_paper_size_set_size (GtkPaperSize *size,
633 g_return_if_fail (size != NULL);
634 g_return_if_fail (size->is_custom);
636 size->width = _gtk_print_convert_to_mm (width, unit);
637 size->height = _gtk_print_convert_to_mm (height, unit);
640 #define NL_PAPER_GET(x) \
641 ((union { char *string; unsigned int word; })nl_langinfo(x)).word
644 * gtk_paper_size_get_default:
646 * Returns the name of the default paper size, which
647 * depends on the current locale.
649 * Return value: the name of the default paper size. The string
650 * is owned by GTK+ and should not be modified.
654 G_CONST_RETURN gchar *
655 gtk_paper_size_get_default (void)
657 char *locale, *freeme = NULL;
658 const char *paper_size;
660 #if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
662 int width = NL_PAPER_GET (_NL_PAPER_WIDTH);
663 int height = NL_PAPER_GET (_NL_PAPER_HEIGHT);
665 if (width == 210 && height == 297)
666 return GTK_PAPER_NAME_A4;
668 if (width == 216 && height == 279)
669 return GTK_PAPER_NAME_LETTER;
674 freeme = locale = g_win32_getlocale ();
675 #elif defined(LC_PAPER)
676 locale = setlocale(LC_PAPER, NULL);
678 locale = setlocale(LC_MESSAGES, NULL);
682 return GTK_PAPER_NAME_A4;
685 * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/territory_language_information.html
687 if (g_regex_match_simple("[^_.@]{2,3}_(BZ|CA|CL|CO|CR|GT|MX|NI|PA|PH|PR|SV|US|VE)",
688 locale, G_REGEX_ANCHORED, G_REGEX_MATCH_ANCHORED))
689 paper_size = GTK_PAPER_NAME_LETTER;
691 paper_size = GTK_PAPER_NAME_A4;
697 /* These get the default margins used for the paper size. Its
698 * larger than most printers margins, so that it will be within
699 * the imageble area on any printer.
701 * I've taken the actual values used from the OSX page setup dialog.
702 * I'm not sure exactly where they got these values for, but might
703 * correspond to this (from ghostscript docs):
705 * All DeskJets have 0.5 inches (1.27cm) of unprintable bottom margin,
706 * due to the mechanical arrangement used to grab the paper. Side margins
707 * are approximately 0.25 inches (0.64cm) for U.S. letter paper, and 0.15
708 * inches (0.38cm) for A4.
712 * gtk_paper_size_get_default_top_margin:
713 * @size: a #GtkPaperSize object
714 * @unit: the unit for the return value
716 * Gets the default top margin for the #GtkPaperSize.
718 * Return value: the default top margin
723 gtk_paper_size_get_default_top_margin (GtkPaperSize *size,
728 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
729 return _gtk_print_convert_from_mm (margin, unit);
733 * gtk_paper_size_get_default_bottom_margin:
734 * @size: a #GtkPaperSize object
735 * @unit: the unit for the return value
737 * Gets the default bottom margin for the #GtkPaperSize.
739 * Return value: the default bottom margin
744 gtk_paper_size_get_default_bottom_margin (GtkPaperSize *size,
750 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
752 name = gtk_paper_size_get_name (size);
753 if (strcmp (name, "na_letter") == 0 ||
754 strcmp (name, "na_legal") == 0 ||
755 strcmp (name, "iso_a4") == 0)
756 margin = _gtk_print_convert_to_mm (0.56, GTK_UNIT_INCH);
758 return _gtk_print_convert_from_mm (margin, unit);
762 * gtk_paper_size_get_default_left_margin:
763 * @size: a #GtkPaperSize object
764 * @unit: the unit for the return value
766 * Gets the default left margin for the #GtkPaperSize.
768 * Return value: the default left margin
773 gtk_paper_size_get_default_left_margin (GtkPaperSize *size,
778 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
779 return _gtk_print_convert_from_mm (margin, unit);
783 * gtk_paper_size_get_default_right_margin:
784 * @size: a #GtkPaperSize object
785 * @unit: the unit for the return value
787 * Gets the default right margin for the #GtkPaperSize.
789 * Return value: the default right margin
794 gtk_paper_size_get_default_right_margin (GtkPaperSize *size,
799 margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
800 return _gtk_print_convert_from_mm (margin, unit);
804 * gtk_paper_size_new_from_key_file:
805 * @key_file: the #GKeyFile to retrieve the papersize from
806 * @group_name: the name ofthe group in the key file to read,
807 * or %NULL to read the first group
808 * @error: (allow-none): return location for an error, or %NULL
810 * Reads a paper size from the group @group_name in the key file
813 * Returns: a new #GtkPaperSize object with the restored
814 * paper size, or %NULL if an error occurred.
819 gtk_paper_size_new_from_key_file (GKeyFile *key_file,
820 const gchar *group_name,
823 GtkPaperSize *paper_size = NULL;
824 char *name = NULL, *ppd_name = NULL, *display_name = NULL, *freeme = NULL;
825 gdouble width, height;
826 gboolean retval = TRUE;
829 g_return_val_if_fail (key_file != NULL, NULL);
832 group_name = freeme = g_key_file_get_start_group (key_file);
833 if (!group_name || !g_key_file_has_group (key_file, group_name))
835 g_set_error_literal (error,
837 GTK_PRINT_ERROR_INVALID_FILE,
838 _("Not a valid page setup file"));
843 #define GET_DOUBLE(kf, group, name, v) \
844 v = g_key_file_get_double (kf, group, name, &err); \
847 g_propagate_error (error, err);\
852 GET_DOUBLE (key_file, group_name, "Width", width);
853 GET_DOUBLE (key_file, group_name, "Height", height);
857 name = g_key_file_get_string (key_file, group_name,
859 ppd_name = g_key_file_get_string (key_file, group_name,
861 display_name = g_key_file_get_string (key_file, group_name,
862 "DisplayName", NULL);
863 /* Fallback for old ~/.gtk-custom-paper entries */
865 display_name = g_strdup (name);
867 if (ppd_name != NULL)
868 paper_size = gtk_paper_size_new_from_ppd (ppd_name,
870 _gtk_print_convert_from_mm (width, GTK_UNIT_POINTS),
871 _gtk_print_convert_from_mm (height, GTK_UNIT_POINTS));
872 else if (name != NULL)
873 paper_size = gtk_paper_size_new_custom (name, display_name,
874 width, height, GTK_UNIT_MM);
877 g_set_error_literal (error,
879 GTK_PRINT_ERROR_INVALID_FILE,
880 _("Not a valid page setup file"));
884 g_assert (paper_size != NULL);
889 g_free (display_name);
896 * gtk_paper_size_to_key_file:
897 * @size: a #GtkPaperSize
898 * @key_file: the #GKeyFile to save the paper size to
899 * @group_name: the group to add the settings to in @key_file
901 * This function adds the paper size from @size to @key_file.
906 gtk_paper_size_to_key_file (GtkPaperSize *size,
908 const gchar *group_name)
910 const char *name, *ppd_name, *display_name;
912 g_return_if_fail (size != NULL);
913 g_return_if_fail (key_file != NULL);
915 name = gtk_paper_size_get_name (size);
916 display_name = gtk_paper_size_get_display_name (size);
917 ppd_name = gtk_paper_size_get_ppd_name (size);
919 if (ppd_name != NULL)
920 g_key_file_set_string (key_file, group_name,
921 "PPDName", ppd_name);
923 g_key_file_set_string (key_file, group_name,
927 g_key_file_set_string (key_file, group_name,
928 "DisplayName", display_name);
930 g_key_file_set_double (key_file, group_name,
931 "Width", gtk_paper_size_get_width (size, GTK_UNIT_MM));
932 g_key_file_set_double (key_file, group_name,
933 "Height", gtk_paper_size_get_height (size, GTK_UNIT_MM));