Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control "f0" in RTF file from Mac OS X could not resolve charset. #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyth/encodings/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ def search(name):
return info
return None

codecs.register(search)
#codecs.register(search)
23 changes: 14 additions & 9 deletions pyth/plugins/rtf15/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,20 @@ def handle_f(self, fontNum):
if 'FONT_TABLE' in (self.parent.specialMeaning, self.specialMeaning):
self.fontNum = int(fontNum)
self._setFontCharset()
elif self.charsetTable is not None:
try:
self.charset = self.charsetTable[int(fontNum)]
except KeyError:
# fontNum not found in charsetTable, ignore if requested
if self.reader.errors == 'ignore':
pass
else:
raise
else:
charsetTable = self.charsetTable
if not charsetTable:
charsetTable = self.reader.charsetTable

if charsetTable is not None:
try:
self.charset = charsetTable[int(fontNum)]
except KeyError:
# fontNum not found in charsetTable, ignore if requested
if self.reader.errors == 'ignore':
pass
else:
raise

def handle_fcharset(self, charsetNum):
if 'FONT_TABLE' in (self.parent.specialMeaning, self.specialMeaning):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

setup(name="pyth",
version="0.6.0",
version="0.6.1",
packages = find_packages(),
zip_safe = False,

Expand Down
36 changes: 36 additions & 0 deletions tests/test_readosxrtf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# -*- coding: UTF-8 -*-
"""
Unit tests of the rtf15 reader.
"""

import unittest

from pyth.plugins.rtf15.reader import Rtf15Reader
from pyth.plugins.plaintext.writer import PlaintextWriter
from StringIO import StringIO

class TestReadOSXRtf(unittest.TestCase):

def test_read(self):
rtf = StringIO("""{\\rtf1\\ansi\\ansicpg1252\\cocoartf1343\\cocoasubrtf160\\cocoascreenfonts1{\\fonttbl\\f0\\fnil\\fcharset222 Thonburi;}
{\\colortbl;\\red255\\green255\\blue255;}
\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc

\\f0\\fs24 \\cf0 \\'b9\\'e9\\'d3\\'b5\\'a1""")
doc = Rtf15Reader.read(rtf)
text = PlaintextWriter.write(doc).read()
print text
self.assertEquals(u"น้ำตก", text.decode('utf8'))


def test_read2(self):
rtf = StringIO("""{\\rtf1\\ansi\\ansicpg1252\\cocoartf1343\\cocoasubrtf160\\cocoascreenfonts1{\\fonttbl\\f0\\fnil\\fcharset222 Thonburi;}
{\\colortbl;\\red255\\green255\\blue255;}
\\pard\\tx560\\tx1120\\tx1680\\tx2240\\tx2800\\tx3360\\tx3920\\tx4480\\tx5040\\tx5600\\tx6160\\tx6720\\pardirnatural\\qc

{\\f0\\fs24 \\cf0 \\'b9\\'e9\\'d3\\'b5\\'a1}""")
doc = Rtf15Reader.read(rtf)
text = PlaintextWriter.write(doc).read()
print text
self.assertEquals(u"น้ำตก", text.decode('utf8'))