summaryrefslogtreecommitdiff
path: root/test/fonts.cxx
blob: 694b1d7bf7812633e910fc3b8b8da95c6b6284de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Font demo program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Tile.H>
#include <FL/Fl_Hold_Browser.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <FL/fl_ask.H>
#include <FL/Fl_File_Chooser.H>

Fl_Double_Window *form;
Fl_Tile *tile;
Fl_Window *vector_font_editor = 0;

class FontDisplay : public Fl_Widget {
  void draw();
public:
  int font, size;
  FontDisplay(Fl_Boxtype B, int X, int Y, int W, int H, const char* L = 0) :
    Fl_Widget(X,Y,W,H,L) {box(B); font = 0; size = 14;}
};
void FontDisplay::draw() {
  draw_box();
  fl_font((Fl_Font)font, size);
  fl_color(FL_BLACK);
  fl_draw(label(), x()+3, y()+3, w()-6, h()-6, align());
}

FontDisplay *textobj;

Fl_Hold_Browser *fontobj, *sizeobj;

int **sizes;
int *numsizes;
int pickedsize = 14;

void font_cb(Fl_Widget *, long) {
  int fn = fontobj->value();
  if (!fn) return;
  fn--;
  textobj->font = fn;
  sizeobj->clear();
  int n = numsizes[fn];
  int *s = sizes[fn];
  if (!n) {
    // no sizes
  } else if (s[0] == 0) {
    // many sizes;
    int j = 1;
    for (int i = 1; i<64 || i<s[n-1]; i++) {
      char buf[20];
      if (j < n && i==s[j]) {snprintf(buf, 20,"@b%d",i); j++;}
      else snprintf(buf, 20,"%d",i);
      sizeobj->add(buf);
    }
    sizeobj->value(pickedsize);
  } else {
    // some sizes
    int w = 0;
    for (int i = 0; i < n; i++) {
      if (s[i]<=pickedsize) w = i;
      char buf[20];
      snprintf(buf, 20,"@b%d",s[i]);
      sizeobj->add(buf);
    }
    sizeobj->value(w+1);
  }
  textobj->redraw();
}

void size_cb(Fl_Widget *, long) {
  int i = sizeobj->value();
  if (!i) return;
  const char *c = sizeobj->text(i);
  while (*c < '0' || *c > '9') c++;
  pickedsize = atoi(c);
  textobj->size = pickedsize;
  textobj->redraw();
}

char label[0x1000];

unsigned char current_char = 'A';
unsigned char vec[255][128] = {
  { 0 }
};

class LetterBox : public Fl_Group
{
public:
  LetterBox(int x, int y, int w, int h, const char *l)
  : Fl_Group(x, y, w, h, l) { }
  void draw() {
    draw_box();
    fl_push_clip(x(), y(), w(), h());
    draw_label(x(), y()-5, w(), h()-16, FL_ALIGN_CENTER);

    fl_color(FL_BLUE);
    fl_line_style(FL_SOLID|FL_CAP_ROUND|FL_JOIN_ROUND, 10);
    bool rendering = false;
    unsigned char *fd = vec[current_char];
    double px, py;
    for (;;) {
      unsigned char cmd = *fd++;
      if (cmd==0) {
        if (rendering) {
          fl_end_line();
          rendering = false;
        }
        break;
      } else if (cmd>63) {
        if (cmd=='\100' && rendering) {
          fl_end_line();
          rendering = false;
        }
      } else {
        if (!rendering) { fl_begin_line(); rendering = true; }
        int vx = (cmd & '\70')>>3;
        int vy = (cmd & '\07');
        px = (vx*16+8+10);
        py = (vy*32+16+10);
        fl_vertex(px, py);
      }
    }
    fl_line_style(FL_SOLID, 1);

    draw_children();
    fl_pop_clip();
  }
};

void add_point_cb(Fl_Widget *w, void *d)
{
  unsigned char *fd = vec[current_char];
  while (*fd) fd++;
  *fd = (fl_uchar)(d);
  w->parent()->redraw();
}

void add_gap_cb(Fl_Widget *w, void *d)
{
  unsigned char *fd = vec[current_char];
  while (*fd) fd++;
  *fd = '\100';
  w->parent()->redraw();
}

void clear_cb(Fl_Widget *w, void *d)
{
  unsigned char *fd = vec[current_char];
  memset(fd, 0, 128);
  w->parent()->redraw();
}

void prev_cb(Fl_Widget *w, void *d)
{
  current_char--;
  char b[2] = { (char)current_char, 0 };
  w->parent()->child(0)->copy_label(b);
  w->parent()->child(0)->redraw();
}

void next_cb(Fl_Widget *w, void *d)
{
  current_char++;
  char b[2] = { (char)current_char, 0 };
  w->parent()->child(0)->copy_label(b);
  w->parent()->child(0)->redraw();
}

void back_cb(Fl_Widget *w, void *d)
{
  unsigned char *fd = vec[current_char];
  if (*fd==0) return;
  while (*fd) fd++;
  *(--fd) = 0;
  w->parent()->child(0)->redraw();
}

void save_cb(Fl_Widget *w, void *d)
{
  const char *filename = fl_file_chooser("Save font as:", 0, 0);
  if (!filename) return;
  FILE *f = fopen(filename, "wb");
  if (!f) {
    fl_alert("can't open file for writing");
    return;
  }
  fprintf(f, "\nstatic const char *font_data[128] = {\n  ");
  for (int i=0; i<128; i++) {
    unsigned char *fd = vec[i];
    if (i>=32 && i<127) fprintf(f, "/*%c*/", i); else fprintf(f, "/*%02X*/", i);
    if (*fd==0) {
      fprintf(f, "0");
    } else {
      fprintf(f, "\"");
      for (;;) {
        unsigned char c = *fd++;
        if (c==0) break;
        fprintf(f, "\\%02o", c);
      }
      fprintf(f, "\"");
    }
    if (i<127) fprintf(f, ", ");
    if ((i&3)==3)fprintf(f, "\n  ");
  }
  fprintf(f, "};\n\n");
  fclose(f);
}

Fl_Window *create_editor()
{
  Fl_Window *win = new Fl_Double_Window(400,400);
  LetterBox *c = new LetterBox(10, 10, 128, 256, "A");
  //c->labelfont(FL_COURIER);
  c->align(FL_ALIGN_CENTER);
  c->labelsize(200);
  c->labelcolor(FL_DARK3);
  c->box(FL_DOWN_BOX);
  Fl_Button *b;
  int i, j;
  for (i=0; i<8; i++) {
    for (j=0; j<8; j++) {
      b = new Fl_Button(i*16+8-5+10, j*32+16-5+10, 10, 10);
      b->box(FL_OVAL_BOX);
      b->callback(add_point_cb, (void*)(fl_intptr_t)(i*8+j));
    }
  }
  c->end();

  b = new Fl_Button(10, 290, 70, 20, "Gap");
  b->callback(add_gap_cb);
  b = new Fl_Button(90, 290, 70, 20, "Clear");
  b->callback(clear_cb);
  b = new Fl_Button(10, 315, 70, 20, "<-");
  b->callback(prev_cb);
  b->shortcut(FL_Left);
  b = new Fl_Button(90, 315, 70, 20, "->");
  b->callback(next_cb);
  b->shortcut(FL_Right);
  b = new Fl_Button(10, 340, 70, 20, "Back");
  b->callback(back_cb);
  b = new Fl_Button(90, 340, 70, 20, "Save");
  b->callback(save_cb);
  b->shortcut(FL_COMMAND+'s');
  return win;
}

class MainWindow : public Fl_Double_Window
{
public:
  MainWindow(int w, int h, const char *l=0)
  : Fl_Double_Window(w, h, l) { }
  int handle(int event) {
    if (event==FL_KEYBOARD && Fl::event_key()==FL_F+1) {
      if (!vector_font_editor) vector_font_editor = create_editor();
      vector_font_editor->show();
      return 1;
    } else {
      return Fl_Double_Window::handle(event);
    }
  }
};

void create_the_forms() {
  // create the sample string
  int n = 0;
  strcpy(label, "Hello, world!\n");
  int i = (int)strlen(label);
  ulong c;
  for (c = ' '+1; c < 127; c++) {
    if (!(c&0x1f)) label[i++] = '\n';
    if (c == '@') label[i++] = '@';
    label[i++] = (char)c;
  }
  label[i++] = '\n';
  for (c = 0xA1; c < 0x600; c += 9) {
    if (!(++n&(0x1f))) label[i++]='\n';
    i += fl_utf8encode((unsigned int)c, label + i);
  }
  label[i] = 0;

  // create the basic layout
  form = new MainWindow(550,370);

  tile = new Fl_Tile(0, 0, 550, 370);

  Fl_Group *textgroup = new Fl_Group(0, 0, 550, 185);
  textgroup->box(FL_FLAT_BOX);
  textobj = new FontDisplay(FL_FRAME_BOX,10,10,530,170,label);
  textobj->align(FL_ALIGN_TOP|FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
  textobj->color(9,47);
  textgroup->resizable(textobj);
  textgroup->end();

  Fl_Group *fontgroup = new Fl_Group(0, 185, 550, 185);
  fontgroup->box(FL_FLAT_BOX);
  fontobj = new Fl_Hold_Browser(10, 190, 390, 170);
  fontobj->box(FL_FRAME_BOX);
  fontobj->color(53,3);
  fontobj->callback(font_cb);
  sizeobj = new Fl_Hold_Browser(410, 190, 130, 170);
  sizeobj->box(FL_FRAME_BOX);
  sizeobj->color(53,3);
  sizeobj->callback(size_cb);
  fontgroup->resizable(fontobj);
  fontgroup->end();

  tile->end();

  form->resizable(tile);
  form->end();
}

int main(int argc, char **argv) {
  Fl::scheme(NULL);
  Fl::args(argc, argv);
  Fl::get_system_colors();
  create_the_forms();

// For the Unicode test, get all fonts...
//#ifdef __APPLE__
  int i = 0;
//#else
//  int i = fl_choice("Which fonts:","-*","iso8859","All");
//#endif
  int k = Fl::set_fonts(i ? (i>1 ? "*" : 0) : "-*");
  sizes = new int*[k];
  numsizes = new int[k];
  for (i = 0; i < k; i++) {
    int t; const char *name = Fl::get_font_name((Fl_Font)i,&t);
    char buffer[128];
#if 1
    if (t) {
      char *p = buffer;
      if (t & FL_BOLD) {*p++ = '@'; *p++ = 'b';}
      if (t & FL_ITALIC) {*p++ = '@'; *p++ = 'i';}
          *p++ = '@'; *p++ = '.'; // Suppress subsequent formatting - some MS fonts have '@' in their name
      strcpy(p,name);
      name = buffer;
    }
#else // this is neat, but really slow on some X servers:
    sprintf(buffer, "@F%d@.%s", i, name);
    name = buffer;
#endif
    fontobj->add(name);
    int *s; int n = Fl::get_font_sizes((Fl_Font)i, s);
    numsizes[i] = n;
    if (n) {
      sizes[i] = new int[n];
      for (int j=0; j<n; j++) sizes[i][j] = s[j];
    }
  }
  fontobj->value(1);
  font_cb(fontobj,0);
  form->show(argc,argv);
  return Fl::run();
}