<commit_before>/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed 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$ */

package org.apache.fop.layoutmgr.table;

import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.PercentBase;
import org.apache.fop.fo.PropertyManager;
import org.apache.fop.fo.properties.TableColLength;
import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.LeafPosition;
import org.apache.fop.layoutmgr.BreakPoss;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.BreakPossPosIter;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.TraitSetter;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.fo.properties.CommonBorderAndPadding;
import org.apache.fop.fo.properties.CommonBackground;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * LayoutManager for a table FO.
 * A table consists of columns, table header, table footer and multiple
 * table bodies.
 * The header, footer and body add the areas created from the table cells.
 * The table then creates areas for the columns, bodies and rows
 * the render background.
 */
public class TableLayoutManager extends BlockStackingLayoutManager {
    private List columns = null;
    private Body tableHeader = null;
    private Body tableFooter = null;

    private CommonBorderAndPadding borderProps = null;
    private CommonBackground backgroundProps;

    private Block curBlockArea;

    private List bodyBreaks = new ArrayList();
    private BreakPoss headerBreak;
    private BreakPoss footerBreak;

    private class SectionPosition extends LeafPosition {
        protected List list;
        protected SectionPosition(LayoutManager lm, int pos, List l) {
            super(lm, pos);
            list = l;
        }
    }

    /**
     * Create a new table layout manager.
     *
     */
    public TableLayoutManager() {
    }

    /**
     * @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties(PropertyManager)
     */
    protected void initProperties(PropertyManager propMgr) {
        borderProps = propMgr.getBorderAndPadding();
        backgroundProps = propMgr.getBackgroundProps();
    }

    /**
     * Set the columns for this table.
     *
     * @param cols the list of column layout managers
     */
    public void setColumns(List cols) {
        columns = cols;
    }

    /**
     * Set the table header.
     *
     * @param th the table header layout manager
     */
    public void setTableHeader(Body th) {
        tableHeader = th;
        tableHeader.setParent(this);
    }

    /**
     * Set the table footer.
     *
     * @param tf the table footer layout manager
     */
    public void setTableFooter(Body tf) {
        tableFooter = tf;
        tableFooter.setParent(this);
    }

    /**
     * Get the next break possibility.
     * The break possibility depends on the height of the header and footer
     * and possible breaks inside the table body.
     *
     * @param context the layout context for finding breaks
     * @return the next break possibility
     */
    public BreakPoss getNextBreakPoss(LayoutContext context) {
        Body curLM; // currently active LM

        MinOptMax stackSize = new MinOptMax();
        // if starting add space before
        // stackSize.add(spaceBefore);
        BreakPoss lastPos = null;

        fobj.setLayoutDimension(PercentBase.BLOCK_IPD, context.getRefIPD());
        fobj.setLayoutDimension(PercentBase.BLOCK_BPD, context.getStackLimit().opt);
        fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_IPD, context.getRefIPD());
        fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_BPD, context.getStackLimit().opt);
        
        int sumCols = 0;
        float factors = 0;
        if (columns != null) { 
            for (Iterator i = columns.iterator(); i.hasNext(); ) {
                Column column = (Column) i.next();
                Length width = column.getWidth();
                sumCols += width.getValue();
                if (width instanceof TableColLength) {
                    factors += ((TableColLength) width).getTableUnits();
                }
            }
        }
        if (sumCols < context.getRefIPD()) {
            fobj.setLayoutDimension(PercentBase.TABLE_UNITS, (context.getRefIPD() - sumCols) / factors);
        }
        MinOptMax headerSize = null;
        if (tableHeader != null) {
            tableHeader.setUserAgent(getUserAgent());
            tableHeader.resetPosition(null);
            headerBreak = getHeight(tableHeader, context);
            headerSize = headerBreak.getStackingSize();
            stackSize.add(headerSize);
        }

        MinOptMax footerSize = null;
        if (tableFooter != null) {
            tableFooter.setUserAgent(getUserAgent());
            tableFooter.resetPosition(null);
            footerBreak = getHeight(tableFooter, context);
            footerSize = footerBreak.getStackingSize();
            stackSize.add(footerSize);<commit_msg>fixes problem that prevented proportional-column-width function working<commit_after>/*
 * Copyright 1999-2004 The Apache Software Foundation.
 *
 * Licensed 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: TableLayoutManager.java,v 1.8 2004/03/21 12:03:08 gmazza Exp $ */

package org.apache.fop.layoutmgr.table;

import org.apache.fop.datatypes.Length;
import org.apache.fop.datatypes.PercentBase;
import org.apache.fop.fo.PropertyManager;
import org.apache.fop.fo.properties.TableColLength;
import org.apache.fop.layoutmgr.BlockStackingLayoutManager;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.LeafPosition;
import org.apache.fop.layoutmgr.BreakPoss;
import org.apache.fop.layoutmgr.LayoutContext;
import org.apache.fop.layoutmgr.PositionIterator;
import org.apache.fop.layoutmgr.BreakPossPosIter;
import org.apache.fop.layoutmgr.Position;
import org.apache.fop.layoutmgr.TraitSetter;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.traits.MinOptMax;
import org.apache.fop.fo.properties.CommonBorderAndPadding;
import org.apache.fop.fo.properties.CommonBackground;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * LayoutManager for a table FO.
 * A table consists of columns, table header, table footer and multiple
 * table bodies.
 * The header, footer and body add the areas created from the table cells.
 * The table then creates areas for the columns, bodies and rows
 * the render background.
 */
public class TableLayoutManager extends BlockStackingLayoutManager {
    private List columns = null;
    private Body tableHeader = null;
    private Body tableFooter = null;

    private CommonBorderAndPadding borderProps = null;
    private CommonBackground backgroundProps;

    private Block curBlockArea;

    private List bodyBreaks = new ArrayList();
    private BreakPoss headerBreak;
    private BreakPoss footerBreak;

    private class SectionPosition extends LeafPosition {
        protected List list;
        protected SectionPosition(LayoutManager lm, int pos, List l) {
            super(lm, pos);
            list = l;
        }
    }

    /**
     * Create a new table layout manager.
     *
     */
    public TableLayoutManager() {
    }

    /**
     * @see org.apache.fop.layoutmgr.AbstractLayoutManager#initProperties(PropertyManager)
     */
    protected void initProperties(PropertyManager propMgr) {
        borderProps = propMgr.getBorderAndPadding();
        backgroundProps = propMgr.getBackgroundProps();
    }

    /**
     * Set the columns for this table.
     *
     * @param cols the list of column layout managers
     */
    public void setColumns(List cols) {
        columns = cols;
    }

    /**
     * Set the table header.
     *
     * @param th the table header layout manager
     */
    public void setTableHeader(Body th) {
        tableHeader = th;
        tableHeader.setParent(this);
    }

    /**
     * Set the table footer.
     *
     * @param tf the table footer layout manager
     */
    public void setTableFooter(Body tf) {
        tableFooter = tf;
        tableFooter.setParent(this);
    }

    /**
     * Get the next break possibility.
     * The break possibility depends on the height of the header and footer
     * and possible breaks inside the table body.
     *
     * @param context the layout context for finding breaks
     * @return the next break possibility
     */
    public BreakPoss getNextBreakPoss(LayoutContext context) {
        Body curLM; // currently active LM

        MinOptMax stackSize = new MinOptMax();
        // if starting add space before
        // stackSize.add(spaceBefore);
        BreakPoss lastPos = null;

        fobj.setLayoutDimension(PercentBase.BLOCK_IPD, context.getRefIPD());
        fobj.setLayoutDimension(PercentBase.BLOCK_BPD, context.getStackLimit().opt);
        fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_IPD, context.getRefIPD());
        fobj.setLayoutDimension(PercentBase.REFERENCE_AREA_BPD, context.getStackLimit().opt);

        // either works out table of column widths or if proportional-column-width function
        // is used works out total factor, so that value of single unit can be computed.
        int sumCols = 0;
        float factors = 0;
        if (columns != null) {
            for (Iterator i = columns.iterator(); i.hasNext(); ) {
                Column column = (Column) i.next();
                Length width = column.getWidth();
                sumCols += width.getValue();
                if (width instanceof TableColLength) {
                    factors += ((TableColLength) width).getTableUnits();
                }
            }
        }
        // sets TABLE_UNITS in case where one or more columns is defined using proportional-column-width
        if (sumCols < context.getRefIPD()) {
            if (fobj.getLayoutDimension(PercentBase.TABLE_UNITS).floatValue() == 0.0) {
                fobj.setLayoutDimension(PercentBase.TABLE_UNITS,
                                      (context.getRefIPD() - sumCols) / factors);
            }
        }
        MinOptMax headerSize = null;
        if (tableHeader != null) {
            tableHeader.setUserAgent(getUserAgent());
            tableHeader.resetPosition(null);
            headerBreak = getHeight(tableHeader, context);
            headerSize = headerBreak.getStackingSize();
            stackSize.add(headerSize);
        }

        MinOptMax footerSize = null;
        if (tableFooter != null) {
            tableFooter.setUserAgent(getUserAgent());
            tableFooter.resetPosition(null);
            footerBreak = getHeight(tableFooter, context);
            footerSize = footerBreak.getStackingSize();
            stackSize.add(footerSize);
{'fraction_non_alphanumeric': 0.08883732493577526, 'fraction_numerical': 0.003812049390900804, 'mean_word_length': 2.7258413090460016, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4739793', 'n_tokens_mistral': 3345, 'n_tokens_neox': 3266, 'n_words': 929}