Package org.apache.fop.layoutmgr

Examples of org.apache.fop.layoutmgr.KnuthSequence


     * @throws Exception if an error occurs
     */
    public void test1() throws Exception {
        MyBreakingAlgorithm algo = new MyBreakingAlgorithm(0, 0, true, true, 0);
        algo.setConstantLineWidth(30000);
        KnuthSequence seq = getKnuthSequence1();
        algo.findBreakingPoints(seq, 1, true, BreakingAlgorithm.ALL_BREAKS);
        Part[] parts = algo.getParts();
        assertEquals("Sequence must produce 3 parts", 3, parts.length);
        assertEquals(5000, parts[0].difference);
        assertEquals(5000, parts[1].difference);
View Full Code Here


        lineStartBAP = context.getLineStartBorderAndPaddingWidth();
        lineEndBAP = context.getLineEndBorderAndPaddingWidth();
        alignmentContext = context.getAlignmentContext();

        LinkedList returnList = new LinkedList();
        KnuthSequence sequence = new KnuthSequence(true);
        AreaInfo ai = null;
        returnList.add(sequence);

        while (iNextStart < textArray.length) {
            if (textArray[iNextStart] == SPACE
                || textArray[iNextStart] == NBSPACE) {
                // normal space or non-breaking space:
                // create the AreaInfo object
                ai = new AreaInfo(iNextStart, (short) (iNextStart + 1),
                        (short) 1, (short) 0,
                        wordSpaceIPD, false);
                vecAreaInfo.add(ai);

                // create the elements
                sequence.addAll
                    (createElementsForASpace(alignment, ai, vecAreaInfo.size() - 1));

                // advance to the next character
                iNextStart++;
            } else if (textArray[iNextStart] == NEWLINE) {
                // linefeed; this can happen when linefeed-treatment="preserve"
                // add a penalty item to the list and start a new sequence
                if (lineEndBAP != 0) {
                    sequence.add
                        (new KnuthGlue(lineEndBAP, 0, 0,
                                       new LeafPosition(this, -1), true));
                }
                sequence.add
                    (new KnuthPenalty(0, -KnuthElement.INFINITE,
                                      false, null, false));
                sequence = new KnuthSequence(true);
                returnList.add(sequence);

                // advance to the next character
                iNextStart++;
            } else {
                // the beginning of a word
                iThisStart = iNextStart;
                iTempStart = iNextStart;
                MinOptMax wordIPD = new MinOptMax(0);
                for (; iTempStart < textArray.length
                        && textArray[iTempStart] != SPACE
                        && textArray[iTempStart] != NBSPACE
                        && textArray[iTempStart] != NEWLINE
                        && !(iTempStart > iNextStart
                             && BREAK_CHARS.indexOf(textArray[iTempStart - 1]) >= 0);
                        iTempStart++) {
                    wordIPD.add(font.getCharWidth(textArray[iTempStart]));
                }
                int iLetterSpaces = iTempStart - iThisStart - 1;
                // if the last character is '-' or '/' and the next one
                // is not a space, it could be used as a line end;
                // add one more letter space, in case other text follows
                if (BREAK_CHARS.indexOf(textArray[iTempStart - 1]) >= 0
                    && iTempStart < textArray.length
                    && textArray[iTempStart] != SPACE
                    && textArray[iTempStart] != NBSPACE) {
                    iLetterSpaces++;
                }
                wordIPD.add(MinOptMax.multiply(letterSpaceIPD, iLetterSpaces));

                // create the AreaInfo object
                ai = new AreaInfo(iThisStart, iTempStart, (short) 0,
                        (short) iLetterSpaces,
                        wordIPD, false);
                vecAreaInfo.add(ai);

                // create the elements
                sequence.addAll
                (createElementsForAWordFragment(alignment, ai,
                        vecAreaInfo.size() - 1, letterSpaceIPD));

                // advance to the next character
                iNextStart = iTempStart;
View Full Code Here

        LinkedList returnedList;
        KnuthElement returnedElement;

        // the list which will be returned to the parent LM
        LinkedList returnList = new LinkedList();
        KnuthSequence lastSequence = null;

        SpaceSpecifier leadingSpace = context.getLeadingSpace();
       
        alignmentContext = new AlignmentContext(font
                                    , lineHeight.getOptimum(this).getLength().getValue(this)
                                    , alignmentAdjust
                                    , alignmentBaseline
                                    , baselineShift
                                    , dominantBaseline
                                    , context.getAlignmentContext());

        childLC = new LayoutContext(context);
        childLC.setAlignmentContext(alignmentContext);

        if (context.startsNewArea()) {
            // First call to this LM in new parent "area", but this may
            // not be the first area created by this inline
            if (getSpaceStart() != null) {
                context.getLeadingSpace().addSpace(new SpaceVal(getSpaceStart(), this));
            }

            // Check for "fence"
            if (hasLeadingFence(!context.isFirstArea())) {
                // Reset leading space sequence for child areas
                leadingSpace = new SpaceSpecifier(false);
            }
            // Reset state variables
            clearPrevIPD(); // Clear stored prev content dimensions
        }

        StringBuffer trace = new StringBuffer("InlineLM:");

        // We'll add the border to the first inline sequence created.
        // This flag makes sure we do it only once.
        boolean borderAdded = false;

        if (borderProps != null) {
            childLC.setLineStartBorderAndPaddingWidth(context.getLineStartBorderAndPaddingWidth()
                + borderProps.getPaddingStart(true, this)
                + borderProps.getBorderStartWidth(true)
             );
            childLC.setLineEndBorderAndPaddingWidth(context.getLineEndBorderAndPaddingWidth()
                + borderProps.getPaddingEnd(true, this)
                + borderProps.getBorderEndWidth(true)
             );
        }
       
        while ((curLM = (LayoutManager) getChildLM()) != null) {
            if (!(curLM instanceof InlineLevelLayoutManager)) {
                // A block LM
                // Leave room for start/end border and padding
                if (borderProps != null) {
                    childLC.setRefIPD(childLC.getRefIPD()
                            - borderProps.getPaddingStart(lastChildLM != null, this)
                            - borderProps.getBorderStartWidth(lastChildLM != null)
                            - borderProps.getPaddingEnd(hasNextChildLM(), this)
                            - borderProps.getBorderEndWidth(hasNextChildLM()));
                }
            }
            // get KnuthElements from curLM
            returnedList = curLM.getNextKnuthElements(childLC, alignment);
            if (returnedList == null) {
                // curLM returned null because it finished;
                // just iterate once more to see if there is another child
                continue;
            }
            if (curLM instanceof InlineLevelLayoutManager) {
                // close the last block sequence
                if (lastSequence != null && !lastSequence.isInlineSequence()) {
                    lastSequence = null;
                    if (log.isTraceEnabled()) {
                        trace.append(" ]");
                    }
                }
                // "wrap" the Position stored in each element of returnedList
                ListIterator seqIter = returnedList.listIterator();
                while (seqIter.hasNext()) {
                    KnuthSequence sequence = (KnuthSequence) seqIter.next();
                    ListIterator listIter = sequence.listIterator();
                    while (listIter.hasNext()) {
                        returnedElement = (KnuthElement) listIter.next();
                        returnedElement.setPosition
                        (notifyPos(new NonLeafPosition(this,
                                returnedElement.getPosition())));
                    }
                    if (!sequence.isInlineSequence()) {
                        if (lastSequence != null && lastSequence.isInlineSequence()) {
                            // log.error("Last inline sequence should be closed before"
                            //                + " a block sequence");
                            lastSequence.add(new KnuthPenalty(0, -KnuthElement.INFINITE,
                                                   false, null, false));
                            lastSequence = null;
                            if (log.isTraceEnabled()) {
                                trace.append(" ]");
                            }
                        }
                        returnList.add(sequence);
                        if (log.isTraceEnabled()) {
                            trace.append(" B");
                        }
                    } else {
                        if (lastSequence == null) {
                            lastSequence = new KnuthSequence(true);
                            returnList.add(lastSequence);
                            if (!borderAdded) {
                                addKnuthElementsForBorderPaddingStart(lastSequence);
                                borderAdded = true;
                            }
                            if (log.isTraceEnabled()) {
                                trace.append(" [");
                            }
                        } else {
                            if (log.isTraceEnabled()) {
                                trace.append(" +");
                            }
                        }
                        lastSequence.addAll(sequence);
                        if (log.isTraceEnabled()) {
                            trace.append(" I");
                        }
                       // finish last paragraph if it was closed with a linefeed
                        KnuthElement lastElement = (KnuthElement) sequence.getLast();
                        if (lastElement.isPenalty()
                                && ((KnuthPenalty) lastElement).getP()
                                == -KnuthPenalty.INFINITE) {
                            // a penalty item whose value is -inf
                            // represents a preserved linefeed,
                            // wich forces a line break
                            lastSequence = null;
                            if (log.isTraceEnabled()) {
                                trace.append(" ]");
                            }
                        }
                    }
                }
            } else { // A block LM
                // close the last inline sequence
                if (lastSequence != null && lastSequence.isInlineSequence()) {
                    lastSequence.add(new KnuthPenalty(0, -KnuthElement.INFINITE,
                                           false, null, false));
                    lastSequence = null;
                    if (log.isTraceEnabled()) {
                        trace.append(" ]");
                    }
                }
                if (curLM != lastLM) {
                    // close the last block sequence
                    if (lastSequence != null && !lastSequence.isInlineSequence()) {
                        lastSequence = null;
                        if (log.isTraceEnabled()) {
                            trace.append(" ]");
                        }
                    }
                    lastLM = curLM;
                }
                if (lastSequence == null) {
                    lastSequence = new KnuthSequence(false);
                    returnList.add(lastSequence);
                    if (log.isTraceEnabled()) {
                        trace.append(" [");
                    }
                    if (!borderAdded) {
View Full Code Here

    /** @see org.apache.fop.layoutmgr.LayoutManager#getNextKnuthElements(LayoutContext, int) */
    public LinkedList getNextKnuthElements(LayoutContext context,
                                           int alignment) {
        MinOptMax ipd;
        curArea = get(context);
        KnuthSequence seq = new KnuthSequence(true);

        if (curArea == null) {
            setFinished(true);
            return null;
        }

        alignmentContext = new AlignmentContext(curArea.getBPD()
                                    , fobj.getAlignmentAdjust()
                                    , fobj.getAlignmentBaseline()
                                    , fobj.getBaselineShift()
                                    , fobj.getDominantBaseline()
                                    , context.getAlignmentContext());

        ipd = getAllocationIPD(context.getRefIPD());
        if (fobj.getLeaderPattern() == EN_USECONTENT && curArea instanceof FilledArea) {
            // If we have user supplied content make it fit if we can
            int unitWidth = ((FilledArea)curArea).getUnitWidth();
            if (ipd.opt < unitWidth && ipd.max >= unitWidth) {
                ipd.opt = unitWidth;
            }
        }

        // create the AreaInfo object to store the computed values
        areaInfo = new AreaInfo((short) 0, ipd, false, context.getAlignmentContext());

        addKnuthElementsForBorderPaddingStart(seq);
       
        // node is a fo:Leader
        seq.add(new KnuthInlineBox(0, alignmentContext,
                                    new LeafPosition(this, -1), true));
        seq.add(new KnuthPenalty(0, KnuthElement.INFINITE, false,
                                        new LeafPosition(this, -1), true));
        if (alignment == EN_JUSTIFY || alignment == 0) {
            seq.add
                (new KnuthGlue(areaInfo.ipdArea.opt,
                               areaInfo.ipdArea.max - areaInfo.ipdArea.opt,
                               areaInfo.ipdArea.opt - areaInfo.ipdArea.min,
                               new LeafPosition(this, 0), false));
        } else {
            seq.add
                (new KnuthGlue(areaInfo.ipdArea.opt,
                               0,
                               0,
                               new LeafPosition(this, 0), false));
        }
        seq.add(new KnuthInlineBox(0, alignmentContext,
                                    new LeafPosition(this, -1), true));

        addKnuthElementsForBorderPaddingEnd(seq);
       
        LinkedList returnList = new LinkedList();
View Full Code Here

                ignoreAtStart++;
            }
        }

        public void endParagraph() {
            KnuthSequence finishedPar = this.endSequence();
            if (finishedPar != null) {
                knuthParagraphs.add(finishedPar);
            }
        }
View Full Code Here

            }
        }
        if (returnedList.size() == 0) {
            //Inline part of the footnote is empty. Need to send back an auxiliary
            //zero-width, zero-height inline box so the footnote gets painted.
            KnuthSequence seq = new KnuthSequence(true);
            //Need to use an aux. box, otherwise, the line height can't be forced to zero height.
            forcedAnchor = new KnuthInlineBox(0, null, null, true);
            seq.add(forcedAnchor);
            returnedList.add(seq);
        }
        setFinished(true);

        addAnchor(returnedList);
View Full Code Here

                KnuthElement element = (KnuthElement)obj;
                if (element instanceof KnuthInlineBox) {
                    lastBox = (KnuthInlineBox) element;
                }
            } else {
                KnuthSequence seq = (KnuthSequence)obj;
                ListIterator nestedIterator = seq.listIterator(seq.size());
                while (nestedIterator.hasPrevious() && lastBox == null) {
                    KnuthElement element = (KnuthElement)nestedIterator.previous();
                    if (element instanceof KnuthInlineBox && !element.isAuxiliary()
                            || element == forcedAnchor) {
                        lastBox = (KnuthInlineBox) element;
View Full Code Here

                    continue;
                }

                if (lastPar != null) {
                    Object firstObj;
                    KnuthSequence firstSeq = null;
                    firstObj = returnedList.getFirst();
                    if (firstObj instanceof KnuthSequence) {
                        firstSeq = (KnuthSequence) firstObj;
                    }
                   
                    // finish last paragraph before a new block sequence
                    if (firstSeq != null && !firstSeq.isInlineSequence()) {
                        lastPar.endParagraph();
                        ElementListObserver.observe(lastPar, "line", null);
                        lastPar = null;
                        if (log.isTraceEnabled()) {
                            trace.append(" ]");
                        }
                        bPrevWasKnuthBox = false;
                    }
               
                    // does the first element of the first paragraph add to an existing word?
                    if (lastPar != null) {
                        KnuthElement thisElement;
                        if (firstObj instanceof KnuthElement) {
                            thisElement = (KnuthElement) firstObj;
                        } else {
                            thisElement = (KnuthElement) firstSeq.get(0);
                        }
                        if (thisElement.isBox() && !thisElement.isAuxiliary()
                                && bPrevWasKnuthBox) {
                            lastPar.addALetterSpace();
                        }
                    }

                }

               
                // loop over the KnuthSequences (and single KnuthElements) in returnedList
                // (LeafNodeLM descendants may also skip wrapping elements in KnuthSequences
                // to cause fewer container structures)
                // TODO the mixture here adds a little to the complexity. Decide whether:
                // - to leave as is and save some container instances
                // - to use KnuthSequences exclusively (adjustments on leaf-type LMs necessary)
                // See also FootnoteLM.addAnchor() as well as right above this comment
                // for similar code. Or see http://svn.apache.org/viewcvs?rev=230779&view=rev
                ListIterator iter = returnedList.listIterator();
                while (iter.hasNext()) {
                    Object obj = iter.next();
                    KnuthElement singleElement = null;
                    KnuthSequence sequence = null;
                    if (obj instanceof KnuthElement) {
                        singleElement = (KnuthElement)obj;
                    } else {
                        sequence = (KnuthSequence)obj;
                    }
                    // the sequence contains inline Knuth elements
                    if (singleElement != null || sequence.isInlineSequence()) {
                        // look at the last element
                        KnuthElement lastElement;
                        if (singleElement != null) {
                            lastElement = singleElement;
                        } else {
                            lastElement = (KnuthElement) sequence.getLast();
                            if (lastElement == null) {
                                throw new NullPointerException(
                                        "Sequence was empty! lastElement is null");
                            }
                        }
View Full Code Here

        // find the optimal line breaking points for each paragraph
        ListIterator paragraphsIterator
            = knuthParagraphs.listIterator(knuthParagraphs.size());
        lineLayoutsList = new ArrayList(knuthParagraphs.size());
        while (paragraphsIterator.hasPrevious()) {
            KnuthSequence seq = (KnuthSequence) paragraphsIterator.previous();
            if (!seq.isInlineSequence()) {
                lineLayouts = createBlockLineBreak(seq);
            } else {
                lineLayouts = findOptimalBreakingPoints(alignment, (Paragraph) seq);
            }
            lineLayoutsList.add(0, lineLayouts);
View Full Code Here

                        new Position(this), 0, context));
                //returnList.add(new KnuthPenalty(0, 0, false, new Position(this), false));
            }
       
            lineLayouts = (LineLayoutPossibilities)lineLayoutsList.get(p);
            KnuthSequence seq = (KnuthSequence) knuthParagraphs.get(p);

            if (!seq.isInlineSequence()) {
                LinkedList targetList = new LinkedList();
                ListIterator listIter = seq.listIterator();
                while (listIter.hasNext()) {
                    KnuthElement tempElement;
                    tempElement = (KnuthElement) listIter.next();
                    if (tempElement.getLayoutManager() != this) {
                        tempElement.setPosition(notifyPos(new NonLeafPosition(this,
                                tempElement.getPosition())));
                    }
                    targetList.add(tempElement);
                }
                returnList.addAll(targetList);
            } else if (seq.isInlineSequence() && alignment == EN_JUSTIFY) {
                /* justified vertical alignment (not in the XSL FO recommendation):
                   create a multi-layout sequence whose elements will contain
                   a conventional Position */
                Position returnPosition = new LeafPosition(this, p);
                createElements(returnList, lineLayouts, returnPosition);
            } else {
                /* "normal" vertical alignment: create a sequence whose boxes
                   represent effective lines, and contain LineBreakPositions */
                Position returnPosition = new LeafPosition(this, p);
                int startIndex = 0;
                for (int i = 0;
                        i < lineLayouts.getChosenLineCount();
                        i++) {
                    if (!((BlockLevelLayoutManager) parentLM).mustKeepTogether()
                            && i >= fobj.getOrphans()
                            && i <= lineLayouts.getChosenLineCount() - fobj.getWidows()
                            && returnList.size() > 0) {
                        // null penalty allowing a page break between lines
                        returnList.add(new BreakElement(
                                returnPosition, 0, context));
                        //returnList.add(new KnuthPenalty(0, 0, false, returnPosition, false));
                    }
                    int endIndex = ((LineBreakPosition) lineLayouts.getChosenPosition(i)).getLeafPos();
                    // create a list of the FootnoteBodyLM handling footnotes
                    // whose citations are in this line
                    LinkedList footnoteList = new LinkedList();
                    ListIterator elementIterator = seq.listIterator(startIndex);
                    while (elementIterator.nextIndex() <= endIndex) {
                        KnuthElement element = (KnuthElement) elementIterator.next();
                        if (element instanceof KnuthInlineBox
                            && ((KnuthInlineBox) element).isAnchor()) {
                            footnoteList.add(((KnuthInlineBox) element).getFootnoteBodyLM());
View Full Code Here

TOP

Related Classes of org.apache.fop.layoutmgr.KnuthSequence

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.