g_pagetype.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains a utility function, _set_page_type(), which searches
  16. the database of known pagetypes for the one specified in the PAGESIZE
  17. parameter, sets page-related data (dimensions, size of viewport) in the
  18. Plotter, and also passes back the viewport offset vector.
  19. The PAGESIZE parameter should be of a form resembling "letter", or "a4",
  20. or "letter,xoffset=-1.2in", or "a4,yoffset=0.5cm,xoffset = 2mm". */
  21. #include "sys-defines.h"
  22. #include "extern.h"
  23. #include "g_pagetype.h"
  24. /* forward references */
  25. static bool parse_page_type (const char *pagesize, const plPageData **pagedata, double *xoffset, double *yoffset, double *xorigin, double *yorigin, double *xsize, double *ysize);
  26. static bool string_to_inches (const char *string, double *inches);
  27. void
  28. _set_page_type (plPlotterData *data)
  29. {
  30. const char *pagesize;
  31. const plPageData *pagedata;
  32. double viewport_xoffset, viewport_yoffset;
  33. double viewport_xorigin, viewport_yorigin;
  34. double viewport_xsize, viewport_ysize;
  35. /* examine user-specified value for PAGESIZE parameter, or the default
  36. value if we can't parse the user-specified value */
  37. pagesize = (const char *)_get_plot_param (data, "PAGESIZE");
  38. if (!parse_page_type (pagesize, &pagedata,
  39. &viewport_xoffset, &viewport_yoffset,
  40. &viewport_xorigin, &viewport_yorigin,
  41. &viewport_xsize, &viewport_ysize))
  42. {
  43. pagesize = (const char *)_get_default_plot_param ("PAGESIZE");
  44. parse_page_type (pagesize, &pagedata,
  45. &viewport_xoffset, &viewport_yoffset,
  46. &viewport_xorigin, &viewport_yorigin,
  47. &viewport_xsize, &viewport_ysize);
  48. }
  49. /* set page data in Plotter */
  50. data->page_data = pagedata;
  51. data->viewport_xoffset = viewport_xoffset;
  52. data->viewport_yoffset = viewport_yoffset;
  53. data->viewport_xorigin = viewport_xorigin;
  54. data->viewport_yorigin = viewport_yorigin;
  55. data->viewport_xsize = viewport_xsize;
  56. data->viewport_ysize = viewport_ysize;
  57. }
  58. static bool
  59. parse_page_type (const char *pagesize, const plPageData **pagedata, double *xoffset, double *yoffset, double *xorigin, double *yorigin, double *xsize, double *ysize)
  60. {
  61. const plPageData *local_pagedata = _pagedata;
  62. char *viewport_pagesize, *first, *next;
  63. char xoffset_s[32], yoffset_s[32]; /* each field should have length <=31 */
  64. char xorigin_s[32], yorigin_s[32];
  65. char xsize_s[32], ysize_s[32];
  66. bool anotherfield, success;
  67. bool got_xoffset = false, got_yoffset = false;
  68. bool got_xorigin = false, got_yorigin = false;
  69. bool got_xsize = false, got_ysize = false;
  70. int i;
  71. viewport_pagesize = (char *)_pl_xmalloc (strlen (pagesize) + 1);
  72. strcpy (viewport_pagesize, pagesize);
  73. first = viewport_pagesize;
  74. next = strchr (viewport_pagesize, (int)',');
  75. if (next)
  76. {
  77. anotherfield = true;
  78. *next = '\0';
  79. next++;
  80. }
  81. else
  82. anotherfield = false;
  83. /* try to match page type to a page type on our list */
  84. success = false;
  85. for (i = 0; i < PL_NUM_PAGESIZES; i++, local_pagedata++)
  86. if (strcasecmp (local_pagedata->name, viewport_pagesize) == 0
  87. ||
  88. (local_pagedata->alt_name
  89. && strcasecmp (local_pagedata->alt_name, viewport_pagesize) == 0))
  90. {
  91. success = true;
  92. break;
  93. }
  94. if (success)
  95. /* matched page type, at least */
  96. {
  97. /* pass back pointer to page data via pointer */
  98. *pagedata = local_pagedata;
  99. while (anotherfield && *next) /* i.e. while there's a nonempty field */
  100. {
  101. first = next;
  102. next = strchr (next, (int)',');
  103. if (next)
  104. {
  105. anotherfield = true;
  106. *next = '\0';
  107. next++;
  108. }
  109. else
  110. anotherfield = false;
  111. /* try to parse field */
  112. if (sscanf (first, "xoffset = %31s", xoffset_s) == 1)
  113. got_xoffset = true;
  114. else if (sscanf (first, "yoffset = %31s", yoffset_s) == 1)
  115. got_yoffset = true;
  116. else if (sscanf (first, "xorigin = %31s", xorigin_s) == 1)
  117. got_xorigin = true;
  118. else if (sscanf (first, "yorigin = %31s", yorigin_s) == 1)
  119. got_yorigin = true;
  120. else if (sscanf (first, "xsize = %31s", xsize_s) == 1)
  121. got_xsize = true;
  122. else if (sscanf (first, "ysize = %31s", ysize_s) == 1)
  123. got_ysize = true;
  124. }
  125. /* pass back viewport size-and-location data via pointers */
  126. {
  127. double viewport_xsize, viewport_ysize;
  128. double viewport_xorigin, viewport_yorigin;
  129. double viewport_xoffset, viewport_yoffset;
  130. /* xsize, ysize default to this page type's default */
  131. if (!(got_xsize && string_to_inches (xsize_s, &viewport_xsize)))
  132. viewport_xsize = local_pagedata->default_viewport_size;
  133. if (!(got_ysize && string_to_inches (ysize_s, &viewport_ysize)))
  134. viewport_ysize = local_pagedata->default_viewport_size;
  135. /* xorigin, yorigin default to whatever is needed to center the
  136. viewport on the page */
  137. if (!(got_xorigin && string_to_inches (xorigin_s, &viewport_xorigin)))
  138. viewport_xorigin = 0.5 * (local_pagedata->xsize - viewport_xsize);
  139. if (!(got_yorigin && string_to_inches (yorigin_s, &viewport_yorigin)))
  140. viewport_yorigin = 0.5 * (local_pagedata->ysize - viewport_ysize);
  141. /* xoffset, yoffset default to zero */
  142. if (!(got_xoffset && string_to_inches (xoffset_s, &viewport_xoffset)))
  143. viewport_xoffset = 0.0;
  144. if (!(got_yoffset && string_to_inches (yoffset_s, &viewport_yoffset)))
  145. viewport_yoffset = 0.0;
  146. *xsize = viewport_xsize;
  147. *ysize = viewport_ysize;
  148. *xorigin = viewport_xorigin;
  149. *yorigin = viewport_yorigin;
  150. *xoffset = viewport_xoffset;
  151. *yoffset = viewport_yoffset;
  152. }
  153. }
  154. free (viewport_pagesize);
  155. /* indicate whether we were able to match the page type */
  156. return success;
  157. }
  158. /* convert a string representing a distance measurement to inches; units
  159. `in', `cm', `mm' are supported */
  160. static bool
  161. string_to_inches (const char *string, double *inches)
  162. {
  163. double val;
  164. char s[4];
  165. if (sscanf (string, "%lf %3s" , &val, s) == 2)
  166. {
  167. if (strlen (s) > 2)
  168. return false;
  169. if (strcmp (s, "in") == 0)
  170. {
  171. *inches = val;
  172. return true;
  173. }
  174. else if (strcmp (s, "cm") == 0)
  175. {
  176. *inches = val / 2.54;
  177. return true;
  178. }
  179. else if (strcmp (s, "mm") == 0)
  180. {
  181. *inches = val / 25.4;
  182. return true;
  183. }
  184. }
  185. return false;
  186. }