Tscope5
textio.c
Go to the documentation of this file.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 #include "../include/tscope5/textio.h"
13 #include "../include/tscope5/display.h"
14 #include "../include/tscope5/graphics.h"
15 #include "../include/tscope5/textio_internal.h"
16 #include "../include/tscope5/system_internal.h"
17 #include "../include/tscope5/keyboard_internal.h"
18 #include "../include/tscope5/graphics_internal.h"
19 
20 
21 #include <allegro5/allegro_font.h>
22 #include <allegro5/allegro_ttf.h>
23 
24 
39 
40 
41 
42 
52 double ts5_printf(double x, double y, const char *format, ...)
53 {
54  ts5_check_textio("ts5_printf");
55  ts5_log(TS5_LOGLEVEL_5, "ts5_printf(%f,%f,...)\n", x, y);
56 
57  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
60  }
61 
62  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
64  y = ts5_cartesian_to_display_coordinate_y(y + _ts5_status.graphics_status.font_size / 2.0);
65  }
66 
67  TS5_USTR *buf;
68  va_list ap;
69  double width;
70 
71  va_start(ap, format);
72  buf = al_ustr_new("");
73  al_ustr_vappendf(buf, format, ap);
74  va_end(ap);
75 
76  al_draw_ustr(_ts5_status.graphics_status.font, _ts5_status.graphics_status.foreground_color,
77  x, y, _ts5_status.graphics_status.text_alignment, buf);
78 
79  width = al_get_ustr_width(_ts5_status.graphics_status.font, buf);
80  al_ustr_free(buf);
81 
82  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
83  width/=_ts5_status.graphics_status.target_width;
84  }
85 
86  return width;
87 }
88 
89 
99 void ts5_printf_justify(double x1, double x2, double y, const double diff, const char *format, ...)
100 {
101  ts5_check_textio("ts5_printf_justify");
102  ts5_log(TS5_LOGLEVEL_5, "ts5_printf_justify(%f,%f,%f,%f,...)\n", x1, x2, y, diff);
103 
104  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
108  }
109 
110  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
113  y = ts5_cartesian_to_display_coordinate_y(y + _ts5_status.graphics_status.font_size / 2.0);
114  }
115 
116  TS5_USTR *buf;
117  va_list ap;
118 
119  va_start(ap, format);
120  buf = al_ustr_new("");
121  al_ustr_vappendf(buf, format, ap);
122  va_end(ap);
123 
124  al_draw_justified_ustr(_ts5_status.graphics_status.font, _ts5_status.graphics_status.foreground_color,
125  x1, x2, y, diff, _ts5_status.graphics_status.text_alignment, buf);
126 }
127 
128 
138 double ts5_printf_ustr(double x, double y, const TS5_USTR *ustr)
139 {
140  ts5_check_textio("ts5_printf_ustr");
141  ts5_log(TS5_LOGLEVEL_5, "ts5_printf_ustr(%f,%f,...)\n", x, y);
142 
143  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
146  }
147 
148  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
150  y = ts5_cartesian_to_display_coordinate_y(y + _ts5_status.graphics_status.font_size / 2.0);
151  }
152 
153  double width;
154  al_draw_ustr(_ts5_status.graphics_status.font, _ts5_status.graphics_status.foreground_color,
155  x, y, _ts5_status.graphics_status.text_alignment, ustr);
156 
157  width = al_get_ustr_width(_ts5_status.graphics_status.font, ustr);
158 
159  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
160  width/=_ts5_status.graphics_status.target_width;
161  }
162 
163  return width;
164 }
165 
166 
176 void ts5_printf_justify_ustr(double x1, double x2, double y, const double diff, const TS5_USTR *ustr)
177 {
178  ts5_check_textio("ts5_printf_justify_ustr");
179  ts5_log(TS5_LOGLEVEL_5, "ts5_printf_justify_ustr(%f,%f,%f,%f,...)\n", x1, x2, y, diff);
180 
181  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
185  }
186 
187  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
190  y = ts5_cartesian_to_display_coordinate_y(y + _ts5_status.graphics_status.font_size / 2.0);
191  }
192 
193  al_draw_justified_ustr(_ts5_status.graphics_status.font, _ts5_status.graphics_status.foreground_color,
194  x1, x2, y, diff, _ts5_status.graphics_status.text_alignment, ustr);
195 }
196 
197 
207 int ts5_scanf(double x, double y, const char *format, ...)
208 {
209  ts5_check_textio("ts5_scanf");
210  ts5_check_keyboard("ts5_scanf");
211  ts5_log(TS5_LOGLEVEL_5, "ts5_scanf(%f,%f,...)\n", x, y);
212 
213  char str[TS5_MAX_CHAR];
214  int done = 0, pos = 0;
215  int ascii;
216  TS5_COLOR fg = _ts5_status.graphics_status.foreground_color;
217  TS5_COLOR bg = _ts5_status.graphics_status.background_color;
218 
219  ALLEGRO_EVENT_QUEUE *event_queue;
220  event_queue = al_create_event_queue();
221 
222  if (!event_queue) {
223  ts5_fatal("ts5_scanf: could not create event cue for keyboard\n");
224  return 1;
225  }
226 
227  al_register_event_source(event_queue, al_get_keyboard_event_source());
228 
229  ALLEGRO_EVENT event;
230 
231  do {
232  al_wait_for_event(event_queue, &event);
233 
234  if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
235 
237  ts5_printf(x, y, "%s", str);
238 
239  ascii = event.keyboard.unichar;
240  if (ascii == 3 || ascii == 13) {
241  done = 1;
242  }
243  else if (ascii == 8 || ascii == 127) {
244  if (pos > 0)
245  pos--;
246  }
247  else if (ascii == 0) {
248 
249  }
250  else {
251  str[pos] = ascii;
252  pos++;
253  }
254 
255  if (pos > TS5_MAX_CHAR) {
256  pos = TS5_MAX_CHAR;
257  }
258  str[pos] = '\0';
260  ts5_printf(x, y, "%s", str);
262  }
263  } while (!done);
264 
265  al_unregister_event_source(event_queue, al_get_keyboard_event_source());
266  al_destroy_event_queue(event_queue);
267 
268  int i;
269  va_list args;
270  va_start(args, format);
271  i = vsscanf(str, format, args);
272  va_end(args);
273 
274  return i;
275 }
276 
277 
285 double ts5_get_text_width(const char *format, ...)
286 {
287  ts5_check_textio("ts5_get_text_width");
288  ts5_log(TS5_LOGLEVEL_5, "ts5_get_text_width(...)\n");
289 
290  TS5_USTR *buf;
291  va_list ap;
292  int width;
293 
294  va_start(ap, format);
295  buf = al_ustr_new("");
296  al_ustr_vappendf(buf, format, ap);
297  va_end(ap);
298 
299  width = al_get_ustr_width(_ts5_status.graphics_status.font, buf);
300  al_ustr_free(buf);
301 
302  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
303  width/=_ts5_status.graphics_status.target_width;
304  }
305 
306  return width;
307 }
308 
309 
317 double ts5_get_text_width_ustr(TS5_USTR *ustr)
318 {
319  ts5_check_textio("ts5_get_text_width_ustr");
320  ts5_log(TS5_LOGLEVEL_5, "ts5_get_text_width_ustr(...)\n");
321 
322  double width;
323  width = al_get_ustr_width(_ts5_status.graphics_status.font, ustr);
324 
325  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
326  width/=_ts5_status.graphics_status.target_width;
327  }
328 
329  return width;
330 }
331 
332 
334 
335 
336 
337 
342 
343 
344 
345 
356 TS5_FONT *ts5_read_font(const char *fontfile, double size)
357 {
358  ts5_check_textio("ts5_read_font");
359  ts5_log(TS5_LOGLEVEL_4, "ts5_read_font(%s,%f)\n", fontfile, size);
360 
361  ts5_log(TS5_LOGLEVEL_2, "ts5_read_font: loading font %s %f\n", fontfile, size);
362  TS5_FONT *font;
363  font = al_load_ttf_font(fontfile, -size, 0);
364 
365  if (!font) {
366  ts5_fatal("ts5_read_font: could not load font %s %d\n", fontfile,
367  _ts5_status.graphics_status.font_size);
368  }
369 
370  return font;
371 }
372 
373 
381 TS5_FONT *ts5_set_font(TS5_FONT *font)
382 {
383  ts5_check_textio("ts5_set_font");
384  ts5_log(TS5_LOGLEVEL_4, "ts5_set_font(%p)\n", font);
385 
386  if (font == NULL) {
387  ts5_fatal("ts5_set_font: font is a NULL pointer\n");
388  }
389 
390  TS5_FONT *retval = _ts5_status.graphics_status.font;
391  _ts5_status.graphics_status.font = font;
392  _ts5_status.graphics_status.font_index = TS5_USERFONT;
393 
394  _ts5_status.graphics_status.font_size = al_get_font_line_height(_ts5_status.graphics_status.font);
395 
396  return retval;
397 }
398 
399 
408 void ts5_free_font(TS5_FONT *font)
409 {
410  ts5_check_textio("ts5_free_font");
411  ts5_log(TS5_LOGLEVEL_2, "ts5_free_font(%p)\n", font);
412 
413  if (font) {
414  al_destroy_font(font);
415  ts5_log(TS5_LOGLEVEL_2, "ts5_free_font: removed font\n");
416  font = NULL;
417  }
418 }
419 
420 
422 
423