zchar42.c 10.5 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15
*/

16

17 18 19 20 21 22 23 24 25
/* Type 42 character display operator */
#include "ghost.h"
#include "oper.h"
#include "gsmatrix.h"
#include "gspaint.h"		/* for gs_fill, gs_stroke */
#include "gspath.h"
#include "gxfixed.h"
#include "gxfont.h"
#include "gxfont42.h"
26
#include "gxgstate.h"
27 28 29 30 31 32 33 34 35 36 37 38 39
#include "gxpath.h"
#include "gxtext.h"
#include "gzstate.h"		/* only for ->path */
#include "dstack.h"		/* only for systemdict */
#include "estack.h"
#include "ichar.h"
#include "icharout.h"
#include "ifont.h"		/* for font_param */
#include "igstate.h"
#include "iname.h"
#include "store.h"
#include "string_.h"
#include "zchar42.h"
40
#include "idict.h"
41 42 43

/* Get a Type 42 character metrics and set the cache device. */
int
44 45
zchar42_set_cache(i_ctx_t *i_ctx_p, gs_font_base *pbfont, ref *cnref,
            uint glyph_index, op_proc_t cont, op_proc_t *exec_cont)
46 47 48 49 50 51 52 53
{   double sbw[4];
    double w[2];
    int present;
    gs_font_type42 *pfont42 = (gs_font_type42 *)pbfont;
    int code = zchar_get_metrics(pbfont, cnref, sbw);
    gs_rect bbox;
    int vertical = gs_rootfont(igs)->WMode;
    float sbw_bbox[8];
54 55 56 57
    float sbw_bbox_h[8];
    ref *fdict = (ref *)pbfont->client_data;
    ref *rpath = NULL;
    bool embedded = true;
58 59

    if (code < 0)
60
        return code;
61
    present = code;
62 63 64 65 66

    if (dict_find_string(fdict, "Path", &rpath) > 0) {
        embedded = false;
    }

67 68
    if (vertical) { /* for vertically-oriented metrics */

69 70 71 72 73 74 75 76 77
        /* Always call get_metrics because we'll need glyph bbox below in any case
           as a workaround for Dynalab fonts. We can't recognize Dynalab here. */
        code = pfont42->data.get_metrics(pfont42, glyph_index,
                    gs_type42_metrics_options_WMODE0_AND_BBOX, sbw_bbox_h);
        if (code < 0)
            return code;
        code = pfont42->data.get_metrics(pfont42, glyph_index,
                gs_type42_metrics_options_WMODE1_AND_BBOX, sbw_bbox);
        /* Here code=0 means success, code<0 means no vertical metrics. */
78 79 80 81 82 83 84
        /* We only want to create fake vertical metrics for TTF fonts
           being used to emulate a vertical writing CIDFont. If we have
           a CIDType 2 font, without vertical metrics, we're supposed to
           treat it as a horizontal writing font, regardless of the wmode
           setting
         */
        if (code < 0 && !embedded) {
85 86 87 88 89 90 91
            /* No vertical metrics in the font,
               hewristically compose vertical metrics from bounding boxes. */
            sbw_bbox[0] = 0;
            sbw_bbox[1] = pbfont->FontBBox.q.y - 1;
            sbw_bbox[2] = 0;
            sbw_bbox[3] = -1;
        }
92 93 94 95 96
        else {
            vertical = false;
        }
    }
    if (vertical) {
97 98 99 100 101 102 103 104
        if (present != metricsSideBearingAndWidth) {
            /* metricsNone or metricsWidthOnly. */
            /* No top side bearing (in Metrics2) in Postscript font. */
            /* Note that Postscript wants the 'V' vector in sbw[0:1],
               and True Type supplies Top Side Bearing in sbw_bbox[0:1],
               and Left Side Bearing in sbw_bbox_h[0:1].
               So we need to compute V from FontBBox as we do for FontType 9
               (see FontBBox_as_Metrics2) and add TSB to it. */
105
#	    if 0 /* old code taken from empirics, keepping it for a while to compare results. */
106 107
            sbw[0] = (sbw_bbox[6] + sbw_bbox[4]) / 2;
            sbw[1] = (pbfont->FontBBox.q.y + pbfont->FontBBox.p.y - sbw_bbox[3]) / 2;
108
#	    else
109 110
            sbw[0] = sbw_bbox_h[2] / 2;
            sbw[1] = sbw_bbox[1] - sbw_bbox[3];
111
#	    endif
112 113 114 115 116 117
        }
        if (present == metricsNone) {
            /* No adwance width (in Metrcis2) in Postscript font. */
            sbw[2] = 0;
            sbw[3] = sbw_bbox[3];
        }
118
    } else {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        /* Always call get_metrics because we'll need glyph bbox below in any case
           as a workaround for Dynalab fonts. We can't recognize Dynalab here. */
        code = pfont42->data.get_metrics(pfont42, glyph_index,
                    gs_type42_metrics_options_WMODE0_AND_BBOX, sbw_bbox);
        if (code < 0)
            return code;
        if (present != metricsSideBearingAndWidth) {
            /* metricsNone or metricsWidthOnly. */
            /* No left side bearing (in Metrics) in Postscript font. */
            sbw[0] = sbw_bbox[0];
            sbw[1] = sbw_bbox[1];
        }
        if (present == metricsNone) {
            /* No advance width (in Metrics) in Postscript font. */
            sbw[2] = sbw_bbox[2];
            sbw[3] = sbw_bbox[3];
        }
136 137 138 139
    }
    w[0] = sbw[2];
    w[1] = sbw[3];
    if (!vertical) {
140 141
        sbw_bbox[6] = (sbw_bbox[6] - sbw_bbox[4]) + sbw_bbox[0];
        sbw_bbox[4] = sbw_bbox[0];
142 143 144 145 146 147 148 149 150 151
    }
    /* Note: The glyph bbox usn't useful for Dynalab fonts,
       which stretch subglyphs. Uniting with FontBBox helps.
       In same time, FontBBox with no glyph bbox
       doesn't work for 34_all.PS page 4. */
    bbox.p.x = min(sbw_bbox[4], pbfont->FontBBox.p.y);
    bbox.p.y = min(sbw_bbox[5], pbfont->FontBBox.p.y);
    bbox.q.x = max(sbw_bbox[6], pbfont->FontBBox.q.x);
    bbox.q.y = max(sbw_bbox[7], pbfont->FontBBox.q.y);
    return zchar_set_cache(i_ctx_p, pbfont, cnref,
152 153 154
                           NULL,
                           w, &bbox,
                           cont, exec_cont,
155
                           vertical ? sbw : NULL);
156 157 158 159 160 161 162 163 164 165
}

/* <font> <code|name> <name> <glyph_index> .type42execchar - */
static int type42_fill(i_ctx_t *);
static int type42_stroke(i_ctx_t *);
static int
ztype42execchar(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    gs_font *pfont;
166 167 168
    int code;
    gs_font_base *pbfont;
    gs_font_type42 *pfont42;
169
    gs_text_enum_t *penum = op_show_find(i_ctx_p);
170
    op_proc_t cont, exec_cont = 0;
171
    ref *cnref;
172 173
    uint glyph_index;

174 175 176 177 178 179 180
    check_op(4);
    check_type(*(op - 1), t_name);
    if (!r_has_type((op - 2), t_name)) {
        check_type(*(op - 2), t_integer);
    }

    code = font_param(op - 3, &pfont);
181
    if (code < 0)
182
        return code;
183 184 185 186 187

    pbfont = (gs_font_base *) pfont;
    cont = (pbfont->PaintType == 0 ? type42_fill : type42_stroke);
    pfont42 = (gs_font_type42 *) pfont;

188
    if (penum == 0 ||
189 190 191
        (pfont->FontType != ft_TrueType &&
         pfont->FontType != ft_CID_TrueType)
        )
192
        return_error(gs_error_undefined);
193 194 195 196 197 198 199 200
    /*
     * Any reasonable implementation would execute something like
     *  1 setmiterlimit 0 setlinejoin 0 setlinecap
     * here, but apparently the Adobe implementations aren't reasonable.
     *
     * If this is a stroked font, set the stroke width.
     */
    if (pfont->PaintType)
201
        gs_setlinewidth(igs, pfont->StrokeWidth);
202 203 204 205 206
    check_estack(3);		/* for continuations */
    /*
     * Execute the definition of the character.
     */
    if (r_is_proc(op))
207
        return zchar_exec_char_proc(i_ctx_p);
208 209 210 211 212 213 214 215 216
    /*
     * The definition must be a Type 42 glyph index.
     * Note that we do not require read access: this is deliberate.
     */
    check_type(*op, t_integer);
    check_ostack(3);		/* for lsb values */
    /* Establish a current point. */
    code = gs_moveto(igs, 0.0, 0.0);
    if (code < 0)
217
        return code;
218 219 220
    cnref = op - 1;
    glyph_index = (uint)op->value.intval;
    if (pfont42->data.gsub_size) {
221 222 223
        glyph_index = pfont42->data.substitute_glyph_index_vertical(pfont42, glyph_index,
                gs_rootfont(igs)->WMode, penum->returned.current_glyph);
        make_int(op, glyph_index);
224 225 226
    }
    code = zchar42_set_cache(i_ctx_p, pbfont, cnref, glyph_index, cont, &exec_cont);
    if (code >= 0 && exec_cont != 0)
227
        code = (*exec_cont)(i_ctx_p);
228 229 230 231 232
    return code;
}

/* Continue after a CDevProc callout. */
static int type42_finish(i_ctx_t *i_ctx_p,
233
                          int (*cont)(gs_gstate *));
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static int
type42_fill(i_ctx_t *i_ctx_p)
{
    int code;
    gs_fixed_point fa = i_ctx_p->pgs->fill_adjust;

    i_ctx_p->pgs->fill_adjust.x = i_ctx_p->pgs->fill_adjust.y = -1;
    code = type42_finish(i_ctx_p, gs_fill);
    i_ctx_p->pgs->fill_adjust = fa; /* Not sure whether we need to restore it,
                                       but this isn't harmful. */
    return code;
}
static int
type42_stroke(i_ctx_t *i_ctx_p)
{
    return type42_finish(i_ctx_p, gs_stroke);
}
/* <font> <code|name> <name> <glyph_index> %type42_{fill|stroke} - */
static int
253
type42_finish(i_ctx_t *i_ctx_p, int (*cont) (gs_gstate *))
254 255 256 257 258 259 260 261 262 263 264 265
{
    os_ptr op = osp;
    gs_font *pfont;
    gs_font_type42 *pfont42;
    int code;
    gs_text_enum_t *penum = op_show_find(i_ctx_p);
    os_ptr opc = op;
    uint glyph_index;

    check_type(*opc, t_integer);
    code = font_param(opc - 3, &pfont);
    if (code < 0)
266
        return code;
267
    if (penum == 0 || (pfont->FontType != ft_TrueType &&
268 269
                       pfont->FontType != ft_CID_TrueType)
        )
270
        return_error(gs_error_undefined);
271 272 273
    pfont42 = (gs_font_type42 *)pfont;

    if (!i_ctx_p->RenderTTNotdef) {
274 275
        if (r_has_type(opc - 1, t_name)) {
            ref gref;
276

277 278 279 280 281 282 283
            name_string_ref(imemory, opc - 1, &gref);
            if ((gref.tas.rsize == 7 && strncmp((const char *)gref.value.const_bytes, ".notdef", 7) == 0) ||
                (gref.tas.rsize > 9 && strncmp((const char *)gref.value.const_bytes, ".notdef~GS", 10) == 0)) {
                pop(4);
                return (*cont)(igs);
            }
        }
284 285 286
    }
    glyph_index = (uint)opc->value.intval;
    if (pfont42->data.gsub_size)
287 288
        glyph_index = pfont42->data.substitute_glyph_index_vertical(pfont42, glyph_index,
                    gs_rootfont(igs)->WMode, penum->returned.current_glyph);
289 290 291 292 293 294
    /*
     * We have to disregard penum->pis and penum->path, and render to
     * the current gstate and path.  This is a design bug that we will
     * have to address someday!
     */
    code = gs_type42_append(glyph_index, igs,
295 296
                            igs->path, penum, pfont,
                            (penum->text.operation & TEXT_DO_ANY_CHARPATH) != 0);
297
    if (code < 0)
298
        return code;
299 300 301 302 303 304 305 306 307 308 309
    pop(4);
    return (*cont)(igs);
}

/* ------ Initialization procedure ------ */

const op_def zchar42_op_defs[] =
{
    {"4.type42execchar", ztype42execchar},
    op_def_end(0)
};