mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-03 19:17:02 -05:00 
			
		
		
		
	Re-organize font loading to work on windows. Add option to disable image autorotation. Fix possible bug with image processing.
This commit is contained in:
		
							parent
							
								
									806aba6f80
								
							
						
					
					
						commit
						2a83e24aa7
					
				@ -33,7 +33,7 @@ You may have to adjust the GROUP and the location of the rules file to
 | 
				
			|||||||
suit your distribution.
 | 
					suit your distribution.
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__version__   = "0.3.33"
 | 
					__version__   = "0.3.34"
 | 
				
			||||||
__docformat__ = "epytext"
 | 
					__docformat__ = "epytext"
 | 
				
			||||||
__author__    = "Kovid Goyal <kovid@kovidgoyal.net>"
 | 
					__author__    = "Kovid Goyal <kovid@kovidgoyal.net>"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -12,7 +12,8 @@
 | 
				
			|||||||
##    You should have received a copy of the GNU General Public License along
 | 
					##    You should have received a copy of the GNU General Public License along
 | 
				
			||||||
##    with this program; if not, write to the Free Software Foundation, Inc.,
 | 
					##    with this program; if not, write to the Free Software Foundation, Inc.,
 | 
				
			||||||
##    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 | 
					##    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 | 
				
			||||||
import pkg_resources
 | 
					import pkg_resources, sys, os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    from PIL import ImageFont
 | 
					    from PIL import ImageFont
 | 
				
			||||||
except ImportError:
 | 
					except ImportError:
 | 
				
			||||||
@ -22,19 +23,29 @@ except ImportError:
 | 
				
			|||||||
Default fonts used in the PRS500
 | 
					Default fonts used in the PRS500
 | 
				
			||||||
'''
 | 
					'''
 | 
				
			||||||
FONT_MAP = {
 | 
					FONT_MAP = {
 | 
				
			||||||
            'Swis721 BT Roman'     : 'tt0003m_.ttf',
 | 
					            'Swis721 BT Roman'     : 'prs500/tt0003m_.ttf',
 | 
				
			||||||
            'Dutch801 Rm BT Roman' : 'tt0011m_.ttf',
 | 
					            'Dutch801 Rm BT Roman' : 'prs500/tt0011m_.ttf',
 | 
				
			||||||
            'Courier10 BT Roman'   : 'tt0419m_.ttf'
 | 
					            'Courier10 BT Roman'   : 'prs500/tt0419m_.ttf'
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_font_path(name):
 | 
				
			||||||
 | 
					    name = name.replace('/', os.path.sep)
 | 
				
			||||||
 | 
					    if hasattr(sys, 'frozen'):
 | 
				
			||||||
 | 
					        src = os.path.dirname(sys.executable)+os.path.sep+'fonts'+os.path.sep+name
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        src = pkg_resources.resource_filename('libprs500.ebooks.lrf.fonts', name)
 | 
				
			||||||
 | 
					    return src
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_font(name, size, encoding='unic'):
 | 
					def get_font(name, size, encoding='unic'):
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
    Get an ImageFont object by name. 
 | 
					    Get an ImageFont object by name. 
 | 
				
			||||||
    @param size: Font height in pixels. To convert from pts:
 | 
					    @param size: Font height in pixels. To convert from pts:
 | 
				
			||||||
                 sz in pixels = (dpi/72) * size in pts
 | 
					                 sz in pixels = (dpi/72) * size in pts
 | 
				
			||||||
    @param encoding: Font encoding to use. E.g. 'unic', 'symbol', 'ADOB', 'ADBE', 'aprm'
 | 
					    @param encoding: Font encoding to use. E.g. 'unic', 'symbol', 'ADOB', 'ADBE', 'aprm'
 | 
				
			||||||
 | 
					    @param manager: A dict that will store the PersistentTemporary
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
    if name in FONT_MAP.keys():
 | 
					    if name in FONT_MAP.keys():
 | 
				
			||||||
        path = pkg_resources.resource_filename('libprs500.ebooks.lrf.fonts', FONT_MAP[name])
 | 
					        path = get_font_path(FONT_MAP[name])
 | 
				
			||||||
        return ImageFont.truetype(path, size, encoding=encoding)
 | 
					        return ImageFont.truetype(path, size, encoding=encoding)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
@ -247,7 +247,8 @@ class HTMLConverter(object):
 | 
				
			|||||||
                 chapter_regex=re.compile('chapter|book|appendix', re.IGNORECASE),
 | 
					                 chapter_regex=re.compile('chapter|book|appendix', re.IGNORECASE),
 | 
				
			||||||
                 link_exclude=re.compile('$'), 
 | 
					                 link_exclude=re.compile('$'), 
 | 
				
			||||||
                 page_break=re.compile('h[12]', re.IGNORECASE),
 | 
					                 page_break=re.compile('h[12]', re.IGNORECASE),
 | 
				
			||||||
                 profile=PRS500_PROFILE, hide_broken_links=False):
 | 
					                 profile=PRS500_PROFILE, hide_broken_links=False,
 | 
				
			||||||
 | 
					                 disable_autorotation=False):
 | 
				
			||||||
        '''
 | 
					        '''
 | 
				
			||||||
        Convert HTML file at C{path} and add it to C{book}. After creating
 | 
					        Convert HTML file at C{path} and add it to C{book}. After creating
 | 
				
			||||||
        the object, you must call L{self.process_links} on it to create the links and
 | 
					        the object, you must call L{self.process_links} on it to create the links and
 | 
				
			||||||
@ -307,6 +308,7 @@ class HTMLConverter(object):
 | 
				
			|||||||
        self.rotated_images = {}  #: Temporary files with rotated version of images        
 | 
					        self.rotated_images = {}  #: Temporary files with rotated version of images        
 | 
				
			||||||
        self.max_link_levels = max_link_levels #: Number of link levels to process recursively
 | 
					        self.max_link_levels = max_link_levels #: Number of link levels to process recursively
 | 
				
			||||||
        self.link_level  = link_level  #: Current link level
 | 
					        self.link_level  = link_level  #: Current link level
 | 
				
			||||||
 | 
					        self.disable_autorotation = disable_autorotation
 | 
				
			||||||
        self.blockquote_style = book.create_block_style(sidemargin=60, 
 | 
					        self.blockquote_style = book.create_block_style(sidemargin=60, 
 | 
				
			||||||
                                                        topskip=20, footskip=20)
 | 
					                                                        topskip=20, footskip=20)
 | 
				
			||||||
        self.unindented_style = book.create_text_style(parindent=0)
 | 
					        self.unindented_style = book.create_text_style(parindent=0)
 | 
				
			||||||
@ -577,7 +579,8 @@ class HTMLConverter(object):
 | 
				
			|||||||
                                     chapter_regex=self.chapter_regex,
 | 
					                                     chapter_regex=self.chapter_regex,
 | 
				
			||||||
                                     link_exclude=self.link_exclude,
 | 
					                                     link_exclude=self.link_exclude,
 | 
				
			||||||
                                     page_break=self.page_break,
 | 
					                                     page_break=self.page_break,
 | 
				
			||||||
                                     hide_broken_links=self.hide_broken_links)
 | 
					                                     hide_broken_links=self.hide_broken_links,
 | 
				
			||||||
 | 
					                                     disable_autorotation=self.disable_autorotation)
 | 
				
			||||||
                        HTMLConverter.processed_files[path] = self.files[path]
 | 
					                        HTMLConverter.processed_files[path] = self.files[path]
 | 
				
			||||||
                    except Exception:
 | 
					                    except Exception:
 | 
				
			||||||
                        print >>sys.stderr, 'Unable to process', path
 | 
					                        print >>sys.stderr, 'Unable to process', path
 | 
				
			||||||
@ -758,7 +761,7 @@ class HTMLConverter(object):
 | 
				
			|||||||
            self.scaled_images[path] = pt
 | 
					            self.scaled_images[path] = pt
 | 
				
			||||||
            return pt.name
 | 
					            return pt.name
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        if width > self.profile.page_width and width > height:
 | 
					        if not self.disable_autorotation and width > self.profile.page_width and width > height:
 | 
				
			||||||
            pt = PersistentTemporaryFile(suffix='.jpeg')
 | 
					            pt = PersistentTemporaryFile(suffix='.jpeg')
 | 
				
			||||||
            im = im.rotate(-90)
 | 
					            im = im.rotate(-90)
 | 
				
			||||||
            im.convert('RGB').save(pt, 'JPEG')
 | 
					            im.convert('RGB').save(pt, 'JPEG')
 | 
				
			||||||
@ -813,7 +816,7 @@ class HTMLConverter(object):
 | 
				
			|||||||
            self.current_page.append(Canvas(width=self.profile.page_width,
 | 
					            self.current_page.append(Canvas(width=self.profile.page_width,
 | 
				
			||||||
                                            height=height))
 | 
					                                            height=height))
 | 
				
			||||||
            left = int(floor((self.profile.page_width - width)/2.))
 | 
					            left = int(floor((self.profile.page_width - width)/2.))
 | 
				
			||||||
            self.current_page.contents[0].put_object(ImageBlock(self.images[path]),
 | 
					            self.current_page.contents[-1].put_object(ImageBlock(self.images[path]),
 | 
				
			||||||
                                                  left, 0)
 | 
					                                                  left, 0)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def parse_tag(self, tag, parent_css):
 | 
					    def parse_tag(self, tag, parent_css):
 | 
				
			||||||
@ -1155,7 +1158,8 @@ def process_file(path, options):
 | 
				
			|||||||
                             chapter_detection=options.chapter_detection,
 | 
					                             chapter_detection=options.chapter_detection,
 | 
				
			||||||
                             chapter_regex=re.compile(options.chapter_regex, re.IGNORECASE),
 | 
					                             chapter_regex=re.compile(options.chapter_regex, re.IGNORECASE),
 | 
				
			||||||
                             link_exclude=re.compile(le), page_break=pb,
 | 
					                             link_exclude=re.compile(le), page_break=pb,
 | 
				
			||||||
                             hide_broken_links=not options.show_broken_links)
 | 
					                             hide_broken_links=not options.show_broken_links,
 | 
				
			||||||
 | 
					                             disable_autorotation=options.disable_autorotation)
 | 
				
			||||||
        conv.process_links()
 | 
					        conv.process_links()
 | 
				
			||||||
        oname = options.output
 | 
					        oname = options.output
 | 
				
			||||||
        if not oname:
 | 
					        if not oname:
 | 
				
			||||||
@ -1221,12 +1225,15 @@ def parse_options(argv=None, cli=True):
 | 
				
			|||||||
    parser = option_parser("""usage: %prog [options] mybook.[html|rar|zip]
 | 
					    parser = option_parser("""usage: %prog [options] mybook.[html|rar|zip]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
         %prog converts mybook.html to mybook.lrf""")
 | 
					         %prog converts mybook.html to mybook.lrf""")
 | 
				
			||||||
    parser.add_option('--cover', action='store', dest='cover', default=None, \
 | 
					    laf = parser.add_option_group('LOOK AND FEEL')
 | 
				
			||||||
 | 
					    laf.add_option('--cover', action='store', dest='cover', default=None, \
 | 
				
			||||||
                      help='Path to file containing image to be used as cover')
 | 
					                      help='Path to file containing image to be used as cover')
 | 
				
			||||||
    parser.add_option('--font-delta', action='store', type='int', default=0, \
 | 
					    laf.add_option('--font-delta', action='store', type='int', default=0, \
 | 
				
			||||||
                      help="""Increase the font size by 2 * FONT_DELTA pts. 
 | 
					                      help="""Increase the font size by 2 * FONT_DELTA pts. 
 | 
				
			||||||
                      If FONT_DELTA is negative, the font size is decreased.""",
 | 
					                      If FONT_DELTA is negative, the font size is decreased.""",
 | 
				
			||||||
                      dest='font_delta')
 | 
					                      dest='font_delta')
 | 
				
			||||||
 | 
					    laf.add_option('--disable-autorotation', action='store_true', default=False, 
 | 
				
			||||||
 | 
					                   help='Disable autorotation of images.', dest='disable_autorotation')
 | 
				
			||||||
    link = parser.add_option_group('LINK PROCESSING OPTIONS')
 | 
					    link = parser.add_option_group('LINK PROCESSING OPTIONS')
 | 
				
			||||||
    link.add_option('--link-levels', action='store', type='int', default=sys.maxint, \
 | 
					    link.add_option('--link-levels', action='store', type='int', default=sys.maxint, \
 | 
				
			||||||
                      dest='link_levels',
 | 
					                      dest='link_levels',
 | 
				
			||||||
 | 
				
			|||||||
@ -18,7 +18,7 @@
 | 
				
			|||||||
    <li><a href='#text'>Text formatting and ruled lines</a></li>
 | 
					    <li><a href='#text'>Text formatting and ruled lines</a></li>
 | 
				
			||||||
    <li><a href='#images'>Inline images</a></li>
 | 
					    <li><a href='#images'>Inline images</a></li>
 | 
				
			||||||
    <li><a href='#recursive'>Recursive link following</a></li>
 | 
					    <li><a href='#recursive'>Recursive link following</a></li>
 | 
				
			||||||
    <!--<li><a href='demo_ext.html'>The HTML used to create this file</a>-->
 | 
					    <li><a href='demo_ext.html'>The HTML used to create this file</a>
 | 
				
			||||||
 </ul>
 | 
					 </ul>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 <h2><a name='lists'>Lists</a></h2>
 | 
					 <h2><a name='lists'>Lists</a></h2>
 | 
				
			||||||
@ -62,6 +62,10 @@
 | 
				
			|||||||
 </p>
 | 
					 </p>
 | 
				
			||||||
 <br/>
 | 
					 <br/>
 | 
				
			||||||
 <p>
 | 
					 <p>
 | 
				
			||||||
 | 
					 Not that if you have custom fonts on your reader, the table may not be properly aligned.
 | 
				
			||||||
 | 
					 </p>
 | 
				
			||||||
 | 
					 <br />
 | 
				
			||||||
 | 
					 <p>
 | 
				
			||||||
 The table conversion code is very new and likely to be swarming with bugs, so please report them at <br/><font name="monospace>https://libprs500.kovidgoyal.net/newticket</font>
 | 
					 The table conversion code is very new and likely to be swarming with bugs, so please report them at <br/><font name="monospace>https://libprs500.kovidgoyal.net/newticket</font>
 | 
				
			||||||
 </p>
 | 
					 </p>
 | 
				
			||||||
 <br/>
 | 
					 <br/>
 | 
				
			||||||
 | 
				
			|||||||
@ -2313,7 +2313,7 @@ class ImageBlock(LrsObject, LrsContainer, LrsAttributes):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Font(LrsContainer):
 | 
					class Font(LrsContainer):
 | 
				
			||||||
    """ Allows a TrueType file to embedded in an Lrf. """
 | 
					    """ Allows a TrueType file to be embedded in an Lrf. """
 | 
				
			||||||
    def __init__(self, filename, facename):
 | 
					    def __init__(self, filename, facename):
 | 
				
			||||||
        LrsContainer.__init__(self, [])
 | 
					        LrsContainer.__init__(self, [])
 | 
				
			||||||
        _checkExists(filename)
 | 
					        _checkExists(filename)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user