> If you could provide a short example program including some of
> the glyphs (maybe U+1D160 - U+1D164) I could patch my FLTK lib
> and give it a try on Win7. Other than that, the patch looks
> plausible, and I don't know what else to do.
> 
> BTW: I'm looking at http://unicode.org/charts/PDF/U1D100.pdf

Yup - though I favour:

http://www.decodeunicode.org/en/musical_symbols 

I'm using a number of fonts, but you can try Musica 3.06 and Symbola
6.02 from:

http://users.teilar.gr/~g1951d/ 

If you don't have anything suitable...

The code I've used for testing is a hack of the UTF8 demo, reworked to
explicitly load the fonts on a winXX host - note that the load path is
hard coded in the function load_extra_font() (see below...)
I think it's all OK!

I also tried using the MS own-brand MultiByteToWideChar() function in
place of our fl_utf8toUtf16() and the results are identical, viz:

    if(u > 0xFFFF) {
//      cc = fl_utf8toUtf16((str + i), l, ucs, 4);

cc = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (str + i), l,
(WCHAR*)ucs, 4);
printf("0x%06x 0x%04x 0x%04x %d\n", u, (ucs[0] & 0xFFFF), (ucs[1] &
0xFFFF), cc);
    }
    else { // not a surrogate pair, use a single value

So our conversion code seems to be compatible with the MS functions...



Anyway, here's my (not very small) test harness.

-------------------

//
// "$Id: utf8.cxx 8171 2011-01-02 15:31:09Z manolo $"
//
// UTF-8 test program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

#ifdef WIN32
#define _WIN32_WINNT 0x0500
#include <windows.h>
#endif

#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_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(600, 150, 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 __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);
  font_cb(fontobj, 0);
  fnt_cnt->value(font_count);
  return font_count;
} // make_font_chooser

/* End of Font Chooser Widget code */

/***********************************************************************
******/
static int loaded_font = 0;
static const char fnt_name[] = "../Musica306/Musica306.otf";
//static const char fnt_name[] = "../Symbola602/Symbola602.otf";
void load_extra_font(void)
{
#ifdef WIN32
        DWORD fl = FR_PRIVATE;
        PVOID pdv = 0;

        loaded_font = AddFontResourceEx(fnt_name, fl, pdv);
#endif
} // load_extra_font

void free_extra_font(void)
{
#ifdef WIN32
        if (loaded_font)
        {
                DWORD fl = FR_PRIVATE;
                PVOID pdv = 0;
                RemoveFontResourceEx(fnt_name, fl, pdv);
        }
#endif
} // free_extra_font
/***********************************************************************
******/
int main(int argc, char** argv)
{
  int l = 0;

        load_extra_font();

  make_font_chooser();
  extra_font = FL_TIMES_BOLD_ITALIC;

  /* setup the extra font */
  Fl::set_font(extra_font,
#ifdef WIN32
                           " Musica"
#elif __APPLE__
               "Monaco"
#else
               "-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1"
#endif
               );

  main_win = new Fl_Double_Window (150, 150, 400, 600, "Unicode Font
Test");
  main_win->begin();

  Fl_Scroll scroll(5, 5, 390, 590);

  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++) {
    static int fff = 0;//-1; // dump debug
    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);

        // dump debug
        if(fff)
        {
                int len, idx = 0;
                fff = 0;
                while(buf[idx] != 0)
                {
                        printf("0x%02x ", (buf[idx] & 0x00ff));
                        idx++;
                }
                puts("");

                idx = 0;
                while(buf[idx] != 0)
                {
                        unsigned uv = fl_utf8decode(&buf[idx], &buf[o],
&len);
                        printf("0x%08x %d\n", uv, len);
                        idx += len;
                }
        }

    Fl_Input *b = new Fl_Input(5, ((y - off) * 25), 78, 25);
    b->textfont(FL_COURIER);
    b->value(strdup(bu));

    b = new Fl_Input(85, ((y - off) * 25), 290, 25);
    b->textfont(extra_font);
    b->value(strdup(buf));
  }
  scroll.end();
  main_win->resizable(scroll);

  main_win->end();
  main_win->callback((Fl_Callback*)cb_exit);

  main_win->show(argc,argv);
  fnt_chooser_win->show();

  int ret = Fl::run();

        free_extra_font();

  // Free up the sizes arrays we allocated
  if(numsizes) {delete [] numsizes;}
  if(sizes) {delete [] sizes;}

//  int rs = sizeof(WCHAR);
//  printf("wchar size %d\n", rs);

  return ret;
}

/* end of file */





SELEX Galileo Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 
3EL
A company registered in England & Wales.  Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to