Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/text/dynamicTextGlyph.cxx

Go to the documentation of this file.
00001 // Filename: dynamicTextGlyph.cxx
00002 // Created by:  drose (09Feb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "dynamicTextGlyph.h"
00020 
00021 #ifdef HAVE_FREETYPE
00022 
00023 #include "dynamicTextPage.h"
00024 #include "geomTextGlyph.h"
00025 #include "textureAttrib.h"
00026 #include "transparencyAttrib.h"
00027 #include "renderState.h"
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: DynamicTextGlyph::Destructor
00031 //       Access: Public, Virtual
00032 //  Description: 
00033 ////////////////////////////////////////////////////////////////////
00034 DynamicTextGlyph::
00035 ~DynamicTextGlyph() {
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: DynamicTextGlyph::get_row
00040 //       Access: Public
00041 //  Description: Returns a pointer to the first byte in the pixel
00042 //               buffer associated with the leftmost pixel in the
00043 //               indicated row, where 0 is the topmost row and _y_size
00044 //               - _margin * 2 - 1 is the bottommost row.
00045 ////////////////////////////////////////////////////////////////////
00046 unsigned char *DynamicTextGlyph::
00047 get_row(int y) {
00048   nassertr(y >= 0 && y < _y_size - _margin * 2, (unsigned char *)NULL);
00049   nassertr(_page != (DynamicTextPage *)NULL, (unsigned char *)NULL);
00050   nassertr(_page->_pbuffer != (PixelBuffer *)NULL, (unsigned char *)NULL);
00051 
00052   // First, offset y by the glyph's start.
00053   y += _y + _margin;
00054   // Also, get the x start.
00055   int x = _x + _margin;
00056 
00057   // Invert y.
00058   y = _page->_pbuffer->get_ysize() - 1 - y;
00059 
00060   int offset = (y * _page->_pbuffer->get_xsize()) + x;
00061   return _page->_pbuffer->_image + offset; 
00062 }
00063 
00064 ////////////////////////////////////////////////////////////////////
00065 //     Function: DynamicTextGlyph::erase
00066 //       Access: Public
00067 //  Description: Erases the glyph from the texture map.
00068 ////////////////////////////////////////////////////////////////////
00069 void DynamicTextGlyph::
00070 erase() {
00071   nassertv(_page != (DynamicTextPage *)NULL);
00072   nassertv(_page->_pbuffer != (PixelBuffer *)NULL);
00073 
00074   int ysizetop = _page->_pbuffer->get_ysize() - 1;
00075   int xsize = _page->_pbuffer->get_xsize();
00076   unsigned char *buffer = _page->_pbuffer->_image;
00077 
00078   for (int y = _y; y < _y + _y_size; y++) {
00079     int offset = (ysizetop - y) * xsize + _x;
00080     memset(buffer + offset, 0, _x_size);
00081   }
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: DynamicTextGlyph::make_geom
00086 //       Access: Public
00087 //  Description: Creates the actual geometry for the glyph.  The
00088 //               parameters bitmap_top and bitmap_left are from
00089 //               FreeType, and indicate the position of the top left
00090 //               corner of the bitmap relative to the glyph's origin.
00091 //               The advance number represents the number of pixels
00092 //               the pen should be advanced after drawing this glyph.
00093 ////////////////////////////////////////////////////////////////////
00094 void DynamicTextGlyph::
00095 make_geom(int bitmap_top, int bitmap_left, float advance, float poly_margin, 
00096           float tex_x_size, float tex_y_size,
00097           float font_pixels_per_unit, float tex_pixels_per_unit) {
00098   nassertv(_page != (DynamicTextPage *)NULL);
00099 
00100   // This function should not be called twice.
00101   nassertv(_geom_count == 0);
00102 
00103   tex_x_size += _margin * 2;
00104   tex_y_size += _margin * 2;
00105 
00106   // Determine the corners of the rectangle in geometric units.
00107   float tex_poly_margin = poly_margin / tex_pixels_per_unit;
00108   float origin_y = bitmap_top / font_pixels_per_unit;
00109   float origin_x = bitmap_left / font_pixels_per_unit;
00110   float top = origin_y + tex_poly_margin;
00111   float left = origin_x - tex_poly_margin;
00112   float bottom = origin_y - tex_y_size / tex_pixels_per_unit - tex_poly_margin;
00113   float right = origin_x + tex_x_size / tex_pixels_per_unit + tex_poly_margin;
00114 
00115   // And the corresponding corners in UV units.
00116   float uv_top = 1.0f - (float)(_y - poly_margin) / _page->get_y_size();
00117   float uv_left = (float)(_x - poly_margin) / _page->get_x_size();
00118   float uv_bottom = 1.0f - (float)(_y + poly_margin + tex_y_size) / _page->get_y_size();
00119   float uv_right = (float)(_x + poly_margin + tex_x_size) / _page->get_x_size();
00120 
00121   // Create a corresponding tristrip.
00122   _geom = new GeomTextGlyph(this);
00123 
00124   // The above will increment our _geom_count to 1.  Reset it back
00125   // down to 0, since our own internal Geom doesn't count.
00126   nassertv(_geom_count == 1);
00127   _geom_count--;
00128 
00129   PTA_Vertexf coords;
00130   coords.push_back(Vertexf(left, 0, top));
00131   coords.push_back(Vertexf(left, 0, bottom));
00132   coords.push_back(Vertexf(right, 0, top));
00133   coords.push_back(Vertexf(right, 0, bottom));
00134 
00135   PTA_TexCoordf texcoords;
00136   texcoords.push_back(TexCoordf(uv_left, uv_top));
00137   texcoords.push_back(TexCoordf(uv_left, uv_bottom));
00138   texcoords.push_back(TexCoordf(uv_right, uv_top));
00139   texcoords.push_back(TexCoordf(uv_right, uv_bottom));
00140 
00141   PTA_Colorf colors;
00142   colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f));
00143 
00144   PTA_int lengths;
00145   lengths.push_back(4);
00146 
00147   _geom->set_coords(coords);
00148   _geom->set_texcoords(texcoords, G_PER_VERTEX);
00149   _geom->set_colors(colors, G_OVERALL);
00150   _geom->set_lengths(lengths);
00151   _geom->set_num_prims(1);
00152 
00153   _state = RenderState::make(TextureAttrib::make(_page),
00154                              TransparencyAttrib::make(TransparencyAttrib::M_alpha));
00155 
00156   _advance = advance / font_pixels_per_unit;
00157 }
00158 
00159 
00160 #endif  // HAVE_FREETYPE

Generated on Fri May 2 00:44:15 2003 for Panda by doxygen1.3