Fresco/Berlin/modules/Fonts Font.cc,1.4,1.5 Font.hh,1.2,1.3 FontKitImpl.cc,1.5,1.6 FontKitImpl.hh,1.5,1.6 Glyph.cc,1.4,1.5 Glyph.hh,1.4,1.5

Nick Lewycky <[email protected]>
Newsgroups gmane.comp.video.fresco.cvs
Message-ID <[email protected]>
Update of /cvs/fresco/Fresco/Berlin/modules/Fonts
In directory purcel:/tmp/cvs-serv24779/Berlin/modules/Fonts

Modified Files:
	Font.cc Font.hh FontKitImpl.cc FontKitImpl.hh Glyph.cc 
	Glyph.hh 
Log Message:
Move the dpi from Font::glyph_char to Glyph::bitmap where it belongs.
The FontKit can now return a Raster containing the glyph at a given dpi!


Index: Font.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/Font.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Font.cc	16 Feb 2003 04:11:44 -0000	1.4
+++ Font.cc	17 Feb 2003 02:10:33 -0000	1.5
@@ -30,17 +30,17 @@
 Font::Font(const char *filename, int size, FT_Library library)
   : my_ftlib(library), my_size(size)
 {
-  FT_New_Face(my_ftlib, filename, 0, &my_face);
+  if (FT_New_Face(my_ftlib, filename, 0, &my_face) != 0)
+    { throw Fresco::CreationFailureException(); }
   FT_Set_Char_Size(my_face, 0, my_size*64, 0, 0);
 }
 
 Font::~Font() {}
 
-Fresco::Glyph_ptr Font::glyph_char(Fresco::Unichar c, short unsigned int xdpi, short unsigned int ydpi)
+Fresco::Glyph_ptr Font::glyph_char(Fresco::Unichar c)
 {
-  FT_Set_Char_Size(my_face, 0, my_size*64, xdpi, ydpi);
-  GlyphImpl *glyph = new GlyphImpl(my_face, c);
-  return Fresco::Glyph_var(glyph->_this());
+  GlyphImpl *glyph = new GlyphImpl(my_face, my_size, c);
+  return glyph->_this();
 }
 
 CORBA::Boolean Font::has_char(Fresco::Unichar c)
@@ -95,6 +95,7 @@
   Fresco::Vertex v;
   v.x = kern.x / 0x10000;
   v.y = kern.y / 0x10000;
+  return v;
 }
 
 CORBA::Float Font::angle()

Index: Font.hh
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/Font.hh,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Font.hh	16 Feb 2003 03:47:47 -0000	1.2
+++ Font.hh	17 Feb 2003 02:10:33 -0000	1.3
@@ -19,8 +19,8 @@
  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
  * MA 02139, USA.
  */
-#ifndef _FontKit_Font_hh
-#define _FontKit_Font_hh
+#ifndef _Fonts_Font_hh
+#define _Fonts_Font_hh
 
 #include <Fresco/config.hh>
 #include <Fresco/Font.hh>
@@ -46,7 +46,7 @@
   Font(const char *filename, int size, FT_Library library);
   virtual ~Font();
 
-  virtual Fresco::Glyph_ptr glyph_char(Fresco::Unichar c, short unsigned int xdpi, short unsigned int ydpi);
+  virtual Fresco::Glyph_ptr glyph_char(Fresco::Unichar c);
   virtual CORBA::Boolean has_char(Fresco::Unichar c);
   virtual CORBA::Boolean can_display(Fresco::Unichar begin,
                                      Fresco::Unichar end);

Index: FontKitImpl.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/FontKitImpl.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- FontKitImpl.cc	16 Feb 2003 04:11:44 -0000	1.5
+++ FontKitImpl.cc	17 Feb 2003 02:10:33 -0000	1.6
@@ -22,6 +22,7 @@
 
 #include <Prague/Sys/Tracer.hh>
 #include <Fresco/config.hh>
+#include <Fresco/PickTraversal.hh>
 #include <Fresco/DrawTraversal.hh>
 #include <Berlin/MonoGraphic.hh>
 #include "FontKitImpl.hh"
@@ -31,28 +32,29 @@
 using namespace Fresco;
 using namespace Berlin::FontKit;
 
+namespace Berlin
+{
+namespace FontKit
+{
+
 class FontDecorator : public MonoGraphic
 {
 public:
   FontDecorator(Font_ptr f) : my_font(f) {}
-#if 0
   virtual void traverse(Traversal_ptr traversal)
   {
     traversal->visit(Graphic_var(_this()));
   }
-#endif
   virtual void draw(DrawTraversal_ptr traversal)
   {
     DrawingKit_var drawing = traversal->drawing();
     // and do... NOTHING! yet.
     MonoGraphic::traverse(traversal);
   }
-#if 0
   virtual void pick(PickTraversal_ptr traversal)
   {
     MonoGraphic::traverse(traversal);
   }
-#endif
 private:
   Font_ptr my_font;
 };
@@ -61,99 +63,90 @@
 {
 public:
   FontSizeDecorator(Coord s) : my_size(s) {}
-#if 0
   virtual void traverse(Traversal_ptr traversal)
   {
     traversal->visit(Graphic_var(_this()));
   }
-#endif
   virtual void draw(DrawTraversal_ptr traversal)
   {
     DrawingKit_var drawing = traversal->drawing();
     // and do... NOTHING! yet.
     MonoGraphic::traverse(traversal);
   }
-#if 0
   virtual void pick(PickTraversal_ptr traversal)
   {
     MonoGraphic::traverse(traversal);
   }
-#endif
 private:
   Coord my_size;
 };
 
-class FontIterator : public virtual POA_Fresco::FontIterator
+FontKitImpl::FontIterator::FontIterator(FontKitImpl *fk)
+  : my_fk(fk)
 {
-public:
-  FontIterator::FontIterator(FontKitImpl *fk)
-    : my_fk(fk)
-  {
-    Prague::Path path = RCManager::get_path("fontpath");
-    scan(path);
-  }
-  Font_ptr child() const
-  {
-    return my_fk->_cxx_default();
-  }
-  void next() { faces_iterator++;}
-  void prev() { faces_iterator--;}
-  void destroy() { delete this;}//deactivate();}
+  Prague::Path path = RCManager::get_path("fontpath");
+  scan(path);
+}
 
-  void begin() { faces_iterator = faces.begin();}
-  void end() { faces_iterator = faces.end();}
-  void scan(Prague::Path path)
-  {
-    FT_Face my_face;
-    for (Prague::Path::iterator i = path.begin(); i != path.end(); ++i)
+FontKitImpl::FontIterator::~FontIterator() {}
+
+Font_ptr FontKitImpl::FontIterator::child()
+{
+  return my_fk->_cxx_default();
+}
+void FontKitImpl::FontIterator::next() { faces_iterator++;}
+void FontKitImpl::FontIterator::prev() { faces_iterator--;}
+void FontKitImpl::FontIterator::destroy() { delete this;}//deactivate();}
+
+void FontKitImpl::FontIterator::begin() { faces_iterator = faces.begin();}
+void FontKitImpl::FontIterator::end() { faces_iterator = faces.end();}
+void FontKitImpl::FontIterator::scan(Prague::Path path)
+{
+  FT_Face my_face;
+  for (Prague::Path::iterator i = path.begin(); i != path.end(); ++i)
     {
       Directory directory(*i, Directory::alpha);
       for (Prague::Directory::iterator j = directory.begin(); j != directory.end(); ++j)
-      {
-        if ((*j)->name()[0] == '.') continue;
-        std::string file = (*j)->long_name();
-        if (FT_New_Face(*(my_fk->get_ftlibrary()), file.c_str(), 0, &my_face))
-        {
-          Logger::log(Logger::text) << "FontKit: file " << file << " is not a font." << std::endl;
-          continue;
-        }
-        faces.push_back(file);
-      }
+	{
+	  if ((*j)->name()[0] == '.') continue;
+	  std::string file = (*j)->long_name();
+	  if (FT_New_Face(*(my_fk->get_ftlibrary()), file.c_str(), 0, &my_face))
+	    {
+	      Logger::log(Logger::text) << "FontKit: file " << file << " is not a font." << std::endl;
+	      continue;
+	    }
+	  faces.push_back(file);
+	}
     }
-  }
-private:
-  FontKitImpl *my_fk;
-  std::vector<std::string> faces;
-  std::vector<std::string>::iterator faces_iterator;
-};
+}
 
 FontKitImpl::FontKitImpl(const std::string &id,
                          const Fresco::Kit::PropertySeq &p)
     : KitImpl(id, p)
 {
   FT_Init_FreeType(&my_library);
-  FT_Face face;
 }
 
 FontKitImpl::~FontKitImpl() {}
 
 Font_ptr FontKitImpl::_cxx_default()
 {
-  return Font_ptr(new Font("/usr/share/fonts/truetype/commercial/arialuni.ttf", 12, my_library));
+  Font *f = new Font("/usr/share/fonts/truetype/commercial/arialuni.ttf", 12, my_library);
+  return f->_this();
 }
 
 Font_ptr FontKitImpl::filename(const char *fn, const Fresco::Unistring &style,
                                const Fresco::Coord size)
 {
-  return Font_ptr(new Font(fn, size, my_library));
+  Font *f = new Font(fn, size, my_library);
+  return f->_this();
 }
 
 Font_ptr FontKitImpl::provide(const Fresco::Unistring &family,
                               const Fresco::Unistring &style,
                               const Fresco::Coord size)
 {
-  return _cxx_default();
-  //return Font_ptr::_nil();
+  return _cxx_default(); // XXX
 }
 
 Fresco::Graphic_ptr FontKitImpl::set_font(Fresco::Graphic_ptr g, Font_ptr f)
@@ -197,10 +190,16 @@
 
 FontIterator_ptr FontKitImpl::first_font()
 {
+  FontIterator *fi = new FontIterator(this);
+  fi->begin();
+  return fi->_this();
 }
 
 FontIterator_ptr FontKitImpl::last_font()
 {
+  FontIterator *fi = new FontIterator(this);
+  fi->end();
+  return fi->_this();
 }
 
 extern "C" KitImpl *load()
@@ -208,3 +207,6 @@
   static std::string properties[] = {"implementation", "FontKitImpl"};
   return create_kit<Berlin::FontKit::FontKitImpl> ("IDL:fresco.org/Fresco/FontKit:1.0", properties, 2);
 }
+
+} // namespace
+} // namespace

Index: FontKitImpl.hh
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/FontKitImpl.hh,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- FontKitImpl.hh	16 Feb 2003 03:47:47 -0000	1.5
+++ FontKitImpl.hh	17 Feb 2003 02:10:33 -0000	1.6
@@ -19,8 +19,8 @@
  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
  * MA 02139, USA.
  */
-#ifndef _FontKitImpl_hh
-#define _FontKitImpl_hh
+#ifndef _Fonts_FontKitImpl_hh
+#define _Fonts_FontKitImpl_hh
 
 #include <Fresco/config.hh>
 #include <Fresco/FontKit.hh>
@@ -38,8 +38,10 @@
 #include <ft2build.h>
 #include FT_FREETYPE_H
 
-namespace Berlin {
-namespace FontKit {
+namespace Berlin
+{
+namespace FontKit
+{
 
 using namespace Fresco;
 
@@ -51,17 +53,21 @@
   class FontIterator : public virtual POA_Fresco::FontIterator
   {
   public:
-    FontIterator::FontIterator(FontKitImpl *);
-    virtual Font_ptr child() const;
+    FontIterator(FontKitImpl *);
+    virtual ~FontIterator();
+    virtual Font_ptr child();
     virtual void next();
-    void prev();
-    void destroy();
+    virtual void prev();
+    virtual void destroy();
 
-    void begin();
-    void end();
-    void scan(Prague::Path);
+    virtual void begin();
+    virtual void end();
+    virtual void scan(Prague::Path);
+  private:
+    FontKitImpl *my_fk;
+    std::vector<std::string> faces;
+    std::vector<std::string>::iterator faces_iterator;
   };
-
 
   FontKitImpl(const std::string &, const Fresco::Kit::PropertySeq &);
   virtual ~FontKitImpl();

Index: Glyph.cc
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/Glyph.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Glyph.cc	16 Feb 2003 04:11:44 -0000	1.4
+++ Glyph.cc	17 Feb 2003 02:10:33 -0000	1.5
@@ -29,32 +29,35 @@
 namespace FontKit
 {
 
-GlyphImpl::GlyphImpl(FT_Face face, FT_ULong char_index)
-  : my_face(face)
+GlyphImpl::GlyphImpl(FT_Face face, Fresco::Coord size, FT_ULong char_index)
+  : my_face(face), my_size(size), my_uc(char_index)
 {
   my_tr.xx = 0x10000;
   my_tr.xy = 0x00000;
   my_tr.yx = 0x00000;
   my_tr.yy = 0x10000;
-  FT_Load_Glyph(my_face, char_index, FT_LOAD_DEFAULT);
 }
 
 GlyphImpl::~GlyphImpl() {}
 
-Fresco::Raster_ptr GlyphImpl::bitmap()
+Fresco::Raster_ptr GlyphImpl::bitmap(short unsigned int xdpi, short unsigned int ydpi)
 {
   RasterImpl *raster = new RasterImpl();
   activate(raster);
 
+  if (FT_Set_Char_Size(my_face, 0, my_size*64, xdpi, ydpi) != 0)
+    std::cerr << "set char size failed." << std::endl;
   FT_Set_Transform(my_face, &my_tr, 0);
 
-  FT_Vector origin;
-  origin.x = 0;
-  origin.y = 0;
+  if (FT_Load_Glyph(my_face, FT_Get_Char_Index(my_face, my_uc),
+		    FT_LOAD_DEFAULT) != 0)
+      std::cerr << "load glyph failed." << std::endl;
 
   FT_Glyph glyph;
-  FT_Get_Glyph(my_face->glyph, &glyph);
-  FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, &origin, 1);
+  if (FT_Get_Glyph(my_face->glyph, &glyph) != 0)
+    std::cerr << "get glyph failed." << std::endl;
+  if (FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1) != 0)
+    std::cerr << "glyph to bitmap failed." << std::endl;
   FT_BitmapGlyph glyph_bitmap = (FT_BitmapGlyph)glyph;
 
   unsigned char *buffer = glyph_bitmap->bitmap.buffer;
@@ -62,29 +65,31 @@
   int height = glyph_bitmap->bitmap.rows;
 
   Fresco::Raster::ColorSeq pixels;
+  pixels.length(width*height);
 
   switch (glyph_bitmap->bitmap.pixel_mode)
     {
     case FT_PIXEL_MODE_NONE:
       // should never happen
-      std::cout << "WTF;FT_PIXEL_MODE_NONE" << std::endl;
+      std::cout << "FontKit confused: FT_PIXEL_MODE_NONE?" << std::endl;
       break;
     case FT_PIXEL_MODE_MONO: // MSB 1 bpp
       for (int i = 0; i < height; i++)
         for (int j = 0; j < width; j++) {
           Fresco::Color c;
-          c.red = (buffer[i*(width/8)+(j/8)] &
-                             (0x80 >> j%8)) ? 0xFFFFFF00 : 0;
-          c.green = c.red; c.blue = c.red;
+          c.red = 1.; c.green = 1.; c.blue = 1.;
+          c.alpha = (buffer[i*(width/8)+(j/8)] & (0x80 >> j%8)) ? 1. : 0.;
+          pixels[i*width+j] = c;
         }
       break;
     case FT_PIXEL_MODE_GRAY: // 8 bpp count of grey levels in num_bytes
       for (int i = 0; i < height; i++)
         for (int j = 0; j < width; j++) {
           Fresco::Color c;
-          c.red = buffer[i*width+j];
-          c.green = c.red; c.blue = c.red;
-	}
+          c.red = 1.; c.green = 1.; c.blue = 1.;
+          c.alpha = buffer[i*width+j] / 255.;
+          pixels[i*width+j] = c;
+        }
       break;
     case FT_PIXEL_MODE_GRAY2: // 2bpp (no known fonts)
       std::cout << "FontKit NYI: FT_PIXEL_MODE_GRAY2" << std::endl;
@@ -99,23 +104,27 @@
       std::cout << "FontKit NYI: FT_PIXEL_MODE_LCD_V" << std::endl;
       break;
     default:
-      std::cout << "FontKit unknown type : "
+      std::cout << "FontKit unknown type: "
                 << glyph_bitmap->bitmap.pixel_mode << std::endl;
       break;
     }
-  FT_Set_Transform(my_face, 0, 0);
   FT_Done_Glyph(glyph);
+  FT_Set_Transform(my_face, 0, 0);
 
   Fresco::Raster::Index lower; lower.x = 0; lower.y = 0;
   Fresco::Raster::Index upper; upper.x = width; upper.y = height;
   raster->load_pixels(lower, upper, pixels);
 
-  return Fresco::Raster_ptr(raster);
+  return raster->_this();
 }
 
 Fresco::FontShape *GlyphImpl::decompose()
 {
+  FT_Set_Char_Size(my_face, 0, my_size*64, 0, 0);
   FT_Set_Transform(my_face, &my_tr, 0);
+  if (FT_Load_Glyph(my_face, FT_Get_Char_Index(my_face, my_uc),
+		    FT_LOAD_DEFAULT & FT_LOAD_IGNORE_TRANSFORM) != 0)
+      std::cerr << "load glyph failed." << std::endl;
   Fresco::FontShape *f = new Fresco::FontShape(); f->length(0);
   // use FT_Outline_Decompose
   FT_Set_Transform(my_face, 0, 0);
@@ -126,6 +135,7 @@
 void GlyphImpl::char_info(Fresco::GlyphMetrics &gm)
 {
   double scale = 1.;
+  // this isn't safe enough. We need to keep our own copy of the glyph.
   gm.size.x = static_cast<CORBA::Long>(my_face->glyph->metrics.width / scale);
   gm.size.y = static_cast<CORBA::Long>(my_face->glyph->metrics.height / scale);
   gm.hori_bearing.x = static_cast<CORBA::Long>(my_face->glyph->metrics.horiBearingX / scale);

Index: Glyph.hh
===================================================================
RCS file: /cvs/fresco/Fresco/Berlin/modules/Fonts/Glyph.hh,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Glyph.hh	16 Feb 2003 04:11:44 -0000	1.4
+++ Glyph.hh	17 Feb 2003 02:10:33 -0000	1.5
@@ -19,12 +19,13 @@
  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
  * MA 02139, USA.
  */
-#ifndef _FontKit_Glyph_hh
-#define _FontKit_Glyph_hh
+#ifndef _Fonts_Glyph_hh
+#define _Fonts_Glyph_hh
 
 #include <Fresco/config.hh>
 #include <Fresco/Font.hh>
 #include <Fresco/Transform.hh>
+#include <Fresco/Unicode.hh>
 #include <Berlin/RefCountBaseImpl.hh>
 #include <Berlin/IdentifiableImpl.hh>
 
@@ -45,16 +46,19 @@
                   public virtual IdentifiableImpl
 {
 public:
-  GlyphImpl(FT_Face face, FT_ULong char_index);
+  GlyphImpl(FT_Face face, Fresco::Coord size, FT_ULong char_index);
   virtual ~GlyphImpl();
 
-  virtual Fresco::Raster_ptr bitmap();
+  virtual Fresco::Raster_ptr bitmap(short unsigned int xdpi,
+                                    short unsigned int ydpi);
   virtual Fresco::FontShape *decompose();
   virtual void char_info(Fresco::GlyphMetrics &gm);
   virtual void transformation(Fresco::Transform_ptr);
 private:
   FT_Face my_face;
   FT_Matrix my_tr;
+  Fresco::Coord my_size;
+  Fresco::Unichar my_uc;
 };
 
 } // namespace
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.