g_font.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 fontname, fontsize, and textangle methods, which
  16. are a GNU extension to libplot. They set drawing attributes: the name
  17. of the font used for text subsequent drawn on the graphics device, the
  18. size, and the text angle.
  19. The fontname, fontsize, and textangle methods return the fontsize in
  20. user units, as an aid to vertical positioning by the user. (The
  21. fontsize is normally taken to be a minimum vertical spacing between
  22. adjacent lines of text.)
  23. The return value may depend on the mapping from user coordinates to
  24. graphics device coordinates, and hence, e.g., on the arguments given to
  25. the space() method.
  26. Note that the size of the font may change when the rotation angle is
  27. changed, since some fonts may not be available at all rotation angles,
  28. so that a default font must be switched to. Also, not all font sizes
  29. may be available (there may need to be some size quantization). So the
  30. return value should always be checked. */
  31. #include "sys-defines.h"
  32. #include "extern.h"
  33. double
  34. _API_ffontname (R___(Plotter *_plotter) const char *s)
  35. {
  36. char *font_name;
  37. if (!_plotter->data->open)
  38. {
  39. _plotter->error (R___(_plotter)
  40. "ffontname: invalid operation");
  41. return -1;
  42. }
  43. /* Null pointer resets to default. (N.B. we don't look at the font_name
  44. field in _default_drawstate, because it's a dummy.) */
  45. if ((s == NULL) || (*s == '\0') || !strcmp(s, "(null)"))
  46. switch (_plotter->data->default_font_type)
  47. {
  48. case PL_F_HERSHEY:
  49. default:
  50. s = PL_DEFAULT_HERSHEY_FONT;
  51. break;
  52. case PL_F_POSTSCRIPT:
  53. s = PL_DEFAULT_POSTSCRIPT_FONT;
  54. break;
  55. case PL_F_PCL:
  56. s = PL_DEFAULT_PCL_FONT;
  57. break;
  58. case PL_F_STICK:
  59. s = PL_DEFAULT_STICK_FONT;
  60. break;
  61. }
  62. /* save new font name */
  63. free ((char *)_plotter->drawstate->font_name);
  64. font_name = (char *)_pl_xmalloc (strlen (s) + 1);
  65. strcpy (font_name, s);
  66. _plotter->drawstate->font_name = font_name;
  67. /* retrieve font and metrics; compute `true' font size (may differ) */
  68. _pl_g_set_font (S___(_plotter));
  69. /* return value is size in user units */
  70. return _plotter->drawstate->true_font_size;
  71. }
  72. double
  73. _API_ffontsize (R___(Plotter *_plotter) double size)
  74. {
  75. if (!_plotter->data->open)
  76. {
  77. _plotter->error (R___(_plotter)
  78. "ffontsize: invalid operation");
  79. return -1;
  80. }
  81. if (size < 0.0) /* reset to default */
  82. {
  83. size = _plotter->drawstate->default_font_size;
  84. _plotter->drawstate->font_size_is_default = true;
  85. }
  86. else
  87. _plotter->drawstate->font_size_is_default = false;
  88. /* set the new nominal size in the drawing state */
  89. _plotter->drawstate->font_size = size;
  90. /* retrieve font and metrics; compute `true' font size (may differ) */
  91. _pl_g_set_font (S___(_plotter));
  92. /* flag fontsize as having been invoked (so that fsetmatrix will no
  93. longer automatically adjust the font size to a reasonable value) */
  94. _plotter->data->fontsize_invoked = true;
  95. /* return quantized user-specified font size */
  96. return _plotter->drawstate->true_font_size;
  97. }
  98. double
  99. _API_ftextangle (R___(Plotter *_plotter) double angle)
  100. {
  101. if (!_plotter->data->open)
  102. {
  103. _plotter->error (R___(_plotter)
  104. "ftextangle: invalid operation");
  105. return -1;
  106. }
  107. /* save new rotation angle */
  108. _plotter->drawstate->text_rotation = angle;
  109. /* retrieve font and metrics; compute `true' font size (may differ) */
  110. _pl_g_set_font (S___(_plotter));
  111. /* return quantized user-specified font size */
  112. return _plotter->drawstate->true_font_size;
  113. }
  114. /* Below are four rather silly Plotter methods that are an undocumented
  115. part of the libplot/libplotter API. Each returns a pointer to the head
  116. of a font database in g_fontdb.c, so that an application program that is
  117. too nosy for its own good can pry out font information.
  118. These should be replaced by a properly crafted API for querying font
  119. names, font metrics, etc. */
  120. void *
  121. _pl_get_hershey_font_info (S___(Plotter *_plotter))
  122. {
  123. return (void *)_pl_g_hershey_font_info;
  124. }
  125. void *
  126. _pl_get_ps_font_info (S___(Plotter *_plotter))
  127. {
  128. return (void *)_pl_g_ps_font_info;
  129. }
  130. void *
  131. _pl_get_pcl_font_info (S___(Plotter *_plotter))
  132. {
  133. return (void *)_pl_g_pcl_font_info;
  134. }
  135. void *
  136. _pl_get_stick_font_info (S___(Plotter *_plotter))
  137. {
  138. return (void *)_pl_g_stick_font_info;
  139. }