Skip to content

Commit 7859515

Browse files
committed
generate v1.14.3
1 parent bffaabb commit 7859515

File tree

6 files changed

+516
-244
lines changed

6 files changed

+516
-244
lines changed

fitz/fitz.i

+70-55
Original file line numberDiff line numberDiff line change
@@ -3162,62 +3162,80 @@ fannot._erase()
31623162
//---------------------------------------------------------------------
31633163
%pythoncode
31643164
%{
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:
31683169
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
31733183
3174-
if not fontname:
3175-
fontname = "helv"
3184+
#--------------------------------------------------------------------------
3185+
# the font is not present for this page
3186+
#--------------------------------------------------------------------------
31763187
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
31793189
31803190
serif = 0
31813191
CJK_number = -1
31823192
CJK_list_n = ["china-t", "china-s", "japan", "korea"]
31833193
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)
31863197
serif = 0
3198+
except:
3199+
pass
31873200
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
31913207
3208+
# install the font for the page
31923209
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+
32063224
%}
32073225
32083226
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,
32113229
PyObject *fontbuffer,
32123230
int set_simple, int idx,
3213-
int wmode, int style, int serif,
3231+
int wmode, int serif,
32143232
int encoding, int ordering)
32153233
{
32163234
pdf_page *page = pdf_page_from_fz_page(gctx, $self);
32173235
pdf_document *pdf;
32183236
pdf_obj *resources, *fonts, *font_obj;
32193237
fz_font *font;
3220-
const char *data = NULL;
3238+
char *data = NULL;
32213239
int size, ixref = 0, index = 0, simple = 0;
32223240
PyObject *info, *value;
32233241
PyObject *exto = NULL;
@@ -3237,23 +3255,20 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
32373255
//-------------------------------------------------------------
32383256
// check for CJK font
32393257
//-------------------------------------------------------------
3240-
if (ordering > -1)
3258+
if (ordering > -1) data = fz_lookup_cjk_font(gctx, ordering, &size, &index);
3259+
if (data)
32413260
{
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;
32513266
}
32523267
32533268
//-------------------------------------------------------------
32543269
// check for PDF Base-14 font
32553270
//-------------------------------------------------------------
3256-
data = fz_lookup_base14_font(gctx, bfname, &size);
3271+
if (bfname) data = fz_lookup_base14_font(gctx, bfname, &size);
32573272
if (data)
32583273
{
32593274
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,
32633278
goto weiter;
32643279
}
32653280
3266-
if (!fontfile && !fontbuffer) THROWMSG("unknown Base-14 or CJK font");
32673281
if (fontfile)
32683282
font = fz_new_font_from_file(gctx, NULL, fontfile, idx, 0);
32693283
else
32703284
{
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");
32743287
font = fz_new_font_from_memory(gctx, NULL, data, size, idx, 0);
32753288
}
3276-
if (set_simple == 0)
3289+
3290+
if (!set_simple)
32773291
{
32783292
font_obj = pdf_add_cid_font(gctx, pdf, font);
32793293
simple = 0;
@@ -3283,7 +3297,7 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
32833297
font_obj = pdf_add_simple_font(gctx, pdf, font, encoding);
32843298
simple = 2;
32853299
}
3286-
3300+
32873301
weiter: ;
32883302
ixref = pdf_to_num(gctx, font_obj);
32893303
@@ -3298,13 +3312,14 @@ def insertFont(self, fontname=None, fontfile=None, fontbuffer=None,
32983312
32993313
value = Py_BuildValue("[i, {s:O, s:O, s:O, s:O, s:i}]",
33003314
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?
33063320
Py_CLEAR(exto);
33073321
Py_CLEAR(name);
3322+
Py_CLEAR(subt);
33083323
33093324
// resources and fonts objects will contain named reference to font
33103325
pdf_dict_puts(gctx, fonts, fontname, font_obj);

0 commit comments

Comments
 (0)