xref: /plan9/sys/src/cmd/gs/src/zfontenum.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2003 artofcode LLC.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: zfontenum.c,v 1.4 2004/08/04 19:36:13 stefan Exp $ */
18 
19 /* this is the ps interpreter interface to the native font
20    enumeration code. it calls the platform-specific routines
21    to obtain an additional set of entries that can be added
22    to the Fontmap to reference fonts stored on the system.
23  */
24 
25 #include "memory_.h"
26 #include "string_.h"
27 #include <stdlib.h>
28 #include "ghost.h"
29 #include "oper.h"
30 #include "gsstruct.h"
31 #include "gsmalloc.h"
32 #include "ialloc.h"
33 #include "iname.h"
34 #include "iutil.h"
35 #include "store.h"
36 #include "gp.h"
37 
38 
39 typedef struct fontenum_s {
40 	char *fontname, *path;
41 	struct fontenum_s *next;
42 } fontenum_t;
43 
44 /* .getnativefonts [ [<name> <path>] ... ] */
45 private int
z_fontenum(i_ctx_t * i_ctx_p)46 z_fontenum(i_ctx_t *i_ctx_p)
47 {
48     os_ptr op = osp;
49     void *enum_state;
50     int code = 0;
51     int e,elements;
52     char *fontname, *path;
53     fontenum_t *r, *results;
54     ref array;
55     uint length;
56     byte *string;
57 
58     enum_state = gp_enumerate_fonts_init(imemory);
59     if (enum_state == NULL) {
60       /* put false on the stack and return */
61       push(1);
62       make_bool(op, false);
63       return code;
64     }
65 
66     r = results = gs_malloc(imemory->non_gc_memory, 1, sizeof(fontenum_t), "fontenum list");
67     elements = 0;
68     while((code = gp_enumerate_fonts_next(enum_state, &fontname, &path )) > 0) {
69 	if (fontname == NULL || path == NULL) {
70 	    gp_enumerate_fonts_free(enum_state);
71 	    return_error(e_ioerror);
72 	}
73 
74 	length = strlen(fontname) + 1;
75 	r->fontname = gs_malloc(imemory->non_gc_memory, length, 1, "native font name");
76 	memcpy(r->fontname, fontname, length);
77 
78 	length = strlen(path) + 1;
79 	    r->path = gs_malloc(imemory->non_gc_memory, length, 1, "native font path");
80 	    memcpy(r->path, path, length);
81 
82 	    r->next = gs_malloc(imemory->non_gc_memory, 1, sizeof(fontenum_t), "fontenum list");
83 	    r = r->next;
84 	    elements += 1;
85 	}
86 
87 	gp_enumerate_fonts_free(enum_state);
88 
89 	code = ialloc_ref_array(&array, a_all | icurrent_space, elements, "native fontmap");
90 
91 	r = results;
92 	for (e = 0; e < elements; e++) {
93 	    ref mapping;
94 
95 	    code = ialloc_ref_array(&mapping, a_all | icurrent_space, 2, "native font mapping");
96 
97 	    length = strlen(r->fontname);
98 	    string = ialloc_string(length, "native font name");
99 	    if (string == NULL)
100 		return_error(e_VMerror);
101 	    memcpy(string, r->fontname, length);
102 	    make_string(&(mapping.value.refs[0]), a_all | icurrent_space, length, string);
103 
104 	    length = strlen(r->path);
105 	    string = ialloc_string(length, "native font path");
106 	    if (string == NULL)
107 		return_error(e_VMerror);
108 	    memcpy(string, r->path, length);
109 	    make_string(&(mapping.value.refs[1]), a_all | icurrent_space, length, string);
110 
111 	    ref_assign(&(array.value.refs[e]), &mapping);
112 	    results = r;
113 	    r = r->next;
114 
115 	    gs_free(imemory->non_gc_memory,
116 		    results->fontname, strlen(results->fontname) + 1, 1, "native font name");
117 	    gs_free(imemory->non_gc_memory,
118 		    results->path, strlen(results->path) + 1, 1, "native font path");
119 	    gs_free(imemory->non_gc_memory,
120 		    results, 1, sizeof(fontenum_t), "fontenum list");
121 	}
122 
123     push(2);
124     ref_assign(op-1, &array);
125     make_bool(op, true);
126 
127     return code;
128 }
129 
130 
131 /* match the above routines to their postscript filter names
132    this is how our 'private' routines get called externally */
133 const op_def zfontenum_op_defs[] = {
134     {"0.getnativefonts", z_fontenum},
135     op_def_end(0)
136 };
137