s_text.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 prints a single-font, single-font-size label. When this is called,
  16. the current point is on the intended baseline of the label. */
  17. /* This version is for SVGPlotters. We use the ISO-Latin-1 encoding, so <
  18. > & and left-quote and double-quote are the only characters we need to
  19. escape. */
  20. #include "sys-defines.h"
  21. #include "extern.h"
  22. /* maximum length we support */
  23. #define PL_MAX_SVG_STRING_LEN 256
  24. /* The fixed value we specify for the font-size parameter, when any font is
  25. retrieved, in terms of `px'. (We now scale as needed by choosing an
  26. appropriate transformation matrix.) According to the SVG Authoring
  27. Guide, a `px' means simply a user-space unit, but some SVG renderers
  28. (e.g., in Firefox) get confused if it's smaller than 1.0 or so, and
  29. return absurdly scaled fonts. Maybe they think px stands for pixels?
  30. :-) */
  31. #define PL_SVG_FONT_SIZE_IN_PX 20.0
  32. /* forward references */
  33. static void write_svg_text_style (plOutbuf *page, const plDrawState *drawstate, int h_just, int v_just);
  34. typedef struct
  35. {
  36. char c;
  37. const char *s;
  38. }
  39. plCharEscape;
  40. #define NUM_SVG_CHAR_ESCAPES 5
  41. static const plCharEscape _svg_char_escapes[NUM_SVG_CHAR_ESCAPES] =
  42. {
  43. {'\'', "apos"},
  44. {'"', "quot"},
  45. {'&', "amp"},
  46. {'<', "lt"},
  47. {'>', "gt"}
  48. };
  49. #define MAX_SVG_CHAR_ESCAPE_LEN 4 /* i.e., length of "apos" or "quot" */
  50. /* SVG horizontal alignment styles, i.e., text-anchor attribute, indexed by
  51. internal number (left/center/right) */
  52. static const char * const svg_horizontal_alignment_style[PL_NUM_HORIZ_JUST_TYPES] =
  53. { "start", "middle", "end" };
  54. /* SVG vertical alignment styles, i.e., alignment-baseline attribute,
  55. indexed by internal number (top/half/base/bottom/cap) */
  56. static const char * const svg_vertical_alignment_style[PL_NUM_VERT_JUST_TYPES] =
  57. { "text-before-edge", "central", "alphabetic", "text-after-edge", "hanging" };
  58. /* This version of the paint_text_string method, for SVG Plotters, supports
  59. each of libplot's possible vertical justifications (see the list
  60. immediately above). However, only the `baseline' justification is
  61. currently used. That's because in s_defplot.c we set the Plotter
  62. parameter `have_vertical_justification' to false. Too many SVG
  63. renderers don't support the SVG alignment-baseline attribute; e.g.,
  64. Firefox 1.5 doesn't. So it's best for libplot to do its own vertical
  65. positioning of text strings. */
  66. double
  67. _pl_s_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
  68. {
  69. const unsigned char *sp = s;
  70. unsigned char *t, *tp;
  71. int i, n = 0;
  72. double local_matrix[6];
  73. double angle = _plotter->drawstate->text_rotation;
  74. /* replace certain printable ASCII characters by entities */
  75. tp = t = (unsigned char *)_pl_xmalloc ((2 + MAX_SVG_CHAR_ESCAPE_LEN) * strlen ((const char *)s) + 1);
  76. while (*sp && n < PL_MAX_SVG_STRING_LEN)
  77. {
  78. bool matched;
  79. int i;
  80. matched = false;
  81. for (i = 0; i < NUM_SVG_CHAR_ESCAPES; i++)
  82. {
  83. if (*sp == (unsigned char)_svg_char_escapes[i].c)
  84. {
  85. matched = true;
  86. break;
  87. }
  88. }
  89. if (matched)
  90. {
  91. *tp++ = (unsigned char)'&';
  92. strcpy ((char *)tp, _svg_char_escapes[i].s);
  93. tp += strlen (_svg_char_escapes[i].s);
  94. *tp++ = (unsigned char)';';
  95. }
  96. else
  97. *tp++ = *sp;
  98. sp++;
  99. n++;
  100. }
  101. *tp = '\0';
  102. sprintf (_plotter->data->page->point, "<text ");
  103. _update_buffer (_plotter->data->page);
  104. /* CTM equals CTM_local * CTM_base, if matrix multiplication is defined
  105. as in PS and libplot. (Which is the opposite of the SVG convention,
  106. since SVG documentation uses column vectors instead of row vectors, so
  107. that the CTM is effectively transposed. Although SVG's matrix()
  108. construct uses PS order for the six matrix elements... go figure.)
  109. Here CTM_local rotates by the libplot's text angle parameter, and
  110. translates to the correct position. And CTM_base is libplot's current
  111. user_to_ndc transformation matrix. We separate them because we use
  112. the CTM of the first-plotted object on the page as the page's global
  113. transformation matrix, and if that object happens to be a text object,
  114. we'd like it to simply to be the current user_to_ndc transformation
  115. matrix, i.e. not to include irrelevancies such as the text position
  116. and angle.
  117. Sigh... If only things were so simple. SVG's native coordinate frame,
  118. which libplot's user coordinates must ultimately be mapped to,
  119. unfortunately uses a flipped-y convention, unlike PS and libplot.
  120. (The global flipping of y, relative to libplot's NDC coordinates, is
  121. accomplished by a scale(1,-1) that's placed at the head of the SVG
  122. file; see s_output.c.) This flipping has a special effect on the
  123. drawing of text strings, though no other libplot primitive. For
  124. everything to work out when drawing a text string, we must precede the
  125. sequence of transformations leading from user coordinates to native
  126. SVG coordinates by an initial scale(1,-1). CTM_local, as defined
  127. above, must have two elements sign-flipped (see below). Trust me. */
  128. local_matrix[0] = cos (M_PI * angle / 180.0);
  129. local_matrix[1] = sin (M_PI * angle / 180.0);
  130. local_matrix[2] = -sin (M_PI * angle / 180.0) * (-1); /* SEE ABOVE */
  131. local_matrix[3] = cos (M_PI * angle / 180.0) * (-1); /* SEE ABOVE */
  132. /* since we now specify a fixed font-size, equal to PL_SVG_FONT_SIZE_IN_PX
  133. (see below), rather than specifying a font size equal to the
  134. font size in user units, we must here scale the text string to
  135. the right size */
  136. for (i = 0; i < 4; i++)
  137. local_matrix[i] *= (_plotter->drawstate->font_size
  138. / PL_SVG_FONT_SIZE_IN_PX);
  139. local_matrix[4] = _plotter->drawstate->pos.x;
  140. local_matrix[5] = _plotter->drawstate->pos.y;
  141. _pl_s_set_matrix (R___(_plotter) local_matrix);
  142. write_svg_text_style (_plotter->data->page, _plotter->drawstate,
  143. h_just, v_just);
  144. sprintf (_plotter->data->page->point, ">");
  145. _update_buffer (_plotter->data->page);
  146. sprintf (_plotter->data->page->point, "%s",
  147. (char *)t);
  148. _update_buffer (_plotter->data->page);
  149. sprintf (_plotter->data->page->point, "</text>\n");
  150. _update_buffer (_plotter->data->page);
  151. free (t);
  152. return _plotter->get_text_width (R___(_plotter) s);
  153. }
  154. static void
  155. write_svg_text_style (plOutbuf *page, const plDrawState *drawstate, int h_just, int v_just)
  156. {
  157. const char *ps_name, *css_family, *css_generic_family; /* last may be NULL */
  158. const char *css_style, *css_weight, *css_stretch;
  159. bool css_family_is_ps_name;
  160. char color_buf[8]; /* enough room for "#ffffff", incl. NUL */
  161. /* extract official PS font name, and CSS font specification, from master
  162. table of PS [or PCL] fonts, in g_fontdb.c */
  163. switch (drawstate->font_type)
  164. {
  165. int master_font_index;
  166. case PL_F_POSTSCRIPT:
  167. master_font_index =
  168. (_pl_g_ps_typeface_info[drawstate->typeface_index].fonts)[drawstate->font_index];
  169. ps_name = _pl_g_ps_font_info[master_font_index].ps_name;
  170. css_family = _pl_g_ps_font_info[master_font_index].css_family;
  171. css_generic_family = _pl_g_ps_font_info[master_font_index].css_generic_family;
  172. css_style = _pl_g_ps_font_info[master_font_index].css_style;
  173. css_weight = _pl_g_ps_font_info[master_font_index].css_weight;
  174. css_stretch = _pl_g_ps_font_info[master_font_index].css_stretch;
  175. /* flag this font as used */
  176. page->ps_font_used[master_font_index] = true;
  177. break;
  178. case PL_F_PCL:
  179. master_font_index =
  180. (_pl_g_pcl_typeface_info[drawstate->typeface_index].fonts)[drawstate->font_index];
  181. ps_name = _pl_g_pcl_font_info[master_font_index].ps_name;
  182. css_family = _pl_g_pcl_font_info[master_font_index].css_family;
  183. css_generic_family = _pl_g_pcl_font_info[master_font_index].css_generic_family;
  184. css_style = _pl_g_pcl_font_info[master_font_index].css_style;
  185. css_weight = _pl_g_pcl_font_info[master_font_index].css_weight;
  186. css_stretch = _pl_g_pcl_font_info[master_font_index].css_stretch;
  187. /* flag this font as used */
  188. page->pcl_font_used[master_font_index] = true;
  189. break;
  190. default: /* shouldn't happen */
  191. return;
  192. break;
  193. }
  194. if (strcmp (ps_name, css_family) == 0)
  195. /* no need to specify both */
  196. css_family_is_ps_name = true;
  197. else
  198. css_family_is_ps_name = false;
  199. /* N.B. In each of the following four sprintf()'s, we should apparently
  200. enclose css_family in single quotes, at least if it contains a space.
  201. But doing so would cause the SVG renderer in `display', which is part
  202. of the ImageMagick package, to reject the emitted SVG file. */
  203. if (css_generic_family)
  204. {
  205. if (css_family_is_ps_name)
  206. sprintf (page->point, "font-family=\"%s,%s\" ",
  207. css_family, css_generic_family);
  208. else
  209. sprintf (page->point, "font-family=\"%s,%s,%s\" ",
  210. ps_name, css_family, css_generic_family);
  211. }
  212. else
  213. {
  214. if (css_family_is_ps_name)
  215. sprintf (page->point, "font-family=\"%s\" ",
  216. css_family);
  217. else
  218. sprintf (page->point, "font-family=\"%s,%s\" ",
  219. ps_name, css_family);
  220. }
  221. _update_buffer (page);
  222. if (strcmp (css_style, "normal") != 0) /* not default */
  223. {
  224. sprintf (page->point, "font-style=\"%s\" ",
  225. css_style);
  226. _update_buffer (page);
  227. }
  228. if (strcmp (css_weight, "normal") != 0) /* not default */
  229. {
  230. sprintf (page->point, "font-weight=\"%s\" ",
  231. css_weight);
  232. _update_buffer (page);
  233. }
  234. if (strcmp (css_stretch, "normal") != 0) /* not default */
  235. {
  236. sprintf (page->point, "font-stretch=\"%s\" ",
  237. css_stretch);
  238. _update_buffer (page);
  239. }
  240. sprintf (page->point, "font-size=\"%.5gpx\" ",
  241. /* see comments above for why we don't simply specify
  242. drawstate->font_size here */
  243. PL_SVG_FONT_SIZE_IN_PX);
  244. _update_buffer (page);
  245. if (h_just != PL_JUST_LEFT) /* not default */
  246. {
  247. sprintf (page->point, "text-anchor=\"%s\" ",
  248. svg_horizontal_alignment_style[h_just]);
  249. _update_buffer (page);
  250. }
  251. if (v_just != PL_JUST_BASE) /* not default */
  252. {
  253. sprintf (page->point, "alignment-baseline=\"%s\" ",
  254. svg_vertical_alignment_style[v_just]);
  255. _update_buffer (page);
  256. }
  257. /* currently, we never draw character outlines; we only fill */
  258. sprintf (page->point, "stroke=\"none\" ");
  259. _update_buffer (page);
  260. if (drawstate->pen_type)
  261. /* according to libplot convention, text should be filled, and since
  262. SVG's default filling is "none", we must say so */
  263. {
  264. sprintf (page->point, "fill=\"%s\" ",
  265. _libplot_color_to_svg_color (drawstate->fgcolor, color_buf));
  266. _update_buffer (page);
  267. }
  268. }