1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

refactoring: ZLPaintContext static instance has gone

This commit is contained in:
Nikolay Pultsin 2010-10-22 20:10:11 +01:00
parent bdd95679f1
commit f02f50cea8
11 changed files with 155 additions and 97 deletions

View file

@ -31,7 +31,6 @@ public final class FBView extends ZLTextView {
private FBReaderApp myReader;
FBView(FBReaderApp reader) {
super(ZLibrary.Instance().getPaintContext());
myReader = reader;
}
@ -116,15 +115,15 @@ public final class FBView extends ZLTextView {
myIsManualScrollingActive = true;
} else {
if (preferences.HorizontalOption.getValue()) {
if (x <= Context.getWidth() / 3) {
if (x <= myContext.getWidth() / 3) {
doScrollPage(false);
} else if (x >= Context.getWidth() * 2 / 3) {
} else if (x >= myContext.getWidth() * 2 / 3) {
doScrollPage(true);
}
} else {
if (y <= Context.getHeight() / 3) {
if (y <= myContext.getHeight() / 3) {
doScrollPage(false);
} else if (y >= Context.getHeight() * 2 / 3) {
} else if (y >= myContext.getHeight() * 2 / 3) {
doScrollPage(true);
}
}
@ -193,8 +192,8 @@ public final class FBView extends ZLTextView {
}
}
if (doScroll) {
final int h = Context.getHeight();
final int w = Context.getWidth();
final int h = myContext.getHeight();
final int w = myContext.getWidth();
final int minDiff = horizontal ?
((w > h) ? w / 4 : w / 3) :
((h > w) ? h / 4 : h / 3);

View file

@ -45,7 +45,7 @@ public class OptionsDialog {
new FormatOptionsPage(myDialog.createTab("Format"));
new StyleOptionsPage(myDialog.createTab("Styles"), ZLibrary.Instance().getPaintContext());
new StyleOptionsPage(myDialog.createTab("Styles"), fbreader.getCurrentView().getContext());
final ZLDialogContent colorsTab = myDialog.createTab("Colors");
final String colorKey = "colorFor";

View file

@ -36,6 +36,5 @@ public abstract class ZLibrary {
abstract public ZLResourceFile createResourceFile(String path);
abstract public String getVersionName();
abstract public ZLPaintContext getPaintContext();
abstract public void openInBrowser(String reference);
}

View file

@ -0,0 +1,95 @@
/*
* 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.zlibrary.core.view;
import java.util.*;
import org.geometerplus.zlibrary.core.util.*;
import org.geometerplus.zlibrary.core.image.ZLImageData;
final class DummyPaintContext extends ZLPaintContext {
DummyPaintContext() {
}
public void clear(ZLColor color) {
}
protected void setFontInternal(String family, int size, boolean bold, boolean italic, boolean underline) {
}
public void setTextColor(ZLColor color) {
}
public void setLineColor(ZLColor color, int style) {
}
public void setFillColor(ZLColor color, int style) {
}
public int getWidth() {
return 1;
}
public int getHeight() {
return 1;
}
public int getStringWidth(char[] string, int offset, int length) {
return 1;
}
protected int getSpaceWidthInternal() {
return 1;
}
protected int getStringHeightInternal() {
return 1;
}
protected int getDescentInternal() {
return 1;
}
public void drawString(int x, int y, char[] string, int offset, int length) {
}
public int imageWidth(ZLImageData image) {
return 1;
}
public int imageHeight(ZLImageData image) {
return 1;
}
public void drawImage(int x, int y, ZLImageData image) {
}
public void drawLine(int x0, int y0, int x1, int y1) {
}
public void fillRectangle(int x0, int y0, int x1, int y1) {
}
public void drawFilledCircle(int x, int y, int r) {
}
public void drawOutline(int[] xs, int ys[]) {
}
public String realFontFamilyName(String fontFamily) {
return fontFamily;
}
protected void fillFamiliesList(ArrayList<String> families) {
}
}

View file

@ -49,10 +49,6 @@ abstract public class ZLPaintContext {
private boolean myFontIsItalic;
private boolean myFontIsUnderlined;
public final void resetFont() {
myResetFont = true;
}
public final void setFont(String family, int size, boolean bold, boolean italic, boolean underline) {
if ((family != null) && !myFontFamily.equals(family)) {
myFontFamily = family;

View file

@ -20,10 +20,10 @@
package org.geometerplus.zlibrary.core.view;
abstract public class ZLView {
public final ZLPaintContext Context;
protected ZLPaintContext myContext = new DummyPaintContext();
public ZLView(ZLPaintContext context) {
Context = context;
public final ZLPaintContext getContext() {
return myContext;
}
public static final int PAGE_CENTRAL = 0;
@ -32,7 +32,7 @@ abstract public class ZLView {
public static final int PAGE_TOP = 3;
public static final int PAGE_BOTTOM = 4;
abstract public void paint(int viewPage);
abstract public void paint(ZLPaintContext context, int viewPage);
abstract public void onScrollingFinished(int viewPage);
public boolean onStylusPress(int x, int y) {

View file

@ -52,8 +52,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
private final HashMap<ZLTextLineInfo,ZLTextLineInfo> myLineInfoCache = new HashMap<ZLTextLineInfo,ZLTextLineInfo>();
public ZLTextView(ZLPaintContext context) {
super(context);
public ZLTextView() {
mySelectionModel = new ZLTextSelectionModel(this);
}
@ -250,8 +249,10 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
}
public synchronized void paint(int viewPage) {
Context.clear(getBackgroundColor());
@Override
public synchronized void paint(ZLPaintContext context, int viewPage) {
myContext = context;
context.clear(getBackgroundColor());
if ((myModel == null) || (myModel.getParagraphsNumber() == 0)) {
return;
@ -314,7 +315,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
final ZLTextHyperlinkArea hyperlinkArea = getCurrentHyperlinkArea(page);
if (hyperlinkArea != null) {
hyperlinkArea.draw(Context);
hyperlinkArea.draw(context);
}
}
@ -415,7 +416,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
float charsPerLine = Math.min(effectiveWidth / charWidth,
charsPerParagraph * 1.2f);
final int strHeight = getWordHeight() + Context.getDescent();
final int strHeight = getWordHeight() + myContext.getDescent();
final int effectiveHeight = (int) (textHeight - (getTextStyle().getSpaceBefore()
+ getTextStyle().getSpaceAfter()) / charsPerParagraph);
final int linesPerPage = effectiveHeight / strHeight;
@ -497,7 +498,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
private final float computeCharWidth(char[] pattern, int length) {
return Context.getStringWidth(pattern, 0, length) / ((float) length);
return myContext.getStringWidth(pattern, 0, length) / ((float) length);
}
public final synchronized int computePageNumber() {
@ -573,7 +574,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
private static final char[] SPACE = new char[] { ' ' };
private void drawTextLine(ZLTextPage page, ZLTextLineInfo info, int from, int to, int y) {
final ZLTextParagraphCursor paragraph = info.ParagraphCursor;
final ZLPaintContext context = Context;
final ZLPaintContext context = myContext;
if ((page == myCurrentPage) && !mySelectionModel.isEmpty() && (from != to)) {
final int paragraphIndex = paragraph.Index;
@ -709,7 +710,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
private ZLTextLineInfo processTextLine(ZLTextParagraphCursor paragraphCursor,
final int startIndex, final int startCharIndex, final int endIndex) {
final ZLPaintContext context = Context;
final ZLPaintContext context = myContext;
final ZLTextLineInfo info = new ZLTextLineInfo(paragraphCursor, startIndex, startCharIndex, getTextStyle());
final ZLTextLineInfo cachedInfo = myLineInfoCache.get(info);
if (cachedInfo != null) {
@ -831,7 +832,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
final ZLTextWord word = (ZLTextWord)element;
newWidth -= getWordWidth(word, currentCharIndex);
int spaceLeft = maxWidth - newWidth;
if ((word.Length > 3) && (spaceLeft > 2 * Context.getSpaceWidth())) {
if ((word.Length > 3) && (spaceLeft > 2 * context.getSpaceWidth())) {
ZLTextHyphenationInfo hyphenationInfo = ZLTextHyphenator.Instance().getInfo(word);
int hyphenationPosition = word.Length - 1;
int subwordWidth = 0;
@ -887,7 +888,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
private void prepareTextLine(ZLTextPage page, ZLTextLineInfo info, int y) {
y = Math.min(y + info.Height, getBottomLine());
final ZLPaintContext context = Context;
final ZLPaintContext context = myContext;
final ZLTextParagraphCursor paragraphCursor = info.ParagraphCursor;
setTextStyle(info.StartStyle);

View file

@ -30,15 +30,14 @@ abstract class ZLTextViewBase extends ZLView {
private ZLTextStyle myTextStyle;
private int myWordHeight = -1;
ZLTextViewBase(ZLPaintContext context) {
super(context);
ZLTextViewBase() {
resetTextStyle();
}
final int getWordHeight() {
if (myWordHeight == -1) {
final ZLTextStyle textStyle = myTextStyle;
myWordHeight = (int)(Context.getStringHeight() * textStyle.getLineSpacePercent() / 100) + textStyle.getVerticalShift();
myWordHeight = (int)(myContext.getStringHeight() * textStyle.getLineSpacePercent() / 100) + textStyle.getVerticalShift();
}
return myWordHeight;
}
@ -54,19 +53,19 @@ abstract class ZLTextViewBase extends ZLView {
public abstract ZLColor getHighlightingColor();
int getTextAreaHeight() {
return Context.getHeight() - getTopMargin() - getBottomMargin();
return myContext.getHeight() - getTopMargin() - getBottomMargin();
}
int getTextAreaWidth() {
return Context.getWidth() - getLeftMargin() - getRightMargin();
return myContext.getWidth() - getLeftMargin() - getRightMargin();
}
int getBottomLine() {
return Context.getHeight() - getBottomMargin() - 1;
return myContext.getHeight() - getBottomMargin() - 1;
}
int getRightLine() {
return Context.getWidth() - getRightMargin() - 1;
return myContext.getWidth() - getRightMargin() - 1;
}
final ZLTextStyle getTextStyle() {
@ -78,7 +77,7 @@ abstract class ZLTextViewBase extends ZLView {
myTextStyle = style;
myWordHeight = -1;
}
Context.setFont(style.getFontFamily(), style.getFontSize(), style.isBold(), style.isItalic(), style.isUnderline());
myContext.setFont(style.getFontFamily(), style.getFontSize(), style.isBold(), style.isItalic(), style.isUnderline());
}
final void resetTextStyle() {
@ -111,11 +110,11 @@ abstract class ZLTextViewBase extends ZLView {
if (element instanceof ZLTextWord) {
return getWordWidth((ZLTextWord)element, charIndex);
} else if (element instanceof ZLTextImageElement) {
return Context.imageWidth(((ZLTextImageElement)element).ImageData);
return myContext.imageWidth(((ZLTextImageElement)element).ImageData);
} else if (element == ZLTextElement.IndentElement) {
return myTextStyle.getFirstLineIndentDelta();
} else if (element instanceof ZLTextFixedHSpaceElement) {
return Context.getSpaceWidth() * ((ZLTextFixedHSpaceElement)element).Length;
return myContext.getSpaceWidth() * ((ZLTextFixedHSpaceElement)element).Length;
}
return 0;
}
@ -124,26 +123,25 @@ abstract class ZLTextViewBase extends ZLView {
if (element instanceof ZLTextWord) {
return getWordHeight();
} else if (element instanceof ZLTextImageElement) {
final ZLPaintContext context = Context;
return context.imageHeight(((ZLTextImageElement)element).ImageData) +
Math.max(context.getStringHeight() * (myTextStyle.getLineSpacePercent() - 100) / 100, 3);
return myContext.imageHeight(((ZLTextImageElement)element).ImageData) +
Math.max(myContext.getStringHeight() * (myTextStyle.getLineSpacePercent() - 100) / 100, 3);
}
return 0;
}
final int getElementDescent(ZLTextElement element) {
return (element instanceof ZLTextWord) ? Context.getDescent() : 0;
return (element instanceof ZLTextWord) ? myContext.getDescent() : 0;
}
final int getWordWidth(ZLTextWord word, int start) {
return
(start == 0) ?
word.getWidth(Context) :
Context.getStringWidth(word.Data, word.Offset + start, word.Length - start);
word.getWidth(myContext) :
myContext.getStringWidth(word.Data, word.Offset + start, word.Length - start);
}
final int getWordWidth(ZLTextWord word, int start, int length) {
return Context.getStringWidth(word.Data, word.Offset + start, length);
return myContext.getStringWidth(word.Data, word.Offset + start, length);
}
private char[] myWordPartArray = new char[20];
@ -151,12 +149,12 @@ abstract class ZLTextViewBase extends ZLView {
final int getWordWidth(ZLTextWord word, int start, int length, boolean addHyphenationSign) {
if (length == -1) {
if (start == 0) {
return word.getWidth(Context);
return word.getWidth(myContext);
}
length = word.Length - start;
}
if (!addHyphenationSign) {
return Context.getStringWidth(word.Data, word.Offset + start, length);
return myContext.getStringWidth(word.Data, word.Offset + start, length);
}
char[] part = myWordPartArray;
if (length + 1 > part.length) {
@ -165,7 +163,7 @@ abstract class ZLTextViewBase extends ZLView {
}
System.arraycopy(word.Data, word.Offset + start, part, 0, length);
part[length] = '-';
return Context.getStringWidth(part, 0, length + 1);
return myContext.getStringWidth(part, 0, length + 1);
}
int getAreaLength(ZLTextParagraphCursor paragraph, ZLTextElementArea area, int toCharIndex) {
@ -184,7 +182,7 @@ abstract class ZLTextViewBase extends ZLView {
}
final void drawWord(int x, int y, ZLTextWord word, int start, int length, boolean addHyphenationSign) {
final ZLPaintContext context = Context;
final ZLPaintContext context = myContext;
context.setTextColor(getTextColor(myTextStyle.Hyperlink.Type));
if ((start == 0) && (length == -1)) {
drawString(x, y, word.Data, word.Offset, word.Length, word.getMark(), 0);
@ -208,7 +206,7 @@ abstract class ZLTextViewBase extends ZLView {
}
private final void drawString(int x, int y, char[] str, int offset, int length, ZLTextWord.Mark mark, int shift) {
final ZLPaintContext context = Context;
final ZLPaintContext context = myContext;
context.setTextColor(getTextColor(myTextStyle.Hyperlink.Type));
if (mark == null) {
context.drawString(x, y, str, offset, length);

View file

@ -65,10 +65,6 @@ public final class ZLAndroidLibrary extends ZLibrary {
}
}
public ZLAndroidPaintContext getPaintContext() {
return getWidget().getPaintContext();
}
public ZLAndroidWidget getWidget() {
if (myWidget == null) {
myWidget = (ZLAndroidWidget)myActivity.findViewById(R.id.main_view);

View file

@ -32,28 +32,24 @@ import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
import org.geometerplus.zlibrary.ui.android.util.ZLAndroidColorUtil;
public final class ZLAndroidPaintContext extends ZLPaintContext {
private Canvas myCanvas;
private final Canvas myCanvas;
private final Paint myTextPaint = new Paint();
private final Paint myLinePaint = new Paint();
private final Paint myFillPaint = new Paint();
private final Paint myOutlinePaint = new Paint();
private int myWidth;
private int myHeight;
private int myScrollbarWidth;
static ZLAndroidPaintContext Instance() {
if (ourInstance == null) {
ourInstance = new ZLAndroidPaintContext();
}
return ourInstance;
}
private static ZLAndroidPaintContext ourInstance;
private final int myWidth;
private final int myHeight;
private final int myScrollbarWidth;
private HashMap<String,Typeface[]> myTypefaces = new HashMap<String,Typeface[]>();
private ZLAndroidPaintContext() {
ZLAndroidPaintContext(Canvas canvas, int width, int height, int scrollbarWidth) {
myCanvas = canvas;
myWidth = width - scrollbarWidth;
myHeight = height;
myScrollbarWidth = scrollbarWidth;
myTextPaint.setLinearText(false);
myTextPaint.setAntiAlias(true);
myTextPaint.setSubpixelText(false);
@ -67,21 +63,6 @@ public final class ZLAndroidPaintContext extends ZLPaintContext {
myOutlinePaint.setMaskFilter(new EmbossMaskFilter(new float[] {1, 1, 1}, .4f, 6f, 3.5f));
}
void setSize(int width, int height, int scrollbarWidth) {
myWidth = width - scrollbarWidth;
myHeight = height;
myScrollbarWidth = scrollbarWidth;
}
void beginPaint(Canvas canvas) {
myCanvas = canvas;
resetFont();
}
void endPaint() {
myCanvas = null;
}
public void clear(ZLColor color) {
myFillPaint.setColor(ZLAndroidColorUtil.rgb(color));
myCanvas.drawRect(0, 0, myWidth + myScrollbarWidth, myHeight, myFillPaint);

View file

@ -55,10 +55,6 @@ public class ZLAndroidWidget extends View {
setDrawingCacheEnabled(false);
}
public ZLAndroidPaintContext getPaintContext() {
return ZLAndroidPaintContext.Instance();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (myScreenIsTouched) {
@ -265,16 +261,13 @@ public class ZLAndroidWidget extends View {
mySecondaryBitmapIsUpToDate = true;
}
final int w = getWidth();
final int h = getHeight();
final ZLAndroidPaintContext context = ZLAndroidPaintContext.Instance();
Canvas canvas = new Canvas(bitmap);
context.beginPaint(canvas);
final int scrollbarWidth = view.isScrollbarShown() ? getVerticalScrollbarWidth() : 0;
context.setSize(w, h, scrollbarWidth);
view.paint((bitmap == myMainBitmap) ? ZLView.PAGE_CENTRAL : myViewPageToScroll);
context.endPaint();
final ZLAndroidPaintContext context = new ZLAndroidPaintContext(
new Canvas(bitmap),
getWidth(),
getHeight(),
view.isScrollbarShown() ? getVerticalScrollbarWidth() : 0
);
view.paint(context, (bitmap == myMainBitmap) ? ZLView.PAGE_CENTRAL : myViewPageToScroll);
}
private void onDrawStatic(Canvas canvas) {