* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
import org.apache.fop.area.Trait;
import org.apache.fop.area.inline.TextArea;
import org.apache.fop.fo.Constants;
this.isSpace = isSpace;
this.breakOppAfter = breakOppAfter;
}
-
+
public String toString() {
return "[ lscnt=" + iLScount
+ ", wscnt=" + iWScount
/** Used to reduce instantiation of MinOptMax with zero length. Do not modify! */
private static final MinOptMax ZERO_MINOPTMAX = new MinOptMax(0);
-
+
private FOText foText;
private char[] textArray;
/**
*/
private MinOptMax[] letterAdjustArray; //size = textArray.length + 1
- private static final char NEWLINE = '\n';
-
private Font font = null;
/** Start index of first character in this parent Area */
private short iAreaStart = 0;
/** 1/2 of word-spacing value */
private SpaceVal halfWS;
/** 1/2 of letter-spacing value */
- private SpaceVal halfLS;
+ private SpaceVal halfLS;
/** Number of space characters after previous possible break position. */
private int iNbSpacesPending;
private int lineStartBAP = 0;
private int lineEndBAP = 0;
-
+
private boolean keepTogether;
/**
public TextLayoutManager(FOText node) {
super();
foText = node;
-
+
textArray = new char[node.endIndex - node.startIndex];
System.arraycopy(node.ca, node.startIndex, textArray, 0,
node.endIndex - node.startIndex);
vecAreaInfo = new java.util.ArrayList();
}
-
+
/** {@inheritDoc} */
public void initialize() {
FontInfo fi = foText.getFOEventHandler().getFontInfo();
FontTriplet[] fontkeys = foText.getCommonFont().getFontState(fi);
font = fi.getFontInstance(fontkeys[0], foText.getCommonFont().fontSize.getValue(this));
-
+
// With CID fonts, space isn't neccesary currentFontState.width(32)
spaceCharIPD = font.getCharWidth(' ');
// Use hyphenationChar property
hyphIPD = foText.getCommonHyphenation().getHyphIPD(font);
-
+
SpaceVal ls = SpaceVal.makeLetterSpacing(foText.getLetterSpacing());
halfLS = new SpaceVal(MinOptMax.multiply(ls.getSpace(), 0.5),
ls.isConditional(), ls.isForcing(), ls.getPrecedence());
-
+
ws = SpaceVal.makeWordSpacing(foText.getWordSpacing(), ls, font);
// Make half-space: <space> on either side of a word-space)
halfWS = new SpaceVal(MinOptMax.multiply(ws.getSpace(), 0.5),
// in the SpaceVal.makeWordSpacing() method
letterSpaceIPD = ls.getSpace();
wordSpaceIPD = MinOptMax.add(new MinOptMax(spaceCharIPD), ws.getSpace());
-
+
keepTogether = foText.getKeepTogether().getWithinLine().getEnum() == Constants.EN_ALWAYS;
}
realWidth.add(MinOptMax.multiply(letterSpaceIPD, -1));
iLScount--;
}
-
+
for (int i = ai.iStartIndex; i < ai.iBreakIndex; i++) {
- MinOptMax ladj = letterAdjustArray[i + 1];
+ MinOptMax ladj = letterAdjustArray[i + 1];
if (ladj != null && ladj.isElastic()) {
iLScount++;
}
iDifference = (int) ((double) (realWidth.opt - realWidth.min)
* dIPDAdjust);
}
-
+
// set letter space adjustment
if (dIPDAdjust > 0.0) {
iLetterSpaceDim
iTotalAdjust += (iLetterSpaceDim - letterSpaceIPD.opt) * iLScount;
// set word space adjustment
- //
+ //
if (iWScount > 0) {
iWordSpaceDim += (int) ((iDifference - iTotalAdjust) / iWScount);
} else {
// the last character of a word and to space characters: in order
// to avoid this, we must subtract the letter space width twice;
// the renderer will compute the space width as:
- // space width =
+ // space width =
// = "normal" space width + letterSpaceAdjust + wordSpaceAdjust
// = spaceCharIPD + letterSpaceAdjust +
// + (iWordSpaceDim - spaceCharIPD - 2 * letterSpaceAdjust)
* @param context the layout context
* @param spaceDiff unused
* @param firstIndex the index of the first AreaInfo used for the TextArea
- * @param lastIndex the index of the last AreaInfo used for the TextArea
+ * @param lastIndex the index of the last AreaInfo used for the TextArea
* @param isLastArea is this TextArea the last in a line?
* @return the new text area
*/
// here ends a new word
// add a word to the TextArea
if (isLastArea
- && i == lastIndex
+ && i == lastIndex
&& areaInfo.bHyphenated) {
len++;
}
}
// String wordChars = new String(textArray, wordStartIndex, len);
if (isLastArea
- && i == lastIndex
+ && i == lastIndex
&& areaInfo.bHyphenated) {
// add the hyphenation character
wordChars.append(foText.getCommonHyphenation().getHyphChar(font));
}
TraitSetter.addFontTraits(textArea, font);
textArea.addTrait(Trait.COLOR, foText.getColor());
-
+
TraitSetter.addTextDecoration(textArea, foText.getTextDecoration());
-
+
return textArea;
}
-
+
private void addToLetterAdjust(int index, int width) {
if (letterAdjustArray[index] == null) {
letterAdjustArray[index] = new MinOptMax(width);
|| CharUtilities.isNonBreakableSpace(ch)
|| CharUtilities.isFixedWidthSpace(ch);
}
-
+
/** {@inheritDoc} */
public LinkedList getNextKnuthElements(LayoutContext context, int alignment) {
lineStartBAP = context.getLineStartBorderAndPaddingWidth();
iThisStart = iNextStart;
boolean inWord = false;
boolean inWhitespace = false;
- char ch = 0;
+ char ch = 0;
while (iNextStart < textArray.length) {
- ch = textArray[iNextStart];
+ ch = textArray[iNextStart];
boolean breakOpportunity = false;
- byte breakAction = keepTogether? LineBreakStatus.PROHIBITED_BREAK : lbs.nextChar(ch);
+ byte breakAction = keepTogether ? LineBreakStatus.PROHIBITED_BREAK : lbs.nextChar(ch);
switch (breakAction) {
case LineBreakStatus.COMBINING_PROHIBITED_BREAK:
case LineBreakStatus.PROHIBITED_BREAK:
log.error("Unexpected breakAction: " + breakAction);
}
if (inWord) {
- if (breakOpportunity || isSpace(ch) || ch == NEWLINE) {
+ if (breakOpportunity || isSpace(ch) || CharUtilities.isLineBreakCharacter(ch)) {
//Word boundary found, process widths and kerning
int lastIndex = iNextStart;
while (lastIndex > 0 && textArray[lastIndex - 1] == CharUtilities.SOFT_HYPHEN) {
ai = new AreaInfo(iThisStart, (short) (iNextStart),
(short) (iNextStart - iThisStart), (short) 0,
MinOptMax.multiply(wordSpaceIPD, iNextStart - iThisStart),
- false, true, breakOpportunity);
+ false, true, breakOpportunity);
vecAreaInfo.add(ai);
prevAi = ai;
returnList.add(sequence);
}
}
-
- if ((ch == CharUtilities.SPACE
- && foText.getWhitespaceTreatment() == Constants.EN_PRESERVE)
+
+ if ((ch == CharUtilities.SPACE
+ && foText.getWhitespaceTreatment() == Constants.EN_PRESERVE)
|| ch == CharUtilities.NBSPACE) {
// preserved space or non-breaking space:
// create the AreaInfo object
MinOptMax ipd = new MinOptMax(font.getCharWidth(ch));
ai = new AreaInfo(iNextStart, (short) (iNextStart + 1),
(short) 0, (short) 0,
- ipd, false, true, breakOpportunity);
+ ipd, false, true, breakOpportunity);
iThisStart = (short) (iNextStart + 1);
- } else if (ch == NEWLINE) {
+ } else if (CharUtilities.isLineBreakCharacter(ch)) {
// linefeed; this can happen when linefeed-treatment="preserve"
iThisStart = (short) (iNextStart + 1);
}
- inWord = !isSpace(ch) && ch != NEWLINE;
+ inWord = !isSpace(ch) && !CharUtilities.isLineBreakCharacter(ch);
inWhitespace = ch == CharUtilities.SPACE && foText.getWhitespaceTreatment() != Constants.EN_PRESERVE;
iNextStart++;
} // end of while
-
+
// Process any last elements
if (inWord) {
int lastIndex = iNextStart;
ai = new AreaInfo(iThisStart, (short) (iNextStart),
(short) (iNextStart - iThisStart), (short) 0,
MinOptMax.multiply(wordSpaceIPD, iNextStart - iThisStart),
- false, true, true);
+ false, true, true);
vecAreaInfo.add(ai);
// create the elements
sequence.addAll
(createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));
ai = null;
- } else if (ch == NEWLINE) {
+ } else if (CharUtilities.isLineBreakCharacter(ch)) {
if (lineEndBAP != 0) {
sequence.add
(new KnuthGlue(lineEndBAP, 0, 0,
: (iStopIndex - iStartIndex))));
if (!(bNothingChanged
- && iStopIndex == ai.iBreakIndex
+ && iStopIndex == ai.iBreakIndex
&& bHyphenFollows == false)) {
// the new AreaInfo object is not equal to the old one
if (changeList == null) {
AreaInfo ai, int leafValue) {
LinkedList spaceElements = new LinkedList();
LeafPosition mainPosition = new LeafPosition(this, leafValue);
-
+
if (!ai.breakOppAfter) {
// a non-breaking space
if (alignment == EN_JUSTIFY) {
notifyPos(new LeafPosition(this, -1)), true));
spaceElements.add(new KnuthPenalty(0, KnuthElement.INFINITE,
false, new LeafPosition(this, -1), false));
- spaceElements.add(new KnuthGlue(ai.ipdArea.opt, ai.ipdArea.max - ai.ipdArea.opt,
+ spaceElements.add(new KnuthGlue(ai.ipdArea.opt, ai.ipdArea.max - ai.ipdArea.opt,
ai.ipdArea.opt - ai.ipdArea.min, mainPosition, false));
} else {
// the space does not need to stretch or shrink, and must be
switch (alignment) {
case EN_CENTER:
// centered text:
- // if the second element is chosen as a line break these elements
+ // if the second element is chosen as a line break these elements
// add a constant amount of stretch at the end of a line and at the
// beginning of the next one, otherwise they don't add any stretch
spaceElements.add(new KnuthGlue(lineEndBAP,
case EN_START: // fall through
case EN_END:
// left- or right-aligned text:
- // if the second element is chosen as a line break these elements
+ // if the second element is chosen as a line break these elements
// add a constant amount of stretch at the end of a line, otherwise
// they don't add any stretch
spaceElements.add(new KnuthGlue(lineEndBAP,
this, -1), false));
spaceElements.add(new KnuthGlue(lineStartBAP + ai.ipdArea.opt, 0, 0,
mainPosition, false));
- }
+ }
} else {
// a (possible block) of breaking spaces
switch (alignment) {
case EN_CENTER:
// centered text:
- // if the second element is chosen as a line break these elements
+ // if the second element is chosen as a line break these elements
// add a constant amount of stretch at the end of a line and at the
// beginning of the next one, otherwise they don't add any stretch
spaceElements.add(new KnuthGlue(lineEndBAP,
case EN_START: // fall through
case EN_END:
// left- or right-aligned text:
- // if the second element is chosen as a line break these elements
+ // if the second element is chosen as a line break these elements
// add a constant amount of stretch at the end of a line, otherwise
// they don't add any stretch
if (lineStartBAP != 0 || lineEndBAP != 0) {
}
}
}
-
+
return spaceElements;
}
// constant letter spacing
wordElements.add
(new KnuthInlineBox(
- bSuppressibleLetterSpace
+ bSuppressibleLetterSpace
? ai.ipdArea.opt - letterSpaceWidth.opt
: ai.ipdArea.opt,
alignmentContext,
notifyPos(mainPosition), false));
} else {
// adjustable letter spacing
- int unsuppressibleLetterSpaces
+ int unsuppressibleLetterSpaces
= bSuppressibleLetterSpace ? ai.iLScount - 1 : ai.iLScount;
wordElements.add
(new KnuthInlineBox(ai.ipdArea.opt
(new KnuthInlineBox(0, null,
notifyPos(new LeafPosition(this, -1)), true));
}
-
+
// extra-elements if the word fragment is the end of a syllable,
// or it ends with a character that can be used as a line break
if (ai.bHyphenated) {
widthIfNoBreakOccurs = letterAdjustArray[ai.iBreakIndex];
}
//if (ai.iBreakIndex)
-
+
// the word fragment ends at the end of a syllable:
// if a break occurs the content width increases,
// otherwise nothing happens
widthIfNoBreakOccurs = ZERO_MINOPTMAX;
}
LinkedList hyphenElements = new LinkedList();
-
+
switch (alignment) {
case EN_CENTER :
// centered text:
(new KnuthGlue(lineStartBAP, 3 * LineLayoutManager.DEFAULT_SPACE_WIDTH, 0,
new LeafPosition(this, -1), true));
break;
-
+
case EN_START : // fall through
case EN_END :
// left- or right-aligned text:
new LeafPosition(this, -1), false));
}
break;
-
+
default:
// justified text, or last line justified:
// just a flagged penalty
}
}
}
-
+
return hyphenElements;
}
-
+
}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!-- $Id$ -->
+<testcase>
+ <info>
+ <p>
+ This test checks some of the UAX#14 breaking rules
+ (this file: BK: Mandatory Break (A) (Non-tailorable)).
+ </p>
+ </info>
+ <fo>
+ <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
+ <fo:layout-master-set>
+ <fo:simple-page-master master-name="normal" page-width="2.5in" page-height="10in" margin="5pt">
+ <fo:region-body/>
+ </fo:simple-page-master>
+ </fo:layout-master-set>
+ <fo:page-sequence master-reference="normal" white-space-collapse="true">
+ <fo:flow flow-name="xsl-region-body" font-size="10pt">
+ <fo:block background-color="silver" font-size="8pt" margin="3pt 0pt 0pt 0pt">
+ BK -- Mandatory Break (A) (Non-tailorable)
+ </fo:block>
+ <!-- Form Feed (FF) and LINE TABULATION (VT) no legal XML characters, thus left out -->
+ <fo:block background-color="yellow" margin="0pt 0pt 3pt 0pt">
+ VeryLongWordWithALineSeparator
PutInTheMiddleOfIt
+ </fo:block>
+ <!-- Paragraph Separator 
 left out as it might have to be treated differently from LS -->
+ <fo:block background-color="yellow" margin="0pt 0pt 3pt 0pt">
+ VeryLongWordWithANEL…PutInTheMiddleOfIt
+ </fo:block>
+ <fo:block background-color="yellow" margin="0pt 0pt 3pt 0pt"
+ linefeed-treatment="preserve">VeryLongWordWithACarriageReturn
+ PutInTheMiddleOfIt
+ </fo:block>
+ </fo:flow>
+ </fo:page-sequence>
+ </fo:root>
+ </fo>
+ <checks>
+ <eval expected="2" xpath="count(//flow/block[2]/lineArea)"/>
+ <eval expected="30" xpath="string-length(//flow/block[2]/lineArea[1]/text)"/>
+ <eval expected="156170" xpath="//flow/block[2]/lineArea[1]/text/@ipd"/>
+ <eval expected="18" xpath="string-length(//flow/block[2]/lineArea[2]/text)"/>
+ <eval expected="86150" xpath="//flow/block[2]/lineArea[2]/text/@ipd"/>
+
+ <eval expected="2" xpath="count(//flow/block[3]/lineArea)"/>
+ <eval expected="20" xpath="string-length(//flow/block[3]/lineArea[1]/text)"/>
+ <eval expected="112810" xpath="//flow/block[3]/lineArea[1]/text/@ipd"/>
+ <eval expected="18" xpath="string-length(//flow/block[3]/lineArea[2]/text)"/>
+ <eval expected="86150" xpath="//flow/block[3]/lineArea[2]/text/@ipd"/>
+
+ <eval expected="2" xpath="count(//flow/block[4]/lineArea)"/>
+ <eval expected="31" xpath="string-length(//flow/block[4]/lineArea[1]/text)"/>
+ <eval expected="161710" xpath="//flow/block[4]/lineArea[1]/text/@ipd"/>
+ <eval expected="18" xpath="string-length(//flow/block[4]/lineArea[2]/text)"/>
+ <eval expected="86150" xpath="//flow/block[4]/lineArea[2]/text/@ipd"/>
+
+ </checks>
+</testcase>