xref: /plan9/sys/src/cmd/gs/src/gsfcid.c (revision 6a9fc400c33447ef5e1cda7185cb4de2c8e8010e)
1 /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of AFPL Ghostscript.
4 
5   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
6   distributor accepts any responsibility for the consequences of using it, or
7   for whether it serves any particular purpose or works at all, unless he or
8   she says so in writing.  Refer to the Aladdin Free Public License (the
9   "License") for full details.
10 
11   Every copy of AFPL Ghostscript must include a copy of the License, normally
12   in a plain ASCII text file named PUBLIC.  The License grants you the right
13   to copy, modify and redistribute AFPL Ghostscript, but only under certain
14   conditions described in the License.  Among other things, the License
15   requires that the copyright notice and this notice be preserved on all
16   copies.
17 */
18 
19 /*$Id: gsfcid.c,v 1.5 2000/11/23 23:34:22 lpd Exp $ */
20 /* Support for CID-keyed fonts */
21 #include "memory_.h"
22 #include "gx.h"
23 #include "gsmatrix.h"		/* for gsfont.h */
24 #include "gsstruct.h"
25 #include "gxfcid.h"
26 
27 /* CIDSystemInfo structure descriptors */
28 public_st_cid_system_info();
29 public_st_cid_system_info_element();
30 
31 /* CID-keyed font structure descriptors */
32 public_st_gs_font_cid_data();
33 public_st_gs_font_cid0();
34 private
35 ENUM_PTRS_WITH(font_cid0_enum_ptrs, gs_font_cid0 *pfcid0)
36 {
37     index -= 2;
38     if (index < st_gs_font_cid_data_num_ptrs)
39 	return ENUM_USING(st_gs_font_cid_data, &pfcid0->cidata.common,
40 			  sizeof(gs_font_cid_data), index);
41     ENUM_PREFIX(st_gs_font_base, st_gs_font_cid_data_num_ptrs);
42 }
43 ENUM_PTR(0, gs_font_cid0, cidata.FDArray);
44 ENUM_PTR(1, gs_font_cid0, cidata.proc_data);
45 ENUM_PTRS_END
46 private
47 RELOC_PTRS_WITH(font_cid0_reloc_ptrs, gs_font_cid0 *pfcid0);
48     RELOC_PREFIX(st_gs_font_base);
49     RELOC_USING(st_gs_font_cid_data, &pfcid0->cidata.common,
50 		sizeof(st_gs_font_cid_data));
51     RELOC_VAR(pfcid0->cidata.FDArray);
52     RELOC_VAR(pfcid0->cidata.proc_data);
53 RELOC_PTRS_END
54 public_st_gs_font_cid1();
55 private
56 ENUM_PTRS_WITH(font_cid1_enum_ptrs, gs_font_cid1 *pfcid1)
57 {
58     if (index < st_cid_system_info_num_ptrs)
59 	return ENUM_USING(st_cid_system_info, &pfcid1->cidata.CIDSystemInfo,
60 			  sizeof(st_cid_system_info), index);
61     ENUM_PREFIX(st_gs_font_base, st_cid_system_info_num_ptrs);
62 }
63 ENUM_PTRS_END
64 private
65 RELOC_PTRS_WITH(font_cid1_reloc_ptrs, gs_font_cid1 *pfcid1);
66     RELOC_PREFIX(st_gs_font_base);
67     RELOC_USING(st_cid_system_info, &pfcid1->cidata.CIDSystemInfo,
68 		sizeof(st_cid_system_info));
69 RELOC_PTRS_END
70 public_st_gs_font_cid2();
71 private
72 ENUM_PTRS_WITH(font_cid2_enum_ptrs, gs_font_cid2 *pfcid2)
73 {
74     if (index < st_gs_font_cid_data_num_ptrs)
75 	return ENUM_USING(st_gs_font_cid_data, &pfcid2->cidata.common,
76 			  sizeof(gs_font_cid_data), index);
77     ENUM_PREFIX(st_gs_font_type42, st_gs_font_cid_data_num_ptrs);
78 }
79 ENUM_PTRS_END
80 private
81 RELOC_PTRS_WITH(font_cid2_reloc_ptrs, gs_font_cid2 *pfcid2);
82     RELOC_PREFIX(st_gs_font_type42);
83     RELOC_USING(st_gs_font_cid_data, &pfcid2->cidata.common,
84 		sizeof(st_gs_font_cid_data));
85 RELOC_PTRS_END
86 
87 /*
88  * The CIDSystemInfo of a CMap may be null.  We represent this by setting
89  * Registry and Ordering to empty strings, and Supplement to 0.
90  */
91 void
92 cid_system_info_set_null(gs_cid_system_info_t *pcidsi)
93 {
94     memset(pcidsi, 0, sizeof(*pcidsi));
95 }
96 bool
97 cid_system_info_is_null(const gs_cid_system_info_t *pcidsi)
98 {
99     return (pcidsi->Registry.size == 0 && pcidsi->Ordering.size == 0 &&
100 	    pcidsi->Supplement == 0);
101 }
102 
103 /*
104  * Get the CIDSystemInfo of a font.  If the font is not a CIDFont,
105  * return NULL.
106  */
107 const gs_cid_system_info_t *
108 gs_font_cid_system_info(const gs_font *pfont)
109 {
110     switch (pfont->FontType) {
111     case ft_CID_encrypted:
112 	return &((const gs_font_cid0 *)pfont)->cidata.common.CIDSystemInfo;
113     case ft_CID_user_defined:
114 	return &((const gs_font_cid1 *)pfont)->cidata.CIDSystemInfo;
115     case ft_CID_TrueType:
116 	return &((const gs_font_cid2 *)pfont)->cidata.common.CIDSystemInfo;
117     default:
118 	return 0;
119     }
120 }
121 
122 /*
123  * Provide a default enumerate_glyph procedure for CIDFontType 0 fonts.
124  * Built for simplicity, not for speed.
125  */
126 font_proc_enumerate_glyph(gs_font_cid0_enumerate_glyph); /* check prototype */
127 int
128 gs_font_cid0_enumerate_glyph(gs_font *font, int *pindex,
129 			     gs_glyph_space_t ignore_glyph_space,
130 			     gs_glyph *pglyph)
131 {
132     gs_font_cid0 *const pfont = (gs_font_cid0 *)font;
133 
134     while (*pindex < pfont->cidata.common.CIDCount) {
135 	gs_const_string gstr;
136 	int fidx;
137 	gs_glyph glyph = (gs_glyph)(gs_min_cid_glyph + (*pindex)++);
138 	int code = pfont->cidata.glyph_data((gs_font_base *)pfont, glyph,
139 					    &gstr, &fidx);
140 
141 	if (code < 0 || gstr.size == 0)
142 	    continue;
143 	*pglyph = glyph;
144 	if (code > 0)
145 	    gs_free_const_string(font->memory, gstr.data, gstr.size,
146 				 "gs_font_cid0_enumerate_glyphs");
147 	return 0;
148     }
149     *pindex = 0;
150     return 0;
151 }
152