Browse Source

calc_width

David Rose 16 years ago
parent
commit
96dbda26ba

+ 13 - 0
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);
   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());
+}

+ 20 - 7
panda/src/pnmtext/pnmTextMaker.cxx

@@ -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;
-  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 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

+ 2 - 0
panda/src/pnmtext/pnmTextMaker.h

@@ -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);