Skip to content

Instantly share code, notes, and snippets.

@idlecool
Created May 11, 2011 12:59
Show Gist options
  • Save idlecool/966410 to your computer and use it in GitHub Desktop.
Save idlecool/966410 to your computer and use it in GitHub Desktop.
import os
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import reportlab
reportlab.rl_config.warnOnMissingFontGlyphs = 0
#set the directory where the font is stored.
fonts_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"fonts")
#adding one font
pdfmetrics.registerFont(TTFont("unifont",
os.path.join(fonts_directory,
"unifont/unifont.ttf")))
#and is character mapping
unifont_map = [
#output of above script goes here
]
# similarly add more fonts
# and make a list of added fonts
fontlist = [
"Helvetica", # english and latin english fonts
"AlMateen-Bold", # arabic fonts
"AlMohanad", # arabic fonts
"SazanamiGothic", # japanese fonts
"SazanamiMincho", # japanese fonts
"unifont", # unifont should be always at the last
]
fontmapping = {
"Helvetica": Helvetica_map,
"AlMateen-Bold": AlMateenBold_map,
"AlMohanad": AlMohanad_map,
"SazanamiGothic": SazanamiGothic_map,
"SazanamiMincho": SazanamiMincho_map,
"unifont": unifont_map,
}
fontchecksequence = []
for eachfont in fontlist:
if len(fontmapping[eachfont]) != 0:
fontchecksequence.append(eachfont)
def selectfont(self, char):
""" Select font according to the input character """
charcode = ord(char)
for font in fontchecksequence:
for fontrange in fontmapping[font]:
if charcode in xrange(fontrange[0], fontrange[1]):
return font
return "Helvetica" # fallback, if no thirdparty font is installed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment