Skip to content

Commit

Permalink
Merge branch 'master' into GH-173.printing.popup.annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Corless committed Apr 13, 2023
2 parents 5e487c0 + 7e1721a commit 3090bd2
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 43 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ ICEpdf is an open source project and is always looking for more contributors. T
<dependency>
<groupId>com.github.pcorless.icepdf</groupId>
<artifactId>icepdf-core</artifactId>
<version>7.0.0</version>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>com.github.pcorless.icepdf</groupId>
<artifactId>icepdf-viewer</artifactId>
<version>7.0.0</version>
<version>7.0.2</version>
</dependency>
```

Expand Down Expand Up @@ -170,14 +170,14 @@ window.setVisible(true);
controller.openDocument(filePath);
```

Make sure to take a look at the [Wiki](https://github.com/pcorless/icepdf/wiki/Examples) for more examples of extracting content.
Make sure to take a look at the [Wiki](https://github.com/pcorless/icepdf/wiki/Usage-Examples)) for more examples of extracting content.

## Learning

### Examples

There are bunch of examples located in the root of the project grouped by common usage scenarios. Similarly the
Wiki contains [example](https://github.com/pcorless/icepdf/wiki/Examples) information.
Wiki contains [example](https://github.com/pcorless/icepdf/wiki/Usage-Examples) information.

### API Documentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ public StringBuilder getLiteralStringBuffer(final int fontFormat, FontFile font)
charValue = getUnsignedInt(i - lastIndex, offset);
// 0 cid is valid, so we have ot be careful we don't exclude the
// cid 00 = 0 or 0000 = 0, not 0000 = 00.
if (!(offset < length && charValue == 0) &&
font.canDisplay((char) charValue)) {
// removed font check as it was causing problems with a lot of Latin based hex strings
// may need to revisit in the future when getting back to multibyte encodings.
if (!(offset < length && charValue == 0)) {
tmp.append((char) charValue);
lastIndex = 0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.icepdf.core.pobjects.*;
import org.icepdf.core.util.Library;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -222,22 +223,23 @@ public boolean isEmbeddedFontDamaged() {
}

/**
* Gets the fonts bounding box.
* Gets the fonts bounding box using the raw PRectangle values no conversion
* to Java2D coordinates are made.
*
* @return bounding box in PDF coordinate space.
*/
public PRectangle getFontBBox() {
public Rectangle2D getFontBBox() {
Object value = library.getObject(entries, FONT_BBOX);
if (value instanceof List) {
List rectangle = (List) value;
return new PRectangle(rectangle);
return new PRectangle(rectangle).getOriginalPoints();
} else if (value instanceof int[]) {
int[] ints = (int[]) value;
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints) {
intList.add(i);
}
return new PRectangle(intList);
return new PRectangle(intList).getOriginalPoints();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class ZFontTrueType extends ZSimpleFont implements Cloneable {

private HorizontalMetricsTable horizontalMetricsTable;

private HeaderTable headerTable;

protected TrueTypeFont trueTypeFont;

protected ZFontTrueType() {
Expand All @@ -60,6 +62,7 @@ public ZFontTrueType(byte[] fontBytes) throws Exception {

extractCmapTable();
extractMetricsTable();
extractHeadTable();
}
} catch (Throwable e) {
logger.log(Level.FINE, "Error reading font file with", e);
Expand All @@ -75,6 +78,7 @@ protected ZFontTrueType(ZFontTrueType font) {
this.cmapWinSymbol = font.cmapWinSymbol;
this.cmapMacRoman = font.cmapMacRoman;
this.horizontalMetricsTable = font.horizontalMetricsTable;
this.headerTable = font.headerTable;
this.fontMatrix = convertFontMatrix(fontBoxFont);
font.missingWidth = this.missingWidth;
}
Expand Down Expand Up @@ -184,11 +188,12 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.firstCh = firstCh;
font.ascent = ascent;
font.descent = descent;
// go with the PDF define bounds if we have width
if (widths != null && widths.length > 0) {
font.widths = widths;
font.bbox = bbox;
}
font.cMap = diff;
font.bbox = bbox;
font.maxCharBounds = null;
return font;
}
Expand Down Expand Up @@ -348,6 +353,21 @@ protected void extractMetricsTable() throws IOException {
horizontalMetricsTable = trueTypeFont.getHorizontalMetrics();
}

protected void extractHeadTable() throws IOException {
headerTable = trueTypeFont.getHeader();
calculateFontBbox();
}

private void calculateFontBbox(){
if (headerTable != null) {
Rectangle2D bbox = new Rectangle2D.Float(
headerTable.getXMin(), headerTable.getYMin(), headerTable.getXMax(), headerTable.getYMax());
if (bbox.getWidth() > 0 && bbox.getHeight() > 0) {
this.bbox = bbox;
}
}
}

protected void extractCmapTable() throws IOException {
CmapTable cmapTable = trueTypeFont.getCmap();
if (cmapTable != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.icepdf.core.pobjects.fonts.zfont.fontFiles;

import org.apache.fontbox.type1.Type1Font;
import org.apache.fontbox.util.BoundingBox;
import org.icepdf.core.pobjects.Name;
import org.icepdf.core.pobjects.Stream;
import org.icepdf.core.pobjects.fonts.CMap;
Expand Down Expand Up @@ -52,6 +53,7 @@ public ZFontType1(Stream fontStream) throws Exception {
}
}
fontBoxFont = type1Font;
calculateFontBbox();
} catch (Throwable e) {
logger.log(Level.FINE, "Error reading font file with ", e);
throw new Exception(e);
Expand All @@ -62,6 +64,7 @@ public ZFontType1(URL url) throws IOException {
byte[] fontBytes = url.openStream().readAllBytes();
source = url;
type1Font = Type1Font.createWithPFB(fontBytes);
calculateFontBbox();
}

private ZFontType1(ZFontType1 font) {
Expand Down Expand Up @@ -129,7 +132,9 @@ public FontFile deriveFont(float[] widths, int firstCh, float missingWidth, floa
font.widths = widths;
}
font.cMap = diff != null ? diff : font.cMap;
font.bbox = bbox;
if (font.bbox == null) {
font.bbox = bbox;
}
return font;
}

Expand Down Expand Up @@ -166,6 +171,15 @@ public String getName() {
return type1Font.getName();
}

private void calculateFontBbox() {
BoundingBox fontBBox = type1Font.getFontBBox();
if (fontBBox.getWidth() > 0 && fontBBox.getHeight() > 0) {
bbox = new Rectangle2D.Double(
fontBBox.getLowerLeftX(), fontBBox.getLowerLeftY(),
fontBBox.getUpperRightX(), fontBBox.getUpperRightY());
}
}

/**
* Some Type 1 fonts have an invalid Length2, see PDFBOX-3475. A negative /Length2 brings an
* IllegalArgumentException in Arrays.copyOfRange(), a huge value eats up memory because of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ protected ZSimpleFont(ZSimpleFont font) {
this.gsTransform = new AffineTransform(gsTransform);
this.fontMatrix = new AffineTransform(font.fontMatrix);
this.fontTransform = new AffineTransform(font.fontTransform);
Rectangle2D maxCharBounds = font.maxCharBounds;
if (maxCharBounds != null) {
this.maxCharBounds = new Rectangle2D.Double(
maxCharBounds.getX(), maxCharBounds.getY(), maxCharBounds.getWidth(), maxCharBounds.getHeight());
}
}

@Override
Expand Down Expand Up @@ -187,10 +192,22 @@ public Shape getOutline(String estr, float x, float y) {

@Override
public Rectangle2D getMaxCharBounds() {
AffineTransform af = new AffineTransform();
af.scale(size, -size);
af.concatenate(fontMatrix);
return af.createTransformedShape(bbox).getBounds2D();
// bbox isn't a proper rectangle but p1x, p1y, p2x, p2y
double[] bboxPrimitives = new double[]{
bbox.getX() * size, bbox.getY() * size, bbox.getWidth() * size, bbox.getHeight() * size};
// transform the two points to the correct space
fontMatrix.deltaTransform(bboxPrimitives, 0, bboxPrimitives, 0, 2);
// flip if needed
if (bboxPrimitives[3] < 0.0) {
bboxPrimitives[1] = -bboxPrimitives[1];
bboxPrimitives[3] = -bboxPrimitives[3];
}
// convert ot a proper java2d rectangle
return new Rectangle2D.Double(
bboxPrimitives[0],
-bboxPrimitives[3],
bboxPrimitives[2] - bboxPrimitives[0],
bboxPrimitives[3] - bboxPrimitives[1]);
}

@Override
Expand Down Expand Up @@ -326,14 +343,15 @@ protected void setFontTransform(AffineTransform at) {
fontTransform = new AffineTransform(fontMatrix);
fontTransform.concatenate(at);
fontTransform.scale(size, -size);
maxCharBounds = getMaxCharBounds();
}

protected void setPointSize(float pointSize) {
fontTransform = new AffineTransform(fontMatrix);
fontTransform.concatenate(gsTransform);
fontTransform.scale(pointSize, -pointSize);
size = pointSize;
maxCharBounds = null;
maxCharBounds = getMaxCharBounds();
}

protected char getCharDiff(char character) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ private byte[] ccittFaxDecodeTwelveMonkeys(byte[] streamData, int k, Boolean enc
}

Class<?> tmDecoder = Class.forName("com.twelvemonkeys.imageio.plugins.tiff.CCITTFaxDecoderStream");
Constructor tmDecoderConst = tmDecoder.getConstructor(
InputStream.class, int.class, int.class, int.class, long.class, boolean.class);
Constructor<?> tmDecoderConst = tmDecoder.getConstructor(
InputStream.class, int.class, int.class, long.class, boolean.class);
tmDecoderConst.setAccessible(true);

ByteArrayInputStream bis = new ByteArrayInputStream(streamData);
InputStream decoderStream = (InputStream) tmDecoderConst.newInstance(
bis, columns, compression, 1, options, encodedByteAlign);
bis, columns, compression, options, encodedByteAlign);

DataInputStream dis = new DataInputStream(decoderStream);
dis.readFully(decodedStreamData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2963,9 +2963,8 @@ public void commonNewDocumentHandling(String fileDescription) {
}
// make sure we don't keep Attachments view around from a previous load
// as we don't want to use it for a none attachments PDF file.
if (documentViewController.getViewMode() ==
DocumentViewControllerImpl.USE_ATTACHMENTS_VIEW) {
documentViewController.revertViewType();
else if (documentViewController.getViewMode() == DocumentViewControllerImpl.USE_ATTACHMENTS_VIEW) {
documentViewController.setViewType(DocumentViewControllerImpl.ONE_COLUMN_VIEW);
}
// check to see if we have collection
if (isPdfCollection()) {
Expand Down Expand Up @@ -3220,7 +3219,7 @@ public void closeDocument() {
annotationSummaryFrame.dispose();
}

// set the default cursor.
// set the default cursor.
documentViewController.closeDocument();

// clear search controller caches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected void resize() {

/**
* Refreshes the components bounds for the current page transformation.
* Bounds have are already in user space.
* Bounds are already in user space.
*/
public void refreshDirtyBounds() {
Page currentPage = pageViewComponent.getPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ public void focusGained(FocusEvent e) {
super.focusGained(e);
}

@Override
public abstract void validate();

@Override
public void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void validate() {
}
comboBoxList.setFont(new Font(fontName, Font.PLAIN,
(int) (choiceFieldDictionary.getSize() * documentViewModel.getViewZoom())));
super.validate();
}

public void propertyChange(PropertyChangeEvent evt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void validate() {
}
choiceList.setFont(new Font(fontName, Font.PLAIN,
(int) (choiceFieldDictionary.getSize() * documentViewModel.getViewZoom())));
super.validate();
}

@Override
Expand Down
Loading

0 comments on commit 3090bd2

Please sign in to comment.