OTF embedding

This commit is contained in:
Kovid Goyal 2012-12-20 21:55:15 +05:30
parent 8e0adf59f2
commit 20bb107a3a
3 changed files with 14 additions and 9 deletions

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import sys, traceback import sys, traceback, unicodedata
from math import sqrt from math import sqrt
from collections import namedtuple from collections import namedtuple
from functools import wraps from functools import wraps
@ -358,6 +358,7 @@ class PdfEngine(QPaintEngine):
def drawTextItem(self, point, text_item): def drawTextItem(self, point, text_item):
# super(PdfEngine, self).drawTextItem(point+QPoint(0, 0), text_item) # super(PdfEngine, self).drawTextItem(point+QPoint(0, 0), text_item)
text = type(u'')(text_item.text()).replace('\n', ' ') text = type(u'')(text_item.text()).replace('\n', ' ')
text = unicodedata.normalize('NFKC', text)
tl = QTextLayout(text, text_item.font(), self.paintDevice()) tl = QTextLayout(text, text_item.font(), self.paintDevice())
self.text_option.setTextDirection(Qt.RightToLeft if self.text_option.setTextDirection(Qt.RightToLeft if
text_item.renderFlags() & text_item.RightToLeft else Qt.LeftToRight) text_item.renderFlags() & text_item.RightToLeft else Qt.LeftToRight)
@ -488,7 +489,7 @@ if __name__ == '__main__':
# f.setUnderline(True) # f.setUnderline(True)
# f.setOverline(True) # f.setOverline(True)
# f.setStrikeOut(True) # f.setStrikeOut(True)
f.setFamily('Calibri') f.setFamily('OpenDyslexic')
p.setFont(f) p.setFont(f)
# p.scale(2, 2) # p.scale(2, 2)
# p.rotate(45) # p.rotate(45)

View File

@ -55,7 +55,7 @@ class FontStream(Stream):
def add_extra_keys(self, d): def add_extra_keys(self, d):
d['Length1'] = d['DL'] d['Length1'] = d['DL']
if self.is_otf: if self.is_otf:
d['Subtype'] = Name('OpenType') d['Subtype'] = Name('CIDFontType0C')
def to_hex_string(c): def to_hex_string(c):
return bytes(hex(c)[2:]).rjust(4, b'0').decode('ascii') return bytes(hex(c)[2:]).rjust(4, b'0').decode('ascii')
@ -110,6 +110,7 @@ class Font(object):
def __init__(self, metrics, num, objects, compress): def __init__(self, metrics, num, objects, compress):
self.metrics, self.compress = metrics, compress self.metrics, self.compress = metrics, compress
self.is_otf = self.metrics.is_otf
self.subset_tag = bytes(re.sub('.', lambda m: chr(int(m.group())+ord('A')), self.subset_tag = bytes(re.sub('.', lambda m: chr(int(m.group())+ord('A')),
oct(num))).rjust(6, b'A').decode('ascii') oct(num))).rjust(6, b'A').decode('ascii')
self.font_stream = FontStream(metrics.is_otf, compress=compress) self.font_stream = FontStream(metrics.is_otf, compress=compress)
@ -136,7 +137,7 @@ class Font(object):
'Supplement':0, 'Supplement':0,
}), }),
}) })
if not metrics.is_otf: if not self.is_otf:
self.descendant_font['CIDToGIDMap'] = Name('Identity') self.descendant_font['CIDToGIDMap'] = Name('Identity')
self.font_dict = Dictionary({ self.font_dict = Dictionary({
@ -150,14 +151,17 @@ class Font(object):
self.used_glyphs = set() self.used_glyphs = set()
def embed(self, objects): def embed(self, objects):
# TODO: Subsetting and OpenType self.font_descriptor['FontFile'+('3' if self.is_otf else '2')
self.font_descriptor['FontFile2'] = objects.add(self.font_stream) ] = objects.add(self.font_stream)
self.write_widths(objects) self.write_widths(objects)
glyph_map = self.metrics.sfnt['cmap'].get_char_codes(self.used_glyphs) glyph_map = self.metrics.sfnt['cmap'].get_char_codes(self.used_glyphs)
self.write_to_unicode(objects, glyph_map) self.write_to_unicode(objects, glyph_map)
pdf_subset(self.metrics.sfnt, set(glyph_map)) pdf_subset(self.metrics.sfnt, set(glyph_map))
self.metrics.os2.zero_fstype() if self.is_otf:
self.metrics.sfnt(self.font_stream) self.font_stream.write(self.metrics.sfnt['CFF '].raw)
else:
self.metrics.os2.zero_fstype()
self.metrics.sfnt(self.font_stream)
def write_to_unicode(self, objects, glyph_map): def write_to_unicode(self, objects, glyph_map):
glyph_map = {k:unicodedata.normalize('NFKC', unichr(v)) for k, v in glyph_map = {k:unicodedata.normalize('NFKC', unichr(v)) for k, v in

View File

@ -18,7 +18,7 @@ from calibre.ebooks.pdf.render.common import (
GlyphIndex) GlyphIndex)
from calibre.ebooks.pdf.render.fonts import FontManager from calibre.ebooks.pdf.render.fonts import FontManager
PDFVER = b'%PDF-1.6' PDFVER = b'%PDF-1.3'
Color = namedtuple('Color', 'red green blue opacity') Color = namedtuple('Color', 'red green blue opacity')