From f45341702f2cf539deea1da905256df6a88d3864 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 14 Jul 2012 11:08:15 +0530 Subject: [PATCH] KF8 Output: Fix calibre produced KF8 files not showing the 'Use publisher font' option on the Kindle Touch when they have embedded fonts --- src/calibre/ebooks/mobi/writer2/main.py | 8 +++++++- src/calibre/ebooks/mobi/writer2/resources.py | 2 ++ src/calibre/ebooks/mobi/writer8/exth.py | 2 +- src/calibre/ebooks/mobi/writer8/mobi.py | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/mobi/writer2/main.py b/src/calibre/ebooks/mobi/writer2/main.py index e9f10a605e..b0146daebc 100644 --- a/src/calibre/ebooks/mobi/writer2/main.py +++ b/src/calibre/ebooks/mobi/writer2/main.py @@ -297,10 +297,13 @@ class MobiWriter(object): # 0x70 - 0x73 : EXTH flags # Bit 6 (0b1000000) being set indicates the presence of an EXTH header + # Bit 12 being set indicates the presence of embedded fonts # The purpose of the other bits is unknown exth_flags = 0b1010000 if self.is_periodical: exth_flags |= 0b1000 + if self.resources.has_fonts: + exth_flags |= 0b1000000000000 record0.write(pack(b'>I', exth_flags)) # 0x74 - 0x93 : Unknown @@ -406,7 +409,10 @@ class MobiWriter(object): # Now change the header fields that need to be different in the MOBI 6 # header header_fields['first_resource_record'] = first_image_record - header_fields['exth_flags'] = 0b100001010000 # Kinglegen uses this + ef = 0b100001010000 # Kinglegen uses this + if self.resources.has_fonts: + ef |= 0b1000000000000 + header_fields['exth_flags'] = ef header_fields['fdst_record'] = pack(b'>HH', 1, last_content_record) header_fields['fdst_count'] = 1 # Why not 0? Kindlegen uses 1 header_fields['flis_record'] = flis_number diff --git a/src/calibre/ebooks/mobi/writer2/resources.py b/src/calibre/ebooks/mobi/writer2/resources.py index 2f12793b03..bdf20a6f2c 100644 --- a/src/calibre/ebooks/mobi/writer2/resources.py +++ b/src/calibre/ebooks/mobi/writer2/resources.py @@ -32,6 +32,7 @@ class Resources(object): self.used_image_indices = set() self.image_indices = set() self.cover_offset = self.thumbnail_offset = None + self.has_fonts = False self.add_resources(add_fonts) @@ -109,6 +110,7 @@ class Resources(object): 'ttf', 'otf'} and isinstance(item.data, bytes): self.records.append(write_font_record(item.data)) self.item_map[item.href] = len(self.records) + self.has_fonts = True def add_extra_images(self): ''' diff --git a/src/calibre/ebooks/mobi/writer8/exth.py b/src/calibre/ebooks/mobi/writer8/exth.py index df952a8eb4..508b77ce5b 100644 --- a/src/calibre/ebooks/mobi/writer8/exth.py +++ b/src/calibre/ebooks/mobi/writer8/exth.py @@ -140,7 +140,7 @@ def build_exth(metadata, prefer_author_sort=False, is_periodical=False, nrecs += 1 if be_kindlegen2: - vals = {204:201, 205:2, 206:2, 207:35621} + vals = {204:201, 205:2, 206:5, 207:0} elif is_periodical: # Pretend to be amazon's super secret periodical generator vals = {204:201, 205:2, 206:0, 207:101} diff --git a/src/calibre/ebooks/mobi/writer8/mobi.py b/src/calibre/ebooks/mobi/writer8/mobi.py index 48e0833ddb..769b301a50 100644 --- a/src/calibre/ebooks/mobi/writer8/mobi.py +++ b/src/calibre/ebooks/mobi/writer8/mobi.py @@ -277,6 +277,8 @@ class KF8Book(object): self.exth_flags = 0b1010000 if writer.opts.mobi_periodical: self.exth_flags |= 0b1000 + if resources.has_fonts: + self.exth_flags |= 0b1000000000000 self.opts = writer.opts self.start_offset = writer.start_offset