2014年1月5日 星期日

ePub Reader App

For iOS ePub reader app, like iBook:

Low level ---> High level
Core Text --> Text Kit or Cocoa text system --> App

Ref: https://developer.apple.com/library/mac/DOCUMENTATION/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html


Implement using WebView and Titanium
Ref: http://stackoverflow.com/questions/1388467/reading-epub-format/4436783#4436783

1) create a view with a UIWebView
2) download the EPUB file
3) unzip it to a subdirectory in your app's documents folder .

4) parse the XML file at META-INF/container.xml (if this file doesn't exist the EPUB is invalid) [Parsing XML in Titanium http://developer.appcelerator.com/question/15881/xml-parsing ]

Note: OEBPS (Open Ebook Forum Publication Structure) has two version 1.2 and 1.0  [http://www.digitalpreservation.gov/formats/fdd/fdd000171.shtml

5) In this XML, find the first "rootfile" with media-type application/oebps-package+xml. This is the OPF file for the book.
6) parse the OPF file (also XML)
7) now you need to know what the first chapter of the book is.
a) each <item> in the <manifest> element has an id and an href. Store these in an NSDictionary where the key is the id and the object is the href.

b) Look at the first <itemref> in the <spine>. It has an idref attribute which corresponds to one of the ids in (a). Look up that id in the NSDictionary and you'll get an href.

c) this is the file of the first chapter to show the user. Work out what the full path is (hint: it's wherever you unzipped the zip file to in (3) plus the base directory of the OPF file in (6))

8) create an NSURL using fileURLWithPath:, where the path is the full path from (7c). Load this request using the UIWebView you created in (1).

You'll need to implement forward / backward buttons or swipes or something so that users can move from one chapter to another. Use the <spine> to work out which file to show next - the <itemrefs> in the XML are in the order they should appear to the reader.

Beware of differences between different OPF versions.
OPF v2 Spec: http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#AppendixA
OPF v3 Spec: http://www.idpf.org/epub/30/spec/epub30-publications.html

For example, for book cover image, definition in OPF v3 is
<manifest>
<item id="cover" href="cover.jpg" media-type="image/jpeg" properties="cover-image"/> 
.... 
<manifest>

However in OPF v2, it is defined using the following pair:
<meta name="cover" content="My_Cover_ID" />
and 
<item href="cover.jpg" id="My_Cover_ID" media-type="image/jpeg" />


沒有留言:

張貼留言