g2o
Loading...
Searching...
No Matches
freeglut_font.cpp
Go to the documentation of this file.
1/*
2 * freeglut_font.c
3 *
4 * Bitmap and stroke fonts displaying.
5 *
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <iostream>
29
30#include "freeglut_minimal.h"
31
32#define freeglut_return_if_fail(expr) \
33 if (!(expr)) return;
34#define freeglut_return_val_if_fail(expr, val) \
35 if (!(expr)) return val;
36
38
39/*
40 * Matches a font ID with a SFG_StrokeFont structure pointer.
41 * This was changed to match the GLUT header style.
42 */
44 if (font == GLUT_STROKE_ROMAN) return (SFG_StrokeFont*)&fgStrokeRoman;
45 if (font == GLUT_STROKE_MONO_ROMAN)
47
48 std::cerr << "stroke font " << (int)font << " not found" << std::endl;
49 return 0;
50}
51
52void glutStrokeString(FontID fontID, const char* string_) {
53 const unsigned char* string = reinterpret_cast<const unsigned char*>(string_);
54 unsigned char c;
55 int i, j;
56 float length = 0.0;
57 SFG_StrokeFont* font;
58 // FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeString" );
59 font = fghStrokeByID(fontID);
61 if (!string || !*string) return;
62
63 /*
64 * Step through the string, drawing each character.
65 * A newline will simply translate the next character's insertion
66 * point back to the start of the line and down one line.
67 */
68 while ((c = *string++))
69 if (c < font->Quantity) {
70 if (c == '\n') {
71 glTranslatef(-length, -(float)(font->Height), 0.0);
72 length = 0.0;
73 } else /* Not an EOL, draw the bitmap character */
74 {
75 const SFG_StrokeChar* schar = font->Characters[c];
76 if (schar) {
77 const SFG_StrokeStrip* strip = schar->Strips;
78
79 for (i = 0; i < schar->Number; i++, strip++) {
80 glBegin(GL_LINE_STRIP);
81 for (j = 0; j < strip->Number; j++)
82 glVertex2f(strip->Vertices[j].X, strip->Vertices[j].Y);
83
84 glEnd();
85 }
86
87 length += schar->Right;
88 glTranslatef(schar->Right, 0.0f, 0.0f);
89 }
90 }
91 }
92}
93
94/*
95 * Return the width of a string drawn using a stroke font
96 */
97int glutStrokeLength(FontID fontID, const char* string_) {
98 const unsigned char* string = reinterpret_cast<const unsigned char*>(string_);
99 unsigned char c;
100 float length = 0.0;
101 float this_line_length = 0.0;
102 SFG_StrokeFont* font;
103 // FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeLength" );
104 font = fghStrokeByID(fontID);
106 if (!string || !*string) return 0;
107
108 while ((c = *string++))
109 if (c < font->Quantity) {
110 if (c == '\n') /* EOL; reset the length of this line */
111 {
112 if (length < this_line_length) length = this_line_length;
113 this_line_length = 0.0;
114 } else /* Not an EOL, increment the length of this line */
115 {
116 const SFG_StrokeChar* schar = font->Characters[c];
117 if (schar) this_line_length += schar->Right;
118 }
119 }
120 if (length < this_line_length) length = this_line_length;
121 return (int)(length + 0.5);
122}
123
124/*
125 * Returns the height of a stroke font
126 */
127GLfloat glutStrokeHeight(FontID fontID) {
128 SFG_StrokeFont* font;
129 // FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutStrokeHeight" );
130 font = fghStrokeByID(fontID);
132 return font->Height;
133}
134
135} // namespace freeglut_minimal
136
137/*** END OF FILE ***/
#define freeglut_return_if_fail(expr)
#define freeglut_return_val_if_fail(expr, val)
const SFG_StrokeFont fgStrokeMonoRoman
static SFG_StrokeFont * fghStrokeByID(FontID font)
void glutStrokeString(FontID fontID, const char *string_)
const SFG_StrokeFont fgStrokeRoman
int glutStrokeLength(FontID fontID, const char *string_)
GLfloat glutStrokeHeight(FontID fontID)