@@ -1166,57 +1166,54 @@ if links:
1166
1166
FITZEXCEPTION(_getCharWidths, !result)
1167
1167
CLOSECHECK(_getCharWidths)
1168
1168
%feature("autodoc","Return list of glyphs and glyph widths of a font.") _getCharWidths;
1169
- PyObject *_getCharWidths(int xref, int limit, int idx = 0)
1169
+ PyObject *_getCharWidths(int xref, char *bfname, char *ext,
1170
+ int ordering, int limit, int idx = 0)
1170
1171
{
1171
1172
pdf_document *pdf = pdf_specifics(gctx, $self);
1172
1173
PyObject *wlist = NULL;
1173
1174
int i, glyph, mylimit;
1174
1175
mylimit = limit;
1175
1176
if (mylimit < 256) mylimit = 256;
1176
1177
int cwlen = 0;
1178
+ int lang = 0;
1177
1179
const char *data;
1178
- int size;
1179
- fz_font *font = NULL;
1180
+ int size, index ;
1181
+ fz_font *font = NULL, *fb_font= NULL ;
1180
1182
fz_buffer *buf = NULL;
1181
- pdf_obj *basefont = NULL;
1182
- const char *bfname = NULL;
1183
+
1183
1184
fz_try(gctx)
1184
1185
{
1185
1186
assert_PDF(pdf);
1186
- if (xref < 1) THROWMSG("xref must at least 1");
1187
- pdf_obj *o = pdf_load_object(gctx, pdf, xref);
1188
- if (pdf_is_dict(gctx, o))
1187
+ if (ordering >= 0)
1189
1188
{
1190
- basefont = pdf_dict_get(gctx, o, PDF_NAME(BaseFont));
1191
- if (pdf_is_name(gctx, basefont))
1192
- {
1193
- bfname = (char *) pdf_to_name(gctx, basefont);
1194
- data = fz_lookup_base14_font(gctx, bfname, &size);
1195
- if (data)
1196
- {
1197
- font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0);
1198
- }
1199
- else
1200
- {
1201
- buf = fontbuffer(gctx, pdf, xref);
1202
- if (!buf) THROWMSG("xref is not a supported font");
1203
- font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0);
1204
- }
1205
- }
1189
+ data = fz_lookup_cjk_font(gctx, ordering, &size, &index);
1190
+ font = fz_new_font_from_memory(gctx, NULL, data, size, index, 0);
1191
+ goto weiter;
1206
1192
}
1207
- else
1193
+ data = fz_lookup_base14_font(gctx, bfname, &size);
1194
+ if (data)
1208
1195
{
1209
- buf = fontbuffer(gctx, pdf, xref);
1210
- if (!buf) THROWMSG("xref is not a supported font");
1211
- font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0);
1196
+ font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0);
1197
+ goto weiter;
1212
1198
}
1199
+ buf = fontbuffer(gctx, pdf, xref);
1200
+ if (!buf) THROWMSG("xref is not a supported font");
1201
+ font = fz_new_font_from_buffer(gctx, NULL, buf, idx, 0);
1202
+
1203
+ weiter:;
1213
1204
wlist = PyList_New(0);
1205
+ float adv;
1214
1206
for (i = 0; i < mylimit; i++)
1215
1207
{
1216
1208
glyph = fz_encode_character(gctx, font, i);
1209
+ adv = fz_advance_glyph(gctx, font, glyph, 0);
1210
+ if (ordering >= 0)
1211
+ glyph = i;
1212
+
1213
+
1217
1214
if (glyph > 0)
1218
1215
{
1219
- PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, fz_advance_glyph(gctx, font, glyph, 0) ));
1216
+ PyList_Append(wlist, Py_BuildValue("(i, f)", glyph, adv ));
1220
1217
}
1221
1218
else
1222
1219
{
@@ -3163,106 +3160,160 @@ fannot._erase()
3163
3160
//---------------------------------------------------------------------
3164
3161
// insert font
3165
3162
//---------------------------------------------------------------------
3166
- FITZEXCEPTION(insertFont, !result)
3167
- %pythonprepend insertFont %{
3168
- if not self.parent:
3169
- raise ValueError(" orphaned object: parent is None" )
3163
+ %pythoncode
3164
+ %{
3165
+ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
3166
+ set_simple=False, idx=0, wmode=0, style=0, encoding=0):
3167
+ if not self.parent:
3168
+ raise ValueError(" orphaned object: parent is None" )
3169
+ f = CheckFont(self, fontname)
3170
+ if f is not None: # drop out if fontname already in page list
3171
+ return f[0]
3172
+ bfname = " "
3173
+
3174
+ if not fontname:
3175
+ fontname = " helv"
3176
+
3177
+ if fontname.lower() in Base14_fontdict.keys():
3178
+ bfname = Base14_fontdict[fontname.lower()]
3179
+
3180
+ serif = 0
3181
+ CJK_number = -1
3182
+ CJK_list_n = [" china-t" , " china-s" , " japan" , " korea" ]
3183
+ CJK_list_s = [" china-ts" , " china-ss" , " japan-s" , " korea-s" ]
3184
+ if fontname in CJK_list_n:
3185
+ CJK_number = CJK_list.index(fontname)
3186
+ serif = 0
3187
+
3188
+ if fontname in CJK_list_s:
3189
+ CJK_number = CJK_list_s.index(fontname)
3190
+ serif = 1
3191
+
3192
+ val = self._insertFont(fontname, bfname, fontfile, fontbuffer, set_simple, idx,
3193
+ wmode, style, serif, encoding, CJK_number)
3194
+
3195
+ if val:
3196
+ xref = val[0]
3170
3197
f = CheckFont(self, fontname)
3171
- if f is not None: # drop out if fontname already in page list
3172
- return f[0]
3173
- if not fontname:
3174
- fontname = " Helvetica"
3175
- if xref > 0:
3176
- _, _, _, fontbuffer = self.parent.extractFont(xref)
3177
- if not fontbuffer:
3178
- raise ValueError(" xref is no valid font" )
3179
- %}
3180
- %pythonappend insertFont %{
3181
- if val:
3182
- xref = val[0]
3183
- f = CheckFont(self, fontname)
3184
- if f is not None:
3185
- val[1][" type" ] = f[2] # put /Subtype in font info
3186
- val[1][" glyphs" ] = None
3187
- doc = self.parent # now add to document font info
3188
- fi = CheckFontInfo(doc, xref)
3189
- if fi is None: # look if we are already present
3190
- doc.FontInfos.append(val) # no: add me to document object
3191
- return xref
3192
- %}
3193
- PyObject *insertFont(const char *fontname = NULL, const char *fontfile = NULL, PyObject *fontbuffer = NULL, int xref = 0, int set_simple = 0, int idx = 0)
3198
+ if f is not None:
3199
+ val[1][" type" ] = f[2] # put /Subtype in font info
3200
+ val[1][" glyphs" ] = None
3201
+ doc = self.parent # now add to document font info
3202
+ fi = CheckFontInfo(doc, xref)
3203
+ if fi is None: # look if we are already present
3204
+ doc.FontInfos.append(val) # no: add me to document object
3205
+ return xref
3206
+ %}
3207
+
3208
+ FITZEXCEPTION(_insertFont, !result)
3209
+ PyObject *_insertFont(const char *fontname, const char *bfname,
3210
+ const char *fontfile,
3211
+ PyObject *fontbuffer,
3212
+ int set_simple, int idx,
3213
+ int wmode, int style, int serif,
3214
+ int encoding, int ordering)
3194
3215
{
3195
3216
pdf_page *page = pdf_page_from_fz_page(gctx, $self);
3196
3217
pdf_document *pdf;
3197
3218
pdf_obj *resources, *fonts, *font_obj;
3198
3219
fz_font *font;
3199
3220
const char *data = NULL;
3200
- int size, ixref = 0;
3201
- PyObject *info;
3221
+ int size, ixref = 0, index = 0, simple = 0;
3222
+ PyObject *info, *value;
3223
+ PyObject *exto = NULL;
3202
3224
fz_try(gctx)
3203
3225
{
3204
3226
assert_PDF(page);
3205
3227
pdf = page->doc;
3206
- // get objects " Resources" , " Resources/Font"
3228
+ // get the objects / Resources, / Resources/Font
3207
3229
resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources));
3208
3230
fonts = pdf_dict_get(gctx, resources, PDF_NAME(Font));
3209
- int simple = 0;
3210
3231
if (!fonts) // page has no fonts yet
3211
- fonts = pdf_add_object_drop(gctx, pdf, pdf_new_dict(gctx, pdf, 1));
3212
-
3213
- data = fz_lookup_base14_font(gctx, fontname, &size);
3214
- if (data) // base 14 font found
3215
3232
{
3216
- font = fz_new_font_from_memory(gctx, fontname, data, size, 0, 0);
3217
- font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN);
3218
- simple = 1;
3233
+ fonts = pdf_new_dict(gctx, pdf, 10);
3234
+ pdf_dict_put_drop(gctx, resources, PDF_NAME(Font), fonts);
3219
3235
}
3220
- else
3236
+
3237
+ //-------------------------------------------------------------
3238
+ // check for CJK font
3239
+ //-------------------------------------------------------------
3240
+ if (ordering > -1)
3221
3241
{
3222
- if (!fontfile && !fontbuffer) THROWMSG( " unknown PDF Base 14 font " );
3223
- if (fontfile )
3242
+ data = fz_lookup_cjk_font(gctx, ordering, &size, &index );
3243
+ if (data )
3224
3244
{
3225
- font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0);
3226
- }
3227
- else
3228
- {
3229
- if (!PyBytes_Check(fontbuffer)) THROWMSG(" fontbuffer must be bytes" );
3230
- data = PyBytes_AsString(fontbuffer);
3231
- size = PyBytes_Size(fontbuffer);
3232
- font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0);
3233
- }
3234
- if (set_simple == 0)
3235
- {
3236
- font_obj = pdf_add_cid_font(gctx, pdf, font);
3245
+ font = fz_new_font_from_memory(gctx, NULL, data, size, index, 0);
3246
+ font_obj = pdf_add_cjk_font(gctx, pdf, font, ordering, wmode, serif);
3247
+ exto = Py_BuildValue(" s" , " n/a" );
3237
3248
simple = 0;
3238
- }
3239
- else
3240
- {
3241
- font_obj = pdf_add_simple_font(gctx, pdf, font, PDF_SIMPLE_ENCODING_LATIN);
3242
- simple = 2;
3249
+ goto weiter;
3243
3250
}
3244
3251
}
3245
- ixref = pdf_to_num(gctx, font_obj);
3246
- PyObject *name = PyString_FromString(fz_font_name(gctx, font));
3247
- PyObject *exto;
3248
- if (simple != 1)
3249
- exto = PyString_FromString(fontextension(gctx, pdf, ixref));
3252
+
3253
+ //-------------------------------------------------------------
3254
+ // check for PDF Base-14 font
3255
+ //-------------------------------------------------------------
3256
+ data = fz_lookup_base14_font(gctx, bfname, &size);
3257
+ if (data)
3258
+ {
3259
+ font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0);
3260
+ font_obj = pdf_add_simple_font(gctx, pdf, font, encoding);
3261
+ exto = Py_BuildValue(" s" , " n/a" );
3262
+ simple = 1;
3263
+ goto weiter;
3264
+ }
3265
+
3266
+ if (!fontfile && !fontbuffer) THROWMSG(" unknown Base-14 or CJK font" );
3267
+ if (fontfile)
3268
+ font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0);
3250
3269
else
3251
- exto = PyString_FromString(" n/a" );
3252
- PyObject *simpleo = JM_BOOL(simple);
3253
- PyObject *idxo = PyInt_FromLong((long) idx);
3254
- info = PyDict_New();
3255
- PyDict_SetItemString(info, " name" , name);
3256
- PyDict_SetItemString(info, " simple" , simpleo);
3257
- PyDict_SetItemString(info, " ext" , exto);
3258
- fz_drop_font(gctx, font);
3270
+ {
3271
+ if (!PyBytes_Check(fontbuffer)) THROWMSG(" fontbuffer must be bytes" );
3272
+ data = PyBytes_AsString(fontbuffer);
3273
+ size = PyBytes_Size(fontbuffer);
3274
+ font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0);
3275
+ }
3276
+ if (set_simple == 0)
3277
+ {
3278
+ font_obj = pdf_add_cid_font(gctx, pdf, font);
3279
+ simple = 0;
3280
+ }
3281
+ else
3282
+ {
3283
+ font_obj = pdf_add_simple_font(gctx, pdf, font, encoding);
3284
+ simple = 2;
3285
+ }
3286
+
3287
+ weiter: ;
3288
+ ixref = pdf_to_num(gctx, font_obj);
3289
+
3290
+ PyObject *name = Py_BuildValue(" s" , pdf_to_name(gctx,
3291
+ pdf_dict_get(gctx, font_obj, PDF_NAME(BaseFont))));
3292
+
3293
+ PyObject *subt = Py_BuildValue(" s" , pdf_to_name(gctx,
3294
+ pdf_dict_get(gctx, font_obj, PDF_NAME(Subtype))));
3295
+
3296
+ if (!exto)
3297
+ exto = Py_BuildValue(" s" , fontextension(gctx, pdf, ixref));
3298
+
3299
+ value = Py_BuildValue(" [i, {s:O, s:O, s:O, s:O, s:i}]" ,
3300
+ ixref,
3301
+ " name" , name,
3302
+ " type" , subt,
3303
+ " ext" , exto,
3304
+ " simple" , JM_BOOL(simple),
3305
+ " ordering" , ordering);
3306
+ Py_CLEAR(exto);
3307
+ Py_CLEAR(name);
3308
+
3259
3309
// resources and fonts objects will contain named reference to font
3260
3310
pdf_dict_puts(gctx, fonts, fontname, font_obj);
3261
- pdf_dict_put(gctx, resources, PDF_NAME(Font), fonts);
3311
+ pdf_drop_obj(gctx, font_obj);
3312
+ fz_drop_font(gctx, font);
3262
3313
}
3263
3314
fz_catch(gctx) return NULL;
3264
3315
pdf->dirty = 1;
3265
- return Py_BuildValue( " [i, O] " , ixref, info) ;
3316
+ return value ;
3266
3317
}
3267
3318
3268
3319
//---------------------------------------------------------------------
0 commit comments