gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash ChangeLog server/FreetypeRasterizer.cpp s...


From: Sandro Santilli
Subject: [Gnash-commit] gnash ChangeLog server/FreetypeRasterizer.cpp s...
Date: Fri, 08 Jun 2007 21:40:39 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Sandro Santilli <strk>  07/06/08 21:40:39

Modified files:
        .              : ChangeLog 
Added files:
        server         : FreetypeRasterizer.cpp FreetypeRasterizer.h 

Log message:
                * server/FreetypeRasterizer.{cpp,h}: Ported truetype
                  rasterizer from gameswf, cleaned up and documented
                  (as far as understood).

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.3501&r2=1.3502
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeRasterizer.cpp?cvsroot=gnash&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/gnash/server/FreetypeRasterizer.h?cvsroot=gnash&rev=1.1

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.3501
retrieving revision 1.3502
diff -u -b -r1.3501 -r1.3502
--- ChangeLog   8 Jun 2007 21:10:43 -0000       1.3501
+++ ChangeLog   8 Jun 2007 21:40:38 -0000       1.3502
@@ -1,5 +1,11 @@
 2007-06-08 Sandro Santilli <address@hidden>
 
+       * server/FreetypeRasterizer.{cpp,h}: Ported truetype
+         rasterizer from gameswf, cleaned up and documented
+         (as far as understood).
+
+2007-06-08 Sandro Santilli <address@hidden>
+
        * libbase/embedVideoDecoderGst.{cpp,h}: Store decodedFrame
          in an auto_ptr to avoid leaks. Fix the case in which
          there's NO decoded frame (ie: no gst-ffmpeg installed)

Index: server/FreetypeRasterizer.cpp
===================================================================
RCS file: server/FreetypeRasterizer.cpp
diff -N server/FreetypeRasterizer.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ server/FreetypeRasterizer.cpp       8 Jun 2007 21:40:39 -0000       1.1
@@ -0,0 +1,206 @@
+// gameswf_freetype.cpp        -- Vitaly Alexeev <address@hidden>      2007
+
+// This source code has been donated to the Public Domain.  Do
+// whatever you want with it.
+
+// TrueType font rasterizer based on freetype library,
+// used code from demos/font_output/font_output.cpp
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "FreetypeRasterizer.h"
+#include "log.h"
+
+#include <cstdio> // for snprintf
+#include <string>
+#include <memory> // for auto_ptr
+
+#define FREETYPE_MAX_FONTSIZE 96
+
+namespace gnash {
+
+#ifdef HAVE_LIBFREETYPE 
+
+// 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*  src = bitmap.buffer + bitmap.pitch * i;
+               uint8*  dst = alpha->m_data + alpha->m_pitch * i;
+               int     x = bitmap.width;
+               while (x-- > 0)
+               {
+                       *dst++ = *src++;
+               }
+       }
+
+       return alpha;
+}
+
+// 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 // HAVE_LIBFREETYPE 
+
+#ifdef HAVE_LIBFREETYPE 
+// 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_LIBFREETYPE 
+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_LIBFREETYPE 
+
+#ifdef HAVE_LIBFREETYPE 
+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 (get_fontfile(name, filename, bold, italic) == 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:
+                       char buf[64];
+                       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 !
+                       char buf[64];
+                       snprintf(buf, maxerrlen, _("Some error opening font 
'%s'"), filename.c_str());
+                       buf[maxerrlen-1] = '\0';
+                       throw GnashException(buf):
+                       break;
+       }
+}
+#else // ndef(HAVE_LIBFREETYPE)
+FreetypeRasterizer::FreetypeRasterizer(const std::string&, bool, bool)
+{
+       assert(0); // should never be called
+}
+#endif // ndef HAVE_LIBFREETYPE 
+
+#ifdef HAVE_LIBFREETYPE
+std::auto_ptr<bitmap_info>
+FreetypeRasterizer::getRenderedGlyph(uint16_t code, rect& box, float& advance)
+{
+       std::auto_ptr<bitmap_info> bi;
+
+       FT_Set_Pixel_Sizes(m_face, 0, FREETYPE_MAX_FONTSIZE);
+       if (FT_Load_Char(m_face, code, FT_LOAD_RENDER))
+       {
+               return bi;
+       }
+
+
+       std::auto_ptr<image::alpha> im ( draw_bitmap(m_face->glyph->bitmap) );
+       bi.reset( render::create_bitmap_info_alpha(im->m_width, im->m_height, 
im->m_data) );
+
+       box.m_x_max = float(m_face->glyph->bitmap.width) / 
float(bi->m_suspended_image->m_width);
+       box.m_y_max = float(m_face->glyph->bitmap.rows) / 
float(bi->m_suspended_image->m_height);
+
+       box.m_x_min = float(m_face->glyph->metrics.horiBearingX) / 
float(m_face->glyph->metrics.width);
+       box.m_y_min = float(m_face->glyph->metrics.horiBearingY) / 
float(m_face->glyph->metrics.height);
+       box.m_x_min *= -box.m_x_max;
+       box.m_y_min *= box.m_y_max;
+       
+       advance = (float) m_face->glyph->metrics.horiAdvance * s_advance_scale;
+
+       return bi;
+}
+#else // ndef(HAVE_LIBFREETYPE)
+std::auto_ptr<bitmap_info>
+FreetypeRasterizer::getRenderedGlyph(uint16_t, rect& , float&)
+{
+       assert(0); // should never be called... 
+}
+#endif // ndef(HAVE_LIBFREETYPE)
+
+
+} // namespace gnash
+

Index: server/FreetypeRasterizer.h
===================================================================
RCS file: server/FreetypeRasterizer.h
diff -N server/FreetypeRasterizer.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ server/FreetypeRasterizer.h 8 Jun 2007 21:40:39 -0000       1.1
@@ -0,0 +1,114 @@
+// freetype.h  -- Vitaly Alexeev <address@hidden>      2007
+
+// This source code has been donated to the Public Domain.  Do
+// whatever you want with it.
+
+#ifndef GNASH_FREETYPE_H
+#define GNASH_FREETYPE_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "rect.h"
+
+#include <string>
+#include <memory> // for auto_ptr
+
+#ifdef HAVE_LIBFREETYPE 
+# include <ft2build.h>
+# include <freetype.h>
+# include <ftglyph.h>
+# include FT_GLYPH_H
+#endif
+
+// Forward declarations
+namespace gnash {
+       class bitmap_info;
+       namespace image {
+               class alpha;
+       }
+}
+
+
+namespace gnash {
+
+/// Truetype font rasterizer based on freetype library
+//
+/// Instances of this class provide rasterized 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).
+///
+class FreetypeRasterizer 
+{
+       /// 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... TODO: describe what it is.
+       ///
+       /// @param advance
+       ///     Output parameter... TODO: describe what it is.
+       ///
+       /// @return A bitmap_info, or a NULL pointer if the given character code
+       ///         doesn't exist in this font.
+       ///
+       std::auto_ptr<bitmap_info> getRenderedGlyph(uint16_t code, rect& box, 
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_LIBFREETYPE 
+
+       // TODO: drop ?
+       float get_advance_x(uint16_t code);
+
+       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_LIBFREETYPE
+
+};
+
+} // namespace gnash
+
+
+#endif // GNASH_FREETYPE_H




reply via email to

[Prev in Thread] Current Thread [Next in Thread]