From aaf21faf2a712f38f1268ed5564ec8b89115cf65 Mon Sep 17 00:00:00 2001
From: Kovid Goyal
Date: Wed, 4 Jul 2007 21:11:07 +0000
Subject: [PATCH] Support for dropcaps in html2lrf
---
src/libprs500/__init__.py | 2 +-
src/libprs500/ebooks/lrf/html/convert_from.py | 41 +++++++++++++++---
src/libprs500/ebooks/lrf/html/demo/a.png | Bin 0 -> 2645 bytes
src/libprs500/ebooks/lrf/html/demo/demo.html | 15 +++++++
4 files changed, 50 insertions(+), 8 deletions(-)
create mode 100644 src/libprs500/ebooks/lrf/html/demo/a.png
diff --git a/src/libprs500/__init__.py b/src/libprs500/__init__.py
index f1e6394235..b57d648f0c 100644
--- a/src/libprs500/__init__.py
+++ b/src/libprs500/__init__.py
@@ -13,7 +13,7 @@
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
''' E-book management software'''
-__version__ = "0.3.65"
+__version__ = "0.3.66"
__docformat__ = "epytext"
__author__ = "Kovid Goyal "
__appname__ = 'libprs500'
diff --git a/src/libprs500/ebooks/lrf/html/convert_from.py b/src/libprs500/ebooks/lrf/html/convert_from.py
index b105fb45b9..57b6d5d744 100644
--- a/src/libprs500/ebooks/lrf/html/convert_from.py
+++ b/src/libprs500/ebooks/lrf/html/convert_from.py
@@ -17,7 +17,7 @@
"""
Code to convert HTML ebooks into LRF ebooks.
-I am indebted to esperanc for the initial CSS->Xylog Style conversion routines
+I am indebted to esperanc for the initial CSS->Xylog Style conversion code
and to Falstaff for pylrs.
"""
import os, re, sys, shutil, traceback, copy, glob
@@ -36,7 +36,7 @@ from libprs500.ebooks.BeautifulSoup import BeautifulSoup, BeautifulStoneSoup, \
Comment, Tag, NavigableString, Declaration, ProcessingInstruction
from libprs500.ebooks.lrf.pylrs.pylrs import Paragraph, CR, Italic, ImageStream, \
TextBlock, ImageBlock, JumpButton, CharButton, Bold, Space, \
- Plot, Image, BlockSpace, RuledLine, BookSetting, Canvas
+ Plot, Image, BlockSpace, RuledLine, BookSetting, Canvas, DropCaps
from libprs500.ebooks.lrf.pylrs.pylrs import Span as _Span
from libprs500.ebooks.lrf import option_parser, Book, PRS500_PROFILE
from libprs500.ebooks import ConversionError
@@ -304,11 +304,13 @@ class HTMLConverter(object):
em = {"font-style" : "italic"},
small = {'font-size' : 'small'},
pre = {'font-family' : 'monospace' },
+ code = {'font-family' : 'monospace' },
tt = {'font-family' : 'monospace'},
center = {'text-align' : 'center'},
th = {'font-size' : 'large', 'font-weight':'bold'},
big = {'font-size' : 'large', 'font-weight':'bold'},
- )
+ )
+ self.css['.libprs500_dropcaps'] = {'font-size': 'xx-large'}
self.fonts = fonts #: dict specifting font families to use
self.profile = profile #: Defines the geometry of the display device
self.chapter_detection = chapter_detection #: Flag to toggle chapter detection
@@ -764,7 +766,7 @@ class HTMLConverter(object):
self.current_block = self.book.create_text_block(textStyle=self.current_block.textStyle,
blockStyle=self.current_block.blockStyle)
- def process_image(self, path, tag_css, width=None, height=None):
+ def process_image(self, path, tag_css, width=None, height=None, dropcaps=False):
if self.rotated_images.has_key(path):
path = self.rotated_images[path].name
if self.scaled_images.has_key(path):
@@ -779,6 +781,8 @@ class HTMLConverter(object):
if width == None or height == None:
width, height = im.size
+
+ factor = 720./self.profile.dpi
def scale_image(width, height):
pt = PersistentTemporaryFile(suffix='.jpeg')
@@ -790,6 +794,29 @@ class HTMLConverter(object):
pheight = int(self.current_page.pageStyle.attrs['textheight'])
pwidth = int(self.current_page.pageStyle.attrs['textwidth'])
+ if dropcaps:
+ scale = False
+ if width > 0.75*pwidth:
+ width = int(0.75*pwidth)
+ scale = True
+ if height > 0.75*pheight:
+ height = int(0.75*pheight)
+ scale = True
+ if scale:
+ path = scale_image(width, height)
+ if not self.images.has_key(path):
+ self.images[path] = ImageStream(path)
+ im = Image(self.images[path], x0=0, y0=0, x1=width, y1=height,\
+ xsize=width, ysize=height)
+ line_height = (int(self.current_block.textStyle.attrs['baselineskip']) +
+ int(self.current_block.textStyle.attrs['linespace']))//10
+ line_height *= self.profile.dpi/72.
+ lines = int(ceil(float(height)/line_height))
+ dc = DropCaps(lines)
+ dc.append(Plot(im, xsize=ceil(width*factor), ysize=ceil(height*factor)))
+ self.current_para.append(dc)
+ return
+
if not self.disable_autorotation and width > pwidth and width > height:
pt = PersistentTemporaryFile(suffix='.jpeg')
im = im.rotate(90)
@@ -821,7 +848,7 @@ class HTMLConverter(object):
im = Image(self.images[path], x0=0, y0=0, x1=width, y1=height,\
xsize=width, ysize=height)
- factor = 720./self.profile.dpi
+
self.process_alignment(tag_css)
@@ -954,8 +981,8 @@ class HTMLConverter(object):
height = int(tag['height'])
except:
pass
- self.process_image(path, tag_css, width, height)
-
+ dropcaps = tag.has_key('class') and tag['class'] == 'libprs500_dropcaps'
+ self.process_image(path, tag_css, width, height, dropcaps=dropcaps)
else:
print >>sys.stderr, "Failed to process:", tag
elif tagname in ['style', 'link']:
diff --git a/src/libprs500/ebooks/lrf/html/demo/a.png b/src/libprs500/ebooks/lrf/html/demo/a.png
new file mode 100644
index 0000000000000000000000000000000000000000..185036fc896516d18cbd1fc51b6bf2ebbf0aab01
GIT binary patch
literal 2645
zcmV-b3aa&qP)e
zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{013TGL_t(|+U;B|bfZice%?Kw
zXdRW2)Meeov2xZSiKCJ_)=`JeI_eTPmDEv}IwG@RO9k{K#pLoyA0SYAKuRrI#
z=Ww=5V1Q@teeRt*cjg}=BAi%?qQGo6!(y?3ZQB5V>+5R(faiI5eSO7XFo5s-co(4)
zOOhmHSr)}{OhiO^o)hPsG)<%FbV^y45fM?AW#l*x<#~SUSEPRqsVS?~3X8=80HEtS
zk|e?T`8l4SpK*3}hO@IXyu7?X2vN}*0JylgfN7eT&*zmkZ*Olg91gKuE}`o>bX~{m
z>nqOA&M+E{plRC2)08~VX*!*fuIt1YYwcM8WLZ{gJDl^*-=t=hB~4Qbf}ruYeFXb}
z;dvfunnt>=6X%?g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%aFf*|DkiTCj&*g%{E{FR6I|8c+hrFSFZL`fRg8VV^5b4
znNyXadDpeaHk*@xB745;LuDYh>6)(TkweSTy^m#C;GA#ej=N+y9DXgAe26*W^Yins
z1q$bUqqc04zkmOJEl>c!b=|%0e|mcQdZ1XAwO7Qt4exw9P-K2%S6Q#uUlWvlc;(B2
zvM*W6+Ltc{%IoXvry$#tg0gQG(j{R%oV^7X==ZH{AR^p9>nVHh@&
zWtmHU{P@wk-TV9d&uczS(-rl)*RDAtbR@e!rTH{v^4s`en~Z+_*tK~0UCpP-blTFA
zFYHZV>_h`PN6M0mX?(FG6=)4mpRDl$)Y2u4kZ!N92!dea!;d=0y9X$3!Qoz@l(!9euQ}d*2aDU9
z5w_)#Z3lFR%zzz=Q~JIiwgIG1jBv;dSYOkXE7Lb!*R+Sz)}Y8OI{Nm;rm@d19&eMT
z*7s%Ehj`JI7Tm-ka?_|LkF?D^&!IeCQ_stNQ+Cmq&P`>yOr%Vxa)XEZb(==N`)2gy
zMp#`M*0fvCrok)vkebY~_L-L0C%3U&E<1B3+q&2+%UTDonx?(cn8uRP#bSYText formatting and ruled lines
Inline images
Embedded Fonts
+ Dropcaps
Recursive link following
The HTML used to create this file
@@ -149,6 +150,20 @@
Table of Contents
+
+
+
beautiful image based dropcaps to emphasize this
+ paragraph. Image based dropcaps are specified by adding the class = 'libprs500_dropcaps'
+ attribute to an <img>
tag.
+
+
+ This is a plain text based dropcaps. It
+ is not nearly as dramatic, but easier to code ;-)
+
+
+
+ Table of Contents
+