[reportlab-users] canvas.stringWidth

Tim Roberts reportlab-users@reportlab.com
Wed, 08 Jan 2003 11:15:23 -0800


The most common need by far for canvas.stringWidth is to find the width in 
the current font & size.  A quick grep through the reportlab source code 
shows this quite clearly.  Given that, is there a good reason not to add a 
shortcut, as in the following patch to pfdgen/canvas.py, so that we can call:

  x = self.canvas.stringWidth("xxx")

in that case?

*** canvas.py	Wed Nov 06 03:32:42 2002
--- canvas.py.1	Wed Jan 08 11:08:46 2003
***************
*** 1136,1143 ****
              pdffontname = self._doc.getInternalFontName(psfontname)
              self._code.append('BT %s %s Tf %s TL ET' % (pdffontname, fp_str
(size), fp_str(leading)))
  
!     def stringWidth(self, text, fontName, fontSize, encoding=None):
          "gets width of a string in the given font and size"
          if encoding is not None:
              import logger
              logger.warnOnce('encoding argument to Canvas.stringWidth is 
deprecated and has no effect!')
--- 1136,1145 ----
              pdffontname = self._doc.getInternalFontName(psfontname)
              self._code.append('BT %s %s Tf %s TL ET' % (pdffontname, fp_str
(size), fp_str(leading)))
  
!     def stringWidth(self, text, fontName=None, fontSize=None, 
encoding=None):
          "gets width of a string in the given font and size"
+         if fontName is None: fontName = self._fontname
+         if fontSize is None: fontSize = self._fontsize
          if encoding is not None:
              import logger
              logger.warnOnce('encoding argument to Canvas.stringWidth is 
deprecated and has no effect!')

--
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.