1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * This file is part of openfx-supportext <https://github.com/devernay/openfx-supportext>,
4  * Copyright (C) 2013-2018 INRIA
5  *
6  * openfx-supportext is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * openfx-supportext is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with openfx-supportext.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
18  * ***** END LICENSE BLOCK ***** */
19 
20 /*
21  * Small utility to draw text using OpenGL.
22  * This code is based on the free glut source code.
23  *
24  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
25  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
26  * Creation date: Thu Dec 2 1999
27  *
28  * Permission is hereby granted, free of charge, to any person obtaining a
29  * copy of this software and associated documentation files (the "Software"),
30  * to deal in the Software without restriction, including without limitation
31  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
32  * and/or sell copies of the Software, and to permit persons to whom the
33  * Software is furnished to do so, subject to the following conditions:
34  *
35  * The above copyright notice and this permission notice shall be included
36  * in all copies or substantial portions of the Software.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
39  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
41  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
42  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44  */
45 
46 
47 #include "ofxsOGLTextRenderer.h"
48 #include "ofxsOGLFontUtils.h"
49 
50 #include <cstdlib>
51 
52 namespace  {
53 const OFX::SFG_Font*
getFont(OFX::TextRenderer::Font font)54 getFont(OFX::TextRenderer::Font font)
55 {
56     switch (font) {
57     case OFX::TextRenderer::FONT_FIXED_8_X_13:
58 
59         return &OFX::fgFontFixed8x13;
60         break;
61     case OFX::TextRenderer::FONT_FIXED_9_X_15:
62 
63         return &OFX::fgFontFixed9x15;
64         break;
65     case OFX::TextRenderer::FONT_HELVETICA_10:
66 
67         return &OFX::fgFontHelvetica10;
68         break;
69     case OFX::TextRenderer::FONT_HELVETICA_12:
70 
71         return &OFX::fgFontHelvetica12;
72         break;
73     case OFX::TextRenderer::FONT_HELVETICA_18:
74 
75         return &OFX::fgFontHelvetica18;
76         break;
77     case OFX::TextRenderer::FONT_TIMES_ROMAN_10:
78 
79         return &OFX::fgFontTimesRoman10;
80         break;
81     case OFX::TextRenderer::FONT_TIMES_ROMAN_24:
82 
83         return &OFX::fgFontTimesRoman24;
84         break;
85     default:
86 
87         return (const OFX::SFG_Font*)NULL;
88         break;
89     }
90 }
91 }
92 
93 void
bitmapString(const char * string,TextRenderer::Font f)94 OFX::TextRenderer::bitmapString(const char *string,
95                                 TextRenderer::Font f)
96 {
97     unsigned char c;
98     float x = 0.0f;
99     const SFG_Font* font = getFont(f);
100 
101     if (!font) {
102         return;
103     }
104     if (!string || !*string) {
105         return;
106     }
107 
108     glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
109     glPixelStorei( GL_UNPACK_SWAP_BYTES,  GL_FALSE );
110     glPixelStorei( GL_UNPACK_LSB_FIRST,   GL_FALSE );
111     glPixelStorei( GL_UNPACK_ROW_LENGTH,  0        );
112     glPixelStorei( GL_UNPACK_SKIP_ROWS,   0        );
113     glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0        );
114     glPixelStorei( GL_UNPACK_ALIGNMENT,   1        );
115 
116     /*
117      * Step through the string, drawing each character.
118      * A newline will simply translate the next character's insertion
119      * point back to the start of the line and down one line.
120      */
121     while ( ( c = *string++) ) {
122         if (c == '\n') {
123             glBitmap ( 0, 0, 0, 0, -x, (float) -font->Height, NULL );
124             x = 0.0f;
125         } else {   /* Not an EOL, draw the bitmap character */
126             const GLubyte* face = font->Characters[ c ];
127 
128             glBitmap(
129                 face[ 0 ], font->Height,          /* Bitmap's width and height    */
130                 font->xorig, font->yorig,         /* The origin in the font glyph */
131                 ( float )( face[ 0 ] ), 0.0f,     /* The raster advance; inc. x,y */
132                 ( face + 1 )                      /* The packed bitmap data...    */
133                 );
134 
135             x += ( float )( face[ 0 ] );
136         }
137     }
138 
139     glPopClientAttrib( );
140 }
141 
142 void
bitmapString(double x,double y,const char * string,TextRenderer::Font font)143 OFX::TextRenderer::bitmapString(double x,
144                                 double y,
145                                 const char*string,
146                                 TextRenderer::Font font)
147 {
148     //glPushAttrib(GL_CURRENT_BIT); // caller is responsible for protecting attribs
149     glRasterPos2d(x, y);
150     bitmapString(string, font);
151     //glPopAttrib();
152 }
153