From 96dbda26babefef2a2fadb81734779bb6ab87988 Mon Sep 17 00:00:00 2001 From: David Rose Date: Sun, 13 Dec 2009 07:53:30 +0000 Subject: [PATCH] calc_width --- panda/src/pnmtext/pnmTextMaker.I | 13 +++++++++++++ panda/src/pnmtext/pnmTextMaker.cxx | 27 ++++++++++++++++++++------- panda/src/pnmtext/pnmTextMaker.h | 2 ++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/panda/src/pnmtext/pnmTextMaker.I b/panda/src/pnmtext/pnmTextMaker.I index d36e94fcfa..0662d62255 100644 --- a/panda/src/pnmtext/pnmTextMaker.I +++ b/panda/src/pnmtext/pnmTextMaker.I @@ -137,3 +137,16 @@ generate_into(const string &text, PNMImage &dest_image, int x, int y) { encoder.set_text(text); 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()); +} diff --git a/panda/src/pnmtext/pnmTextMaker.cxx b/panda/src/pnmtext/pnmTextMaker.cxx index 3791451fe6..b1b3bb4435 100644 --- a/panda/src/pnmtext/pnmTextMaker.cxx +++ b/panda/src/pnmtext/pnmTextMaker.cxx @@ -63,13 +63,7 @@ PNMTextMaker:: int PNMTextMaker:: generate_into(const wstring &text, PNMImage &dest_image, int x, int y) { // First, measure the total width in pixels. - 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(); - } + int width = calc_width(text); int xp = x; int yp = y; @@ -89,6 +83,7 @@ generate_into(const wstring &text, PNMImage &dest_image, int x, int y) { } // Now place the text. + wstring::const_iterator ti; for (ti = text.begin(); ti != text.end(); ++ti) { int ch = (*ti); PNMTextGlyph *glyph = get_glyph(ch); @@ -103,6 +98,24 @@ generate_into(const wstring &text, PNMImage &dest_image, int x, int y) { 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 // Access: Public diff --git a/panda/src/pnmtext/pnmTextMaker.h b/panda/src/pnmtext/pnmTextMaker.h index ab1c97692d..89bc85c637 100644 --- a/panda/src/pnmtext/pnmTextMaker.h +++ b/panda/src/pnmtext/pnmTextMaker.h @@ -66,6 +66,8 @@ PUBLISHED: PNMImage &dest_image, int x, int y); int generate_into(const wstring &text, 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);