Source

Coin / src / misc / SoGlyph.cpp

Full commit
Tamer Fahmy 872e884 
Marius Kintel 3ba6b41 




























Tamer Fahmy 872e884 

























































































































































































































































































































































































































































































































































































































































































































  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
720
721
722
723
724
725
726
727
728
729
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

/*!
  \class SoGlyph SoGlyph.h Inventor/misc/SoGlyph.h
  \brief The SoGlyph class is used to generate and reuse font glyph bitmaps and outlines.

  <b>This class is now obsolete, and will be removed from a later
  version of Coin.</b>
  
  SoGlyph is the public interface all text nodes (both built-in and
  extensions) should use to generate bitmaps and outlines for font
  glyphs. It maintains an internal cache of previously requested
  glyphs to avoid needless calls into the font library.
  
  Primer: a \e glyph is the graphical representation of a given
  character of a given font at a given size and orientation. It can be
  either a \e bitmap (pixel aligned with the viewport) or an \e
  outline (polygonal representation) that can be transformed or
  extruded like any other 3D geometry. Bitmaps are used by SoText2,
  while the other text nodes uses outlines.
  
  \COIN_CLASS_EXTENSION

  \since Coin 2.0

  \sa SoText2, SoText3, SoAsciiText
*/

// SoGlyph uses the Coin-internal font lib wrapper functions
// (cc_flw_*()) to provide bitmaps and outlines.
//
// FIXME: font support for outline glyphs. 200303?? preng.
  
#include <Inventor/misc/SoGlyph.h>
#include "coindefs.h"

#include <stdlib.h>
#include <string.h>

#include <Inventor/errors/SoDebugError.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/SbName.h>
#include <Inventor/SbString.h>
#include <Inventor/SbVec2f.h>
#include <Inventor/SbVec2s.h>
#include <Inventor/lists/SbList.h>
#include <Inventor/elements/SoFontNameElement.h>
#include <Inventor/elements/SoFontSizeElement.h>

#include "tidbitsp.h"
#include "threads/threadsutilp.h"
#include "fonts/common.h"
#include "fonts/fontlib_wrapper.h"
#include "fonts/defaultfonts.h"

class SoGlyphP {
public:
  SoGlyphP(SoGlyph * master) : master(master) { }
  SoGlyph * master;


  const SbVec2f * coords;
  SbBox2f bbox;
  const int * faceidx;
  const int * edgeidx;
  int refcount;
  float ymin, ymax;
  
  int fontidx;
  int glyphidx;
  float angle;
  SbVec2s size;
  unsigned int character;
  SbBool fonttypeis3d;
  SbBool coordsinstalled;

  int bitmapwidth;
  int bitmapheight;
  cc_font_bitmap * bitmap;

  struct {
    unsigned int didcalcbbox : 1;
  } flags;

  void setup3DFontData();

  static SoGlyph * createSystemGlyph(const char character, const SbName & font);
  static SoGlyph * createSystemGlyph(const unsigned int COIN_UNUSED_ARG(character), SoState * COIN_UNUSED_ARG(state)) {return NULL;};  static SoGlyph * createSystemGlyph(const char character, int fontid);
};

#define PRIVATE(p) ((p)->pimpl)
#define PUBLIC(p) ((p)->master)


/*!
  Constructor.
*/
SoGlyph::SoGlyph(void)
{
  PRIVATE(this) = new SoGlyphP(this);
  PRIVATE(this)->refcount = 0;
  PRIVATE(this)->flags.didcalcbbox = 0;
  PRIVATE(this)->coords = NULL;
  PRIVATE(this)->faceidx = NULL;
  PRIVATE(this)->edgeidx = NULL;
  PRIVATE(this)->ymin = 0.0f;
  PRIVATE(this)->ymax = 0.0f;

  /* Setting fonttype==3D as default since the 2D text node (SoText2)
     is no longer depending on the SoGlyph node (20030908 handegar) */
  PRIVATE(this)->fonttypeis3d = TRUE;

  PRIVATE(this)->fontidx = 0;
  PRIVATE(this)->coordsinstalled = FALSE;

  PRIVATE(this)->bitmapwidth = 0;
  PRIVATE(this)->bitmapheight = 0;
  PRIVATE(this)->bitmap = NULL;
}

/*!
  Destructor.
*/
SoGlyph::~SoGlyph()
{
  delete PRIVATE(this);
}

/*!
  Should be called when a node no longer will use a glyph. Will
  free memory used by this glyph when it is no longer used by any node.
*/
void
SoGlyph::unref(void) const
{
  SoGlyph::unrefGlyph((SoGlyph*)this);
}

/*!  
  Used to indicate how the glyph should be treated. This is needed if
  correct bounding box shall be calculated etc. As default, glyphs are
  treated as a part of a 2D font.
*/

void 
SoGlyph::setFontType(Fonttype type) const
{
  if (type == SoGlyph::FONT3D) {
    PRIVATE(this)->fonttypeis3d = TRUE;
  }
  else {
    PRIVATE(this)->fonttypeis3d = FALSE;
  }  
}


/*!
  Returns coordinates for this glyph.
*/
const SbVec2f *
SoGlyph::getCoords(void) const
{
  if (!PRIVATE(this)->coordsinstalled) {
    PRIVATE(this)->setup3DFontData();
    PRIVATE(this)->coordsinstalled = TRUE;
  }
  return PRIVATE(this)->coords;
}

/*!
  Returns face indices for this glyph.
*/
const int *
SoGlyph::getFaceIndices(void) const
{
  if (!PRIVATE(this)->coordsinstalled) {
    PRIVATE(this)->setup3DFontData();
    PRIVATE(this)->coordsinstalled = TRUE;
  }

  return PRIVATE(this)->faceidx;
}

/*!
  Returns edge indices for this glyph.
*/
const int *
SoGlyph::getEdgeIndices(void) const
{
  if (!PRIVATE(this)->coordsinstalled) {
    PRIVATE(this)->setup3DFontData();
    PRIVATE(this)->coordsinstalled = TRUE;
  }
  
  return PRIVATE(this)->edgeidx;
}

/*!
  Returns a pointer to the next clockwise edge. Returns NULL if
  none could be found.
*/
const int *
SoGlyph::getNextCWEdge(const int edgeidx) const
{
  int idx = edgeidx * 2;
  // test for common case
  if (edgeidx > 0) {
    if (PRIVATE(this)->edgeidx[idx] == PRIVATE(this)->edgeidx[idx-1])
      return &PRIVATE(this)->edgeidx[idx-2];
  }
  // do a linear search
  int findidx = PRIVATE(this)->edgeidx[idx];
  const int * ptr = PRIVATE(this)->edgeidx;
  while (*ptr >= 0) {
    if (ptr[1] == findidx) return ptr;
    ptr += 2;
  }
  return NULL;
}

/*!
  Returns a pointer to the next counter clockwise edge.
  NULL if none could be found.
*/
const int *
SoGlyph::getNextCCWEdge(const int edgeidx) const
{
  int idx = edgeidx * 2;
  // test for common case
  if (PRIVATE(this)->edgeidx[idx+1] == PRIVATE(this)->edgeidx[idx+2])
    return &PRIVATE(this)->edgeidx[idx+2];
  // do a linear search
  int findidx = PRIVATE(this)->edgeidx[idx+1];
  const int * ptr = PRIVATE(this)->edgeidx;
  while (*ptr >= 0) {
    if (*ptr == findidx) return ptr;
    ptr += 2;
  }
  return NULL;
}

/*!
  Convenience method which returns the exact width of the glyph.
*/
float
SoGlyph::getWidth(void) const
{

  if (!PRIVATE(this)->fonttypeis3d)
    return (float) PRIVATE(this)->bitmapwidth;

  const SbBox2f & box = this->getBoundingBox();
  return box.getMax()[0] - box.getMin()[0];
}

/*!
  Returns the bounding box of this glyph. This value is cached for performance.
*/
const SbBox2f &
SoGlyph::getBoundingBox(void) const
{
  // this method needs to be const, so cast away constness
  SoGlyph * thisp = (SoGlyph*) this;
  if (!PRIVATE(this)->flags.didcalcbbox) {

    if (!PRIVATE(this)->coordsinstalled) {
      PRIVATE(this)->setup3DFontData();
      PRIVATE(this)->coordsinstalled = TRUE;
    }

    PRIVATE(thisp)->bbox.makeEmpty();
    const int *ptr = PRIVATE(this)->edgeidx;
    int idx = *ptr++;

    while (idx >= 0) {
      PRIVATE(thisp)->bbox.extendBy(PRIVATE(this)->coords[idx]);
      idx = *ptr++;
    }

    // FIXME: the 'get_advance' call has been separated into one
    // 'get_vector_advance' and one 'get_bitmap_advance' call. As we
    // cannot separate whether we are dealing with bitmaps or vector
    // glyphs in this class, we choose 'get_vector_advance' as
    // default. (20030926 handegar)    
    float advancex, advancey;
    cc_flw_get_vector_advance(PRIVATE(this)->fontidx, PRIVATE(this)->glyphidx, &advancex, &advancey);
    SbVec2f max = PRIVATE(this)->bbox.getMax();
    
    PRIVATE(this)->bbox.extendBy(SbVec2f(advancex, advancey));
   
    PRIVATE(thisp)->flags.didcalcbbox = 1;
  }


  return PRIVATE(this)->bbox;
}

/*!
  Sets the coordinates for this glyph.
*/
void
SoGlyph::setCoords(const SbVec2f *coords, int numcoords)
{
  // It used to be valid to call this function with a negative value
  // (which signified that data should not be copied). All invoking
  // code just passed in -1, so we have simplified this function
  // (SoGlyph is being obsoleted anyway).
  //
  // Note that since we are just copying the data pointer, we assume
  // that all 3D glyphs use permanent storage for their publicly
  // exposed data.
  assert(numcoords == -1);

  PRIVATE(this)->coords = coords;
}

/*!
  Sets the face indices for this glyph.
*/
void
SoGlyph::setFaceIndices(const int *indices, int numindices)
{
  // It used to be valid to call this function with a negative value
  // (which signified that data should not be copied). All invoking
  // code just passed in -1, so we have simplified this function
  // (SoGlyph is being obsoleted anyway).
  //
  // Note that since we are just copying the data pointer, we assume
  // that all 3D glyphs use permanent storage for their publicly
  // exposed data.
  assert(numindices == -1);

  PRIVATE(this)->faceidx = indices;
}

/*!
  Sets the edge indices for this glyph.
*/
void
SoGlyph::setEdgeIndices(const int *indices, int numindices)
{
  // It used to be valid to call this function with a negative value
  // (which signified that data should not be copied). All invoking
  // code just passed in -1, so we have simplified this function
  // (SoGlyph is being obsoleted anyway).
  //
  // Note that since we are just copying the data pointer, we assume
  // that all 3D glyphs use permanent storage for their publicly
  // exposed data.
  assert(numindices == -1);

  PRIVATE(this)->edgeidx = indices;
}



//
// static methods to handle glyph reusage.
//
// FIXME: use SbHash to look up glyphs a bit faster. pederb, 20000323
//

class coin_glyph_info {
public:
  coin_glyph_info() {
    this->character = 0;
    this->size = 0.0;
    this->glyph = NULL;
    this->angle = 0.0;
  }
  coin_glyph_info(const unsigned int characterarg, const float sizearg, const SbName &fontarg, SoGlyph *glypharg, const float anglearg)
  {
    this->character = characterarg;
    this->size = sizearg;
    this->font = fontarg;
    this->angle = anglearg;
    this->glyph = glypharg;
  }
  
  // Note: bitmap glyphs have valid size, polygonal glyphs have size=-1.0
  SbBool matches(const unsigned int characterarg, const float sizearg, 
                 const SbName fontarg, const float anglearg) {
    return (this->character == characterarg) && (this->size == sizearg) && (this->font == fontarg) && (this->angle == anglearg);
  }
  
  // AIX native compiler xlC needs equality and inequality operators
  // to compile templates where these operators are referenced (even
  // if they are actually never used).
  
  SbBool operator==(const coin_glyph_info & gi) {
    return this->matches(gi.character, gi.size, gi.font, gi.angle) && this->glyph == gi.glyph;
  }
  SbBool operator!=(const coin_glyph_info & gi) {
    return !(*this == gi);
  }

  unsigned int character;
  float size;
  SbName font;
  SoGlyph * glyph;
  float angle;
};

static SbList <coin_glyph_info> * activeGlyphs = NULL;
static void * SoGlyph_mutex = NULL;

static void
SoGlyph_cleanup(void)
{
  delete activeGlyphs;
  activeGlyphs = NULL;
  CC_MUTEX_DESTRUCT(SoGlyph_mutex);
}

/*!
  Returns a character of the specified font, suitable for polygonal
  rendering.
*/
const SoGlyph *
SoGlyph::getGlyph(const char character, const SbName & font)
{

  // FIXME: the API and implementation of this class isn't consistent
  // with regard to the paraneters and variabls that are glyph codes
  // -- some places they are "char", other places "int", some places
  // signed, other places unsigned. Should audit and fix as much as
  // possible without breaking API and ABI compatibility. *sigh*
  // 20030611 mortene.
  
  // Similar code in start of getGlyph(..., state) - keep in sync.
  if (SoGlyph_mutex == NULL) {
    CC_MUTEX_CONSTRUCT(SoGlyph_mutex);
  }

  // FIXME: it would probably be a good idea to have a small LRU-type
  // glyph cache to avoid freeing glyphs too early. If for instance
  // the user creates a single SoText3 node which is used several
  // times in a graph with different fonts, glyphs will be freed and
  // recreated all the time. pederb, 20000324

  CC_MUTEX_LOCK(SoGlyph_mutex);

  if (activeGlyphs == NULL) {
    activeGlyphs = new SbList <coin_glyph_info>;
    coin_atexit((coin_atexit_f *)SoGlyph_cleanup, CC_ATEXIT_NORMAL);
  }

  int i, n = activeGlyphs->getLength();
  for (i = 0; i < n; i++) {
    // Search for fontsize -1 to avoid getting a bitmap glyph.
    if ((*activeGlyphs)[i].matches(character, -1.0, font, 0.0)) break;
  }
  if (i < n) {
    SoGlyph *glyph = (*activeGlyphs)[i].glyph;
    PRIVATE(glyph)->refcount++;
    CC_MUTEX_UNLOCK(SoGlyph_mutex);
    return glyph;
  }

  SoGlyph * glyph = SoGlyphP::createSystemGlyph(character, font);
  
  // FIXME: don't think this is necessary, we should _always_ get a
  // glyph. If none exist in the font, we should eventually fall back
  // on making a square in the code we're calling into. Move the code
  // below to handle this deeper down into the call-stack. 20030527 mortene.
  if (glyph == NULL) { // no system font could be loaded
    glyph = new SoGlyph;
    if (character <= 32 || character >= 127) {
      // treat all these characters as spaces
      static int spaceidx[] = { -1 };
      glyph->setCoords(NULL);
      glyph->setFaceIndices(spaceidx);
      glyph->setEdgeIndices(spaceidx);
      PRIVATE(glyph)->bbox.setBounds(SbVec2f(0.0f, 0.0f), SbVec2f(0.2f, 0.0f));
      PRIVATE(glyph)->flags.didcalcbbox = 1;
    }
    else {
      const int idx = character-33;
      glyph->setCoords((const SbVec2f*)coin_default3dfont_get_coords()[idx]);
      glyph->setFaceIndices(coin_default3dfont_get_faceidx()[idx]);
      glyph->setEdgeIndices(coin_default3dfont_get_edgeidx()[idx]);
    }
  }
  // Use impossible font size to avoid mixing polygonal & bitmap glyphs.
  coin_glyph_info info(character, -1.0, font, glyph, 0.0);
  PRIVATE(glyph)->refcount++;
  activeGlyphs->append(info);
  CC_MUTEX_UNLOCK(SoGlyph_mutex);
  return glyph;

}

// private method that removed glyph from active list when deleted
void
SoGlyph::unrefGlyph(SoGlyph *glyph)
{
  CC_MUTEX_LOCK(SoGlyph_mutex);
  assert(activeGlyphs);
  assert(PRIVATE(glyph)->refcount > 0);
  PRIVATE(glyph)->refcount--;
  if (PRIVATE(glyph)->refcount == 0) {
    int i, n = activeGlyphs->getLength();
    for (i = 0; i < n; i++) {
      if ((*activeGlyphs)[i].glyph == glyph) break;
    }
    assert(i < n);
    activeGlyphs->removeFast(i);
    cc_flw_unref_font(PRIVATE(glyph)->fontidx);
    delete glyph;
  }
  CC_MUTEX_UNLOCK(SoGlyph_mutex);
}

/*!
  Returns a character of the specified font, suitable for bitmap
  rendering.  The size parameter overrides state's SoFontSizeElement
  (if != SbVec2s(0,0))
*/
const SoGlyph *
SoGlyph::getGlyph(SoState * state,
                  const unsigned int character,
                  const SbVec2s & size,
                  const float angle)
{
  assert(state);
  
  SbName state_name = SoFontNameElement::get(state);
  float state_size = SoFontSizeElement::get(state);
  
  if (state_name == SbName::empty()) {
    state_name = SbName("defaultFont");
    state_size = 10.0;
  } 
     
  SbVec2s fontsize((short)state_size, (short)state_size);
  if (size != SbVec2s(0,0)) { fontsize = size; }
  
  // Similar code in start of getGlyph(..., fontname) - keep in sync.
  if (SoGlyph_mutex == NULL) {
    CC_MUTEX_CONSTRUCT(SoGlyph_mutex);
  }

  CC_MUTEX_LOCK(SoGlyph_mutex);
  
  if (activeGlyphs == NULL) {
    activeGlyphs = new SbList <coin_glyph_info>;
    coin_atexit((coin_atexit_f *)SoGlyph_cleanup, CC_ATEXIT_NORMAL);
  }

  int i, n = activeGlyphs->getLength();
  for (i = 0; i < n; i++) {
    if ((*activeGlyphs)[i].matches(character, fontsize[0], state_name, angle)) {
      SoGlyph *glyph = (*activeGlyphs)[i].glyph;
      PRIVATE(glyph)->refcount++;
      CC_MUTEX_UNLOCK(SoGlyph_mutex);
      return glyph;
    }
  }
  
  // FIXME: use font style in addition to font name. preng 2003-03-03
  SbString fontname = state_name.getString();

  const int font = cc_flw_get_font_id(fontname.getString(), fontsize[1], angle, 0.5f);
  // Should _always_ be able to get hold of a font.
  assert(font >= 0);
  cc_flw_ref_font(font);

  const int glyphidx = cc_flw_get_glyph(font, character);

  // Should _always_ be able to get hold of a glyph -- if no glyph is
  // available for a specific character, a default empty rectangle
  // should be used.  -mortene.
  assert(glyphidx >= 0);

  SoGlyph * g = new SoGlyph();
  PRIVATE(g)->fontidx = font;
  PRIVATE(g)->glyphidx = glyphidx;
  PRIVATE(g)->size = fontsize;
  PRIVATE(g)->angle = angle;
  PRIVATE(g)->character = character;
  coin_glyph_info info(character, state_size, state_name, g, angle);
  PRIVATE(g)->refcount++;
  activeGlyphs->append(info);

  CC_MUTEX_UNLOCK(SoGlyph_mutex);
  return g;
}

// Pixel advance for this glyph.
SbVec2s
SoGlyph::getAdvance(void) const
{
  assert(PRIVATE(this)->fontidx >= 0 && PRIVATE(this)->glyphidx >= 0);

  int x, y;
  // FIXME: the 'get_advance' call has been separated into one
  // 'get_vector_advance' and one 'get_bitmap_advance' call. As we
  // cannot separate whether we are dealing with bitmaps or vector
  // glyphs in this class, we choose 'get_bitmap_advance' as
  // default. (20030926 handegar)    
  cc_flw_get_bitmap_advance(PRIVATE(this)->fontidx, PRIVATE(this)->glyphidx, &x, &y);
  return SbVec2s((short)x, (short)y);
}

// Pixel kerning when rightglyph is placed to the right of this.
SbVec2s
SoGlyph::getKerning(const SoGlyph & rightglyph) const
{
  assert(PRIVATE(this)->fontidx >= 0 && PRIVATE(this)->glyphidx >= 0);
  assert(PRIVATE(&rightglyph)->fontidx >= 0 && PRIVATE(&rightglyph)->glyphidx >= 0);

  int x, y;
  // FIXME: Same issues as with 'getAdvance()'. See fixme. (20030926 handegar)
  cc_flw_get_bitmap_kerning(PRIVATE(this)->fontidx,
                            PRIVATE(this)->glyphidx, PRIVATE(&rightglyph)->glyphidx,
                            &x, &y);
  return SbVec2s((short)x, (short)y);
}

/*!
  Bitmap for glyph. \a size and \a pos are return parameters.
  Antialiased bitmap graphics not yet supported.

  Note that this function may return \c NULL if the glyph has no
  visible pixels (as for e.g. the space character).

  The returned buffer should \e not be deallocated by the caller.
*/
unsigned char *
SoGlyph::getBitmap(SbVec2s & size, SbVec2s & pos, const SbBool COIN_UNUSED_ARG(antialiased)) const
{
  if (PRIVATE(this)->bitmap == NULL) {
    PRIVATE(this)->bitmap = cc_flw_get_bitmap(PRIVATE(this)->fontidx, PRIVATE(this)->glyphidx);
  }
  struct cc_font_bitmap * bm = PRIVATE(this)->bitmap;
  assert(bm);
  
  PRIVATE(this)->bitmapwidth = bm->width;
  PRIVATE(this)->bitmapheight = bm->rows;
  
  size[0] = bm->pitch * 8;
  size[1] = bm->rows;
  pos[0] = bm->bearingX;
  pos[1] = bm->bearingY; 

  return bm->buffer;
}


void
SoGlyphP::setup3DFontData(void)
{
  PUBLIC(this)->setFontType(SoGlyph::FONT3D);
  
  if (character <= 32 || character >= 127) {
    // treat all these characters as spaces
    static int spaceidx[] = { -1 };
    PUBLIC(this)->setCoords(NULL);
    PUBLIC(this)->setFaceIndices(spaceidx);
    PUBLIC(this)->setEdgeIndices(spaceidx);
    this->bbox.setBounds(SbVec2f(0.0f, 0.0f), SbVec2f(0.2f, 0.0f));
    this->flags.didcalcbbox = 1;
  }
  else {
    cc_font_vector_glyph * vector_glyph = cc_flw_get_vector_glyph(this->fontidx, this->character);
    
    if (vector_glyph == NULL) {
      // Default hardcoded 3d font. Size = 1.0
      const int idx = this->character-33;
      PUBLIC(this)->setCoords((const SbVec2f *)coin_default3dfont_get_coords()[idx]);
      PUBLIC(this)->setFaceIndices(coin_default3dfont_get_faceidx()[idx]);
      PUBLIC(this)->setEdgeIndices(coin_default3dfont_get_edgeidx()[idx]);
    } 
    else {
      // Install truetype font

      PUBLIC(this)->setCoords((const SbVec2f *)cc_flw_get_vector_glyph_coords(vector_glyph));
      PUBLIC(this)->setFaceIndices(cc_flw_get_vector_glyph_faceidx(vector_glyph));
      PUBLIC(this)->setEdgeIndices(cc_flw_get_vector_glyph_edgeidx(vector_glyph));
    }
  }
}


// should handle platform-specific font loading
SoGlyph *
SoGlyphP::createSystemGlyph(const char COIN_UNUSED_ARG(character), int COIN_UNUSED_ARG(fontid))
{ 
  return NULL;
}

SoGlyph *
SoGlyphP::createSystemGlyph(const char COIN_UNUSED_ARG(character), const SbName & COIN_UNUSED_ARG(font))
{
  return NULL;
}

#undef PRIVATE
#undef PUBLIC