pappl icon indicating copy to clipboard operation
pappl copied to clipboard

API function which determines A4/Letter default for user's location

Open tillkamppeter opened this issue 3 years ago • 2 comments

In the PostScript Printer Application I have added the following function to determine by the system's locale environment variables whether the standard page size at the user's location is A4 or Letter:

static const char *ps_default_paper_size()
{
  static char result[128];            // Resulting default paper size
  char        *val;                   // Paper size/locale value
  int         i;
  const char  * const lc_env_vars[] = // Environment variables with suitable
  {                                   // locale information
    "LC_PAPER",
    "LC_CTYPE",
    "LC_ALL",
    "LANG",
  };


  memset(result, 0, sizeof(result));
  for (i = 0; i < sizeof(lc_env_vars) / sizeof(lc_env_vars[0]); i ++)
  {
    if ((val = getenv(lc_env_vars[i])) == NULL)
      continue;
    if (!strcmp(val, "C") ||
	!strcmp(val, "POSIX"))
      continue;
    if (!strcmp(val, "en") ||
	!strncmp(val, "en.", 3) ||
	!strncmp(val, "en_US", 5) ||
	!strncmp(val, "en_CA", 5) ||
	!strncmp(val, "fr_CA", 5))
      // These are the only locales that will default to "Letter" size...
      strcpy(result, "Letter");
    else
      // Rest of the world is A4
      strcpy(result, "A4");
    if (result[0])
      break;
  }
  return (result[0] ? result : NULL);
}

It returns "A4" or "Letter" if it can determine suitable locale information and NULL otherwise.

I use it when creating a new print queue and the default page size for the printer is A4 or Letter and the printer supports both A4 and Letter page sizes to get the actually more suitable size as the default. I use following code for that (Note that the PostScript Printer Application gets the printer capabilities from the printer's PPD file):

 if ((val = ps_default_paper_size()) == NULL ||
	(option = ppdFindOption(ppd, "PageSize")) == NULL ||
	((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL &&
	 strcasecmp(choice->choice, "Letter") &&
	 strcasecmp(choice->choice, "A4")) ||
	(choice = ppdFindChoice(option, val)) == NULL)
      choice = ppdFindMarkedChoice(ppd, "PageSize");

choice is the default to be used for the "PageSize" option (and so also for the size in media_default). If we can determine a standard size from the locale and if the default of the "PageSize" option in the PPD is A4 or Letterwe switch to A4 or Letter according to the locale, otherwise we stay with whatever is the default in the PPD.

My feature request here is to add a function like ps_default_paper_size() to the public API of PAPPL (call it something like papplDefaultPaperSize() then) and perhaps also another function which does something similar to my example code but more generally usable (applying to a pappl_media_col_t record or to media_default in a pappl_pr_driver_data_t data structure). It would be great when an A4/Letter default gets auto-adjusted when creating a new queue,

tillkamppeter avatar May 02 '21 17:05 tillkamppeter

@tillkamppeter I will consider this, however there are many environments where this defaulting is insufficient. I'd also want to support libpaper on platforms that have it...

michaelrsweet avatar May 03 '21 11:05 michaelrsweet

es_CL default is Letter. Legal is another format used for, well, legal documents here. [An] formats are foraneous here.

a-opazo avatar Apr 24 '22 15:04 a-opazo

[master c08ee0f] Add papplLocGetDefaultMediaSizeName API (Issue #167)

michaelrsweet avatar Oct 10 '22 02:10 michaelrsweet

OK, thanks, but Brazil has A4 as default paper size. This is my personal experience from being there. I know it at least from the states of Minas Gerais and Espirito Santo, but I assume that it is A4 in the whole country.

tillkamppeter avatar Oct 10 '22 06:10 tillkamppeter

I would even say that the whole South America uses A4, except es_CL as reported above ...

tillkamppeter avatar Oct 10 '22 06:10 tillkamppeter

@tillkamppeter Thanks, I was able to find a more authoritative source than OOo and Windows: https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/territory_information.html

[master ef6f1f8] Update list of US Letter countries.

michaelrsweet avatar Oct 10 '22 13:10 michaelrsweet