Skip to content

Commit

Permalink
remove no print glyphs
Browse files Browse the repository at this point in the history
  • Loading branch information
v0lk3r committed Sep 6, 2011
1 parent e7e68aa commit a2dbb98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
13 changes: 13 additions & 0 deletions upstream-src/src/reportlab/lib/utils.py
Expand Up @@ -1189,4 +1189,17 @@ def escapeOnce(data):
data = data.replace(">", ">")
data = data.replace("<", "<")
return data

import re

# 'Other, Format' http://www.fileformat.info/info/unicode/category/Cf/list.htm
noprint_chars = [unichr(int(c)) for c in '173 1536 1537 1538 1539 1757 1807 6068 6069 8203 8204 8205 8206 8207 8234 8235 8236 8237 8238 8288 8289 8290 8291 8292 8298 8299 8300 8301 8302 8303 65279 65529 65530 65531'.split()]

repl_regex = re.compile('|'.join(noprint_chars))

def remove_noprint(s):
if isinstance(s, str):
return repl_regex.sub('', s.decode('utf-8')).encode('utf-8')
else:
return repl_regex.sub('', s)

6 changes: 2 additions & 4 deletions upstream-src/src/reportlab/pdfbase/pdfmetrics.py
Expand Up @@ -22,7 +22,7 @@
from types import StringType, ListType, TupleType
from reportlab.pdfbase import _fontdata
from reportlab.lib.logger import warnOnce
from reportlab.lib.utils import rl_isfile, rl_glob, rl_isdir, open_and_read, open_and_readlines, findInPaths
from reportlab.lib.utils import rl_isfile, rl_glob, rl_isdir, open_and_read, open_and_readlines, findInPaths, remove_noprint
from reportlab import rl_config
defaultEncoding = rl_config.defaultEncoding
T1SearchPath = rl_config.T1SearchPath
Expand Down Expand Up @@ -732,9 +732,7 @@ def stringWidth(text, fontName, fontSize, encoding='utf8'):
"""Compute width of string in points;
not accelerated as fast enough because of _instanceStringWidthU"""
text = log2vis(text, DIR_RTL if rl_config.rtl else DIR_LTR)
rpl = unichr(65279)
rpl = rpl.encode('utf-8') if isinstance(text, str) else rpl
text = text.replace(rpl, '') #ignore zero width no-break space
text = remove_noprint(text)
return getFont(fontName).stringWidth(text, fontSize, encoding=encoding)

try:
Expand Down
6 changes: 2 additions & 4 deletions upstream-src/src/reportlab/pdfgen/textobject.py
Expand Up @@ -12,7 +12,7 @@
import string
from types import *
from reportlab.lib.colors import Color, CMYKColor, CMYKColorSep, toColor, black, white, _CMYK_black, _CMYK_white
from reportlab.lib.utils import fp_str
from reportlab.lib.utils import fp_str, remove_noprint
from reportlab.pdfbase import pdfmetrics

# try to import pyfribidi
Expand Down Expand Up @@ -363,9 +363,7 @@ def _formatText(self, text):
# Use pyfribidi to write the text in the correct visual order.
directions = { 'LTR': DIR_LTR, 'RTL': DIR_RTL }
text = log2vis(text, directions.get(self.direction, DIR_ON), reordernsm=False)
rpl = unichr(65279)
rpl = rpl.encode('utf-8') if isinstance(text, str) else rpl
text = text.replace(rpl, '') #ignore zero width no-break space
text = remove_noprint(text)
canv = self._canvas
font = pdfmetrics.getFont(self._fontname)
R = []
Expand Down

0 comments on commit a2dbb98

Please sign in to comment.