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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
|
//
// "$Id$"
//
// UTF-8 test program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 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:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Tile.H>
#include <FL/Fl_Hold_Browser.H>
#include <FL/Fl_Value_Output.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Check_Button.H>
#include <FL/Fl_Output.H>
#include <FL/fl_draw.H>
#include <FL/fl_utf8.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//
// Font chooser widget for the Fast Light Tool Kit(FLTK).
//
#define DEF_SIZE 16 // default value for the font size picker
static Fl_Double_Window *fnt_chooser_win;
static Fl_Hold_Browser *fontobj;
static Fl_Hold_Browser *sizeobj;
static Fl_Value_Output *fnt_cnt;
static Fl_Button *refresh_btn;
static Fl_Button *choose_btn;
static Fl_Output *fix_prop;
static Fl_Check_Button *own_face;
static int **sizes = NULL;
static int *numsizes = NULL;
static int pickedsize = DEF_SIZE;
static char label[1000];
static Fl_Double_Window *main_win;
static Fl_Scroll *thescroll;
static Fl_Font extra_font;
static int font_count = 0;
static int first_free = 0;
static void cb_exit(Fl_Button*, void*) {
if(fnt_chooser_win) fnt_chooser_win->hide();
if(main_win) main_win->hide();
} /* cb_exit */
/*
Class for displaying sample fonts.
*/
class FontDisplay : public Fl_Widget
{
void draw(void);
public:
int font, size;
int test_fixed_pitch(void);
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 = DEF_SIZE;
}
};
/*
Draw the sample text.
*/
void FontDisplay::draw(void)
{
draw_box();
fl_font((Fl_Font)font, size);
fl_color(FL_BLACK);
fl_draw(label(), x() + 3, y() + 3, w() - 6, h() - 6, align());
}
int FontDisplay::test_fixed_pitch(void)
{
int w1, w2;
int h1, h2;
w1 = w2 = 0;
h1 = h2 = 0;
fl_font((Fl_Font)font, size);
fl_measure("MHMHWWMHMHMHM###WWX__--HUW", w1, h1, 0);
fl_measure("iiiiiiiiiiiiiiiiiiiiiiiiii", w2, h2, 0);
if (w1 == w2) return 1; // exact match - fixed pitch
// Is the font "nearly" fixed pitch? If it is within 5%, say it is...
double f1 = (double)w1;
double f2 = (double)w2;
double delta = fabs(f1 - f2) * 5.0;
if (delta <= f1) return 2; // nearly fixed pitch...
return 0; // NOT fixed pitch
}
static FontDisplay *textobj;
static void size_cb(Fl_Widget *, long)
{
int size_idx = sizeobj->value();
if (!size_idx) return;
const char *c = sizeobj->text(size_idx);
while (*c < '0' || *c > '9') c++; // find the first numeric char
pickedsize = atoi(c); // convert the number string to a value
// Now set the font view to the selected size and redraw it.
textobj->size = pickedsize;
textobj->redraw();
}
static void font_cb(Fl_Widget *, long)
{
int font_idx = fontobj->value() + first_free;
if (!font_idx) return;
font_idx--;
textobj->font = font_idx;
sizeobj->clear();
int size_count = numsizes[font_idx-first_free];
int *size_array = sizes[font_idx-first_free];
if (!size_count)
{
// no preferred sizes - probably TT fonts etc...
}
else if (size_array[0] == 0)
{
// many sizes, probably a scaleable font with preferred sizes
int j = 1;
for (int i = 1; i <= 64 || i < size_array[size_count - 1]; i++)
{
char buf[16];
if (j < size_count && i == size_array[j])
{
sprintf(buf, "@b%d", i);
j++;
}
else
sprintf(buf, "%d", i);
sizeobj->add(buf);
}
sizeobj->value(pickedsize);
}
else
{
// some sizes, probably a font with a few fixed sizes available
int w = 0;
for (int i = 0; i < size_count; i++)
{
// find the nearest available size to the current picked size
if (size_array[i] <= pickedsize) w = i;
char buf[16];
sprintf(buf, "@b%d", size_array[i]);
sizeobj->add(buf);
}
sizeobj->value(w + 1);
}
size_cb(sizeobj, 0); // force selection of nearest valid size, then redraw
// Now check to see if the font looks like a fixed pitch font or not...
int looks_fixed = textobj->test_fixed_pitch();
if(looks_fixed)
{
if (looks_fixed > 1)
fix_prop->value("near");
else
fix_prop->value("fixed");
}
else
{
fix_prop->value("prop");
}
}
static void choose_cb(Fl_Widget *, long)
{
int font_idx = fontobj->value() + first_free;
if (!font_idx)
{
puts("No font chosen");
}
else
{
int font_type;
font_idx -= 1;
const char *name = Fl::get_font_name((Fl_Font)font_idx, &font_type);
printf("idx %d\nUser name :%s:\n", font_idx, name);
printf("FLTK name :%s:\n", Fl::get_font((Fl_Font)font_idx));
Fl::set_font(extra_font, (Fl_Font)font_idx);
// Fl::set_font(extra_font, Fl::get_font((Fl_Font)font_idx));
}
int size_idx = sizeobj->value();
if (!size_idx)
{
puts("No size selected");
}
else
{
const char *c = sizeobj->text(size_idx);
while (*c < '0' || *c > '9') c++; // find the first numeric char
int pickedsize = atoi(c); // convert the number string to a value
printf("size %d\n\n", pickedsize);
}
fflush(stdout);
main_win->redraw();
}
static void refresh_cb(Fl_Widget *, long)
{
main_win->redraw();
}
static void own_face_cb(Fl_Widget *, void *)
{
int font_idx;
int cursor_restore = 0;
static int i_was = -1; // used to keep track of where we were in the list...
if (i_was < 0) { // not been here before
i_was = 1;
} else {
i_was = fontobj->topline(); // record which was the topmost visible line
fontobj->clear();
// Populating the font widget can be slower than an old dog with three legs
// on a bad day, show a wait cursor
fnt_chooser_win->cursor(FL_CURSOR_WAIT);
cursor_restore = 1;
}
// Populate the font list with the names of the fonts found
for (font_idx = first_free; font_idx < font_count; font_idx++)
{
int font_type;
const char *name = Fl::get_font_name((Fl_Font)font_idx, &font_type);
char buffer[128];
if(own_face->value() == 0) {
char *p = buffer;
// if the font is BOLD, set the bold attribute in the list
if (font_type & FL_BOLD) {
*p++ = '@';
*p++ = 'b';
}
if (font_type & FL_ITALIC) { // ditto for italic fonts
*p++ = '@';
*p++ = 'i';
}
// Suppress subsequent formatting - some MS fonts have '@' in their name
*p++ = '@';
*p++ = '.';
strcpy(p, name);
} else {
// Show font in its own face
// this is neat, but really slow on some systems:
// uses each font to display its own name
sprintf (buffer, "@F%d@.%s", font_idx, name);
}
fontobj->add(buffer);
}
// now put the browser position back the way it was... more or less
fontobj->topline(i_was);
// restore the cursor
if(cursor_restore) fnt_chooser_win->cursor(FL_CURSOR_DEFAULT);
}
static void create_font_widget()
{
// Create the font sample label
strcpy(label, "Font Sample\n");
int i = 12; // strlen(label);
int n = 0;
ulong c;
for (c = ' '+1; c < 127; c++) {
if (!(c&0x1f)) label[i++]='\n';
if (c=='@') label[i++]=c;
label[i++]=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 window layout
fnt_chooser_win = new Fl_Double_Window(380, 420, "Font Selector");
{
Fl_Tile *tile = new Fl_Tile(0, 0, 380, 420);
{
Fl_Group *textgroup = new Fl_Group(0, 0, 380, 105);
{
textobj = new FontDisplay(FL_FRAME_BOX, 10, 10, 360, 90, label);
textobj->align(FL_ALIGN_TOP|FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
textobj->color(53, 3);
textgroup->box(FL_FLAT_BOX);
textgroup->resizable(textobj);
textgroup->end();
}
Fl_Group *fontgroup = new Fl_Group(0, 105, 380, 315);
{
fontobj = new Fl_Hold_Browser(10, 110, 290, 270);
fontobj->box(FL_FRAME_BOX);
fontobj->color(53, 3);
fontobj->callback(font_cb);
fnt_chooser_win->resizable(fontobj);
sizeobj = new Fl_Hold_Browser(310, 110, 60, 270);
sizeobj->box(FL_FRAME_BOX);
sizeobj->color(53, 3);
sizeobj->callback(size_cb);
// Create the status bar
Fl_Group *stat_bar = new Fl_Group (10, 385, 380, 30);
{
fnt_cnt = new Fl_Value_Output(10, 390, 40, 20);
fnt_cnt->label("fonts");
fnt_cnt->align(FL_ALIGN_RIGHT);
fix_prop = new Fl_Output(100, 390, 40, 20);
fix_prop->color(FL_BACKGROUND_COLOR);
fix_prop->value("prop");
fix_prop->clear_visible_focus();
own_face = new Fl_Check_Button(150, 390, 40, 20, "Self");
own_face->value(0);
own_face->type(FL_TOGGLE_BUTTON);
own_face->clear_visible_focus();
own_face->callback(own_face_cb);
own_face->tooltip("Display font names in their own face");
Fl_Box * dummy = new Fl_Box(220, 390, 1, 1);
choose_btn = new Fl_Button(240, 385, 60, 30);
choose_btn->label("Select");
choose_btn->callback(choose_cb);
refresh_btn = new Fl_Button(310, 385, 60, 30);
refresh_btn->label("Refresh");
refresh_btn->callback(refresh_cb);
stat_bar->resizable (dummy);
stat_bar->end();
}
fontgroup->box(FL_FLAT_BOX);
fontgroup->resizable(fontobj);
fontgroup->end();
}
tile->end();
}
fnt_chooser_win->resizable(tile);
fnt_chooser_win->end();
fnt_chooser_win->callback((Fl_Callback*)cb_exit);
}
}
int make_font_chooser(void)
{
int font_idx;
// create the widget frame
create_font_widget();
// Load the systems available fonts - ask for everything
// font_count = Fl::set_fonts("*");
#ifdef WIN32
font_count = Fl::set_fonts("*");
#elif defined(__APPLE__)
font_count = Fl::set_fonts("*");
#else
// Load the systems available fonts - ask for everything that claims to be
// iso10646 compatible
font_count = Fl::set_fonts("-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1");
#endif
// allocate space for the sizes and numsizes array, now we know how many
// entries it needs
sizes = new int*[font_count];
numsizes = new int[font_count];
// Populate the font list with the names of the fonts found
first_free = FL_FREE_FONT;
for (font_idx = first_free; font_idx < font_count; font_idx++)
{
// Find out how many sizes are supported for each font face
int *size_array;
int size_count = Fl::get_font_sizes((Fl_Font)font_idx, size_array);
numsizes[font_idx-first_free] = size_count;
// if the font has multiple sizes, populate the 2-D sizes array
if (size_count)
{
sizes[font_idx-first_free] = new int[size_count];
for (int j = 0; j < size_count; j++)
sizes[font_idx-first_free][j] = size_array[j];
}
} // end of font list filling loop
// Call this once to get the font browser loaded up
own_face_cb(NULL, 0);
fontobj->value(1);
// optional hard-coded font for testing - do not use!
// fontobj->textfont(261);
font_cb(fontobj, 0);
fnt_cnt->value(font_count);
return font_count;
} // make_font_chooser
/* End of Font Chooser Widget code */
/* Unicode Font display widget */
void box_cb(Fl_Widget* o, void*) {
thescroll->box(((Fl_Button*)o)->value() ? FL_DOWN_FRAME : FL_NO_BOX);
thescroll->redraw();
}
class right_left_input : public Fl_Input
{
public:
right_left_input (int x, int y, int w, int h) : Fl_Input(x, y, w, h) {};
void draw() {
if (type() == FL_HIDDEN_INPUT) return;
Fl_Boxtype b = box();
if (damage() & FL_DAMAGE_ALL) draw_box(b, color());
drawtext(x()+Fl::box_dx(b)+3, y()+Fl::box_dy(b),
w()-Fl::box_dw(b)-6, h()-Fl::box_dh(b));
}
void drawtext(int X, int Y, int W, int H) {
fl_color(textcolor());
fl_font(textfont(), textsize());
fl_rtl_draw(value(), strlen(value()),
X + W, Y + fl_height() -fl_descent());
}
};
void i7_cb(Fl_Widget *w, void *d)
{
int i = 0;
char nb[] = "01234567";
Fl_Input *i7 = (Fl_Input*)w;
Fl_Input *i8 = (Fl_Input*)d;
static char buf[1024];
const char *ptr = i7->value();
while (ptr && *ptr) {
if (*ptr < ' ' || *ptr > 126) {
buf[i++] = '\\';
buf[i++] = nb[((*ptr >> 6) & 0x3)];
buf[i++] = nb[((*ptr >> 3) & 0x7)];
buf[i++] = nb[(*ptr & 0x7)];
} else {
if (*ptr == '\\') buf[i++] = '\\';
buf[i++] = *ptr;
}
ptr++;
}
buf[i] = 0;
i8->value(buf);
}
class UCharDropBox : public Fl_Output {
public:
UCharDropBox(int x, int y, int w, int h, const char *label=0) :
Fl_Output(x, y, w, h, label) { }
int handle(int event) {
switch (event) {
case FL_DND_ENTER: return 1;
case FL_DND_DRAG: return 1;
case FL_DND_RELEASE: return 1;
case FL_PASTE:
{
static const char lut[] = "0123456789abcdef";
const char *t = Fl::event_text();
int i, n;
unsigned int ucode = fl_utf8decode(t, t+Fl::event_length(), &n);
if (n==0) {
value("");
return 1;
}
char buffer[200], *d = buffer;
for (i=0; i<n; i++) *d++ = t[i];
*d++ = ' ';
for (i=0; i<n; i++) {
*d++ = '\\'; *d++ = 'x';
*d++ = lut[(t[i]>>4)&0x0f]; *d++ = lut[t[i]&0x0f];
}
*d++ = ' ';
*d++ = '0';
*d++ = 'x';
*d++ = lut[(ucode>>20)&0x0f]; *d++ = lut[(ucode>>16)&0x0f];
*d++ = lut[(ucode>>12)&0x0f]; *d++ = lut[(ucode>>8)&0x0f];
*d++ = lut[(ucode>>4)&0x0f]; *d++ = lut[ucode&0x0f];
*d++ = 0;
value(buffer);
}
return 1;
}
return Fl_Output::handle(event);
}
};
int main(int argc, char** argv)
{
int l;
const char *latin1 =
"\x41\x42\x43\x61\x62\x63\xe0\xe8\xe9\xef\xe2\xee\xf6\xfc\xe3\x31\x32\x33";
char *utf8 = (char*) malloc(strlen(latin1) * 5 + 1);
l = 0;
// l = fl_latin12utf((const unsigned char*)latin1, strlen(latin1), utf8);
l = fl_utf8froma(utf8, (strlen(latin1) * 5 + 1), latin1, strlen(latin1));
make_font_chooser();
extra_font = FL_TIMES_BOLD_ITALIC;
/* setup the extra font */
Fl::set_font(extra_font,
#ifdef WIN32
" Arial Unicode MS"
#elif defined(__APPLE__)
"Monaco"
#else
"-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1"
#endif
);
main_win = new Fl_Double_Window (200 + 5*75, 400, "Unicode Display Test");
main_win->begin();
Fl_Input i1(5, 5, 190, 25);
utf8[l] = '\0';
i1.value(utf8);
Fl_Scroll scroll(200,0,5 * 75,400);
int off = 2;
int end_list = 0x10000 / 16;
if (argc > 1) {
off = (int)strtoul(argv[1], NULL, 0);
end_list = off + 0x10000;
off /= 16;
end_list /= 16;
}
argc = 1;
for (long y = off; y < end_list; y++) {
int o = 0;
char bu[25]; // index label
char buf[16 * 6]; // utf8 text
int i = 16 * y;
for (int x = 0; x < 16; x++) {
int l;
l = fl_utf8encode(i, buf + o);
if (l < 1) l = 1;
o += l;
i++;
}
buf[o] = '\0';
sprintf(bu, "0x%06lX", y * 16);
Fl_Input *b = new Fl_Input(200,(y-off)*25,80,25);
b->textfont(FL_COURIER);
b->value(strdup(bu));
b = new Fl_Input(280,(y-off)*25,380,25);
b->textfont(extra_font);
b->value(strdup(buf));
}
scroll.end();
main_win->resizable(scroll);
thescroll = &scroll;
char *utf8l = (char*) malloc(strlen(utf8) * 3 + 1);
Fl_Input i2(5, 35, 190, 25);
l = fl_utf_tolower((const unsigned char*)utf8, l, utf8l);
utf8l[l] = '\0';
i2.value(utf8l);
char *utf8u = (char*) malloc(strlen(utf8l) * 3 + 1);
Fl_Input i3(5, 65, 190, 25);
l = fl_utf_toupper((const unsigned char*)utf8l, l, utf8u);
utf8u[l] = '\0';
i3.value(utf8u);
const char *ltr_txt = "\\->e\xCC\x82=\xC3\xAA";
Fl_Input i4(5, 90, 190, 25);
i4.value(ltr_txt);
i4.textfont(extra_font);
wchar_t r_to_l_txt[] = {/*8238,*/
1610, 1608, 1606, 1604, 1603, 1608, 1583, 0};
char abuf[40];
// l = fl_unicode2utf(r_to_l_txt, 8, abuf);
l = fl_utf8fromwc(abuf, 40, r_to_l_txt, 8);
abuf[l] = 0;
right_left_input i5(5, 115, 190, 50);
i5.textfont(extra_font);
i5.textsize(30);
i5.value(abuf);
Fl_Input i7(5, 230, 190, 25);
Fl_Input i8(5, 260, 190, 25);
i7.callback(i7_cb, &i8);
i7.textsize(20);
i7.value(abuf);
i7.when(FL_WHEN_CHANGED);
wchar_t r_to_l_txt1[] = { /*8238,*/
1610, 0x20, 1608, 0x20, 1606, 0x20,
1604, 0x20, 1603, 0x20, 1608, 0x20, 1583, 0};
// l = fl_unicode2utf(r_to_l_txt1, 14, abuf);
l = fl_utf8fromwc(abuf, 40, r_to_l_txt1, 14);
abuf[l] = 0;
right_left_input i6(5, 175, 190, 50);
i6.textfont(extra_font);
i6.textsize(30);
i6.value(abuf);
// Now try Greg Ercolano's Japanese test sequence
// SOME JAPANESE UTF-8 TEXT
const char *utfstr =
"\xe4\xbd\x95\xe3\x82\x82\xe8\xa1"
"\x8c\xe3\x82\x8b\xe3\x80\x82";
UCharDropBox db(5, 300, 190, 30);
db.textsize(16);
db.value("unichar drop box");
Fl_Output o9(5, 330, 190, 45);
o9.textfont(extra_font);
o9.textsize(30);
o9.value(utfstr);
main_win->end();
main_win->callback((Fl_Callback*)cb_exit);
fl_set_status(0, 370, 100, 30);
main_win->show(argc,argv);
fnt_chooser_win->show();
int ret = Fl::run();
// Free up the sizes arrays we allocated
if(numsizes) {delete [] numsizes;}
if(sizes) {delete [] sizes;}
return ret;
}
//
// End of "$Id$".
//
|