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@hocevar.net>
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 <string>
30 
31 #include "FTGL/ftgl.h"
32 
33 #include "FTInternals.h"
34 #include "FTBitmapGlyphImpl.h"
35 
36 
37 //
38 //  FTGLBitmapGlyph
39 //
40 
41 
FTBitmapGlyph(FT_GlyphSlot glyph)42 FTBitmapGlyph::FTBitmapGlyph(FT_GlyphSlot glyph) :
43     FTGlyph(new FTBitmapGlyphImpl(glyph))
44 {}
45 
46 
~FTBitmapGlyph()47 FTBitmapGlyph::~FTBitmapGlyph()
48 {}
49 
50 
Render(const FTPoint & pen,int renderMode)51 const FTPoint& FTBitmapGlyph::Render(const FTPoint& pen, int renderMode)
52 {
53     FTBitmapGlyphImpl *myimpl = dynamic_cast<FTBitmapGlyphImpl *>(impl);
54     return myimpl->RenderImpl(pen, renderMode);
55 }
56 
57 
58 //
59 //  FTGLBitmapGlyphImpl
60 //
61 
62 
FTBitmapGlyphImpl(FT_GlyphSlot glyph)63 FTBitmapGlyphImpl::FTBitmapGlyphImpl(FT_GlyphSlot glyph)
64 :   FTGlyphImpl(glyph),
65     destWidth(0),
66     destHeight(0),
67     data(0)
68 {
69     err = FT_Render_Glyph(glyph, FT_RENDER_MODE_MONO);
70     if(err || ft_glyph_format_bitmap != glyph->format)
71     {
72         return;
73     }
74 
75     FT_Bitmap bitmap = glyph->bitmap;
76 
77     unsigned int srcWidth = bitmap.width;
78     unsigned int srcHeight = bitmap.rows;
79     unsigned int srcPitch = bitmap.pitch;
80 
81     destWidth = srcWidth;
82     destHeight = srcHeight;
83     destPitch = srcPitch;
84 
85     if(destWidth && destHeight)
86     {
87         data = new unsigned char[destPitch * destHeight];
88         unsigned char* dest = data + ((destHeight - 1) * destPitch);
89 
90         unsigned char* src = bitmap.buffer;
91 
92         for(unsigned int y = 0; y < srcHeight; ++y)
93         {
94             memcpy(dest, src, srcPitch);
95             dest -= destPitch;
96             src += srcPitch;
97         }
98     }
99 
100     pos = FTPoint(glyph->bitmap_left, static_cast<int>(srcHeight) - glyph->bitmap_top, 0.0);
101 }
102 
103 
~FTBitmapGlyphImpl()104 FTBitmapGlyphImpl::~FTBitmapGlyphImpl()
105 {
106     delete [] data;
107 }
108 
109 
RenderImpl(const FTPoint & pen,int renderMode)110 const FTPoint& FTBitmapGlyphImpl::RenderImpl(const FTPoint& pen, int renderMode)
111 {
112     (void)renderMode;
113 
114     if(data)
115     {
116         float dx, dy;
117 
118         dx = pen.Xf() + pos.Xf();
119         dy = pen.Yf() - pos.Yf();
120 
121         glBitmap(0, 0, 0.0f, 0.0f, dx, dy, (const GLubyte*)0);
122         glPixelStorei(GL_UNPACK_ROW_LENGTH, destPitch * 8);
123         glBitmap(destWidth, destHeight, 0.0f, 0.0, 0.0, 0.0,
124                  (const GLubyte*)data);
125         glBitmap(0, 0, 0.0f, 0.0f, -dx, -dy, (const GLubyte*)0);
126     }
127 
128     return advance;
129 }
130 
131