mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Various improvements to lrs2lrf
This commit is contained in:
parent
f9adfdba83
commit
c26d35e25b
@ -161,7 +161,9 @@ class LrsParser(object):
|
||||
newstyle = None
|
||||
#
|
||||
# yuck - headers and footers really mess this up
|
||||
#
|
||||
# until then, there were no objid pointers in any
|
||||
# style object.
|
||||
# hmm, so maybe we push them always into the page
|
||||
if stylename == 'pagestyle':
|
||||
for e in self.pagestyles:
|
||||
if self.equal_attrib(e, style):
|
||||
@ -171,7 +173,11 @@ class LrsParser(object):
|
||||
if newstyle == None:
|
||||
#print "making pagestyle %s"%id
|
||||
self.pagestyles.append(style)
|
||||
newstyle = self.book.create_page_style(**self.process_attrib(style))
|
||||
attrib = self.process_attrib(style)
|
||||
for name in ['evenfooter', 'evenheader', 'footer', 'header', 'oddfooter', 'oddheader' ]:
|
||||
if name+'id' in style.attrib:
|
||||
attrib[name] = self.fetch_header_footer(style, name+'id')
|
||||
newstyle = self.book.create_page_style(**attrib)
|
||||
elif stylename == 'blockstyle':
|
||||
for e in self.blockstyles:
|
||||
if self.equal_attrib(e, style):
|
||||
@ -191,6 +197,8 @@ class LrsParser(object):
|
||||
if newstyle == None:
|
||||
#print "making textstyle %s"%id
|
||||
self.textstyles.append(style)
|
||||
#if 'textlinewidth' in style.attrib:
|
||||
# print "creating new TextStyle with textlinewidth='%s'"%style.attrib['textlinewidth']
|
||||
newstyle = self.book.create_text_style(**self.process_attrib(style))
|
||||
else:
|
||||
raise LrsError, "no handler for %s style name"
|
||||
@ -303,7 +311,9 @@ class LrsParser(object):
|
||||
|
||||
for element in draw_char:
|
||||
if element.tag == "Span":
|
||||
obj.append(self.process_draw_char(element, Span(**self.process_attrib(element))))
|
||||
span = self.process_draw_char(element, Span(**self.process_attrib(element)))
|
||||
if not span.isEmpty():
|
||||
obj.append(span)
|
||||
elif element.tag == "Plot":
|
||||
obj.append(self.process_text(element, self.process_Plot(element)))
|
||||
elif element.tag == "CR":
|
||||
|
@ -110,7 +110,7 @@ def writeColor(f, color):
|
||||
f.write(struct.pack(">I", int(color, 0)))
|
||||
|
||||
def writeLineWidth(f, width):
|
||||
writeWord(f, int(width)//5)
|
||||
writeWord(f, int(width))
|
||||
|
||||
def writeUnicode(f, string, encoding):
|
||||
if isinstance(string, str):
|
||||
@ -292,7 +292,7 @@ TAG_INFO = dict(
|
||||
ParentPageTree = (0xF57C, "<I"),
|
||||
Italic = (0xF581,),
|
||||
ItalicEnd = (0xF582,),
|
||||
pstart = (0xF5A1, writeDWord), # what goes in the dword?
|
||||
pstart = (0xF5A1, writeDWord), # what goes in the dword? refesound
|
||||
pend = (0xF5A2,),
|
||||
CharButton = (0xF5A7, writeDWord),
|
||||
CharButtonEnd = (0xF5A8,),
|
||||
|
@ -1662,8 +1662,22 @@ class LrsTextTag(LrsContainer):
|
||||
|
||||
|
||||
class LrsSimpleChar1(object):
|
||||
pass
|
||||
|
||||
def isEmpty(self):
|
||||
for content in self.contents:
|
||||
if not content.isEmpty():
|
||||
return False
|
||||
return True
|
||||
|
||||
def hasFollowingContent(self):
|
||||
foundSelf = False
|
||||
for content in self.parent.contents:
|
||||
if content == self:
|
||||
foundSelf = True
|
||||
elif foundSelf:
|
||||
if not content.isEmpty():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class DropCaps(LrsTextTag):
|
||||
|
||||
@ -1672,6 +1686,9 @@ class DropCaps(LrsTextTag):
|
||||
if int(line) <= 0:
|
||||
raise LrsError('A DrawChar must span at least one line.')
|
||||
self.line = int(line)
|
||||
|
||||
def isEmpty(self):
|
||||
return self.text == None or not self.text.strip()
|
||||
|
||||
def toElement(self, se):
|
||||
elem = Element('DrawChar', line=str(self.line))
|
||||
@ -1800,6 +1817,9 @@ class Text(LrsContainer):
|
||||
LrsContainer.__init__(self, [])
|
||||
self.text = text
|
||||
|
||||
def isEmpty(self):
|
||||
return not self.text or not self.text.strip()
|
||||
|
||||
def toLrfContainer(self, lrfWriter, parent):
|
||||
if self.text:
|
||||
if isinstance(self.text, str):
|
||||
@ -1922,9 +1942,15 @@ class Span(LrsSimpleChar1, LrsContainer):
|
||||
|
||||
def toLrfContainer(self, lrfWriter, container):
|
||||
|
||||
# find the currentTextStyle
|
||||
oldTextStyle = self.findCurrentTextStyle()
|
||||
|
||||
# set the attributes we want changed
|
||||
for (name, value) in self.attrs.items():
|
||||
container.appendLrfTag(LrfTag(name, value))
|
||||
if name in oldTextStyle.attrs and oldTextStyle.attrs[name] == self.attrs[name]:
|
||||
self.attrs.pop(name)
|
||||
else:
|
||||
container.appendLrfTag(LrfTag(name, value))
|
||||
|
||||
# set a currentTextStyle so nested span can put things back
|
||||
oldTextStyle = self.findCurrentTextStyle()
|
||||
@ -1935,6 +1961,8 @@ class Span(LrsSimpleChar1, LrsContainer):
|
||||
content.toLrfContainer(lrfWriter, container)
|
||||
|
||||
# put the attributes back the way we found them
|
||||
# the attributes persist beyond the next </P>
|
||||
# if self.hasFollowingContent():
|
||||
for name in self.attrs.keys():
|
||||
container.appendLrfTag(LrfTag(name, oldTextStyle.attrs[name]))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user