summaryrefslogtreecommitdiff
path: root/src/glut_font.cxx
blob: 2940863f8c7425e374119d951a31ddddea752343 (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
//
// GLUT font routines 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
//
// The stroked text code was copied from FreeGLUT 2.4.0 which carries
// the following notice:
//
//     Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
//

// (sort of) emulation of Glut's bitmap drawing functions, using FL's
// font stuff.  Not all the fonts match!

#include <config.h>
#if HAVE_GL
#  include <FL/glut.H>

Fl_Glut_Bitmap_Font glutBitmap9By15 = {FL_SCREEN, 15};
Fl_Glut_Bitmap_Font glutBitmap8By13 = {FL_SCREEN, 13};
Fl_Glut_Bitmap_Font glutBitmapTimesRoman10 = {FL_TIMES, 10};
Fl_Glut_Bitmap_Font glutBitmapTimesRoman24 = {FL_TIMES, 24};
Fl_Glut_Bitmap_Font glutBitmapHelvetica10 = {FL_HELVETICA, 10};
Fl_Glut_Bitmap_Font glutBitmapHelvetica12 = {FL_HELVETICA, 12};
Fl_Glut_Bitmap_Font glutBitmapHelvetica18 = {FL_HELVETICA, 18};

void glutBitmapCharacter(void* font, int character) {
  gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
  char a[1]; a[0] = character;
  gl_draw(a,1);
}

int glutBitmapHeight(void* font) {
  gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
  return gl_height();
}

int glutBitmapLength(void *font, const unsigned char *string) {
  gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
  const char *s = (const char*)string;
  return int(gl_width(s)+.5);
}

void glutBitmapString(void *font, const unsigned char *string) {
  gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
  const char *s = (const char*)string;
  gl_draw(s);
}

int glutBitmapWidth(void* font, int character) {
  gl_font(((Fl_Glut_Bitmap_Font *)font)->font,((Fl_Glut_Bitmap_Font *)font)->size);
  return int(gl_width(character)+.5);
}


/*
 * Draw a stroke character
 */
void glutStrokeCharacter(void* fontID, int character) {
  const Fl_Glut_StrokeChar *schar;
  const Fl_Glut_StrokeStrip *strip;
  int i, j;
  Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;

  if (character < 0 || character >= font->Quantity) return;

  schar = font->Characters[character];
  if (!schar) return;

  strip = schar->Strips;

  for (i = 0; i < schar->Number; i++, strip++)
  {
    glBegin(GL_LINE_STRIP);
    for (j = 0; j < strip->Number; j++)
      glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
    glEnd();
  }

  glTranslatef(schar->Right, 0.0, 0.0);
}

void glutStrokeString(void* fontID, const unsigned char *string) {
  unsigned char c;
  int i, j;
  float length = 0.0;
  Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;

  if (!string || ! *string) return;

 /*
  * Step through the string, drawing each character.
  * A newline will simply translate the next character's insertion
  * point back to the start of the line and down one line.
  */
  while ((c = *string++) != 0) {
    if (c < font->Quantity) {
      if (c == '\n') {
        glTranslatef(-length, -(float)(font->Height), 0.0);
        length = 0.0;
      } else  {
        /* Not an EOL, draw the bitmap character */
        const Fl_Glut_StrokeChar *schar = font->Characters[c];
        if (schar) {
          const Fl_Glut_StrokeStrip *strip = schar->Strips;

          for (i = 0; i < schar->Number; i++, strip++) {
            glBegin(GL_LINE_STRIP);
            for (j = 0; j < strip->Number; j++)
              glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
            glEnd();
          }

          length += schar->Right;
          glTranslatef(schar->Right, 0.0, 0.0);
        }
      }
    }
  }
}

/*
 * Return the width in pixels of a stroke character
 */
int glutStrokeWidth( void* fontID, int character )
{
  const Fl_Glut_StrokeChar *schar;
  Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
  if (character < 0 || character >= font->Quantity) return 0;
  schar = font->Characters[ character ];

  return schar ? (int)(schar->Right + 0.5) : 0;
}

/*
 * Return the width of a string drawn using a stroke font
 */
int glutStrokeLength(void* fontID, const unsigned char* string) {
  unsigned char c;
  float length = 0.0;
  float this_line_length = 0.0;
  Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;

  if (!string || ! *string) return 0;

  while ((c = *string++) != 0) {
    if (c < font->Quantity) {
      if (c == '\n') {
        /* EOL; reset the length of this line */
        if (length < this_line_length) length = this_line_length;
        this_line_length = 0.0;
      } else {
        /* Not an EOL, increment the length of this line */
        const Fl_Glut_StrokeChar *schar = font->Characters[c];
        if (schar) this_line_length += schar->Right;
      }
    }
  }

  if (length < this_line_length) length = this_line_length;

  return (int)(length + 0.5);
}

/*
 * Returns the height of a stroke font
 */
GLfloat glutStrokeHeight(void* fontID) {
  Fl_Glut_StrokeFont* font = (Fl_Glut_StrokeFont *)fontID;
  return font->Height;
}

#endif // HAVE_GL