jython25-reprotlab: checkin some mods for ttf jython25
authorrgbecker
Tue, 24 Feb 2009 16:55:25 +0000
branchjython25
changeset 3123 e53425121cc4
parent 3119 b9e540d1267a
jython25-reprotlab: checkin some mods for ttf
src/reportlab/lib/arciv.py
src/reportlab/lib/utils.py
src/reportlab/pdfbase/ttfonts.py
src/rl_addons/rl_accel/_rl_accel.java
--- a/src/reportlab/lib/arciv.py	Tue Feb 17 17:32:00 2009 +0000
+++ b/src/reportlab/lib/arciv.py	Tue Feb 24 16:55:25 2009 +0000
@@ -44,7 +44,7 @@
 		'''
 		sbox, i, j = self._sbox, self._i, self._j
 
-		C = type(B) is StringType and map(ord,B) or B[:]
+		C = isinstance(B,(str,unicode)) and map(ord,B) or B[:]
 		n = len(C)
 		p = 0
 		while p<n:
--- a/src/reportlab/lib/utils.py	Tue Feb 17 17:32:00 2009 +0000
+++ b/src/reportlab/lib/utils.py	Tue Feb 24 16:55:25 2009 +0000
@@ -356,6 +356,8 @@
     if haveImages: del Image
 
 try:
+    #remove after 2.5b2
+    if jython: raise ImportError('jython cStringIO is bad')
     from cStringIO import StringIO as __StringIO
 except ImportError:
     from StringIO import StringIO as __StringIO
--- a/src/reportlab/pdfbase/ttfonts.py	Tue Feb 17 17:32:00 2009 +0000
+++ b/src/reportlab/pdfbase/ttfonts.py	Tue Feb 24 16:55:25 2009 +0000
@@ -55,7 +55,7 @@
 import string
 from types import StringType, UnicodeType
 from struct import pack, unpack
-from cStringIO import StringIO
+from reportlab.lib.utils import getStringIO
 from reportlab.pdfbase import pdfmetrics, pdfdoc
 
 class TTFError(pdfdoc.PDFError):
@@ -357,7 +357,8 @@
 
     def makeStream(self):
         "Finishes the generation and returns the TTF file as a string"
-        stm = StringIO()
+        stm = getStringIO()
+        write = stm.write
 
         numTables = len(self.tables)
         searchRange = 1
@@ -369,7 +370,7 @@
         rangeShift = numTables * 16 - searchRange
 
         # Header
-        stm.write(pack(">lHHHH", 0x00010000, numTables, searchRange,
+        write(pack(">lHHHH", 0x00010000, numTables, searchRange,
                                  entrySelector, rangeShift))
 
         # Table directory
@@ -380,20 +381,20 @@
             if tag == 'head':
                 head_start = offset
             checksum = calcChecksum(data)
-            stm.write(tag)
-            stm.write(pack(">LLL", checksum, offset, len(data)))
+            write(tag)
+            write(pack(">LLL", checksum, offset, len(data)))
             paddedLength = (len(data)+3)&~3
             offset = offset + paddedLength
 
         # Table data
         for tag, data in tables:
-            data = data + "\0\0\0"
-            stm.write(data[:len(data)&~3])
+            data += "\0\0\0"
+            write(data[:len(data)&~3])
 
         checksum = calcChecksum(stm.getvalue())
         checksum = add32(0xB1B0AFBAL, -checksum)
         stm.seek(head_start + 8)
-        stm.write(pack('>L', checksum))
+        write(pack('>L', checksum))
 
         return stm.getvalue()
 
--- a/src/rl_addons/rl_accel/_rl_accel.java	Tue Feb 17 17:32:00 2009 +0000
+++ b/src/rl_addons/rl_accel/_rl_accel.java	Tue Feb 24 16:55:25 2009 +0000
@@ -3,48 +3,48 @@
 import java.io.*;
 
 public class _rl_accel{
-    public static String version = "0.30";
-    private static String[] formats = {"#", "#.#", "#.##", "#.###", "#.####", "#.#####", "#.######"};
+	public static String version = "0.31";
+	private static String[] formats = {"#", "#.#", "#.##", "#.###", "#.####", "#.#####", "#.######"};
 
-    public static Object fp_str(PyObject[] args){
-        StringBuffer buffer = new StringBuffer();
+	public static Object fp_str(PyObject[] args){
+		StringBuffer buffer = new StringBuffer();
 
-        if(args.length==1 && args[0] instanceof PySequence){
-            //iterate through the first element
-            PySequence seq = (PySequence)args[0];
-            for(int i=0;i<seq.__len__();i++){
-                format(buffer, seq.__getitem__(i).toString());
-            }
-        }
-        else{
-            //iterate through the args list
-            for(int i=0;i<args.length;i++){
-                format(buffer, args[i].toString());
-            }
-        }
-        return buffer.toString().trim();
-    }
+		if(args.length==1 && args[0] instanceof PySequence){
+			//iterate through the first element
+			PySequence seq = (PySequence)args[0];
+			for(int i=0;i<seq.__len__();i++){
+				format(buffer, seq.__getitem__(i).toString());
+			}
+		}
+		else{
+			//iterate through the args list
+			for(int i=0;i<args.length;i++){
+				format(buffer, args[i].toString());
+			}
+		}
+		return new PyString(buffer.toString().trim());
+	}
 
-    private static double log_e_10 = Math.log(10.0);
+	private static double log_e_10 = Math.log(10.0);
 
-    private static void format(StringBuffer buffer, String a){
-        String num;
-        int l;
-        double d = Double.parseDouble(a);
-        if(Math.abs(d)<=1.0e-7) num = "0";
-        else{
-            if(Math.abs(d)>1.0)
-                l = (Math.min(Math.max(0,(6-(int)(Math.log(Math.abs(d))/log_e_10))),6));
-            else l = 6;
-            NumberFormat formatter = new DecimalFormat(formats[l]);
-            num = formatter.format(d);
-            if(num.startsWith("0")&&(num.length()>1))
-                num = num.substring(1);
-        }
-        buffer.append(num).append(' ');
-    }
+	private static void format(StringBuffer buffer, String a){
+		String num;
+		int l;
+		double d = Double.parseDouble(a);
+		if(Math.abs(d)<=1.0e-7) num = "0";
+		else{
+			if(Math.abs(d)>1.0)
+				l = (Math.min(Math.max(0,(6-(int)(Math.log(Math.abs(d))/log_e_10))),6));
+			else l = 6;
+			NumberFormat formatter = new DecimalFormat(formats[l]);
+			num = formatter.format(d);
+			if(num.startsWith("0")&&(num.length()>1))
+				num = num.substring(1);
+		}
+		buffer.append(num).append(' ');
+	}
 
-	public static String escapePDF(String text){
+	public static PyString escapePDF(String text){
 		int textlen = text.length();
 		StringBuffer out = new StringBuffer(textlen*4);
 		int i=0;
@@ -67,11 +67,10 @@
 				out.append(c);
 			}
 		}
-
-		return out.toString();
+		return new PyString(out.toString());
 	}
 
-	public static String _instanceEscapePDF(String text){
+	public static PyString _instanceEscapePDF(String text){
 		return escapePDF(text);
 	}
 
@@ -81,7 +80,7 @@
 	static long a85_3 = 614125;
 	static long a85_4 = 52200625;
 
-	public static String _AsciiBase85Encode(String inData){
+	public static PyString _AsciiBase85Encode(String inData){
 		int	length = inData.length();
 		int blocks, extra, i, k, lim;
 		long block, res;
@@ -145,68 +144,66 @@
 
 		buf.append('~');
 		buf.append('>');
-		return buf.toString();
+		return new PyString(buf.toString());
 	}
-    public static final boolean isWhitespace(int ch){
-        return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32);
-    	}
-	public static String _AsciiBase85Decode(String inData){
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        int state = 0;
-        int chn[] = new int[5];
-	int len = inData.length();
-        for(int k = 0; k<len; ++k){
-            int ch = ((int)inData.charAt(k)) & 0xff;
-            if(ch == '~') break;
-            if(isWhitespace(ch)) continue;
-            if (ch=='z' && state==0){
-                out.write(0);
-                out.write(0);
-                out.write(0);
-                out.write(0);
-                continue;
-            	}
-            if (ch < '!' || ch > 'u') throw new RuntimeException("Illegal character in _AsciiBase85Decode.");
-            chn[state] = ch - '!';
-            ++state;
-            if(state == 5){
-                state = 0;
-                int r = 0;
-                for (int j = 0; j < 5; ++j) r = r*85 + chn[j];
-                out.write((byte)(r>>24));
-                out.write((byte)(r>>16));
-                out.write((byte)(r>>8));
-                out.write((byte)r);
-            	}
-        	}
-        long r;
-        if(state==1) throw new RuntimeException("Illegal length in _AsciiBase85Decode.");
-        if (state == 2) {
-            r = ((chn[0]*85+ chn[1])*85*85*85 + 0xfffff)&0x0ffffffffL;
-            out.write((byte)(r>>24));
-        }
-        else if (state == 3) {
-            r = (((chn[0]*85 + chn[1])*85 + chn[2])*85*85+0xffff)&0x0ffffffffL;
-            out.write((byte)(r >> 24));
-            out.write((byte)(r >> 16));
-        }
-        else if (state == 4) {
-            r = ((((chn[0]*85 + chn[1])*85 + chn[2])*85 + chn[3])*85+0xff)&0x0ffffffffL;
-            out.write((byte)(r >> 24));
-            out.write((byte)(r >> 16));
-            out.write((byte)(r >> 8));
-        }
-        return out.toString();
+	public static final boolean isWhitespace(int ch){
+		return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32);
 		}
-		
-		
+	public static PyString _AsciiBase85Decode(String inData){
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		int state = 0;
+		int chn[] = new int[5];
+		int len = inData.length();
+		for(int k = 0; k<len; ++k){
+			int ch = ((int)inData.charAt(k)) & 0xff;
+			if(ch == '~') break;
+			if(isWhitespace(ch)) continue;
+			if (ch=='z' && state==0){
+				out.write(0);
+				out.write(0);
+				out.write(0);
+				out.write(0);
+				continue;
+				}
+			if (ch < '!' || ch > 'u') throw new RuntimeException("Illegal character in _AsciiBase85Decode.");
+			chn[state] = ch - '!';
+			++state;
+			if(state == 5){
+				state = 0;
+				int r = 0;
+				for (int j = 0; j < 5; ++j) r = r*85 + chn[j];
+				out.write((byte)(r>>24));
+				out.write((byte)(r>>16));
+				out.write((byte)(r>>8));
+				out.write((byte)r);
+				}
+			}
+		long r;
+		if(state==1) throw new RuntimeException("Illegal length in _AsciiBase85Decode.");
+		if (state == 2) {
+			r = ((chn[0]*85+ chn[1])*85*85*85 + 0xfffff)&0x0ffffffffL;
+			out.write((byte)(r>>24));
+		}
+		else if (state == 3) {
+			r = (((chn[0]*85 + chn[1])*85 + chn[2])*85*85+0xffff)&0x0ffffffffL;
+			out.write((byte)(r >> 24));
+			out.write((byte)(r >> 16));
+		}
+		else if (state == 4) {
+			r = ((((chn[0]*85 + chn[1])*85 + chn[2])*85 + chn[3])*85+0xff)&0x0ffffffffL;
+			out.write((byte)(r >> 24));
+			out.write((byte)(r >> 16));
+			out.write((byte)(r >> 8));
+		}
+		return new PyString(out.toString());
+		}
 	public static int calcChecksum(String data){
 		int	 dl = data.length();
 		int i;
 		long sum = 0L;
 		long n;
 		int leftover;
-		
+
 		/*full ULONGs*/
 		for(i=0;(dl-i)>=4;i+=4){
 			n = ((int)data.charAt(i)) << 24;
@@ -215,7 +212,7 @@
 			n += (int)data.charAt(i+3);
 			sum += n;
 			}
-	
+
 		/*pad with zeros*/
 		leftover = dl & 3;
 		if(leftover>0){
@@ -224,8 +221,6 @@
 			if (leftover>2) n += ((int)data.charAt(i+2)) << 8;
 			sum += n;
 			}
-	
 		return (int)sum;
 	}
-		
 }