private int addCellToCellMatrix()

in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/TableSimplifier.java [490:561]


    private int addCellToCellMatrix(int rowSpan, int colSpan)
            throws DocgenException {
        // Find the coordinates of the first free cell in the current row:
        int curIdx = cellMatrixCurRow * cellMatrixWidth + cellMatrixCurCol;
        if (cellMatrixCurRow < cellMatrixHeight) {
            int curIdxLim = (cellMatrixCurRow + 1) * cellMatrixWidth;
            while (curIdx < curIdxLim && cellMatrix[curIdx]) {
                curIdx++;
                cellMatrixCurCol++;
            }
        }
        
        // Draw a rectangle of rowSpan*colSpan there:
        
        // - Ensure that the backing array has the required capacity:
        int newCellMatrixHeight = cellMatrixHeight;
        int newCellMatrixWidth = cellMatrixWidth;
        if (cellMatrixHeight <= cellMatrixCurRow + rowSpan - 1) {
            newCellMatrixHeight = (cellMatrixCurRow + rowSpan) * 2;
        }
        if (cellMatrixWidth <= cellMatrixCurCol + colSpan - 1) {
            newCellMatrixWidth = (cellMatrixCurCol + colSpan) * 2;
        }
        if (newCellMatrixHeight != cellMatrixHeight
                || newCellMatrixWidth != cellMatrixWidth) {
            // Resize the backing array...
            boolean[] newCellMatrix = new boolean[
                    newCellMatrixHeight * newCellMatrixWidth];
            for (int row = 0; row < cellMatrixHeight; row++) {
                System.arraycopy(
                        cellMatrix, row * cellMatrixWidth,
                        newCellMatrix, row * newCellMatrixWidth,
                        cellMatrixWidth);
            }
            cellMatrix = newCellMatrix;
            cellMatrixWidth = newCellMatrixWidth;
            cellMatrixHeight = newCellMatrixHeight;
            
            // Re-calculation needed as width maybe changed
            curIdx = cellMatrixCurRow * cellMatrixWidth + cellMatrixCurCol; 
        }
        
        // - "Pain" the rectangle:
        for (int relRow = 0; relRow < rowSpan; relRow++) {
            int brushIdx = curIdx + relRow * cellMatrixWidth;
            for (int relCol = 0; relCol < colSpan; relCol++) {
                if (cellMatrix[brushIdx]) {
                    throw new DocgenException(
                            XMLUtil.theSomethingElement(table, true)
                            + " has overlapping cells; check if \""
                            + A_ROWSPAN + "\"-s and/or \"" + A_COLSPAN
                            + "\"-s are correct.");
                }
                cellMatrix[brushIdx++] = true;
            }
        }
        
        /*
        // For debugging: Prints the current cell layout to the stdout
        System.out.println(
                "(" + cellMatrixWidth + "x" + cellMatrixHeight + ")");
        for (int row = 0; row < cellMatrixHeight; row++) {
            for (int col = 0; col < cellMatrixWidth; col++) {
                System.out.print(
                        cellMatrix[row * cellMatrixWidth + col] ? "X" : ".");
            }
            System.out.println();
        }
        */
    
        return cellMatrixCurCol;
    }