1 
2 /*
3  * Copyright 1991 by the Open Software Foundation
4  * Copyright 1993 by the TOSHIBA Corp.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name Open Software Foundation
11  * not be used in advertising or publicity pertaining to distribution of the
12  * software without specific, written prior permission.  Open Software
13  * Foundation makes no representations about the suitability of this
14  * software for any purpose.  It is provided "as is" without express or
15  * implied warranty.
16  *
17  * OPEN SOFTWARE FOUNDATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
18  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19  * FITNESS, IN NO EVENT SHALL OPEN SOFTWARE FOUNDATIONN BE
20  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  *		 M. Collins		OSF
26  *
27  *		 Katsuhisa Yano		TOSHIBA Corp.
28  */
29 
30 /*
31 
32 Copyright 1991, 1998  The Open Group
33 
34 Permission to use, copy, modify, distribute, and sell this software and its
35 documentation for any purpose is hereby granted without fee, provided that
36 the above copyright notice appear in all copies and that both that
37 copyright notice and this permission notice appear in supporting
38 documentation.
39 
40 The above copyright notice and this permission notice shall be included
41 in all copies or substantial portions of the Software.
42 
43 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
44 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
47 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
48 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
49 OTHER DEALINGS IN THE SOFTWARE.
50 
51 Except as contained in this notice, the name of The Open Group shall
52 not be used in advertising or otherwise to promote the sale, use or
53 other dealings in this Software without prior written authorization
54 from The Open Group.
55 
56 */
57 
58 
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif
62 #include "Xlibint.h"
63 #include "Xlcint.h"
64 #include <ctype.h>
65 #include <X11/Xos.h>
66 #include "reallocarray.h"
67 
68 
69 #define	XMAXLIST	256
70 
71 char **
_XParseBaseFontNameList(char * str,int * num)72 _XParseBaseFontNameList(
73     char           *str,
74     int            *num)
75 {
76     char           *plist[XMAXLIST];
77     char          **list;
78     char           *ptr, *psave;
79 
80     *num = 0;
81     if (!str || !*str) {
82 	return (char **)NULL;
83     }
84     while (*str && isspace(*str))
85 	str++;
86     if (!*str)
87 	return (char **)NULL;
88 
89     if (!(ptr = strdup(str))) {
90 	return (char **)NULL;
91     }
92 
93     psave = ptr;
94     /* somebody who specifies more than XMAXLIST basefontnames will lose */
95     while (*num < (sizeof plist / sizeof plist[0])) {
96 	char	*back;
97 
98 	plist[*num] = ptr;
99 	if ((ptr = strchr(ptr, ','))) {
100 	    back = ptr;
101 	} else {
102 	    back = plist[*num] + strlen(plist[*num]);
103 	}
104 	while (isspace(*(back - 1)))
105 	    back--;
106 	*back = '\0';
107 	(*num)++;
108 	if (!ptr)
109 	    break;
110 	ptr++;
111 	while (*ptr && isspace(*ptr))
112 	    ptr++;
113 	if (!*ptr)
114 	    break;
115     }
116     if (!(list = Xmallocarray((*num + 1), sizeof(char *)))) {
117 	Xfree(psave);
118 	return (char **)NULL;
119     }
120     memcpy((char *)list, (char *)plist, sizeof(char *) * (size_t) (*num));
121     *(list + *num) = NULL;
122 
123     return list;
124 }
125 
126 static char **
copy_string_list(char ** string_list,int list_count)127 copy_string_list(
128     char **string_list,
129     int list_count)
130 {
131     char **string_list_ret, **list_src, **list_dst, *dst;
132     int length, count;
133 
134     if (string_list == NULL || list_count <= 0)
135 	return (char **) NULL;
136 
137     string_list_ret = Xmallocarray(list_count, sizeof(char *));
138     if (string_list_ret == NULL)
139 	return (char **) NULL;
140 
141     list_src = string_list;
142     count = list_count;
143     for (length = 0; count-- > 0; list_src++)
144 	length = length + (int) strlen(*list_src) + 1;
145 
146     dst = Xmalloc(length);
147     if (dst == NULL) {
148 	Xfree(string_list_ret);
149 	return (char **) NULL;
150     }
151 
152     list_src = string_list;
153     count = list_count;
154     list_dst = string_list_ret;
155     for ( ;  count-- > 0; list_src++) {
156 	strcpy(dst, *list_src);
157 	*list_dst++ = dst;
158 	dst += strlen(dst) + 1;
159     }
160 
161     return string_list_ret;
162 }
163 
164 XFontSet
XCreateFontSet(Display * dpy,_Xconst char * base_font_name_list,char *** missing_charset_list,int * missing_charset_count,char ** def_string)165 XCreateFontSet (
166     Display        *dpy,
167     _Xconst char   *base_font_name_list,
168     char         ***missing_charset_list,
169     int            *missing_charset_count,
170     char          **def_string)
171 {
172     XOM om;
173     XOC oc;
174     XOMCharSetList *list;
175 
176     *missing_charset_list = NULL;
177     *missing_charset_count = 0;
178 
179     om = XOpenOM(dpy, NULL, NULL, NULL);
180     if (om == NULL)
181 	return (XFontSet) NULL;
182 
183     if ((oc = XCreateOC(om, XNBaseFontName, base_font_name_list, NULL))) {
184 	list = &oc->core.missing_list;
185 	oc->core.om_automatic = True;
186     } else
187 	list = &om->core.required_charset;
188 
189     *missing_charset_list = copy_string_list(list->charset_list,
190 					     list->charset_count);
191     *missing_charset_count = list->charset_count;
192 
193     if (list->charset_list && *missing_charset_list == NULL)
194 	oc = NULL;
195 
196     if (oc && def_string) {
197 	*def_string = oc->core.default_string;
198 	if (!*def_string)
199 	    *def_string = (char *)"";
200     }
201 
202     if (oc == NULL)
203 	XCloseOM(om);
204 
205     return (XFontSet) oc;
206 }
207 
208 int
XFontsOfFontSet(XFontSet font_set,XFontStruct *** font_struct_list,char *** font_name_list)209 XFontsOfFontSet(
210     XFontSet        font_set,
211     XFontStruct  ***font_struct_list,
212     char         ***font_name_list)
213 {
214     *font_name_list   = font_set->core.font_info.font_name_list;
215     *font_struct_list = font_set->core.font_info.font_struct_list;
216     return font_set->core.font_info.num_font;
217 }
218 
219 char *
XBaseFontNameListOfFontSet(XFontSet font_set)220 XBaseFontNameListOfFontSet(XFontSet font_set)
221 {
222     return font_set->core.base_name_list;
223 }
224 
225 char *
XLocaleOfFontSet(XFontSet font_set)226 XLocaleOfFontSet(XFontSet font_set)
227 {
228     return font_set->core.om->core.lcd->core->name;
229 }
230 
231 Bool
XContextDependentDrawing(XFontSet font_set)232 XContextDependentDrawing(XFontSet font_set)
233 {
234     return font_set->core.om->core.context_dependent;
235 }
236 
237 Bool
XDirectionalDependentDrawing(XFontSet font_set)238 XDirectionalDependentDrawing(XFontSet font_set)
239 {
240     return font_set->core.om->core.directional_dependent;
241 }
242 
243 Bool
XContextualDrawing(XFontSet font_set)244 XContextualDrawing(XFontSet font_set)
245 {
246     return font_set->core.om->core.contextual_drawing;
247 }
248 
249 XFontSetExtents *
XExtentsOfFontSet(XFontSet font_set)250 XExtentsOfFontSet(XFontSet font_set)
251 {
252     if (!font_set)
253 	return NULL;
254     return &font_set->core.font_set_extents;
255 }
256 
257 void
XFreeFontSet(Display * dpy,XFontSet font_set)258 XFreeFontSet(
259     Display        *dpy,
260     XFontSet        font_set)
261 {
262     XCloseOM(font_set->core.om);
263 }
264