GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71793
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
font.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 
4 #include <grass/gis.h>
5 #include "driver.h"
6 #include "driverlib.h"
7 
8 static int font_type = GFONT_STROKE;
9 static char *encoding;
10 
11 static void stroke_set(const char *filename)
12 {
13  if (font_init(filename) == 0)
14  font_type = GFONT_STROKE;
15 }
16 
17 static void freetype_set(const char *filename, int index)
18 {
19  if (font_init_freetype(filename, index) == 0)
20  font_type = GFONT_FREETYPE;
21 }
22 
23 static void driver_set(const char *name)
24 {
25  (*driver->Set_font)(name);
26  font_type = GFONT_DRIVER;
27 }
28 
29 int font_get_type(void)
30 {
31  return font_type;
32 }
33 
34 const char *font_get_encoding(void)
35 {
36  if (!encoding)
37  encoding = G_store("ISO-8859-1");
38  return encoding;
39 }
40 
41 static void font_list(char ***list, int *count, int verbose)
42 {
43  char **fonts;
44  int num_fonts;
45  int i;
46 
47  for (i = 0; ftcap[i].name; i++)
48  ;
49  num_fonts = i;
50 
51  G_debug(2, "font_list: num_fonts=%d", num_fonts);
52  fonts = G_malloc(num_fonts * sizeof(const char *));
53 
54  for (i = 0; i < num_fonts; i++) {
55  struct GFONT_CAP *p = &ftcap[i];
56 
57  G_debug(4, "font: %d (%s)", i, p->name);
58  if (verbose) {
59  char buf[GPATH_MAX];
60 
61  sprintf(buf, "%s|%s|%d|%s|%d|%s|",
62  p->name, p->longname, p->type,
63  p->path, p->index, p->encoding);
64 
65  fonts[i] = G_store(buf);
66  }
67  else
68  fonts[i] = G_store(p->name);
69  }
70 
71  *list = fonts;
72  *count = num_fonts;
73 }
74 
75 static void free_font_list(char **fonts, int count)
76 {
77  int i;
78 
79  for (i = 0; i < count; i++)
80  G_free(fonts[i]);
81 
82  G_free(fonts);
83 }
84 
85 void COM_Set_font(const char *name)
86 {
87  int i;
88 
89  if (G_is_absolute_path(name)) {
90  if (font_exists(name))
91  freetype_set(name, 0);
92  return;
93  }
94 
95  for (i = 0; ftcap[i].name; i++) {
96  struct GFONT_CAP *cap = &ftcap[i];
97 
98  if (strcmp(name, cap->name) != 0)
99  continue;
100 
101  switch (cap->type) {
102  case GFONT_FREETYPE:
103  freetype_set(cap->path, cap->index);
105  break;
106  case GFONT_STROKE:
107  stroke_set(cap->name);
108  break;
109  }
110  return;
111  }
112 
113 
114  if (driver->Font_info && driver->Set_font) {
115  char **list = NULL;
116  int count = 0;
117 
118  (*driver->Font_info)(&list, &count);
119 
120  for (i = 0; i < count; i++) {
121  struct GFONT_CAP cap;
122 
123  if (!parse_fontcap_entry(&cap, list[i]))
124  continue;
125 
126  if (cap.type != GFONT_DRIVER || strcmp(name, cap.name) != 0)
127  continue;
128 
129  driver_set(cap.name);
131  break;
132  }
133 
134  free_font_list(list, count);
135  return;
136  }
137 
138  stroke_set("romans");
139 }
140 
141 void COM_Set_encoding(const char *enc)
142 {
143  if (encoding)
144  G_free(encoding);
145 
146  encoding = G_store(enc);
147 }
148 
149 void COM_Font_list(char ***list, int *count)
150 {
151  font_list(list, count, 0);
152  if (driver->Font_list)
153  (*driver->Font_list)(list, count);
154 }
155 
156 void COM_Font_info(char ***list, int *count)
157 {
158  font_list(list, count, 1);
159  if (driver->Font_info)
160  (*driver->Font_info)(list, count);
161 }
void(* Font_info)(char ***, int *)
Definition: driver.h:54
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
int type
Definition: fontcap.h:10
int font_exists(const char *)
Check if font exists.
Definition: parse_ftcap.c:27
int font_get_type(void)
Definition: font.c:29
const char * font_get_encoding(void)
Definition: font.c:34
void(* Font_list)(char ***, int *)
Definition: driver.h:53
int font_init(const char *)
Definition: font2.c:180
int count
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
void COM_Set_font(const char *)
Definition: font.c:85
#define NULL
Definition: ccmath.h:32
char * path
Definition: fontcap.h:8
#define GFONT_STROKE
Definition: fontcap.h:14
struct GFONT_CAP * ftcap
Definition: driver/init.c:27
char * encoding
Definition: fontcap.h:11
int font_init_freetype(const char *, int)
Definition: font_freetype.c:9
void COM_Font_list(char ***, int *)
Definition: font.c:149
void COM_Set_encoding(const char *)
Definition: font.c:141
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
#define GFONT_FREETYPE
Definition: fontcap.h:15
int index
Definition: fontcap.h:9
#define GPATH_MAX
Definition: gis.h:151
void(* Set_font)(const char *)
Definition: driver.h:52
void COM_Font_info(char ***, int *)
Definition: font.c:156
char * longname
Definition: fontcap.h:7
Definition: driver.h:22
int parse_fontcap_entry(struct GFONT_CAP *, const char *)
Parse fontcap entry.
Definition: parse_ftcap.c:41
Definition: manage.h:4
#define GFONT_DRIVER
Definition: fontcap.h:16
int G_is_absolute_path(const char *path)
Checks if a specified path looks like an absolute path on the host system.
Definition: paths.c:62
const char * name
Definition: named_colr.c:7
char * name
Definition: fontcap.h:6