Skip to content

Commit bffaabb

Browse files
committed
upload v1.14.3
1 parent 7b11402 commit bffaabb

File tree

9 files changed

+491
-273
lines changed

9 files changed

+491
-273
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PyMuPDF 1.14.2
1+
# PyMuPDF 1.14.3
22

33
![logo](https://github.com/rk700/PyMuPDF/blob/master/demo/pymupdf.jpg)
44

@@ -14,7 +14,7 @@ On **[PyPI](https://pypi.org/project/PyMuPDF)** since August 2016: [![](https://
1414

1515
# Introduction
1616

17-
This is **version 1.14.2 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.14.x](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer".
17+
This is **version 1.14.3 of PyMuPDF (formerly python-fitz)**, a Python binding with support for [MuPDF 1.14.x](http://mupdf.com/) - "a lightweight PDF, XPS, and E-book viewer".
1818

1919
MuPDF can access files in PDF, XPS, OpenXPS, CBZ, EPUB and FB2 (e-books) formats, and it is known for its top performance and high rendering quality.
2020

fitz/fitz.i

+153-102
Original file line numberDiff line numberDiff line change
@@ -1166,57 +1166,54 @@ if links:
11661166
FITZEXCEPTION(_getCharWidths, !result)
11671167
CLOSECHECK(_getCharWidths)
11681168
%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)
11701171
{
11711172
pdf_document *pdf = pdf_specifics(gctx, $self);
11721173
PyObject *wlist = NULL;
11731174
int i, glyph, mylimit;
11741175
mylimit = limit;
11751176
if (mylimit < 256) mylimit = 256;
11761177
int cwlen = 0;
1178+
int lang = 0;
11771179
const char *data;
1178-
int size;
1179-
fz_font *font = NULL;
1180+
int size, index;
1181+
fz_font *font = NULL, *fb_font= NULL;
11801182
fz_buffer *buf = NULL;
1181-
pdf_obj *basefont = NULL;
1182-
const char *bfname = NULL;
1183+
11831184
fz_try(gctx)
11841185
{
11851186
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)
11891188
{
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;
12061192
}
1207-
else
1193+
data = fz_lookup_base14_font(gctx, bfname, &size);
1194+
if (data)
12081195
{
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;
12121198
}
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:;
12131204
wlist = PyList_New(0);
1205+
float adv;
12141206
for (i = 0; i < mylimit; i++)
12151207
{
12161208
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+
12171214
if (glyph > 0)
12181215
{
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));
12201217
}
12211218
else
12221219
{
@@ -3163,106 +3160,160 @@ fannot._erase()
31633160
//---------------------------------------------------------------------
31643161
// insert font
31653162
//---------------------------------------------------------------------
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]
31703197
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)
31943215
{
31953216
pdf_page *page = pdf_page_from_fz_page(gctx, $self);
31963217
pdf_document *pdf;
31973218
pdf_obj *resources, *fonts, *font_obj;
31983219
fz_font *font;
31993220
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;
32023224
fz_try(gctx)
32033225
{
32043226
assert_PDF(page);
32053227
pdf = page->doc;
3206-
// get objects "Resources", "Resources/Font"
3228+
// get the objects /Resources, /Resources/Font
32073229
resources = pdf_dict_get(gctx, page->obj, PDF_NAME(Resources));
32083230
fonts = pdf_dict_get(gctx, resources, PDF_NAME(Font));
3209-
int simple = 0;
32103231
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
32153232
{
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);
32193235
}
3220-
else
3236+
3237+
//-------------------------------------------------------------
3238+
// check for CJK font
3239+
//-------------------------------------------------------------
3240+
if (ordering > -1)
32213241
{
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)
32243244
{
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");
32373248
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;
32433250
}
32443251
}
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);
32503269
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+
32593309
// resources and fonts objects will contain named reference to font
32603310
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);
32623313
}
32633314
fz_catch(gctx) return NULL;
32643315
pdf->dirty = 1;
3265-
return Py_BuildValue("[i, O]", ixref, info);
3316+
return value;
32663317
}
32673318
32683319
//---------------------------------------------------------------------

0 commit comments

Comments
 (0)