15 #include "dynamicTextPage.h"
16 #include "dynamicTextFont.h"
17 #include "config_text.h"
30 DynamicTextPage(DynamicTextFont *font,
int page_number) :
35 set_compression(CM_off);
39 set_quality_level(text_quality_level);
41 _x_size = _font->get_page_x_size();
42 _y_size = _font->get_page_y_size();
44 setup_2d_texture(_x_size, _y_size, T_unsigned_byte, font->get_tex_format());
48 strm << font->get_name() <<
"_" << page_number;
52 set_keep_ram_image(
true);
54 set_minfilter(_font->get_minfilter());
55 set_magfilter(_font->get_magfilter());
57 set_anisotropic_degree(_font->get_anisotropic_degree());
61 set_wrap_u(text_wrap_mode);
62 set_wrap_v(text_wrap_mode);
63 set_border_color(font->get_bg());
66 fill_region(0, 0, _x_size, _y_size, font->get_bg());
76 DynamicTextGlyph *DynamicTextPage::
77 slot_glyph(
int character,
int x_size,
int y_size,
int margin) {
79 if (!find_hole(x, y, x_size, y_size)) {
81 return (DynamicTextGlyph *)NULL;
85 PT(DynamicTextGlyph) glyph =
86 new DynamicTextGlyph(character, this,
87 x, y, x_size, y_size, margin);
88 _glyphs.push_back(glyph);
98 void DynamicTextPage::
99 fill_region(
int x,
int y,
int x_size,
int y_size, const
LColor &
color) {
100 nassertv(x >= 0 && x + x_size <= _x_size && y >= 0 && y + y_size <= _y_size);
101 int num_components = get_num_components();
102 if (num_components == 1) {
105 if (get_format() != Texture::F_alpha) {
109 unsigned char v = (
unsigned char)(color[ci] * 255.0f);
111 unsigned char *image = modify_ram_image();
112 for (
int yi = y; yi < y + y_size; yi++) {
113 unsigned char *row = image + yi * _x_size;
114 memset(row + x, v, x_size);
117 }
else if (num_components == 2) {
125 v.p[0] = (
unsigned char)(color[0] * 255.0f);
126 v.p[1] = (
unsigned char)(color[3] * 255.0f);
128 PN_uint16 *image = (PN_uint16 *)modify_ram_image().p();
129 for (
int yi = y; yi < y + y_size; yi++) {
130 PN_uint16 *row = image + yi * _x_size ;
131 for (
int xi = x; xi < x + x_size; xi++) {
136 }
else if (num_components == 3) {
139 unsigned char p0 = (
unsigned char)(color[2] * 255.0f);
140 unsigned char p1 = (
unsigned char)(color[1] * 255.0f);
141 unsigned char p2 = (
unsigned char)(color[0] * 255.0f);
143 unsigned char *image = modify_ram_image();
144 for (
int yi = y; yi < y + y_size; yi++) {
145 unsigned char *row = image + yi * _x_size * 3;
146 for (
int xi = x; xi < x + x_size; xi++) {
148 row[xi * 3 + 1] = p1;
149 row[xi * 3 + 2] = p2;
160 v.p[0] = (
unsigned char)(color[2] * 255.0f);
161 v.p[1] = (
unsigned char)(color[1] * 255.0f);
162 v.p[2] = (
unsigned char)(color[0] * 255.0f);
163 v.p[3] = (
unsigned char)(color[3] * 255.0f);
165 PN_uint32 *image = (PN_uint32 *)modify_ram_image().p();
166 for (
int yi = y; yi < y + y_size; yi++) {
167 PN_uint32 *row = image + yi * _x_size;
168 for (
int xi = x; xi < x + x_size; xi++) {
184 int DynamicTextPage::
185 garbage_collect(DynamicTextFont *font) {
186 int removed_count = 0;
190 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
191 DynamicTextGlyph *glyph = (*gi);
192 if (glyph->_geom_count != 0) {
194 new_glyphs.insert(new_glyphs.end(), (*gi));
202 _glyphs.swap(new_glyphs);
203 return removed_count;
214 bool DynamicTextPage::
215 find_hole(
int &x,
int &y,
int x_size,
int y_size)
const {
217 while (y + y_size <= _y_size) {
218 int next_y = _y_size;
221 while (x + x_size <= _x_size) {
225 DynamicTextGlyph *overlap = find_overlap(x, y, x_size, y_size);
227 if (overlap == (DynamicTextGlyph *)NULL) {
232 next_x = overlap->_x + overlap->_x_size;
233 next_y = min(next_y, overlap->_y + overlap->_y_size);
234 nassertr(next_x > x,
false);
238 nassertr(next_y > y,
false);
257 DynamicTextGlyph *DynamicTextPage::
258 find_overlap(
int x,
int y,
int x_size,
int y_size)
const {
259 Glyphs::const_iterator gi;
260 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
261 DynamicTextGlyph *glyph = (*gi);
262 if (glyph->intersects(x, y, x_size, y_size)) {
267 return (DynamicTextGlyph *)NULL;
271 #endif // HAVE_FREETYPE
This is the base class for all three-component vectors and points.
TypeHandle is the identifier used to differentiate C++ class types.