diff --git a/src/calibre/ebooks/conversion/plugins/pdf_output.py b/src/calibre/ebooks/conversion/plugins/pdf_output.py index 35504b31fb..e12f094e18 100644 --- a/src/calibre/ebooks/conversion/plugins/pdf_output.py +++ b/src/calibre/ebooks/conversion/plugins/pdf_output.py @@ -14,6 +14,8 @@ import os from calibre.customize.conversion import OutputFormatPlugin, \ OptionRecommendation from calibre.ptempfile import TemporaryDirectory +from calibre.constants import iswindows +from calibre import walk UNITS = [ 'millimeter', @@ -148,6 +150,14 @@ class PDFOutput(OutputFormatPlugin): oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb_book, oeb_dir, self.input_plugin, self.opts, self.log) + if iswindows: + for f in walk(oeb_dir): + if f.rpartition('.')[-1].lower() in {'ttf', 'otf'}: + self.log.warn('Found embedded font %s, removing it, as ' + 'embedded fonts on windows are not supported by ' + 'the PDF Output plugin'%os.path.basename(f)) + os.remove(f) + opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0] opf = OPF(opfpath, os.path.dirname(opfpath)) diff --git a/src/calibre/utils/fonts/embedflag.py b/src/calibre/utils/fonts/embedflag.py new file mode 100644 index 0000000000..0c4e94bae6 --- /dev/null +++ b/src/calibre/utils/fonts/embedflag.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai +from __future__ import (unicode_literals, division, absolute_import, + print_function) + +__license__ = 'GPL v3' +__copyright__ = '2012, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +import sys, struct + +class UnsupportedFont(ValueError): + pass + +def remove_embed_restriction(raw): + sfnt_version = raw[:4] + if sfnt_version not in {b'\x00\x01\x00\x00', b'OTTO'}: + raise UnsupportedFont('Not a supported font, sfnt_version: %r'%sfnt_version) + + num_tables = struct.unpack_from(b'>H', raw, 4)[0] + + # Find OS/2 table + offset = 4 + 4*2 # Start of the Table record entries + os2_table_offset = None + for i in xrange(num_tables): + table_tag = raw[offset:offset+4] + offset += 16 # Size of a table record + if table_tag == b'OS/2': + os2_table_offset = struct.unpack_from(b'>I', raw, offset+8)[0] + break + if os2_table_offset is None: + raise UnsupportedFont('Not a supported font, has no OS/2 table') + + version, = struct.unpack_from(b'>H', raw, os2_table_offset) + + fs_type_offset = os2_table_offset + struct.calcsize(b'>HhHH') + fs_type = struct.unpack_from(b'>H', raw, fs_type_offset)[0] + if fs_type == 0: + return raw + + return raw[:fs_type_offset] + struct.pack(b'>H', 0) + raw[fs_type_offset+2:] + +if __name__ == '__main__': + remove_embed_restriction(open(sys.argv[-1], 'rb').read()) +