[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gnash-commit] gnash ChangeLog server/Makefile.am server/font....
From: |
Sandro Santilli |
Subject: |
[Gnash-commit] gnash ChangeLog server/Makefile.am server/font.... |
Date: |
Wed, 13 Jun 2007 15:17:31 +0000 |
CVSROOT: /sources/gnash
Module name: gnash
Changes by: Sandro Santilli <strk> 07/06/13 15:17:30
Modified files:
. : ChangeLog
server : Makefile.am font.cpp font.h
Added files:
server : FreetypeGlyphsProvider.cpp
FreetypeGlyphsProvider.h
Removed files:
server : FreetypeRasterizer.cpp FreetypeRasterizer.h
Log message:
* server/: font.{cpp,h}, Makefile.am,
FreetypeRasterizer.{cpp,h},
FreetypeGlyphsProvider.{cpp,h}: renamed file and class since
we won't serve rendered glyphs anymore. Rendered glyphs can
always be obtained by rendering the shape_character_def,
the code should be buried somewhere in fontlib.cpp.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.3525&r2=1.3526
http://cvs.savannah.gnu.org/viewcvs/gnash/server/Makefile.am?cvsroot=gnash&r1=1.117&r2=1.118
http://cvs.savannah.gnu.org/viewcvs/gnash/server/font.cpp?cvsroot=gnash&r1=1.37&r2=1.38
http://cvs.savannah.gnu.org/viewcvs/gnash/server/font.h?cvsroot=gnash&r1=1.22&r2=1.23
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeGlyphsProvider.cpp?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeGlyphsProvider.h?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeRasterizer.cpp?cvsroot=gnash&r1=1.10&r2=0
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeRasterizer.h?cvsroot=gnash&r1=1.6&r2=0
Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.3525
retrieving revision 1.3526
diff -u -b -r1.3525 -r1.3526
--- ChangeLog 13 Jun 2007 13:38:28 -0000 1.3525
+++ ChangeLog 13 Jun 2007 15:17:29 -0000 1.3526
@@ -1,5 +1,13 @@
2007-06-13 Sandro Santilli <address@hidden>
+ * server/: font.{cpp,h}, Makefile.am, FreetypeRasterizer.{cpp,h},
+ FreetypeGlyphsProvider.{cpp,h}: renamed file and class since
+ we won't serve rendered glyphs anymore. Rendered glyphs can
+ always be obtained by rendering the shape_character_def,
+ the code should be buried somewhere in fontlib.cpp.
+
+2007-06-13 Sandro Santilli <address@hidden>
+
* server/font.cpp (add_os_font): use the advance value
computed by getGlyph, not getRenderedGlyph (the latter
is bogus).
Index: server/Makefile.am
===================================================================
RCS file: /sources/gnash/gnash/server/Makefile.am,v
retrieving revision 1.117
retrieving revision 1.118
diff -u -b -r1.117 -r1.118
--- server/Makefile.am 13 Jun 2007 09:59:42 -0000 1.117
+++ server/Makefile.am 13 Jun 2007 15:17:30 -0000 1.118
@@ -18,7 +18,7 @@
#
#
-# $Id: Makefile.am,v 1.117 2007/06/13 09:59:42 strk Exp $
+# $Id: Makefile.am,v 1.118 2007/06/13 15:17:30 strk Exp $
AUTOMAKE_OPTIONS =
@@ -29,7 +29,7 @@
# noinst_SCRIPTS = gen-files.sh
# TODO: use a conditional to optionally link ?
-FREETYPE_SOURCES = FreetypeRasterizer.h FreetypeRasterizer.cpp
+FREETYPE_SOURCES = FreetypeGlyphsProvider.h FreetypeGlyphsProvider.cpp
# Only enable if we're configured with --enable-mp3
AM_CPPFLAGS = \
Index: server/font.cpp
===================================================================
RCS file: /sources/gnash/gnash/server/font.cpp,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- server/font.cpp 13 Jun 2007 13:38:29 -0000 1.37
+++ server/font.cpp 13 Jun 2007 15:17:30 -0000 1.38
@@ -17,7 +17,7 @@
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
-/* $Id: font.cpp,v 1.37 2007/06/13 13:38:29 strk Exp $ */
+/* $Id: font.cpp,v 1.38 2007/06/13 15:17:30 strk Exp $ */
// Based on the public domain work of Thatcher Ulrich <address@hidden> 2003
@@ -131,7 +131,7 @@
readDefineFont2_or_3(in, m);
}
- if ( m_name && ! initDeviceFontRasterizer() )
+ if ( m_name && ! initDeviceFontProvider() )
{
log_error("Could not initialize device font face '%s'",
m_name);
}
@@ -453,7 +453,7 @@
}
// Try adding an os font, of possible
- if ( _ftRasterizer.get() )
+ if ( _ftProvider.get() )
{
glyph_index =
const_cast<font*>(this)->add_os_glyph(code);
}
@@ -563,60 +563,36 @@
int
font::add_os_glyph(uint16_t code)
{
- assert ( _ftRasterizer.get() );
+ assert ( _ftProvider.get() );
assert(m_code_table.find(code) == m_code_table.end());
- float advance1, advance2;
+ float advance;
// Get the vectorial glyph
- boost::intrusive_ptr<shape_character_def> sh =
_ftRasterizer->getGlyph(code, advance1);
+ boost::intrusive_ptr<shape_character_def> sh =
_ftProvider->getGlyph(code, advance);
- // Get the textured glyph and the advance info
- rect box;
- boost::intrusive_ptr<bitmap_info> bi (
_ftRasterizer->getRenderedGlyph(code, box, advance2) );
-
- // Advance must be given relative to the EM, is that correct or
should we change ?
- float advance = advance1; // vect
- //float advance = advance2; // rast
-
- if ( ! sh && ! bi )
+ if ( ! sh )
{
- log_error("Could not create either bitmap or shape "
+ log_error("Could not create shape "
"glyph for character code %u (%c) with "
"device font %s (%p)", code, code,
m_name,
- _ftRasterizer.get());
+ _ftProvider.get());
return -1;
}
- // Create textured glyph from the bitmap info
- texture_glyph tg;
-
- if ( bi.get() )
- {
-
- if ( ! box.is_null() )
- {
- tg.m_uv_bounds.enclose_point(0, 0);
- tg.m_uv_bounds.expand_to_point(box.get_x_max(),
box.get_y_max());
- // the origin
- tg.m_uv_origin.m_x = box.get_x_min();
- tg.m_uv_origin.m_y = box.get_y_min();
- }
- else
- {
- tg.m_uv_bounds.set_null();
- tg.m_uv_origin.m_x = 0;
- tg.m_uv_origin.m_y = 0;
- }
- tg.set_bitmap_info(bi.get());
- }
-
- // Add the textured glyph
+ // Find new glyph offset
int newOffset = m_texture_glyphs.size();
+
+ // Add the new glyph id
m_code_table[code] = newOffset;
+
+ // Add advance info
m_advance_table.push_back(advance);
- m_texture_glyphs.push_back(tg);
+ // Add dummy textured glyph
+ m_texture_glyphs.push_back(texture_glyph());
+
+ // Add vector glyph
m_glyphs.push_back(sh);
testInvariant();
@@ -625,7 +601,7 @@
}
bool
- font::initDeviceFontRasterizer()
+ font::initDeviceFontProvider()
{
if ( ! m_name )
{
@@ -633,8 +609,8 @@
return false;
}
- _ftRasterizer = FreetypeRasterizer::createFace(m_name,
m_is_bold, m_is_italic);
- if ( ! _ftRasterizer.get() )
+ _ftProvider = FreetypeGlyphsProvider::createFace(m_name,
m_is_bold, m_is_italic);
+ if ( ! _ftProvider.get() )
{
log_error("Could not create a freetype face %s",
m_name);
return false;
Index: server/font.h
===================================================================
RCS file: /sources/gnash/gnash/server/font.h,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -b -r1.22 -r1.23
--- server/font.h 13 Jun 2007 10:21:36 -0000 1.22
+++ server/font.h 13 Jun 2007 15:17:30 -0000 1.23
@@ -17,7 +17,7 @@
#include "ref_counted.h" // for inheritance of texture_glyph
#include "swf.h" // for tag_type definition
#include "bitmap_info.h" // for dtor visibility by smart pointer
-#include "FreetypeRasterizer.h" // for device fonts support
+#include "FreetypeGlyphsProvider.h" // for device fonts support
#include "log.h"
#include <map>
@@ -241,7 +241,7 @@
/// Initialize the freetype rasterizer
//
/// Return true on success, false on error
- bool initDeviceFontRasterizer();
+ bool initDeviceFontProvider();
std::vector< boost::intrusive_ptr<shape_character_def> >
m_glyphs;
@@ -273,7 +273,7 @@
typedef std::map<kerning_pair, float> kernings_table;
kernings_table m_kerning_pairs;
- std::auto_ptr<FreetypeRasterizer> _ftRasterizer;
+ std::auto_ptr<FreetypeGlyphsProvider> _ftProvider;
};
Index: server/FreetypeGlyphsProvider.cpp
===================================================================
RCS file: server/FreetypeGlyphsProvider.cpp
diff -N server/FreetypeGlyphsProvider.cpp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ server/FreetypeGlyphsProvider.cpp 13 Jun 2007 15:17:30 -0000 1.1
@@ -0,0 +1,378 @@
+// FreetypeGlyphsProvider.cpp: Freetype glyphs manager
+//
+// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "FreetypeGlyphsProvider.h"
+#include "smart_ptr.h" // for intrusive_ptr
+#include "image.h" // for create_alpha
+#include "GnashException.h"
+#include "render.h"
+#include "DynamicShape.h"
+#include "log.h"
+
+#ifdef HAVE_FREETYPE2
+# include <ft2build.h>
+# include FT_OUTLINE_H
+# include FT_BBOX_H
+#endif
+
+#ifdef HAVE_FONTCONFIG_FONTCONFIG_H
+# define HAVE_FONTCONFIG 1
+#endif
+
+#ifdef HAVE_FONTCONFIG
+# include <fontconfig/fontconfig.h>
+# include <fontconfig/fcfreetype.h>
+#endif
+
+#include <cstdio> // for snprintf
+#include <string>
+#include <memory> // for auto_ptr
+
+// Define the following to make outline decomposition verbose
+//#define DEBUG_OUTLINE_DECOMPOSITION 1
+
+// Define the following to make glyph rendering verbose
+//#define DEBUG_GLYPH_RENDERING 1
+
+// TODO: drop this ?
+#define FREETYPE_MAX_FONTSIZE 96
+
+namespace gnash {
+
+#ifdef HAVE_FREETYPE2
+
+static int
+walkMoveTo(FT_Vector* to, void* ptr)
+{
+ DynamicShape* sh = static_cast<DynamicShape*>(ptr);
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_debug("moveTo: %ld,%ld", to->x, to->y);
+#endif
+ sh->moveTo(to->x, -to->y);
+ return 0;
+}
+
+static int
+walkLineTo(FT_Vector* to, void* ptr)
+{
+ DynamicShape* sh = static_cast<DynamicShape*>(ptr);
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_debug("lineTo: %ld,%ld", to->x, to->y);
+#endif
+ sh->lineTo(to->x, -to->y);
+ return 0;
+}
+
+static int
+walkConicTo(FT_Vector* ctrl, FT_Vector* to, void* ptr)
+{
+ DynamicShape* sh = static_cast<DynamicShape*>(ptr);
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_debug("conicTo: %ld,%ld %ld,%ld", ctrl->x, ctrl->y, to->x, to->y);
+#endif
+ sh->curveTo(ctrl->x, -ctrl->y, to->x, -to->y);
+ return 0;
+}
+
+static int
+walkCubicTo(FT_Vector* ctrl1, FT_Vector* ctrl2, FT_Vector* to, void* ptr)
+{
+ DynamicShape* sh = static_cast<DynamicShape*>(ptr);
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_debug("cubicTo: %ld,%ld %ld,%ld %ld,%ld", ctrl1->x, ctrl1->y,
ctrl2->x, ctrl2->y, to->x, to->y);
+#endif
+
+ float x = ctrl1->x + ( (ctrl2->x - ctrl1->x) * 0.5 );
+ float y = ctrl1->y + ( (ctrl2->y - ctrl1->y) * 0.5 );
+
+ sh->curveTo(x, -y, to->x, -to->y);
+
+ return 0;
+}
+
+// static
+FT_Library FreetypeGlyphsProvider::m_lib;
+
+// static private
+void FreetypeGlyphsProvider::init()
+{
+ int error = FT_Init_FreeType(&m_lib);
+ if (error)
+ {
+ fprintf(stderr, "can't init FreeType! error = %d\n", error);
+ exit(1);
+ }
+}
+
+// static private
+void FreetypeGlyphsProvider::close()
+{
+ int error = FT_Done_FreeType(m_lib);
+ if (error)
+ {
+ fprintf(stderr, "can't close FreeType! error = %d\n", error);
+ }
+}
+
+// private
+std::auto_ptr<image::alpha>
+FreetypeGlyphsProvider::draw_bitmap(const FT_Bitmap& bitmap)
+{
+ // You must use power-of-two dimensions!!
+ int w = 1; while (w < bitmap.pitch) { w <<= 1; }
+ int h = 1; while (h < bitmap.rows) { h <<= 1; }
+
+ std::auto_ptr<image::alpha> alpha ( image::create_alpha(w, h) );
+
+ memset(alpha->m_data, 0, alpha->m_width * alpha->m_height);
+
+ // copy image to alpha
+ for (int i = 0; i < bitmap.rows; i++)
+ {
+ uint8_t* src = bitmap.buffer + bitmap.pitch * i;
+ uint8_t* dst = alpha->m_data + alpha->m_pitch * i;
+ int x = bitmap.width;
+ while (x-- > 0)
+ {
+ *dst++ = *src++;
+ }
+ }
+
+ return alpha;
+}
+
+#if 0
+// private
+float
+FreetypeGlyphsProvider::get_advance_x(uint16_t code)
+{
+ FT_Set_Pixel_Sizes(m_face, 0, FREETYPE_MAX_FONTSIZE);
+ if (FT_Load_Char(m_face, code, FT_LOAD_RENDER))
+ {
+ return 0;
+ }
+ return (float) m_face->glyph->metrics.horiAdvance * s_advance_scale;
+}
+#endif
+
+// private
+bool
+FreetypeGlyphsProvider::getFontFilename(const std::string& name,
+ bool /*bold*/, bool /*italic*/, std::string& filename)
+{
+
+#define DEFAULT_FONTFILE "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
+
+#ifdef HAVE_FONTCONFIG
+
+ if (!FcInit ())
+ {
+
+ log_error("Can't init fontconfig library, using hard-coded font
filename");
+ filename = DEFAULT_FONTFILE;
+ return true;
+ //return false;
+ }
+
+ FcResult result;
+
+ FcPattern* pat = FcNameParse((const FcChar8*)name.c_str());
+
+ FcConfigSubstitute (0, pat, FcMatchPattern);
+ FcDefaultSubstitute (pat);
+
+ FcPattern *match;
+ match = FcFontMatch (0, pat, &result);
+ FcPatternDestroy (pat);
+
+ FcFontSet* fs = NULL;
+ if (match)
+ {
+ fs = FcFontSetCreate ();
+ FcFontSetAdd (fs, match);
+ }
+
+ if ( fs )
+ {
+ log_debug("Found %d fonts matching the family %s (using
first)", fs->nfont, name.c_str());
+
+ for (int j = 0; j < fs->nfont; j++)
+ {
+ FcChar8 *file;
+ if (FcPatternGetString (fs->fonts[j], FC_FILE, 0,
&file) != FcResultMatch)
+ {
+ log_debug("Matching font %d has unknown filename, skipping", j);
+ continue;
+ }
+
+ filename = (char *)file;
+ return true;
+
+ }
+
+ FcFontSetDestroy(fs);
+ }
+
+ log_error("No device font matches the name '%s', using hard-coded font
filename", name.c_str());
+ filename = DEFAULT_FONTFILE;
+ return true;
+#else
+ log_error("Font filename matching not implemented (no fontconfig
support built-in), using hard-coded font filename",
+ name.c_str());
+ filename = DEFAULT_FONTFILE;
+ return true;
+#endif
+}
+
+#endif // HAVE_FREETYPE2
+
+#ifdef HAVE_FREETYPE2
+// static
+std::auto_ptr<FreetypeGlyphsProvider>
+FreetypeGlyphsProvider::createFace(const std::string& name, bool bold, bool
italic)
+{
+
+ std::auto_ptr<FreetypeGlyphsProvider> ret;
+
+ try {
+ ret.reset( new FreetypeGlyphsProvider(name, bold, italic) );
+ } catch (GnashException& ge) {
+ log_error(ge.what());
+ assert(! ret.get());
+ }
+
+ return ret;
+
+}
+#else // ndef HAVE_FREETYPE2
+std::auto_ptr<FreetypeGlyphsProvider>
+FreetypeGlyphsProvider::createFace(const std::string&, bool, bool)
+{
+ log_error("Freetype not supported");
+ return std::auto_ptr<FreetypeGlyphsProvider>(NULL);
+}
+#endif // ndef HAVE_FREETYPE2
+
+#ifdef HAVE_FREETYPE2
+FreetypeGlyphsProvider::FreetypeGlyphsProvider(const std::string& name, bool
bold, bool italic)
+ :
+ m_face(NULL)
+{
+ const unsigned maxerrlen = 64;
+ char buf[maxerrlen];
+
+ if (m_lib == NULL)
+ {
+ init();
+ }
+
+ std::string filename;
+ if (getFontFilename(name, bold, italic, filename) == false)
+ {
+ snprintf(buf, maxerrlen, _("Can't find font file for font
'%s'"), name.c_str());
+ buf[maxerrlen-1] = '\0';
+ throw GnashException(buf);
+ }
+
+ int error = FT_New_Face(m_lib, filename.c_str(), 0, &m_face);
+ switch (error)
+ {
+ case 0:
+ break;
+
+ case FT_Err_Unknown_File_Format:
+ snprintf(buf, maxerrlen, _("Font file '%s' has bad
format"), filename.c_str());
+ buf[maxerrlen-1] = '\0';
+ throw GnashException(buf);
+ break;
+
+ default:
+ // TODO: return a better error message !
+ snprintf(buf, maxerrlen, _("Some error opening font
'%s'"), filename.c_str());
+ buf[maxerrlen-1] = '\0';
+ throw GnashException(buf);
+ break;
+ }
+}
+#else // ndef(HAVE_FREETYPE2)
+FreetypeGlyphsProvider::FreetypeGlyphsProvider(const std::string&, bool, bool)
+{
+ assert(0); // should never be called
+}
+#endif // ndef HAVE_FREETYPE2
+
+#ifdef HAVE_FREETYPE2
+boost::intrusive_ptr<shape_character_def>
+FreetypeGlyphsProvider::getGlyph(uint16_t code, float& advance)
+{
+ boost::intrusive_ptr<DynamicShape> sh;
+
+ FT_Error error = FT_Load_Char(m_face, code,
FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
+ if ( error != 0 )
+ {
+ log_error("Error loading freetype outline glyph for char '%c'
(error: %d)", code, error);
+ return sh.get();
+ }
+
+ // TODO: check this. Also check FT_FaceRec::units_per_EM
+ advance = m_face->glyph->metrics.horiAdvance;
+
+ assert(m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE);
+
+ FT_Outline* outline = &(m_face->glyph->outline);
+
+ //FT_BBox glyphBox;
+ //FT_Outline_Get_BBox(outline, &glyphBox);
+ //rect r(glyphBox.xMin, glyphBox.yMin, glyphBox.xMax, glyphBox.yMax);
+ //log_msg("Glyph for character '%c' has computed bounds %s", code,
r.toString().c_str());
+
+ sh = new DynamicShape();
+ sh->beginFill(rgba(255, 255, 255, 255));
+
+ FT_Outline_Funcs walk;
+ walk.move_to = walkMoveTo;
+ walk.line_to = walkLineTo;
+ walk.conic_to = walkConicTo;
+ walk.cubic_to = walkCubicTo;
+ walk.shift = 0; // ?
+ walk.delta = 0; // ?
+
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_debug("Decomposing glyph outline for character %u", code);
+#endif
+ FT_Outline_Decompose(outline, &walk, sh.get());
+#ifdef DEBUG_OUTLINE_DECOMPOSITION
+ log_msg("Decomposed glyph for character '%c' has bounds %s", code,
sh->get_bound().toString().c_str());
+#endif
+
+ return sh.get();
+}
+#else // ndef(HAVE_FREETYPE2)
+boost::intrusive_ptr<shape_character_def>
+FreetypeGlyphsProvider::getGlyph(uint16_t, float& advance)
+{
+ assert(0); // should never be called...
+}
+#endif // ndef(HAVE_FREETYPE2)
+
+} // namespace gnash
+
Index: server/FreetypeGlyphsProvider.h
===================================================================
RCS file: server/FreetypeGlyphsProvider.h
diff -N server/FreetypeGlyphsProvider.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ server/FreetypeGlyphsProvider.h 13 Jun 2007 15:17:30 -0000 1.1
@@ -0,0 +1,162 @@
+// FreetypeGlyphsProvider.h: Freetype glyphs manager
+//
+// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+#ifndef GNASH_FREETYPE_H
+#define GNASH_FREETYPE_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+
+#ifdef HAVE_FREETYPE_FREETYPE_H
+# define HAVE_FREETYPE2 1
+#endif
+
+#include "rect.h"
+#include "smart_ptr.h" // for intrusive_ptr
+
+#include <string>
+#include <memory> // for auto_ptr
+
+#ifdef HAVE_FREETYPE2
+# include <ft2build.h>
+# include FT_FREETYPE_H
+# include FT_GLYPH_H
+#endif
+
+// Forward declarations
+namespace gnash {
+ class bitmap_info;
+ class shape_character_def;
+}
+namespace image {
+ class alpha;
+}
+
+
+namespace gnash {
+
+/// Truetype font rasterizer/converter based on freetype library
+//
+/// Instances of this class provide rasterized or vectorial glyphs
+/// for a given truetype font face.
+///
+/// The rasterized glyphs have a max size of 96 (TODO: make parametrizable)
+/// but I think the actual size could change between glyphs (see the 'box'
+/// parameter of getRenderedGlyph() method).
+///
+/// Vectorial glyphs are instances of a shape_character_def, same class
+/// resulting from parsing of embedded fonts.
+///
+/// TODO: rename this class to something like FreetypeGlyphProvider...
+///
+class FreetypeGlyphsProvider
+{
+
+public:
+
+ /// Named constructor for a face-bound rasterizer.
+ //
+ /// @param name
+ /// Name of the font to get glyphs info from
+ ///
+ /// @param bold
+ /// Whether to use a bold version of the font
+ ///
+ /// @param italic
+ /// Whether to use an italic version of the font
+ ///
+ /// @return a rasterizer bound to the given font name,
+ /// or a NULL auto_ptr if the given truetype font
+ /// could not be found.
+ ///
+ static std::auto_ptr<FreetypeGlyphsProvider> createFace(const
std::string& name, bool bold, bool italic);
+
+ /// Return the given character glyph as a shape character definition
+ //
+ /// @param code
+ /// Character code.
+ ///
+ /// @param advance
+ /// Output parameter... units to advance horizontally from this
glyph to the next,
+ /// in EM units.
+ ///
+ /// @return A shape_character_def, or a NULL pointer if the given
character code
+ /// doesn't exist in this font.
+ ///
+ boost::intrusive_ptr<shape_character_def> getGlyph(uint16_t code,
float& advance);
+
+
+private:
+
+ /// Use the named constructor to create an instance
+ //
+ /// throw a GnashException on error (unkonwn font name or similar).
+ ///
+ FreetypeGlyphsProvider(const std::string& fontname, bool bold, bool
italic);
+
+#ifdef HAVE_FREETYPE2
+
+ // TODO: drop ?
+ //float get_advance_x(uint16_t code);
+
+ /// Get filename containing given font
+ //
+ /// @param name
+ /// Font name
+ ///
+ /// @param bold
+ /// Want bold version
+ ///
+ /// @param italic
+ /// Want italic version
+ ///
+ /// @param filename
+ /// Where to return the filename to
+ ///
+ /// @return true if the font was found, false otherwise.
+ /// Actually, this function should return a default
+ /// filename in any case, so false should only be
+ /// returned if not even a default font was found.
+ ///
+ bool getFontFilename(const std::string& name, bool bold, bool italic,
+ std::string& filename);
+
+ static void init();
+
+ static void close();
+
+ /// Used by getRenderedGlyph to get the glyph bitmap.
+ //
+ /// NOTE: calls the currently registered renderer (create_alpha)
+ ///
+ std::auto_ptr<image::alpha> draw_bitmap(const FT_Bitmap& bitmap);
+
+
+ static FT_Library m_lib;
+ FT_Face m_face;
+
+#endif // HAVE_FREETYPE2
+
+};
+
+} // namespace gnash
+
+
+#endif // GNASH_FREETYPE_H
Index: server/FreetypeRasterizer.cpp
===================================================================
RCS file: server/FreetypeRasterizer.cpp
diff -N server/FreetypeRasterizer.cpp
--- server/FreetypeRasterizer.cpp 13 Jun 2007 09:59:42 -0000 1.10
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,446 +0,0 @@
-// FreetypeRasterizer.cpp: Freetype glyphs manager
-//
-// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include "FreetypeRasterizer.h"
-#include "smart_ptr.h" // for intrusive_ptr
-#include "image.h" // for create_alpha
-#include "GnashException.h"
-#include "render.h"
-#include "DynamicShape.h"
-#include "log.h"
-
-#ifdef HAVE_FREETYPE2
-# include <ft2build.h>
-# include FT_OUTLINE_H
-# include FT_BBOX_H
-#endif
-
-#ifdef HAVE_FONTCONFIG_FONTCONFIG_H
-# define HAVE_FONTCONFIG 1
-#endif
-
-#ifdef HAVE_FONTCONFIG
-# include <fontconfig/fontconfig.h>
-# include <fontconfig/fcfreetype.h>
-#endif
-
-#include <cstdio> // for snprintf
-#include <string>
-#include <memory> // for auto_ptr
-
-// Define the following to make outline decomposition verbose
-//#define DEBUG_OUTLINE_DECOMPOSITION 1
-
-// Define the following to make glyph rendering verbose
-//#define DEBUG_GLYPH_RENDERING 1
-
-// TODO: drop this ?
-#define FREETYPE_MAX_FONTSIZE 96
-
-namespace gnash {
-
-#ifdef HAVE_FREETYPE2
-
-static int
-walkMoveTo(FT_Vector* to, void* ptr)
-{
- DynamicShape* sh = static_cast<DynamicShape*>(ptr);
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_debug("moveTo: %ld,%ld", to->x, to->y);
-#endif
- sh->moveTo(to->x, -to->y);
- return 0;
-}
-
-static int
-walkLineTo(FT_Vector* to, void* ptr)
-{
- DynamicShape* sh = static_cast<DynamicShape*>(ptr);
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_debug("lineTo: %ld,%ld", to->x, to->y);
-#endif
- sh->lineTo(to->x, -to->y);
- return 0;
-}
-
-static int
-walkConicTo(FT_Vector* ctrl, FT_Vector* to, void* ptr)
-{
- DynamicShape* sh = static_cast<DynamicShape*>(ptr);
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_debug("conicTo: %ld,%ld %ld,%ld", ctrl->x, ctrl->y, to->x, to->y);
-#endif
- sh->curveTo(ctrl->x, -ctrl->y, to->x, -to->y);
- return 0;
-}
-
-static int
-walkCubicTo(FT_Vector* ctrl1, FT_Vector* ctrl2, FT_Vector* to, void* ptr)
-{
- DynamicShape* sh = static_cast<DynamicShape*>(ptr);
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_debug("cubicTo: %ld,%ld %ld,%ld %ld,%ld", ctrl1->x, ctrl1->y,
ctrl2->x, ctrl2->y, to->x, to->y);
-#endif
-
- float x = ctrl1->x + ( (ctrl2->x - ctrl1->x) * 0.5 );
- float y = ctrl1->y + ( (ctrl2->y - ctrl1->y) * 0.5 );
-
- sh->curveTo(x, -y, to->x, -to->y);
-
- return 0;
-}
-
-// static
-FT_Library FreetypeRasterizer::m_lib;
-
-// static private
-void FreetypeRasterizer::init()
-{
- int error = FT_Init_FreeType(&m_lib);
- if (error)
- {
- fprintf(stderr, "can't init FreeType! error = %d\n", error);
- exit(1);
- }
-}
-
-// static private
-void FreetypeRasterizer::close()
-{
- int error = FT_Done_FreeType(m_lib);
- if (error)
- {
- fprintf(stderr, "can't close FreeType! error = %d\n", error);
- }
-}
-
-// private
-std::auto_ptr<image::alpha>
-FreetypeRasterizer::draw_bitmap(const FT_Bitmap& bitmap)
-{
- // You must use power-of-two dimensions!!
- int w = 1; while (w < bitmap.pitch) { w <<= 1; }
- int h = 1; while (h < bitmap.rows) { h <<= 1; }
-
- std::auto_ptr<image::alpha> alpha ( image::create_alpha(w, h) );
-
- memset(alpha->m_data, 0, alpha->m_width * alpha->m_height);
-
- // copy image to alpha
- for (int i = 0; i < bitmap.rows; i++)
- {
- uint8_t* src = bitmap.buffer + bitmap.pitch * i;
- uint8_t* dst = alpha->m_data + alpha->m_pitch * i;
- int x = bitmap.width;
- while (x-- > 0)
- {
- *dst++ = *src++;
- }
- }
-
- return alpha;
-}
-
-#if 0
-// private
-float
-FreetypeRasterizer::get_advance_x(uint16_t code)
-{
- FT_Set_Pixel_Sizes(m_face, 0, FREETYPE_MAX_FONTSIZE);
- if (FT_Load_Char(m_face, code, FT_LOAD_RENDER))
- {
- return 0;
- }
- return (float) m_face->glyph->metrics.horiAdvance * s_advance_scale;
-}
-#endif
-
-// private
-bool
-FreetypeRasterizer::getFontFilename(const std::string& name,
- bool /*bold*/, bool /*italic*/, std::string& filename)
-{
-
-#define DEFAULT_FONTFILE "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
-
-#ifdef HAVE_FONTCONFIG
-
- if (!FcInit ())
- {
-
- log_error("Can't init fontconfig library, using hard-coded font
filename");
- filename = DEFAULT_FONTFILE;
- return true;
- //return false;
- }
-
- FcResult result;
-
- FcPattern* pat = FcNameParse((const FcChar8*)name.c_str());
-
- FcConfigSubstitute (0, pat, FcMatchPattern);
- FcDefaultSubstitute (pat);
-
- FcPattern *match;
- match = FcFontMatch (0, pat, &result);
- FcPatternDestroy (pat);
-
- FcFontSet* fs = NULL;
- if (match)
- {
- fs = FcFontSetCreate ();
- FcFontSetAdd (fs, match);
- }
-
- if ( fs )
- {
- log_debug("Found %d fonts matching the family %s (using
first)", fs->nfont, name.c_str());
-
- for (int j = 0; j < fs->nfont; j++)
- {
- FcChar8 *file;
- if (FcPatternGetString (fs->fonts[j], FC_FILE, 0,
&file) != FcResultMatch)
- {
- log_debug("Matching font %d has unknown filename, skipping", j);
- continue;
- }
-
- filename = (char *)file;
- return true;
-
- }
-
- FcFontSetDestroy(fs);
- }
-
- log_error("No device font matches the name '%s', using hard-coded font
filename", name.c_str());
- filename = DEFAULT_FONTFILE;
- return true;
-#else
- log_error("Font filename matching not implemented (no fontconfig
support built-in), using hard-coded font filename",
- name.c_str());
- filename = DEFAULT_FONTFILE;
- return true;
-#endif
-}
-
-#endif // HAVE_FREETYPE2
-
-#ifdef HAVE_FREETYPE2
-// static
-std::auto_ptr<FreetypeRasterizer>
-FreetypeRasterizer::createFace(const std::string& name, bool bold, bool italic)
-{
-
- std::auto_ptr<FreetypeRasterizer> ret;
-
- try {
- ret.reset( new FreetypeRasterizer(name, bold, italic) );
- } catch (GnashException& ge) {
- log_error(ge.what());
- assert(! ret.get());
- }
-
- return ret;
-
-}
-#else // ndef HAVE_FREETYPE2
-std::auto_ptr<FreetypeRasterizer>
-FreetypeRasterizer::createFace(const std::string&, bool, bool)
-{
- log_error("Freetype not supported");
- return std::auto_ptr<FreetypeRasterizer>(NULL);
-}
-#endif // ndef HAVE_FREETYPE2
-
-#ifdef HAVE_FREETYPE2
-FreetypeRasterizer::FreetypeRasterizer(const std::string& name, bool bold,
bool italic)
- :
- m_face(NULL)
-{
- const unsigned maxerrlen = 64;
- char buf[maxerrlen];
-
- if (m_lib == NULL)
- {
- init();
- }
-
- std::string filename;
- if (getFontFilename(name, bold, italic, filename) == false)
- {
- snprintf(buf, maxerrlen, _("Can't find font file for font
'%s'"), name.c_str());
- buf[maxerrlen-1] = '\0';
- throw GnashException(buf);
- }
-
- int error = FT_New_Face(m_lib, filename.c_str(), 0, &m_face);
- switch (error)
- {
- case 0:
- break;
-
- case FT_Err_Unknown_File_Format:
- snprintf(buf, maxerrlen, _("Font file '%s' has bad
format"), filename.c_str());
- buf[maxerrlen-1] = '\0';
- throw GnashException(buf);
- break;
-
- default:
- // TODO: return a better error message !
- snprintf(buf, maxerrlen, _("Some error opening font
'%s'"), filename.c_str());
- buf[maxerrlen-1] = '\0';
- throw GnashException(buf);
- break;
- }
-}
-#else // ndef(HAVE_FREETYPE2)
-FreetypeRasterizer::FreetypeRasterizer(const std::string&, bool, bool)
-{
- assert(0); // should never be called
-}
-#endif // ndef HAVE_FREETYPE2
-
-#ifdef HAVE_FREETYPE2
-boost::intrusive_ptr<bitmap_info>
-FreetypeRasterizer::getRenderedGlyph(uint16_t code, rect& box, float& advance)
-{
- boost::intrusive_ptr<bitmap_info> bi;
-
- FT_Set_Pixel_Sizes(m_face, 0, FREETYPE_MAX_FONTSIZE);
- FT_Error error = FT_Load_Char(m_face, code, FT_LOAD_RENDER);
- if ( error != 0 )
- {
- log_error("Error loading freetype render glyph for char '%c'
(error: %d)", code, error);
- return bi;
- }
-
- FT_GlyphSlot glyph = m_face->glyph;
- FT_Bitmap bitmap = glyph->bitmap;
- FT_Glyph_Metrics metrics = glyph->metrics;
-
- std::auto_ptr<image::alpha> im ( draw_bitmap(bitmap) );
-
-#ifdef DEBUG_GLYPH_RENDERING
- log_debug("image::alpha drawn for character glyph '%c' bitmap has size
%dx%d", code, im->m_width, im->m_height);
- log_debug("ttf bitmap glyph width:%d, rows:%d", bitmap.width,
bitmap.rows);
- log_debug("ttf glyph metrics width:%ld, height:%ld", metrics.width,
metrics.height);
- log_debug("ttf glyph metrics X bearing:%ld, Y bearing:%ld",
metrics.horiBearingX, metrics.horiBearingY);
-#endif
-
- bi = render::create_bitmap_info_alpha(im->m_width, im->m_height,
im->m_data);
-
- if ( bitmap.width && bitmap.rows && metrics.width && metrics.height )
- {
- float xmax = float(bitmap.width) / float(im->m_width);
- float ymax = float(bitmap.rows) / float(im->m_height);
-
- float xmin = float(metrics.horiBearingX) / float(metrics.width);
- float ymin = float(metrics.horiBearingY) /
float(metrics.height);
-
- // ???
- xmin *= -xmax;
- ymin *= ymax;
-
- box.enclose_point(xmin, ymin);
- box.expand_to_point(xmax, ymax);
- }
- else
- {
- box.set_null();
- }
-
- // TODO: check this. Also check FT_FaceRec::units_per_EM
- static float s_advance_scale = 0.16666666f; //vv hack
- advance = (float) m_face->glyph->metrics.horiAdvance * s_advance_scale;
-
-#ifdef DEBUG_GLYPH_RENDERING
- log_debug(" box: %s, advance: %g", box.toString().c_str(), advance);
-#endif
-
-
- return bi;
-}
-#else // ndef(HAVE_FREETYPE2)
-boost::intrusive_ptr<bitmap_info>
-FreetypeRasterizer::getRenderedGlyph(uint16_t, rect& , float&)
-{
- assert(0); // should never be called...
-}
-#endif // ndef(HAVE_FREETYPE2)
-
-#ifdef HAVE_FREETYPE2
-boost::intrusive_ptr<shape_character_def>
-FreetypeRasterizer::getGlyph(uint16_t code, float& advance)
-{
- boost::intrusive_ptr<DynamicShape> sh;
-
- FT_Error error = FT_Load_Char(m_face, code,
FT_LOAD_NO_BITMAP|FT_LOAD_NO_SCALE);
- if ( error != 0 )
- {
- log_error("Error loading freetype outline glyph for char '%c'
(error: %d)", code, error);
- return sh.get();
- }
-
- // TODO: check this. Also check FT_FaceRec::units_per_EM
- advance = m_face->glyph->metrics.horiAdvance;
-
- assert(m_face->glyph->format == FT_GLYPH_FORMAT_OUTLINE);
-
- FT_Outline* outline = &(m_face->glyph->outline);
-
- //FT_BBox glyphBox;
- //FT_Outline_Get_BBox(outline, &glyphBox);
- //rect r(glyphBox.xMin, glyphBox.yMin, glyphBox.xMax, glyphBox.yMax);
- //log_msg("Glyph for character '%c' has computed bounds %s", code,
r.toString().c_str());
-
- sh = new DynamicShape();
- sh->beginFill(rgba(255, 255, 255, 255));
-
- FT_Outline_Funcs walk;
- walk.move_to = walkMoveTo;
- walk.line_to = walkLineTo;
- walk.conic_to = walkConicTo;
- walk.cubic_to = walkCubicTo;
- walk.shift = 0; // ?
- walk.delta = 0; // ?
-
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_debug("Decomposing glyph outline for character %u", code);
-#endif
- FT_Outline_Decompose(outline, &walk, sh.get());
-#ifdef DEBUG_OUTLINE_DECOMPOSITION
- log_msg("Decomposed glyph for character '%c' has bounds %s", code,
sh->get_bound().toString().c_str());
-#endif
-
- return sh.get();
-}
-#else // ndef(HAVE_FREETYPE2)
-boost::intrusive_ptr<shape_character_def>
-FreetypeRasterizer::getGlyph(uint16_t, float& advance)
-{
- assert(0); // should never be called...
-}
-#endif // ndef(HAVE_FREETYPE2)
-
-} // namespace gnash
-
Index: server/FreetypeRasterizer.h
===================================================================
RCS file: server/FreetypeRasterizer.h
diff -N server/FreetypeRasterizer.h
--- server/FreetypeRasterizer.h 13 Jun 2007 09:59:42 -0000 1.6
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,183 +0,0 @@
-// FreetypeRasterizer.h: Freetype glyphs manager
-//
-// Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-#ifndef GNASH_FREETYPE_H
-#define GNASH_FREETYPE_H
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-
-#ifdef HAVE_FREETYPE_FREETYPE_H
-# define HAVE_FREETYPE2 1
-#endif
-
-#include "rect.h"
-#include "smart_ptr.h" // for intrusive_ptr
-
-#include <string>
-#include <memory> // for auto_ptr
-
-#ifdef HAVE_FREETYPE2
-# include <ft2build.h>
-# include FT_FREETYPE_H
-# include FT_GLYPH_H
-#endif
-
-// Forward declarations
-namespace gnash {
- class bitmap_info;
- class shape_character_def;
-}
-namespace image {
- class alpha;
-}
-
-
-namespace gnash {
-
-/// Truetype font rasterizer/converter based on freetype library
-//
-/// Instances of this class provide rasterized or vectorial glyphs
-/// for a given truetype font face.
-///
-/// The rasterized glyphs have a max size of 96 (TODO: make parametrizable)
-/// but I think the actual size could change between glyphs (see the 'box'
-/// parameter of getRenderedGlyph() method).
-///
-/// Vectorial glyphs are instances of a shape_character_def, same class
-/// resulting from parsing of embedded fonts.
-///
-/// TODO: rename this class to something like FreetypeGlyphProvider...
-///
-class FreetypeRasterizer
-{
-
-public:
-
- /// Named constructor for a face-bound rasterizer.
- //
- /// @param name
- /// Name of the font to get glyphs info from
- ///
- /// @param bold
- /// Whether to use a bold version of the font
- ///
- /// @param italic
- /// Whether to use an italic version of the font
- ///
- /// @return a rasterizer bound to the given font name,
- /// or a NULL auto_ptr if the given truetype font
- /// could not be found.
- ///
- static std::auto_ptr<FreetypeRasterizer> createFace(const std::string&
name, bool bold, bool italic);
-
- /// Return the given character glyph as a bitmap
- //
- /// @param code
- /// Character code.
- ///
- /// @param box
- /// Output parameter. Bounding box of glyph is returned here.
- /// TODO: document units, and more about what a bounding box is
- /// (ie: actual bounds of visible shape or including padding?).
- /// NOTE: can be the NULL bound, for non-visible characters
- /// (space, tab, whatever else).
- ///
- /// @param advance
- /// Output parameter... TODO: describe what it is (units?)
- ///
- /// @return A bitmap_info, or a NULL pointer if the given character code
- /// doesn't exist in this font.
- ///
- /// TODO: drop ?
- ///
- boost::intrusive_ptr<bitmap_info> getRenderedGlyph(uint16_t code, rect&
box, float& advance);
-
- /// Return the given character glyph as a shape character definition
- //
- /// @param code
- /// Character code.
- ///
- /// @param advance
- /// Output parameter... TODO: describe what it is (units?)
- ///
- /// @return A shape_character_def, or a NULL pointer if the given
character code
- /// doesn't exist in this font.
- ///
- boost::intrusive_ptr<shape_character_def> getGlyph(uint16_t code,
float& advance);
-
-
-private:
-
- /// Use the named constructor to create an instance
- //
- /// throw a GnashException on error (unkonwn font name or similar).
- ///
- FreetypeRasterizer(const std::string& fontname, bool bold, bool italic);
-
-#ifdef HAVE_FREETYPE2
-
- // TODO: drop ?
- //float get_advance_x(uint16_t code);
-
- /// Get filename containing given font
- //
- /// @param name
- /// Font name
- ///
- /// @param bold
- /// Want bold version
- ///
- /// @param italic
- /// Want italic version
- ///
- /// @param filename
- /// Where to return the filename to
- ///
- /// @return true if the font was found, false otherwise.
- /// Actually, this function should return a default
- /// filename in any case, so false should only be
- /// returned if not even a default font was found.
- ///
- bool getFontFilename(const std::string& name, bool bold, bool italic,
- std::string& filename);
-
- static void init();
-
- static void close();
-
- /// Used by getRenderedGlyph to get the glyph bitmap.
- //
- /// NOTE: calls the currently registered renderer (create_alpha)
- ///
- std::auto_ptr<image::alpha> draw_bitmap(const FT_Bitmap& bitmap);
-
-
- static FT_Library m_lib;
- FT_Face m_face;
-
-#endif // HAVE_FREETYPE2
-
-};
-
-} // namespace gnash
-
-
-#endif // GNASH_FREETYPE_H
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Gnash-commit] gnash ChangeLog server/Makefile.am server/font....,
Sandro Santilli <=