c_text.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #include "sys-defines.h"
  16. #include "extern.h"
  17. #define GOOD_PRINTABLE_ASCII(c) ((c >= 0x20) && (c <= 0x7E))
  18. /* CGM horizontal alignment styles, indexed by internal number
  19. (left/center/right) */
  20. static const int cgm_horizontal_alignment_style[PL_NUM_HORIZ_JUST_TYPES] =
  21. { CGM_ALIGN_LEFT, CGM_ALIGN_CENTER, CGM_ALIGN_RIGHT };
  22. /* corresponding strings, as used in the text encoding */
  23. static const char * const cgm_horizontal_alignment_style_string[PL_NUM_HORIZ_JUST_TYPES] =
  24. { "left", "ctr", "right" };
  25. /* CGM vertical alignment styles, indexed by internal number
  26. (top/half/base/bottom/cap) */
  27. static const int cgm_vertical_alignment_style[PL_NUM_VERT_JUST_TYPES] =
  28. { CGM_ALIGN_TOP, CGM_ALIGN_HALF, CGM_ALIGN_BASE, CGM_ALIGN_BOTTOM, CGM_ALIGN_CAP };
  29. /* corresponding strings, as used in the text encoding */
  30. static const char * const cgm_vertical_alignment_style_string[PL_NUM_VERT_JUST_TYPES] =
  31. { "top", "half", "base", "bottom", "cap" };
  32. /* This prints a single-font, single-font-size label. */
  33. double
  34. _pl_c_paint_text_string (R___(Plotter *_plotter) const unsigned char *s, int h_just, int v_just)
  35. {
  36. int master_font_index, desired_cgm_font_id;
  37. double theta, costheta, sintheta;
  38. double user_text_transformation_matrix[6];
  39. double text_transformation_matrix[6];
  40. int desired_char_base_vector_x, desired_char_base_vector_y;
  41. int desired_char_up_vector_x, desired_char_up_vector_y;
  42. double relative_cap_height, user_cap_height;
  43. double up_vector_x, up_vector_y, cap_height;
  44. double base_vector_x, base_vector_y, base_width;
  45. int desired_char_height, desired_base_width;
  46. bool font_is_symbol, need_lower_half, need_upper_half;
  47. bool set_lower_half_charset = false, set_upper_half_charset = false;
  48. int lower_half_charset = 0, upper_half_charset = 0; /* dummy values */
  49. const unsigned char *t;
  50. double width;
  51. int desired_cgm_h_alignment, desired_cgm_v_alignment;
  52. int byte_count, data_byte_count, data_len;
  53. /* sanity check */
  54. if (_plotter->drawstate->font_type != PL_F_POSTSCRIPT)
  55. return 0.0;
  56. /* if empty string, nothing to do */
  57. if (*s == (unsigned char)'\0')
  58. return 0.0;
  59. /* Many CGM interpreters can't handle zero font size (i.e., zero
  60. `character height'). [The RALCGM viewer actually crashes!]
  61. So bail if the font size is zero. */
  62. if (_plotter->drawstate->true_font_size == 0.0)
  63. return 0.0;
  64. /* set CGM text color */
  65. _pl_c_set_pen_color (R___(_plotter) CGM_OBJECT_TEXT);
  66. /* compute index of font in master table of PS fonts, in g_fontdb.c */
  67. master_font_index =
  68. (_pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts)[_plotter->drawstate->font_index];
  69. /* flag current PS font as used on this page */
  70. _plotter->data->page->ps_font_used[master_font_index] = true;
  71. /* synchronize CGM's font with our notion of current font, by setting the
  72. CGM font index (in range 1..35; as explained in g_fontdb.c, the
  73. traditional `Adobe 13' will be placed in slots 1..13) */
  74. desired_cgm_font_id = _pl_g_ps_font_to_cgm_font_id[master_font_index];
  75. if (_plotter->cgm_font_id != desired_cgm_font_id)
  76. /* emit "TEXT FONT INDEX" command */
  77. {
  78. data_len = 2; /* 2 bytes per index */
  79. byte_count = data_byte_count = 0;
  80. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  81. CGM_ATTRIBUTE_ELEMENT, 10,
  82. data_len, &byte_count,
  83. "TEXTFONTINDEX");
  84. _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
  85. /* index value in range 1..35, not 0..34 */
  86. desired_cgm_font_id + 1,
  87. data_len, &data_byte_count, &byte_count);
  88. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  89. &byte_count);
  90. /* update CGM font id */
  91. _plotter->cgm_font_id = desired_cgm_font_id;
  92. }
  93. /* label rotation angle in radians, in user frame */
  94. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  95. sintheta = sin (theta);
  96. costheta = cos (theta);
  97. /* this transformation matrix rotates, and translates: it maps (0,0) to
  98. the origin of the string, in user coordinates */
  99. user_text_transformation_matrix[0] = costheta;
  100. user_text_transformation_matrix[1] = sintheta;
  101. user_text_transformation_matrix[2] = - sintheta;
  102. user_text_transformation_matrix[3] = costheta;
  103. user_text_transformation_matrix[4] = _plotter->drawstate->pos.x;
  104. user_text_transformation_matrix[5] = _plotter->drawstate->pos.y;
  105. /* Construct a temporary matrix that rotates, translates, and then maps
  106. to device coordinates. This matrix transforms from a frame in which
  107. nominal character sizes are roughly 1 unit in the horizontal and
  108. vertical directions, to device coordinates. */
  109. _matrix_product (user_text_transformation_matrix,
  110. _plotter->drawstate->transform.m,
  111. text_transformation_matrix);
  112. /* Character base and up vectors, in the device frame, are what vectors
  113. (1,0) and (0,1) in the user frame are mapped to. According to CGM
  114. documentation, their overall normalization isn't important, but the
  115. ratio between their lengths affects the aspect ratio of the font. The
  116. `4000' factor is arbitrary; but it gives good quantization to integers. */
  117. {
  118. double base_x = text_transformation_matrix[0];
  119. double base_y = text_transformation_matrix[1];
  120. double up_x = text_transformation_matrix[2];
  121. double up_y = text_transformation_matrix[3];
  122. double base_len = sqrt (base_x * base_x + base_y * base_y);
  123. double up_len = sqrt (up_x * up_x + up_y * up_y);
  124. double max_len = DMAX(base_len, up_len);
  125. if (max_len != 0.0)
  126. {
  127. base_x /= max_len;
  128. base_y /= max_len;
  129. up_x /= max_len;
  130. up_y /= max_len;
  131. }
  132. #define QUANTIZATION_FACTOR 4000
  133. desired_char_base_vector_x = IROUND(QUANTIZATION_FACTOR * base_x);
  134. desired_char_base_vector_y = IROUND(QUANTIZATION_FACTOR * base_y);
  135. desired_char_up_vector_x = IROUND(QUANTIZATION_FACTOR * up_x);
  136. desired_char_up_vector_y = IROUND(QUANTIZATION_FACTOR * up_y);
  137. }
  138. if (_plotter->cgm_char_base_vector_x != desired_char_base_vector_x
  139. || _plotter->cgm_char_base_vector_y != desired_char_base_vector_y
  140. || _plotter->cgm_char_up_vector_x != desired_char_up_vector_x
  141. || _plotter->cgm_char_up_vector_y != desired_char_up_vector_y)
  142. /* emit CHARACTER ORIENTATION command */
  143. {
  144. data_len = 4 * CGM_BINARY_BYTES_PER_INTEGER; /* args: 4 integers */
  145. byte_count = data_byte_count = 0;
  146. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  147. CGM_ATTRIBUTE_ELEMENT, 16,
  148. data_len, &byte_count,
  149. "CHARORI");
  150. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  151. desired_char_up_vector_x,
  152. data_len, &data_byte_count, &byte_count);
  153. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  154. desired_char_up_vector_y,
  155. data_len, &data_byte_count, &byte_count);
  156. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  157. desired_char_base_vector_x,
  158. data_len, &data_byte_count, &byte_count);
  159. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  160. desired_char_base_vector_y,
  161. data_len, &data_byte_count, &byte_count);
  162. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  163. &byte_count);
  164. /* update char and base vectors */
  165. _plotter->cgm_char_base_vector_x = desired_char_base_vector_x;
  166. _plotter->cgm_char_base_vector_y = desired_char_base_vector_y;
  167. _plotter->cgm_char_up_vector_x = desired_char_up_vector_x;
  168. _plotter->cgm_char_up_vector_y = desired_char_up_vector_y;
  169. }
  170. /* cap height as fraction of font height */
  171. relative_cap_height =
  172. _pl_g_ps_font_info[master_font_index].font_cap_height / 1000.0;
  173. /* cap height in user frame */
  174. user_cap_height = relative_cap_height * _plotter->drawstate->true_font_size;
  175. /* true up vector (pointing up to cap height level) in device frame */
  176. up_vector_x = user_cap_height * text_transformation_matrix[2];
  177. up_vector_y = user_cap_height * text_transformation_matrix[3];
  178. /* length of up vector in device frame, i.e. desired cap height */
  179. cap_height = sqrt (up_vector_x * up_vector_x + up_vector_y * up_vector_y);
  180. /* in CGM, `character height' means cap height, measured along up vector */
  181. desired_char_height = IROUND(cap_height);
  182. if (_plotter->cgm_char_height != desired_char_height)
  183. /* emit CHARACTER HEIGHT command */
  184. {
  185. data_len = CGM_BINARY_BYTES_PER_INTEGER; /* args: 1 integer */
  186. byte_count = data_byte_count = 0;
  187. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  188. CGM_ATTRIBUTE_ELEMENT, 15,
  189. data_len, &byte_count,
  190. "CHARHEIGHT");
  191. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  192. desired_char_height,
  193. data_len, &data_byte_count, &byte_count);
  194. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  195. &byte_count);
  196. /* update character height */
  197. _plotter->cgm_char_height = desired_char_height;
  198. }
  199. /* is label to be rendered in Symbol font (which has its own charsets)? */
  200. if (strcmp (_pl_g_ps_font_info[master_font_index].ps_name, "Symbol") == 0)
  201. font_is_symbol = true;
  202. else
  203. font_is_symbol = false;
  204. /* will we be using lower, upper halves of font? */
  205. need_lower_half = need_upper_half = false;
  206. for (t = s; *t != (unsigned char)'\0'; t++)
  207. {
  208. if ((*t) <= 127)
  209. need_lower_half = true;
  210. else
  211. need_upper_half = true;
  212. }
  213. /* Do we need to switch lower/upper CGM charsets? Our indexing of
  214. charsets (see c_defplot.c) is: 1=lower half ISO-Latin-1, 2=upper half
  215. ISO-Latin-1, 3=lower half Symbol, 4=upper half Symbol. */
  216. if (font_is_symbol)
  217. /* may need to switch to CGM's Symbol charset(s) */
  218. {
  219. if (need_lower_half && _plotter->cgm_charset_lower != 3)
  220. {
  221. set_lower_half_charset = true;
  222. lower_half_charset = 3;
  223. }
  224. if (need_upper_half && _plotter->cgm_charset_upper != 4)
  225. {
  226. set_upper_half_charset = true;
  227. upper_half_charset = 4;
  228. }
  229. }
  230. else
  231. /* font is ISO-Latin-1, for CGM purposes; may need to switch charset(s) */
  232. {
  233. if (need_lower_half && _plotter->cgm_charset_lower != 1)
  234. {
  235. set_lower_half_charset = true;
  236. lower_half_charset = 1;
  237. }
  238. if (need_upper_half && _plotter->cgm_charset_upper != 2)
  239. {
  240. set_upper_half_charset = true;
  241. upper_half_charset = 2;
  242. }
  243. }
  244. if (set_lower_half_charset)
  245. /* emit SET CHARACTER SET INDEX command */
  246. {
  247. data_len = 2; /* 2 bytes per index */
  248. byte_count = data_byte_count = 0;
  249. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  250. CGM_ATTRIBUTE_ELEMENT, 19,
  251. data_len, &byte_count,
  252. "CHARSETINDEX");
  253. _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
  254. lower_half_charset,
  255. data_len, &data_byte_count, &byte_count);
  256. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  257. &byte_count);
  258. /* update our knowledge of CGM's lower-half charset */
  259. _plotter->cgm_charset_lower = lower_half_charset;
  260. }
  261. if (set_upper_half_charset)
  262. /* emit SET ALTERNATE CHARACTER SET INDEX command */
  263. {
  264. data_len = 2; /* 2 bytes per index */
  265. byte_count = data_byte_count = 0;
  266. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  267. CGM_ATTRIBUTE_ELEMENT, 20,
  268. data_len, &byte_count,
  269. "ALTCHARSETINDEX");
  270. _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
  271. upper_half_charset,
  272. data_len, &data_byte_count, &byte_count);
  273. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  274. &byte_count);
  275. /* update our knowledge of CGM's upper-half charset */
  276. _plotter->cgm_charset_upper = upper_half_charset;
  277. }
  278. /* update CGM text alignment if necessary */
  279. desired_cgm_h_alignment = cgm_horizontal_alignment_style[h_just];
  280. desired_cgm_v_alignment = cgm_vertical_alignment_style[v_just];
  281. if (_plotter->cgm_horizontal_text_alignment != desired_cgm_h_alignment
  282. || _plotter->cgm_vertical_text_alignment != desired_cgm_v_alignment)
  283. /* emit "TEXT ALIGNMENT" command (args = 2 enums, 2 reals) */
  284. {
  285. const char *desired_cgm_h_alignment_string, *desired_cgm_v_alignment_string;
  286. desired_cgm_h_alignment_string =
  287. cgm_horizontal_alignment_style_string[h_just];
  288. desired_cgm_v_alignment_string =
  289. cgm_vertical_alignment_style_string[v_just];
  290. data_len = 2 * 2 + 2 * 4; /* 2 bytes per enum, 4 bytes per real */
  291. byte_count = data_byte_count = 0;
  292. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  293. CGM_ATTRIBUTE_ELEMENT, 18,
  294. data_len, &byte_count,
  295. "TEXTALIGN");
  296. _cgm_emit_enum (_plotter->data->page, false, _plotter->cgm_encoding,
  297. desired_cgm_h_alignment,
  298. data_len, &data_byte_count, &byte_count,
  299. desired_cgm_h_alignment_string);
  300. _cgm_emit_enum (_plotter->data->page, false, _plotter->cgm_encoding,
  301. desired_cgm_v_alignment,
  302. data_len, &data_byte_count, &byte_count,
  303. desired_cgm_v_alignment_string);
  304. _cgm_emit_real_fixed_point (_plotter->data->page, false, _plotter->cgm_encoding,
  305. 0.0,
  306. data_len, &data_byte_count, &byte_count);
  307. _cgm_emit_real_fixed_point (_plotter->data->page, false, _plotter->cgm_encoding,
  308. 0.0,
  309. data_len, &data_byte_count, &byte_count);
  310. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  311. &byte_count);
  312. /* update our knowledge of text alignment */
  313. _plotter->cgm_horizontal_text_alignment = desired_cgm_h_alignment;
  314. _plotter->cgm_vertical_text_alignment = desired_cgm_v_alignment;
  315. }
  316. if (_plotter->cgm_max_version >= 3)
  317. /* can specify type of restricted text */
  318. {
  319. if (_plotter->cgm_restricted_text_type
  320. != CGM_RESTRICTED_TEXT_TYPE_BOXED_CAP)
  321. /* emit "RESTRICTED TEXT TYPE" command */
  322. {
  323. data_len = 2; /* 2 bytes per index */
  324. byte_count = data_byte_count = 0;
  325. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  326. CGM_ATTRIBUTE_ELEMENT, 42,
  327. data_len, &byte_count,
  328. "RESTRTEXTTYPE");
  329. _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
  330. CGM_RESTRICTED_TEXT_TYPE_BOXED_CAP,
  331. data_len, &data_byte_count, &byte_count);
  332. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  333. &byte_count);
  334. /* update restricted text type, and CGM version for this page */
  335. _plotter->cgm_restricted_text_type =
  336. CGM_RESTRICTED_TEXT_TYPE_BOXED_CAP;
  337. _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
  338. }
  339. }
  340. /* compute string width in user coordinates */
  341. width = _plotter->get_text_width (R___(_plotter) s);
  342. /* true base vector (pointing across to end of string) in device frame */
  343. base_vector_x = width * text_transformation_matrix[0];
  344. base_vector_y = width * text_transformation_matrix[1];
  345. /* length of base vector in device frame, i.e. width of string */
  346. base_width = sqrt (base_vector_x * base_vector_x + base_vector_y * base_vector_y);
  347. /* same (integer device coordinates) */
  348. desired_base_width = IROUND(base_width);
  349. /* emit "RESTRICTED TEXT" command (args= 2 ints, 1 point, 1 enum, 1 string)*/
  350. {
  351. int string_length, encoded_string_length;
  352. double xdev, ydev;
  353. int xdev_int, ydev_int;
  354. string_length = strlen ((const char *)s);
  355. encoded_string_length = CGM_BINARY_BYTES_PER_STRING(string_length);
  356. /* bytes per integer coordinate may vary; 2 bytes per enum */
  357. data_len = (4 * CGM_BINARY_BYTES_PER_INTEGER) + 2 + encoded_string_length;
  358. byte_count = data_byte_count = 0;
  359. xdev = XD(_plotter->drawstate->pos.x, _plotter->drawstate->pos.y);
  360. ydev = YD(_plotter->drawstate->pos.x, _plotter->drawstate->pos.y);
  361. xdev_int = IROUND(xdev);
  362. ydev_int = IROUND(ydev);
  363. _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
  364. CGM_GRAPHICAL_PRIMITIVE_ELEMENT, 5,
  365. data_len, &byte_count,
  366. "RESTRTEXT");
  367. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  368. desired_base_width,
  369. data_len, &data_byte_count, &byte_count);
  370. _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
  371. desired_char_height,
  372. data_len, &data_byte_count, &byte_count);
  373. _cgm_emit_point (_plotter->data->page, false, _plotter->cgm_encoding,
  374. xdev_int, ydev_int,
  375. data_len, &data_byte_count, &byte_count);
  376. _cgm_emit_enum (_plotter->data->page, false, _plotter->cgm_encoding,
  377. 1,
  378. data_len, &data_byte_count, &byte_count,
  379. "final");
  380. _cgm_emit_string (_plotter->data->page, false, _plotter->cgm_encoding,
  381. (const char *)s,
  382. string_length, true,
  383. data_len, &data_byte_count, &byte_count);
  384. _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
  385. &byte_count);
  386. /* update CGM profile for this page */
  387. if (string_length > 254)
  388. _plotter->cgm_page_profile = IMAX(_plotter->cgm_page_profile, CGM_PROFILE_NONE);
  389. }
  390. /* return string width in user coordinates */
  391. return width;
  392. }