calc_width

This commit is contained in:
David Rose 2009-12-13 07:53:30 +00:00
parent ca5f030cc1
commit 96dbda26ba
3 changed files with 35 additions and 7 deletions

View File

@ -137,3 +137,16 @@ generate_into(const string &text, PNMImage &dest_image, int x, int y) {
encoder.set_text(text); encoder.set_text(text);
return generate_into(encoder.get_wtext(), dest_image, x, y); return generate_into(encoder.get_wtext(), dest_image, x, y);
} }
////////////////////////////////////////////////////////////////////
// Function: PNMTextMaker::calc_width
// Access: Public
// Description: Returns the width in pixels of the indicated line of
// text.
////////////////////////////////////////////////////////////////////
INLINE int PNMTextMaker::
calc_width(const string &text) {
TextEncoder encoder;
encoder.set_text(text);
return calc_width(encoder.get_wtext());
}

View File

@ -63,13 +63,7 @@ PNMTextMaker::
int PNMTextMaker:: int PNMTextMaker::
generate_into(const wstring &text, PNMImage &dest_image, int x, int y) { generate_into(const wstring &text, PNMImage &dest_image, int x, int y) {
// First, measure the total width in pixels. // First, measure the total width in pixels.
int width = 0; int width = calc_width(text);
wstring::const_iterator ti;
for (ti = text.begin(); ti != text.end(); ++ti) {
int ch = (*ti);
PNMTextGlyph *glyph = get_glyph(ch);
width += glyph->get_advance();
}
int xp = x; int xp = x;
int yp = y; int yp = y;
@ -89,6 +83,7 @@ generate_into(const wstring &text, PNMImage &dest_image, int x, int y) {
} }
// Now place the text. // Now place the text.
wstring::const_iterator ti;
for (ti = text.begin(); ti != text.end(); ++ti) { for (ti = text.begin(); ti != text.end(); ++ti) {
int ch = (*ti); int ch = (*ti);
PNMTextGlyph *glyph = get_glyph(ch); PNMTextGlyph *glyph = get_glyph(ch);
@ -103,6 +98,24 @@ generate_into(const wstring &text, PNMImage &dest_image, int x, int y) {
return width; return width;
} }
////////////////////////////////////////////////////////////////////
// Function: PNMTextMaker::calc_width
// Access: Public
// Description: Returns the width in pixels of the indicated line of
// text.
////////////////////////////////////////////////////////////////////
int PNMTextMaker::
calc_width(const wstring &text) {
int width = 0;
wstring::const_iterator ti;
for (ti = text.begin(); ti != text.end(); ++ti) {
int ch = (*ti);
PNMTextGlyph *glyph = get_glyph(ch);
width += glyph->get_advance();
}
return width;
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: PNMTextMaker::get_glyph // Function: PNMTextMaker::get_glyph
// Access: Public // Access: Public

View File

@ -66,6 +66,8 @@ PUBLISHED:
PNMImage &dest_image, int x, int y); PNMImage &dest_image, int x, int y);
int generate_into(const wstring &text, int generate_into(const wstring &text,
PNMImage &dest_image, int x, int y); PNMImage &dest_image, int x, int y);
INLINE int calc_width(const string &text);
int calc_width(const wstring &text);
PNMTextGlyph *get_glyph(int character); PNMTextGlyph *get_glyph(int character);