g_retrieve.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 the internal _pl_g_set_font method, which is called
  16. when the font_name, font_size, and textangle fields of the current
  17. drawing state have been filled in. It retrieves the specified font, and
  18. fills in the true_font_name, true_font_size, font_type, typeface_index,
  19. font_index, font_is_iso8858_1, font_ascent, font_descent, and
  20. font_cap_height fields of the drawing state.
  21. _pl_g_set_font is invoked by _API_alabel() and _API_flabelwidth(), and
  22. also by _API_fontname(), _API_fontsize(), and _API_textangle() (only
  23. because those three functions in the API must return a font size).
  24. _pl_g_set_font does the following, in order:
  25. 1. If font_name is the name of a supported Hershey font, i.e., one in
  26. libplot's database, it fills in the fields itself.
  27. 2. If the font name is the name of a supported font of a non-Hershey
  28. type (i.e. PS/PCL/Stick), it _tentatively_ fills in the fields. If the
  29. font name doesn't appear in libplot's internal database, it simply sets
  30. the font_type to `PL_F_OTHER'.
  31. 3a. If the font name isn't the name of a supported Hershey font, it
  32. invokes the Plotter-specific `retrieve_font' method. In most Plotters
  33. this simply returns true. (See the stub _pl_g_retrieve_font() below.)
  34. But in principle it may modify the fields arbitrarily, perhaps consult
  35. an external database, etc.
  36. 3b. If retrieve_font returns false, a default Hershey font is
  37. substituted, and the fields are filled in. This feature is used e.g. by
  38. Fig Plotters, which support any of the 35 PS fonts in libplot's
  39. database, except when the user frame -> device frame map is anisotropic
  40. (i.e. non-uniform). It is also used by X Drawable Plotters, which may
  41. or may not have a PS font available, and in fact may or may not have any
  42. given font of type `PL_F_OTHER' (see above) available. */
  43. #include "sys-defines.h"
  44. #include "extern.h"
  45. #include "g_her_metr.h"
  46. #define MAP_HERSHEY_UNITS_TO_USER_UNITS(size, drawstate) \
  47. ((size)*(drawstate->true_font_size)/(HERSHEY_EM))
  48. /* forward references */
  49. static bool _match_hershey_font (plDrawState *drawstate);
  50. static bool _match_ps_font (plDrawState *drawstate);
  51. static bool _match_pcl_font (plDrawState *drawstate);
  52. static bool _match_stick_font (plDrawState *drawstate, bool have_extra_stick_fonts);
  53. void
  54. _pl_g_set_font (S___(Plotter *_plotter))
  55. {
  56. plDrawState *drawstate = _plotter->drawstate;
  57. plPlotterData *data = _plotter->data;
  58. const char *default_font_name;
  59. bool matched;
  60. /* try to match font name in our database */
  61. if (_match_hershey_font (drawstate))
  62. /* Matched a Hershey font name in our database, and all fields filled
  63. in definitively, so return without invoking Plotter-specific
  64. `retrieve_font', which knows nothing about Hershey fonts anyway */
  65. return;
  66. matched = false;
  67. /* Try to match the font name with the name of a PCL font or a PS font in
  68. the database, and tentatively fill in fields. But there is a
  69. namespace collision: "Courier" etc. and "Symbol" occur on both lists.
  70. Which list we search first depends on what type of Plotter this is. */
  71. if (data->pcl_before_ps)
  72. {
  73. /* search PCL font list first */
  74. if ((data->have_pcl_fonts
  75. && _match_pcl_font (drawstate))
  76. ||
  77. (data->have_ps_fonts
  78. && _match_ps_font (drawstate)))
  79. matched = true;
  80. }
  81. else
  82. {
  83. /* search PS font list first */
  84. if ((data->have_ps_fonts
  85. && _match_ps_font (drawstate))
  86. ||
  87. (data->have_pcl_fonts
  88. && _match_pcl_font (drawstate)))
  89. matched = true;
  90. }
  91. /* if unmatched, search through Stick font list too */
  92. if (matched == false && data->have_stick_fonts
  93. && _match_stick_font (drawstate,
  94. data->have_extra_stick_fonts ? true : false))
  95. matched = true;
  96. if (matched == false)
  97. /* fill in the only fields we can */
  98. {
  99. free ((char *)drawstate->true_font_name);
  100. drawstate->true_font_name =
  101. (const char *)_pl_xmalloc (strlen (drawstate->font_name) + 1);
  102. strcpy ((char *)drawstate->true_font_name, drawstate->font_name);
  103. drawstate->true_font_size = drawstate->font_size;
  104. drawstate->font_type = PL_F_OTHER;
  105. drawstate->typeface_index = 0;
  106. drawstate->font_index = 1;
  107. /* NOT filled in yet: font_is_iso8859_1, font_ascent, font_descent,
  108. and font_cap_height; they're left to Plotter-specific retrieval
  109. routine, if it can supply them (in the case of a Metafile Plotter,
  110. it surely can't). */
  111. }
  112. /* If we got here, font name isn't that of a Hershey font, and `matched'
  113. indicates whether or not it's listed in our font name database, as one
  114. of the types of font this Plotter can handle. So invoke low-level
  115. Plotter-specific retrieval routine. Do it even if we haven't matched
  116. the font, if this Plotter claims to be able to handle `other' fonts,
  117. i.e., ones not listed in our database. */
  118. if (matched || (!matched && data->have_other_fonts))
  119. /* try to invoke low-level Plotter-specific routine to finish the job
  120. of filling in fields, including Plotter-specific ones */
  121. {
  122. if (_plotter->retrieve_font (S___(_plotter)))
  123. /* all finished... */
  124. return;
  125. }
  126. /* Plotter-specific retrieval failed: it doesn't like the font name or
  127. other drawstate parameters (size, textangle, transformation matrix?) */
  128. /* Via a recursive call, try to retrieve default font for this Plotter
  129. type (if it's different from the one we just failed to retrieve;
  130. otherwise retrieve the default Hershey font). */
  131. switch (data->default_font_type)
  132. {
  133. case PL_F_POSTSCRIPT:
  134. default_font_name = PL_DEFAULT_POSTSCRIPT_FONT;
  135. break;
  136. case PL_F_PCL:
  137. default_font_name = PL_DEFAULT_PCL_FONT;
  138. break;
  139. case PL_F_STICK:
  140. default_font_name = PL_DEFAULT_STICK_FONT;
  141. break;
  142. case PL_F_HERSHEY: /* Hershey shouldn't happen */
  143. default:
  144. default_font_name = PL_DEFAULT_HERSHEY_FONT;
  145. break;
  146. /* N.B. No support yet for a Plotter-specific default font that is of
  147. PL_F_OTHER type, i.e., isn't listed in the libplot font database. */
  148. }
  149. if (strcmp (drawstate->font_name, default_font_name) == 0
  150. || strcmp (drawstate->true_font_name, default_font_name) == 0)
  151. /* default font is the one we just failed to retrieve: so use Hershey:
  152. internal and guaranteed to be available */
  153. default_font_name = PL_DEFAULT_HERSHEY_FONT;
  154. /* stash fontname we failed to retrieve; then do recursive call, turning
  155. off font warnings for the duration; restore fontname field */
  156. {
  157. const char *saved_font_name;
  158. bool saved_font_warning_issued;
  159. saved_font_name = drawstate->font_name;
  160. drawstate->font_name = default_font_name;
  161. saved_font_warning_issued = _plotter->data->font_warning_issued;
  162. _plotter->data->font_warning_issued = true; /* turn off warnings */
  163. _pl_g_set_font (S___(_plotter));
  164. _plotter->data->font_warning_issued = saved_font_warning_issued;
  165. drawstate->font_name = saved_font_name;
  166. }
  167. if (data->issue_font_warning && !_plotter->data->font_warning_issued)
  168. /* squawk */
  169. {
  170. char *buf;
  171. buf = (char *)_pl_xmalloc (strlen (drawstate->font_name) + strlen (drawstate->true_font_name) + 100);
  172. sprintf (buf, "cannot retrieve font \"%s\", using default \"%s\"",
  173. drawstate->font_name, drawstate->true_font_name);
  174. _plotter->warning (R___(_plotter) buf);
  175. free (buf);
  176. _plotter->data->font_warning_issued = true;
  177. }
  178. }
  179. static bool
  180. _match_hershey_font (plDrawState *drawstate)
  181. {
  182. int i;
  183. /* is font a Hershey font? */
  184. i = -1;
  185. while (_pl_g_hershey_font_info[++i].name)
  186. {
  187. if (_pl_g_hershey_font_info[i].visible) /* i.e. font not internal-only */
  188. if (strcasecmp (_pl_g_hershey_font_info[i].name,
  189. drawstate->font_name) == 0
  190. || (_pl_g_hershey_font_info[i].othername
  191. && strcasecmp (_pl_g_hershey_font_info[i].othername,
  192. drawstate->font_name) == 0))
  193. /* fill in fields */
  194. {
  195. free ((char *)drawstate->true_font_name);
  196. drawstate->true_font_name =
  197. (const char *)_pl_xmalloc (strlen (_pl_g_hershey_font_info[i].name) + 1);
  198. strcpy ((char *)drawstate->true_font_name, _pl_g_hershey_font_info[i].name);
  199. drawstate->true_font_size = drawstate->font_size;
  200. drawstate->font_type = PL_F_HERSHEY;
  201. drawstate->typeface_index = _pl_g_hershey_font_info[i].typeface_index;
  202. drawstate->font_index = _pl_g_hershey_font_info[i].font_index;
  203. drawstate->font_is_iso8859_1 = _pl_g_hershey_font_info[i].iso8859_1;
  204. /* N.B. this macro uses true_font_size */
  205. drawstate->font_cap_height =
  206. MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_CAPHEIGHT, drawstate);
  207. drawstate->font_ascent =
  208. MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_ASCENT, drawstate);
  209. drawstate->font_descent =
  210. MAP_HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_DESCENT, drawstate);
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. static bool
  217. _match_pcl_font (plDrawState *drawstate)
  218. {
  219. int i = -1;
  220. /* is font a PCL font in libplot's database? */
  221. while (_pl_g_pcl_font_info[++i].ps_name)
  222. {
  223. if (strcasecmp (_pl_g_pcl_font_info[i].ps_name,
  224. drawstate->font_name) == 0
  225. /* try alternative PS font name if any */
  226. || (_pl_g_pcl_font_info[i].ps_name_alt != NULL
  227. && strcasecmp (_pl_g_pcl_font_info[i].ps_name_alt,
  228. drawstate->font_name) == 0)
  229. /* try X font name too */
  230. || strcasecmp (_pl_g_pcl_font_info[i].x_name,
  231. drawstate->font_name) == 0)
  232. {
  233. free ((char *)drawstate->true_font_name);
  234. drawstate->true_font_name =
  235. (const char *)_pl_xmalloc (strlen (_pl_g_pcl_font_info[i].ps_name) + 1);
  236. strcpy ((char *)drawstate->true_font_name, _pl_g_pcl_font_info[i].ps_name);
  237. drawstate->true_font_size = drawstate->font_size;
  238. drawstate->font_type = PL_F_PCL;
  239. drawstate->typeface_index = _pl_g_pcl_font_info[i].typeface_index;
  240. drawstate->font_index = _pl_g_pcl_font_info[i].font_index;
  241. drawstate->font_is_iso8859_1 = _pl_g_pcl_font_info[i].iso8859_1;
  242. drawstate->font_ascent
  243. = drawstate->true_font_size
  244. * (double)(_pl_g_pcl_font_info[i].font_ascent)/1000.0;
  245. drawstate->font_descent
  246. = drawstate->true_font_size
  247. * (double)(_pl_g_pcl_font_info[i].font_descent)/1000.0;
  248. drawstate->font_cap_height
  249. = drawstate->true_font_size
  250. * (double)(_pl_g_pcl_font_info[i].font_cap_height)/1000.0;
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. static bool
  257. _match_ps_font (plDrawState *drawstate)
  258. {
  259. int i = -1;
  260. /* is font a PS font in libplot's database ? */
  261. while (_pl_g_ps_font_info[++i].ps_name)
  262. {
  263. if (strcasecmp (_pl_g_ps_font_info[i].ps_name,
  264. drawstate->font_name) == 0
  265. /* try alternative PS font name if any */
  266. || (_pl_g_ps_font_info[i].ps_name_alt != NULL
  267. && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt,
  268. drawstate->font_name) == 0)
  269. /* try 2nd alternative PS font name if any */
  270. || (_pl_g_ps_font_info[i].ps_name_alt2 != NULL
  271. && strcasecmp (_pl_g_ps_font_info[i].ps_name_alt2,
  272. drawstate->font_name) == 0)
  273. /* try X font name too */
  274. || strcasecmp (_pl_g_ps_font_info[i].x_name,
  275. drawstate->font_name) == 0
  276. /* try alternative X font name if any */
  277. || (_pl_g_ps_font_info[i].x_name_alt != NULL
  278. && strcasecmp (_pl_g_ps_font_info[i].x_name_alt,
  279. drawstate->font_name) == 0))
  280. {
  281. free ((char *)drawstate->true_font_name);
  282. drawstate->true_font_name =
  283. (const char *)_pl_xmalloc (strlen (_pl_g_ps_font_info[i].ps_name) + 1);
  284. strcpy ((char *)drawstate->true_font_name, _pl_g_ps_font_info[i].ps_name);
  285. drawstate->true_font_size = drawstate->font_size;
  286. drawstate->font_type = PL_F_POSTSCRIPT;
  287. drawstate->typeface_index = _pl_g_ps_font_info[i].typeface_index;
  288. drawstate->font_index = _pl_g_ps_font_info[i].font_index;
  289. drawstate->font_is_iso8859_1 = _pl_g_ps_font_info[i].iso8859_1;
  290. drawstate->font_ascent
  291. = drawstate->true_font_size
  292. * (double)(_pl_g_ps_font_info[i].font_ascent)/1000.0;
  293. drawstate->font_descent
  294. = drawstate->true_font_size
  295. * (double)(_pl_g_ps_font_info[i].font_descent)/1000.0;
  296. drawstate->font_cap_height
  297. = drawstate->true_font_size
  298. * (double)(_pl_g_ps_font_info[i].font_cap_height)/1000.0;
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. static bool
  305. _match_stick_font (plDrawState *drawstate, bool have_extra_stick_fonts)
  306. {
  307. int i = -1;
  308. /* is font a PCL font in libplot's database? */
  309. while (_pl_g_stick_font_info[++i].ps_name)
  310. {
  311. if (_pl_g_stick_font_info[i].basic == false
  312. && !have_extra_stick_fonts)
  313. /* not a basic Stick font, and this Plotter supports only the basic
  314. ones */
  315. continue;
  316. if (strcasecmp (_pl_g_stick_font_info[i].ps_name,
  317. drawstate->font_name) == 0)
  318. /* fill in fields */
  319. {
  320. free ((char *)drawstate->true_font_name);
  321. drawstate->true_font_name =
  322. (const char *)_pl_xmalloc (strlen (_pl_g_stick_font_info[i].ps_name) + 1);
  323. strcpy ((char *)drawstate->true_font_name, _pl_g_stick_font_info[i].ps_name);
  324. drawstate->true_font_size = drawstate->font_size;
  325. drawstate->true_font_size = drawstate->font_size;
  326. drawstate->true_font_size = drawstate->font_size;
  327. drawstate->font_type = PL_F_STICK;
  328. drawstate->typeface_index = _pl_g_stick_font_info[i].typeface_index;
  329. drawstate->font_index = _pl_g_stick_font_info[i].font_index;
  330. drawstate->font_is_iso8859_1 = _pl_g_stick_font_info[i].iso8859_1;
  331. drawstate->font_ascent
  332. = drawstate->true_font_size
  333. * (double)(_pl_g_stick_font_info[i].font_ascent)/1000.0;
  334. drawstate->font_descent
  335. = drawstate->true_font_size
  336. * (double)(_pl_g_stick_font_info[i].font_descent)/1000.0;
  337. drawstate->font_cap_height
  338. /* The `0.7' is undocumented HP magic; see comments in
  339. h_font.c and g_fontd2.c */
  340. = 0.7 * drawstate->true_font_size;
  341. return true;
  342. }
  343. }
  344. return false;
  345. }
  346. /* This is the generic version of the _retrieve_font method, which does
  347. nothing. Many types of Plotter use this, but some, e.g., FigPlotters
  348. and XDrawablePlotters (and X Plotters) override it. See
  349. _pl_f_retrieve_font() and _pl_x_retrieve_font(). */
  350. bool
  351. _pl_g_retrieve_font (S___(Plotter *_plotter))
  352. {
  353. return true;
  354. }