diff options
author | Paul Gardiner <paulg.artifex@glidos.net> | 2013-02-18 15:06:44 +0000 |
---|---|---|
committer | Paul Gardiner <paulg.artifex@glidos.net> | 2013-02-22 12:21:23 +0000 |
commit | f72c3f422ab2790fd55a056e5bbdbfdb388c0811 (patch) | |
tree | b558588a4227f1b84ebcc925660a8dc784da6bfd /android/src/com/artifex/mupdfdemo | |
parent | e4b05d70f8d82b478c5803ad52fb77875e4c9b4f (diff) | |
download | mupdf-f72c3f422ab2790fd55a056e5bbdbfdb388c0811.tar.xz |
Android: move selected text operations from PageView to MuPDFPageView
Diffstat (limited to 'android/src/com/artifex/mupdfdemo')
-rw-r--r-- | android/src/com/artifex/mupdfdemo/MuPDFPageView.java | 121 | ||||
-rw-r--r-- | android/src/com/artifex/mupdfdemo/PageView.java | 138 |
2 files changed, 140 insertions, 119 deletions
diff --git a/android/src/com/artifex/mupdfdemo/MuPDFPageView.java b/android/src/com/artifex/mupdfdemo/MuPDFPageView.java index cf5e1af5..0c47c327 100644 --- a/android/src/com/artifex/mupdfdemo/MuPDFPageView.java +++ b/android/src/com/artifex/mupdfdemo/MuPDFPageView.java @@ -1,6 +1,9 @@ package com.artifex.mupdfdemo; +import java.util.ArrayList; + import android.app.AlertDialog; +import android.content.ClipData; import android.content.Context; import android.content.DialogInterface; import android.graphics.Bitmap; @@ -56,6 +59,8 @@ class PassClickResultChoice extends PassClickResult { } public class MuPDFPageView extends PageView implements MuPDFView { + private static final float LINE_THICKNESS = 0.07f; + private static final float STRIKE_HEIGHT = 0.375f; private final MuPDFCore mCore; private AsyncTask<Void,Void,PassClickResult> mPassClick; private RectF mWidgetAreas[]; @@ -66,6 +71,7 @@ public class MuPDFPageView extends PageView implements MuPDFView { private EditText mEditText; private AsyncTask<String,Void,Boolean> mSetWidgetText; private AsyncTask<String,Void,Void> mSetWidgetChoice; + private AsyncTask<RectF[],Void,Void> mAddStrikeOut; private Runnable changeReporter; public MuPDFPageView(Context c, MuPDFCore core, Point parentSize) { @@ -200,6 +206,91 @@ public class MuPDFPageView extends PageView implements MuPDFView { return hitWidget; } + public boolean copySelection() { + final StringBuilder text = new StringBuilder(); + + processSelectedText(new TextProcessor() { + StringBuilder line; + + public void onStartLine() { + line = new StringBuilder(); + } + + public void onWord(TextWord word) { + if (line.length() > 0) + line.append(' '); + line.append(word.w); + } + + public void onEndLine() { + if (text.length() > 0) + text.append('\n'); + text.append(line); + } + }); + + if (text.length() == 0) + return false; + + int currentApiVersion = android.os.Build.VERSION.SDK_INT; + if (currentApiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { + android.content.ClipboardManager cm = (android.content.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE); + + cm.setPrimaryClip(ClipData.newPlainText("MuPDF", text)); + } else { + android.text.ClipboardManager cm = (android.text.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE); + cm.setText(text); + } + + deselectText(); + + return true; + } + + public void strikeOutSelection() { + final ArrayList<RectF> lines = new ArrayList<RectF>(); + processSelectedText(new TextProcessor() { + RectF rect; + + public void onStartLine() { + rect = new RectF(); + } + + public void onWord(TextWord word) { + rect.union(word); + } + + public void onEndLine() { + if (!rect.isEmpty()) { + // These are vertical lines so we can specify + // both position and thickness with a RectF + float vcenter = rect.bottom - (rect.bottom - rect.top)*STRIKE_HEIGHT; + float thickness = (rect.bottom - rect.top)*LINE_THICKNESS; + rect.top = vcenter - thickness/2; + rect.bottom = vcenter + thickness/2; + lines.add(rect); + } + } + }); + + mAddStrikeOut = new AsyncTask<RectF[],Void,Void>() { + @Override + protected Void doInBackground(RectF[]... params) { + addStrikeOut(params[0]); + return null; + } + + @Override + protected void onPostExecute(Void result) { + update(); + } + }; + + mAddStrikeOut.execute(lines.toArray(new RectF[lines.size()])); + + deselectText(); + } + @Override protected Bitmap drawPage(int sizeX, int sizeY, int patchX, int patchY, int patchWidth, int patchHeight) { @@ -250,4 +341,34 @@ public class MuPDFPageView extends PageView implements MuPDFView { // This type of view scales automatically to fit the size // determined by the parent view groups during layout } + + @Override + public void releaseResources() { + if (mPassClick != null) { + mPassClick.cancel(true); + mPassClick = null; + } + + if (mLoadWidgetAreas != null) { + mLoadWidgetAreas.cancel(true); + mLoadWidgetAreas = null; + } + + if (mSetWidgetText != null) { + mSetWidgetText.cancel(true); + mSetWidgetText = null; + } + + if (mSetWidgetChoice != null) { + mSetWidgetChoice.cancel(true); + mSetWidgetChoice = null; + } + + if (mAddStrikeOut != null) { + mAddStrikeOut.cancel(true); + mAddStrikeOut = null; + } + + super.releaseResources(); + } } diff --git a/android/src/com/artifex/mupdfdemo/PageView.java b/android/src/com/artifex/mupdfdemo/PageView.java index 17673493..ccc0e4fb 100644 --- a/android/src/com/artifex/mupdfdemo/PageView.java +++ b/android/src/com/artifex/mupdfdemo/PageView.java @@ -3,7 +3,6 @@ package com.artifex.mupdfdemo; import java.util.ArrayList; import java.util.Iterator; -import android.content.ClipData; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -47,7 +46,13 @@ class OpaqueImageView extends ImageView { } } -abstract class TextSelector { +interface TextProcessor { + void onStartLine(); + void onWord(TextWord word); + void onEndLine(); +} + +class TextSelector { final private TextWord[][] mText; final private RectF mSelectBox; @@ -56,11 +61,7 @@ abstract class TextSelector { mSelectBox = selectBox; } - protected abstract void onStartLine(); - protected abstract void onWord(TextWord word); - protected abstract void onEndLine(); - - public void select() { + public void select(TextProcessor tp) { if (mText == null || mSelectBox == null) return; @@ -86,13 +87,13 @@ abstract class TextSelector { end = mSelectBox.right; } - onStartLine(); + tp.onStartLine(); for (TextWord word : line) if (word.right > start && word.left < end) - onWord(word); + tp.onWord(word); - onEndLine(); + tp.onEndLine(); } } } @@ -102,9 +103,7 @@ public abstract class PageView extends ViewGroup { private static final int LINK_COLOR = 0x80AC7225; private static final int BACKGROUND_COLOR = 0xFFFFFFFF; private static final int PROGRESS_DIALOG_DELAY = 200; - private static final float LINE_THICKNESS = 0.07f; - private static final float STRIKE_HEIGHT = 0.375f; - private final Context mContext; + protected final Context mContext; protected int mPageNumber; private Point mParentSize; protected Point mSize; // Size of page at minimum zoom @@ -113,7 +112,6 @@ public abstract class PageView extends ViewGroup { private ImageView mEntire; // Image rendered at minimum zoom private BitmapHolder mEntireBmh; private AsyncTask<Void,Void,TextWord[][]> mGetText; - private AsyncTask<RectF[],Void,Void> mAddStrikeOut; private AsyncTask<Void,Void,LinkInfo[]> mGetLinkInfo; private AsyncTask<Void,Void,Bitmap> mDrawEntire; @@ -322,27 +320,22 @@ public abstract class PageView extends ViewGroup { if (mSelectBox != null && mText != null) { paint.setColor(HIGHLIGHT_COLOR); - TextSelector sel = new TextSelector(mText, mSelectBox) { + processSelectedText(new TextProcessor() { RectF rect; - @Override - protected void onStartLine() { + public void onStartLine() { rect = new RectF(); } - @Override - protected void onWord(TextWord word) { + public void onWord(TextWord word) { rect.union(word); } - @Override - protected void onEndLine() { + public void onEndLine() { if (!rect.isEmpty()) canvas.drawRect(rect.left*scale, rect.top*scale, rect.right*scale, rect.bottom*scale, paint); } - }; - - sel.select(); + }); } } }; @@ -400,101 +393,8 @@ public abstract class PageView extends ViewGroup { } } - public boolean copySelection() { - final StringBuilder text = new StringBuilder(); - - TextSelector sel = new TextSelector(mText, mSelectBox) { - StringBuilder line; - - @Override - protected void onStartLine() { - line = new StringBuilder(); - } - - @Override - protected void onWord(TextWord word) { - if (line.length() > 0) - line.append(' '); - line.append(word.w); - } - - @Override - protected void onEndLine() { - if (text.length() > 0) - text.append('\n'); - text.append(line); - } - }; - - sel.select(); - - if (text.length() == 0) - return false; - - int currentApiVersion = android.os.Build.VERSION.SDK_INT; - if (currentApiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { - android.content.ClipboardManager cm = (android.content.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE); - - cm.setPrimaryClip(ClipData.newPlainText("MuPDF", text)); - } else { - android.text.ClipboardManager cm = (android.text.ClipboardManager)mContext.getSystemService(Context.CLIPBOARD_SERVICE); - cm.setText(text); - } - - mSelectBox = null; - mSearchView.invalidate(); - - return true; - } - - public void strikeOutSelection() { - final ArrayList<RectF> lines = new ArrayList<RectF>(); - TextSelector sel = new TextSelector(mText, mSelectBox) { - RectF rect; - - @Override - protected void onStartLine() { - rect = new RectF(); - } - - @Override - protected void onWord(TextWord word) { - rect.union(word); - } - - @Override - protected void onEndLine() { - if (!rect.isEmpty()) { - // These are vertical lines so we can specify - // both position and thickness with a RectF - float vcenter = rect.bottom - (rect.bottom - rect.top)*STRIKE_HEIGHT; - float thickness = (rect.bottom - rect.top)*LINE_THICKNESS; - rect.top = vcenter - thickness/2; - rect.bottom = vcenter + thickness/2; - lines.add(rect); - } - } - }; - - sel.select(); - - mAddStrikeOut = new AsyncTask<RectF[],Void,Void>() { - @Override - protected Void doInBackground(RectF[]... params) { - addStrikeOut(params[0]); - return null; - } - - @Override - protected void onPostExecute(Void result) { - update(); - } - }; - - mAddStrikeOut.execute(lines.toArray(new RectF[lines.size()])); - - mSelectBox = null; - mSearchView.invalidate(); + protected void processSelectedText(TextProcessor tp) { + (new TextSelector(mText, mSelectBox)).select(tp); } @Override |