-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop using pickle - deprecating the font caching mechanism + .pkl font files definitions parsing #345
Comments
I guess this is referring to the font caching file. The docs here state:
but the docs here state:
where only the second one is correct. The caching can be disabled via the |
I opened PR #347 to deprecate both the font caching mechanism and prepare deprecation for This won't be enough to get rid right now of the Regarding performances, I did a quick comparison using this test code: issue_345.pyimport fpdf, warnings
from fpdf.ttfonts import TTFontFile
class MyTTFontFile(TTFontFile):
def getCMAP4(self, unicode_cmap_offset, glyphToChar, charToGlyph):
TTFontFile.getCMAP4(self, unicode_cmap_offset, glyphToChar, charToGlyph)
self.saveChar = charToGlyph
def getCMAP12(self, unicode_cmap_offset, glyphToChar, charToGlyph):
TTFontFile.getCMAP12(self, unicode_cmap_offset, glyphToChar, charToGlyph)
self.saveChar = charToGlyph
def main():
warnings.simplefilter('ignore', UserWarning)
for font_filename in ("DejaVuSans.ttf", "DroidSansFallback.ttf", "Roboto-Regular.ttf", "cmss12.ttf"):
font_path = "test/fonts/" + font_filename
font_name = font_filename.split(".")[0]
pdf = fpdf.FPDF()
pdf.add_page()
pdf.add_font(font_name, fname=font_path, uni=True)
pdf.set_font(font_name, size=10)
ttf = MyTTFontFile()
ttf.getMetrics(font_path)
# Create a PDF with the first 999 charters defined in the font:
for counter, character in enumerate(ttf.saveChar, 0):
pdf.write(8, f"{counter:03}) {character:03x} - {character:c}")
pdf.ln()
if counter >= 999:
break
pdf.output(f"charmap_first_999_chars-{font_name}.pdf") The result of running
Which means that without this cache, if we build many different PDF documents from different fpdf.FPDF instances, we loose approximatively 40ms per font file. This seems fine with me. |
@Spenhouet would you kindly review the PR please ? 😊 |
I opened #402 to finally get rid of the dependency on |
Intent
The
pickle
module is currently used in fpdf.py to implement a font caching mechanism.However this library is notoriously dangerous: https://intoli.com/blog/dangerous-pickles/
bandit warned us about it: .banditrc.yml
Solution
a. in a single Python script execution, with in-memory caching (no impact expected)
b. when several consecutive calls to a Python script are made (there the cache should have some use)
The text was updated successfully, but these errors were encountered: