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  char path[TS5_MAX_CHAR];
61  char path_with_prefix[TS5_MAX_CHAR];
62 
63  if (_ts5_status.graphics.font_type == TS5_COURIER) {
64  sprintf(path, "Courier New");
65  }
66  else if (_ts5_status.graphics.font_type == TS5_ARIAL) {
67  sprintf(path, "Arial");
68  }
69  else if (_ts5_status.graphics.font_type == TS5_TIMES) {
70  sprintf(path, "Times New Roman");
71  }
72  else {
73  ts5_fatal("%s: invalid font type (%d)\n", calling_function,
74  _ts5_status.graphics.font_type);
75  }
76 
77  if (_ts5_status.graphics.font_style == TS5_REGULAR) {
78  }
79  else if (_ts5_status.graphics.font_style == TS5_BOLD) {
80  sprintf(path, "%s Bold", path);
81  }
82  else if (_ts5_status.graphics.font_style == TS5_ITALIC) {
83  sprintf(path, "%s Italic", path);
84  }
85  else if (_ts5_status.graphics.font_style == TS5_BOLD_ITALIC) {
86  sprintf(path, "%s Bold Italic", path);
87  }
88  else {
89  ts5_fatal("%s: invalid font style (%d)\n", calling_function,
90  _ts5_status.graphics.font_style);
91  }
92  sprintf(path, "%s.ttf", path);
93 
94  // look in the program directory
95  sprintf(path_with_prefix, "%s", path);
96  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index] =
97  al_load_font(path_with_prefix,
98  _ts5_status.graphics.font_size, 0);
99 
100  // look in the fonts subdirectory
101  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]) {
102  sprintf(path_with_prefix, "fonts/%s", path);
103  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index] =
104  al_load_font(path_with_prefix,
105  _ts5_status.graphics.font_size, 0);
106  }
107 
108  #ifndef TS5_WINDOWS
109 
110  // look in /usr/local/share/tscope5_data/
111  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]) {
112 
113  sprintf(path_with_prefix, "/usr/local/share/tscope5_data/%s", path);
114  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index] =
115  al_load_font(path_with_prefix,
116  _ts5_status.graphics.font_size, 0);
117  }
118 
119  #else
120 
121  // look in c:\mingw4tscope5\usr\local\share\tscope5_data
122  if (!_ts5_status.graphics.fonts[_ts5_status.graphics.font_index]) {
123 
124 
125  char *systemdrive = getenv("SYSTEMDRIVE");
126  if (!systemdrive) {
127  ts5_fatal("%s: could not determine system drive letter\n",
128  calling_function);
129  }
130 
131  sprintf(path_with_prefix, "%s%s%s", systemdrive,
132  "/MinGW4Tscope5/msys/1.0/local/share/tscope5_data/",
133  path);
134 
135  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index] =
136  al_load_font(path_with_prefix,
137  _ts5_status.graphics.font_size, 0);
138  }
139  #endif
140 
141 
142  ts5_log(TS5_LOGLEVEL_2, "%s: %s %s %d (index %d)\n",
143  calling_function, "loading font", path,
144  _ts5_status.graphics.font_size,
145  _ts5_status.graphics.font_index);
146 
147  _ts5_status.graphics.font =
148  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index];
149 
150  if (!_ts5_status.graphics.font) {
151  ts5_fatal("%s: could not load font %s %d\n", calling_function, path,
152  _ts5_status.graphics.font_size);
153  }
154  }
155  else {
156  _ts5_status.graphics.font =
157  _ts5_status.graphics.fonts[_ts5_status.graphics.font_index];
158  }
159 }
160 
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 /// Install the textio subsystem.
164 ///
165 /// \param calling_function Name the function that calls
166 /// for this check or installation.
167 ///
168 /// This function is called automatically if necessary.
169 ////////////////////////////////////////////////////////////////////////////////
170 void ts5_install_textio(char *calling_function)
171 {
173  ts5_install_tscope5(calling_function);
174  }
175 
177  ts5_install_bitmaps(calling_function);
178  }
179 
181 
183  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 text input/output\n",
184  calling_function);
185 
186  al_init_font_addon(); // does not return succes/failure (yet)
187 
188  if (!al_init_ttf_addon()) {
189  ts5_fatal("%s: could not install Tscope5 text input/output\n",
190  calling_function);
191  }
192 
193  atexit(ts5_uninstall_textio);
194  }
195 }
196 
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Uninstall the textio subsystem.
200 ///
201 /// This function is called automatically at the end of the program.
202 ////////////////////////////////////////////////////////////////////////////////
204 {
206 
207  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 text input/output\n");
208 
209  int i;
210  for (i = 0; i < TS5_NFONTS; i++) {
211 
212  if (_ts5_status.graphics.fonts[i] != NULL) {
213 
214  #ifdef TS5_MACOSX
215  al_destroy_font(_ts5_status.graphics.fonts[i]);
216  _ts5_status.graphics.fonts[i] = NULL;
217 
218  #endif
219 
220  ts5_log(TS5_LOGLEVEL_2, "%s: %s %d\n",
221  "Uninstalling Tscope5 text input/output",
222  "removed font", i);
223  }
224  }
225 
226  al_shutdown_ttf_addon();
227  al_shutdown_font_addon();
228 
230  }
231 }