Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Thienel2016-01-28 13:25:38 -0500
committerFlorian Thienel2016-01-28 13:25:39 -0500
commitc020bf1d2dfbe9553b5cec2ee52b5e73280f2a83 (patch)
tree35e93160cafe15ba7187ee082593e6d0ce3ed5a5
parent599929f776f14cb6c79f6768b9db1b71c5f635b6 (diff)
downloadorg.eclipse.mylyn.docs.vex-c020bf1d2dfbe9553b5cec2ee52b5e73280f2a83.tar.gz
org.eclipse.mylyn.docs.vex-c020bf1d2dfbe9553b5cec2ee52b5e73280f2a83.tar.xz
org.eclipse.mylyn.docs.vex-c020bf1d2dfbe9553b5cec2ee52b5e73280f2a83.zip
add support for a preferred image width/height
Signed-off-by: Florian Thienel <florian@thienel.org>
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/boxes/Image.java50
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/Length.java76
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/LengthProperty.java33
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java6
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBoxFactory.java2
-rw-r--r--org.eclipse.vex.docbook/styles/docbook-plain.css6
6 files changed, 121 insertions, 52 deletions
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/boxes/Image.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/boxes/Image.java
index 276e9e41..36442264 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/boxes/Image.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/boxes/Image.java
@@ -13,6 +13,8 @@ package org.eclipse.vex.core.internal.boxes;
import java.net.URL;
import org.eclipse.vex.core.internal.core.Graphics;
+import org.eclipse.vex.core.internal.core.Length;
+import org.eclipse.vex.core.internal.core.Point;
import org.eclipse.vex.core.internal.core.Rectangle;
public class Image extends BaseBox implements IInlineBox {
@@ -26,7 +28,10 @@ public class Image extends BaseBox implements IInlineBox {
private LineWrappingRule lineWrappingAtEnd;
private URL imageUrl;
- private org.eclipse.vex.core.internal.core.Image image;
+ private Length preferredWidth;
+ private Length preferredHeight;
+
+ private org.eclipse.vex.core.internal.core.Image image; // TODO use a cache for the actual image data
private boolean layoutValid;
@@ -134,6 +139,24 @@ public class Image extends BaseBox implements IInlineBox {
layoutValid = false;
}
+ public Length getPreferredWidth() {
+ return preferredWidth;
+ }
+
+ public void setPreferredWidth(final Length preferredWidth) {
+ this.preferredWidth = preferredWidth;
+ layoutValid = false;
+ }
+
+ public Length getPreferredHeight() {
+ return preferredHeight;
+ }
+
+ public void setPreferredHeight(final Length preferredHeight) {
+ this.preferredHeight = preferredHeight;
+ layoutValid = false;
+ }
+
@Override
public void accept(final IBoxVisitor visitor) {
visitor.visit(this);
@@ -151,12 +174,33 @@ public class Image extends BaseBox implements IInlineBox {
}
image = graphics.getImage(imageUrl);
- width = image.getWidth();
- height = image.getHeight();
+ final Point dimensions = calculateActualDimensions();
+
+ width = dimensions.getX();
+ height = dimensions.getY();
layoutValid = true;
}
+ private Point calculateActualDimensions() {
+ final int width = preferredWidth == null ? 0 : preferredWidth.get(image.getWidth());
+ final int height = preferredHeight == null ? 0 : preferredHeight.get(image.getHeight());
+ if (width != 0 && height != 0) {
+ return new Point(width, height);
+ }
+ if (width == 0 && height != 0) {
+ return new Point(scale(image.getWidth(), image.getHeight(), height), height);
+ }
+ if (width != 0 && height == 0) {
+ return new Point(width, scale(image.getHeight(), image.getWidth(), width));
+ }
+ return new Point(image.getWidth(), image.getHeight());
+ }
+
+ private static int scale(final int opposite, final int current, final int scaled) {
+ return Math.round(1f * scaled / current * opposite);
+ }
+
@Override
public boolean reconcileLayout(final Graphics graphics) {
final int oldHeight = height;
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/Length.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/Length.java
index a2edcba8..ce0a5b81 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/Length.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/Length.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2015 John Krasnay and others.
+ * Copyright (c) 2004, 2016 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -14,36 +14,52 @@ package org.eclipse.vex.core.internal.core;
/**
* A length that may be expressed as an absolute or relative value.
*/
-public class Length {
+public abstract class Length {
- private static final Length ZERO = new Length(0, 0, true);
-
- private final float percentageValue;
- private final int absoluteValue;
- private final boolean absolute;
+ public static final Length ZERO = new Length() {
+ @Override
+ public int get(final int referenceLength) {
+ return 0;
+ }
- private Length(final float percentage, final int absolute, final boolean isAbsolute) {
- percentageValue = percentage;
- absoluteValue = absolute;
- this.absolute = isAbsolute;
- }
+ @Override
+ public int getBaseValue() {
+ return 0;
+ }
+ };
/**
* @return a length representing an absolute value.
*/
public static Length absolute(final int value) {
- if (value == 0) {
- return ZERO;
- } else {
- return new Length(0, value, true);
- }
+ return new Length() {
+ @Override
+ public int get(final int referenceLength) {
+ return value;
+ }
+
+ @Override
+ public int getBaseValue() {
+ return value;
+ }
+ };
}
/**
* @return a length representing a relative value.
*/
public static Length relative(final float percentage) {
- return new Length(percentage, 0, false);
+ return new Length() {
+ @Override
+ public int get(final int referenceLength) {
+ return Math.round(percentage * referenceLength);
+ }
+
+ @Override
+ public int getBaseValue() {
+ return Float.floatToIntBits(percentage);
+ }
+ };
}
/**
@@ -55,21 +71,15 @@ public class Length {
* reference length by which percentage lengths will by multiplied.
* @return the actual value
*/
- public int get(final int referenceLength) {
- if (absolute) {
- return absoluteValue;
- } else {
- return Math.round(percentageValue * referenceLength);
- }
- }
+ public abstract int get(final int referenceLength);
+
+ public abstract int getBaseValue();
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + (absolute ? 1231 : 1237);
- result = prime * result + absoluteValue;
- result = prime * result + Float.floatToIntBits(percentageValue);
+ result = prime * result + getBaseValue();
return result;
}
@@ -85,13 +95,7 @@ public class Length {
return false;
}
final Length other = (Length) obj;
- if (absolute != other.absolute) {
- return false;
- }
- if (absoluteValue != other.absoluteValue) {
- return false;
- }
- if (Float.floatToIntBits(percentageValue) != Float.floatToIntBits(other.percentageValue)) {
+ if (getBaseValue() != other.getBaseValue()) {
return false;
}
return true;
@@ -99,7 +103,7 @@ public class Length {
@Override
public String toString() {
- return "Length [percentageValue=" + percentageValue + ", absoluteValue=" + absoluteValue + ", absolute=" + absolute + "]";
+ return "Length [baseValue=" + getBaseValue() + "]";
}
}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/LengthProperty.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/LengthProperty.java
index e0bda722..fb39ae98 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/LengthProperty.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/LengthProperty.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2004, 2008 John Krasnay and others.
+ * Copyright (c) 2004, 2016 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* John Krasnay - initial API and implementation
+ * Florian Thienel - add support for attribute dependend values
*******************************************************************************/
package org.eclipse.vex.core.internal.css;
@@ -40,25 +41,38 @@ public class LengthProperty extends AbstractProperty {
@Override
public Object calculate(final LexicalUnit lu, final Styles parentStyles, final Styles styles, final INode node) {
- final int ppi = getPpi();
if (isAttr(lu)) {
return node.accept(new BaseNodeVisitorWithResult<Object>(Length.absolute(0)) {
@Override
public Object visit(final IElement element) {
- return calculate(parseAttribute(lu, element), parentStyles, styles, element);
+ return new Length() {
+ @Override
+ public int get(final int referenceLength) {
+ return parseLength(parseAttribute(lu, element), parentStyles, styles).get(referenceLength);
+ }
+
+ @Override
+ public int getBaseValue() {
+ return parseLength(parseAttribute(lu, element), parentStyles, styles).getBaseValue();
+ }
+ };
}
});
}
+ return parseLength(lu, parentStyles, styles);
+ }
+
+ private Length parseLength(final LexicalUnit lu, final Styles parentStyles, final Styles styles) {
if (isLength(lu)) {
- final int length = getIntLength(lu, styles.getFontSize(), ppi);
+ final int length = getIntLength(lu, styles.getFontSize(), getPpi());
return Length.absolute(length);
} else if (isPercentage(lu)) {
return Length.relative(lu.getFloatValue() / 100);
} else if (isInherit(lu) && parentStyles != null) {
- return parentStyles.get(getName());
+ return (Length) parentStyles.get(getName());
} else {
// not specified, "auto", or other unknown value
- return Length.absolute(0);
+ return Length.ZERO;
}
}
@@ -86,7 +100,10 @@ public class LengthProperty extends AbstractProperty {
private int getPpi() {
final DisplayDevice device = DisplayDevice.getCurrent();
- final int ppi = axis == Axis.HORIZONTAL ? device.getHorizontalPPI() : device.getVerticalPPI();
- return ppi;
+ if (axis == Axis.HORIZONTAL) {
+ return device.getHorizontalPPI();
+ } else {
+ return device.getVerticalPPI();
+ }
}
}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
index b7eeea68..fe72e486 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBasedBoxModelBuilder.java
@@ -19,6 +19,7 @@ import static org.eclipse.vex.core.internal.boxes.BoxFactory.verticalBlock;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.endOffsetPlaceholder;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.endTag;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.frame;
+import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.image;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.nodeTag;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.paragraph;
import static org.eclipse.vex.core.internal.visualization.CssBoxFactory.startTag;
@@ -339,14 +340,13 @@ public class CssBasedBoxModelBuilder implements IBoxModelBuilder {
@Override
public IInlineBox visit(final ImageContent content) {
- final Image image = new Image();
try {
- image.setImageUrl(content.getResolvedImageURL());
+ return image(content.getResolvedImageURL(), styles);
} catch (final MalformedURLException e) {
// TODO log error, render error information
e.printStackTrace();
+ return staticText(e.getMessage(), styles);
}
- return image;
}
});
if (box != null) {
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBoxFactory.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBoxFactory.java
index 84fcfd52..11cd6a6d 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBoxFactory.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/visualization/CssBoxFactory.java
@@ -106,6 +106,8 @@ public class CssBoxFactory {
public static Image image(final URL imageUrl, final Styles styles) {
final Image image = new Image();
image.setImageUrl(imageUrl);
+ image.setPreferredWidth(styles.getElementWidth());
+ image.setPreferredHeight(styles.getElementHeight());
return image;
}
diff --git a/org.eclipse.vex.docbook/styles/docbook-plain.css b/org.eclipse.vex.docbook/styles/docbook-plain.css
index 74bdd688..480452a6 100644
--- a/org.eclipse.vex.docbook/styles/docbook-plain.css
+++ b/org.eclipse.vex.docbook/styles/docbook-plain.css
@@ -623,13 +623,15 @@ honorific {
display: inline;
}
-inlinemediaobject>imageobject>imagedata {
- display: inline;
+info {
+ display: block;
}
imagedata {
display: inline;
content: image(attr(fileref));
+ width: attr(width);
+ height: attr(depth);
}
imageobject {

Back to the top