g_alab_her.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 method _pl_g_alabel_hershey(), which
  16. plots a label using Hershey fonts. Each character in a Hershey font is
  17. a sequence of pen motions, so this function calls _API_fmoverel() and
  18. _API_fcontrel() to `stroke' each character in the argument string.
  19. The width of the string in user units is returned. The internal method
  20. _pl_g_flabelwidth_hershey() is similar, but does not actually plot the
  21. label. */
  22. #include "sys-defines.h"
  23. #include "extern.h"
  24. #include "g_control.h"
  25. #include "g_her_metr.h"
  26. /* Shearing factor for oblique fonts, new_x = x + SHEAR * y */
  27. #define SHEAR (2.0/7.0)
  28. /* Relative size of subscripts/superscripts (i.e. `indexical' size) */
  29. #define SCRIPTSIZE (0.6)
  30. /* Positioning of subscripts/superscripts */
  31. #define SUBSCRIPT_DX 0.0
  32. #define SUBSCRIPT_DY (-0.25)
  33. #define SUPERSCRIPT_DX 0.0
  34. #define SUPERSCRIPT_DY 0.4
  35. /* Positioning of accents (in Hershey units). UP_SHIFT is amount by which
  36. accents are raised when placed over upper-case letters. RIGHT_SHIFT is
  37. applied as well, if the upper-case letter is italic. */
  38. #define ACCENT_UP_SHIFT 7.0
  39. #define ACCENT_RIGHT_SHIFT 2.0
  40. /* Relative size of small Japanese Kana */
  41. #define SMALL_KANA_SIZE 0.725
  42. /* Hershey glyph arrays */
  43. #define OCCIDENTAL 0
  44. #define ORIENTAL 1
  45. /* Location of first Kana in the occidental glyph arrays. (Kana, unlike
  46. Kanji, are placed in the occidental array, at the very end.) */
  47. #define BEGINNING_OF_KANA 4195
  48. /* forward references */
  49. static bool composite_char (unsigned char *composite, unsigned char *character, unsigned char *accent);
  50. static double label_width_hershey (const unsigned short *label);
  51. /* An version of the alabel() method that is specific to the case when the
  52. current Plotter font is a Hershey font. It handles escape sequences for
  53. subscripts, superscripts, shifts among Hershey fonts, etc. */
  54. double
  55. _pl_g_alabel_hershey (R___(Plotter *_plotter) const unsigned char *s, int x_justify, int y_justify)
  56. {
  57. unsigned short *codestring;
  58. char x_justify_c, y_justify_c;
  59. double label_width, label_height;
  60. double x_offset, y_offset;
  61. double x_displacement;
  62. double postdx, dx, dy;
  63. double theta;
  64. /* convert string to a codestring, including annotations */
  65. codestring = _pl_g_controlify (R___(_plotter) s);
  66. /* dimensions of the string in user units */
  67. label_width = HERSHEY_UNITS_TO_USER_UNITS(label_width_hershey (codestring));
  68. label_height = HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_HEIGHT);
  69. x_justify_c = (char)x_justify;
  70. y_justify_c = (char)y_justify;
  71. switch (x_justify_c)
  72. {
  73. case 'l': /* left justified */
  74. default:
  75. x_offset = 0.0;
  76. x_displacement = 1.0;
  77. break;
  78. case 'c': /* centered */
  79. x_offset = -0.5;
  80. x_displacement = 0.0;
  81. break;
  82. case 'r': /* right justified */
  83. x_offset = -1.0;
  84. x_displacement = -1.0;
  85. break;
  86. }
  87. switch (y_justify_c)
  88. {
  89. case 'b': /* current point is at bottom */
  90. y_offset = (double)HERSHEY_DESCENT / (double)HERSHEY_HEIGHT;
  91. break;
  92. case 'x': /* current point is on baseline */
  93. default:
  94. y_offset = 0.0;
  95. break;
  96. case 'c': /* current point midway between bottom, top */
  97. y_offset = 0.5 * ((double)HERSHEY_DESCENT - (double)HERSHEY_ASCENT)
  98. / (double)HERSHEY_HEIGHT;
  99. break;
  100. case 'C': /* current point is on cap line */
  101. y_offset = - (double)HERSHEY_CAPHEIGHT / (double)HERSHEY_HEIGHT;
  102. break;
  103. case 't': /* current point is at top */
  104. y_offset = - (double)HERSHEY_ASCENT / (double)HERSHEY_HEIGHT;
  105. break;
  106. }
  107. /* save relevant drawing attributes, and restore them later */
  108. {
  109. char *old_line_mode, *old_cap_mode, *old_join_mode;
  110. int old_fill_type;
  111. double oldposx, oldposy;
  112. bool old_dash_array_in_effect;
  113. old_line_mode = (char *)_pl_xmalloc (strlen (_plotter->drawstate->line_mode) + 1);
  114. old_cap_mode = (char *)_pl_xmalloc (strlen (_plotter->drawstate->cap_mode) + 1);
  115. old_join_mode = (char *)_pl_xmalloc (strlen (_plotter->drawstate->join_mode) + 1);
  116. oldposx = _plotter->drawstate->pos.x;
  117. oldposy = _plotter->drawstate->pos.y;
  118. strcpy (old_line_mode, _plotter->drawstate->line_mode);
  119. strcpy (old_cap_mode, _plotter->drawstate->cap_mode);
  120. strcpy (old_join_mode, _plotter->drawstate->join_mode);
  121. old_fill_type = _plotter->drawstate->fill_type;
  122. old_dash_array_in_effect = _plotter->drawstate->dash_array_in_effect;
  123. /* Our choices for rendering: solid lines, rounded capitals and joins,
  124. a line width equal to slightly more than 1 Hershey unit, and no
  125. filling.
  126. We don't set the pen type: we allow it to be 0, which will mean no
  127. stroking at all. */
  128. _API_linemod (R___(_plotter) "solid");
  129. _API_capmod (R___(_plotter) "round");
  130. _API_joinmod (R___(_plotter) "round");
  131. _API_filltype (R___(_plotter) 0);
  132. /* move to take horizontal and vertical justification into account */
  133. {
  134. double theta, deltax, deltay, dx_just, dy_just;
  135. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  136. deltax = x_offset * label_width;
  137. deltay = y_offset * label_height;
  138. dx_just = cos(theta) * deltax - sin(theta) * deltay;
  139. dy_just = sin(theta) * deltax + cos(theta) * deltay;
  140. _API_fmoverel (R___(_plotter) dx_just, dy_just);
  141. }
  142. /* call stroker on the sequence of strokes obtained from each char (the
  143. stroker may manipulate the line width) */
  144. _pl_g_draw_hershey_string (R___(_plotter) codestring);
  145. /* Restore original values of relevant drawing attributes, free
  146. storage. endpath() will be invoked in here automatically, flushing
  147. the created polyline object comprising the stroked text. */
  148. _API_linemod (R___(_plotter) old_line_mode);
  149. _API_capmod (R___(_plotter) old_cap_mode);
  150. _API_joinmod (R___(_plotter) old_join_mode);
  151. _API_filltype (R___(_plotter) old_fill_type);
  152. _plotter->drawstate->dash_array_in_effect = old_dash_array_in_effect;
  153. free (old_line_mode);
  154. free (old_cap_mode);
  155. free (old_join_mode);
  156. /* return to original position */
  157. _API_fmove (R___(_plotter) oldposx, oldposy);
  158. }
  159. /* amount by which to shift after printing label (user units) */
  160. postdx = x_displacement * label_width;
  161. theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  162. dx = cos (theta) * postdx;
  163. dy = sin (theta) * postdx;
  164. _API_fmoverel (R___(_plotter) dx, dy);
  165. free (codestring);
  166. return label_width; /* user units */
  167. }
  168. /* A version of the flabelwidth() method that is specific to the case when
  169. the current Plotter font is a Hershey font. */
  170. double
  171. _pl_g_flabelwidth_hershey (R___(Plotter *_plotter) const unsigned char *s)
  172. {
  173. double label_width;
  174. unsigned short *codestring;
  175. /* convert string to a codestring, including annotations */
  176. codestring = _pl_g_controlify (R___(_plotter) s);
  177. label_width = HERSHEY_UNITS_TO_USER_UNITS(label_width_hershey (codestring));
  178. free (codestring);
  179. return label_width;
  180. }
  181. /* _pl_g_draw_hershey_stroke() draws a stroke, taking into account the
  182. transformation from Hershey units to user units, and also the angle in
  183. user space at which the label should be plotted. */
  184. void
  185. _pl_g_draw_hershey_stroke (R___(Plotter *_plotter) bool pendown, double deltax, double deltay)
  186. {
  187. double theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  188. double dx, dy;
  189. deltax = HERSHEY_UNITS_TO_USER_UNITS (deltax);
  190. deltay = HERSHEY_UNITS_TO_USER_UNITS (deltay);
  191. dx = cos(theta) * deltax - sin(theta) * deltay;
  192. dy = sin(theta) * deltax + cos(theta) * deltay;
  193. if (pendown)
  194. _API_fcontrel (R___(_plotter) dx, dy);
  195. else
  196. _API_fmoverel (R___(_plotter) dx, dy);
  197. }
  198. /* label_width_hershey() computes the width (total delta x) of a
  199. controlified character string to be rendered in a vector font, in
  200. Hershey units. Parsing must take into account the many control
  201. sequences which perform shifts, and initiate/terminate
  202. subscripts/superscripts. */
  203. /* In addition to scaling the character sizes and the `width', we perform
  204. the following (dx, dy):
  205. enter subscript (dx, dy) = (-1/9, -1/2) * width
  206. exit subscript (dx, dy) = (+1/6, +1/2) * width
  207. enter superscript (dx, dy) = (-1/9, +1/2) * width
  208. exit superscript (dx, dy) = (+1/6, -1/2) * width
  209. For clarity here, `width' refers to the width _before_ it is
  210. multiplied by a factor 2/3.
  211. [N.B. In Bob Beach's original UGS character stroke generator,
  212. the +1/6's here were +2/9 instead. Better?] */
  213. static double
  214. label_width_hershey (const unsigned short *label)
  215. {
  216. const unsigned short *ptr = label;
  217. unsigned short c;
  218. double charsize = 1.0; /* relative char size, 1.0 means full size */
  219. double saved_charsize = 1.0;
  220. double width = 0.0; /* label width */
  221. double saved_width = 0.0;
  222. /* loop through unsigned shorts in label */
  223. while ((c = (*ptr)) != (unsigned short)'\0')
  224. {
  225. int glyphnum; /* glyph in Hershey array */
  226. const unsigned char *glyph;
  227. if (c & RAW_HERSHEY_GLYPH)
  228. /* glyph was spec'd via an escape, not as a char in a font */
  229. {
  230. glyphnum = c & GLYPH_SPEC;
  231. glyph = (const unsigned char *)(_pl_g_occidental_hershey_glyphs[glyphnum]);
  232. if (*glyph != '\0') /* nonempty glyph */
  233. /* 1st two chars are bounds */
  234. width += charsize * ((int)glyph[1] - (int)glyph[0]);
  235. }
  236. else if (c & RAW_ORIENTAL_HERSHEY_GLYPH)
  237. /* glyph was spec'd via an escape, not as a char in a font */
  238. {
  239. glyphnum = c & GLYPH_SPEC;
  240. glyph = (const unsigned char *)_pl_g_oriental_hershey_glyphs[glyphnum];
  241. if (*glyph != '\0') /* nonempty glyph */
  242. /* 1st two chars are bounds */
  243. width += charsize * ((int)glyph[1] - (int)glyph[0]);
  244. }
  245. else if (c & CONTROL_CODE) /* parse control code */
  246. {
  247. switch (c & ~CONTROL_CODE)
  248. {
  249. case C_BEGIN_SUBSCRIPT:
  250. case C_BEGIN_SUPERSCRIPT :
  251. charsize *= SCRIPTSIZE;
  252. break;
  253. case C_END_SUBSCRIPT:
  254. case C_END_SUPERSCRIPT:
  255. charsize /= SCRIPTSIZE;
  256. break;
  257. case C_PUSH_LOCATION:
  258. saved_width = width;
  259. saved_charsize = charsize;
  260. break;
  261. case C_POP_LOCATION:
  262. width = saved_width;
  263. charsize = saved_charsize;
  264. break;
  265. case C_RIGHT_ONE_EM:
  266. width += charsize * HERSHEY_EM;
  267. break;
  268. case C_RIGHT_HALF_EM:
  269. width += charsize * HERSHEY_EM / 2.0;
  270. break;
  271. case C_RIGHT_QUARTER_EM:
  272. width += charsize * HERSHEY_EM / 4.0;
  273. break;
  274. case C_RIGHT_SIXTH_EM:
  275. width += charsize * HERSHEY_EM / 6.0;
  276. break;
  277. case C_RIGHT_EIGHTH_EM:
  278. width += charsize * HERSHEY_EM / 8.0;
  279. break;
  280. case C_RIGHT_TWELFTH_EM:
  281. width += charsize * HERSHEY_EM / 12.0;
  282. break;
  283. case C_LEFT_ONE_EM:
  284. width -= charsize * HERSHEY_EM;
  285. break;
  286. case C_LEFT_HALF_EM:
  287. width -= charsize * HERSHEY_EM / 2.0;
  288. break;
  289. case C_LEFT_QUARTER_EM:
  290. width -= charsize * HERSHEY_EM / 4.0;
  291. break;
  292. case C_LEFT_SIXTH_EM:
  293. width -= charsize * HERSHEY_EM / 6.0;
  294. break;
  295. case C_LEFT_EIGHTH_EM:
  296. width -= charsize * HERSHEY_EM / 8.0;
  297. break;
  298. case C_LEFT_TWELFTH_EM:
  299. width -= charsize * HERSHEY_EM / 12.0;
  300. break;
  301. /* unrecognized control code */
  302. default:
  303. break;
  304. }
  305. }
  306. else /* yow, an actual character */
  307. {
  308. int raw_fontnum;
  309. /* compute index of font, in table in g_fontdb.c */
  310. raw_fontnum = (c >> FONT_SHIFT) & ONE_BYTE;
  311. c &= ~FONT_SPEC; /* extract character proper */
  312. glyphnum = (_pl_g_hershey_font_info[raw_fontnum].chars)[c];
  313. /* could be a pseudo glyph number, e.g. an indication that
  314. character is composite */
  315. if (glyphnum == ACC0 || glyphnum == ACC1 || glyphnum == ACC2)
  316. {
  317. unsigned char composite, character, accent;
  318. /* if so, use 1st element of composite character */
  319. composite = (unsigned char)c;
  320. if (composite_char (&composite, &character, &accent))
  321. glyphnum = (_pl_g_hershey_font_info[raw_fontnum].chars)[character];
  322. else
  323. glyphnum = UNDE; /* hope this won't happen */
  324. }
  325. /* could also be a glyph number displaced by KS, to indicate
  326. that this is a small kana */
  327. if (glyphnum & KS)
  328. glyphnum -= KS;
  329. glyph = (const unsigned char *)(_pl_g_occidental_hershey_glyphs[glyphnum]);
  330. if (*glyph != '\0') /* nonempty glyph */
  331. /* 1st two chars are bounds */
  332. width += charsize * ((int)glyph[1] - (int)glyph[0]);
  333. }
  334. ptr++; /* bump pointer in string */
  335. }
  336. return width;
  337. }
  338. /* _pl_g_draw_hershey_penup_stroke() draws a penup stroke, along a vector
  339. specified in Hershey units. Size scaling and obliquing (true/false) are
  340. specified. This is used for repositioning during rendering of composite
  341. (accented) characters. */
  342. void
  343. _pl_g_draw_hershey_penup_stroke(R___(Plotter *_plotter) double dx, double dy, double charsize, bool oblique)
  344. {
  345. double shear;
  346. shear = oblique ? (SHEAR) : 0.0;
  347. _pl_g_draw_hershey_stroke (R___(_plotter)
  348. false, /* pen up */
  349. charsize * (dx + shear * dy),
  350. charsize * dy);
  351. }
  352. /* _pl_g_draw_hershey_glyph() invokes move() and cont() to draw a raw
  353. Hershey glyph, specified by index in the occidental or oriental glyph
  354. arrays. Size scaling and obliquing (true/false) are specified. */
  355. void
  356. _pl_g_draw_hershey_glyph (R___(Plotter *_plotter) int glyphnum, double charsize, int type, bool oblique)
  357. {
  358. double xcurr, ycurr;
  359. double xfinal, yfinal;
  360. bool pendown = false;
  361. const unsigned char *glyph;
  362. double dx, dy;
  363. double shear;
  364. shear = oblique ? (SHEAR) : 0.0;
  365. switch (type)
  366. {
  367. case OCCIDENTAL:
  368. default:
  369. glyph = (const unsigned char *)(_pl_g_occidental_hershey_glyphs[glyphnum]);
  370. break;
  371. case ORIENTAL:
  372. glyph = (const unsigned char *)(_pl_g_oriental_hershey_glyphs[glyphnum]);
  373. break;
  374. }
  375. if (*glyph != '\0') /* nonempty glyph */
  376. {
  377. xcurr = charsize * (double)glyph[0];
  378. xfinal = charsize * (double)glyph[1];
  379. ycurr = yfinal = 0.0;
  380. glyph += 2;
  381. while (*glyph)
  382. {
  383. int xnewint;
  384. xnewint = (int)glyph[0];
  385. if (xnewint == (int)' ')
  386. pendown = false;
  387. else
  388. {
  389. double xnew, ynew;
  390. xnew = (double)charsize * xnewint;
  391. ynew = (double)charsize
  392. * ((int)'R'
  393. - ((int)glyph[1] + (double)HERSHEY_BASELINE));
  394. dx = xnew - xcurr;
  395. dy = ynew - ycurr;
  396. _pl_g_draw_hershey_stroke (R___(_plotter)
  397. pendown, dx + shear * dy, dy);
  398. xcurr = xnew, ycurr = ynew;
  399. pendown = true;
  400. }
  401. glyph +=2; /* on to next pair */
  402. }
  403. /* final penup stroke, to end where we should */
  404. dx = xfinal - xcurr;
  405. dy = yfinal - ycurr;
  406. _pl_g_draw_hershey_stroke (R___(_plotter) false, dx + shear * dy, dy);
  407. }
  408. }
  409. /* _pl_g_draw_hershey_string() strokes a string beginning at present
  410. location, which is taken to be on the string's baseline. Besides
  411. invoking move() and cont(), it invokes linewidth(). */
  412. void
  413. _pl_g_draw_hershey_string (R___(Plotter *_plotter) const unsigned short *string)
  414. {
  415. unsigned short c;
  416. const unsigned short *ptr = string;
  417. double charsize = 1.0;
  418. double saved_charsize = 1.0;
  419. double saved_position_x = _plotter->drawstate->pos.x;
  420. double saved_position_y = _plotter->drawstate->pos.y;
  421. double old_line_width;
  422. int line_width_type = 0; /* 0,1,2 = unset,occidental,oriental */
  423. /* save line width (will restore at end) */
  424. old_line_width = _plotter->drawstate->line_width;
  425. while ((c = (*ptr++)) != '\0')
  426. {
  427. /* Check for the four possibilities: (1) a Hershey glyph specified by
  428. glyph number, (2) an oriental Hershey glyph specified by glyph
  429. number, (3) a control code, and (4) an ordinary font character,
  430. which will be mapped to a Hershey glyph by one of the tables in
  431. g_fontdb.c. */
  432. if (c & RAW_HERSHEY_GLYPH)
  433. {
  434. if (line_width_type != 1)
  435. {
  436. _API_flinewidth (R___(_plotter)
  437. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
  438. line_width_type = 1;
  439. }
  440. _pl_g_draw_hershey_glyph (R___(_plotter)
  441. c & GLYPH_SPEC, charsize, OCCIDENTAL, false);
  442. }
  443. else if (c & RAW_ORIENTAL_HERSHEY_GLYPH)
  444. {
  445. if (line_width_type != 2)
  446. {
  447. _API_flinewidth (R___(_plotter)
  448. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
  449. line_width_type = 2;
  450. }
  451. _pl_g_draw_hershey_glyph (R___(_plotter)
  452. c & GLYPH_SPEC, charsize, ORIENTAL, false);
  453. }
  454. else if (c & CONTROL_CODE)
  455. switch (c & ~CONTROL_CODE) /* parse control codes */
  456. {
  457. case C_BEGIN_SUPERSCRIPT :
  458. _pl_g_draw_hershey_stroke (R___(_plotter)
  459. false,
  460. SUPERSCRIPT_DX * charsize * HERSHEY_EM,
  461. SUPERSCRIPT_DY * charsize * HERSHEY_EM);
  462. charsize *= SCRIPTSIZE;
  463. break;
  464. case C_END_SUPERSCRIPT:
  465. charsize /= SCRIPTSIZE;
  466. _pl_g_draw_hershey_stroke (R___(_plotter)
  467. false,
  468. - SUPERSCRIPT_DX * charsize * HERSHEY_EM,
  469. - SUPERSCRIPT_DY * charsize * HERSHEY_EM);
  470. break;
  471. case C_BEGIN_SUBSCRIPT:
  472. _pl_g_draw_hershey_stroke (R___(_plotter)
  473. false,
  474. SUBSCRIPT_DX * charsize * HERSHEY_EM,
  475. SUBSCRIPT_DY * charsize * HERSHEY_EM);
  476. charsize *= SCRIPTSIZE;
  477. break;
  478. case C_END_SUBSCRIPT:
  479. charsize /= SCRIPTSIZE;
  480. _pl_g_draw_hershey_stroke (R___(_plotter)
  481. false,
  482. - SUBSCRIPT_DX * charsize * HERSHEY_EM,
  483. - SUBSCRIPT_DY * charsize * HERSHEY_EM);
  484. break;
  485. case C_PUSH_LOCATION:
  486. saved_charsize = charsize;
  487. saved_position_x = _plotter->drawstate->pos.x;
  488. saved_position_y = _plotter->drawstate->pos.y;
  489. break;
  490. case C_POP_LOCATION:
  491. charsize = saved_charsize;
  492. _API_fmove (R___(_plotter)
  493. saved_position_x, saved_position_y);
  494. break;
  495. case C_RIGHT_ONE_EM:
  496. _pl_g_draw_hershey_stroke (R___(_plotter)
  497. false, charsize * HERSHEY_EM, 0.0);
  498. break;
  499. case C_RIGHT_HALF_EM:
  500. _pl_g_draw_hershey_stroke (R___(_plotter)
  501. false, charsize * HERSHEY_EM / 2.0, 0.0);
  502. break;
  503. case C_RIGHT_QUARTER_EM:
  504. _pl_g_draw_hershey_stroke (R___(_plotter)
  505. false, charsize * HERSHEY_EM / 4.0, 0.0);
  506. break;
  507. case C_RIGHT_SIXTH_EM:
  508. _pl_g_draw_hershey_stroke (R___(_plotter)
  509. false, charsize * HERSHEY_EM / 6.0, 0.0);
  510. break;
  511. case C_RIGHT_EIGHTH_EM:
  512. _pl_g_draw_hershey_stroke (R___(_plotter)
  513. false, charsize * HERSHEY_EM / 8.0, 0.0);
  514. break;
  515. case C_RIGHT_TWELFTH_EM:
  516. _pl_g_draw_hershey_stroke (R___(_plotter)
  517. false, charsize * HERSHEY_EM / 12.0, 0.0);
  518. break;
  519. case C_LEFT_ONE_EM:
  520. _pl_g_draw_hershey_stroke (R___(_plotter)
  521. false, - charsize * HERSHEY_EM, 0.0);
  522. break;
  523. case C_LEFT_HALF_EM:
  524. _pl_g_draw_hershey_stroke (R___(_plotter)
  525. false, - charsize * HERSHEY_EM / 2.0, 0.0);
  526. break;
  527. case C_LEFT_QUARTER_EM:
  528. _pl_g_draw_hershey_stroke (R___(_plotter)
  529. false, - charsize * HERSHEY_EM / 4.0, 0.0);
  530. break;
  531. case C_LEFT_SIXTH_EM:
  532. _pl_g_draw_hershey_stroke (R___(_plotter)
  533. false, - charsize * HERSHEY_EM / 6.0, 0.0);
  534. break;
  535. case C_LEFT_EIGHTH_EM:
  536. _pl_g_draw_hershey_stroke (R___(_plotter)
  537. false, - charsize * HERSHEY_EM / 8.0, 0.0);
  538. break;
  539. case C_LEFT_TWELFTH_EM:
  540. _pl_g_draw_hershey_stroke (R___(_plotter)
  541. false, - charsize * HERSHEY_EM / 12.0, 0.0);
  542. break;
  543. /* unrecognized control code, punt */
  544. default:
  545. break;
  546. }
  547. else
  548. /* yow, an actual font character! Several possibilities: could be
  549. a composite (accented) character, could be a small Kana, or
  550. could be a garden-variety character. */
  551. {
  552. int raw_fontnum;
  553. int glyphnum; /* glyph in Hershey array */
  554. int char_glyphnum, accent_glyphnum; /* for composite chars */
  555. int char_width, accent_width; /* for composite chars */
  556. const unsigned char *char_glyph, *accent_glyph;
  557. unsigned char composite, character, accent;
  558. bool oblique, small_kana = false;
  559. /* compute index of font, in font table in g_fontdb.c */
  560. raw_fontnum = (c >> FONT_SHIFT) & ONE_BYTE;
  561. /* shear font? (for HersheySans-Oblique, etc.) */
  562. oblique = _pl_g_hershey_font_info[raw_fontnum].obliquing;
  563. c &= ~FONT_SPEC; /* extract character proper */
  564. glyphnum = (_pl_g_hershey_font_info[raw_fontnum].chars)[c];
  565. if (glyphnum & KS) /* a small kana? */
  566. {
  567. glyphnum -= KS;
  568. small_kana = true;
  569. }
  570. switch (glyphnum)
  571. {
  572. /* special case: this is a composite (accented) character;
  573. search font table in g_fontdb.c for it */
  574. case ACC0:
  575. case ACC1:
  576. case ACC2:
  577. composite = (unsigned char)c;
  578. if (composite_char (&composite, &character, &accent))
  579. {
  580. char_glyphnum =
  581. (_pl_g_hershey_font_info[raw_fontnum].chars)[character];
  582. accent_glyphnum =
  583. (_pl_g_hershey_font_info[raw_fontnum].chars)[accent];
  584. }
  585. else
  586. { /* hope this won't happen */
  587. char_glyphnum = UNDE;
  588. accent_glyphnum = 0;
  589. }
  590. char_glyph =
  591. (const unsigned char *)_pl_g_occidental_hershey_glyphs[char_glyphnum];
  592. accent_glyph =
  593. (const unsigned char *)_pl_g_occidental_hershey_glyphs[accent_glyphnum];
  594. if (*char_glyph != '\0') /* nonempty glyph */
  595. /* 1st two chars are bounds, in Hershey units */
  596. char_width = (int)char_glyph[1] - (int)char_glyph[0];
  597. else
  598. char_width = 0;
  599. if (*accent_glyph != '\0') /* nonempty glyph */
  600. /* 1st two chars are bounds, in Hershey units */
  601. accent_width = (int)accent_glyph[1] - (int)accent_glyph[0];
  602. else
  603. accent_width = 0;
  604. /* draw the character */
  605. if (line_width_type != 1)
  606. {
  607. _API_flinewidth (R___(_plotter)
  608. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
  609. line_width_type = 1;
  610. }
  611. _pl_g_draw_hershey_glyph (R___(_plotter)
  612. char_glyphnum, charsize,
  613. OCCIDENTAL, oblique);
  614. /* back up to draw accent */
  615. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  616. -0.5 * (double)char_width
  617. -0.5 * (double)accent_width,
  618. 0.0, charsize, oblique);
  619. /* repositioning for uppercase and uppercase italic */
  620. if (glyphnum == ACC1)
  621. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  622. 0.0,
  623. (double)(ACCENT_UP_SHIFT),
  624. charsize, oblique);
  625. else if (glyphnum == ACC2)
  626. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  627. (double)(ACCENT_RIGHT_SHIFT),
  628. (double)(ACCENT_UP_SHIFT),
  629. charsize, oblique);
  630. /* draw the accent */
  631. _pl_g_draw_hershey_glyph (R___(_plotter)
  632. accent_glyphnum, charsize,
  633. OCCIDENTAL, oblique);
  634. /* undo special repositioning if any */
  635. if (glyphnum == ACC1)
  636. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  637. 0.0,
  638. -(double)(ACCENT_UP_SHIFT),
  639. charsize, oblique);
  640. else if (glyphnum == ACC2)
  641. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  642. -(double)(ACCENT_RIGHT_SHIFT),
  643. -(double)(ACCENT_UP_SHIFT),
  644. charsize, oblique);
  645. /* move forward, to end composite char where we should */
  646. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  647. 0.5 * (double)char_width
  648. -0.5 * (double)accent_width,
  649. 0.0, charsize, oblique);
  650. break;
  651. /* not a composite (accented) character; just an ordinary
  652. glyph from occidental+Kana array (could be a Kana, in
  653. particular, could be a small Kana) */
  654. default:
  655. if (small_kana)
  656. {
  657. int kana_width;
  658. const unsigned char *kana_glyph;
  659. double shift = 0.5 * (1.0 - (SMALL_KANA_SIZE));
  660. kana_glyph =
  661. (const unsigned char *)_pl_g_occidental_hershey_glyphs[glyphnum];
  662. kana_width = (int)kana_glyph[1] - (int)kana_glyph[0];
  663. /* draw small Kana, preceded and followed by a penup
  664. stroke in order to traverse the full width of an
  665. ordinary Kana */
  666. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  667. shift * (double)kana_width,
  668. 0.0, charsize, oblique);
  669. if (line_width_type != 2)
  670. {
  671. _API_flinewidth (R___(_plotter)
  672. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
  673. line_width_type = 2;
  674. }
  675. _pl_g_draw_hershey_glyph (R___(_plotter)
  676. glyphnum,
  677. (SMALL_KANA_SIZE) * charsize,
  678. OCCIDENTAL, oblique);
  679. _pl_g_draw_hershey_penup_stroke (R___(_plotter)
  680. shift * (double)kana_width,
  681. 0.0, charsize, oblique);
  682. }
  683. else
  684. /* whew! just an ordinary glyph from the occidental array
  685. (could be a Kana however, since they're confusingly
  686. placed in that array, at the end) */
  687. {
  688. if (glyphnum >= BEGINNING_OF_KANA)
  689. {
  690. if (line_width_type != 2)
  691. {
  692. _API_flinewidth (R___(_plotter)
  693. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
  694. line_width_type = 2;
  695. }
  696. }
  697. else
  698. if (line_width_type != 1)
  699. {
  700. _API_flinewidth (R___(_plotter)
  701. HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
  702. line_width_type = 1;
  703. }
  704. _pl_g_draw_hershey_glyph (R___(_plotter)
  705. glyphnum, charsize,
  706. OCCIDENTAL, oblique);
  707. }
  708. break;
  709. } /* end of case statement that switches based on glyphnum */
  710. } /* end of font character case */
  711. } /* end of loop through unsigned shorts in the codestring */
  712. if (line_width_type != 0)
  713. /* must restore old line width */
  714. _API_flinewidth (R___(_plotter) old_line_width);
  715. return;
  716. }
  717. /* retrieve the two elements of a composite character from the table in
  718. g_fontdb.c */
  719. static bool
  720. composite_char (unsigned char *composite, unsigned char *character, unsigned char *accent)
  721. {
  722. const struct plHersheyAccentedCharInfoStruct *compchar = _pl_g_hershey_accented_char_info;
  723. bool found = false;
  724. unsigned char given = *composite;
  725. while (compchar->composite)
  726. {
  727. if (compchar->composite == given)
  728. {
  729. found = true;
  730. /* return char and accent via pointers */
  731. *character = compchar->character;
  732. *accent = compchar->accent;
  733. }
  734. compchar++;
  735. }
  736. return found;
  737. }