mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-03-11 20:15:46 +01:00
Convert calibre modules to ebook_converter.
Here is the first batch of modules, which are needed for converting several formats to LRF. Some of the logic has been change, more cleanups will follow.
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env python2
|
||||
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
@@ -10,8 +10,8 @@ from io import BytesIO
|
||||
from struct import calcsize, unpack, unpack_from
|
||||
from collections import namedtuple
|
||||
|
||||
from calibre.utils.fonts.utils import get_font_names2, get_font_characteristics
|
||||
from polyglot.builtins import range, unicode_type
|
||||
from ebook_converter.utils.fonts.utils import get_font_names2, get_font_characteristics
|
||||
from ebook_converter.polyglot.builtins import range, unicode_type
|
||||
|
||||
|
||||
class UnsupportedFont(ValueError):
|
||||
|
||||
@@ -10,12 +10,11 @@ import os
|
||||
from collections import defaultdict
|
||||
from threading import Thread
|
||||
|
||||
from calibre import walk, prints, as_unicode
|
||||
from calibre.constants import (config_dir, iswindows, isosx, plugins, DEBUG,
|
||||
from ebook_converter import walk, prints, as_unicode
|
||||
from ebook_converter.constants import (config_dir, iswindows, isosx, plugins, DEBUG,
|
||||
isworker, filesystem_encoding)
|
||||
from calibre.utils.fonts.metadata import FontMetadata, UnsupportedFont
|
||||
from calibre.utils.icu import sort_key
|
||||
from polyglot.builtins import itervalues, unicode_type, filter
|
||||
from ebook_converter.utils.fonts.metadata import FontMetadata, UnsupportedFont
|
||||
from ebook_converter.polyglot.builtins import itervalues, unicode_type, filter
|
||||
|
||||
|
||||
class NoFonts(ValueError):
|
||||
@@ -153,12 +152,12 @@ def path_significance(path, folders):
|
||||
|
||||
def build_families(cached_fonts, folders, family_attr='font-family'):
|
||||
families = defaultdict(list)
|
||||
for f in itervalues(cached_fonts):
|
||||
if not f:
|
||||
for font in itervalues(cached_fonts):
|
||||
if not font:
|
||||
continue
|
||||
lf = icu_lower(f.get(family_attr) or '')
|
||||
lf = (font.get(family_attr) or '').lower()
|
||||
if lf:
|
||||
families[lf].append(f)
|
||||
families[lf].append(font)
|
||||
|
||||
for fonts in itervalues(families):
|
||||
# Look for duplicate font files and choose the copy that is from a
|
||||
@@ -166,26 +165,26 @@ def build_families(cached_fonts, folders, family_attr='font-family'):
|
||||
# system directories).
|
||||
fmap = {}
|
||||
remove = []
|
||||
for f in fonts:
|
||||
fingerprint = (icu_lower(f['font-family']), f['font-weight'],
|
||||
f['font-stretch'], f['font-style'])
|
||||
for font in fonts:
|
||||
fingerprint = (font['font-family'].lower(), font['font-weight'],
|
||||
font['font-stretch'], font['font-style'])
|
||||
if fingerprint in fmap:
|
||||
opath = fmap[fingerprint]['path']
|
||||
npath = f['path']
|
||||
npath = font['path']
|
||||
if path_significance(npath, folders) >= path_significance(opath, folders):
|
||||
remove.append(fmap[fingerprint])
|
||||
fmap[fingerprint] = f
|
||||
fmap[fingerprint] = font
|
||||
else:
|
||||
remove.append(f)
|
||||
remove.append(font)
|
||||
else:
|
||||
fmap[fingerprint] = f
|
||||
for font in remove:
|
||||
fonts.remove(font)
|
||||
fmap[fingerprint] = font
|
||||
for fnt in remove:
|
||||
fonts.remove(fnt)
|
||||
fonts.sort(key=font_priority)
|
||||
|
||||
font_family_map = dict.copy(families)
|
||||
font_families = tuple(sorted((f[0]['font-family'] for f in
|
||||
itervalues(font_family_map)), key=sort_key))
|
||||
font_families = tuple(sorted((font[0]['font-family'] for font in
|
||||
itervalues(font_family_map))))
|
||||
return font_family_map, font_families
|
||||
# }}}
|
||||
|
||||
@@ -198,8 +197,8 @@ class FontScanner(Thread):
|
||||
Thread.__init__(self)
|
||||
self.folders = folders + font_dirs() + [os.path.join(config_dir, 'fonts'),
|
||||
P('fonts/liberation')]
|
||||
self.folders = [os.path.normcase(os.path.abspath(f)) for f in
|
||||
self.folders]
|
||||
self.folders = [os.path.normcase(os.path.abspath(font)) for font in
|
||||
self.folders]
|
||||
self.font_families = ()
|
||||
self.allowed_extensions = allowed_extensions
|
||||
|
||||
@@ -218,7 +217,7 @@ class FontScanner(Thread):
|
||||
'''
|
||||
self.join()
|
||||
try:
|
||||
return self.font_family_map[icu_lower(family)]
|
||||
return self.font_family_map[family.lower()]
|
||||
except KeyError:
|
||||
raise NoFonts('No fonts found for the family: %r'%family)
|
||||
|
||||
@@ -264,7 +263,7 @@ class FontScanner(Thread):
|
||||
|
||||
:return: (family name, faces) or None, None
|
||||
'''
|
||||
from calibre.utils.fonts.utils import (supports_text,
|
||||
from ebook_converter.utils.fonts.utils import (supports_text,
|
||||
panose_to_css_generic_family, get_printable_characters)
|
||||
if not isinstance(text, unicode_type):
|
||||
raise TypeError(u'%r is not unicode'%text)
|
||||
@@ -297,7 +296,7 @@ class FontScanner(Thread):
|
||||
|
||||
def reload_cache(self):
|
||||
if not hasattr(self, 'cache'):
|
||||
from calibre.utils.config import JSONConfig
|
||||
from ebook_converter.utils.config import JSONConfig
|
||||
self.cache = JSONConfig('fonts/scanner_cache')
|
||||
else:
|
||||
self.cache.refresh()
|
||||
|
||||
@@ -10,7 +10,7 @@ import struct
|
||||
from io import BytesIO
|
||||
from collections import defaultdict
|
||||
|
||||
from polyglot.builtins import iteritems, itervalues, unicode_type, range, as_bytes
|
||||
from ebook_converter.polyglot.builtins import iteritems, itervalues, unicode_type, range, as_bytes
|
||||
|
||||
|
||||
class UnsupportedFont(ValueError):
|
||||
@@ -441,7 +441,7 @@ def get_font_for_text(text, candidate_font_data=None):
|
||||
if candidate_font_data is not None:
|
||||
ok = supports_text(candidate_font_data, text)
|
||||
if not ok:
|
||||
from calibre.utils.fonts.scanner import font_scanner
|
||||
from ebook_converter.utils.fonts.scanner import font_scanner
|
||||
family, faces = font_scanner.find_font_for_text(text)
|
||||
if faces:
|
||||
with lopen(faces[0]['path'], 'rb') as f:
|
||||
@@ -450,7 +450,7 @@ def get_font_for_text(text, candidate_font_data=None):
|
||||
|
||||
|
||||
def test_glyph_ids():
|
||||
from calibre.utils.fonts.free_type import FreeType
|
||||
from ebook_converter.utils.fonts.free_type import FreeType
|
||||
data = P('fonts/liberation/LiberationSerif-Regular.ttf', data=True)
|
||||
ft = FreeType()
|
||||
font = ft.load_font(data)
|
||||
@@ -470,7 +470,7 @@ def test_supports_text():
|
||||
|
||||
|
||||
def test_find_font():
|
||||
from calibre.utils.fonts.scanner import font_scanner
|
||||
from ebook_converter.utils.fonts.scanner import font_scanner
|
||||
abcd = '诶比西迪'
|
||||
family = font_scanner.find_font_for_text(abcd)[0]
|
||||
print('Family for Chinese text:', family)
|
||||
|
||||
Reference in New Issue
Block a user