Author: vhennebert
Date: Thu Feb 10 17:19:54 2011
New Revision: 1069474
URL: http://svn.apache.org/viewvc?rev=1069474&view=rev
Log:
Clean-ups, simplifications and improvements of AlignmentContext and
ScaledBaselineTable
Added:
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTable.java
- copied, changed from r1069455,
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/BasicScaledBaselineTable.java
Removed:
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/BasicScaledBaselineTable.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTableFactory.java
Modified:
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/AlignmentContext.java
Modified:
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/AlignmentContext.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/AlignmentContext.java?rev=1069474&r1=1069473&r2=1069474&view=diff
==============================================================================
---
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/AlignmentContext.java
(original)
+++
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/AlignmentContext.java
Thu Feb 10 17:19:54 2011
@@ -32,21 +32,27 @@ import org.apache.fop.fonts.Font;
*/
public class AlignmentContext implements Constants {
- /** The height or BPD of this context */
+ /** The height or BPD of this context. */
private int areaHeight;
- /** The computed line-height property value applicable */
+
+ /** The computed line-height property value applicable. */
private int lineHeight;
- /** The distance in BPD from the top of the box to the alignmentPoint */
+
+ /** The distance in BPD from the top of the box to the alignmentPoint. */
private int alignmentPoint;
- /** The baseline shift value in effect */
+
+ /** The baseline shift value in effect. */
private int baselineShiftValue;
- /** The computed alignment baseline identifier */
+
+ /** The computed alignment baseline identifier. */
private int alignmentBaselineIdentifier;
- /** The x height */
+
+ /** The x height. */
private int xHeight;
- private ScaledBaselineTable scaledBaselineTable = null;
- private ScaledBaselineTable actualBaselineTable = null;
- private AlignmentContext parentAlignmentContext = null;
+
+ private ScaledBaselineTable scaledBaselineTable;
+ private ScaledBaselineTable actualBaselineTable;
+ private AlignmentContext parentAlignmentContext;
/**
* Creates a new instance of AlignmentContext
@@ -58,57 +64,20 @@ public class AlignmentContext implements
* @param dominantBaseline the dominant-baseline property
* @param parentAlignmentContext the parent alignment context
*/
- public AlignmentContext(int height
- , Length alignmentAdjust
- , int alignmentBaseline
- , Length baselineShift
- , int dominantBaseline
- , AlignmentContext parentAlignmentContext) {
-
- this.areaHeight = height;
- this.lineHeight = height;
- this.xHeight = height;
- this.parentAlignmentContext = parentAlignmentContext;
- this.scaledBaselineTable =
parentAlignmentContext.getScaledBaselineTable();
- setAlignmentBaselineIdentifier(alignmentBaseline
- ,
parentAlignmentContext.getDominantBaselineIdentifier());
- setBaselineShift(baselineShift);
- int dominantBaselineIdentifier =
parentAlignmentContext.getDominantBaselineIdentifier();
- boolean newScaledBaselineTableRequired = false;
- if (baselineShiftValue != 0) {
- newScaledBaselineTableRequired = true;
- }
- switch (dominantBaseline) {
- case EN_AUTO:
- newScaledBaselineTableRequired = baselineShiftValue != 0;
- break;
- case EN_USE_SCRIPT: // TODO
- break;
- case EN_NO_CHANGE:
- break;
- case EN_RESET_SIZE:
- newScaledBaselineTableRequired = true;
- break;
- default:
- newScaledBaselineTableRequired = true;
- dominantBaselineIdentifier = dominantBaseline;
- break;
- }
- actualBaselineTable =
ScaledBaselineTableFactory.makeGraphicsScaledBaselineTable(
- height,
-
dominantBaselineIdentifier,
-
scaledBaselineTable.getWritingMode());
- if (newScaledBaselineTableRequired) {
- scaledBaselineTable =
ScaledBaselineTableFactory.makeGraphicsScaledBaselineTable(
- height,
-
dominantBaselineIdentifier,
-
scaledBaselineTable.getWritingMode());
- }
- setAlignmentAdjust(alignmentAdjust);
+ AlignmentContext(int height,
+ Length alignmentAdjust,
+ int alignmentBaseline,
+ Length baselineShift,
+ int dominantBaseline,
+ AlignmentContext parentAlignmentContext) {
+
+ this(height, 0, height, height, alignmentAdjust, alignmentBaseline,
baselineShift,
+ dominantBaseline, parentAlignmentContext);
}
/**
- * Creates a new instance of AlignmentContext
+ * Creates a new instance.
+ *
* @param font the font
* @param lineHeight the computed value of the lineHeight property
* @param alignmentAdjust the alignment-adjust property
@@ -117,18 +86,45 @@ public class AlignmentContext implements
* @param dominantBaseline the dominant-baseline property
* @param parentAlignmentContext the parent alignment context
*/
- public AlignmentContext(Font font
- , int lineHeight
- , Length alignmentAdjust
- , int alignmentBaseline
- , Length baselineShift
- , int dominantBaseline
- , AlignmentContext parentAlignmentContext) {
- this.areaHeight = font.getAscender() - font.getDescender();
+ AlignmentContext(Font font,
+ int lineHeight,
+ Length alignmentAdjust,
+ int alignmentBaseline,
+ Length baselineShift,
+ int dominantBaseline,
+ AlignmentContext parentAlignmentContext) {
+ this(font.getAscender(), font.getDescender(), lineHeight,
font.getXHeight(),
+ alignmentAdjust, alignmentBaseline, baselineShift,
dominantBaseline,
+ parentAlignmentContext);
+ }
+
+ /**
+ * Creates a new instance of AlignmentContext.
+ * @param altitude the altitude of the area
+ * @param depth the depth of the area
+ * @param lineHeight the line height
+ * @param xHeight the xHeight
+ * @param alignmentAdjust the alignment-adjust property
+ * @param alignmentBaseline the alignment-baseline property
+ * @param baselineShift the baseline-shift property
+ * @param dominantBaseline the dominant-baseline property
+ * @param parentAlignmentContext the parent alignment context
+ */
+ private AlignmentContext(int altitude,
+ int depth,
+ int lineHeight,
+ int xHeight,
+ Length alignmentAdjust,
+ int alignmentBaseline,
+ Length baselineShift,
+ int dominantBaseline,
+ AlignmentContext parentAlignmentContext) {
+
+ this.areaHeight = altitude - depth;
this.lineHeight = lineHeight;
+ this.xHeight = xHeight;
this.parentAlignmentContext = parentAlignmentContext;
this.scaledBaselineTable =
parentAlignmentContext.getScaledBaselineTable();
- this.xHeight = font.getXHeight();
setAlignmentBaselineIdentifier(alignmentBaseline
,
parentAlignmentContext.getDominantBaselineIdentifier());
setBaselineShift(baselineShift);
@@ -153,13 +149,19 @@ public class AlignmentContext implements
dominantBaselineIdentifier = dominantBaseline;
break;
}
- actualBaselineTable =
ScaledBaselineTableFactory.makeFontScaledBaselineTable(font,
-
dominantBaselineIdentifier,
-
scaledBaselineTable.getWritingMode());
+ actualBaselineTable = new ScaledBaselineTable(
+ altitude,
+ depth,
+ xHeight,
+ dominantBaselineIdentifier,
+ scaledBaselineTable.getWritingMode());
if (newScaledBaselineTableRequired) {
- scaledBaselineTable =
ScaledBaselineTableFactory.makeFontScaledBaselineTable(font,
- dominantBaselineIdentifier,
- scaledBaselineTable.getWritingMode());
+ scaledBaselineTable = new ScaledBaselineTable(
+ altitude,
+ depth,
+ xHeight,
+ dominantBaselineIdentifier,
+ scaledBaselineTable.getWritingMode());
}
setAlignmentAdjust(alignmentAdjust);
}
@@ -171,13 +173,12 @@ public class AlignmentContext implements
* @param lineHeight the computed value of the lineHeight property
* @param writingMode the current writing mode
*/
- public AlignmentContext(Font font, int lineHeight, int writingMode) {
+ AlignmentContext(Font font, int lineHeight, int writingMode) {
this.areaHeight = font.getAscender() - font.getDescender();
this.lineHeight = lineHeight;
this.xHeight = font.getXHeight();
- this.parentAlignmentContext = null;
- this.scaledBaselineTable
- =
ScaledBaselineTableFactory.makeFontScaledBaselineTable(font, writingMode);
+ this.scaledBaselineTable = new ScaledBaselineTable(font.getAscender(),
font.getDescender(),
+ font.getXHeight(), Constants.EN_ALPHABETIC, writingMode);
this.actualBaselineTable = scaledBaselineTable;
this.alignmentBaselineIdentifier = getDominantBaselineIdentifier();
this.alignmentPoint = font.getAscender();
@@ -203,7 +204,8 @@ public class AlignmentContext implements
}
/**
- * Returns the current alignment baseline identifier
+ * Returns the current alignment baseline identifier.
+ *
* @return the alignment baseline identifier
*/
public int getAlignmentBaselineIdentifier() {
@@ -211,7 +213,7 @@ public class AlignmentContext implements
}
/**
- * Sets the current alignment baseline identifer. For
+ * Sets the current alignment baseline identifier. For
* alignment-baseline values of "auto" and "baseline" this
* method does the conversion into the appropriate computed
* value assuming script is "auto" and the fo is not fo:character.
@@ -237,8 +239,7 @@ public class AlignmentContext implements
case EN_MATHEMATICAL:
this.alignmentBaselineIdentifier = alignmentBaseline;
break;
- default:
- break;
+ default: throw new
IllegalArgumentException(String.valueOf(alignmentBaseline));
}
}
@@ -285,7 +286,7 @@ public class AlignmentContext implements
* Return the scaled baseline table for this context.
* @return the scaled baseline table
*/
- public ScaledBaselineTable getScaledBaselineTable() {
+ private ScaledBaselineTable getScaledBaselineTable() {
return this.scaledBaselineTable;
}
@@ -293,16 +294,8 @@ public class AlignmentContext implements
* Return the dominant baseline identifier.
* @return the dominant baseline identifier
*/
- public int getDominantBaselineIdentifier() {
- return scaledBaselineTable.getDominantBaselineIdentifier();
- }
-
- /**
- * Return the writing mode.
- * @return the writing mode
- */
- public int getWritingMode() {
- return scaledBaselineTable.getWritingMode();
+ private int getDominantBaselineIdentifier() {
+ return actualBaselineTable.getDominantBaselineIdentifier();
}
/**
@@ -312,7 +305,6 @@ public class AlignmentContext implements
*/
private void setBaselineShift(Length baselineShift) {
baselineShiftValue = 0;
- ScaledBaselineTable sbt = null;
switch (baselineShift.getEnum()) {
case EN_BASELINE: //Nothing to do
break;
@@ -332,9 +324,7 @@ public class AlignmentContext implements
, LengthBase.CUSTOM_BASE
,
parentAlignmentContext.getLineHeight()));
break;
- default:
- break;
-
+ default: throw new
IllegalArgumentException(String.valueOf(baselineShift.getEnum()));
}
}
@@ -351,7 +341,7 @@ public class AlignmentContext implements
* the parent dominant baseline.
* @return the offset in shift direction
*/
- public int getBaselineOffset() {
+ private int getBaselineOffset() {
if (parentAlignmentContext == null) {
return 0;
}
@@ -370,9 +360,9 @@ public class AlignmentContext implements
/**
* Return the offset between the current dominant baseline and
* the outermost parent dominant baseline.
- * @return the offet in shift direction
+ * @return the offset in shift direction
*/
- public int getTotalBaselineOffset() {
+ private int getTotalBaselineOffset() {
int offset = 0;
if (parentAlignmentContext != null) {
offset = getBaselineOffset() +
parentAlignmentContext.getTotalBaselineOffset();
@@ -395,7 +385,7 @@ public class AlignmentContext implements
* @param alignmentBaselineId the alignment baseline
* @return the offset
*/
- public int getTotalAlignmentBaselineOffset(int alignmentBaselineId) {
+ private int getTotalAlignmentBaselineOffset(int alignmentBaselineId) {
int offset = baselineShiftValue;
if (parentAlignmentContext != null) {
offset = parentAlignmentContext.getTotalBaselineOffset()
@@ -408,11 +398,12 @@ public class AlignmentContext implements
/**
* Return the offset between the dominant baseline and
- * the given actual baseline
+ * the given actual baseline.
+ *
* @param baselineIdentifier the baseline
* @return the offset
*/
- public int getActualBaselineOffset(int baselineIdentifier) {
+ private int getActualBaselineOffset(int baselineIdentifier) {
// This is the offset from the dominant baseline to the alignment
baseline
int offset = getTotalAlignmentBaselineOffset() -
getTotalBaselineOffset();
// Add the offset to the actual baseline we want
@@ -443,7 +434,7 @@ public class AlignmentContext implements
* Return the line height of the context.
* @return the height
*/
- public int getLineHeight() {
+ private int getLineHeight() {
return lineHeight;
}
@@ -469,7 +460,7 @@ public class AlignmentContext implements
* The x height of the context.
* @return the x height
*/
- public int getXHeight() {
+ private int getXHeight() {
return this.xHeight;
}
@@ -513,10 +504,6 @@ public class AlignmentContext implements
&& parentAlignmentContext.usesInitialBaselineTable());
}
- private boolean isHorizontalWritingMode() {
- return (getWritingMode() == EN_LR_TB || getWritingMode() == EN_RL_TB);
- }
-
/** {@inheritDoc} */
public String toString() {
StringBuffer sb = new StringBuffer(64);
Copied:
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTable.java
(from r1069455,
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/BasicScaledBaselineTable.java)
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTable.java?p2=xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTable.java&p1=xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/BasicScaledBaselineTable.java&r1=1069455&r2=1069474&rev=1069474&view=diff
==============================================================================
---
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/BasicScaledBaselineTable.java
(original)
+++
xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/ScaledBaselineTable.java
Thu Feb 10 17:19:54 2011
@@ -19,48 +19,54 @@
package org.apache.fop.layoutmgr.inline;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
import org.apache.fop.fo.Constants;
/**
- * An implementation of the ScaledBaselineTable interface which calculates
- * all baselines given the height above and below the dominant baseline.
+ * The FOP specific incarnation of the XSL-FO scaled baseline table.
+ * All baseline tables are scaled to the font size of the font they
+ * apply to. This class uses a coordinate system with its origin
+ * where the dominant baseline intersects the start edge of the box.
+ * All measurements are in mpt.
*/
-public class BasicScaledBaselineTable implements ScaledBaselineTable,
Constants {
+final class ScaledBaselineTable {
+
+ private static final float HANGING_BASELINE_FACTOR = 0.8f;
+
+ private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f;
+
+ private final int altitude;
+
+ private final int depth;
- /** A logger for this class */
- protected Log log = LogFactory.getLog(BasicScaledBaselineTable.class);
+ private final int xHeight;
+
+ private final int dominantBaselineIdentifier;
+
+ private final int writingMode;
+
+ private final int dominantBaselineOffset;
- private int altitude;
- private int depth;
- private int xHeight;
- private int dominantBaselineIdentifier;
- private int writingMode;
- private int dominantBaselineOffset;
private int beforeEdgeOffset;
+
private int afterEdgeOffset;
- private static final float HANGING_BASELINE_FACTOR = 0.8f;
- private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f;
/**
*
* Creates a new instance of BasicScaledBaselineTable for the given
- * altitude, depth, xHeight, baseline and writingmode.
+ * altitude, depth, xHeight, baseline and writing mode.
* @param altitude the height of the box or the font ascender
* @param depth the font descender or 0
* @param xHeight the font xHeight
* @param dominantBaselineIdentifier the dominant baseline given as an
integer constant
* @param writingMode the writing mode given as an integer constant
*/
- public BasicScaledBaselineTable(int altitude
- , int depth
- , int xHeight
- , int dominantBaselineIdentifier
- , int writingMode) {
+ ScaledBaselineTable(int altitude,
+ int depth,
+ int xHeight,
+ int dominantBaselineIdentifier,
+ int writingMode) {
this.altitude = altitude;
this.depth = depth;
this.xHeight = xHeight;
@@ -75,7 +81,7 @@ public class BasicScaledBaselineTable im
* Return the dominant baseline for this baseline table.
* @return the dominant baseline
*/
- public int getDominantBaselineIdentifier() {
+ int getDominantBaselineIdentifier() {
return this.dominantBaselineIdentifier;
}
@@ -83,61 +89,57 @@ public class BasicScaledBaselineTable im
* Return the writing mode for this baseline table.
* @return the writing mode
*/
- public int getWritingMode() {
+ int getWritingMode() {
return this.writingMode;
}
/**
- * Return the baseline offset measured from the dominant
- * baseline for the given baseline.
- * @param baselineIdentifier the baseline identifier
- * @return the baseline offset
+ * Return the offset of the given baseline from the dominant baseline.
+ *
+ * @param baselineIdentifier a baseline identifier
+ * @return the offset from the dominant baseline
*/
- public int getBaseline(int baselineIdentifier) {
- int offset;
+ int getBaseline(int baselineIdentifier) {
+ int offset = 0;
if (!isHorizontalWritingMode()) {
switch (baselineIdentifier) {
- default:
- case EN_TOP:
- case EN_TEXT_TOP:
- case EN_TEXT_BOTTOM:
- case EN_BOTTOM:
- log.warn("The given baseline is only supported for
horizontal"
- + " writing modes");
- offset = 0;
- }
- } else {
- switch (baselineIdentifier) {
- case EN_TOP: // fall through
- case EN_BEFORE_EDGE:
- offset = beforeEdgeOffset;
- break;
- case EN_TEXT_TOP:
- case EN_TEXT_BEFORE_EDGE:
- case EN_HANGING:
- case EN_CENTRAL:
- case EN_MIDDLE:
- case EN_MATHEMATICAL:
- case EN_ALPHABETIC:
- case EN_IDEOGRAPHIC:
- case EN_TEXT_BOTTOM:
- case EN_TEXT_AFTER_EDGE:
- offset = getBaselineDefaultOffset(baselineIdentifier) -
dominantBaselineOffset;
- break;
- case EN_BOTTOM: // fall through
- case EN_AFTER_EDGE:
- offset = afterEdgeOffset;
- break;
- default:
- offset = 0;
- break;
+ case Constants.EN_TOP:
+ case Constants.EN_TEXT_TOP:
+ case Constants.EN_TEXT_BOTTOM:
+ case Constants.EN_BOTTOM:
+ throw new IllegalArgumentException("Baseline " +
baselineIdentifier
+ + " only supported for horizontal writing modes");
+ default: // TODO
}
}
+ switch (baselineIdentifier) {
+ case Constants.EN_TOP: // fall through
+ case Constants.EN_BEFORE_EDGE:
+ offset = beforeEdgeOffset;
+ break;
+ case Constants.EN_TEXT_TOP:
+ case Constants.EN_TEXT_BEFORE_EDGE:
+ case Constants.EN_HANGING:
+ case Constants.EN_CENTRAL:
+ case Constants.EN_MIDDLE:
+ case Constants.EN_MATHEMATICAL:
+ case Constants.EN_ALPHABETIC:
+ case Constants.EN_IDEOGRAPHIC:
+ case Constants.EN_TEXT_BOTTOM:
+ case Constants.EN_TEXT_AFTER_EDGE:
+ offset = getBaselineDefaultOffset(baselineIdentifier) -
dominantBaselineOffset;
+ break;
+ case Constants.EN_BOTTOM: // fall through
+ case Constants.EN_AFTER_EDGE:
+ offset = afterEdgeOffset;
+ break;
+ default: throw new
IllegalArgumentException(String.valueOf(baselineIdentifier));
+ }
return offset;
}
private boolean isHorizontalWritingMode() {
- return writingMode == EN_LR_TB || writingMode == EN_RL_TB;
+ return writingMode == Constants.EN_LR_TB || writingMode ==
Constants.EN_RL_TB;
}
/**
@@ -149,49 +151,55 @@ public class BasicScaledBaselineTable im
private int getBaselineDefaultOffset(int baselineIdentifier) {
int offset = 0;
switch (baselineIdentifier) {
- case EN_TEXT_BEFORE_EDGE:
+ case Constants.EN_TEXT_BEFORE_EDGE:
offset = altitude;
break;
- case EN_HANGING:
- offset = (int)Math.round(altitude * HANGING_BASELINE_FACTOR);
+ case Constants.EN_HANGING:
+ offset = Math.round(altitude * HANGING_BASELINE_FACTOR);
break;
- case EN_CENTRAL:
+ case Constants.EN_CENTRAL:
offset = (altitude - depth) / 2 + depth;
break;
- case EN_MIDDLE:
+ case Constants.EN_MIDDLE:
offset = xHeight / 2;
break;
- case EN_MATHEMATICAL:
- offset = (int)Math.round(altitude *
MATHEMATICAL_BASELINE_FACTOR);
+ case Constants.EN_MATHEMATICAL:
+ offset = Math.round(altitude * MATHEMATICAL_BASELINE_FACTOR);
break;
- case EN_ALPHABETIC:
+ case Constants.EN_ALPHABETIC:
offset = 0;
break;
- case EN_IDEOGRAPHIC: // Fall through
- case EN_TEXT_AFTER_EDGE:
+ case Constants.EN_IDEOGRAPHIC: // Fall through
+ case Constants.EN_TEXT_AFTER_EDGE:
offset = depth;
break;
- default:
- break;
+ default: throw new
IllegalArgumentException(String.valueOf(baselineIdentifier));
}
return offset;
}
/**
- * {@inheritDoc}
+ * Sets the position of the before and after baselines.
+ * This is usually only done for line areas. For other
+ * areas the position of the before and after baselines
+ * are fixed when the table is constructed.
+ * @param beforeBaseline the offset of the before-edge baseline from the
dominant baseline
+ * @param afterBaseline the offset of the after-edge baseline from the
dominant baseline
*/
- public void setBeforeAndAfterBaselines(int beforeBaseline, int
afterBaseline) {
+ void setBeforeAndAfterBaselines(int beforeBaseline, int afterBaseline) {
beforeEdgeOffset = beforeBaseline;
afterEdgeOffset = afterBaseline;
}
/**
- * {@inheritDoc}
- */
- public ScaledBaselineTable deriveScaledBaselineTable(int
baselineIdentifier) {
- BasicScaledBaselineTable bac
- = new BasicScaledBaselineTable(altitude, depth, xHeight
- , baselineIdentifier,
this.writingMode);
+ * Return a new baseline table for the given baseline based
+ * on the current baseline table.
+ * @param baselineIdentifier the baseline identifer
+ * @return a new baseline with the new baseline
+ */
+ ScaledBaselineTable deriveScaledBaselineTable(int baselineIdentifier) {
+ ScaledBaselineTable bac = new ScaledBaselineTable(altitude, depth,
xHeight,
+ baselineIdentifier, this.writingMode);
return bac;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]