Skip to content

Commit

Permalink
GH-134 First implementation of fit width
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Tâche committed Jul 25, 2023
1 parent d1cbfb2 commit 4fa328f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.icepdf.ri.common.views;

import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.pobjects.PageTree;
import org.icepdf.core.util.ColorUtil;
import org.icepdf.core.util.Defs;
import org.icepdf.core.util.PropertyConstants;
Expand Down Expand Up @@ -351,6 +353,21 @@ protected boolean isTextSelectionTool() {
getCurrentToolHandler() instanceof HighLightAnnotationHandler);
}

protected PDimension getMaxPageDimension() {
final PDimension dimension = new PDimension(0, 0);
final PageTree pt = documentViewModel.getDocument().getPageTree();
for (int i = 0; i < pt.getNumberOfPages(); ++i) {
final PDimension dim = pt.getPage(i).getSize(0f, 1f);
if (dim.getHeight() > dimension.getHeight()) {
dimension.set((float) dimension.getWidth(), (float) dim.getHeight());
}
if (dim.getWidth() > dimension.getWidth()) {
dimension.set((float) dim.getWidth(), (float) dimension.getHeight());
}
}
return dimension;
}

/**
* Checks to see if the mouse has exited the scroll pane viewport on the vertical plane.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.icepdf.ri.common.views;

import org.icepdf.core.pobjects.PDimension;
import org.icepdf.ri.common.CurrentPageChanger;
import org.icepdf.ri.common.KeyListenerPageColumnChanger;

Expand Down Expand Up @@ -124,19 +125,9 @@ public void dispose() {
}

public Dimension getDocumentSize() {
// still used by page fit code
float pageViewWidth = 0;
float pageViewHeight = 0;
int currCompIndex = documentViewController.getCurrentPageIndex();
Rectangle bounds = documentViewModel.getPageBounds(currCompIndex);
if (bounds != null) {
pageViewWidth = bounds.width;
pageViewHeight = bounds.height;
}
// normalize the dimensions to a zoom level of zero.
float currentZoom = documentViewController.getDocumentViewModel().getViewZoom();
pageViewWidth = Math.abs(pageViewWidth / currentZoom);
pageViewHeight = Math.abs(pageViewHeight / currentZoom);
final PDimension dimension = getMaxPageDimension();
float pageViewWidth = (float) dimension.getWidth();
float pageViewHeight = (float) dimension.getHeight();

// add any horizontal padding from layout manager
pageViewWidth += PAGE_SPACING_HORIZONTAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.icepdf.ri.common.views;

import org.icepdf.core.pobjects.PDimension;
import org.icepdf.ri.common.KeyListenerPageChanger;
import org.icepdf.ri.common.MouseWheelListenerPageChanger;

Expand Down Expand Up @@ -132,18 +133,9 @@ public void dispose() {
}

public Dimension getDocumentSize() {
float pageViewWidth = 0;
float pageViewHeight = 0;
int currCompIndex = documentViewController.getCurrentPageIndex();
Rectangle bounds = documentViewModel.getPageBounds(currCompIndex);
if (bounds != null) {
pageViewWidth = bounds.width;
pageViewHeight = bounds.height;
}
// normalize the dimensions to a zoom level of zero.
float currentZoom = documentViewController.getDocumentViewModel().getViewZoom();
pageViewWidth = Math.abs(pageViewWidth / currentZoom);
pageViewHeight = Math.abs(pageViewHeight / currentZoom);
final PDimension maxDimension = getMaxPageDimension();
float pageViewWidth = (float) maxDimension.getWidth();
float pageViewHeight = (float) maxDimension.getHeight();

// add any horizontal padding from layout manager
pageViewWidth += PAGE_SPACING_HORIZONTAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.icepdf.ri.common.views;

import org.icepdf.core.pobjects.PDimension;
import org.icepdf.ri.common.CurrentPageChanger;
import org.icepdf.ri.common.KeyListenerPageColumnChanger;

Expand Down Expand Up @@ -139,58 +140,9 @@ public void dispose() {
}

public Dimension getDocumentSize() {
float pageViewWidth = 0;
float pageViewHeight = 0;
// The page index and corresponding component index are approximately equal
// If the first page is on the right, then there's a spacer on the left,
// bumping indexes up by one.
int currPageIndex = documentViewController.getCurrentPageIndex();
int currCompIndex = currPageIndex;
int numComponents = getComponentCount();
boolean foundCurrent = false;
while (currCompIndex >= 0 && currCompIndex < numComponents) {
Component comp = getComponent(currCompIndex);
if (comp instanceof PageViewDecorator) {
PageViewDecorator pvd = (PageViewDecorator) comp;
PageViewComponent pvc = pvd.getPageViewComponent();
if (pvc.getPageIndex() == currPageIndex) {
Dimension dim = pvd.getPreferredSize();
pageViewWidth = dim.width;
pageViewHeight = dim.height;
foundCurrent = true;
break;
}
}
currCompIndex++;
}
if (foundCurrent) {
// Determine if the page at (currPageIndex,currCompIndex) was
// on the left or right, so that if there's a page next to
// it, whether it's earlier or later in the component list,
// so we can get it's pageViewHeight and use that for our pageViewHeight
// calculation.
// If the other component is past the ends of the component
// list, or not a PageViewDecorator, then current was either
// the first or last page in the document
boolean evenPageIndex = ((currPageIndex & 0x1) == 0);
boolean bumpedIndex = (currCompIndex != currPageIndex);
boolean onLeft = evenPageIndex ^ bumpedIndex; // XOR
int otherCompIndex = onLeft ? (currCompIndex + 1) : (currCompIndex - 1);
if (otherCompIndex >= 0 && otherCompIndex < numComponents) {
Component comp = getComponent(otherCompIndex);
if (comp instanceof PageViewDecorator) {
PageViewDecorator pvd = (PageViewDecorator) comp;
Dimension dim = pvd.getPreferredSize();
pageViewWidth = dim.width;
pageViewHeight = dim.height;
}
}
}

// normalize the dimensions to a zoom level of zero.
float currentZoom = documentViewController.getDocumentViewModel().getViewZoom();
pageViewWidth = Math.abs(pageViewWidth / currentZoom);
pageViewHeight = Math.abs(pageViewHeight / currentZoom);
final PDimension dimension = getMaxPageDimension();
float pageViewWidth = (float) dimension.getWidth();
float pageViewHeight = (float) dimension.getHeight();

// two pages wide, generalization, pages are usually the same size we
// don't bother to look at the second pages size for the time being.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.icepdf.ri.common.views;

import org.icepdf.core.pobjects.PDimension;
import org.icepdf.ri.common.CurrentPageChanger;
import org.icepdf.ri.common.KeyListenerPageChanger;
import org.icepdf.ri.common.MouseWheelListenerPageChanger;
Expand Down Expand Up @@ -172,27 +173,12 @@ public void dispose() {
}

public Dimension getDocumentSize() {
float pageViewWidth = 0;
float pageViewHeight = 0;
int count = getComponentCount();
Component comp;
for (int i = 0; i < count; i++) {
comp = getComponent(i);
if (comp instanceof PageViewDecorator) {
PageViewDecorator pvd = (PageViewDecorator) comp;
Dimension dim = pvd.getPreferredSize();
pageViewWidth = dim.width;
pageViewHeight = dim.height;
break;
}
}
// normalize the dimensions to a zoom level of zero.
float currentZoom = documentViewController.getDocumentViewModel().getViewZoom();
pageViewWidth = Math.abs(pageViewWidth / currentZoom);
pageViewHeight = Math.abs(pageViewHeight / currentZoom);
final PDimension dimension = getMaxPageDimension();
float pageViewWidth = (float) dimension.getWidth();
float pageViewHeight = (float) dimension.getHeight();

// two pages wide, generalization, pages are usually the same size we
// don't bother to look at the second pages size for the time being.
// don't bother to look at the second pages size for the time being.
pageViewWidth *= 2;

// add any horizontal padding from layout manager
Expand Down

0 comments on commit 4fa328f

Please sign in to comment.