mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1557 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
a2d29880aa
commit
608406ab1c
7 changed files with 253 additions and 1 deletions
|
@ -22,12 +22,14 @@ package org.geometerplus.fbreader.formats;
|
||||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
import org.geometerplus.fbreader.library.Book;
|
import org.geometerplus.fbreader.library.Book;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
|
||||||
public abstract class FormatPlugin {
|
public abstract class FormatPlugin {
|
||||||
public abstract boolean acceptsFile(ZLFile file);
|
public abstract boolean acceptsFile(ZLFile file);
|
||||||
public abstract boolean readMetaInfo(Book book);
|
public abstract boolean readMetaInfo(Book book);
|
||||||
public abstract boolean readModel(BookModel model);
|
public abstract boolean readModel(BookModel model);
|
||||||
|
public abstract ZLImage readCover(Book book);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public static void detectEncodingAndLanguage(Book book, InputStream stream) throws IOException {
|
public static void detectEncodingAndLanguage(Book book, InputStream stream) throws IOException {
|
||||||
String language = book.getLanguage();
|
String language = book.getLanguage();
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
import org.geometerplus.fbreader.library.Book;
|
import org.geometerplus.fbreader.library.Book;
|
||||||
import org.geometerplus.fbreader.formats.FormatPlugin;
|
import org.geometerplus.fbreader.formats.FormatPlugin;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
|
||||||
public class FB2Plugin extends FormatPlugin {
|
public class FB2Plugin extends FormatPlugin {
|
||||||
@Override
|
@Override
|
||||||
|
@ -39,4 +40,10 @@ public class FB2Plugin extends FormatPlugin {
|
||||||
public boolean readModel(BookModel model) {
|
public boolean readModel(BookModel model) {
|
||||||
return new FB2Reader(model).readBook();
|
return new FB2Reader(model).readBook();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZLImage readCover(Book book) {
|
||||||
|
// TODO: implement
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
import org.geometerplus.fbreader.library.Book;
|
import org.geometerplus.fbreader.library.Book;
|
||||||
import org.geometerplus.fbreader.formats.FormatPlugin;
|
import org.geometerplus.fbreader.formats.FormatPlugin;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
|
||||||
public class HtmlPlugin extends FormatPlugin {
|
public class HtmlPlugin extends FormatPlugin {
|
||||||
|
|
||||||
|
@ -47,4 +48,9 @@ public class HtmlPlugin extends FormatPlugin {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZLImage readCover(Book book) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
124
src/org/geometerplus/fbreader/formats/oeb/OEBCoverReader.java
Normal file
124
src/org/geometerplus/fbreader/formats/oeb/OEBCoverReader.java
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2007-2010 Geometer Plus <contact@geometerplus.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.geometerplus.fbreader.formats.oeb;
|
||||||
|
|
||||||
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLFileImage;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
import org.geometerplus.zlibrary.core.xml.*;
|
||||||
|
|
||||||
|
import org.geometerplus.fbreader.constants.XMLNamespace;
|
||||||
|
import org.geometerplus.fbreader.formats.util.MiscUtil;
|
||||||
|
|
||||||
|
class OEBCoverReader extends ZLXMLReaderAdapter implements XMLNamespace {
|
||||||
|
|
||||||
|
private class XHTMLImageFinder extends ZLXMLReaderAdapter {
|
||||||
|
|
||||||
|
private static final String IMG = "img";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean startElementHandler(String tag, ZLStringMap attributes) {
|
||||||
|
tag = tag.toLowerCase().intern();
|
||||||
|
if (tag == IMG) {
|
||||||
|
final String src = attributes.getValue("src");
|
||||||
|
if (src != null) {
|
||||||
|
myImage = new ZLFileImage(
|
||||||
|
"image/auto",
|
||||||
|
ZLFile.createFileByPath(myPathPrefix + src)
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private ZLImage myImage;
|
||||||
|
private String myPathPrefix;
|
||||||
|
private String myCoverXHTML;
|
||||||
|
private boolean myReadGuide;
|
||||||
|
|
||||||
|
|
||||||
|
OEBCoverReader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZLImage readCover(ZLFile file) {
|
||||||
|
myPathPrefix = MiscUtil.htmlDirectoryPrefix(file);
|
||||||
|
myReadGuide = false;
|
||||||
|
myImage = null;
|
||||||
|
myCoverXHTML = null;
|
||||||
|
if (!read(file) || myCoverXHTML == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ZLFile coverFile = ZLFile.createFileByPath(myCoverXHTML);
|
||||||
|
final String ext = coverFile.getExtension();
|
||||||
|
if (ext != null && (ext.equals("gif") || ext.equals("jpg") || ext.equals("jpeg"))) {
|
||||||
|
myImage = new ZLFileImage("image/auto", coverFile);
|
||||||
|
} else {
|
||||||
|
new XHTMLImageFinder().read(coverFile);
|
||||||
|
}
|
||||||
|
return myImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String GUIDE = "guide";
|
||||||
|
private static final String REFERENCE = "reference";
|
||||||
|
private static final String COVER = "cover";
|
||||||
|
private static final String COVER_IMAGE = "other.ms-coverimage-standard";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean startElementHandler(String tag, ZLStringMap attributes) {
|
||||||
|
tag = tag.toLowerCase().intern();
|
||||||
|
if (GUIDE == tag) {
|
||||||
|
myReadGuide = true;
|
||||||
|
} else if (myReadGuide && REFERENCE == tag) {
|
||||||
|
final String type = attributes.getValue("type");
|
||||||
|
if (type != null) {
|
||||||
|
if (COVER == type) {
|
||||||
|
final String href = attributes.getValue("href");
|
||||||
|
if (href != null) {
|
||||||
|
myCoverXHTML = myPathPrefix + MiscUtil.decodeHtmlReference(href);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (COVER_IMAGE == type) {
|
||||||
|
final String href = attributes.getValue("href");
|
||||||
|
if (href != null) {
|
||||||
|
myImage = new ZLFileImage(
|
||||||
|
"image/auto",
|
||||||
|
ZLFile.createFileByPath(myPathPrefix + MiscUtil.decodeHtmlReference(href))
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean endElementHandler(String tag) {
|
||||||
|
tag = tag.toLowerCase();
|
||||||
|
if (GUIDE == tag) {
|
||||||
|
myReadGuide = false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
import org.geometerplus.fbreader.library.Book;
|
import org.geometerplus.fbreader.library.Book;
|
||||||
import org.geometerplus.fbreader.formats.FormatPlugin;
|
import org.geometerplus.fbreader.formats.FormatPlugin;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.*;
|
import org.geometerplus.zlibrary.core.filesystem.*;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
|
||||||
public class OEBPlugin extends FormatPlugin {
|
public class OEBPlugin extends FormatPlugin {
|
||||||
public boolean acceptsFile(ZLFile file) {
|
public boolean acceptsFile(ZLFile file) {
|
||||||
|
@ -43,14 +44,21 @@ public class OEBPlugin extends FormatPlugin {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean readMetaInfo(Book book) {
|
public boolean readMetaInfo(Book book) {
|
||||||
final ZLFile opfFile = getOpfFile(book.File);
|
final ZLFile opfFile = getOpfFile(book.File);
|
||||||
return (opfFile != null) ? new OEBMetaInfoReader(book).readMetaInfo(opfFile) : false;
|
return (opfFile != null) ? new OEBMetaInfoReader(book).readMetaInfo(opfFile) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean readModel(BookModel model) {
|
public boolean readModel(BookModel model) {
|
||||||
model.Book.File.setCached(true);
|
model.Book.File.setCached(true);
|
||||||
final ZLFile opfFile = getOpfFile(model.Book.File);
|
final ZLFile opfFile = getOpfFile(model.Book.File);
|
||||||
return (opfFile != null) ? new OEBBookReader(model).readBook(opfFile) : false;
|
return (opfFile != null) ? new OEBBookReader(model).readBook(opfFile) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZLImage readCover(Book book) {
|
||||||
|
return new OEBCoverReader().readCover(book.File);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ package org.geometerplus.fbreader.formats.pdb;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
import org.geometerplus.zlibrary.core.encoding.ZLEncodingCollection;
|
import org.geometerplus.zlibrary.core.encoding.ZLEncodingCollection;
|
||||||
import org.geometerplus.zlibrary.core.util.ZLLanguageUtil;
|
import org.geometerplus.zlibrary.core.util.ZLLanguageUtil;
|
||||||
|
|
||||||
|
@ -130,4 +131,102 @@ public class MobipocketPlugin extends PdbPlugin {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZLImage readCover(Book book) {
|
||||||
|
InputStream stream = null;
|
||||||
|
try {
|
||||||
|
stream = book.File.getInputStream();
|
||||||
|
final PdbHeader header = new PdbHeader(stream);
|
||||||
|
PdbUtil.skip(stream, header.Offsets[0] + 16 - header.length());
|
||||||
|
if (PdbUtil.readInt(stream) != 0x4D4F4249) /* "MOBI" */ {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final int length = (int)PdbUtil.readInt(stream);
|
||||||
|
PdbUtil.skip(stream, 104);
|
||||||
|
|
||||||
|
final int exthFlags = (int)PdbUtil.readInt(stream);
|
||||||
|
int coverIndex = -1;
|
||||||
|
int thumbIndex = -1;
|
||||||
|
|
||||||
|
int offset = 132;
|
||||||
|
if ((exthFlags & 0x40) != 0) {
|
||||||
|
PdbUtil.skip(stream, length - 116);
|
||||||
|
offset = length + 20;
|
||||||
|
if (PdbUtil.readInt(stream) != 0x45585448) /* "EXTH" */ {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
PdbUtil.skip(stream, 4);
|
||||||
|
final int recordsNumber = (int)PdbUtil.readInt(stream);
|
||||||
|
offset += 8;
|
||||||
|
for (int i = 0; i < recordsNumber; ++i) {
|
||||||
|
final int type = (int)PdbUtil.readInt(stream);
|
||||||
|
final int size = (int)PdbUtil.readInt(stream);
|
||||||
|
offset += size;
|
||||||
|
if (size <= 8) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
switch (type) {
|
||||||
|
default:
|
||||||
|
PdbUtil.skip(stream, size - 8);
|
||||||
|
break;
|
||||||
|
case 201:
|
||||||
|
{
|
||||||
|
if (size == 12) {
|
||||||
|
coverIndex = (int)PdbUtil.readInt(stream);
|
||||||
|
} else {
|
||||||
|
PdbUtil.skip(stream, size - 8);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 202:
|
||||||
|
{
|
||||||
|
if (size == 12) {
|
||||||
|
thumbIndex = (int)PdbUtil.readInt(stream);
|
||||||
|
} else {
|
||||||
|
PdbUtil.skip(stream, size - 8);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final InputStream tempStream = stream;
|
||||||
|
stream = null;
|
||||||
|
tempStream.close();
|
||||||
|
|
||||||
|
if (coverIndex == -1) {
|
||||||
|
if (thumbIndex == -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
coverIndex = thumbIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ZLFile file = book.File;
|
||||||
|
final MobipocketStream mpStream = new MobipocketStream(file);
|
||||||
|
|
||||||
|
// TODO: implement
|
||||||
|
/*int index = pbStream.firstImageLocationIndex(file.path());
|
||||||
|
if (index >= 0) {
|
||||||
|
std::pair<int,int> imageLocation = pbStream.imageLocation(pbStream.header(), index + coverIndex);
|
||||||
|
if ((imageLocation.first > 0) && (imageLocation.second > 0)) {
|
||||||
|
return new ZLFileImage(
|
||||||
|
file,
|
||||||
|
imageLocation.first,
|
||||||
|
imageLocation.second
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
if (stream != null) {
|
||||||
|
try {
|
||||||
|
stream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||||
import org.geometerplus.fbreader.library.Book;
|
import org.geometerplus.fbreader.library.Book;
|
||||||
import org.geometerplus.fbreader.formats.pdb.PdbPlugin;
|
import org.geometerplus.fbreader.formats.pdb.PdbPlugin;
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
|
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||||
|
|
||||||
public class PluckerPlugin extends PdbPlugin {
|
public class PluckerPlugin extends PdbPlugin {
|
||||||
@Override
|
@Override
|
||||||
|
@ -54,4 +55,9 @@ public class PluckerPlugin extends PdbPlugin {
|
||||||
public boolean readModel(BookModel model) {
|
public boolean readModel(BookModel model) {
|
||||||
return new PluckerBookReader(model.Book.File, model, model.Book.getEncoding()).readDocument();
|
return new PluckerBookReader(model.Book.File, model, model.Book.getEncoding()).readDocument();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ZLImage readCover(Book book) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue