00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00031
00032
00033
00034 DynamicTextGlyph::
00035 ~DynamicTextGlyph() {
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
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
00053 y += _y + _margin;
00054
00055 int x = _x + _margin;
00056
00057
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
00066
00067
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
00086
00087
00088
00089
00090
00091
00092
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
00101 nassertv(_geom_count == 0);
00102
00103 tex_x_size += _margin * 2;
00104 tex_y_size += _margin * 2;
00105
00106
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
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
00122 _geom = new GeomTextGlyph(this);
00123
00124
00125
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