diff --git a/src/org/geometerplus/android/fbreader/DisplayBookPopupAction.java b/src/org/geometerplus/android/fbreader/DisplayBookPopupAction.java index 6eb0f2577..bcc203e4c 100644 --- a/src/org/geometerplus/android/fbreader/DisplayBookPopupAction.java +++ b/src/org/geometerplus/android/fbreader/DisplayBookPopupAction.java @@ -70,10 +70,14 @@ class DisplayBookPopupAction extends FBAndroidAction { return; } final ZLTextRegion region = (ZLTextRegion)params[0]; - if (!(region.getSoul() instanceof BookRegionSoul)) { + if (!(region.getSoul() instanceof ExtensionRegionSoul)) { return; } - final BookElement element = ((BookRegionSoul)region.getSoul()).Element; + final ExtensionElement e = ((ExtensionRegionSoul)region.getSoul()).Element; + if (!(e instanceof BookElement)) { + return; + } + final BookElement element = (BookElement)e; if (!element.isInitialized()) { return; } diff --git a/src/org/geometerplus/fbreader/fbreader/FBView.java b/src/org/geometerplus/fbreader/fbreader/FBView.java index af23b97cb..3d060ba51 100644 --- a/src/org/geometerplus/fbreader/fbreader/FBView.java +++ b/src/org/geometerplus/fbreader/fbreader/FBView.java @@ -88,7 +88,7 @@ public final class FBView extends ZLTextView { return true; } - final ZLTextRegion bookRegion = findRegion(x, y, 0, ZLTextRegion.BookFilter); + final ZLTextRegion bookRegion = findRegion(x, y, 0, ZLTextRegion.ExtensionFilter); if (bookRegion != null) { myReader.runAction(ActionCode.DISPLAY_BOOK_POPUP, bookRegion); return true; diff --git a/src/org/geometerplus/zlibrary/text/view/BookElement.java b/src/org/geometerplus/zlibrary/text/view/BookElement.java index 25ee4c502..e35a9d610 100644 --- a/src/org/geometerplus/zlibrary/text/view/BookElement.java +++ b/src/org/geometerplus/zlibrary/text/view/BookElement.java @@ -20,8 +20,10 @@ package org.geometerplus.zlibrary.text.view; import org.geometerplus.zlibrary.core.image.ZLImageData; +import org.geometerplus.zlibrary.core.library.ZLibrary; +import org.geometerplus.zlibrary.core.view.ZLPaintContext; -public final class BookElement extends ZLTextElement { +public final class BookElement extends ExtensionElement { public boolean isInitialized() { return false; } @@ -29,4 +31,47 @@ public final class BookElement extends ZLTextElement { public ZLImageData getImageData() { return null; } + + @Override + protected int getWidth(ZLTextViewBase view) { + // 1/\phi (= 0.618) inch width + 1/10 inch left & right margin + return Math.min(ZLibrary.Instance().getDisplayDPI() * 818 / 1000, view.getTextColumnWidth()); + } + + @Override + protected int getHeight(ZLTextViewBase view) { + // 1 inch height + 1/15 inch top & bottom margin + return ZLibrary.Instance().getDisplayDPI() * 17 / 15; + } + + @Override + protected void draw(ZLPaintContext context, ZLTextView view, ZLTextElementArea area) { + final int vMargin = ZLibrary.Instance().getDisplayDPI() / 15; + final int hMargin = ZLibrary.Instance().getDisplayDPI() / 10; + final ZLImageData imageData = getImageData(); + if (imageData != null) { + context.drawImage( + area.XStart + hMargin, area.YEnd - vMargin, + imageData, + new ZLPaintContext.Size( + area.XEnd - area.XStart - 2 * hMargin + 1, + area.YEnd - area.YStart - 2 * vMargin + 1 + ), + ZLPaintContext.ScalingType.FitMaximum, + ZLPaintContext.ColorAdjustingMode.NONE + ); + } else { + context.setLineColor(view.getTextColor(ZLTextHyperlink.NO_LINK)); + context.setFillColor(view.getTextColor(ZLTextHyperlink.NO_LINK), 0x33); + final int xStart = area.XStart + hMargin; + final int xEnd = area.XEnd - hMargin; + final int yStart = area.YStart + vMargin; + final int yEnd = area.YEnd - vMargin; + context.fillRectangle(xStart, yStart, xEnd, yEnd); + context.drawLine(xStart, yStart, xStart, yEnd); + context.drawLine(xStart, yEnd, xEnd, yEnd); + context.drawLine(xEnd, yEnd, xEnd, yStart); + context.drawLine(xEnd, yStart, xStart, yStart); + } + } } diff --git a/src/org/geometerplus/zlibrary/text/view/ExtensionElement.java b/src/org/geometerplus/zlibrary/text/view/ExtensionElement.java new file mode 100644 index 000000000..b3ae58e71 --- /dev/null +++ b/src/org/geometerplus/zlibrary/text/view/ExtensionElement.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2007-2014 Geometer Plus + * + * 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.zlibrary.text.view; + +import org.geometerplus.zlibrary.core.view.ZLPaintContext; + +public abstract class ExtensionElement extends ZLTextElement { + protected abstract int getWidth(ZLTextViewBase view); + protected abstract int getHeight(ZLTextViewBase view); + + protected abstract void draw(ZLPaintContext context, ZLTextView view, ZLTextElementArea area); +} diff --git a/src/org/geometerplus/zlibrary/text/view/ExtensionElementManager.java b/src/org/geometerplus/zlibrary/text/view/ExtensionElementManager.java index f64abe19a..45ce28c30 100644 --- a/src/org/geometerplus/zlibrary/text/view/ExtensionElementManager.java +++ b/src/org/geometerplus/zlibrary/text/view/ExtensionElementManager.java @@ -25,9 +25,9 @@ import java.util.Map; import org.geometerplus.zlibrary.text.model.ExtensionEntry; public abstract class ExtensionElementManager { - final List getElements(ExtensionEntry entry) { + final List getElements(ExtensionEntry entry) { return getElements(entry.Type, entry.Data); } - protected abstract List getElements(String type, Map data); + protected abstract List getElements(String type, Map data); } diff --git a/src/org/geometerplus/zlibrary/text/view/BookRegionSoul.java b/src/org/geometerplus/zlibrary/text/view/ExtensionRegionSoul.java similarity index 84% rename from src/org/geometerplus/zlibrary/text/view/BookRegionSoul.java rename to src/org/geometerplus/zlibrary/text/view/ExtensionRegionSoul.java index e3124aa89..7ba285441 100644 --- a/src/org/geometerplus/zlibrary/text/view/BookRegionSoul.java +++ b/src/org/geometerplus/zlibrary/text/view/ExtensionRegionSoul.java @@ -19,10 +19,10 @@ package org.geometerplus.zlibrary.text.view; -public class BookRegionSoul extends ZLTextRegion.Soul { - public final BookElement Element; +public class ExtensionRegionSoul extends ZLTextRegion.Soul { + public final ExtensionElement Element; - BookRegionSoul(ZLTextPosition position, BookElement element) { + ExtensionRegionSoul(ZLTextPosition position, ExtensionElement element) { super(position.getParagraphIndex(), position.getElementIndex(), position.getElementIndex()); Element = element; } diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextElementAreaVector.java b/src/org/geometerplus/zlibrary/text/view/ZLTextElementAreaVector.java index b035c7bb0..9ae77ba30 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextElementAreaVector.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextElementAreaVector.java @@ -71,8 +71,8 @@ final class ZLTextElementAreaVector { soul = new ZLTextVideoRegionSoul(area, (ZLTextVideoElement)area.Element); } else if (area.Element instanceof ZLTextWord && !((ZLTextWord)area.Element).isASpace()) { soul = new ZLTextWordRegionSoul(area, (ZLTextWord)area.Element); - } else if (area.Element instanceof BookElement) { - soul = new BookRegionSoul(area, (BookElement)area.Element); + } else if (area.Element instanceof ExtensionElement) { + soul = new ExtensionRegionSoul(area, (ExtensionElement)area.Element); } if (soul != null) { myCurrentElementRegion = new ZLTextRegion(soul, myAreas, myAreas.size()); diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextRegion.java b/src/org/geometerplus/zlibrary/text/view/ZLTextRegion.java index e6b8ff57e..919e6a1f4 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextRegion.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextRegion.java @@ -118,9 +118,9 @@ public final class ZLTextRegion { } }; - public static Filter BookFilter = new Filter() { + public static Filter ExtensionFilter = new Filter() { public boolean accepts(ZLTextRegion region) { - return region.getSoul() instanceof BookRegionSoul; + return region.getSoul() instanceof ExtensionRegionSoul; } }; diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java index 43427a37a..b7bed6968 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextView.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextView.java @@ -941,34 +941,8 @@ public abstract class ZLTextView extends ZLTextViewBase { final int c = yStart + (yEnd - yStart) / 2; context.setFillColor(new ZLColor(196, 196, 196)); context.fillPolygon(new int[] { l, l, r }, new int[] { t, b, c }); - } else if (element instanceof BookElement) { - final int vMargin = ZLibrary.Instance().getDisplayDPI() / 15; - final int hMargin = ZLibrary.Instance().getDisplayDPI() / 10; - final ZLImageData imageData = ((BookElement)element).getImageData(); - if (imageData != null) { - context.drawImage( - area.XStart + hMargin, area.YEnd - vMargin, - imageData, - new ZLPaintContext.Size( - area.XEnd - area.XStart - 2 * hMargin + 1, - area.YEnd - area.YStart - 2 * vMargin + 1 - ), - ZLPaintContext.ScalingType.FitMaximum, - ZLPaintContext.ColorAdjustingMode.NONE - ); - } else { - context.setLineColor(getTextColor(ZLTextHyperlink.NO_LINK)); - context.setFillColor(getTextColor(ZLTextHyperlink.NO_LINK), 0x33); - final int xStart = area.XStart + hMargin; - final int xEnd = area.XEnd - hMargin; - final int yStart = area.YStart + vMargin; - final int yEnd = area.YEnd - vMargin; - context.fillRectangle(xStart, yStart, xEnd, yEnd); - context.drawLine(xStart, yStart, xStart, yEnd); - context.drawLine(xStart, yEnd, xEnd, yEnd); - context.drawLine(xEnd, yEnd, xEnd, yStart); - context.drawLine(xEnd, yStart, xStart, yStart); - } + } else if (element instanceof ExtensionElement) { + ((ExtensionElement)element).draw(context, this, area); } else if (element == ZLTextElement.HSpace) { final int cw = context.getSpaceWidth(); /* @@ -1188,7 +1162,7 @@ public abstract class ZLTextView extends ZLTextViewBase { } else if (element instanceof ZLTextVideoElement) { wordOccurred = true; isVisible = true; - } else if (element instanceof BookElement) { + } else if (element instanceof ExtensionElement) { wordOccurred = true; isVisible = true; } else if (isStyleChangeElement(element)) { @@ -1391,7 +1365,7 @@ public abstract class ZLTextView extends ZLTextViewBase { wordOccurred = false; --spaceCounter; } - } else if (element instanceof ZLTextWord || element instanceof ZLTextImageElement || element instanceof ZLTextVideoElement || element instanceof BookElement) { + } else if (element instanceof ZLTextWord || element instanceof ZLTextImageElement || element instanceof ZLTextVideoElement || element instanceof ExtensionElement) { final int height = getElementHeight(element); final int descent = getElementDescent(element); final int length = element instanceof ZLTextWord ? ((ZLTextWord)element).Length : 0; diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java b/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java index 944d5509a..fff20d39e 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java @@ -199,9 +199,8 @@ abstract class ZLTextViewBase extends ZLView { return size != null ? size.Width : 0; } else if (element instanceof ZLTextVideoElement) { return Math.min(300, getTextColumnWidth()); - } else if (element instanceof BookElement) { - // 1/\phi (= 0.618) inch width + 1/10 inch left & right margin - return Math.min(ZLibrary.Instance().getDisplayDPI() * 818 / 1000, getTextColumnWidth()); + } else if (element instanceof ExtensionElement) { + return ((ExtensionElement)element).getWidth(this); } else if (element == ZLTextElement.Indent) { return myTextStyle.getFirstLineIndent(metrics()); } else if (element instanceof ZLTextFixedHSpaceElement) { @@ -225,9 +224,8 @@ abstract class ZLTextViewBase extends ZLView { Math.max(getContext().getStringHeight() * (myTextStyle.getLineSpacePercent() - 100) / 100, 3); } else if (element instanceof ZLTextVideoElement) { return Math.min(Math.min(200, getTextAreaHeight()), getTextColumnWidth() * 2 / 3); - } else if (element instanceof BookElement) { - // 1 inch height + 1/15 inch top & bottom margin - return ZLibrary.Instance().getDisplayDPI() * 17 / 15; + } else if (element instanceof ExtensionElement) { + return ((ExtensionElement)element).getHeight(this); } return 0; }