| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifdef Py_LIMITED_API |
| 8 |
# define RLPyBytes_Size PyBytes_Size |
| 9 |
# define RLPyTuple_SetItem PyTuple_SetItem |
| 10 |
# define RLPyList_SetItem PyList_SetItem |
| 11 |
# define RLPyList_Size PyList_Size |
| 12 |
# define RLPyFloat_AsDouble PyFloat_AsDouble |
| 13 |
# define RLPyBytes_AsString PyBytes_AsString |
| 14 |
# define RLPyUnicode_GetLength PyUnicode_GetLength |
| 15 |
#else |
| 16 |
# define RLPyBytes_Size PyBytes_GET_SIZE |
| 17 |
# define RLPyTuple_SetItem PyTuple_SET_ITEM |
| 18 |
# define RLPyList_SetItem PyList_SET_ITEM |
| 19 |
# define RLPyList_Size PyList_GET_SIZE |
| 20 |
# define RLPyFloat_AsDouble PyFloat_AS_DOUBLE |
| 21 |
# define RLPyBytes_AsString PyBytes_AS_STRING |
| 22 |
# define RLPyUnicode_GetLength PyUnicode_GET_LENGTH |
| 23 |
#endif |
| 24 |
#define PY_SSIZE_T_CLEAN |
| 25 |
#include "Python.h" |
| 26 |
#include <stdlib.h> |
| 27 |
#include <math.h> |
| 28 |
|
| 29 |
PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...); |
| 30 |
#define DEFERRED_ADDRESS(A) 0 |
| 31 |
#if defined(__GNUC__) || defined(sun) || defined(_AIX) || defined(__hpux) |
| 32 |
# define STRICMP strcasecmp |
| 33 |
#elif defined(_MSC_VER) |
| 34 |
# define STRICMP stricmp |
| 35 |
#elif defined(macintosh) |
| 36 |
# include <extras.h> |
| 37 |
# define strdup _strdup |
| 38 |
# define STRICMP _stricmp |
| 39 |
#else |
| 40 |
# error "Don't know how to define STRICMP" |
| 41 |
#endif |
| 42 |
#ifndef max |
| 43 |
# define max(a,b) ((a)>(b)?(a):(b)) |
| 44 |
#endif |
| 45 |
#ifndef min |
| 46 |
# define min(a,b) ((a)<(b)?(a):(b)) |
| 47 |
#endif |
| 48 |
#define VERSION "0.9.1" |
| 49 |
#define MODULE "_rl_accel" |
| 50 |
|
| 51 |
#define STRNAME "str" |
| 52 |
#define BYTESNAME "bytes" |
| 53 |
static void ModifyExcValue(PyObject *exc,const char *funcname,int lineno,const char* fmt,va_list ap) |
| 54 |
{ |
| 55 |
PyObject *type = NULL, *value = NULL, *tb = NULL, *uval=NULL; |
| 56 |
PyErr_Fetch(&type, &value, &tb); |
| 57 |
PyErr_NormalizeException(&type, &value, &tb); |
| 58 |
if(PyErr_Occurred()) goto L_BAD; |
| 59 |
uval = PyUnicode_FromFormatV(fmt,ap); |
| 60 |
if(uval){ |
| 61 |
PyErr_Format(exc,"%U in %s @ %s:%d\ncaused by %S",uval,funcname,__FILE__,lineno,value); |
| 62 |
} |
| 63 |
else { |
| 64 |
PyErr_Format(exc,"in %s:%d\ncaused by %S",funcname,__FILE__,lineno,value); |
| 65 |
} |
| 66 |
|
| 67 |
L_exit: |
| 68 |
Py_XDECREF(uval); |
| 69 |
Py_XDECREF(type); |
| 70 |
Py_XDECREF(value); |
| 71 |
Py_XDECREF(tb); |
| 72 |
return; |
| 73 |
L_BAD: |
| 74 |
if(type && value){ |
| 75 |
PyErr_Restore(type,value,tb); |
| 76 |
type = value = tb = NULL; |
| 77 |
} |
| 78 |
goto L_exit; |
| 79 |
} |
| 80 |
|
| 81 |
static void _excAddInfo(const char* funcname,int lineno, PyObject *exc, const char* fmt, va_list ap) |
| 82 |
{ |
| 83 |
PyObject *uval = NULL; |
| 84 |
if(PyErr_Occurred()){ |
| 85 |
ModifyExcValue(exc,funcname,lineno,fmt,ap); |
| 86 |
} |
| 87 |
else{ |
| 88 |
uval = PyUnicode_FromFormatV(fmt,ap); |
| 89 |
if(uval) { |
| 90 |
PyErr_Format(exc,"in %s@%s:%d %U",funcname,__FILE__,lineno,uval); |
| 91 |
Py_DECREF(uval); |
| 92 |
} |
| 93 |
else |
| 94 |
PyErr_Format(exc,"in %s@%s:%d",funcname,__FILE__,lineno,uval); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void excAddInfo(const char* funcname,int lineno, PyObject *exc, const char* fmt, ...) |
| 99 |
{ |
| 100 |
va_list ap; |
| 101 |
va_start(ap,fmt); |
| 102 |
_excAddInfo(funcname,lineno,exc,fmt,ap); |
| 103 |
va_end(ap); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#define FIRST(...) FIRST_HELPER(__VA_ARGS__, throwaway) |
| 110 |
#define FIRST_HELPER(first, ...) first |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#define REST(...) REST_HELPER(NUM(__VA_ARGS__), __VA_ARGS__) |
| 118 |
#define REST_HELPER(qty, ...) REST_HELPER2(qty, __VA_ARGS__) |
| 119 |
#define REST_HELPER2(qty, ...) REST_HELPER_##qty(__VA_ARGS__) |
| 120 |
#define REST_HELPER_ONE(first) |
| 121 |
#define REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__ |
| 122 |
#define NUM(...) \ |
| 123 |
SELECT_10TH(__VA_ARGS__, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\ |
| 124 |
TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway) |
| 125 |
#define SELECT_10TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, ...) a10 |
| 126 |
#define EXC_SET(exc,...) do{excAddInfo(__func__,__LINE__,exc,FIRST(__VA_ARGS__) REST(__VA_ARGS__));}while(0) |
| 127 |
#define EXC_EXIT(exc,...) do{EXC_SET(exc,FIRST(__VA_ARGS__) REST(__VA_ARGS__));goto L_exit;}while(0) |
| 128 |
#define MODULE_STATE_SIZE 0 |
| 129 |
|
| 130 |
#define a85_0 1L |
| 131 |
#define a85_1 85L |
| 132 |
#define a85_2 7225L |
| 133 |
#define a85_3 614125L |
| 134 |
#define a85_4 52200625L |
| 135 |
|
| 136 |
PyObject *_a85_encode(PyObject *module, PyObject *args) |
| 137 |
{ |
| 138 |
unsigned char *inData; |
| 139 |
Py_ssize_t length, blocks, extra; |
| 140 |
int i, k, lim; |
| 141 |
unsigned long block, res; |
| 142 |
char *buf; |
| 143 |
PyObject *retVal=NULL, *inObj, *_o1=NULL; |
| 144 |
if(!PyArg_ParseTuple(args, "O", &inObj)) return NULL; |
| 145 |
if(PyUnicode_Check(inObj)){ |
| 146 |
_o1 = PyUnicode_AsLatin1String(inObj); |
| 147 |
if(!_o1){ |
| 148 |
EXC_EXIT(PyExc_ValueError,"argument not decodable as latin1"); |
| 149 |
} |
| 150 |
inData = (unsigned char*)PyBytes_AsString(_o1); |
| 151 |
inObj = _o1; |
| 152 |
if(!inData){ |
| 153 |
EXC_EXIT(PyExc_ValueError,"argument not converted to internal char string"); |
| 154 |
} |
| 155 |
} |
| 156 |
else if(!PyBytes_Check(inObj)){ |
| 157 |
EXC_EXIT(PyExc_ValueError,"argument should be " BYTESNAME " or latin1 decodable " STRNAME); |
| 158 |
} |
| 159 |
inData = (unsigned char*)PyBytes_AsString(inObj); |
| 160 |
length = RLPyBytes_Size(inObj); |
| 161 |
|
| 162 |
blocks = length / 4; |
| 163 |
extra = length % 4; |
| 164 |
|
| 165 |
buf = (char*)malloc((blocks+1)*5+3); |
| 166 |
lim = 4*(int)blocks; |
| 167 |
|
| 168 |
for(k=i=0; i<lim; i += 4){ |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
block = ((unsigned long)inData[i]<<24)|((unsigned long)inData[i+1]<<16) |
| 174 |
|((unsigned long)inData[i+2]<<8)|((unsigned long)inData[i+3]); |
| 175 |
if (block == 0) buf[k++] = 'z'; |
| 176 |
else { |
| 177 |
res = block/a85_4; |
| 178 |
buf[k++] = (char)(res+33); |
| 179 |
block -= res*a85_4; |
| 180 |
|
| 181 |
res = block/a85_3; |
| 182 |
buf[k++] = (char)(res+33); |
| 183 |
block -= res*a85_3; |
| 184 |
|
| 185 |
res = block/a85_2; |
| 186 |
buf[k++] = (char)(res+33); |
| 187 |
block -= res*a85_2; |
| 188 |
|
| 189 |
res = block / a85_1; |
| 190 |
buf[k++] = (char)(res+33); |
| 191 |
|
| 192 |
buf[k++] = (char)(block-res*a85_1+33); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if(extra>0){ |
| 197 |
block = 0L; |
| 198 |
|
| 199 |
for (i=0; i<extra; i++) |
| 200 |
block += (unsigned long)inData[length-extra+i] << (24-8*i); |
| 201 |
|
| 202 |
res = block/a85_4; |
| 203 |
buf[k++] = (char)(res+33); |
| 204 |
if(extra>=1){ |
| 205 |
block -= res*a85_4; |
| 206 |
|
| 207 |
res = block/a85_3; |
| 208 |
buf[k++] = (char)(res+33); |
| 209 |
if(extra>=2){ |
| 210 |
block -= res*a85_3; |
| 211 |
|
| 212 |
res = block/a85_2; |
| 213 |
buf[k++] = (char)(res+33); |
| 214 |
if(extra>=3) buf[k++] = (char)((block-res*a85_2)/a85_1+33); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
buf[k++] = '~'; |
| 220 |
buf[k++] = '>'; |
| 221 |
retVal = PyUnicode_FromStringAndSize(buf, k); |
| 222 |
free(buf); |
| 223 |
if(!retVal){ |
| 224 |
EXC_EXIT(PyExc_ValueError,"failed to create return " STRNAME " value" ); |
| 225 |
} |
| 226 |
L_exit: |
| 227 |
Py_XDECREF(_o1); |
| 228 |
return retVal; |
| 229 |
} |
| 230 |
|
| 231 |
PyObject *_a85_decode(PyObject *module, PyObject *args) |
| 232 |
{ |
| 233 |
unsigned char *inData, *p, *q, *tmp, *buf; |
| 234 |
unsigned int length, blocks, extra, k, num, c1, c2, c3, c4, c5; |
| 235 |
static unsigned pad[] = {0,0,0xffffff,0xffff,0xff}; |
| 236 |
PyObject *retVal=NULL, *inObj, *_o1=NULL; |
| 237 |
if(!PyArg_ParseTuple(args, "O", &inObj)) return NULL; |
| 238 |
if(PyUnicode_Check(inObj)){ |
| 239 |
_o1 = PyUnicode_AsLatin1String(inObj); |
| 240 |
if(!_o1){ |
| 241 |
EXC_EXIT(PyExc_ValueError,"argument not decodable as latin1"); |
| 242 |
} |
| 243 |
inData = (unsigned char*)PyBytes_AsString(_o1); |
| 244 |
inObj = _o1; |
| 245 |
if(!inData){ |
| 246 |
EXC_EXIT(PyExc_ValueError,"argument not converted to internal char string"); |
| 247 |
} |
| 248 |
} |
| 249 |
else if(!PyBytes_Check(inObj)){ |
| 250 |
EXC_EXIT(PyExc_ValueError,"argument should be " BYTESNAME " or latin1 decodable " STRNAME); |
| 251 |
} |
| 252 |
inData = (unsigned char*)PyBytes_AsString(inObj); |
| 253 |
length = (unsigned int)RLPyBytes_Size(inObj); |
| 254 |
for(k=0,q=inData, p=q+length;q<p && (q=(unsigned char*)strchr((const char*)q,'z'));k++, q++); |
| 255 |
length += k*4; |
| 256 |
tmp = q = (unsigned char*)malloc(length+1); |
| 257 |
while(inData<p && (k = *inData++)){ |
| 258 |
if(isspace(k)) continue; |
| 259 |
if(k=='z'){ |
| 260 |
|
| 261 |
memcpy(q,"!!!!!",5); |
| 262 |
q += 5; |
| 263 |
} |
| 264 |
else |
| 265 |
*q++ = k; |
| 266 |
} |
| 267 |
inData = tmp; |
| 268 |
length = (unsigned int)(q - inData); |
| 269 |
buf = inData+length-2; |
| 270 |
if(buf[0]!='~' || buf[1]!='>'){ |
| 271 |
free(inData); |
| 272 |
EXC_EXIT(PyExc_ValueError, "Invalid terminator for Ascii Base 85 Stream"); |
| 273 |
} |
| 274 |
length -= 2; |
| 275 |
buf[0] = 0; |
| 276 |
|
| 277 |
blocks = length / 5; |
| 278 |
extra = length % 5; |
| 279 |
|
| 280 |
buf = (unsigned char*)malloc((blocks+1)*4); |
| 281 |
q = inData+blocks*5; |
| 282 |
for(k=0;inData<q;inData+=5){ |
| 283 |
c1 = inData[0]-33; |
| 284 |
c2 = inData[1]-33; |
| 285 |
c3 = inData[2]-33; |
| 286 |
c4 = inData[3]-33; |
| 287 |
c5 = inData[4]-33; |
| 288 |
num = (((c1*85+c2)*85+c3)*85+c4)*85+c5; |
| 289 |
buf[k++] = num>>24; |
| 290 |
buf[k++] = num>>16; |
| 291 |
buf[k++] = num>>8; |
| 292 |
buf[k++] = num; |
| 293 |
} |
| 294 |
if(extra>1){ |
| 295 |
c1 = inData[0]-33; |
| 296 |
c2 = extra>=2 ? inData[1]-33: 0; |
| 297 |
c3 = extra>=3 ? inData[2]-33: 0; |
| 298 |
c4 = extra>=4 ? inData[3]-33: 0; |
| 299 |
c5 = 0; |
| 300 |
num = (((c1*85+c2)*85+c3)*85+c4)*85+c5 + pad[extra]; |
| 301 |
if(extra>1){ |
| 302 |
buf[k++] = num>>24; |
| 303 |
if(extra>2){ |
| 304 |
buf[k++] = num>>16; |
| 305 |
if(extra>3){ |
| 306 |
buf[k++] = num>>8; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
retVal = PyBytes_FromStringAndSize((const char*)buf, k); |
| 312 |
free(buf); |
| 313 |
free(tmp); |
| 314 |
if(!retVal){ |
| 315 |
EXC_EXIT(PyExc_ValueError,"failed to create return " BYTESNAME " value" ); |
| 316 |
} |
| 317 |
L_exit: |
| 318 |
Py_XDECREF(_o1); |
| 319 |
return retVal; |
| 320 |
} |
| 321 |
|
| 322 |
static char* _fp_fmts[]={"%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f"}; |
| 323 |
static char *_fp_one(PyObject* module,PyObject *pD) |
| 324 |
{ |
| 325 |
double d, ad; |
| 326 |
static char s[30]; |
| 327 |
int l; |
| 328 |
char* dot; |
| 329 |
PyObject *cD; |
| 330 |
if((cD=PyNumber_Float(pD))){ |
| 331 |
d = RLPyFloat_AsDouble(cD); |
| 332 |
Py_DECREF(cD); |
| 333 |
} |
| 334 |
else { |
| 335 |
EXC_SET(PyExc_ValueError, "bad numeric value %S",pD); |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
ad = fabs(d); |
| 339 |
if(ad<=1.0e-7){ |
| 340 |
s[0]='0'; |
| 341 |
s[1]=0; |
| 342 |
} |
| 343 |
else{ |
| 344 |
if(ad>1e20){ |
| 345 |
EXC_SET(PyExc_ValueError, "number too large %S",pD); |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
if(ad>1) l = min(max(0,6-(int)log10(ad)),6); |
| 349 |
else l = 6; |
| 350 |
sprintf(s,_fp_fmts[l], d); |
| 351 |
if(l){ |
| 352 |
l = (int)strlen(s)-1; |
| 353 |
while(l && s[l]=='0') l--; |
| 354 |
if(s[l]=='.' || s[l]==',') s[l]=0; |
| 355 |
else { |
| 356 |
s[l+1]=0; |
| 357 |
if(s[0]=='0' && (s[1]=='.'||s[1]==',')){ |
| 358 |
if(s[1]==',') s[1] = '.'; |
| 359 |
return s+1; |
| 360 |
} |
| 361 |
} |
| 362 |
if((dot=strchr(s,','))) *dot = '.'; |
| 363 |
} |
| 364 |
} |
| 365 |
return s; |
| 366 |
} |
| 367 |
|
| 368 |
PyObject *_fp_str(PyObject *module, PyObject *args) |
| 369 |
{ |
| 370 |
Py_ssize_t i, aL; |
| 371 |
PyObject *retVal; |
| 372 |
char *pD; |
| 373 |
char *buf, *pB; |
| 374 |
|
| 375 |
if((aL=PySequence_Length(args))>=0){ |
| 376 |
if(aL==1){ |
| 377 |
retVal = PySequence_GetItem(args,0); |
| 378 |
if((i=PySequence_Length(retVal))>=0){ |
| 379 |
aL = i; |
| 380 |
args = retVal; |
| 381 |
} |
| 382 |
else PyErr_Clear(); |
| 383 |
Py_DECREF(retVal); |
| 384 |
} |
| 385 |
buf = malloc(31*aL+1); |
| 386 |
pB = buf; |
| 387 |
for(i=0;i<aL;i++){ |
| 388 |
retVal = PySequence_GetItem(args,i); |
| 389 |
if(retVal){ |
| 390 |
pD = _fp_one(module,retVal); |
| 391 |
Py_DECREF(retVal); |
| 392 |
} |
| 393 |
else pD = NULL; |
| 394 |
if(!pD){ |
| 395 |
free(buf); |
| 396 |
EXC_EXIT(PyExc_ValueError,"_fp_one failed"); |
| 397 |
} |
| 398 |
if(pB!=buf){ |
| 399 |
*pB++ = ' '; |
| 400 |
} |
| 401 |
strcpy(pB,pD); |
| 402 |
pB = pB + strlen(pB); |
| 403 |
} |
| 404 |
*pB = 0; |
| 405 |
retVal = PyUnicode_FromString(buf); |
| 406 |
free(buf); |
| 407 |
return retVal; |
| 408 |
} |
| 409 |
else { |
| 410 |
PyErr_Clear(); |
| 411 |
PyArg_ParseTuple(args, "O:_fp_str", &retVal); |
| 412 |
L_exit: return NULL; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
static PyObject *_escapePDF(unsigned char* text, Py_ssize_t textlen) |
| 417 |
{ |
| 418 |
unsigned char* out = PyMem_Malloc((textlen<<2)+1); |
| 419 |
int j=0, i=0; |
| 420 |
char buf[4]; |
| 421 |
PyObject* ret; |
| 422 |
|
| 423 |
while(i<textlen){ |
| 424 |
unsigned char c = text[i++]; |
| 425 |
if(c<32 || c>=127){ |
| 426 |
sprintf(buf,"%03o",c); |
| 427 |
out[j++] = '\\'; |
| 428 |
out[j++] = buf[0]; |
| 429 |
out[j++] = buf[1]; |
| 430 |
out[j++] = buf[2]; |
| 431 |
} |
| 432 |
else { |
| 433 |
if(c=='\\' || c=='(' || c==')') out[j++] = '\\'; |
| 434 |
out[j++] = c; |
| 435 |
} |
| 436 |
} |
| 437 |
ret = PyUnicode_FromStringAndSize((const char *)out,j); |
| 438 |
PyMem_Free(out); |
| 439 |
return ret; |
| 440 |
} |
| 441 |
|
| 442 |
static PyObject *escapePDF(PyObject *module, PyObject* args) |
| 443 |
{ |
| 444 |
unsigned char *inData; |
| 445 |
Py_ssize_t length; |
| 446 |
PyObject *retVal=NULL, *inObj, *_o1=NULL; |
| 447 |
|
| 448 |
if (!PyArg_ParseTuple(args, "O:escapePDF", &inObj)) return NULL; |
| 449 |
if(PyUnicode_Check(inObj)){ |
| 450 |
_o1 = PyUnicode_AsLatin1String(inObj); |
| 451 |
if(!_o1){ |
| 452 |
EXC_EXIT(PyExc_ValueError,"argument not decodable as latin1"); |
| 453 |
} |
| 454 |
inData = (unsigned char*)PyBytes_AsString(_o1); |
| 455 |
inObj = _o1; |
| 456 |
if(!inData){ |
| 457 |
EXC_EXIT(PyExc_ValueError,"argument not converted to internal char string"); |
| 458 |
} |
| 459 |
} |
| 460 |
else if(!PyBytes_Check(inObj)){ |
| 461 |
EXC_EXIT(PyExc_ValueError,"argument should be " BYTESNAME " or latin1 decodable " STRNAME); |
| 462 |
} |
| 463 |
inData = (unsigned char*)PyBytes_AsString(inObj); |
| 464 |
length = RLPyBytes_Size(inObj); |
| 465 |
retVal = _escapePDF(inData,length); |
| 466 |
L_exit: |
| 467 |
Py_XDECREF(_o1); |
| 468 |
return retVal; |
| 469 |
} |
| 470 |
|
| 471 |
static PyObject *sameFrag(PyObject *module, PyObject* args) |
| 472 |
{ |
| 473 |
PyObject *f, *g; |
| 474 |
static char *names[] = {"fontName", "fontSize", "textColor", "rise", "us_lines", "link", "backColor", "nobr", NULL}; |
| 475 |
int r=0, t; |
| 476 |
char **p; |
| 477 |
if (!PyArg_ParseTuple(args, "OO:sameFrag", &f, &g)) return NULL; |
| 478 |
if(PyObject_HasAttrString(f,"cbDefn")||PyObject_HasAttrString(g,"cbDefn") |
| 479 |
|| PyObject_HasAttrString(f,"lineBreak")||PyObject_HasAttrString(g,"lineBreak")) goto L0; |
| 480 |
for(p=names;*p;p++){ |
| 481 |
PyObject *fa, *ga; |
| 482 |
fa = PyObject_GetAttrString(f,*p); |
| 483 |
ga = PyObject_GetAttrString(g,*p); |
| 484 |
if(fa && ga){ |
| 485 |
t = PyObject_RichCompareBool(fa,ga,Py_NE); |
| 486 |
Py_DECREF(fa); |
| 487 |
Py_DECREF(ga); |
| 488 |
if(PyErr_Occurred()) goto L1; |
| 489 |
} |
| 490 |
else{ |
| 491 |
t = fa==ga ? 0 : 1; |
| 492 |
Py_XDECREF(fa); |
| 493 |
Py_XDECREF(ga); |
| 494 |
PyErr_Clear(); |
| 495 |
} |
| 496 |
if(t) goto L0; |
| 497 |
} |
| 498 |
r = 1; |
| 499 |
L0: return PyBool_FromLong((long)r); |
| 500 |
L1: return NULL; |
| 501 |
} |
| 502 |
|
| 503 |
static PyObject *ttfonts_calcChecksum(PyObject *module, PyObject* args) |
| 504 |
{ |
| 505 |
unsigned char *data; |
| 506 |
Py_ssize_t dataLen; |
| 507 |
unsigned long Sum = 0L; |
| 508 |
unsigned char *EndPtr; |
| 509 |
unsigned long n; |
| 510 |
int leftover; |
| 511 |
|
| 512 |
|
| 513 |
if (!_PyArg_ParseTuple_SizeT(args, "s#:calcChecksum", &data, &dataLen)) return NULL; |
| 514 |
EndPtr = data + (dataLen & ~3); |
| 515 |
|
| 516 |
|
| 517 |
while(data < EndPtr){ |
| 518 |
n = ((*data++) << 24); |
| 519 |
n += ((*data++) << 16); |
| 520 |
n += ((*data++) << 8); |
| 521 |
n += ((*data++)); |
| 522 |
Sum += n; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
leftover = dataLen & 3; |
| 527 |
if(leftover){ |
| 528 |
n = ((*data++) << 24); |
| 529 |
if (leftover>1) n += ((*data++) << 16); |
| 530 |
if (leftover>2) n += ((*data++) << 8); |
| 531 |
Sum += n; |
| 532 |
} |
| 533 |
|
| 534 |
return PyLong_FromUnsignedLong(Sum&0xFFFFFFFFU); |
| 535 |
} |
| 536 |
|
| 537 |
static PyObject *ttfonts_add32(PyObject *module, PyObject* args) |
| 538 |
{ |
| 539 |
unsigned long x, y; |
| 540 |
if(!PyArg_ParseTuple(args, "kk:add32", &x, &y)) return NULL; |
| 541 |
return PyLong_FromUnsignedLong((x+y)&0xFFFFFFFFU); |
| 542 |
} |
| 543 |
|
| 544 |
static PyObject *hex32(PyObject *module, PyObject* args) |
| 545 |
{ |
| 546 |
unsigned long x; |
| 547 |
char buf[20]; |
| 548 |
if(!PyArg_ParseTuple(args, "k:hex32", &x)) return NULL; |
| 549 |
sprintf(buf,"0X%8.8lX",x); |
| 550 |
return PyUnicode_FromString(buf); |
| 551 |
} |
| 552 |
|
| 553 |
static PyObject *_GetExcValue(void) |
| 554 |
{ |
| 555 |
PyObject *type = NULL, *value = NULL, *tb = NULL, *result=NULL; |
| 556 |
PyErr_Fetch(&type, &value, &tb); |
| 557 |
PyErr_NormalizeException(&type, &value, &tb); |
| 558 |
if(PyErr_Occurred()) goto L_BAD; |
| 559 |
if(!value){ |
| 560 |
value = Py_None; |
| 561 |
Py_INCREF(value); |
| 562 |
} |
| 563 |
Py_XINCREF(value); |
| 564 |
result = value; |
| 565 |
L_BAD: |
| 566 |
Py_XDECREF(type); |
| 567 |
Py_XDECREF(value); |
| 568 |
Py_XDECREF(tb); |
| 569 |
return result; |
| 570 |
} |
| 571 |
static PyObject *_GetAttrString(PyObject *obj, char *name) |
| 572 |
{ |
| 573 |
PyObject *res = PyObject_GetAttrString(obj, name); |
| 574 |
if(!res) EXC_SET(PyExc_AttributeError, "missing attribute %s",name); |
| 575 |
return res; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
static PyObject *_GetStringBuf(PyObject *obj, const char **buf) |
| 582 |
{ |
| 583 |
PyObject *res; |
| 584 |
#ifdef Py_LIMITED_API |
| 585 |
if(PyUnicode_Check(obj)){ |
| 586 |
res = PyUnicode_AsUTF8String(obj); |
| 587 |
} |
| 588 |
else if(PyBytes_Check(obj)){ |
| 589 |
res = obj; |
| 590 |
Py_INCREF(res); |
| 591 |
} |
| 592 |
else{ |
| 593 |
EXC_SET(PyExc_ValueError, "require bytes or unicode object"); |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
*buf = PyBytes_AsString(res); |
| 597 |
#else |
| 598 |
if(PyUnicode_Check(obj)){ |
| 599 |
res = obj; |
| 600 |
Py_INCREF(res); |
| 601 |
*buf = (const char *)PyUnicode_AsUTF8(res); |
| 602 |
} |
| 603 |
else if(PyBytes_Check(obj)){ |
| 604 |
res = obj; |
| 605 |
Py_INCREF(res); |
| 606 |
*buf = PyBytes_AsString(res); |
| 607 |
} |
| 608 |
else{ |
| 609 |
EXC_SET(PyExc_ValueError, "require bytes or unicode object"); |
| 610 |
return NULL; |
| 611 |
} |
| 612 |
#endif |
| 613 |
return res; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
static PyObject *unicode2T1(PyObject *module, PyObject *args, PyObject *kwds) |
| 618 |
{ |
| 619 |
long i, j, _i1, _i2; |
| 620 |
PyObject *R, *font, *res, *utext=NULL, *fonts=NULL, |
| 621 |
*_o1 = NULL, *_o2 = NULL, *_o3 = NULL; |
| 622 |
static char *argnames[] = {"utext","fonts",NULL}; |
| 623 |
PyObject *encObj = NULL; |
| 624 |
char *encStr; |
| 625 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", argnames, &utext, &fonts)) return NULL; |
| 626 |
Py_INCREF(utext); |
| 627 |
Py_INCREF(fonts); |
| 628 |
R = Py_None; Py_INCREF(Py_None); |
| 629 |
font = Py_None; Py_INCREF(Py_None); |
| 630 |
|
| 631 |
|
| 632 |
_o2 = PyList_New(0); if(!_o2) EXC_EXIT(PyExc_MemoryError,"unable to create list"); |
| 633 |
Py_DECREF(R); |
| 634 |
R = _o2; |
| 635 |
_o2 = NULL; |
| 636 |
|
| 637 |
_o2 = PySequence_GetItem(fonts,0); if(!_o2) EXC_EXIT(PyExc_IndexError,"fonts[0] failed"); |
| 638 |
_o1 = PySequence_GetSlice(fonts, 1, 0x7fffffff); if(!_o1) EXC_EXIT(PyExc_IndexError,"fonts[1:] failed"); |
| 639 |
Py_DECREF(font); |
| 640 |
font = _o2; |
| 641 |
Py_DECREF(fonts); |
| 642 |
fonts = _o1; |
| 643 |
_o1 = _o2 = NULL; |
| 644 |
|
| 645 |
_o2 = _GetAttrString(font, "encName"); if(!_o2) EXC_EXIT(PyExc_AttributeError,"no encName"); |
| 646 |
encObj = _GetStringBuf(_o2, (const char**)&encStr); |
| 647 |
Py_DECREF(_o2); |
| 648 |
_o2 = NULL; |
| 649 |
if (!encObj) EXC_EXIT(PyExc_TypeError,"could not convert str(font.encName) failed"); |
| 650 |
if(strstr(encStr,"UCS-2")) encStr = "UTF16"; |
| 651 |
|
| 652 |
while((_i1=PyObject_IsTrue(utext))>0){ |
| 653 |
if((_o1 = PyUnicode_AsEncodedString(utext, encStr, NULL))){ |
| 654 |
_o2 = PyTuple_New(2); if(!_o2) EXC_EXIT(PyExc_MemoryError,"create tuple length 2 failed"); |
| 655 |
Py_INCREF(font); |
| 656 |
RLPyTuple_SetItem(_o2, 0, font); |
| 657 |
RLPyTuple_SetItem(_o2, 1, _o1); |
| 658 |
_o1 = NULL; |
| 659 |
if(PyList_Append(R, _o2)) EXC_EXIT(PyExc_RuntimeError,"could not append to result list"); |
| 660 |
Py_DECREF(_o2); _o2 = NULL; |
| 661 |
break; |
| 662 |
} |
| 663 |
else{ |
| 664 |
Py_XDECREF(_o1); _o1 = NULL; |
| 665 |
if(!PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) EXC_EXIT(PyExc_RuntimeError,"unexpected exception"); |
| 666 |
_o1 = _GetExcValue(); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"could not obtain exception value"); |
| 667 |
PyErr_Clear(); |
| 668 |
_o2 = _GetAttrString(_o1, "args"); if(!_o2) EXC_EXIT(PyExc_AttributeError,"missing args attribute"); |
| 669 |
Py_DECREF(_o1); |
| 670 |
_o1 = PySequence_GetSlice(_o2, 2, 4); if(!_o1) EXC_EXIT(PyExc_IndexError,"args[2:4] failed"); |
| 671 |
Py_DECREF(_o2); |
| 672 |
_o2 = PySequence_GetItem(_o1, 0); if(!_o2) EXC_EXIT(PyExc_IndexError,"args[2:4][0] failed"); |
| 673 |
i = PyLong_AsLong(_o2); if(PyErr_Occurred()) EXC_EXIT(PyExc_ValueError,"int(args[2:4][0]) failed"); |
| 674 |
Py_DECREF(_o2); |
| 675 |
|
| 676 |
_o2 = PySequence_GetItem(_o1, 1); if(!_o1) EXC_EXIT(PyExc_IndexError,"args[2:4][1] failed"); |
| 677 |
j = PyLong_AsLong(_o2); if(PyErr_Occurred()) EXC_EXIT(PyExc_ValueError,"int(args[2:4][1]) failed"); |
| 678 |
Py_DECREF(_o2); |
| 679 |
|
| 680 |
Py_DECREF(_o1); _o2 = _o1 = 0; |
| 681 |
|
| 682 |
if(i){ |
| 683 |
_o1 = PySequence_GetSlice(utext, 0, i); if(!_o1) EXC_EXIT(PyExc_IndexError,"utext[0:%d] failed",i); |
| 684 |
_o2 = PyUnicode_AsEncodedString(_o1, encStr, NULL); if(!_o2) EXC_EXIT(PyExc_UnicodeEncodeError,"encode(utext[0:%d],'%s') failed",i,encStr); |
| 685 |
Py_DECREF(_o1); |
| 686 |
_o1 = PyTuple_New(2); if(!_o1) EXC_EXIT(PyExc_MemoryError,"create tuple of length 2 failed"); |
| 687 |
Py_INCREF(font); |
| 688 |
RLPyTuple_SetItem(_o1, 0, font); |
| 689 |
RLPyTuple_SetItem(_o1, 1, _o2); |
| 690 |
_o2 = NULL; |
| 691 |
if(PyList_Append(R, _o1)) EXC_EXIT(PyExc_RuntimeError,"could not append to result list"); |
| 692 |
Py_DECREF(_o1); _o1 = NULL; |
| 693 |
} |
| 694 |
|
| 695 |
_i2 = PyObject_IsTrue(fonts); if(_i2<0) EXC_EXIT(PyExc_ValueError,"bool(fonts) is not True"); |
| 696 |
if(_i2){ |
| 697 |
_o1 = PySequence_GetSlice(utext, i, j); if(!_o1) EXC_EXIT(PyExc_IndexError,"utext[%d:%d] failed",i,j); |
| 698 |
_o2 = PyTuple_New(2); if(!_o2) EXC_EXIT(PyExc_MemoryError,"create tuple of length 2 failed"); |
| 699 |
RLPyTuple_SetItem(_o2, 0, _o1); |
| 700 |
Py_INCREF(fonts); |
| 701 |
RLPyTuple_SetItem(_o2, 1, fonts); |
| 702 |
_o1 = unicode2T1(module,_o2,NULL); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"PyTuple_SetItem(_o2,1,fonts) failed"); |
| 703 |
Py_DECREF(_o2); _o2 = 0; |
| 704 |
_o3 = PyTuple_New(1); if(!_o3) EXC_EXIT(PyExc_MemoryError,"create tuple of length 1 failed"); |
| 705 |
RLPyTuple_SetItem(_o3, 0, _o1); |
| 706 |
_o1 = _GetAttrString(R, "extend"); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"no attriute extend"); |
| 707 |
_o2 = PyObject_CallObject(_o1, _o3); if(!_o2) EXC_EXIT(PyExc_TypeError,"result.extend call failed"); |
| 708 |
Py_DECREF(_o1); |
| 709 |
Py_DECREF(_o3); |
| 710 |
Py_DECREF(_o2); _o1 = _o2 = _o3 = NULL; |
| 711 |
} |
| 712 |
else{ |
| 713 |
_o3 = _GetAttrString(font,"_notdefChar"); |
| 714 |
if(!_o3) EXC_EXIT(PyExc_RuntimeError,"missing _notdefChar"); |
| 715 |
_o2 = PyLong_FromLong((j - i)); if(!_o2) EXC_EXIT(PyExc_ValueError,"int((%d - %d)) failed",j,i); |
| 716 |
_o1 = PyNumber_Multiply(_o3, _o2); if(!_o1) EXC_EXIT(PyExc_ArithmeticError,"_notdefChar multiply failed"); |
| 717 |
Py_DECREF(_o2); Py_DECREF(_o3); _o2=_o3=NULL; |
| 718 |
_o2 = PyTuple_New(2); if(!_o2) EXC_EXIT(PyExc_MemoryError,"create tuple of length 2 failed"); |
| 719 |
_o3 = _GetAttrString(font,"_notdefFont"); if(!_o3) EXC_EXIT(PyExc_AttributeError,"missing _notdefFont"); |
| 720 |
RLPyTuple_SetItem(_o2, 0, _o3); |
| 721 |
RLPyTuple_SetItem(_o2, 1, _o1); |
| 722 |
Py_INCREF(_o3); _o3=NULL; |
| 723 |
_o1 = NULL; |
| 724 |
if(PyList_Append(R, _o2)) EXC_EXIT(PyExc_RuntimeError,"Could not append to result"); |
| 725 |
Py_DECREF(_o2); _o2 = NULL; |
| 726 |
} |
| 727 |
|
| 728 |
_o1 = PySequence_GetSlice(utext, j, 0x7fffffff); if(!_o1) EXC_EXIT(PyExc_IndexError,"utext[%d:] failed",j); |
| 729 |
Py_DECREF(utext); |
| 730 |
utext = _o1; |
| 731 |
_o1 = NULL; |
| 732 |
} |
| 733 |
} |
| 734 |
if(_i1<0) EXC_EXIT(PyExc_ValueError,"_i1=%d became negative",_i1); |
| 735 |
|
| 736 |
Py_INCREF(R); |
| 737 |
res = R; |
| 738 |
goto L_OK; |
| 739 |
|
| 740 |
L_exit: |
| 741 |
Py_XDECREF(_o1); |
| 742 |
Py_XDECREF(_o2); |
| 743 |
Py_XDECREF(_o3); |
| 744 |
res = NULL; |
| 745 |
L_OK: |
| 746 |
Py_XDECREF(encObj); |
| 747 |
Py_DECREF(R); |
| 748 |
Py_DECREF(font); |
| 749 |
Py_DECREF(utext); |
| 750 |
Py_DECREF(fonts); |
| 751 |
return res; |
| 752 |
} |
| 753 |
static PyObject *instanceStringWidthT1(PyObject *module, PyObject *args, PyObject *kwds) |
| 754 |
{ |
| 755 |
PyObject *L=0, *t=0, *f=0, *self, *text, *size, *res, |
| 756 |
*encoding = 0, *_o1 = 0, *_o2 = 0, *_o3 = 0; |
| 757 |
unsigned char *b; |
| 758 |
PyObject *encObj = NULL; |
| 759 |
const char *encStr; |
| 760 |
Py_ssize_t n, m; |
| 761 |
int i, j, s, _i1; |
| 762 |
static char *argnames[]={"self","text","size","encoding",0}; |
| 763 |
if(!PyArg_ParseTupleAndKeywords(args, kwds, "OOO|O", argnames, &self, &text, &size, &encoding)) return 0; |
| 764 |
Py_INCREF(text); |
| 765 |
if(!encoding) encStr="utf8"; |
| 766 |
else { |
| 767 |
encObj = _GetStringBuf(encoding, &encStr); |
| 768 |
if(!encObj) EXC_EXIT(PyExc_ValueError,"cannot obtain encoding"); |
| 769 |
} |
| 770 |
|
| 771 |
if(!PyUnicode_Check(text)){ |
| 772 |
if(PyBytes_Check(text)){ |
| 773 |
_o1=PyUnicode_Decode(RLPyBytes_AsString(text), RLPyBytes_Size(text), encStr,"strict"); |
| 774 |
if(!_o1) EXC_EXIT(PyExc_ValueError,"cannot decode text argument"); |
| 775 |
Py_DECREF(text); |
| 776 |
text = _o1; |
| 777 |
_o1 = NULL; |
| 778 |
} |
| 779 |
else{ |
| 780 |
EXC_EXIT(PyExc_ValueError, "invalid type for argument text"); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
_o3 = PyList_New(1); if(!_o3) EXC_EXIT(PyExc_MemoryError,"PyList_New(1) failed"); |
| 785 |
Py_INCREF(self); |
| 786 |
RLPyList_SetItem(_o3, 0, self); |
| 787 |
_o2 = _GetAttrString(self, "substitutionFonts"); if(!_o2) EXC_EXIT(PyExc_RuntimeError,"missing substitutionFonts"); |
| 788 |
_o1 = PyNumber_Add(_o3, _o2); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"substitution addition failed"); |
| 789 |
Py_DECREF(_o3); _o3 = 0; |
| 790 |
Py_DECREF(_o2); _o2 = NULL; |
| 791 |
_o3 = PyTuple_New(2); if(!_o3) EXC_EXIT(PyExc_MemoryError,"PyTuple_New(2) failed"); |
| 792 |
Py_INCREF(text); |
| 793 |
RLPyTuple_SetItem(_o3, 0, text); |
| 794 |
RLPyTuple_SetItem(_o3, 1, _o1); |
| 795 |
_o1 = NULL; |
| 796 |
_o2 = unicode2T1(module,_o3,NULL); if(!_o2) EXC_EXIT(PyExc_RuntimeError,"unicode2T1 call failed"); |
| 797 |
Py_DECREF(_o3); _o3 = NULL; |
| 798 |
L = _o2; |
| 799 |
_o2 = NULL; |
| 800 |
|
| 801 |
n = RLPyList_Size(L); |
| 802 |
|
| 803 |
for(s=i=0;i<n;++i){ |
| 804 |
_o1 = PyList_GetItem(L,i); if(!_o1) EXC_EXIT(PyExc_IndexError,"L[%d] failed",i); |
| 805 |
Py_INCREF(_o1); |
| 806 |
|
| 807 |
_o2 = PySequence_GetItem(_o1, 0); if(!_o2) EXC_EXIT(PyExc_IndexError,"L[%d][0] failed",i); |
| 808 |
Py_XDECREF(f); |
| 809 |
f = _o2; |
| 810 |
_o2 = NULL; |
| 811 |
|
| 812 |
_o2 = _GetAttrString(f, "widths"); if(!_o2) EXC_EXIT(PyExc_AttributeError,"no widths"); |
| 813 |
Py_DECREF(f); |
| 814 |
f = _o2; |
| 815 |
_o2 = NULL; |
| 816 |
|
| 817 |
_o2 = PySequence_GetItem(_o1, 1); if(!_o2) EXC_EXIT(PyExc_IndexError,"L[%d][1] failed",i); |
| 818 |
Py_XDECREF(t); |
| 819 |
t = _o2; |
| 820 |
Py_DECREF(_o1); |
| 821 |
_o1 = _o2 = NULL; |
| 822 |
|
| 823 |
m = PyBytes_Size(t); |
| 824 |
b = (unsigned char*)RLPyBytes_AsString(t); |
| 825 |
|
| 826 |
for(j=0;j<m;++j){ |
| 827 |
_i1 = (long)(b[j]); |
| 828 |
_o2 = PyList_GetItem(f,_i1); if(!_o2) {EXC_EXIT(PyExc_IndexError,"widths index %d out of range",_i1);} |
| 829 |
_i1 = PyLong_AsLong(_o2); |
| 830 |
_o2 = NULL; |
| 831 |
if(PyErr_Occurred()) EXC_EXIT(PyExc_RuntimeError,"longint conversion failed"); |
| 832 |
s += _i1; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
_o1 = PyFloat_FromDouble((s * 0.001)); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"float(s*0.001) failed"); |
| 837 |
res = PyNumber_Multiply(_o1, size); if(!res) EXC_EXIT(PyExc_RuntimeError,"multiply by size failed"); |
| 838 |
Py_DECREF(_o1); |
| 839 |
goto L_OK; |
| 840 |
L_exit: |
| 841 |
Py_XDECREF(_o1); |
| 842 |
Py_XDECREF(_o2); |
| 843 |
Py_XDECREF(_o3); |
| 844 |
res = NULL; |
| 845 |
L_OK: |
| 846 |
Py_XDECREF(encObj); |
| 847 |
Py_XDECREF(L); |
| 848 |
Py_XDECREF(t); |
| 849 |
Py_XDECREF(f); |
| 850 |
Py_DECREF(text); |
| 851 |
return res; |
| 852 |
} |
| 853 |
static PyObject *instanceStringWidthTTF(PyObject *module, PyObject *args, PyObject *kwds) |
| 854 |
{ |
| 855 |
PyObject *self, *text, *size, *res, |
| 856 |
*encoding = 0, *_o1=NULL, *_o2=NULL, *_o3=NULL; |
| 857 |
Py_UCS4 *b=NULL; |
| 858 |
Py_ssize_t n; |
| 859 |
int i; |
| 860 |
double s, _d1, dw; |
| 861 |
static char *argnames[]={"self","text","size","encoding",0}; |
| 862 |
if(!PyArg_ParseTupleAndKeywords(args, kwds, "OOO|O", argnames, &self, &text, &size, &_o1)) return 0; |
| 863 |
Py_INCREF(text); |
| 864 |
if(_o1){ |
| 865 |
encoding = _o1; |
| 866 |
_o1 = NULL; |
| 867 |
Py_INCREF(encoding); |
| 868 |
} |
| 869 |
else{ |
| 870 |
_o1 = PyUnicode_FromString("utf8"); if(!_o1) EXC_EXIT(PyExc_UnicodeDecodeError,"utf8 decode failed"); |
| 871 |
encoding = _o1; |
| 872 |
_o1 = NULL; |
| 873 |
} |
| 874 |
|
| 875 |
if(!PyUnicode_Check(text)){ |
| 876 |
i = PyObject_IsTrue(encoding); if(i<0) EXC_EXIT(PyExc_RuntimeError,"truth(encoding)=%d",i); |
| 877 |
if(!i){ |
| 878 |
Py_DECREF(encoding); |
| 879 |
encoding = PyUnicode_FromString("utf8"); if(!encoding) EXC_EXIT(PyExc_UnicodeDecodeError,"utf8 decode failed"); |
| 880 |
} |
| 881 |
_o1 = _GetAttrString(text, "decode"); if(!_o1) EXC_EXIT(PyExc_AttributeError,"missing attribute decode"); |
| 882 |
_o3 = PyTuple_New(1); if(!_o3) EXC_EXIT(PyExc_MemoryError,"PyTuple_New(1) failed"); |
| 883 |
Py_INCREF(encoding); |
| 884 |
RLPyTuple_SetItem(_o3, 0, encoding); |
| 885 |
_o2 = PyObject_CallObject(_o1, _o3); if(!_o2) EXC_EXIT(PyExc_RuntimeError,"call of decode attribute failed"); |
| 886 |
Py_DECREF(_o1); |
| 887 |
Py_DECREF(_o3); _o1 = _o3 = NULL; |
| 888 |
Py_DECREF(text); |
| 889 |
text = _o2; |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
_o2 = _GetAttrString(self, "face"); if(!_o2) EXC_EXIT(PyExc_AttributeError,"missing attribute face"); |
| 894 |
_o1 = _GetAttrString(_o2, "charWidths"); if(!_o1) EXC_EXIT(PyExc_AttributeError,"no attribute charWidths"); |
| 895 |
if(!PyDict_Check(_o1)){EXC_EXIT(PyExc_TypeError, "TTFontFace instance charWidths is not a dict");} |
| 896 |
_o3 = _GetAttrString(_o2, "defaultWidth"); if(!_o3) EXC_EXIT(PyExc_AttributeError,"missing attribute defaultWidth"); |
| 897 |
Py_DECREF(_o2); _o2 = NULL; |
| 898 |
dw = PyFloat_AsDouble(_o3); |
| 899 |
if(PyErr_Occurred()) EXC_EXIT(PyExc_RuntimeError,"float() failed"); |
| 900 |
Py_DECREF(_o3); _o3=NULL; |
| 901 |
|
| 902 |
n = RLPyUnicode_GetLength(text); |
| 903 |
b = PyUnicode_AsUCS4Copy(text); |
| 904 |
if(!b) EXC_EXIT(PyExc_MemoryError,"memory failed for UCS4Copy"); |
| 905 |
|
| 906 |
for(s=i=0;i<n;++i){ |
| 907 |
_o3 = PyLong_FromLong((long)b[i]); if(!_o3) EXC_EXIT(PyExc_RuntimeError,"FromLong failed"); |
| 908 |
_o2 = PyDict_GetItem(_o1,_o3); |
| 909 |
Py_DECREF(_o3); _o3 = NULL; |
| 910 |
if(!_o2) _d1 = dw; |
| 911 |
else{ |
| 912 |
_d1 = PyFloat_AsDouble(_o2); |
| 913 |
_o2=NULL; |
| 914 |
if(PyErr_Occurred()) EXC_EXIT(PyExc_RuntimeError,"float-->double failed"); |
| 915 |
} |
| 916 |
s += _d1; |
| 917 |
} |
| 918 |
Py_DECREF(_o1); |
| 919 |
_o1 = PyFloat_FromDouble((s * 0.001)); if(!_o1) EXC_EXIT(PyExc_RuntimeError,"float(s*0.001) failed"); |
| 920 |
res = PyNumber_Multiply(_o1, size); if(!res) EXC_EXIT(PyExc_RuntimeError,"multiply by size failed"); |
| 921 |
Py_DECREF(_o1); |
| 922 |
L_OK: |
| 923 |
if(b) PyMem_Free(b); |
| 924 |
Py_DECREF(text); |
| 925 |
Py_DECREF(encoding); |
| 926 |
return res; |
| 927 |
L_exit: |
| 928 |
Py_XDECREF(_o1); |
| 929 |
Py_XDECREF(_o2); |
| 930 |
Py_XDECREF(_o3); |
| 931 |
res = NULL; |
| 932 |
goto L_OK; |
| 933 |
} |
| 934 |
|
| 935 |
#ifdef HAVE_BOX |
| 936 |
|
| 937 |
typedef struct { |
| 938 |
PyObject_HEAD |
| 939 |
unsigned is_box:1; |
| 940 |
unsigned is_glue:1; |
| 941 |
unsigned is_penalty:1; |
| 942 |
unsigned is_none:1; |
| 943 |
double width,stretch,shrink,penalty; |
| 944 |
int flagged; |
| 945 |
char character; |
| 946 |
} BoxObject; |
| 947 |
|
| 948 |
static void BoxFree(BoxObject* self) |
| 949 |
{ |
| 950 |
PyObject_Del(self); |
| 951 |
} |
| 952 |
|
| 953 |
static int Box_set_int(char* name, int* pd, PyObject *value) |
| 954 |
{ |
| 955 |
PyObject *v = PyNumber_Long(value); |
| 956 |
if(!v) return -1; |
| 957 |
*pd = PyLong_AsLong(v); |
| 958 |
Py_DECREF(v); |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
static int Box_set_double(char* name, double* pd, PyObject *value) |
| 963 |
{ |
| 964 |
PyObject *v = PyNumber_Float(value); |
| 965 |
if(!v) return -1; |
| 966 |
*pd = PyFloat_AsDouble(v); |
| 967 |
Py_DECREF(v); |
| 968 |
return 0; |
| 969 |
} |
| 970 |
|
| 971 |
static int Box_set_character(BoxObject *self, PyObject *value) |
| 972 |
{ |
| 973 |
if(value==Py_None){ |
| 974 |
self->is_none = 1; |
| 975 |
} |
| 976 |
else { |
| 977 |
char *v = PyBytes_AsString(value); |
| 978 |
Py_ssize_t vL; |
| 979 |
if(!v) return -1; |
| 980 |
if((vL=RLPyBytes_Size(value))!=1){ |
| 981 |
EXC_SET(PyExc_AttributeError,"Bad size %d('%s') for attribute character",vL,v); |
| 982 |
return -1; |
| 983 |
} |
| 984 |
self->character = v[0]; |
| 985 |
self->is_none = 0; |
| 986 |
} |
| 987 |
|
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
static int Box_setattr(BoxObject *self, char *name, PyObject* value) |
| 992 |
{ |
| 993 |
if(!strcmp(name,"width")) return Box_set_double(name,&self->width,value); |
| 994 |
else if(!strcmp(name,"character")) return Box_set_character(self,value); |
| 995 |
else if(!strcmp(name,"stretch")) return Box_set_double(name,&self->stretch,value); |
| 996 |
else if(!strcmp(name,"shrink")) return Box_set_double(name,&self->shrink,value); |
| 997 |
else if(!strcmp(name,"penalty")) return Box_set_double(name,&self->penalty,value); |
| 998 |
else if(!strcmp(name,"flagged")) return Box_set_int(name,&self->flagged,value); |
| 999 |
else if( |
| 1000 |
!strcmp(name,"is_penalty") || |
| 1001 |
!strcmp(name,"is_box") || |
| 1002 |
!strcmp(name,"is_glue") |
| 1003 |
) EXC_SET(PyExc_AttributeError, "readonly attribute %s", name); |
| 1004 |
else EXC_SET(PyExc_AttributeError, "no attribute %s", name); |
| 1005 |
return -1; |
| 1006 |
} |
| 1007 |
|
| 1008 |
static double _Glue_compute_width(BoxObject *self, double r) |
| 1009 |
{ |
| 1010 |
if(self->is_glue) return self->width+r*(r<0?self->shrink:self->stretch); |
| 1011 |
return self->width; |
| 1012 |
} |
| 1013 |
|
| 1014 |
static PyObject* Glue_compute_width(BoxObject *self, PyObject *args) |
| 1015 |
{ |
| 1016 |
double r; |
| 1017 |
if(!PyArg_ParseTuple(args, "d:compute_width", &r)) return NULL; |
| 1018 |
return PyFloat_FromDouble(_Glue_compute_width(self,r)); |
| 1019 |
} |
| 1020 |
|
| 1021 |
static struct PyMethodDef Box_methods[] = { |
| 1022 |
{"compute_width", (PyCFunction)Glue_compute_width, METH_VARARGS|METH_KEYWORDS, "compute_width(r)"}, |
| 1023 |
{NULL, NULL} |
| 1024 |
}; |
| 1025 |
|
| 1026 |
static PyObject* Box_get_character(unsigned is_none, char c) |
| 1027 |
{ |
| 1028 |
if(!is_none) return PyBytes_FromStringAndSize(&c,1); |
| 1029 |
else { |
| 1030 |
Py_INCREF(Py_None); |
| 1031 |
return Py_None; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
static PyObject* Box_getattr(BoxObject *self, char *name) |
| 1036 |
{ |
| 1037 |
if(!strcmp(name,"width")) return PyFloat_FromDouble(self->width); |
| 1038 |
else if(!strcmp(name,"character")) return Box_get_character(self->is_none,self->character); |
| 1039 |
else if(!strcmp(name,"is_box")) return PyBool_FromLong(self->is_box); |
| 1040 |
else if(!strcmp(name,"is_glue")) return PyBool_FromLong(self->is_glue); |
| 1041 |
else if(!strcmp(name,"is_penalty")) return PyBool_FromLong(self->is_penalty); |
| 1042 |
else if(!strcmp(name,"stretch")) return PyFloat_FromDouble(self->stretch); |
| 1043 |
else if(!strcmp(name,"shrink")) return PyFloat_FromDouble(self->shrink); |
| 1044 |
else if(!strcmp(name,"penalty")) return PyFloat_FromDouble(self->penalty); |
| 1045 |
else if(!strcmp(name,"flagged")) return PyBool_FromLong(self->flagged); |
| 1046 |
return PyObject_GetAttrString((PyObject *)self, name); |
| 1047 |
} |
| 1048 |
|
| 1049 |
static PyTypeObject BoxType = { |
| 1050 |
PyVarObject_HEAD_INIT(NULL,0) |
| 1051 |
"_rl_accel.Box", |
| 1052 |
sizeof(BoxObject), |
| 1053 |
0, |
| 1054 |
|
| 1055 |
(destructor)BoxFree, |
| 1056 |
(printfunc)0, |
| 1057 |
(getattrfunc)Box_getattr, |
| 1058 |
(setattrfunc)Box_setattr, |
| 1059 |
0, |
| 1060 |
0, |
| 1061 |
0, |
| 1062 |
0, |
| 1063 |
0, |
| 1064 |
0, |
| 1065 |
0, |
| 1066 |
0, |
| 1067 |
0, |
| 1068 |
0, |
| 1069 |
0, |
| 1070 |
Py_TPFLAGS_DEFAULT, |
| 1071 |
"Box instance, see doc string for details.", |
| 1072 |
0, |
| 1073 |
0, |
| 1074 |
0, |
| 1075 |
0, |
| 1076 |
0, |
| 1077 |
0, |
| 1078 |
Box_methods, |
| 1079 |
0, |
| 1080 |
0, |
| 1081 |
0, |
| 1082 |
0, |
| 1083 |
0, |
| 1084 |
0, |
| 1085 |
0, |
| 1086 |
0, |
| 1087 |
0, |
| 1088 |
0, |
| 1089 |
0, |
| 1090 |
0, |
| 1091 |
}; |
| 1092 |
|
| 1093 |
static BoxObject* Box(PyObject* module, PyObject* args, PyObject* kw) |
| 1094 |
{ |
| 1095 |
BoxObject* self; |
| 1096 |
char *kwlist[] = {"width","character",NULL}; |
| 1097 |
PyObject *pC=NULL; |
| 1098 |
double w; |
| 1099 |
|
| 1100 |
if(!PyArg_ParseTupleAndKeywords(args,kw,"d|O:Box",kwlist,&w,&pC)) return NULL; |
| 1101 |
if(!(self = PyObject_New(BoxObject, &BoxType))) return NULL; |
| 1102 |
self->shrink = self->stretch = self->penalty = (double)(self->is_glue = self->is_penalty = self->flagged = 0); |
| 1103 |
self->is_box = 1; |
| 1104 |
self->width = w; |
| 1105 |
if(Box_set_character(self, pC ? pC : Py_None)){ |
| 1106 |
BoxFree(self); |
| 1107 |
return NULL; |
| 1108 |
} |
| 1109 |
|
| 1110 |
return self; |
| 1111 |
} |
| 1112 |
|
| 1113 |
static BoxObject* Glue(PyObject* module, PyObject* args, PyObject* kw) |
| 1114 |
{ |
| 1115 |
BoxObject* self; |
| 1116 |
char *kwlist[] = {"width","stretch","shrink",NULL}; |
| 1117 |
double width,stretch,shrink; |
| 1118 |
|
| 1119 |
if(!PyArg_ParseTupleAndKeywords(args,kw,"ddd:Glue",kwlist,&width,&stretch,&shrink)) return NULL; |
| 1120 |
if(!(self = PyObject_New(BoxObject, &BoxType))) return NULL; |
| 1121 |
self->penalty = (double)(self->is_box = self->is_penalty = self->flagged = 0); |
| 1122 |
self->is_glue = self->is_none = 1; |
| 1123 |
self->width = width; |
| 1124 |
self->stretch = stretch; |
| 1125 |
self->shrink = shrink; |
| 1126 |
|
| 1127 |
return self; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static BoxObject* Penalty(PyObject* module, PyObject* args, PyObject* kw) |
| 1131 |
{ |
| 1132 |
BoxObject* self; |
| 1133 |
char *kwlist[] = {"width","penalty","flagged",NULL}; |
| 1134 |
double width,penalty; |
| 1135 |
int flagged = 0; |
| 1136 |
|
| 1137 |
if(!PyArg_ParseTupleAndKeywords(args,kw,"dd|i:Penalty",kwlist,&width,&penalty,&flagged)) return NULL; |
| 1138 |
if(!(self = PyObject_New(BoxObject, &BoxType))) return NULL; |
| 1139 |
self->shrink = self->stretch = (double)(self->is_box = self->is_glue = 0); |
| 1140 |
self->is_penalty = self->is_none = 1; |
| 1141 |
self->width = width; |
| 1142 |
self->penalty = penalty; |
| 1143 |
self->flagged = flagged; |
| 1144 |
return self; |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
typedef struct { |
| 1149 |
PyListObject list; |
| 1150 |
int state; |
| 1151 |
} BoxListobject; |
| 1152 |
|
| 1153 |
static PyObject *BoxList_getstate(BoxListobject *self, PyObject *args) |
| 1154 |
{ |
| 1155 |
if (!PyArg_ParseTuple(args, ":getstate")) return NULL; |
| 1156 |
return PyLong_FromLong(self->state); |
| 1157 |
} |
| 1158 |
|
| 1159 |
static PyObject *BoxList_setstate(BoxListobject *self, PyObject *args) |
| 1160 |
{ |
| 1161 |
int state; |
| 1162 |
|
| 1163 |
if (!PyArg_ParseTuple(args, "i:setstate", &state)) |
| 1164 |
return NULL; |
| 1165 |
self->state = state; |
| 1166 |
Py_INCREF(Py_None); |
| 1167 |
return Py_None; |
| 1168 |
} |
| 1169 |
|
| 1170 |
static PyObject *BoxList_specialmeth(PyObject *self, PyObject *args, PyObject *kw) |
| 1171 |
{ |
| 1172 |
PyObject *result = PyTuple_New(3); |
| 1173 |
if(result!=NULL){ |
| 1174 |
if(self==NULL) self = Py_None; |
| 1175 |
if(kw==NULL) kw = Py_None; |
| 1176 |
Py_INCREF(self); |
| 1177 |
RLPyTuple_SetItem(result, 0, self); |
| 1178 |
Py_INCREF(args); |
| 1179 |
RLPyTuple_SetItem(result, 1, args); |
| 1180 |
Py_INCREF(kw); |
| 1181 |
RLPyTuple_SetItem(result, 2, kw); |
| 1182 |
} |
| 1183 |
return result; |
| 1184 |
} |
| 1185 |
|
| 1186 |
static PyMethodDef BoxList_methods[] = { |
| 1187 |
{"getstate", (PyCFunction)BoxList_getstate, METH_VARARGS, "getstate() -> state"}, |
| 1188 |
{"setstate", (PyCFunction)BoxList_setstate, METH_VARARGS, "setstate(state)"}, |
| 1189 |
|
| 1190 |
{"classmeth", (PyCFunction)BoxList_specialmeth, METH_VARARGS | METH_KEYWORDS | METH_CLASS, "classmeth(*args, **kw)"}, |
| 1191 |
{"staticmeth", (PyCFunction)BoxList_specialmeth, METH_VARARGS | METH_KEYWORDS | METH_STATIC, "staticmeth(*args, **kw)"}, |
| 1192 |
{NULL, NULL}, |
| 1193 |
}; |
| 1194 |
|
| 1195 |
static PyTypeObject BoxList_type; |
| 1196 |
|
| 1197 |
static int BoxList_init(BoxListobject *self, PyObject *args, PyObject *kwds) |
| 1198 |
{ |
| 1199 |
if(PyList_Type.tp_init((PyObject *)self, args, kwds)<0) return -1; |
| 1200 |
self->state = 0; |
| 1201 |
return 0; |
| 1202 |
} |
| 1203 |
|
| 1204 |
static PyObject *BoxList_state_get(BoxListobject *self) |
| 1205 |
{ |
| 1206 |
return PyLong_FromLong(self->state); |
| 1207 |
} |
| 1208 |
|
| 1209 |
static PyGetSetDef BoxList_getsets[] = { |
| 1210 |
{"state", (getter)BoxList_state_get, NULL, PyDoc_STR("an int variable for demonstration purposes")}, |
| 1211 |
{0} |
| 1212 |
}; |
| 1213 |
|
| 1214 |
static PyTypeObject BoxList_type = { |
| 1215 |
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) |
| 1216 |
"_rl_accel.BoxList", |
| 1217 |
sizeof(BoxListobject), |
| 1218 |
0, |
| 1219 |
0, |
| 1220 |
0, |
| 1221 |
0, |
| 1222 |
0, |
| 1223 |
0, |
| 1224 |
0, |
| 1225 |
0, |
| 1226 |
0, |
| 1227 |
0, |
| 1228 |
0, |
| 1229 |
0, |
| 1230 |
0, |
| 1231 |
0, |
| 1232 |
0, |
| 1233 |
0, |
| 1234 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 1235 |
0, |
| 1236 |
0, |
| 1237 |
0, |
| 1238 |
0, |
| 1239 |
0, |
| 1240 |
0, |
| 1241 |
0, |
| 1242 |
BoxList_methods, |
| 1243 |
0, |
| 1244 |
BoxList_getsets, |
| 1245 |
DEFERRED_ADDRESS(&PyList_Type), |
| 1246 |
0, |
| 1247 |
0, |
| 1248 |
0, |
| 1249 |
0, |
| 1250 |
(initproc)BoxList_init, |
| 1251 |
0, |
| 1252 |
0, |
| 1253 |
}; |
| 1254 |
#endif |
| 1255 |
|
| 1256 |
#ifdef HAVE_BOX |
| 1257 |
#define _BOX__DOC__ \ |
| 1258 |
"\tBox(width,character=None) creates a Knuth character Box with the specified width.\n" \ |
| 1259 |
"\tGlue(width,stretch,shrink) creates a Knuth glue Box with the specified width, stretch and shrink.\n" \ |
| 1260 |
"\tPenalty(width,penalty,flagged=0) creates a Knuth penalty Box with the specified width and penalty.\n" \ |
| 1261 |
"\tBoxList() creates a knuth box list.\n" |
| 1262 |
#else |
| 1263 |
#define _BOX__DOC__ |
| 1264 |
#endif |
| 1265 |
|
| 1266 |
PyDoc_STRVAR(__DOC__, |
| 1267 |
"_rl_accel contains various accelerated utilities\n\ |
| 1268 |
\n\ |
| 1269 |
\tescapePDF makes a unicode or latin1 bytes safe for PDF\n\ |
| 1270 |
\n\ |
| 1271 |
\tasciiBase85Encode does what is says\n\ |
| 1272 |
\tasciiBase85Decode does what is says\n\ |
| 1273 |
\n\ |
| 1274 |
\tfp_str converts numeric arguments to a single blank separated string\n\ |
| 1275 |
\tcalcChecksum calculate checksums for TTFs (legacy)\n\ |
| 1276 |
\tadd32 32 bit unsigned addition (legacy)\n\ |
| 1277 |
\thex32 32 bit unsigned to 0X8.8X string\n\ |
| 1278 |
\tinstanceStringWidthT1 version2 Font instance stringWidth\n\ |
| 1279 |
\tinstanceStringWidthTTF version2 TTFont instance stringWidth\n\ |
| 1280 |
\tunicode2T1 version2 pdfmetrics.unicode2T1\n" |
| 1281 |
_BOX__DOC__ |
| 1282 |
); |
| 1283 |
|
| 1284 |
static struct PyMethodDef _methods[] = { |
| 1285 |
{"asciiBase85Encode", _a85_encode, METH_VARARGS, "asciiBase85Encode(\".....\") return encoded " STRNAME}, |
| 1286 |
{"asciiBase85Decode", _a85_decode, METH_VARARGS, "asciiBase85Decode(\".....\") return decoded " BYTESNAME}, |
| 1287 |
{"escapePDF", escapePDF, METH_VARARGS, "escapePDF(s) return PDF safed string"}, |
| 1288 |
{"fp_str", _fp_str, METH_VARARGS, "fp_str(a0, a1,...) convert numerics to blank separated string"}, |
| 1289 |
{"sameFrag", sameFrag, 1, "sameFrag(f,g) return 1 if fragments have same style"}, |
| 1290 |
{"calcChecksum", ttfonts_calcChecksum, METH_VARARGS, "calcChecksum(string) calculate checksums for TTFs (returns long)"}, |
| 1291 |
{"add32", ttfonts_add32, METH_VARARGS, "add32(x,y) 32 bit unsigned x+y (returns long)"}, |
| 1292 |
{"hex32", hex32, METH_VARARGS, "hex32(x) 32 bit unsigned-->0X8.8X string"}, |
| 1293 |
{"unicode2T1", (PyCFunction)unicode2T1, METH_VARARGS|METH_KEYWORDS, "return a list of (font,string) pairs representing the unicode text"}, |
| 1294 |
{"instanceStringWidthT1", (PyCFunction)instanceStringWidthT1, METH_VARARGS|METH_KEYWORDS, "Font.stringWidth(self,text,fontName,fontSize,encoding='utf8') --> width"}, |
| 1295 |
{"instanceStringWidthTTF", (PyCFunction)instanceStringWidthTTF, METH_VARARGS|METH_KEYWORDS, "TTFont.stringWidth(self,text,fontName,fontSize,encoding='utf8') --> width"}, |
| 1296 |
#ifdef HAVE_BOX |
| 1297 |
{"Box", (PyCFunction)Box, METH_VARARGS|METH_KEYWORDS, "Box(width,character=None) create a Knuth Box instance"}, |
| 1298 |
{"Glue", (PyCFunction)Glue, METH_VARARGS|METH_KEYWORDS, "Glue(width,stretch,shrink) create a Knuth Glue instance"}, |
| 1299 |
{"Penalty", (PyCFunction)Penalty, METH_VARARGS|METH_KEYWORDS, "Penalty(width,penalty,flagged=0) create a Knuth Penalty instance"}, |
| 1300 |
#endif |
| 1301 |
{NULL, NULL} |
| 1302 |
}; |
| 1303 |
|
| 1304 |
|
| 1305 |
static struct PyModuleDef moduledef = { |
| 1306 |
PyModuleDef_HEAD_INIT, |
| 1307 |
"_rl_accel", |
| 1308 |
__DOC__, |
| 1309 |
MODULE_STATE_SIZE, |
| 1310 |
_methods, |
| 1311 |
NULL, |
| 1312 |
NULL, |
| 1313 |
NULL, |
| 1314 |
NULL |
| 1315 |
}; |
| 1316 |
|
| 1317 |
PyMODINIT_FUNC PyInit__rl_accel(void) |
| 1318 |
{ |
| 1319 |
PyObject *module=NULL, *moduleVersion=NULL; |
| 1320 |
|
| 1321 |
module = PyModule_Create(&moduledef); |
| 1322 |
if(!module) goto err; |
| 1323 |
|
| 1324 |
moduleVersion = PyUnicode_FromString(VERSION); |
| 1325 |
if(!moduleVersion)goto err; |
| 1326 |
PyModule_AddObject(module, "version", moduleVersion); |
| 1327 |
|
| 1328 |
#ifdef HAVE_BOX |
| 1329 |
if(PyType_Ready(&BoxType)<0) goto err; |
| 1330 |
BoxList_type.tp_base = &PyList_Type; |
| 1331 |
if(PyType_Ready(&BoxList_type)<0) goto err; |
| 1332 |
Py_INCREF(&BoxList_type); |
| 1333 |
if(PyModule_AddObject(module, "BoxList", (PyObject *)&BoxList_type)<0)goto err; |
| 1334 |
#endif |
| 1335 |
|
| 1336 |
return module; |
| 1337 |
|
| 1338 |
err: |
| 1339 |
Py_XDECREF(moduleVersion); |
| 1340 |
Py_XDECREF(module); |
| 1341 |
return NULL; |
| 1342 |
} |