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"
21 #include <allegro5/allegro_font.h>
22 #include <allegro5/allegro_ttf.h>
52 double ts5_printf(
double x,
double y,
const char *format, ...)
54 ts5_check_textio(
"ts5_printf");
55 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf(%f,%f,...)\n", x, y);
57 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
62 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
72 buf = al_ustr_new(
"");
73 al_ustr_vappendf(buf, format, ap);
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);
79 width = al_get_ustr_width(_ts5_status.graphics_status.font, buf);
82 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
83 width/=_ts5_status.graphics_status.target_width;
99 void ts5_printf_justify(
double x1,
double x2,
double y,
const double diff,
const char *format, ...)
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);
104 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
110 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
119 va_start(ap, format);
120 buf = al_ustr_new(
"");
121 al_ustr_vappendf(buf, format, ap);
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);
140 ts5_check_textio(
"ts5_printf_ustr");
141 ts5_log(TS5_LOGLEVEL_5,
"ts5_printf_ustr(%f,%f,...)\n", x, y);
143 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
148 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
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);
157 width = al_get_ustr_width(_ts5_status.graphics_status.font, ustr);
159 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
160 width/=_ts5_status.graphics_status.target_width;
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);
181 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
187 if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
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);
207 int ts5_scanf(
double x,
double y,
const char *format, ...)
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);
213 char str[TS5_MAX_CHAR];
214 int done = 0, pos = 0;
216 TS5_COLOR fg = _ts5_status.graphics_status.foreground_color;
217 TS5_COLOR bg = _ts5_status.graphics_status.background_color;
219 ALLEGRO_EVENT_QUEUE *event_queue;
220 event_queue = al_create_event_queue();
223 ts5_fatal(
"ts5_scanf: could not create event cue for keyboard\n");
227 al_register_event_source(event_queue, al_get_keyboard_event_source());
232 al_wait_for_event(event_queue, &event);
234 if (event.type == ALLEGRO_EVENT_KEY_CHAR) {
239 ascii =
event.keyboard.unichar;
240 if (ascii == 3 || ascii == 13) {
243 else if (ascii == 8 || ascii == 127) {
247 else if (ascii == 0) {
255 if (pos > TS5_MAX_CHAR) {
265 al_unregister_event_source(event_queue, al_get_keyboard_event_source());
266 al_destroy_event_queue(event_queue);
270 va_start(args, format);
271 i = vsscanf(str, format, args);
287 ts5_check_textio(
"ts5_get_text_width");
288 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width(...)\n");
294 va_start(ap, format);
295 buf = al_ustr_new(
"");
296 al_ustr_vappendf(buf, format, ap);
299 width = al_get_ustr_width(_ts5_status.graphics_status.font, buf);
302 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
303 width/=_ts5_status.graphics_status.target_width;
319 ts5_check_textio(
"ts5_get_text_width_ustr");
320 ts5_log(TS5_LOGLEVEL_5,
"ts5_get_text_width_ustr(...)\n");
323 width = al_get_ustr_width(_ts5_status.graphics_status.font, ustr);
325 if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
326 width/=_ts5_status.graphics_status.target_width;
358 ts5_check_textio(
"ts5_read_font");
359 ts5_log(TS5_LOGLEVEL_4,
"ts5_read_font(%s,%f)\n", fontfile, size);
361 ts5_log(TS5_LOGLEVEL_2,
"ts5_read_font: loading font %s %f\n", fontfile, size);
363 font = al_load_ttf_font(fontfile, -size, 0);
366 ts5_fatal(
"ts5_read_font: could not load font %s %d\n", fontfile,
367 _ts5_status.graphics_status.font_size);
383 ts5_check_textio(
"ts5_set_font");
384 ts5_log(TS5_LOGLEVEL_4,
"ts5_set_font(%p)\n", font);
387 ts5_fatal(
"ts5_set_font: font is a NULL pointer\n");
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;
394 _ts5_status.graphics_status.font_size = al_get_font_line_height(_ts5_status.graphics_status.font);
410 ts5_check_textio(
"ts5_free_font");
411 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font(%p)\n", font);
414 al_destroy_font(font);
415 ts5_log(TS5_LOGLEVEL_2,
"ts5_free_font: removed font\n");