@@ -3162,62 +3162,80 @@ fannot._erase()
3162
3162
//---------------------------------------------------------------------
3163
3163
%pythoncode
3164
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:
3165
+ def insertFont(self, fontname=" helv" , fontfile=None, fontbuffer=None,
3166
+ set_simple=False, wmode=0, encoding=0):
3167
+ doc = self.parent
3168
+ if not doc:
3168
3169
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 = " "
3170
+ idx = 0
3171
+
3172
+ if fontname.startswith(" /" ):
3173
+ fontname = fontname[1:]
3174
+
3175
+ font = CheckFont(self, fontname)
3176
+ if font is not None: # font already in font list of page
3177
+ xref = font[0] # this is the xref
3178
+ if CheckFontInfo(doc, xref): # also in our document font list?
3179
+ return xref # yes: we are done
3180
+ # need to build the doc FontInfo entry - done via getCharWidths
3181
+ doc.getCharWidths(xref)
3182
+ return xref
3173
3183
3174
- if not fontname:
3175
- fontname = " helv"
3184
+ #--------------------------------------------------------------------------
3185
+ # the font is not present for this page
3186
+ #--------------------------------------------------------------------------
3176
3187
3177
- if fontname.lower() in Base14_fontdict.keys():
3178
- bfname = Base14_fontdict[fontname.lower()]
3188
+ bfname = Base14_fontdict.get(fontname.lower(), None) # BaseFont if Base-14 font
3179
3189
3180
3190
serif = 0
3181
3191
CJK_number = -1
3182
3192
CJK_list_n = [" china-t" , " china-s" , " japan" , " korea" ]
3183
3193
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)
3194
+
3195
+ try:
3196
+ CJK_number = CJK_list_n.index(fontname)
3186
3197
serif = 0
3198
+ except:
3199
+ pass
3187
3200
3188
- if fontname in CJK_list_s:
3189
- CJK_number = CJK_list_s.index(fontname)
3190
- serif = 1
3201
+ if CJK_number < 0:
3202
+ try:
3203
+ CJK_number = CJK_list_s.index(fontname)
3204
+ serif = 1
3205
+ except:
3206
+ pass
3191
3207
3208
+ # install the font for the page
3192
3209
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]
3197
- f = CheckFont(self, fontname)
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
3210
+ wmode, serif, encoding, CJK_number)
3211
+
3212
+ if not val: # did not work, error return
3213
+ return val
3214
+
3215
+ xref = val[0] # xref of installed font
3216
+
3217
+ if CheckFontInfo(doc, xref): # check again: document already has this font
3218
+ return xref # we are done
3219
+
3220
+ # need to create document font info
3221
+ doc.getCharWidths(xref)
3222
+ return xref
3223
+
3206
3224
%}
3207
3225
3208
3226
FITZEXCEPTION(_insertFont, !result)
3209
- PyObject *_insertFont(const char *fontname, const char *bfname,
3210
- const char *fontfile,
3227
+ PyObject *_insertFont(char *fontname, char *bfname,
3228
+ char *fontfile,
3211
3229
PyObject *fontbuffer,
3212
3230
int set_simple, int idx,
3213
- int wmode, int style, int serif,
3231
+ int wmode, int serif,
3214
3232
int encoding, int ordering)
3215
3233
{
3216
3234
pdf_page *page = pdf_page_from_fz_page(gctx, $self);
3217
3235
pdf_document *pdf;
3218
3236
pdf_obj *resources, *fonts, *font_obj;
3219
3237
fz_font *font;
3220
- const char *data = NULL;
3238
+ char *data = NULL;
3221
3239
int size, ixref = 0, index = 0, simple = 0;
3222
3240
PyObject *info, *value;
3223
3241
PyObject *exto = NULL;
@@ -3237,23 +3255,20 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
3237
3255
//-------------------------------------------------------------
3238
3256
// check for CJK font
3239
3257
//-------------------------------------------------------------
3240
- if (ordering > -1)
3258
+ if (ordering > -1) data = fz_lookup_cjk_font(gctx, ordering, &size, &index);
3259
+ if (data)
3241
3260
{
3242
- data = fz_lookup_cjk_font(gctx, ordering, &size, &index);
3243
- if (data)
3244
- {
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" );
3248
- simple = 0;
3249
- goto weiter;
3250
- }
3261
+ font = fz_new_font_from_memory(gctx, NULL, data, size, index, 0);
3262
+ font_obj = pdf_add_cjk_font(gctx, pdf, font, ordering, wmode, serif);
3263
+ exto = Py_BuildValue(" s" , " n/a" );
3264
+ simple = 0;
3265
+ goto weiter;
3251
3266
}
3252
3267
3253
3268
//-------------------------------------------------------------
3254
3269
// check for PDF Base-14 font
3255
3270
//-------------------------------------------------------------
3256
- data = fz_lookup_base14_font(gctx, bfname, &size);
3271
+ if (bfname) data = fz_lookup_base14_font(gctx, bfname, &size);
3257
3272
if (data)
3258
3273
{
3259
3274
font = fz_new_font_from_memory(gctx, bfname, data, size, 0, 0);
@@ -3263,17 +3278,16 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
3263
3278
goto weiter;
3264
3279
}
3265
3280
3266
- if (!fontfile && !fontbuffer) THROWMSG(" unknown Base-14 or CJK font" );
3267
3281
if (fontfile)
3268
3282
font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0);
3269
3283
else
3270
3284
{
3271
- if (!PyBytes_Check(fontbuffer)) THROWMSG(" fontbuffer must be bytes" );
3272
- data = PyBytes_AsString(fontbuffer);
3273
- size = PyBytes_Size(fontbuffer);
3285
+ size = (int) JM_CharFromBytesOrArray(fontbuffer, &data);
3286
+ if (!size) THROWMSG(" one of fontfile, fontbuffer must be given" );
3274
3287
font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0);
3275
3288
}
3276
- if (set_simple == 0)
3289
+
3290
+ if (!set_simple)
3277
3291
{
3278
3292
font_obj = pdf_add_cid_font(gctx, pdf, font);
3279
3293
simple = 0;
@@ -3283,7 +3297,7 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
3283
3297
font_obj = pdf_add_simple_font(gctx, pdf, font, encoding);
3284
3298
simple = 2;
3285
3299
}
3286
-
3300
+
3287
3301
weiter: ;
3288
3302
ixref = pdf_to_num(gctx, font_obj);
3289
3303
@@ -3298,13 +3312,14 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
3298
3312
3299
3313
value = Py_BuildValue(" [i, {s:O, s:O, s:O, s:O, s:i}]" ,
3300
3314
ixref,
3301
- " name" , name,
3302
- " type" , subt,
3303
- " ext" , exto,
3304
- " simple" , JM_BOOL(simple),
3305
- " ordering" , ordering);
3315
+ " name" , name, // base font name
3316
+ " type" , subt, // subtype
3317
+ " ext" , exto, // file extension
3318
+ " simple" , JM_BOOL(simple), // simple font?
3319
+ " ordering" , ordering); // CJK font?
3306
3320
Py_CLEAR(exto);
3307
3321
Py_CLEAR(name);
3322
+ Py_CLEAR(subt);
3308
3323
3309
3324
// resources and fonts objects will contain named reference to font
3310
3325
pdf_dict_puts(gctx, fonts, fontname, font_obj);
0 commit comments