14
14
The version number is updated here (above and below in variable).
15
15
"""
16
16
17
- import errno
18
17
import hashlib
19
18
import io
20
19
import logging
@@ -202,17 +201,6 @@ def get_page_format(format, k=None):
202
201
raise FPDFPageFormatException (f"Arguments must be numbers: { args } " ) from e
203
202
204
203
205
- def load_cache (filename : Path ):
206
- """Return unpickled object, or None if cache unavailable"""
207
- if not filename :
208
- return None
209
- try :
210
- return pickle .loads (filename .read_bytes ())
211
- # File missing, unsupported pickle, etc
212
- except (OSError , ValueError ):
213
- return None
214
-
215
-
216
204
def check_page (fn ):
217
205
"""Decorator to protect drawing methods"""
218
206
@@ -232,7 +220,11 @@ class FPDF(GraphicsStateMixin):
232
220
MARKDOWN_UNDERLINE_MARKER = "--"
233
221
234
222
def __init__ (
235
- self , orientation = "portrait" , unit = "mm" , format = "A4" , font_cache_dir = True
223
+ self ,
224
+ orientation = "portrait" ,
225
+ unit = "mm" ,
226
+ format = "A4" ,
227
+ font_cache_dir = "DEPRECATED" ,
236
228
):
237
229
"""
238
230
Args:
@@ -245,11 +237,13 @@ def __init__(
245
237
Default to "mm".
246
238
format (str): possible values are "a3", "a4", "a5", "letter", "legal" or a tuple
247
239
(width, height) expressed in the given unit. Default to "a4".
248
- font_cache_dir (Path or str): directory where pickle files
249
- for TTF font files are kept.
250
- `None` disables font chaching.
251
- The default is `True`, meaning the current folder.
240
+ font_cache_dir (Path or str): [**DEPRECATED**] unused
252
241
"""
242
+ if font_cache_dir != "DEPRECATED" :
243
+ warnings .warn (
244
+ '"font_cache_dir" parameter is deprecated, unused and will soon be removed' ,
245
+ PendingDeprecationWarning ,
246
+ )
253
247
super ().__init__ ()
254
248
# Initialization of instance attributes
255
249
self .offsets = {} # array of object offsets
@@ -272,7 +266,6 @@ def __init__(
272
266
273
267
self .ws = 0 # word spacing
274
268
self .angle = 0 # used by deprecated method: rotate()
275
- self .font_cache_dir = font_cache_dir
276
269
self .xmp_metadata = None
277
270
self .image_filter = "AUTO"
278
271
self .page_duration = 0 # optional pages display duration, cf. add_page()
@@ -1428,13 +1421,13 @@ def solid_arc(
1428
1421
style ,
1429
1422
)
1430
1423
1431
- def add_font (self , family , style = "" , fname = None , uni = False ):
1424
+ def add_font (self , family , style = "" , fname = None , uni = "DEPRECATED" ):
1432
1425
"""
1433
- Imports a TrueType, OpenType or Type1 font and makes it available
1426
+ Imports a TrueType or OpenType font and makes it available
1434
1427
for later calls to the `set_font()` method.
1435
1428
1436
- **Warning:** for Type1 and legacy fonts it is necessary to generate a font definition file first with the `MakeFont` utility.
1437
- This feature is currently deprecated in favour of TrueType Unicode font support
1429
+ **Warning:** there is partial support for Type1 and legacy fonts in .pkl font definition files,
1430
+ generated by the `MakeFont` utility, but this feature is getting deprecated in favour of TrueType Unicode font support
1438
1431
(whose fonts are automatically processed with the included `ttfonts.py` module).
1439
1432
1440
1433
You will find more information on the "Unicode" documentation page.
@@ -1444,17 +1437,13 @@ def add_font(self, family, style="", fname=None, uni=False):
1444
1437
style (str): font style. "B" for bold, "I" for italic.
1445
1438
fname (str): font file name. You can specify a relative or full path.
1446
1439
If the file is not found, it will be searched in `FPDF_FONT_DIR`.
1447
- uni (bool): if set to `True`, enable TrueType font subset embedding.
1448
- Text will then be treated as `utf8` by default.
1449
- Calling this method with uni=False is discouraged as legacy font support is complex and deprecated.
1450
-
1451
- Notes
1452
- -----
1453
-
1454
- Due to the fact that font processing can occupy large amount of time, some data is cached.
1455
- Cache files are created in the current folder by default.
1456
- This can be controlled with the `font_cache_dir` paramater of the `FPDF` constructor.
1440
+ uni (bool): [**DEPRECATED**] unused
1457
1441
"""
1442
+ if uni != "DEPRECATED" :
1443
+ warnings .warn (
1444
+ '"uni" parameter is deprecated, unused and will soon be removed' ,
1445
+ PendingDeprecationWarning ,
1446
+ )
1458
1447
if not fname :
1459
1448
fname = family .replace (" " , "" ) + f"{ style .lower ()} .pkl"
1460
1449
style = "" .join (sorted (style .upper ()))
@@ -1468,7 +1457,7 @@ def add_font(self, family, style="", fname=None, uni=False):
1468
1457
if fontkey in self .fonts or fontkey in self .core_fonts :
1469
1458
warnings .warn (f"Core font or font already added '{ fontkey } ': doing nothing" )
1470
1459
return
1471
- if uni :
1460
+ if str ( fname ). endswith ( ".ttf" ) :
1472
1461
for parent in ("." , FPDF_FONT_DIR ):
1473
1462
if not parent :
1474
1463
continue
@@ -1478,59 +1467,41 @@ def add_font(self, family, style="", fname=None, uni=False):
1478
1467
else :
1479
1468
raise FileNotFoundError (f"TTF Font file not found: { fname } " )
1480
1469
1481
- if self .font_cache_dir is None :
1482
- cache_dir = unifilename = None
1483
- else :
1484
- cache_dir = (
1485
- Path () if self .font_cache_dir is True else Path (self .font_cache_dir )
1486
- )
1487
- unifilename = cache_dir / f"{ ttffilename .stem } .pkl"
1488
-
1489
1470
# include numbers in the subset! (if alias present)
1490
1471
# ensure that alias is mapped 1-by-1 additionally (must be replaceable)
1491
1472
sbarr = "\x00 "
1492
1473
if self .str_alias_nb_pages :
1493
1474
sbarr += "0123456789"
1494
1475
sbarr += self .str_alias_nb_pages
1495
1476
1496
- font_dict = load_cache (unifilename )
1497
- if font_dict is None :
1498
- ttf = TTFontFile ()
1499
- ttf .getMetrics (ttffilename )
1500
- desc = {
1501
- "Ascent" : round (ttf .ascent ),
1502
- "Descent" : round (ttf .descent ),
1503
- "CapHeight" : round (ttf .capHeight ),
1504
- "Flags" : ttf .flags ,
1505
- "FontBBox" : (
1506
- f"[{ ttf .bbox [0 ]:.0f} { ttf .bbox [1 ]:.0f} "
1507
- f" { ttf .bbox [2 ]:.0f} { ttf .bbox [3 ]:.0f} ]"
1508
- ),
1509
- "ItalicAngle" : int (ttf .italicAngle ),
1510
- "StemV" : round (ttf .stemV ),
1511
- "MissingWidth" : round (ttf .defaultWidth ),
1512
- }
1513
-
1514
- # Generate metrics .pkl file
1515
- font_dict = {
1516
- "type" : "TTF" ,
1517
- "name" : re .sub ("[ ()]" , "" , ttf .fullName ),
1518
- "desc" : desc ,
1519
- "up" : round (ttf .underlinePosition ),
1520
- "ut" : round (ttf .underlineThickness ),
1521
- "ttffile" : ttffilename ,
1522
- "fontkey" : fontkey ,
1523
- "unifilename" : unifilename ,
1524
- "originalsize" : os .stat (ttffilename ).st_size ,
1525
- "cw" : ttf .charWidths ,
1526
- }
1527
-
1528
- if unifilename :
1529
- try :
1530
- unifilename .write_bytes (pickle .dumps (font_dict ))
1531
- except OSError as e :
1532
- if e .errno != errno .EACCES :
1533
- raise # Not a permission error.
1477
+ ttf = TTFontFile ()
1478
+ ttf .getMetrics (ttffilename )
1479
+ desc = {
1480
+ "Ascent" : round (ttf .ascent ),
1481
+ "Descent" : round (ttf .descent ),
1482
+ "CapHeight" : round (ttf .capHeight ),
1483
+ "Flags" : ttf .flags ,
1484
+ "FontBBox" : (
1485
+ f"[{ ttf .bbox [0 ]:.0f} { ttf .bbox [1 ]:.0f} "
1486
+ f" { ttf .bbox [2 ]:.0f} { ttf .bbox [3 ]:.0f} ]"
1487
+ ),
1488
+ "ItalicAngle" : int (ttf .italicAngle ),
1489
+ "StemV" : round (ttf .stemV ),
1490
+ "MissingWidth" : round (ttf .defaultWidth ),
1491
+ }
1492
+
1493
+ # Generate metrics .pkl file
1494
+ font_dict = {
1495
+ "type" : "TTF" ,
1496
+ "name" : re .sub ("[ ()]" , "" , ttf .fullName ),
1497
+ "desc" : desc ,
1498
+ "up" : round (ttf .underlinePosition ),
1499
+ "ut" : round (ttf .underlineThickness ),
1500
+ "ttffile" : ttffilename ,
1501
+ "fontkey" : fontkey ,
1502
+ "originalsize" : os .stat (ttffilename ).st_size ,
1503
+ "cw" : ttf .charWidths ,
1504
+ }
1534
1505
1535
1506
self .fonts [fontkey ] = {
1536
1507
"i" : len (self .fonts ) + 1 ,
@@ -1543,29 +1514,21 @@ def add_font(self, family, style="", fname=None, uni=False):
1543
1514
"ttffile" : font_dict ["ttffile" ],
1544
1515
"fontkey" : fontkey ,
1545
1516
"subset" : SubsetMap (map (ord , sbarr )),
1546
- "unifilename" : unifilename ,
1547
1517
}
1548
1518
self .font_files [fontkey ] = {
1549
1519
"length1" : font_dict ["originalsize" ],
1550
1520
"type" : "TTF" ,
1551
1521
"ttffile" : ttffilename ,
1552
1522
}
1553
- self .font_files [fname ] = {"type" : "TTF" }
1554
1523
else :
1555
- if fname .endswith (".ttf" ):
1556
- warnings .warn (
1557
- "When providing a TTF font file you must pass uni=True to FPDF.add_font"
1558
- )
1524
+ warnings .warn (
1525
+ "Support for .pkl font files definition is deprecated, and will be removed from fpdf2 soon."
1526
+ " If you require this feature, please report your need on fpdf2 GitHub project." ,
1527
+ PendingDeprecationWarning ,
1528
+ )
1559
1529
font_dict = pickle .loads (Path (fname ).read_bytes ())
1560
- if font_dict ["type" ] == "TTF" :
1561
- warnings .warn (
1562
- "Pickle was generated from TTF font file, setting uni=True"
1563
- )
1564
- self .add_font (family , style = style , fname = fname , uni = True )
1565
- return
1566
-
1567
- self .fonts [fontkey ] = {"i" : len (self .fonts ) + 1 }
1568
- self .fonts [fontkey ].update (font_dict )
1530
+ font_dict ["i" ] = len (self .fonts ) + 1
1531
+ self .fonts [fontkey ] = font_dict
1569
1532
diff = font_dict .get ("diff" )
1570
1533
if diff :
1571
1534
# Search existing encodings
@@ -2722,7 +2685,7 @@ def image(
2722
2685
"""
2723
2686
if type :
2724
2687
warnings .warn (
2725
- '"type" is unused and will soon be deprecated ' ,
2688
+ '"type" parameter is deprecated, unused and will soon be removed ' ,
2726
2689
PendingDeprecationWarning ,
2727
2690
)
2728
2691
if str (name ).endswith (".svg" ):
@@ -3010,7 +2973,7 @@ def output(self, name="", dest=""):
3010
2973
"""
3011
2974
if dest :
3012
2975
warnings .warn (
3013
- '"dest" is unused and will soon be deprecated ' ,
2976
+ '"dest" parameter is deprecated, unused and will soon be removed ' ,
3014
2977
PendingDeprecationWarning ,
3015
2978
)
3016
2979
# Finish document if necessary:
@@ -3438,40 +3401,18 @@ def _putfonts(self):
3438
3401
self .mtd (font )
3439
3402
3440
3403
def _putTTfontwidths (self , font , maxUni ):
3441
- if font ["unifilename" ] is None :
3442
- cw127fname = None
3443
- else :
3444
- cw127fname = Path (font ["unifilename" ]).with_suffix (".cw127.pkl" )
3445
- font_dict = load_cache (cw127fname )
3446
- if font_dict :
3447
- rangeid = font_dict ["rangeid" ]
3448
- range_ = font_dict ["range" ]
3449
- prevcid = font_dict ["prevcid" ]
3450
- prevwidth = font_dict ["prevwidth" ]
3451
- interval = font_dict ["interval" ]
3452
- range_interval = font_dict ["range_interval" ]
3453
- startcid = 128
3454
- else :
3455
- rangeid = 0
3456
- range_ = {}
3457
- range_interval = {}
3458
- prevcid = - 2
3459
- prevwidth = - 1
3460
- interval = False
3461
- startcid = 1
3404
+ rangeid = 0
3405
+ range_ = {}
3406
+ range_interval = {}
3407
+ prevcid = - 2
3408
+ prevwidth = - 1
3409
+ interval = False
3410
+ startcid = 1
3462
3411
cwlen = maxUni + 1
3463
3412
3464
3413
# for each character
3465
3414
subset = font ["subset" ].dict ()
3466
3415
for cid in range (startcid , cwlen ):
3467
- if cid == 128 and font_dict :
3468
- try :
3469
- with cw127fname .open ("wb" ) as fh :
3470
- pickle .dump (font_dict , fh )
3471
- except OSError as e :
3472
- if e .errno != errno .EACCES :
3473
- raise # Not a permission error.
3474
-
3475
3416
width = _char_width (font , cid )
3476
3417
if "dw" not in font or (font ["dw" ] and width != font ["dw" ]):
3477
3418
cid_mapped = subset .get (cid )
@@ -4194,4 +4135,4 @@ def _is_xml(img: io.BytesIO):
4194
4135
sys .modules [__name__ ].__class__ = WarnOnDeprecatedModuleAttributes
4195
4136
4196
4137
4197
- __all__ = ["FPDF" , "load_cache" , " get_page_format" , "TitleStyle" , "PAGE_FORMATS" ]
4138
+ __all__ = ["FPDF" , "get_page_format" , "TitleStyle" , "PAGE_FORMATS" ]
0 commit comments