diff options
Diffstat (limited to 'platform/java/src/com')
-rw-r--r-- | platform/java/src/com/artifex/mupdf/fitz/StructuredText.java | 60 | ||||
-rw-r--r-- | platform/java/src/com/artifex/mupdf/fitz/StructuredTextWalker.java | 11 |
2 files changed, 70 insertions, 1 deletions
diff --git a/platform/java/src/com/artifex/mupdf/fitz/StructuredText.java b/platform/java/src/com/artifex/mupdf/fitz/StructuredText.java index 82aa55ea..19b85335 100644 --- a/platform/java/src/com/artifex/mupdf/fitz/StructuredText.java +++ b/platform/java/src/com/artifex/mupdf/fitz/StructuredText.java @@ -1,5 +1,7 @@ package com.artifex.mupdf.fitz; +import java.util.ArrayList; + public class StructuredText { static { @@ -23,7 +25,63 @@ public class StructuredText public native Quad[] highlight(Point a, Point b); public native String copy(Point a, Point b); - public native TextBlock[] getBlocks(); + public native void walk(StructuredTextWalker walker); + + public TextBlock[] getBlocks() { + BlockWalker walker = new BlockWalker(); + walk(walker); + return walker.getBlocks(); + } + + class BlockWalker implements StructuredTextWalker { + ArrayList<TextBlock> blocks; + ArrayList<TextLine> lines; + ArrayList<TextChar> chrs; + Rect lineBbox; + Rect blockBbox; + + BlockWalker() { + blocks = new ArrayList<TextBlock>(); + } + + public void onImageBlock(Rect bbox, Matrix transform, Image image) { + } + + public void beginTextBlock(Rect bbox) { + lines = new ArrayList<TextLine>(); + blockBbox = bbox; + } + + public void endTextBlock() { + TextBlock block = new TextBlock(); + block.bbox = blockBbox; + block.lines = (TextLine[]) lines.toArray(new TextLine[0]); + blocks.add(block); + } + + public void beginLine(Rect bbox, int wmode) { + chrs = new ArrayList<TextChar>(); + lineBbox = bbox; + } + + public void endLine() { + TextLine line = new TextLine(); + line.bbox = lineBbox; + line.chars = chrs.toArray(new TextChar[0]); + lines.add(line); + } + + public void onChar(int c, Point origin, Font font, float size, Quad quad) { + TextChar chr = new TextChar(); + chr.c = c; + chr.quad = quad; + chrs.add(chr); + } + + TextBlock[] getBlocks() { + return (TextBlock[]) blocks.toArray(new TextBlock[0]); + } + } public class TextBlock { public TextLine[] lines; diff --git a/platform/java/src/com/artifex/mupdf/fitz/StructuredTextWalker.java b/platform/java/src/com/artifex/mupdf/fitz/StructuredTextWalker.java new file mode 100644 index 00000000..b38686ca --- /dev/null +++ b/platform/java/src/com/artifex/mupdf/fitz/StructuredTextWalker.java @@ -0,0 +1,11 @@ +package com.artifex.mupdf.fitz; + +public interface StructuredTextWalker +{ + void onImageBlock(Rect bbox, Matrix transform, Image image); + void beginTextBlock(Rect bbox); + void endTextBlock(); + void beginLine(Rect bbox, int wmode); + void endLine(); + void onChar(int c, Point origin, Font font, float size, Quad q); +} |