4 * This file contains routines to attempt to add fonts to a font pattern
5 * so that XCreateFontSet will not fail because the given font(s) do not
6 * contain all the characters required by the locale.
8 * The original code was apparently written by Tomohiro Kubota; see
9 * <http://www.debian.org/doc/manuals/intro-i18n/ch-examples.en.html#s13.4.5>.
11 * However, the code that this file is based on, was taken from:
13 * Copyright (c) 2013 - the Notion team
14 * Screen.cc for Blackbox - an X11 Window manager
15 * Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
16 * Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34 * DEALINGS IN THE SOFTWARE.
41 #include <ioncore/common.h>
42 #include <ioncore/global.h>
43 #include <ioncore/log.h>
45 #ifndef CF_FONT_ELEMENT_SIZE
46 #define CF_FONT_ELEMENT_SIZE 50
49 static const char *get_font_element(const char *pattern
, char *buf
,
59 while((v
=va_arg(va
, char *))!=NULL
){
60 p
=libtu_strcasestr(pattern
, v
);
62 strncpy(buf
, p
+1, bufsiz
-2);
70 strncpy(buf
, "*", bufsiz
);
75 static const char *get_font_size(const char *pattern
, int *size
)
81 for(p
=pattern
; 1; p
++){
83 if(p2
!=NULL
&& n
>1 && n
<72){
86 *size
=16; return NULL
;
89 if(n
>1 && n
<72 && p2
!=NULL
){
94 }else if(*p
>='0' && *p
<='9' && p2
!=NULL
){
103 XFontSet
de_create_font_in_current_locale(const char *fontname
)
106 char **missing
=NULL
, *def
="-";
109 LOG(DEBUG
, FONT
, "Creating fontset for: %s", fontname
);
111 fs
=XCreateFontSet(ioncore_g
.dpy
, fontname
, &missing
, &nmissing
, &def
);
115 LOG(DEBUG
, FONT
, "Found a font without missing charsets for %s, returning it.", fontname
);
118 LOG(INFO
, FONT
, "Found a font with %d missing charsets for %s:", nmissing
, fontname
);
119 for(i
=0;i
<nmissing
;i
++)
120 LOG(DEBUG
, FONT
, "* %s", missing
[i
]);
123 LOG(WARN
, FONT
, "Found no font for %s.", fontname
);
127 XFreeStringList(missing
);
132 XFontSet
de_create_font_in_c_locale(const char *fontname
)
138 LOG(DEBUG
, FONT
, "Trying to load %s with the C locale.", fontname
);
140 lc
=setlocale(LC_CTYPE
, NULL
);
141 if(lc
!=NULL
&& strcmp(lc
, "POSIX")!=0 && strcmp(lc
, "C")!=0)
144 setlocale(LC_CTYPE
, "C");
146 fs
=de_create_font_in_current_locale(fontname
);
149 setlocale(LC_CTYPE
, lcc
);
157 XFontSet
de_create_font_kludged(const char *fontname
)
160 #ifndef CF_NO_FONTSET_KLUDGE
162 char weight
[CF_FONT_ELEMENT_SIZE
], slant
[CF_FONT_ELEMENT_SIZE
];
165 LOG(DEBUG
, FONT
, "Doing the fontset_kludge with fontname %s.", fontname
);
167 get_font_element(fontname
, weight
, CF_FONT_ELEMENT_SIZE
,
168 "-medium-", "-bold-", "-demibold-", "-regular-", NULL
);
169 get_font_element(fontname
, slant
, CF_FONT_ELEMENT_SIZE
,
170 "-r-", "-i-", "-o-", "-ri-", "-ro-", NULL
);
171 get_font_size(fontname
, &pixel_size
);
173 if(!strcmp(weight
, "*"))
174 strncpy(weight
, "medium", CF_FONT_ELEMENT_SIZE
);
175 if(!strcmp(slant
, "*"))
176 strncpy(slant
, "r", CF_FONT_ELEMENT_SIZE
);
179 else if(pixel_size
>97)
182 if(ioncore_g
.enc_utf8
){
183 libtu_asprintf(&pattern2
,
185 "-misc-fixed-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
186 "-misc-fixed-*-*-*-*-%d-*-*-*-*-*-*-*",
187 fontname
, weight
, slant
, pixel_size
, pixel_size
);
189 libtu_asprintf(&pattern2
,
191 "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
192 "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*",
193 fontname
, weight
, slant
, pixel_size
, pixel_size
);
197 LOG(DEBUG
, FONT
, "no_fontset_kludge resulted in fontname %s", pattern2
);
199 fs
= de_create_font_in_current_locale(pattern2
);
208 XFontSet
de_create_font_set(const char *fontname
)
210 XFontSet fs
=de_create_font_in_current_locale(fontname
);
215 fs
=de_create_font_in_c_locale(fontname
);
220 fs
= de_create_font_kludged(fontname
);
225 /* The final fallback... */
226 warn(TR("Could not load font %s"), fontname
);
227 return de_create_font_in_current_locale("-*-*-*-*-*-*-*-*-*-*-*-*-*-*");