Friday, April 20, 2012

Epub file format and Parsing epub files using FBReader Open Source Project in Android ?


What is Epub ?

Epub is an abbreviated form of electronic publication.It is is a free and open e-book standard by the International Digital Publishing Forum (IDPF).But in simple way it is a bunch of file which are zipped in one single file which is having extension .epub .
EPUB version 2.0.1 consists of three specifications:
·      Open Publication Structure (OPS) 2.0.1, contains the formatting of its content.
·      Open Packaging Format (OPF) 2.0.1, describes the structure of the .epub file in XML.
·      Open Container Format (OCF) 2.0.1, collects all files as a ZIP archive.
Open Publication Structure 2.0.1
An EPUB file uses XHTML 1.1 (or DTBook) to construct the content of a book as of version 2.0.1.
An example skeleton of an XHTML file for EPUB looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    <title>Pride and Prejudice</title>
    <link rel="stylesheet" href="css/main.css" type="text/css" />
  </head>
  <body>
    ...
  </body>
</html>
Open Packaging Format 2.0.1
The OPF specification's purpose is to "[define] the mechanism by which the various components of an OPS publication are tied together and provides additional structure and semantics to the electronic publication."[13] This is accomplished by two XML files with the extensions .opf and .ncx.
The .opf file houses the EPUB book's metadata, file manifest, and linear reading order. This file has a root element package and four child elements: metadata, manifest, spine, and guide. All of these except guide are required. Furthermore, the package node must have the unique-identifier attribute. The .opf file's mimetype is application/oebps-package+xml
An example .opf file:
<?xml version="1.0"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">

  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
    <dc:title>Pride and Prejudice</dc:title>
    <dc:language>en</dc:language>
    <dc:identifier id="BookId" opf:scheme="ISBN">123456789X</dc:identifier>
    <dc:creator opf:file-as="Austen, Jane" opf:role="aut">Jane Austen</dc:creator>
  </metadata>

  <manifest>
    <item id="chapter1" href="chapter1.xhtml" media-type="application/xhtml+xml"/>
    <item id="stylesheet" href="style.css" media-type="text/css"/>
    <item id="ch1-pic" href="ch1-pic.png" media-type="image/png"/>
    <item id="myfont" href="css/myfont.otf" media-type="application/x-font-opentype"/>
    <item id="ncx" href="book.ncx" media-type="application/x-dtbncx+xml"/>
  </manifest>

  <spine toc="ncx">
    <itemref idref="chapter1" />
  </spine>

  <guide>
    <reference type="loi" title="List Of Illustrations" href="appendix.html#figures" />
  </guide>

</package>


.ncx file
The .ncx file (Navigation Control file for XML) contains the hierarchical table of contents for the EPUB file. The specification for .ncx was developed for Digital Talking Book (DTB), is maintained by the DAISY Consortium, and is not a part of the EPUB specification. The .ncx file has a mimetype of application/x-dtbncx+xml.
An example .ncx file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">

<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">

  <head>
<!-- The following four metadata items are required for all NCX documents,
including those conforming to the relaxed constraints of OPS 2.0 -->

    <meta name="dtb:uid" content="123456789X"/> <!-- same as in .opf -->
    <meta name="dtb:depth" content="1"/> <!-- 1 or higher -->
    <meta name="dtb:totalPageCount" content="0"/> <!-- must be 0 -->
    <meta name="dtb:maxPageNumber" content="0"/> <!-- must be 0 -->
  </head>

  <docTitle>
    <text>Pride and Prejudice</text>
  </docTitle>

  <docAuthor>
    <text>Austen, Jane</text>
  </docAuthor>

  <navMap>
    <navPoint class="chapter" id="chapter1" playOrder="1">
      <navLabel><text>Chapter 1</text></navLabel>
      <content src="chapter1.xhtml"/>
    </navPoint>
  </navMap>

</ncx>
Open Container Format 2.0.1
An EPUB file is a group of files conforming to the OPS/OPF standards that is wrapped in a ZIP file.[3] The OCF specifies how these files should be organized in the ZIP, and defines two additional files that must be included.
An example container.xml, given the above file structure:
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
    <rootfile full-path="OPS/book.opf" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>

FBReader Open Source Project

FBReaderJ is an e-book reader for the Android platform. It is a clone of the FBReader book reader written in Java. FBReaderJ was partially written as a student project at Academy of Modern Software Engineering (the project was done by Olga Melanich, Marina Sokol, Eugene Vlasov and Grigory Yaroslavtsev).
FBReaderJ supports several e-book formats: oeb, epub, fb2. In future releases the list will be extended to support the same formats as the original FBReader. Direct reading from zip, tar and gzip archives is supported.
FBReaderJ is distributed under the terms of the GNU GPL.
Now here is some guidelines to customize FB Reader. I will explore only those classes which are really needed for the cutomization.
·      FBReader is the class which handles the view of the epub reader, the interface which displays you the content of book. It has several menu actions and touch actions you can modify them as per your requirement.
·      What action to perform when a Menu Item is clicked that you can find in ZLAndroidApplication
·      ZLAndroidWidget is the class which refreshes the view when user turns the page or adjust font face,font size,brightness etc.. And also it has code to draw using specific height and width of canvas.
·      ZLAndroidPaintContext is the class which draws a view on window.
·      ImageView Activity is the class which hadles the tap on Image in a book. It displays that Image
·      Now there are some settings reagrding font,background wallpaper etc.. All is in org.geometerplus.android.fbreader.preferences package.
·      PreferenceActivity is the Activity which provides inteface to change settings like font,margins,background etc.
·      FBReaderApp has defined some constructors which provides default values to the app when app starts for the very first time or whenever related data is not found.
·      SQLiteBookDatabase is the class which has all implemented methods which creates,update the database as weel has methods to do insert,update,delete operation on books related transaction only.
·      org.amse.ys.zip is the package where you can find all logic related to parsing an epub file.
·      FBReader has defined all their resources in assets folder.You can find resources like labels used in app,help file, tapzones definition.
·      An interesting part of FBReader is they have used NDK in it. All stuff related to it you can find in jni folder.
Some Example :
Remove an Item From Menu :
Open FBReader class. You will find a line fbReader.addAction(ActionCode.SHOW_LIBRARY, new ShowLibraryAction(this, fbReader)); in onCreate Method. This line adds a class which has code to perform when Library MenuItem is clicked.
And onCreateOptionsMenu has lines of code which adds MenuItem you can remove or add MenuItem according to your requirement. Do not forgot to define Action for that.
Changing a label :
Say you want to display My Collection in stead of lable Library. Go to assets folder->resourses->application->en.xml
Search for <node name="menu"> under this you will find <node name="library" value="Library"/>
Replace Library by My Collection. I would advice not to modify the name attribute until and unless you have thoughrly understand the flow.

2 comments:

  1. how to Read asset folder File using FBReader

    ReplyDelete
  2. sir, images in the book are not showing in the FbReader, i am very tense due to it
    can u plz help me

    ReplyDelete