gxhint1.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* Copyright (C) 1990, 1992, 1993, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gxhint1.c,v 1.2 2000/09/19 19:00:37 lpd Exp $ */
  16. /* Font level hints for Type 1 fonts */
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gxarith.h"
  20. #include "gxfixed.h"
  21. #include "gxmatrix.h"
  22. #include "gxfont.h"
  23. #include "gxfont1.h"
  24. #include "gxtype1.h"
  25. /* Define whether to use font hints. */
  26. /* Only set this to false for debugging the hint machinery. */
  27. static bool USE_HINTS = true;
  28. /* ------ Initialization ------ */
  29. typedef zone_table(1) a_zone_table;
  30. typedef stem_table(1) a_stem_table;
  31. private void
  32. compute_snaps(P6(const gs_matrix_fixed *, const a_stem_table *,
  33. stem_snap_table *, int, int, const char *));
  34. private alignment_zone *
  35. compute_zones(P6(const gs_matrix_fixed *, const font_hints *,
  36. const a_zone_table *, const a_zone_table *, alignment_zone *, int));
  37. private int
  38. transform_zone(P4(const gs_matrix_fixed *, const font_hints *,
  39. const float *, alignment_zone *));
  40. /* Reset the font-level hints. */
  41. void
  42. reset_font_hints(font_hints * pfh, const gs_log2_scale_point * plog2_scale)
  43. {
  44. set_pixel_scale(&pfh->scale.x, plog2_scale->x);
  45. set_pixel_scale(&pfh->scale.y, plog2_scale->y);
  46. pfh->axes_swapped = pfh->x_inverted = pfh->y_inverted = false;
  47. pfh->use_x_hints = pfh->use_y_hints = false;
  48. pfh->snap_h.count = pfh->snap_v.count = 0;
  49. pfh->a_zone_count = 0;
  50. }
  51. /* Compute the font-level hints from the font and the matrix. */
  52. /* We should cache this with the font/matrix pair.... */
  53. void
  54. compute_font_hints(font_hints * pfh, const gs_matrix_fixed * pmat,
  55. const gs_log2_scale_point * plog2_scale, const gs_type1_data * pdata)
  56. {
  57. alignment_zone *zp = &pfh->a_zones[0];
  58. reset_font_hints(pfh, plog2_scale);
  59. /* Figure out which hints, if any, to use, */
  60. /* and the orientation of the axes. */
  61. if (is_fzero(pmat->xy))
  62. pfh->y_inverted = is_fneg(pmat->yy),
  63. pfh->use_y_hints = USE_HINTS;
  64. else if (is_fzero(pmat->xx))
  65. pfh->y_inverted = is_fneg(pmat->xy),
  66. pfh->axes_swapped = true,
  67. pfh->use_y_hints = USE_HINTS;
  68. if (is_fzero(pmat->yx))
  69. pfh->x_inverted = is_fneg(pmat->xx),
  70. pfh->use_x_hints = USE_HINTS;
  71. else if (is_fzero(pmat->yy))
  72. pfh->x_inverted = is_fneg(pmat->yx),
  73. pfh->axes_swapped = true,
  74. pfh->use_x_hints = USE_HINTS;
  75. if_debug6('y', "[y]ctm=[%g %g %g %g %g %g]\n",
  76. pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  77. pmat->tx, pmat->ty);
  78. if_debug7('y', "[y]scale=%d/%d, swapped=%d, x/y_hints=%d,%d, x/y_inverted=%d,%d\n",
  79. 1 << plog2_scale->x, 1 << plog2_scale->y,
  80. pfh->axes_swapped, pfh->use_x_hints, pfh->use_y_hints,
  81. pfh->x_inverted, pfh->y_inverted);
  82. /* Transform the actual hints. */
  83. if (pfh->use_x_hints) {
  84. compute_snaps(pmat, (const a_stem_table *)&pdata->StdHW,
  85. &pfh->snap_h, 0, pfh->axes_swapped, "h");
  86. compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapH,
  87. &pfh->snap_h, 0, pfh->axes_swapped, "h");
  88. }
  89. if (pfh->use_y_hints) {
  90. gs_fixed_point vw;
  91. fixed *vp = (pfh->axes_swapped ? &vw.x : &vw.y);
  92. pixel_scale *psp =
  93. (pfh->axes_swapped ? &pfh->scale.x : &pfh->scale.y);
  94. /* Convert BlueFuzz to device pixels. */
  95. if (gs_distance_transform2fixed(pmat, 0.0,
  96. (float)pdata->BlueFuzz,
  97. &vw) < 0
  98. )
  99. vw.x = vw.y = fixed_0;
  100. pfh->blue_fuzz = any_abs(*vp);
  101. /*
  102. * Decide whether to suppress overshoots. The formula in
  103. * section 5.6 of the "Adobe Type 1 Font Format" says that
  104. * at 300 dpi, if BlueScale = (P - 0.49) / 240, overshoot
  105. * suppression turns off at point sizes at least P, i.e.:
  106. * P >= BlueScale * 240 + 0.49.
  107. * At 300 dpi, P = |CTM.yy| / (300/72), so the condition is
  108. * equivalent to
  109. * |CTM.yy| >= BlueScale * 1000 + 2.0417,
  110. * or
  111. * BlueScale >= (|CTM.yy| - 2.0417) / 1000.
  112. * Since *pmat is the concatenation of the FontMatrix and
  113. * CTM, if we assume a 1000-unit scale, this is equivalent to
  114. * BlueScale >= |pmat->yy| - 0.00020417.
  115. * Since the constant term is slightly smaller than
  116. * fixed_epsilon, we just disregard it.
  117. *
  118. * According to the same section of the Adobe documentation,
  119. * there is a requirement that BlueScale times the maximum
  120. * alignment zone height must be less than 1. We enforced
  121. * this when the font was constructed (in zfont1.c).
  122. */
  123. if (gs_distance_transform2fixed(pmat, 0.0, 1.0, &vw) < 0)
  124. vw.x = vw.y = fixed_0;
  125. pfh->suppress_overshoot =
  126. fixed2float(any_abs(*vp) >> psp->log2_unit) <
  127. pdata->BlueScale;
  128. if (gs_distance_transform2fixed(pmat, 0.0, pdata->BlueShift,
  129. &vw) < 0
  130. )
  131. vw.x = vw.y = fixed_0;
  132. pfh->blue_shift = any_abs(*vp);
  133. /*
  134. * Don't let the blue shift exceed half a pixel.
  135. * See the discussion of BlueShift in section 5.7 of the
  136. * "Adobe Type 1 Font Format" book.
  137. */
  138. if (pfh->blue_shift > psp->half)
  139. pfh->blue_shift = psp->half;
  140. if_debug6('y', "[y]blue_fuzz=%d->%g, blue_scale=%g, blue_shift=%g->%g, sup_ov=%d\n",
  141. pdata->BlueFuzz, fixed2float(pfh->blue_fuzz),
  142. pdata->BlueScale,
  143. pdata->BlueShift, fixed2float(pfh->blue_shift),
  144. pfh->suppress_overshoot);
  145. zp = compute_zones(pmat, pfh,
  146. (const a_zone_table *)&pdata->BlueValues,
  147. (const a_zone_table *)&pdata->FamilyBlues,
  148. zp, 1);
  149. zp = compute_zones(pmat, pfh,
  150. (const a_zone_table *)&pdata->OtherBlues,
  151. (const a_zone_table *)&pdata->FamilyOtherBlues,
  152. zp, max_OtherBlues);
  153. compute_snaps(pmat, (const a_stem_table *)&pdata->StdVW,
  154. &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  155. compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapV,
  156. &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  157. }
  158. pfh->a_zone_count = zp - &pfh->a_zones[0];
  159. }
  160. /* Transform one set of stem snap widths. */
  161. private void
  162. compute_snaps(const gs_matrix_fixed * pmat, const a_stem_table * pst,
  163. stem_snap_table * psst, int from_y, int to_y, const char *tname)
  164. {
  165. gs_fixed_point wxy;
  166. fixed *wp = (to_y ? &wxy.y : &wxy.x);
  167. int i;
  168. int j = psst->count;
  169. for (i = 0; i < pst->count; i++) {
  170. float w = pst->values[i];
  171. int code =
  172. (from_y ?
  173. gs_distance_transform2fixed(pmat, 0.0, w, &wxy) :
  174. gs_distance_transform2fixed(pmat, w, 0.0, &wxy)
  175. );
  176. if (code < 0)
  177. continue;
  178. psst->data[j] = any_abs(*wp);
  179. if_debug3('y', "[y]snap_%s[%d]=%g\n", tname, j,
  180. fixed2float(psst->data[j]));
  181. j++;
  182. }
  183. psst->count = j;
  184. }
  185. /* Compute the alignment zones for one set of 'blue' values. */
  186. private alignment_zone *
  187. compute_zones(const gs_matrix_fixed * pmat, const font_hints * pfh,
  188. const a_zone_table * blues, const a_zone_table * family_blues,
  189. alignment_zone * zp, int bottom_count)
  190. {
  191. int i;
  192. fixed fuzz = pfh->blue_fuzz;
  193. int inverted =
  194. (pfh->axes_swapped ? pfh->x_inverted : pfh->y_inverted);
  195. for (i = 0; i < blues->count; i += 2) {
  196. const float *vp = &blues->values[i];
  197. zp->is_top_zone = i >> 1 >= bottom_count;
  198. if (transform_zone(pmat, pfh, vp, zp) < 0)
  199. continue;
  200. if_debug5('y', "[y]blues[%d]=%g,%g -> %g,%g\n",
  201. i >> 1, vp[0], vp[1],
  202. fixed2float(zp->v0), fixed2float(zp->v1));
  203. if (i < family_blues->count) { /* Check whether family blues should supersede. */
  204. alignment_zone fz;
  205. const float *fvp = &family_blues->values[i];
  206. fixed unit = (pfh->axes_swapped ?
  207. pfh->scale.x.unit : pfh->scale.y.unit);
  208. fixed diff;
  209. if (transform_zone(pmat, pfh, fvp, &fz) < 0)
  210. continue;
  211. if_debug5('y', "[y]f_blues[%d]=%g,%g -> %g,%g\n",
  212. i >> 1, fvp[0], fvp[1],
  213. fixed2float(fz.v0), fixed2float(fz.v1));
  214. diff = (zp->v1 - zp->v0) - (fz.v1 - fz.v0);
  215. if (diff > -unit && diff < unit)
  216. zp->v0 = fz.v0, zp->v1 = fz.v1;
  217. }
  218. /* Compute the flat position, and add the fuzz. */
  219. if ((inverted ? zp->is_top_zone : !zp->is_top_zone))
  220. zp->flat = zp->v1, zp->v0 -= fuzz;
  221. else
  222. zp->flat = zp->v0, zp->v1 += fuzz;
  223. zp++;
  224. }
  225. return zp;
  226. }
  227. /* Transform a single alignment zone to device coordinates, */
  228. /* taking axis swapping into account. */
  229. private int
  230. transform_zone(const gs_matrix_fixed * pmat, const font_hints * pfh,
  231. const float *vp, alignment_zone * zp)
  232. {
  233. gs_fixed_point p0, p1;
  234. fixed v0, v1;
  235. int code;
  236. if ((code = gs_point_transform2fixed(pmat, 0.0, vp[0], &p0)) < 0 ||
  237. (code = gs_point_transform2fixed(pmat, 0.0, vp[1], &p1)) < 0
  238. )
  239. return code;
  240. if (pfh->axes_swapped)
  241. v0 = p0.x, v1 = p1.x;
  242. else
  243. v0 = p0.y, v1 = p1.y;
  244. if (v0 <= v1)
  245. zp->v0 = v0, zp->v1 = v1;
  246. else
  247. zp->v0 = v1, zp->v1 = v0;
  248. return 0;
  249. }