From 37a92b86d13e89d0dcec92be6d23ceced29dbc36 Mon Sep 17 00:00:00 2001 From: scuri Date: Mon, 21 Jun 2010 22:55:10 +0000 Subject: *** empty log message *** --- src/ftgl/FTFont/FTBitmapFont.cpp | 106 +++++++ src/ftgl/FTFont/FTBitmapFontImpl.h | 62 ++++ src/ftgl/FTFont/FTBufferFont.cpp | 349 +++++++++++++++++++++++ src/ftgl/FTFont/FTBufferFontImpl.h | 81 ++++++ src/ftgl/FTFont/FTExtrudeFont.cpp | 89 ++++++ src/ftgl/FTFont/FTExtrudeFontImpl.h | 81 ++++++ src/ftgl/FTFont/FTFont.cpp | 552 ++++++++++++++++++++++++++++++++++++ src/ftgl/FTFont/FTFontGlue.cpp | 227 +++++++++++++++ src/ftgl/FTFont/FTFontImpl.h | 162 +++++++++++ src/ftgl/FTFont/FTOutlineFont.cpp | 129 +++++++++ src/ftgl/FTFont/FTOutlineFontImpl.h | 73 +++++ src/ftgl/FTFont/FTPixmapFont.cpp | 131 +++++++++ src/ftgl/FTFont/FTPixmapFontImpl.h | 60 ++++ src/ftgl/FTFont/FTPolygonFont.cpp | 89 ++++++ src/ftgl/FTFont/FTPolygonFontImpl.h | 60 ++++ src/ftgl/FTFont/FTTextureFont.cpp | 267 +++++++++++++++++ src/ftgl/FTFont/FTTextureFontImpl.h | 152 ++++++++++ 17 files changed, 2670 insertions(+) create mode 100644 src/ftgl/FTFont/FTBitmapFont.cpp create mode 100644 src/ftgl/FTFont/FTBitmapFontImpl.h create mode 100644 src/ftgl/FTFont/FTBufferFont.cpp create mode 100644 src/ftgl/FTFont/FTBufferFontImpl.h create mode 100644 src/ftgl/FTFont/FTExtrudeFont.cpp create mode 100644 src/ftgl/FTFont/FTExtrudeFontImpl.h create mode 100644 src/ftgl/FTFont/FTFont.cpp create mode 100644 src/ftgl/FTFont/FTFontGlue.cpp create mode 100644 src/ftgl/FTFont/FTFontImpl.h create mode 100644 src/ftgl/FTFont/FTOutlineFont.cpp create mode 100644 src/ftgl/FTFont/FTOutlineFontImpl.h create mode 100644 src/ftgl/FTFont/FTPixmapFont.cpp create mode 100644 src/ftgl/FTFont/FTPixmapFontImpl.h create mode 100644 src/ftgl/FTFont/FTPolygonFont.cpp create mode 100644 src/ftgl/FTFont/FTPolygonFontImpl.h create mode 100644 src/ftgl/FTFont/FTTextureFont.cpp create mode 100644 src/ftgl/FTFont/FTTextureFontImpl.h (limited to 'src/ftgl/FTFont') diff --git a/src/ftgl/FTFont/FTBitmapFont.cpp b/src/ftgl/FTFont/FTBitmapFont.cpp new file mode 100644 index 0000000..ebf3f1e --- /dev/null +++ b/src/ftgl/FTFont/FTBitmapFont.cpp @@ -0,0 +1,106 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTBitmapFontImpl.h" + + +// +// FTBitmapFont +// + + +FTBitmapFont::FTBitmapFont(char const *fontFilePath) : + FTFont(new FTBitmapFontImpl(this, fontFilePath)) +{} + + +FTBitmapFont::FTBitmapFont(unsigned char const *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTBitmapFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTBitmapFont::~FTBitmapFont() +{} + + +FTGlyph* FTBitmapFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + return new FTBitmapGlyph(ftGlyph); +} + + +// +// FTBitmapFontImpl +// + + +template +inline FTPoint FTBitmapFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + // Protect GL_BLEND + glPushAttrib(GL_COLOR_BUFFER_BIT); + + // Protect glPixelStorei() calls (also in FTBitmapGlyphImpl::RenderImpl) + glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); + + glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + glDisable(GL_BLEND); + + FTPoint tmp = FTFontImpl::Render(string, len, + position, spacing, renderMode); + + glPopClientAttrib(); + glPopAttrib(); + + return tmp; +} + + +FTPoint FTBitmapFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +FTPoint FTBitmapFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + diff --git a/src/ftgl/FTFont/FTBitmapFontImpl.h b/src/ftgl/FTFont/FTBitmapFontImpl.h new file mode 100644 index 0000000..7f733d6 --- /dev/null +++ b/src/ftgl/FTFont/FTBitmapFontImpl.h @@ -0,0 +1,62 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTBitmapFontImpl__ +#define __FTBitmapFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; + +class FTBitmapFontImpl : public FTFontImpl +{ + friend class FTBitmapFont; + + protected: + FTBitmapFontImpl(FTFont *ftFont, const char* fontFilePath) : + FTFontImpl(ftFont, fontFilePath) {}; + + FTBitmapFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes) {}; + + virtual FTPoint Render(const char *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + private: + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); +}; + +#endif // __FTBitmapFontImpl__ + diff --git a/src/ftgl/FTFont/FTBufferFont.cpp b/src/ftgl/FTFont/FTBufferFont.cpp new file mode 100644 index 0000000..e48e2ab --- /dev/null +++ b/src/ftgl/FTFont/FTBufferFont.cpp @@ -0,0 +1,349 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTBufferFontImpl.h" + + +// +// FTBufferFont +// + + +FTBufferFont::FTBufferFont(char const *fontFilePath) : + FTFont(new FTBufferFontImpl(this, fontFilePath)) +{} + + +FTBufferFont::FTBufferFont(unsigned char const *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTBufferFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTBufferFont::~FTBufferFont() +{} + + +FTGlyph* FTBufferFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + FTBufferFontImpl *myimpl = dynamic_cast(impl); + if(!myimpl) + { + return NULL; + } + + return myimpl->MakeGlyphImpl(ftGlyph); +} + + +// +// FTBufferFontImpl +// + + +FTBufferFontImpl::FTBufferFontImpl(FTFont *ftFont, const char* fontFilePath) : + FTFontImpl(ftFont, fontFilePath), + buffer(new FTBuffer()) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; + + glGenTextures(BUFFER_CACHE_SIZE, idCache); + + for(int i = 0; i < BUFFER_CACHE_SIZE; i++) + { + stringCache[i] = NULL; + glBindTexture(GL_TEXTURE_2D, idCache[i]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + + lastString = 0; +} + + +FTBufferFontImpl::FTBufferFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes), + buffer(new FTBuffer()) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; + + glGenTextures(BUFFER_CACHE_SIZE, idCache); + + for(int i = 0; i < BUFFER_CACHE_SIZE; i++) + { + stringCache[i] = NULL; + glBindTexture(GL_TEXTURE_2D, idCache[i]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + } + + lastString = 0; +} + + +FTBufferFontImpl::~FTBufferFontImpl() +{ + glDeleteTextures(BUFFER_CACHE_SIZE, idCache); + + for(int i = 0; i < BUFFER_CACHE_SIZE; i++) + { + if(stringCache[i]) + { + free(stringCache[i]); + } + } + + delete buffer; +} + + +FTGlyph* FTBufferFontImpl::MakeGlyphImpl(FT_GlyphSlot ftGlyph) +{ + return new FTBufferGlyph(ftGlyph, buffer); +} + + +bool FTBufferFontImpl::FaceSize(const unsigned int size, + const unsigned int res) +{ + for(int i = 0; i < BUFFER_CACHE_SIZE; i++) + { + if(stringCache[i]) + { + free(stringCache[i]); + stringCache[i] = NULL; + } + } + + return FTFontImpl::FaceSize(size, res); +} + + +static inline GLuint NextPowerOf2(GLuint in) +{ + in -= 1; + + in |= in >> 16; + in |= in >> 8; + in |= in >> 4; + in |= in >> 2; + in |= in >> 1; + + return in + 1; +} + + +inline int StringCompare(void const *a, char const *b, int len) +{ + return len < 0 ? strcmp((char const *)a, b) + : strncmp((char const *)a, b, len); +} + + +inline int StringCompare(void const *a, wchar_t const *b, int len) +{ + return len < 0 ? wcscmp((wchar_t const *)a, b) + : wcsncmp((wchar_t const *)a, b, len); +} + + +inline char *StringCopy(char const *s, int len) +{ + if(len < 0) + { + return strdup(s); + } + else + { +#ifdef HAVE_STRNDUP + return strndup(s, len); +#else + char *s2 = (char*)malloc(len + 1); + memcpy(s2, s, len); + s2[len] = 0; + return s2; +#endif + } +} + + +inline wchar_t *StringCopy(wchar_t const *s, int len) +{ + if(len < 0) + { +#if defined HAVE_WCSDUP + return wcsdup(s); +#else + len = (int)wcslen(s); +#endif + } + + wchar_t *s2 = (wchar_t *)malloc((len + 1) * sizeof(wchar_t)); + memcpy(s2, s, len * sizeof(wchar_t)); + s2[len] = 0; + return s2; +} + + +template +inline FTPoint FTBufferFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + const float padding = 3.0f; + int width, height, texWidth, texHeight; + int cacheIndex = -1; + bool inCache = false; + + // Protect blending functions, GL_BLEND and GL_TEXTURE_2D + glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT); + + // Protect glPixelStorei() calls + glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); + + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE + + // Search whether the string is already in a texture we uploaded + for(int n = 0; n < BUFFER_CACHE_SIZE; n++) + { + int i = (lastString + n + BUFFER_CACHE_SIZE) % BUFFER_CACHE_SIZE; + + if(stringCache[i] && !StringCompare(stringCache[i], string, len)) + { + cacheIndex = i; + inCache = true; + break; + } + } + + // If the string was not found, we need to put it in the cache and compute + // its new bounding box. + if(!inCache) + { + // FIXME: this cache is not very efficient. We should first expire + // strings that are not used very often. + cacheIndex = lastString; + lastString = (lastString + 1) % BUFFER_CACHE_SIZE; + + if(stringCache[cacheIndex]) + { + free(stringCache[cacheIndex]); + } + // FIXME: only the first N bytes are copied; we want the first N chars. + stringCache[cacheIndex] = StringCopy(string, len); + bboxCache[cacheIndex] = BBox(string, len, FTPoint(), spacing); + } + + FTBBox bbox = bboxCache[cacheIndex]; + + width = static_cast(bbox.Upper().X() - bbox.Lower().X() + + padding + padding + 0.5); + height = static_cast(bbox.Upper().Y() - bbox.Lower().Y() + + padding + padding + 0.5); + + texWidth = NextPowerOf2(width); + texHeight = NextPowerOf2(height); + + glBindTexture(GL_TEXTURE_2D, idCache[cacheIndex]); + + // If the string was not found, we need to render the text in a new + // texture buffer, then upload it to the OpenGL layer. + if(!inCache) + { + buffer->Size(texWidth, texHeight); + buffer->Pos(FTPoint(padding, padding) - bbox.Lower()); + + advanceCache[cacheIndex] = + FTFontImpl::Render(string, len, FTPoint(), spacing, renderMode); + + glBindTexture(GL_TEXTURE_2D, idCache[cacheIndex]); + + glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + /* TODO: use glTexSubImage2D later? */ + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texWidth, texHeight, 0, + GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid *)buffer->Pixels()); + + buffer->Size(0, 0); + } + + FTPoint low = position + bbox.Lower(); + FTPoint up = position + bbox.Upper(); + + glBegin(GL_QUADS); + glNormal3f(0.0f, 0.0f, 1.0f); + glTexCoord2f(padding / texWidth, + (texHeight - height + padding) / texHeight); + glVertex2f(low.Xf(), up.Yf()); + glTexCoord2f(padding / texWidth, + (texHeight - padding) / texHeight); + glVertex2f(low.Xf(), low.Yf()); + glTexCoord2f((width - padding) / texWidth, + (texHeight - padding) / texHeight); + glVertex2f(up.Xf(), low.Yf()); + glTexCoord2f((width - padding) / texWidth, + (texHeight - height + padding) / texHeight); + glVertex2f(up.Xf(), up.Yf()); + glEnd(); + + glPopClientAttrib(); + glPopAttrib(); + + return position + advanceCache[cacheIndex]; +} + + +FTPoint FTBufferFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +FTPoint FTBufferFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + diff --git a/src/ftgl/FTFont/FTBufferFontImpl.h b/src/ftgl/FTFont/FTBufferFontImpl.h new file mode 100644 index 0000000..15557f6 --- /dev/null +++ b/src/ftgl/FTFont/FTBufferFontImpl.h @@ -0,0 +1,81 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTBufferFontImpl__ +#define __FTBufferFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; +class FTBuffer; + +class FTBufferFontImpl : public FTFontImpl +{ + friend class FTBufferFont; + + protected: + FTBufferFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTBufferFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + virtual ~FTBufferFontImpl(); + + virtual FTPoint Render(const char *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual bool FaceSize(const unsigned int size, + const unsigned int res); + + private: + /** + * Create an FTBufferGlyph object for the base class. + */ + FTGlyph* MakeGlyphImpl(FT_GlyphSlot ftGlyph); + + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); + + /* Pixel buffer */ + FTBuffer *buffer; + + static const int BUFFER_CACHE_SIZE = 16; + /* Texture IDs */ + GLuint idCache[BUFFER_CACHE_SIZE]; + void *stringCache[BUFFER_CACHE_SIZE]; + FTBBox bboxCache[BUFFER_CACHE_SIZE]; + FTPoint advanceCache[BUFFER_CACHE_SIZE]; + int lastString; +}; + +#endif // __FTBufferFontImpl__ + diff --git a/src/ftgl/FTFont/FTExtrudeFont.cpp b/src/ftgl/FTFont/FTExtrudeFont.cpp new file mode 100644 index 0000000..9e8eb72 --- /dev/null +++ b/src/ftgl/FTFont/FTExtrudeFont.cpp @@ -0,0 +1,89 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTExtrudeFontImpl.h" + + +// +// FTExtrudeFont +// + + +FTExtrudeFont::FTExtrudeFont(char const *fontFilePath) : + FTFont(new FTExtrudeFontImpl(this, fontFilePath)) +{} + + +FTExtrudeFont::FTExtrudeFont(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTExtrudeFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTExtrudeFont::~FTExtrudeFont() +{} + + +FTGlyph* FTExtrudeFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + FTExtrudeFontImpl *myimpl = dynamic_cast(impl); + if(!myimpl) + { + return NULL; + } + + return new FTExtrudeGlyph(ftGlyph, myimpl->depth, myimpl->front, + myimpl->back, myimpl->useDisplayLists); +} + + +// +// FTExtrudeFontImpl +// + + +FTExtrudeFontImpl::FTExtrudeFontImpl(FTFont *ftFont, const char* fontFilePath) +: FTFontImpl(ftFont, fontFilePath), + depth(0.0f), front(0.0f), back(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + + +FTExtrudeFontImpl::FTExtrudeFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes), + depth(0.0f), front(0.0f), back(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + diff --git a/src/ftgl/FTFont/FTExtrudeFontImpl.h b/src/ftgl/FTFont/FTExtrudeFontImpl.h new file mode 100644 index 0000000..c2ddf27 --- /dev/null +++ b/src/ftgl/FTFont/FTExtrudeFontImpl.h @@ -0,0 +1,81 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTExtrudeFontImpl__ +#define __FTExtrudeFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; + +class FTExtrudeFontImpl : public FTFontImpl +{ + friend class FTExtrudeFont; + + protected: + FTExtrudeFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTExtrudeFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + /** + * Set the extrusion distance for the font. + * + * @param d The extrusion distance. + */ + virtual void Depth(float d) { depth = d; } + + /** + * Set the outset distance for the font. Only implemented by + * FTOutlineFont, FTPolygonFont and FTExtrudeFont + * + * @param o The outset distance. + */ + virtual void Outset(float o) { front = back = o; } + + /** + * Set the outset distance for the font. Only implemented by + * FTExtrudeFont + * + * @param f The front outset distance. + * @param b The back outset distance. + */ + virtual void Outset(float f, float b) { front = f; back = b; } + + private: + /** + * The extrusion distance for the font. + */ + float depth; + + /** + * The outset distance (front and back) for the font. + */ + float front, back; +}; + +#endif // __FTExtrudeFontImpl__ + diff --git a/src/ftgl/FTFont/FTFont.cpp b/src/ftgl/FTFont/FTFont.cpp new file mode 100644 index 0000000..a7206fb --- /dev/null +++ b/src/ftgl/FTFont/FTFont.cpp @@ -0,0 +1,552 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTInternals.h" +#include "FTUnicode.h" + +#include "FTFontImpl.h" + +#include "FTBitmapFontImpl.h" +#include "FTExtrudeFontImpl.h" +#include "FTOutlineFontImpl.h" +#include "FTPixmapFontImpl.h" +#include "FTPolygonFontImpl.h" +#include "FTTextureFontImpl.h" + +#include "FTGlyphContainer.h" +#include "FTFace.h" + + +// +// FTFont +// + + +FTFont::FTFont(char const *fontFilePath) +{ + impl = new FTFontImpl(this, fontFilePath); +} + + +FTFont::FTFont(const unsigned char *pBufferBytes, size_t bufferSizeInBytes) +{ + impl = new FTFontImpl(this, pBufferBytes, bufferSizeInBytes); +} + + +FTFont::FTFont(FTFontImpl *pImpl) +{ + impl = pImpl; +} + + +FTFont::~FTFont() +{ + delete impl; +} + + +bool FTFont::Attach(const char* fontFilePath) +{ + return impl->Attach(fontFilePath); +} + + +bool FTFont::Attach(const unsigned char *pBufferBytes, size_t bufferSizeInBytes) +{ + return impl->Attach(pBufferBytes, bufferSizeInBytes); +} + + +bool FTFont::FaceSize(const unsigned int size, const unsigned int res) +{ + return impl->FaceSize(size, res); +} + + +unsigned int FTFont::FaceSize() const +{ + return impl->FaceSize(); +} + + +void FTFont::Depth(float depth) +{ + return impl->Depth(depth); +} + + +void FTFont::Outset(float outset) +{ + return impl->Outset(outset); +} + + +void FTFont::Outset(float front, float back) +{ + return impl->Outset(front, back); +} + + +void FTFont::GlyphLoadFlags(FT_Int flags) +{ + return impl->GlyphLoadFlags(flags); +} + + +bool FTFont::CharMap(FT_Encoding encoding) +{ + return impl->CharMap(encoding); +} + + +unsigned int FTFont::CharMapCount() const +{ + return impl->CharMapCount(); +} + + +FT_Encoding* FTFont::CharMapList() +{ + return impl->CharMapList(); +} + + +void FTFont::UseDisplayList(bool useList) +{ + return impl->UseDisplayList(useList); +} + + +float FTFont::Ascender() const +{ + return impl->Ascender(); +} + + +float FTFont::Descender() const +{ + return impl->Descender(); +} + + +float FTFont::LineHeight() const +{ + return impl->LineHeight(); +} + + +FTPoint FTFont::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, int renderMode) +{ + return impl->Render(string, len, position, spacing, renderMode); +} + + +FTPoint FTFont::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, int renderMode) +{ + return impl->Render(string, len, position, spacing, renderMode); +} + + +float FTFont::Advance(const char * string, const int len, FTPoint spacing) +{ + return impl->Advance(string, len, spacing); +} + + +float FTFont::Advance(const wchar_t * string, const int len, FTPoint spacing) +{ + return impl->Advance(string, len, spacing); +} + + +FTBBox FTFont::BBox(const char *string, const int len, + FTPoint position, FTPoint spacing) +{ + return impl->BBox(string, len, position, spacing); +} + + +FTBBox FTFont::BBox(const wchar_t *string, const int len, + FTPoint position, FTPoint spacing) +{ + return impl->BBox(string, len, position, spacing); +} + + +FT_Error FTFont::Error() const +{ + return impl->err; +} + + +// +// FTFontImpl +// + + +FTFontImpl::FTFontImpl(FTFont *ftFont, char const *fontFilePath) : + face(fontFilePath), + useDisplayLists(true), + load_flags(FT_LOAD_DEFAULT), + intf(ftFont), + glyphList(0) +{ + err = face.Error(); + if(err == 0) + { + glyphList = new FTGlyphContainer(&face); + } +} + + +FTFontImpl::FTFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + face(pBufferBytes, bufferSizeInBytes), + useDisplayLists(true), + load_flags(FT_LOAD_DEFAULT), + intf(ftFont), + glyphList(0) +{ + err = face.Error(); + if(err == 0) + { + glyphList = new FTGlyphContainer(&face); + } +} + + +FTFontImpl::~FTFontImpl() +{ + if(glyphList) + { + delete glyphList; + } +} + + +bool FTFontImpl::Attach(const char* fontFilePath) +{ + if(!face.Attach(fontFilePath)) + { + err = face.Error(); + return false; + } + + err = 0; + return true; +} + + +bool FTFontImpl::Attach(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +{ + if(!face.Attach(pBufferBytes, bufferSizeInBytes)) + { + err = face.Error(); + return false; + } + + err = 0; + return true; +} + + +bool FTFontImpl::FaceSize(const unsigned int size, const unsigned int res) +{ + if(glyphList != NULL) + { + delete glyphList; + glyphList = NULL; + } + + charSize = face.Size(size, res); + err = face.Error(); + + if(err != 0) + { + return false; + } + + glyphList = new FTGlyphContainer(&face); + return true; +} + + +unsigned int FTFontImpl::FaceSize() const +{ + return charSize.CharSize(); +} + + +void FTFontImpl::Depth(float depth) +{ + ; +} + + +void FTFontImpl::Outset(float outset) +{ + ; +} + + +void FTFontImpl::Outset(float front, float back) +{ + ; +} + + +void FTFontImpl::GlyphLoadFlags(FT_Int flags) +{ + load_flags = flags; +} + + +bool FTFontImpl::CharMap(FT_Encoding encoding) +{ + bool result = glyphList->CharMap(encoding); + err = glyphList->Error(); + return result; +} + + +unsigned int FTFontImpl::CharMapCount() const +{ + return face.CharMapCount(); +} + + +FT_Encoding* FTFontImpl::CharMapList() +{ + return face.CharMapList(); +} + + +void FTFontImpl::UseDisplayList(bool useList) +{ + useDisplayLists = useList; +} + + +float FTFontImpl::Ascender() const +{ + return charSize.Ascender(); +} + + +float FTFontImpl::Descender() const +{ + return charSize.Descender(); +} + + +float FTFontImpl::LineHeight() const +{ + return charSize.Height(); +} + + +template +inline FTBBox FTFontImpl::BBoxI(const T* string, const int len, + FTPoint position, FTPoint spacing) +{ + FTBBox totalBBox; + + /* Only compute the bounds if string is non-empty. */ + if(string && ('\0' != string[0])) + { + // for multibyte - we can't rely on sizeof(T) == character + FTUnicodeStringItr ustr(string); + unsigned int thisChar = *ustr++; + unsigned int nextChar = *ustr; + + if(CheckGlyph(thisChar)) + { + totalBBox = glyphList->BBox(thisChar); + totalBBox += position; + + position += FTPoint(glyphList->Advance(thisChar, nextChar), 0.0); + } + + /* Expand totalBox by each glyph in string */ + for(int i = 1; (len < 0 && *ustr) || (len >= 0 && i < len); i++) + { + thisChar = *ustr++; + nextChar = *ustr; + + if(CheckGlyph(thisChar)) + { + position += spacing; + + FTBBox tempBBox = glyphList->BBox(thisChar); + tempBBox += position; + totalBBox |= tempBBox; + + position += FTPoint(glyphList->Advance(thisChar, nextChar), + 0.0); + } + } + } + + return totalBBox; +} + + +FTBBox FTFontImpl::BBox(const char *string, const int len, + FTPoint position, FTPoint spacing) +{ + /* The chars need to be unsigned because they are cast to int later */ + return BBoxI((const unsigned char *)string, len, position, spacing); +} + + +FTBBox FTFontImpl::BBox(const wchar_t *string, const int len, + FTPoint position, FTPoint spacing) +{ + return BBoxI(string, len, position, spacing); +} + + +template +inline float FTFontImpl::AdvanceI(const T* string, const int len, + FTPoint spacing) +{ + float advance = 0.0f; + FTUnicodeStringItr ustr(string); + + for(int i = 0; (len < 0 && *ustr) || (len >= 0 && i < len); i++) + { + unsigned int thisChar = *ustr++; + unsigned int nextChar = *ustr; + + if(CheckGlyph(thisChar)) + { + advance += glyphList->Advance(thisChar, nextChar); + } + + if(nextChar) + { + advance += spacing.Xf(); + } + } + + return advance; +} + + +float FTFontImpl::Advance(const char* string, const int len, FTPoint spacing) +{ + /* The chars need to be unsigned because they are cast to int later */ + return AdvanceI((const unsigned char *)string, len, spacing); +} + + +float FTFontImpl::Advance(const wchar_t* string, const int len, FTPoint spacing) +{ + return AdvanceI(string, len, spacing); +} + + +template +inline FTPoint FTFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + // for multibyte - we can't rely on sizeof(T) == character + FTUnicodeStringItr ustr(string); + + for(int i = 0; (len < 0 && *ustr) || (len >= 0 && i < len); i++) + { + unsigned int thisChar = *ustr++; + unsigned int nextChar = *ustr; + + if(CheckGlyph(thisChar)) + { + position += glyphList->Render(thisChar, nextChar, + position, renderMode); + } + + if(nextChar) + { + position += spacing; + } + } + + return position; +} + + +FTPoint FTFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, int renderMode) +{ + return RenderI((const unsigned char *)string, + len, position, spacing, renderMode); +} + + +FTPoint FTFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +bool FTFontImpl::CheckGlyph(const unsigned int characterCode) +{ + if(glyphList->Glyph(characterCode)) + { + return true; + } + + unsigned int glyphIndex = glyphList->FontIndex(characterCode); + FT_GlyphSlot ftSlot = face.Glyph(glyphIndex, load_flags); + if(!ftSlot) + { + err = face.Error(); + return false; + } + + FTGlyph* tempGlyph = intf->MakeGlyph(ftSlot); + if(!tempGlyph) + { + if(0 == err) + { + err = 0x13; + } + + return false; + } + + glyphList->Add(tempGlyph, characterCode); + + return true; +} + diff --git a/src/ftgl/FTFont/FTFontGlue.cpp b/src/ftgl/FTFont/FTFontGlue.cpp new file mode 100644 index 0000000..b23e787 --- /dev/null +++ b/src/ftgl/FTFont/FTFontGlue.cpp @@ -0,0 +1,227 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Éric Beets + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTInternals.h" + +static const FTPoint static_ftpoint; +static const FTBBox static_ftbbox; + +FTGL_BEGIN_C_DECLS + +#define C_TOR(cname, cargs, cxxname, cxxarg, cxxtype) \ + FTGLfont* cname cargs \ + { \ + cxxname *f = new cxxname cxxarg; \ + if(f->Error()) \ + { \ + delete f; \ + return NULL; \ + } \ + FTGLfont *ftgl = (FTGLfont *)malloc(sizeof(FTGLfont)); \ + ftgl->ptr = f; \ + ftgl->type = cxxtype; \ + return ftgl; \ + } + +// FTBitmapFont::FTBitmapFont(); +C_TOR(ftglCreateBitmapFont, (const char *fontname), + FTBitmapFont, (fontname), FONT_BITMAP); + +// FTBufferFont::FTBufferFont(); +C_TOR(ftglCreateBufferFont, (const char *fontname), + FTBufferFont, (fontname), FONT_BUFFER); + +// FTExtrudeFont::FTExtrudeFont(); +C_TOR(ftglCreateExtrudeFont, (const char *fontname), + FTExtrudeFont, (fontname), FONT_EXTRUDE); + +// FTOutlineFont::FTOutlineFont(); +C_TOR(ftglCreateOutlineFont, (const char *fontname), + FTOutlineFont, (fontname), FONT_OUTLINE); + +// FTPixmapFont::FTPixmapFont(); +C_TOR(ftglCreatePixmapFont, (const char *fontname), + FTPixmapFont, (fontname), FONT_PIXMAP); + +// FTPolygonFont::FTPolygonFont(); +C_TOR(ftglCreatePolygonFont, (const char *fontname), + FTPolygonFont, (fontname), FONT_POLYGON); + +// FTTextureFont::FTTextureFont(); +C_TOR(ftglCreateTextureFont, (const char *fontname), + FTTextureFont, (fontname), FONT_TEXTURE); + +// FTCustomFont::FTCustomFont(); +class FTCustomFont : public FTFont +{ +public: + FTCustomFont(char const *fontFilePath, void *p, + FTGLglyph * (*makeglyph) (FT_GlyphSlot, void *)) + : FTFont(fontFilePath), + data(p), + makeglyphCallback(makeglyph) + {} + + ~FTCustomFont() + {} + + FTGlyph* MakeGlyph(FT_GlyphSlot slot) + { + FTGLglyph *g = makeglyphCallback(slot, data); + FTGlyph *glyph = g->ptr; + // XXX: we no longer need g, and no one will free it for us. Not + // very elegant, and we need to make sure no one else will try to + // use it. + free(g); + return glyph; + } + +private: + void *data; + FTGLglyph *(*makeglyphCallback) (FT_GlyphSlot, void *); +}; + +C_TOR(ftglCreateCustomFont, (char const *fontFilePath, void *data, + FTGLglyph * (*makeglyphCallback) (FT_GlyphSlot, void *)), + FTCustomFont, (fontFilePath, data, makeglyphCallback), FONT_CUSTOM); + +#define C_FUN(cret, cname, cargs, cxxerr, cxxname, cxxarg) \ + cret cname cargs \ + { \ + if(!f || !f->ptr) \ + { \ + fprintf(stderr, "FTGL warning: NULL pointer in %s\n", #cname); \ + cxxerr; \ + } \ + return f->ptr->cxxname cxxarg; \ + } + +// FTFont::~FTFont(); +void ftglDestroyFont(FTGLfont *f) +{ + if(!f || !f->ptr) + { + fprintf(stderr, "FTGL warning: NULL pointer in %s\n", __FUNCTION__); + return; + } + delete f->ptr; + free(f); +} + +// bool FTFont::Attach(const char* fontFilePath); +C_FUN(int, ftglAttachFile, (FTGLfont *f, const char* path), + return 0, Attach, (path)); + +// bool FTFont::Attach(const unsigned char *pBufferBytes, +// size_t bufferSizeInBytes); +C_FUN(int, ftglAttachData, (FTGLfont *f, const unsigned char *p, size_t s), + return 0, Attach, (p, s)); + +// void FTFont::GlyphLoadFlags(FT_Int flags); +C_FUN(void, ftglSetFontGlyphLoadFlags, (FTGLfont *f, FT_Int flags), + return, GlyphLoadFlags, (flags)); + +// bool FTFont::CharMap(FT_Encoding encoding); +C_FUN(int, ftglSetFontCharMap, (FTGLfont *f, FT_Encoding enc), + return 0, CharMap, (enc)); + +// unsigned int FTFont::CharMapCount(); +C_FUN(unsigned int, ftglGetFontCharMapCount, (FTGLfont *f), + return 0, CharMapCount, ()); + +// FT_Encoding* FTFont::CharMapList(); +C_FUN(FT_Encoding *, ftglGetFontCharMapList, (FTGLfont* f), + return NULL, CharMapList, ()); + +// virtual bool FTFont::FaceSize(const unsigned int size, +// const unsigned int res = 72); +C_FUN(int, ftglSetFontFaceSize, (FTGLfont *f, unsigned int s, unsigned int r), + return 0, FaceSize, (s, r > 0 ? r : 72)); + +// unsigned int FTFont::FaceSize() const; +// XXX: need to call FaceSize() as FTFont::FaceSize() because of FTGLTexture +C_FUN(unsigned int, ftglGetFontFaceSize, (FTGLfont *f), + return 0, FTFont::FaceSize, ()); + +// virtual void FTFont::Depth(float depth); +C_FUN(void, ftglSetFontDepth, (FTGLfont *f, float d), return, Depth, (d)); + +// virtual void FTFont::Outset(float front, float back); +C_FUN(void, ftglSetFontOutset, (FTGLfont *f, float front, float back), + return, FTFont::Outset, (front, back)); + +// void FTFont::UseDisplayList(bool useList); +C_FUN(void, ftglSetFontDisplayList, (FTGLfont *f, int l), + return, UseDisplayList, (l != 0)); + +// float FTFont::Ascender() const; +C_FUN(float, ftglGetFontAscender, (FTGLfont *f), return 0.f, Ascender, ()); + +// float FTFont::Descender() const; +C_FUN(float, ftglGetFontDescender, (FTGLfont *f), return 0.f, Descender, ()); + +// float FTFont::LineHeight() const; +C_FUN(float, ftglGetFontLineHeight, (FTGLfont *f), return 0.f, LineHeight, ()); + +// void FTFont::BBox(const char* string, float& llx, float& lly, float& llz, +// float& urx, float& ury, float& urz); +extern "C++" { +C_FUN(static FTBBox, _ftglGetFontBBox, (FTGLfont *f, char const *s, int len), + return static_ftbbox, BBox, (s, len)); +} + +void ftglGetFontBBox(FTGLfont *f, const char* s, int len, float c[6]) +{ + FTBBox ret = _ftglGetFontBBox(f, s, len); + FTPoint lower = ret.Lower(), upper = ret.Upper(); + c[0] = lower.Xf(); c[1] = lower.Yf(); c[2] = lower.Zf(); + c[3] = upper.Xf(); c[4] = upper.Yf(); c[5] = upper.Zf(); +} + +// float FTFont::Advance(const char* string); +C_FUN(float, ftglGetFontAdvance, (FTGLfont *f, char const *s), + return 0.0, Advance, (s)); + +// virtual void Render(const char* string, int renderMode); +extern "C++" { +C_FUN(static FTPoint, _ftglRenderFont, (FTGLfont *f, char const *s, int len, + FTPoint pos, FTPoint spacing, int mode), + return static_ftpoint, Render, (s, len, pos, spacing, mode)); +} + +void ftglRenderFont(FTGLfont *f, const char *s, int mode) +{ + _ftglRenderFont(f, s, -1, FTPoint(), FTPoint(), mode); +} + +// FT_Error FTFont::Error() const; +C_FUN(FT_Error, ftglGetFontError, (FTGLfont *f), return -1, Error, ()); + +FTGL_END_C_DECLS + diff --git a/src/ftgl/FTFont/FTFontImpl.h b/src/ftgl/FTFont/FTFontImpl.h new file mode 100644 index 0000000..36c3bf8 --- /dev/null +++ b/src/ftgl/FTFont/FTFontImpl.h @@ -0,0 +1,162 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTFontImpl__ +#define __FTFontImpl__ + +#include "FTGL/ftgl.h" + +#include "FTFace.h" + +class FTGlyphContainer; +class FTGlyph; + +class FTFontImpl +{ + friend class FTFont; + + protected: + FTFontImpl(FTFont *ftFont, char const *fontFilePath); + + FTFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + virtual ~FTFontImpl(); + + virtual bool Attach(const char* fontFilePath); + + virtual bool Attach(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + virtual void GlyphLoadFlags(FT_Int flags); + + virtual bool CharMap(FT_Encoding encoding); + + virtual unsigned int CharMapCount() const; + + virtual FT_Encoding* CharMapList(); + + virtual void UseDisplayList(bool useList); + + virtual float Ascender() const; + + virtual float Descender() const; + + virtual float LineHeight() const; + + virtual bool FaceSize(const unsigned int size, + const unsigned int res); + + virtual unsigned int FaceSize() const; + + virtual void Depth(float depth); + + virtual void Outset(float outset); + + virtual void Outset(float front, float back); + + virtual FTBBox BBox(const char *s, const int len, FTPoint, FTPoint); + + virtual FTBBox BBox(const wchar_t *s, const int len, FTPoint, FTPoint); + + virtual float Advance(const char *s, const int len, FTPoint); + + virtual float Advance(const wchar_t *s, const int len, FTPoint); + + virtual FTPoint Render(const char *s, const int len, + FTPoint, FTPoint, int); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint, FTPoint, int); + + /** + * Current face object + */ + FTFace face; + + /** + * Current size object + */ + FTSize charSize; + + /** + * Flag to enable or disable the use of Display Lists inside FTGL + * true turns ON display lists. + * false turns OFF display lists. + */ + bool useDisplayLists; + + /** + * The default glyph loading flags. + */ + FT_Int load_flags; + + /** + * Current error code. Zero means no error. + */ + FT_Error err; + + private: + /** + * A link back to the interface of which we are the implementation. + */ + FTFont *intf; + + /** + * Check that the glyph at chr exist. If not load it. + * + * @param chr character index + * @return true if the glyph can be created. + */ + bool CheckGlyph(const unsigned int chr); + + /** + * An object that holds a list of glyphs + */ + FTGlyphContainer* glyphList; + + /** + * Current pen or cursor position; + */ + FTPoint pen; + + /* Internal generic BBox() implementation */ + template + inline FTBBox BBoxI(const T *s, const int len, + FTPoint position, FTPoint spacing); + + /* Internal generic Advance() implementation */ + template + inline float AdvanceI(const T *s, const int len, FTPoint spacing); + + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); +}; + +#endif // __FTFontImpl__ + diff --git a/src/ftgl/FTFont/FTOutlineFont.cpp b/src/ftgl/FTFont/FTOutlineFont.cpp new file mode 100644 index 0000000..9ebc9a0 --- /dev/null +++ b/src/ftgl/FTFont/FTOutlineFont.cpp @@ -0,0 +1,129 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTOutlineFontImpl.h" + + +// +// FTOutlineFont +// + + +FTOutlineFont::FTOutlineFont(char const *fontFilePath) : + FTFont(new FTOutlineFontImpl(this, fontFilePath)) +{} + + +FTOutlineFont::FTOutlineFont(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTOutlineFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTOutlineFont::~FTOutlineFont() +{} + + +FTGlyph* FTOutlineFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + FTOutlineFontImpl *myimpl = dynamic_cast(impl); + if(!myimpl) + { + return NULL; + } + + return new FTOutlineGlyph(ftGlyph, myimpl->outset, + myimpl->useDisplayLists); +} + + +// +// FTOutlineFontImpl +// + + +FTOutlineFontImpl::FTOutlineFontImpl(FTFont *ftFont, const char* fontFilePath) +: FTFontImpl(ftFont, fontFilePath), + outset(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + + +FTOutlineFontImpl::FTOutlineFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes), + outset(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + + +template +inline FTPoint FTOutlineFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + // Protect GL_TEXTURE_2D, glHint(), GL_LINE_SMOOTH and blending functions + glPushAttrib(GL_ENABLE_BIT | GL_HINT_BIT | GL_LINE_BIT + | GL_COLOR_BUFFER_BIT); + + glDisable(GL_TEXTURE_2D); + glEnable(GL_LINE_SMOOTH); + glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE + + FTPoint tmp = FTFontImpl::Render(string, len, + position, spacing, renderMode); + + glPopAttrib(); + + return tmp; +} + + +FTPoint FTOutlineFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +FTPoint FTOutlineFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + diff --git a/src/ftgl/FTFont/FTOutlineFontImpl.h b/src/ftgl/FTFont/FTOutlineFontImpl.h new file mode 100644 index 0000000..38b4d26 --- /dev/null +++ b/src/ftgl/FTFont/FTOutlineFontImpl.h @@ -0,0 +1,73 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTOutlineFontImpl__ +#define __FTOutlineFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; + +class FTOutlineFontImpl : public FTFontImpl +{ + friend class FTOutlineFont; + + protected: + FTOutlineFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTOutlineFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + /** + * Set the outset distance for the font. Only implemented by + * FTOutlineFont, FTPolygonFont and FTExtrudeFont + * + * @param outset The outset distance. + */ + virtual void Outset(float o) { outset = o; } + + virtual FTPoint Render(const char *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + private: + /** + * The outset distance for the font. + */ + float outset; + + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); +}; + +#endif // __FTOutlineFontImpl__ + diff --git a/src/ftgl/FTFont/FTPixmapFont.cpp b/src/ftgl/FTFont/FTPixmapFont.cpp new file mode 100644 index 0000000..5638ac8 --- /dev/null +++ b/src/ftgl/FTFont/FTPixmapFont.cpp @@ -0,0 +1,131 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTPixmapFontImpl.h" + + +// +// FTPixmapFont +// + + +FTPixmapFont::FTPixmapFont(char const *fontFilePath) : + FTFont(new FTPixmapFontImpl(this, fontFilePath)) +{} + + +FTPixmapFont::FTPixmapFont(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTPixmapFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTPixmapFont::~FTPixmapFont() +{} + + +FTGlyph* FTPixmapFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + return new FTPixmapGlyph(ftGlyph); +} + + +// +// FTPixmapFontImpl +// + + +FTPixmapFontImpl::FTPixmapFontImpl(FTFont *ftFont, const char* fontFilePath) +: FTFontImpl(ftFont, fontFilePath) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; +} + + +FTPixmapFontImpl::FTPixmapFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; +} + + +template +inline FTPoint FTPixmapFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + // Protect GL_TEXTURE_2D and GL_BLEND, glPixelTransferf(), and blending + // functions. + glPushAttrib(GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT); + + // Protect glPixelStorei() calls (made by FTPixmapGlyphImpl::RenderImpl). + glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glDisable(GL_TEXTURE_2D); + + GLfloat ftglColour[4]; + glGetFloatv(GL_CURRENT_RASTER_COLOR, ftglColour); + + glPixelTransferf(GL_RED_SCALE, ftglColour[0]); + glPixelTransferf(GL_GREEN_SCALE, ftglColour[1]); + glPixelTransferf(GL_BLUE_SCALE, ftglColour[2]); + glPixelTransferf(GL_ALPHA_SCALE, ftglColour[3]); + + FTPoint tmp = FTFontImpl::Render(string, len, + position, spacing, renderMode); + + glPopClientAttrib(); + glPopAttrib(); + + return tmp; +} + + +FTPoint FTPixmapFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +FTPoint FTPixmapFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + diff --git a/src/ftgl/FTFont/FTPixmapFontImpl.h b/src/ftgl/FTFont/FTPixmapFontImpl.h new file mode 100644 index 0000000..321f4a6 --- /dev/null +++ b/src/ftgl/FTFont/FTPixmapFontImpl.h @@ -0,0 +1,60 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTPixmapFontImpl__ +#define __FTPixmapFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; + +class FTPixmapFontImpl : public FTFontImpl +{ + friend class FTPixmapFont; + + protected: + FTPixmapFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTPixmapFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + virtual FTPoint Render(const char *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + private: + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); +}; + +#endif // __FTPixmapFontImpl__ + diff --git a/src/ftgl/FTFont/FTPolygonFont.cpp b/src/ftgl/FTFont/FTPolygonFont.cpp new file mode 100644 index 0000000..83c3ad1 --- /dev/null +++ b/src/ftgl/FTFont/FTPolygonFont.cpp @@ -0,0 +1,89 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" +#include "FTPolygonFontImpl.h" + + +// +// FTPolygonFont +// + + +FTPolygonFont::FTPolygonFont(char const *fontFilePath) : + FTFont(new FTPolygonFontImpl(this, fontFilePath)) +{} + + +FTPolygonFont::FTPolygonFont(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTPolygonFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTPolygonFont::~FTPolygonFont() +{} + + +FTGlyph* FTPolygonFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + FTPolygonFontImpl *myimpl = dynamic_cast(impl); + if(!myimpl) + { + return NULL; + } + + return new FTPolygonGlyph(ftGlyph, myimpl->outset, + myimpl->useDisplayLists); +} + + +// +// FTPolygonFontImpl +// + + +FTPolygonFontImpl::FTPolygonFontImpl(FTFont *ftFont, const char* fontFilePath) +: FTFontImpl(ftFont, fontFilePath), + outset(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + + +FTPolygonFontImpl::FTPolygonFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes), + outset(0.0f) +{ + load_flags = FT_LOAD_NO_HINTING; +} + diff --git a/src/ftgl/FTFont/FTPolygonFontImpl.h b/src/ftgl/FTFont/FTPolygonFontImpl.h new file mode 100644 index 0000000..47b35a6 --- /dev/null +++ b/src/ftgl/FTFont/FTPolygonFontImpl.h @@ -0,0 +1,60 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTPolygonFontImpl__ +#define __FTPolygonFontImpl__ + +#include "FTFontImpl.h" + +class FTGlyph; + +class FTPolygonFontImpl : public FTFontImpl +{ + friend class FTPolygonFont; + + protected: + FTPolygonFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTPolygonFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + /** + * Set the outset distance for the font. Only implemented by + * FTOutlineFont, FTPolygonFont and FTExtrudeFont + * + * @param depth The outset distance. + */ + virtual void Outset(float o) { outset = o; } + + private: + /** + * The outset distance (front and back) for the font. + */ + float outset; +}; + +#endif // __FTPolygonFontImpl__ + diff --git a/src/ftgl/FTFont/FTTextureFont.cpp b/src/ftgl/FTFont/FTTextureFont.cpp new file mode 100644 index 0000000..4e385ea --- /dev/null +++ b/src/ftgl/FTFont/FTTextureFont.cpp @@ -0,0 +1,267 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "config.h" + +#include +#include // For memset + +#include "FTGL/ftgl.h" + +#include "FTInternals.h" + +#include "../FTGlyph/FTTextureGlyphImpl.h" +#include "./FTTextureFontImpl.h" + + +// +// FTTextureFont +// + + +FTTextureFont::FTTextureFont(char const *fontFilePath) : + FTFont(new FTTextureFontImpl(this, fontFilePath)) +{} + + +FTTextureFont::FTTextureFont(const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) : + FTFont(new FTTextureFontImpl(this, pBufferBytes, bufferSizeInBytes)) +{} + + +FTTextureFont::~FTTextureFont() +{} + + +FTGlyph* FTTextureFont::MakeGlyph(FT_GlyphSlot ftGlyph) +{ + FTTextureFontImpl *myimpl = dynamic_cast(impl); + if(!myimpl) + { + return NULL; + } + + return myimpl->MakeGlyphImpl(ftGlyph); +} + + +// +// FTTextureFontImpl +// + + +static inline GLuint NextPowerOf2(GLuint in) +{ + in -= 1; + + in |= in >> 16; + in |= in >> 8; + in |= in >> 4; + in |= in >> 2; + in |= in >> 1; + + return in + 1; +} + + +FTTextureFontImpl::FTTextureFontImpl(FTFont *ftFont, const char* fontFilePath) +: FTFontImpl(ftFont, fontFilePath), + maximumGLTextureSize(0), + textureWidth(0), + textureHeight(0), + glyphHeight(0), + glyphWidth(0), + padding(3), + xOffset(0), + yOffset(0) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; + remGlyphs = numGlyphs = face.GlyphCount(); +} + + +FTTextureFontImpl::FTTextureFontImpl(FTFont *ftFont, + const unsigned char *pBufferBytes, + size_t bufferSizeInBytes) +: FTFontImpl(ftFont, pBufferBytes, bufferSizeInBytes), + maximumGLTextureSize(0), + textureWidth(0), + textureHeight(0), + glyphHeight(0), + glyphWidth(0), + padding(3), + xOffset(0), + yOffset(0) +{ + load_flags = FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; + remGlyphs = numGlyphs = face.GlyphCount(); +} + + +FTTextureFontImpl::~FTTextureFontImpl() +{ + if(textureIDList.size()) + { + glDeleteTextures((GLsizei)textureIDList.size(), + (const GLuint*)&textureIDList[0]); + } +} + + +FTGlyph* FTTextureFontImpl::MakeGlyphImpl(FT_GlyphSlot ftGlyph) +{ + glyphHeight = static_cast(charSize.Height() + 0.5); + glyphWidth = static_cast(charSize.Width() + 0.5); + + if(glyphHeight < 1) glyphHeight = 1; + if(glyphWidth < 1) glyphWidth = 1; + + if(textureIDList.empty()) + { + textureIDList.push_back(CreateTexture()); + xOffset = yOffset = padding; + } + + if(xOffset > (textureWidth - glyphWidth)) + { + xOffset = padding; + yOffset += glyphHeight; + + if(yOffset > (textureHeight - glyphHeight)) + { + textureIDList.push_back(CreateTexture()); + yOffset = padding; + } + } + + FTTextureGlyph* tempGlyph = new FTTextureGlyph(ftGlyph, textureIDList[textureIDList.size() - 1], + xOffset, yOffset, textureWidth, textureHeight); + xOffset += static_cast(tempGlyph->BBox().Upper().X() - tempGlyph->BBox().Lower().X() + padding + 0.5); + + --remGlyphs; + + return tempGlyph; +} + + +void FTTextureFontImpl::CalculateTextureSize() +{ + if(!maximumGLTextureSize) + { + maximumGLTextureSize = 1024; + glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize); + assert(maximumGLTextureSize); // If you hit this then you have an invalid OpenGL context. + } + + textureWidth = NextPowerOf2((remGlyphs * glyphWidth) + (padding * 2)); + textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth; + + int h = static_cast((textureWidth - (padding * 2)) / glyphWidth + 0.5); + + textureHeight = NextPowerOf2(((numGlyphs / h) + 1) * glyphHeight); + textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight; +} + + +GLuint FTTextureFontImpl::CreateTexture() +{ + CalculateTextureSize(); + + int totalMemory = textureWidth * textureHeight; + unsigned char* textureMemory = new unsigned char[totalMemory]; + memset(textureMemory, 0, totalMemory); + + GLuint textID; + glGenTextures(1, (GLuint*)&textID); + + glBindTexture(GL_TEXTURE_2D, textID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, + 0, GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory); + + delete [] textureMemory; + + return textID; +} + + +bool FTTextureFontImpl::FaceSize(const unsigned int size, const unsigned int res) +{ + if(!textureIDList.empty()) + { + glDeleteTextures((GLsizei)textureIDList.size(), (const GLuint*)&textureIDList[0]); + textureIDList.clear(); + remGlyphs = numGlyphs = face.GlyphCount(); + } + + return FTFontImpl::FaceSize(size, res); +} + + +template +inline FTPoint FTTextureFontImpl::RenderI(const T* string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + // Protect GL_TEXTURE_2D, GL_BLEND and blending functions + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE + + glEnable(GL_TEXTURE_2D); + + FTTextureGlyphImpl::ResetActiveTexture(); + + FTPoint tmp = FTFontImpl::Render(string, len, + position, spacing, renderMode); + + glPopAttrib(); + + return tmp; +} + + +FTPoint FTTextureFontImpl::Render(const char * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + + +FTPoint FTTextureFontImpl::Render(const wchar_t * string, const int len, + FTPoint position, FTPoint spacing, + int renderMode) +{ + return RenderI(string, len, position, spacing, renderMode); +} + diff --git a/src/ftgl/FTFont/FTTextureFontImpl.h b/src/ftgl/FTFont/FTTextureFontImpl.h new file mode 100644 index 0000000..c9cf22b --- /dev/null +++ b/src/ftgl/FTFont/FTTextureFontImpl.h @@ -0,0 +1,152 @@ +/* + * FTGL - OpenGL font library + * + * Copyright (c) 2001-2004 Henry Maddocks + * Copyright (c) 2008 Sam Hocevar + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef __FTTextureFontImpl__ +#define __FTTextureFontImpl__ + +#include "FTFontImpl.h" + +#include "FTVector.h" + +class FTTextureGlyph; + +class FTTextureFontImpl : public FTFontImpl +{ + friend class FTTextureFont; + + protected: + FTTextureFontImpl(FTFont *ftFont, const char* fontFilePath); + + FTTextureFontImpl(FTFont *ftFont, const unsigned char *pBufferBytes, + size_t bufferSizeInBytes); + + virtual ~FTTextureFontImpl(); + + /** + * Set the char size for the current face. + * + * @param size the face size in points (1/72 inch) + * @param res the resolution of the target device. + * @return true if size was set correctly + */ + virtual bool FaceSize(const unsigned int size, + const unsigned int res = 72); + + virtual FTPoint Render(const char *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + virtual FTPoint Render(const wchar_t *s, const int len, + FTPoint position, FTPoint spacing, + int renderMode); + + private: + /** + * Create an FTTextureGlyph object for the base class. + */ + FTGlyph* MakeGlyphImpl(FT_GlyphSlot ftGlyph); + + /** + * Get the size of a block of memory required to layout the glyphs + * + * Calculates a width and height based on the glyph sizes and the + * number of glyphs. It over estimates. + */ + inline void CalculateTextureSize(); + + /** + * Creates a 'blank' OpenGL texture object. + * + * The format is GL_ALPHA and the params are + * GL_TEXTURE_WRAP_S = GL_CLAMP + * GL_TEXTURE_WRAP_T = GL_CLAMP + * GL_TEXTURE_MAG_FILTER = GL_LINEAR + * GL_TEXTURE_MIN_FILTER = GL_LINEAR + * Note that mipmapping is NOT used + */ + inline GLuint CreateTexture(); + + /** + * The maximum texture dimension on this OpenGL implemetation + */ + GLsizei maximumGLTextureSize; + + /** + * The minimum texture width required to hold the glyphs + */ + GLsizei textureWidth; + + /** + * The minimum texture height required to hold the glyphs + */ + GLsizei textureHeight; + + /** + *An array of texture ids + */ + FTVector textureIDList; + + /** + * The max height for glyphs in the current font + */ + int glyphHeight; + + /** + * The max width for glyphs in the current font + */ + int glyphWidth; + + /** + * A value to be added to the height and width to ensure that + * glyphs don't overlap in the texture + */ + unsigned int padding; + + /** + * + */ + unsigned int numGlyphs; + + /** + */ + unsigned int remGlyphs; + + /** + */ + int xOffset; + + /** + */ + int yOffset; + + /* Internal generic Render() implementation */ + template + inline FTPoint RenderI(const T *s, const int len, + FTPoint position, FTPoint spacing, int mode); +}; + +#endif // __FTTextureFontImpl__ + -- cgit v1.2.3