[reportlab-users] Marius' TTFontFile breaks in 2.2

Marius Gedminas reportlab-users@reportlab.com
Tue, 28 May 2002 14:18:11 +0200


On Mon, May 27, 2002 at 07:33:36PM +0100, Robin Becker wrote:
> Hi, we're trying to get a release out this week and I'm having problems
> with the Checksum for Marius' TTFont check sum. I suspect that the
> Python 2.2 changes to int/long in the following code are causing the
> problem.
> 
> 
>         checkSum = calcChecksum(self._data)
>         checkSum = _add32(0xB1B0AFBA, -checkSum)

Yes, I just tried python 2.2 and it breaks TTFontFileTestCase.testAdd32
in my test suite.

If somebody know how to achieve efficient arithmetic modulo 2**32 in
Python, please tell me.  This looks really gross:

> def _add32(x, y):
>     "Calculate (x + y) modulo 2**32"
>     try:
>         return x + y
>     except OverflowError:
>         lo = (x & 0xFFFF) + (y & 0xFFFF)
>         hi = (x >> 16) + (y >> 16) + (lo >> 16)
>         return (hi << 16) | (lo & 0xFFFF)

and breaks under Python 2.2.

BTW calcChecksum could look like

> def calcChecksum(stream):
>     """Calculates PDF-style checksums"""
>     if len(stream) & 3:
>         stream = stream + "\0\0\0"
>         stream = stream[:len(stream)&~3]
>     sum = 0
>     for n in unpack(">%dl" % (len(stream)/4), stream):

         sum = _add32(sum, n)

>     return sum

I just was afraid that a function call for every 4 bytes of the output
(twice) was too much overhead.  After profiling my first prototype I
noticed that 40% of the time was spent in calcChecksum (which was
implemented quite differently).  I haven't yet found the time to profile
this code.

Perhaps the best way out of here would be to make calcChecksum a C
function?

Marius Gedminas
-- 
If "con" is the opposite of "pro", then what is the opposite of progress?