Tscope5
textio_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file textio_internal.c
11 /// Definitions of internal text input/output functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 #include "../include/tscope5/textio_internal.h"
15 #include "../include/tscope5/system_internal.h"
16 #include "../include/tscope5/bitmaps_internal.h"
17 
18 #include <allegro5/allegro_font.h>
19 #include <allegro5/allegro_ttf.h>
20 
21 /// Is the textio subsystem installed?
23 
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// Do some checks at the start of each textio function.
27 ///
28 /// \param calling_function Name the function that calls
29 /// for this check or installation.
30 ///
31 /// Checks whether the textio subsystem is installed.
32 /// If not, the textio subsystem is installed.
33 ///
34 /// Checks whether the drawing target is null. If null, the program is aborted.
35 ///
36 /// Checks wheter a font is loaded. If not, a font is loaded.
37 ///
38 /// The font files are looked for in the following places (in this order):
39 /// - the program directory
40 /// - the fonts subdirectory of the program directory
41 /// - /usr/local/share/tscope_data
42 ////////////////////////////////////////////////////////////////////////////////
43 void ts5_check_textio(char *calling_function)
44 {
45  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_textio\n", calling_function);
46 
48  ts5_install_textio(calling_function);
49  }
50 
51  if (!_ts5_data.target) {
52  ts5_fatal("%s: no drawing target specified\n", calling_function);
53  }
54 
55  if (_ts5_status.graphics.font_index == TS5_USERFONT) {
56 
57  }
58  else if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]) {
59 
60  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index] =
61  (TS5_FONT *)al_malloc(sizeof(TS5_FONT));
62 
63  char path[TS5_MAX_CHAR];
64  char path_with_prefix[TS5_MAX_CHAR];
65 
66  int factor = _ts5_status.display[_ts5_status.active_display].sampling_factor;
67 
68  if (_ts5_status.graphics.font_type == TS5_COURIER) {
69  sprintf(path, "Courier New");
70  }
71  else if (_ts5_status.graphics.font_type == TS5_ARIAL) {
72  sprintf(path, "Arial");
73  }
74  else if (_ts5_status.graphics.font_type == TS5_TIMES) {
75  sprintf(path, "Times New Roman");
76  }
77  else {
78  ts5_fatal("%s: invalid font type (%d)\n", calling_function,
79  _ts5_status.graphics.font_type);
80  }
81 
82  if (_ts5_status.graphics.font_style == TS5_REGULAR) {
83  }
84  else if (_ts5_status.graphics.font_style == TS5_BOLD) {
85  sprintf(path, "%s Bold", path);
86  }
87  else if (_ts5_status.graphics.font_style == TS5_ITALIC) {
88  sprintf(path, "%s Italic", path);
89  }
90  else if (_ts5_status.graphics.font_style == TS5_BOLD_ITALIC) {
91  sprintf(path, "%s Bold Italic", path);
92  }
93  else {
94  ts5_fatal("%s: invalid font style (%d)\n", calling_function,
95  _ts5_status.graphics.font_style);
96  }
97  sprintf(path, "%s.ttf", path);
98 
99  // look in the program directory
100  sprintf(path_with_prefix, "%s", path);
101 
102  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font =
103  al_load_font(path_with_prefix,
104  _ts5_status.graphics.font_size, 0);
105 
106  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->multisampled_font =
107  al_load_font(path_with_prefix,
108  _ts5_status.graphics.font_size * factor, 0);
109 
110  // look in the fonts subdirectory
111  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font) {
112 
113  sprintf(path_with_prefix, "fonts/%s", path);
114 
115  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font =
116  al_load_font(path_with_prefix,
117  _ts5_status.graphics.font_size, 0);
118 
119  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->multisampled_font =
120  al_load_font(path_with_prefix,
121  _ts5_status.graphics.font_size * factor, 0);
122  }
123 
124  #ifndef TS5_WINDOWS
125 
126  // look in /usr/local/share/tscope5_data/
127  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font) {
128 
129  sprintf(path_with_prefix, "/usr/local/share/tscope5_data/%s", path);
130 
131  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font =
132  al_load_font(path_with_prefix,
133  _ts5_status.graphics.font_size, 0);
134 
135  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->multisampled_font =
136  al_load_font(path_with_prefix,
137  _ts5_status.graphics.font_size * factor, 0);
138  }
139 
140  #else
141 
142  // look in c:\mingw4tscope5\usr\local\share\tscope5_data
143  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font) {
144 
145  char *systemdrive = getenv("SYSTEMDRIVE");
146  if (!systemdrive) {
147  ts5_fatal("%s: could not determine system drive letter\n",
148  calling_function);
149  }
150 
151  sprintf(path_with_prefix, "%s%s%s", systemdrive,
152  "/MinGW4Tscope5/msys/1.0/local/share/tscope5_data/",
153  path);
154 
155  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->aliased_font =
156  al_load_font(path_with_prefix,
157  _ts5_status.graphics.font_size, 0);
158 
159  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index]->multisampled_font =
160  al_load_font(path_with_prefix,
161  _ts5_status.graphics.font_size * factor, 0);
162  }
163  #endif
164 
165 
166  ts5_log(TS5_LOGLEVEL_2, "%s: %s %s %d (index %d)\n",
167  calling_function, "loading font", path,
168  _ts5_status.graphics.font_size,
169  _ts5_status.graphics.font_index);
170 
171  _ts5_status.graphics.font =
172  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index];
173 
174  if (!_ts5_status.graphics.font) {
175  ts5_fatal("%s: could not load font %s %d\n", calling_function, path,
176  _ts5_status.graphics.font_size);
177  }
178  }
179  else {
180  _ts5_status.graphics.font =
181  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index];
182  }
183 }
184 
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Install the textio subsystem.
188 ///
189 /// \param calling_function Name the function that calls
190 /// for this check or installation.
191 ///
192 /// This function is called automatically if necessary.
193 ////////////////////////////////////////////////////////////////////////////////
194 void ts5_install_textio(char *calling_function)
195 {
197  ts5_install_tscope5(calling_function);
198  }
199 
201  ts5_install_bitmaps(calling_function);
202  }
203 
205 
207  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 text input/output\n",
208  calling_function);
209 
210  al_init_font_addon(); // does not return succes/failure (yet)
211 
212  if (!al_init_ttf_addon()) {
213  ts5_fatal("%s: could not install Tscope5 text input/output\n",
214  calling_function);
215  }
216 
217  atexit(ts5_uninstall_textio);
218  }
219 }
220 
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// Uninstall the textio subsystem.
224 ///
225 /// This function is called automatically at the end of the program.
226 ////////////////////////////////////////////////////////////////////////////////
228 {
230 
231  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 text input/output\n");
232 
233  int i;
234  for (i = 0; i < TS5_NFONTS; i++) {
235 
236  if (_ts5_status.graphics.fonts[i] != NULL) {
237 
238  #ifdef TS5_MACOSX
239  al_destroy_font(_ts5_status.graphics.fonts[i]->aliased_font);
240  al_destroy_font(_ts5_status.graphics.fonts[i]->multisampled_font);
241  al_free(_ts5_status.graphics.fonts[i]);
242  _ts5_status.graphics.fonts[i] = NULL;
243 
244  #endif
245 
246  ts5_log(TS5_LOGLEVEL_2, "%s: %s %d\n",
247  "Uninstalling Tscope5 text input/output",
248  "removed font", i);
249  }
250  }
251 
252  al_shutdown_ttf_addon();
253  al_shutdown_font_addon();
254 
256  }
257 }
int _ts5_is_textio_installed
Is the textio subsystem installed?
int _ts5_is_tscope5_installed
Is Tscope5 installed?
void ts5_uninstall_textio()
Uninstall the textio subsystem.
void ts5_check_textio(char *calling_function)
Do some checks at the start of each textio function.
void ts5_install_bitmaps(char *calling_function)
Install the bitmaps subsystem.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_install_tscope5(char *calling_function)
Install Tscope5.
void ts5_install_textio(char *calling_function)
Install the textio subsystem.
int _ts5_is_bitmaps_installed
Is the bitmaps subsystem installed?
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533