home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GXHINT1.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  209 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gxhint1.c */
  20. /* Font level hints for Type 1 fonts */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxarith.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"
  27. #include "gxdevmem.h"            /* ditto */
  28. #include "gxchar.h"
  29. #include "gxfont.h"
  30. #include "gxfont1.h"
  31. #include "gxtype1.h"
  32.  
  33. /* ------ Initialization ------ */
  34.  
  35. typedef zone_table(1) a_zone_table;
  36. typedef stem_table(1) a_stem_table;
  37. private void near
  38.   compute_snaps(P6(const gs_matrix_fixed *, const a_stem_table *,
  39.     stem_snap_table *, int, int, const char *));
  40. private alignment_zone *near
  41.   compute_zones(P6(const gs_matrix_fixed *, const font_hints *,
  42.     const a_zone_table *, const a_zone_table *, alignment_zone *, int));
  43. private void near
  44.   transform_zone(P4(const gs_matrix_fixed *, const font_hints *,
  45.     const float *, alignment_zone *));
  46.  
  47. /* Reset the font-level hints. */
  48. void
  49. reset_font_hints(font_hints *pfh, const gs_log2_scale_point *plog2_scale)
  50. {    set_pixel_scale(&pfh->scale.x, plog2_scale->x);
  51.     set_pixel_scale(&pfh->scale.y, plog2_scale->y);
  52.     pfh->axes_swapped = pfh->x_inverted = pfh->y_inverted = 0;
  53.     pfh->use_x_hints = pfh->use_y_hints = 0;
  54.     pfh->snap_h.count = pfh->snap_v.count = 0;
  55.     pfh->a_zone_count = 0;
  56. }
  57.  
  58. /* Compute the font-level hints from the font and the matrix. */
  59. /* We should cache this with the font/matrix pair.... */
  60. void
  61. compute_font_hints(font_hints *pfh, const gs_matrix_fixed *pmat,
  62.   const gs_log2_scale_point *plog2_scale, const gs_type1_data *pdata)
  63. {    alignment_zone *zp = &pfh->a_zones[0];
  64.     reset_font_hints(pfh, plog2_scale);
  65.     /* Figure out which hints, if any, to use, */
  66.     /* and the orientation of the axes. */
  67.     if ( is_fzero(pmat->xy) )
  68.         pfh->y_inverted = is_fneg(pmat->yy),
  69.         pfh->use_y_hints = 1;
  70.     else if ( is_fzero(pmat->xx) )
  71.         pfh->y_inverted = is_fneg(pmat->xy),
  72.         pfh->axes_swapped = 1,
  73.         pfh->use_y_hints = 1;
  74.     if ( is_fzero(pmat->yx) )
  75.         pfh->x_inverted = is_fneg(pmat->xx),
  76.         pfh->use_x_hints = 1;
  77.     else if ( is_fzero(pmat->yy) )
  78.         pfh->x_inverted = is_fneg(pmat->yx),
  79.         pfh->axes_swapped = 1,
  80.         pfh->use_x_hints = 1;
  81.     if_debug6('y', "[y]ctm=[%g %g %g %g %g %g]\n",
  82.           pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  83.           pmat->tx, pmat->ty);
  84.     if_debug5('y', "[y]swapped=%d, x/y_hints=%d,%d, x/y_inverted=%d,%d\n",
  85.           pfh->axes_swapped, pfh->use_x_hints, pfh->use_y_hints,
  86.           pfh->x_inverted, pfh->y_inverted);
  87.     /* Transform the actual hints. */
  88.     if ( pfh->use_x_hints )
  89.     {    compute_snaps(pmat, (const a_stem_table *)&pdata->StdHW,
  90.                   &pfh->snap_h, 0, pfh->axes_swapped, "h");
  91.         compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapH,
  92.                   &pfh->snap_h, 0, pfh->axes_swapped, "h");
  93.     }
  94.     if ( pfh->use_y_hints )
  95.     {    gs_fixed_point vw;
  96.         fixed *vp = (pfh->axes_swapped ? &vw.x : &vw.y);
  97.         pixel_scale *psp =
  98.             (pfh->axes_swapped ? &pfh->scale.x : &pfh->scale.y);
  99.         /* Convert blue parameters to device pixels. */
  100.         gs_distance_transform2fixed(pmat, 0.0,
  101.                         (float)pdata->BlueFuzz, &vw);
  102.         pfh->blue_fuzz = any_abs(*vp);
  103.         gs_distance_transform2fixed(pmat, 0.0, 1.0, &vw);
  104.         pfh->suppress_overshoot =
  105.           fixed2float(any_abs(*vp) >> psp->log2_unit) < pdata->BlueScale;
  106.         gs_distance_transform2fixed(pmat, 0.0, pdata->BlueShift, &vw);
  107.         pfh->blue_shift = any_abs(*vp);
  108.         /* Tweak up blue_shift if it is less than half a pixel. */
  109.         /* See the discussion of BlueShift in section 5.7 of */
  110.         /* "Adobe Type 1 Font Format." */
  111.         if ( pfh->blue_shift < psp->half )
  112.             pfh->blue_shift = psp->half;
  113.         if_debug6('y', "[y]blue_fuzz=%d->%g, blue_scale=%g, blue_shift=%g->%g, sup_ov=%d\n",
  114.               pdata->BlueFuzz, fixed2float(pfh->blue_fuzz),
  115.               pdata->BlueScale,
  116.               pdata->BlueShift, fixed2float(pfh->blue_shift),
  117.               pfh->suppress_overshoot);
  118.         zp = compute_zones(pmat, pfh,
  119.                    (const a_zone_table *)&pdata->BlueValues,
  120.                    (const a_zone_table *)&pdata->FamilyBlues,
  121.                    zp, 1);
  122.         zp = compute_zones(pmat, pfh,
  123.                    (const a_zone_table *)&pdata->OtherBlues,
  124.                    (const a_zone_table *)&pdata->FamilyOtherBlues,
  125.                    zp, max_OtherBlues);
  126.         compute_snaps(pmat, (const a_stem_table *)&pdata->StdVW,
  127.                   &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  128.         compute_snaps(pmat, (const a_stem_table *)&pdata->StemSnapV,
  129.                   &pfh->snap_v, 1, !pfh->axes_swapped, "v");
  130.     }
  131.     pfh->a_zone_count = zp - &pfh->a_zones[0];
  132. }
  133.  
  134. /* Transform one set of stem snap widths. */
  135. private void near
  136. compute_snaps(const gs_matrix_fixed *pmat, const a_stem_table *pst,
  137.   stem_snap_table *psst, int from_y, int to_y, const char *tname)
  138. {    gs_fixed_point wxy;
  139.     fixed *wp = (to_y ? &wxy.y : &wxy.x);
  140.     int i;
  141.     int j = psst->count;
  142.     for ( i = 0; i < pst->count; i++, j++ )
  143.     {    float w = pst->values[i];
  144.         if ( from_y )
  145.             gs_distance_transform2fixed(pmat, 0.0, w, &wxy);
  146.         else
  147.             gs_distance_transform2fixed(pmat, w, 0.0, &wxy);
  148.         psst->data[j] = any_abs(*wp);
  149.         if_debug3('y', "[y]snap_%s[%d]=%g\n", tname, j,
  150.               fixed2float(psst->data[j]));
  151.     }
  152.     psst->count = j;
  153. }
  154.  
  155. /* Compute the alignment zones for one set of 'blue' values. */
  156. private alignment_zone *near
  157. compute_zones(const gs_matrix_fixed *pmat, const font_hints *pfh,
  158.   const a_zone_table *blues, const a_zone_table *family_blues,
  159.   alignment_zone *zp, int bottom_count)
  160. {    int i;
  161.     fixed fuzz = pfh->blue_fuzz;
  162.     int inverted =
  163.         (pfh->axes_swapped ? pfh->x_inverted : pfh->y_inverted);
  164.     for ( i = 0; i < blues->count; i += 2, zp++ )
  165.     {    const float *vp = &blues->values[i];
  166.         zp->is_top_zone = i >> 1 >= bottom_count;
  167.         transform_zone(pmat, pfh, vp, zp);
  168.         if_debug5('y', "[y]blues[%d]=%g,%g -> %g,%g\n",
  169.               i >> 1, vp[0], vp[1],
  170.               fixed2float(zp->v0), fixed2float(zp->v1));
  171.         if ( i < family_blues->count )
  172.         {    /* Check whether family blues should supersede. */
  173.             alignment_zone fz;
  174.             const float *fvp = &family_blues->values[i];
  175.             fixed unit = (pfh->axes_swapped ?
  176.                 pfh->scale.x.unit : pfh->scale.y.unit);
  177.             fixed diff;
  178.             transform_zone(pmat, pfh, fvp, &fz);
  179.             if_debug5('y', "[y]f_blues[%d]=%g,%g -> %g,%g\n",
  180.                   i >> 1, fvp[0], fvp[1],
  181.                   fixed2float(fz.v0), fixed2float(fz.v1));
  182.             diff = (zp->v1 - zp->v0) - (fz.v1 - fz.v0);
  183.             if ( diff > -unit && diff < unit )
  184.                 zp->v0 = fz.v0, zp->v1 = fz.v1;
  185.         }
  186.         /* Compute the flat position, and add the fuzz. */
  187.         if ( (inverted ? zp->is_top_zone : !zp->is_top_zone) )
  188.             zp->flat = zp->v1, zp->v0 -= fuzz;
  189.         else
  190.             zp->flat = zp->v0, zp->v1 += fuzz;
  191.     }
  192.     return zp;
  193. }
  194.  
  195. /* Transform a single alignment zone to device coordinates, */
  196. /* taking axis swapping into account. */
  197. private void near
  198. transform_zone(const gs_matrix_fixed *pmat, const font_hints *pfh,
  199.   const float *vp, alignment_zone *zp)
  200. {    gs_fixed_point p0, p1;
  201.     fixed v0, v1;
  202.     gs_point_transform2fixed(pmat, 0.0, vp[0], &p0);
  203.     gs_point_transform2fixed(pmat, 0.0, vp[1], &p1);
  204.     if ( pfh->axes_swapped ) v0 = p0.x, v1 = p1.x;
  205.     else v0 = p0.y, v1 = p1.y;
  206.     if ( v0 <= v1 ) zp->v0 = v0, zp->v1 = v1;
  207.     else zp->v0 = v1, zp->v1 = v0;
  208. }
  209.