Skip to content

Commit

Permalink
Added height to MRow.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Aug 30, 2024
1 parent 7b07314 commit 6f21059
Show file tree
Hide file tree
Showing 34 changed files with 368 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.obeonetwork.m2doc.element.MParagraph;
import org.obeonetwork.m2doc.element.MStyle;
import org.obeonetwork.m2doc.element.MTable.MCell;
import org.obeonetwork.m2doc.element.MTable.MRow;
import org.obeonetwork.m2doc.element.impl.MBorderImpl;
import org.obeonetwork.m2doc.html.services.M2DocHTMLParser.Context;

Expand Down Expand Up @@ -181,6 +182,11 @@ public class M2DocCSSParser extends Parser {
*/
private static final String CSS_WIDTH = WIDTH;

/**
* The width property.
*/
private static final String CSS_HEIGHT = HEIGHT;

/**
* The CSS dot class separator.
*/
Expand Down Expand Up @@ -256,6 +262,11 @@ public class M2DocCSSParser extends Parser {
*/
private static final int CSS_FONT_WEIGHT_BOLD_THRESHOLD = 700;

/**
* Regular expression spaces match.
*/
private static final String REG_EXP_SPACES = "\\s+";

/**
* The style attribute.
*/
Expand All @@ -274,7 +285,7 @@ public Map<String, Map<String, List<String>>> parseClasses(String cssClasses) {
final Matcher matcher = CSS_CLASS_PATTERN.matcher(cssClasses);
while (matcher.find()) {
final String classNames = matcher.group(CSS_CLASS_PATTERN_NAME_GROUP);
for (String className : classNames.split("\\s+")) {
for (String className : classNames.split(REG_EXP_SPACES)) {
final Map<String, List<String>> styles = parseStyles(matcher.group(CSS_CLASS_PATTERN_CSS_STYLES_GROUP));
res.computeIfAbsent(className, n -> new LinkedHashMap<String, List<String>>()).putAll(styles);
}
Expand Down Expand Up @@ -310,7 +321,7 @@ public List<String> getCSSClassNames(Node node) {

res.add(node.nodeName());
if (node.hasAttr(CLASS_ATTR)) {
for (String className : node.attr(CLASS_ATTR).split("\\s+")) {
for (String className : node.attr(CLASS_ATTR).split(REG_EXP_SPACES)) {
res.add(CSS_CLASS_DOT + className);
res.add(VALUE_SEPARATOR + className);
res.add(node.nodeName() + CSS_CLASS_DOT + className);
Expand Down Expand Up @@ -543,6 +554,23 @@ public void setStyle(Map<String, List<String>> cssProperties, MCell mCell) {
setContainerStyle(cssProperties, mCell);
}

/**
* Sets the CSS styles to the given {@link MRow}.
*
* @param cssProperties
* the CSS properties
* @param mRow
* the {@link MRow}
*/
public void setStyle(Map<String, List<String>> cssProperties, MRow mRow) {
final List<String> cssHeights = cssProperties.get(CSS_HEIGHT);
if (cssHeights != null) {
for (String cssHeight : cssHeights) {
setRowHeight(mRow, cssHeight);
}
}
}

/**
* Sets the CSS styles to the given {@link MElementContainer}.
*
Expand Down Expand Up @@ -637,7 +665,7 @@ private void setBorderStyles(Context context, MParagraph paragraph) {
final List<String> cssBorderStyles = context.getCssProperties().get(CSS_BORDER_STYLE);
if (cssBorderStyles != null) {
for (String cssBorderStyle : cssBorderStyles) {
final String[] borderStyles = cssBorderStyle.split("\\s+");
final String[] borderStyles = cssBorderStyle.split(REG_EXP_SPACES);
if (borderStyles.length == 1) {
final Type type = getBorderType(borderStyles[0]);
final MBorder leftBorder = new MBorderImpl();
Expand Down Expand Up @@ -686,7 +714,7 @@ private void setBorders(Context context, MParagraph paragraph) {
final List<String> cssBorders = context.getCssProperties().get(CSS_BORDER);
if (cssBorders != null) {
for (String cssBorder : cssBorders) {
final String[] borders = cssBorder.split("\\s+");
final String[] borders = cssBorder.split(REG_EXP_SPACES);
if (borders.length == 1) {
final Type type = getBorderType(borders[0]);
final MBorder leftBorder = new MBorderImpl();
Expand Down Expand Up @@ -795,7 +823,7 @@ private void setMarginAll(Context context, MParagraph paragraph) {
final List<String> cssMargins = context.getCssProperties().get(CSS_MARGIN);
if (cssMargins != null) {
for (String cssMargin : cssMargins) {
final String[] margins = cssMargin.split("\\s+");
final String[] margins = cssMargin.split(REG_EXP_SPACES);
final int marginLeft;
final int marginRight;
final int marginTop;
Expand Down Expand Up @@ -889,7 +917,7 @@ private void setPaddingAll(Context context, MParagraph paragraph) {
final List<String> cssPaddings = context.getCssProperties().get(CSS_PADDING);
if (cssPaddings != null) {
for (String cssPadding : cssPaddings) {
final String[] paddings = cssPadding.split("\\s+");
final String[] paddings = cssPadding.split(REG_EXP_SPACES);
final int paddingLeft;
final int paddingRight;
final int paddingTop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class M2DocHTMLParser extends Parser {
/**
* The height attribute.
*/
private static final String HEIGHT_ATTR = "height";
private static final String HEIGHT_ATTR = HEIGHT;

/**
* The width attribute.
Expand Down Expand Up @@ -965,22 +965,28 @@ private void insertTable(MParagraph parent, Context context, Node header, Node b
if ("tr".equals(child.nodeName())) {
final MRow row = new MRowImpl();
table.getRows().add(row);
if (child.hasAttr(HEIGHT_ATTR)) {
setRowHeight(row, child.attr(HEIGHT_ATTR));
}
final Context localRowContext = context.copy();
applyGlobalAttibutes(localRowContext, child);
CSS_PARSER.setStyle(localRowContext.cssProperties, row);
for (Node rowChild : child.childNodes()) {
if ("th".equals(rowChild.nodeName()) || "td".equals(rowChild.nodeName())) {
final MList contents = new MListImpl();
final MParagraph newParagraph = new MParagraphImpl(contents, null);
final MCell cell = new MCellImpl(contents, null);
final Context localContext = context.copy();
applyGlobalAttibutes(localContext, rowChild);
final Context localCellContext = context.copy();
applyGlobalAttibutes(localCellContext, rowChild);
if ("th".equals(rowChild.nodeName())) {
cell.setHAlignment(HAlignment.CENTER);
setModifiers(localContext.style, MStyle.FONT_BOLD);
setModifiers(localCellContext.style, MStyle.FONT_BOLD);
}
if (rowChild.hasAttr(WIDTH_ATTR)) {
setCellWidth(cell, rowChild.attr(WIDTH_ATTR));
}
CSS_PARSER.setStyle(localContext.cssProperties, cell);
walkChildren(rowChild, localContext, newParagraph);
CSS_PARSER.setStyle(localCellContext.cssProperties, cell);
walkChildren(rowChild, localCellContext, newParagraph);
createNeededParagraphes(newParagraph);
insertMergedCells(row, rowChild, cell, rowSpans, vMergeCopies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.obeonetwork.m2doc.element.MStyle;
import org.obeonetwork.m2doc.element.MTable.MCell;
import org.obeonetwork.m2doc.element.MTable.MCell.WidthType;
import org.obeonetwork.m2doc.element.MTable.MRow;
import org.obeonetwork.m2doc.element.MTable.MRow.HeightRule;

/**
* Abstract parser class for utility methods.
Expand Down Expand Up @@ -49,6 +51,11 @@ public abstract class Parser {
*/
protected static final String WIDTH = "width";

/**
* The height attribute or CSS property.
*/
protected static final String HEIGHT = "height";

/**
* The % string.
*/
Expand Down Expand Up @@ -504,4 +511,24 @@ protected void setCellWidth(MCell mCell, String width) {
}
}

/**
* Sets the given {@link MRow} to the given height.
*
* @param row
* the {@link MRow}
* @param height
* the height
*/
protected void setRowHeight(MRow row, String height) {
final int relativeHeight = getRelativeSize(height);
if (relativeHeight == -1) {
double pixels = (double) getPixels(height);
if (pixels == -1) {
pixels = Integer.valueOf(height);
}
row.setHeight((int) pixels * 10);
row.setHeightRule(HeightRule.AT_LEAST);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,65 @@ enum MTableAlign {
* @author ldelaigue
*/
public interface MRow {

/**
* The height rule.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
enum HeightRule {
/**
* Auto height.
*/
AUTO,
/**
* Excact height.
*/
EXACT,
/**
* At least height.
*/
AT_LEAST;
}

/**
* The row's defined cells (i.e. non-empty cells). A row may have no cell at all, or may not have a cell for each column of its
* table.
*
* @return The row's defined cells.
*/
List<MCell> getCells();

/**
* Gets the height.
*
* @return the height if any, <code>-1</code> otherwise
*/
int getHeight();

/**
* Set the height.
*
* @param height
* the height, <code>-1</code> for default
*/
void setHeight(int height);

/**
* Gets the {@link HeightRule}.
*
* @return the {@link HeightRule} if any, <code>null</code> otherwise
*/
HeightRule getHeightRule();

/**
* Sets the {@link HeightRule}.
*
* @param rule
* the {@link HeightRule}
*/
void setHeightRule(HeightRule rule);

}

/**
Expand Down Expand Up @@ -243,7 +295,7 @@ enum WidthType {
/**
* Gets the width.
*
* @return the width
* @return the width, <code>-1</code> otherwise
*/
int getWitdh();

Expand All @@ -258,7 +310,7 @@ enum WidthType {
/**
* Gets the {@link WidthType}.
*
* @return the {@link WidthType} if any, <code>-1</code> otherwise
* @return the {@link WidthType} if any, <code>null</code> otherwise
*/
WidthType getWidthType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public class MTableImpl implements MTable {
*/
public static class MRowImpl implements MRow {

/**
* The height.
*/
private int height = -1;

/**
* The {@link HeightRule}.
*/
private HeightRule heightRule;

/**
* The {@link List} of {@link MCell}.
*/
Expand All @@ -41,6 +51,27 @@ public static class MRowImpl implements MRow {
public List<MCell> getCells() {
return cells;
}

@Override
public int getHeight() {
return height;
}

@Override
public void setHeight(int height) {
this.height = height;

}

@Override
public HeightRule getHeightRule() {
return heightRule;
}

@Override
public void setHeightRule(HeightRule rule) {
this.heightRule = rule;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.poi.xwpf.usermodel.IRunBody;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TableRowAlign;
import org.apache.poi.xwpf.usermodel.TableRowHeightRule;
import org.apache.poi.xwpf.usermodel.TableWidthType;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
Expand Down Expand Up @@ -1408,13 +1409,13 @@ private void fillTable(XWPFTable xwpfTable, MTable mTable) {
}

// Iterate over the rows
for (int row = 0; row < mTable.getRows().size(); row++) {
final MRow mRow = mTable.getRows().get(row);
for (MRow mRow : mTable.getRows()) {
final XWPFTableRow xwpfRow = xwpfTable.createRow();
while (!xwpfRow.getTableCells().isEmpty()) {
xwpfRow.removeCell(0);
}
xwpfRow.getCtRow().getTcList().clear();
setRowHeight(xwpfRow, mRow);

// Iterate over the columns
for (int column = 0; column < mRow.getCells().size(); column++) {
Expand All @@ -1435,7 +1436,42 @@ private void fillTable(XWPFTable xwpfTable, MTable mTable) {
&& xwpfCell.getCTTc().getTcPr().isSetTcBorders()) {
xwpfCell.getCTTc().getTcPr().unsetTcBorders();
}
}
}
}

/**
* Sets the for the given row.
*
* @param xwpfRow
* the {@link XWPFTableRow}
* @param mRow
* the {@link MRow}
*/
private void setRowHeight(XWPFTableRow xwpfRow, MRow mRow) {
if (mRow != null) {
if (mRow.getHeightRule() != null) {
final TableRowHeightRule rule;
switch (mRow.getHeightRule()) {
case AUTO:
rule = TableRowHeightRule.AUTO;
break;

case EXACT:
rule = TableRowHeightRule.AT_LEAST;
break;

case AT_LEAST:
rule = TableRowHeightRule.AT_LEAST;
break;

default:
throw new IllegalStateException();
}
xwpfRow.setHeightRule(rule);
}
if (mRow.getHeight() != -1) {
xwpfRow.setHeight(mRow.getHeight());
}
}
}
Expand Down
Loading

0 comments on commit 6f21059

Please sign in to comment.