1 /*
2  * FTGL - OpenGL font library
3  *
4  * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
5  * Copyright (c) 2008 Sam Hocevar <sam@zoy.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "../config.h"
28 
29 #include "FTGL/ftgl.h"
30 
31 #include "../FTInternals.h"
32 #include "FTPixmapFontImpl.h"
33 
34 
35 //
36 //  FTPixmapFont
37 //
38 
39 
FTPixmapFont(char const * fontFilePath)40 FTPixmapFont::FTPixmapFont(char const *fontFilePath) :
41     FTFont(new FTPixmapFontImpl(this, fontFilePath))
42 {}
43 
44 
FTPixmapFont(const unsigned char * pBufferBytes,size_t bufferSizeInBytes)45 FTPixmapFont::FTPixmapFont(const unsigned char *pBufferBytes,
46                            size_t bufferSizeInBytes) :
47     FTFont(new FTPixmapFontImpl(this, pBufferBytes, bufferSizeInBytes))
48 {}
49 
50 
~FTPixmapFont()51 FTPixmapFont::~FTPixmapFont()
52 {}
53 
54 
MakeGlyph(FT_GlyphSlot ftGlyph)55 FTGlyph* FTPixmapFont::MakeGlyph(FT_GlyphSlot ftGlyph)
56 {
57     return new FTPixmapGlyph(ftGlyph);
58 }
59 
60 
61 //
62 //  FTPixmapFontImpl
63 //
64 
65 
FTPixmapFontImpl(FTFont * ftFont,const char * fontFilePath)66 FTPixmapFontImpl::FTPixmapFontImpl(FTFont *ftFont, const char* fontFilePath)
67 : FTFontImpl(ftFont, fontFilePath)
68 {
69     load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
70 }
71 
72 
FTPixmapFontImpl(FTFont * ftFont,const unsigned char * pBufferBytes,size_t bufferSizeInBytes)73 FTPixmapFontImpl::FTPixmapFontImpl(FTFont *ftFont,
74                                    const unsigned char *pBufferBytes,
75                                    size_t bufferSizeInBytes)
76 : FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes)
77 {
78     load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;
79 }
80 
81 
82 template <typename T>
RenderI(const T * string,const int len,FTPoint position,FTPoint spacing,int renderMode)83 inline FTPoint FTPixmapFontImpl::RenderI(const T* string, const int len,
84                                          FTPoint position, FTPoint spacing,
85                                          int renderMode)
86 {
87     // Protect GL_TEXTURE_2D and GL_BLEND, glPixelTransferf(), and blending
88     // functions.
89     glPushAttrib(GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT);
90 
91     // Protect glPixelStorei() calls (made by FTPixmapGlyphImpl::RenderImpl).
92     glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
93 
94     glEnable(GL_BLEND);
95     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
96 
97     glDisable(GL_TEXTURE_2D);
98 
99     GLfloat ftglColour[4];
100     glGetFloatv(GL_CURRENT_RASTER_COLOR, ftglColour);
101 
102     glPixelTransferf(GL_RED_SCALE, ftglColour[0]);
103     glPixelTransferf(GL_GREEN_SCALE, ftglColour[1]);
104     glPixelTransferf(GL_BLUE_SCALE, ftglColour[2]);
105     glPixelTransferf(GL_ALPHA_SCALE, ftglColour[3]);
106 
107     FTPoint tmp = FTFontImpl::Render(string, len,
108                                      position, spacing, renderMode);
109 
110     glPopClientAttrib();
111     glPopAttrib();
112 
113     return tmp;
114 }
115 
116 
Render(const char * string,const int len,FTPoint position,FTPoint spacing,int renderMode)117 FTPoint FTPixmapFontImpl::Render(const char * string, const int len,
118                                  FTPoint position, FTPoint spacing,
119                                  int renderMode)
120 {
121     return RenderI(string, len, position, spacing, renderMode);
122 }
123 
124 
Render(const wchar_t * string,const int len,FTPoint position,FTPoint spacing,int renderMode)125 FTPoint FTPixmapFontImpl::Render(const wchar_t * string, const int len,
126                                  FTPoint position, FTPoint spacing,
127                                  int renderMode)
128 {
129     return RenderI(string, len, position, spacing, renderMode);
130 }
131 
132