Esempio n. 1
0
 def find_css_font_file_families(epub_dir, opftree):
     font_families = []
     css_items = etree.XPath('//opf:item[@media-type="text/css"]',
                             namespaces=OPFNS)(opftree)
     for c in css_items:
         css_file_path = os.path.join(epub_dir, c.get('href'))
         sheet = cssutils.parseFile(css_file_path, validate=True)
         for rule in sheet:
             if rule.type == rule.FONT_FACE_RULE:
                 css_font_family = None
                 font_file_family = None
                 for p in rule.style:
                     if p.name == 'font-family' and css_font_family is None:
                         try:
                             css_font_family = p.value.split(
                                 ',')[0].strip().strip('"').strip("'")
                         except:
                             continue
                         continue
                     if p.name == 'src' and font_file_family is None:
                         ffs = rule.style.getProperty(p.name).propertyValue
                         ff_url = ffs.item(0).value
                         with open(
                                 os.path.join(
                                     os.path.dirname(css_file_path),
                                     ff_url), 'rb') as f:
                             lfp = list_font_basic_properties(f.read())
                             lfp = list(lfp)
                             if 'subset of' in lfp[0]:
                                 lfp[0] = re.sub(r'\w+?\s-\ssubset\sof\s',
                                                 '', lfp[0])
                             font_file_family = lfp[0]
                             continue
                 font_families.append([css_font_family, font_file_family])
         return font_families
Esempio n. 2
0
 def find_old_family_fonts(epub_dir, opftree, family_name):
     font_items = etree.XPath(
         '//opf:item[@media-type="application/vnd.ms-opentype"]',
         namespaces=OPFNS
     )(opftree)
     family_font_list = []
     for f in font_items:
         furl = f.get('href')
         with open(os.path.join(epub_dir, furl)) as f:
             lfp = list_font_basic_properties(f.read())
             if lfp[0] == family_name:
                 family_font_list.append([furl] + list(lfp))
     return family_font_list
Esempio n. 3
0
 def find_old_family_fonts(epub_dir, opftree, family_name):
     font_items = etree.XPath(
         '//opf:item[@media-type="application/vnd.ms-opentype"]',
         namespaces=OPFNS)(opftree)
     family_font_list = []
     for f in font_items:
         furl = f.get('href')
         with open(os.path.join(epub_dir, furl), 'rb') as f:
             lfp = list_font_basic_properties(f.read())
             lfp = list(lfp)
             if 'subset of' in lfp[0]:
                 lfp[0] = re.sub(r'\w+?\s-\ssubset\sof\s', '', lfp[0])
             if lfp[0] == family_name:
                 family_font_list.append([furl] + lfp)
     return family_font_list
Esempio n. 4
0
 def find_old_family_fonts(epub_dir, opftree, family_name):
     font_items = etree.XPath(
         '//opf:item[@media-type="application/vnd.ms-opentype"]',
         namespaces=OPFNS
     )(opftree)
     family_font_list = []
     for f in font_items:
         furl = f.get('href')
         with open(os.path.join(epub_dir, furl), 'rb') as f:
             lfp = list_font_basic_properties(f.read())
             lfp = list(lfp)
             if 'subset of' in lfp[0]:
                 lfp[0] = re.sub(r'\w+?\s-\ssubset\sof\s', '', lfp[0])
             if lfp[0] == family_name:
                 family_font_list.append([furl] + lfp)
     return family_font_list
Esempio n. 5
0
 def find_new_family_fonts(user_font_dir, epub_dir, opftree, family_name,
                           is_all):
     family_font_list = []
     for root, dirs, files in os.walk(user_font_dir):
         for f in files:
             furl = os.path.join(root, f)
             if (furl.lower().endswith('.ttf')
                     or furl.lower().endswith('.otf')):
                 with open(os.path.join(furl), 'rb') as f:
                     try:
                         lfp = list_font_basic_properties(f.read())
                     except:
                         continue
                     if is_all:
                         family_font_list.append([furl] + list(lfp))
                     elif lfp[0] == family_name:
                         family_font_list.append([furl] + list(lfp))
     return family_font_list
Esempio n. 6
0
 def find_new_family_fonts(epub_dir, opftree, family_name):
     family_font_list = []
     # temorarily hardcoded user_font_dir
     user_font_dir = os.path.join(HOME, 'fonts')
     for root, dirs, files in os.walk(user_font_dir):
         for f in files:
             furl = os.path.join(root, f)
             if (
                 furl.lower().endswith('.ttf') or
                 furl.lower().endswith('.otf')
             ):
                 with open(os.path.join(epub_dir, furl)) as f:
                     try:
                         lfp = list_font_basic_properties(f.read())
                     except:
                         continue
                     # print(lfp)
                     if lfp[0] == family_name:
                         family_font_list.append([furl] + list(lfp))
     return family_font_list
Esempio n. 7
0
 def find_new_family_fonts(user_font_dir, epub_dir, opftree, family_name,
                           is_all):
     family_font_list = []
     for root, dirs, files in os.walk(user_font_dir):
         for f in files:
             furl = os.path.join(root, f)
             if (
                 furl.lower().endswith('.ttf') or
                 furl.lower().endswith('.otf')
             ):
                 with open(os.path.join(furl), 'rb') as f:
                     try:
                         lfp = list_font_basic_properties(f.read())
                     except:
                         continue
                     if is_all:
                         family_font_list.append([furl] + list(lfp))
                     elif lfp[0] == family_name:
                         family_font_list.append([furl] + list(lfp))
     return family_font_list
Esempio n. 8
0
 def find_css_font_file_families(epub_dir, opftree):
     font_families = []
     css_items = etree.XPath(
         '//opf:item[@media-type="text/css"]',
         namespaces=OPFNS
     )(opftree)
     for c in css_items:
         css_file_path = os.path.join(epub_dir, c.get('href'))
         sheet = cssutils.parseFile(css_file_path,
                                    validate=True)
         for rule in sheet:
             if rule.type == rule.FONT_FACE_RULE:
                 css_font_family = None
                 font_file_family = None
                 for p in rule.style:
                     if p.name == 'font-family' and css_font_family is None:
                         try:
                             css_font_family = p.value.split(
                                 ',')[0].strip().strip('"').strip("'")
                         except:
                             continue
                         continue
                     if p.name == 'src' and font_file_family is None:
                         ffs = rule.style.getProperty(p.name).propertyValue
                         ff_url = ffs.item(0).value
                         with open(os.path.join(
                             os.path.dirname(css_file_path), ff_url
                         ), 'rb') as f:
                             lfp = list_font_basic_properties(f.read())
                             lfp = list(lfp)
                             if 'subset of' in lfp[0]:
                                 lfp[0] = re.sub(
                                     r'\w+?\s-\ssubset\sof\s', '',
                                     lfp[0]
                                 )
                             font_file_family = lfp[0]
                             continue
                 font_families.append([css_font_family, font_file_family])
         return font_families