20 #if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 10, 0) || defined(CAIRO_HAS_FC_FONT)
21 #define USE_FONTCONFIG 1
22 #include <fontconfig/fontconfig.h>
24 #define USE_FONTCONFIG 0
32 static char *convert(
const char *in)
45 char *p1 = (
char *)in;
50 if ((cd = iconv_open(
"UTF-8", encoding)) == (iconv_t)-1)
51 G_fatal_error(
_(
"Unable to convert from <%s> to UTF-8"), encoding);
53 ret = iconv(cd, &p1, &ilen, &p2, &olen);
60 G_warning(
_(
"Some characters could not be converted to UTF-8"));
64 const unsigned char *p1 = (
const unsigned char *)in;
65 unsigned char *p2 = (
unsigned char *)out;
68 for (i = j = 0; i < ilen; i++) {
74 p2[j++] = 0xC0 + (c >> 6);
75 p2[j++] = 0x80 + (c & 0x3F);
86 static void set_matrix(
void)
88 static cairo_matrix_t mat;
93 cairo_matrix_init_identity(&mat);
97 cairo_set_font_matrix(
cairo, &mat);
109 char *utf8 = convert(str);
117 cairo_show_text(
cairo, utf8);
132 char *utf8 = convert(str);
133 cairo_text_extents_t ext;
140 cairo_text_extents(
cairo, utf8, &ext);
144 *
l =
cur_x + ext.x_bearing;
145 *
r =
cur_x + ext.x_bearing + ext.width;
146 *
t =
cur_y + ext.y_bearing;
147 *
b =
cur_y + ext.y_bearing + ext.height;
150 static void set_font_toy(
const char *
name)
153 cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL;
154 cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL;
157 char *p = strrchr(font,
'-');
163 weight = CAIRO_FONT_WEIGHT_BOLD;
164 else if (strcasecmp(p,
"-italic") == 0)
165 slant = CAIRO_FONT_SLANT_ITALIC;
167 slant = CAIRO_FONT_SLANT_OBLIQUE;
174 cairo_select_font_face(
cairo, font, slant, weight);
181 static void fc_init(
void)
183 static int initialized;
191 static void set_font_fc(
const char *
name)
193 static cairo_font_face_t *face;
200 cairo_font_face_destroy(face);
204 pattern = FcNameParse((FcChar8 *)
name);
205 FcDefaultSubstitute(pattern);
206 FcConfigSubstitute(FcConfigGetCurrent(), pattern, FcMatchPattern);
207 pattern = FcFontMatch(FcConfigGetCurrent(), pattern, &result);
208 face = cairo_ft_font_face_create_for_pattern(pattern);
209 cairo_set_font_face(
cairo, face);
212 static void font_list_fc(
char ***
list,
int *
count,
int verbose)
217 char **fonts = *
list;
218 int num_fonts = *
count;
223 pattern = FcPatternCreate();
224 objset = FcObjectSetBuild(FC_FAMILY, FC_STYLE, (
char *)
NULL);
225 fontset = FcFontList(
NULL, pattern, objset);
227 fonts =
G_realloc(fonts, (num_fonts + fontset->nfont) *
sizeof(
char *));
229 for (i = 0; i < fontset->nfont; i++) {
231 FcPattern *pat = fontset->fonts[i];
232 FcChar8 *family = (FcChar8 *)
"", *style = (FcChar8 *)
"";
234 FcPatternGetString(pat, FC_FAMILY, 0, &family);
235 FcPatternGetString(pat, FC_STYLE, 0, &style);
238 sprintf(buf,
"%s:%s|%s:%s|%d|%s|%d|%s|", family, style, family,
241 sprintf(buf,
"%s:%s", family, style);
243 fonts[num_fonts++] =
G_store(buf);
246 FcObjectSetDestroy(objset);
247 FcPatternDestroy(pattern);
248 FcFontSetDestroy(fontset);
256 static const char *toy_fonts[12] = {
257 "sans",
"sans-italic",
"sans-bold",
"sans-bold-italic",
258 "serif",
"serif-italic",
"serif-bold",
"serif-bold-italic",
259 "mono",
"mono-italic",
"mono-bold",
"mono-bold-italic",
262 static const int num_toy_fonts = 12;
264 static int is_toy_font(
const char *
name)
268 for (i = 0; i < num_toy_fonts; i++)
283 if (is_toy_font(
name))
292 static void font_list_toy(
char ***
list,
int *
count,
int verbose)
294 char **fonts = *
list;
295 int num_fonts = *
count;
298 fonts =
G_realloc(fonts, (num_fonts + num_toy_fonts) *
sizeof(
char *));
300 for (i = 0; i < num_toy_fonts; i++) {
304 sprintf(buf,
"%s|%s|%d|%s|%d|%s|", toy_fonts[i], toy_fonts[i],
307 strcpy(buf, toy_fonts[i]);
308 fonts[num_fonts++] =
G_store(buf);
void Cairo_text_box(const char *str, double *t, double *b, double *l, double *r)
void Cairo_font_info(char ***list, int *count)
Get fonts into.
void Cairo_font_list(char ***list, int *count)
Get list of fonts.
void Cairo_Text(const char *str)
Draw text.
void Cairo_set_font(const char *name)
Set font.
GRASS cairo display driver - header file.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
char * G_store(const char *)
Copy string to allocated memory.
const char * font_get_encoding(void)