Skip to content

Commit

Permalink
Enhance the excel output to get the formatting option of text-indent (#…
Browse files Browse the repository at this point in the history
…1667)

* Enhance the excel output to get the formatting option of text indent (#1666)
* Add a new option to define the indent mode for the internal calculation
  • Loading branch information
speckyspooky authored May 7, 2024
1 parent dca1895 commit 71ce2d9
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class BirtStyle {
private IStyle elemStyle;
private CSSValue[] propertyOverride = new CSSValue[BirtStyle.NUMBER_OF_STYLES];
private CSSEngine cssEngine;
private boolean useTextIndent = true;
private String textIndentMode = "";

/**
* Constructor 01
Expand Down Expand Up @@ -219,6 +221,10 @@ protected BirtStyle clone() {

private static BitSet PrepareSpecialOverlayProperties() {
BitSet result = new BitSet(BirtStyle.NUMBER_OF_STYLES);
result.set(StyleConstants.STYLE_MARGIN_LEFT);
result.set(StyleConstants.STYLE_MARGIN_RIGHT);
result.set(StyleConstants.STYLE_PADDING_LEFT);
result.set(StyleConstants.STYLE_PADDING_RIGHT);
result.set(StyleConstants.STYLE_BACKGROUND_COLOR);
result.set(StyleConstants.STYLE_BORDER_BOTTOM_STYLE);
result.set(StyleConstants.STYLE_BORDER_BOTTOM_WIDTH);
Expand Down Expand Up @@ -337,4 +343,39 @@ public String toString() {
return result.toString();
}

/**
* Set the flag if the text indent is in use
*
* @param useTextIndent flag to define is the text indent is in use
*/
public void setTextIndentInUse(boolean useTextIndent) {
this.useTextIndent = useTextIndent;
}

/**
* Get the result if the text indent is in use
*
* @return Return true if the text indent is in use
*/
public boolean isTextIndentInUse() {
return this.useTextIndent;
}

/**
* Set the text indent mode
*
* @param textIndentMode type of the text indent mode
*/
public void setTextIndentMode(String textIndentMode) {
this.textIndentMode = textIndentMode;
}

/**
* Get the text indent mode
*
* @return Return the text indent mode
*/
public String getTextIndentMode() {
return this.textIndentMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright (c) 2011, 2012, 2013 James Talbut.
* [email protected]
*
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* James Talbut - Initial implementation.
************************************************************************************/
Expand Down Expand Up @@ -40,6 +40,7 @@ public class ClientAnchorConversions {
private static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
private static final int UNIT_OFFSET_LENGTH = 7;
private static final int[] UNIT_OFFSET_MAP = { 0, 36, 73, 109, 146, 182, 219 };
private static final double MILLIMETRE_INDENT_FACTOR = 2.38;

/**
* Convert a measure in column width units (1/256th of a character) to a measure
Expand Down Expand Up @@ -69,6 +70,17 @@ public static int millimetres2WidthUnits(double millimetres) {
return widthUnits;
}

/**
* Convert a measure of millimetres to indent units.
*
* @param millimetres The size in millimetres.
* @return The size in indent units.
*/
public static int millimetres2IndentUnits(double millimetres) {
int indentUnits = (int) Math.round(millimetres / MILLIMETRE_INDENT_FACTOR);
return indentUnits;
}

/**
* Convert a measure of pixels to millimetres (for column widths).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public abstract class ExcelEmitter implements IContentEmitter {
/** property: ExcelEmitter.PagePreview */
public static final String PAGE_PREVIEW = "ExcelEmitter.PagePreview";

/** property: ExcelEmitter.DisplayTextIndent */
public static final String DISPLAY_TEXT_INDENT = "ExcelEmitter.DisplayTextIndent";

/** property: ExcelEmitter.TextIndentMode */
public static final String TEXT_INDENT_MODE = "ExcelEmitter.TextIndentMode";

/** property: minimum zoom value of excel sheet */
public static final short poiExcelDisplaySheetZoomScaleMin = 10;

Expand All @@ -190,6 +196,15 @@ public abstract class ExcelEmitter implements IContentEmitter {
/** property: minimum zoom value of excel sheet */
public static final String poiExcelPreviewPageBreak = "PageBreak";

/** property: text indent mode, spacing based on cell */
public static final String TEXT_INDENT_MODE_SPACING_CELL = "SpacingCell";

/** property: text indent mode, spacing based on element */
public static final String TEXT_INDENT_MODE_SPACING_ELEMENT = "SpacingElement";

/** property: text indent mode, spacing based on cell & element */
public static final String TEXT_INDENT_MODE_SPACING_ALL = "SpacingAll";

/**
* Logger.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,23 +222,6 @@ The following list get an overview of all supported user properties, the content
false, hide cell value of zero
Default true

**ExcelEmitter.ValueAsFormula**

Content the content on an element will be added as excel-formula, for calculation teh string must be start with "="-sign
Location text-element of cell
Data type boolean
Values true, use content like formula
false, use content like text
Default false

**ExcelEmitter.Formula**

Content the content of this property will be added as excel-formula, for calculation teh string must be start with "="-sign
Location text-element of cell
Data type string
Values fixed text or formula for excel cell
Default empty (null)

**ExcelEmitter.TemplateFile**

Content define an excel-template to create the excel
Expand All @@ -255,6 +238,26 @@ The following list get an overview of all supported user properties, the content
Values true, activate the streaming output method
false, standard output handling based on DOM will be used
Default false
Since 4.13

**ExcelEmitter.ValueAsFormula**

Content the content on an element will be added as excel-formula, for calculation the string must be start with "="-sign
Location text-element of cell
Data type boolean
Values true, use content like formula
false, use content like text
Default false
Since 4.16

**ExcelEmitter.Formula**

Content the content of this property will be added as excel-formula, for calculation the string must be start with "="-sign
Location text-element of cell
Data type string
Values fixed text or formula for excel cell
Default empty (null)
Since 4.16

**ExcelEmitter.ForceRecalculation**

Expand All @@ -264,6 +267,7 @@ The following list get an overview of all supported user properties, the content
Values true, activate the recalculation option
false, no recalculation
Default false
Since 4.16

**ExcelEmitter.PrintGridlines**

Expand All @@ -273,6 +277,7 @@ The following list get an overview of all supported user properties, the content
Values true, activate grid line print out
false, without grid line print out
Default false
Since 4.16

**ExcelEmitter.PrintRowColHeadings**

Expand All @@ -282,7 +287,8 @@ The following list get an overview of all supported user properties, the content
Values true, activate print out of the row and column heading labels
false, without print out of the row and column heading labels
Default false

Since 4.16

**ExcelEmitter.PrintFitToPage**

Content the page will be fit to center of the print out
Expand All @@ -291,6 +297,7 @@ The following list get an overview of all supported user properties, the content
Values true, fit the page to center
false, no fitting of the page
Default false
Since 4.16

**ExcelEmitter.DisplaySheetZoom**

Expand All @@ -299,6 +306,7 @@ The following list get an overview of all supported user properties, the content
Data type number, integer
Values the zoom value must be -1 (= off) or between 10 and 400
Default -1
Since 4.16

**ExcelEmitter.PagePreview**

Expand All @@ -308,3 +316,28 @@ The following list get an overview of all supported user properties, the content
Values PageLayout, display the page layout preview
PageBreak, display the page break preview
Default Normal
Since 4.16

**ExcelEmitter.DisplayTextIndent**

Content define if the text-indent will be used based on the margin & padding of the text-element, the indent is dependent at the text-alignment:
- alignment left: use the left-margin and left-padding
- alignment right: use the right-margin and right-padding
Location report, table, grid, list, row, text-element
Data type boolean
Values true, text-indent will be used
false, avoid the usage of the text indent
Default true
Since 4.16


**ExcelEmitter.TextIndentMode**

Content define the indent calculation mode and with it the element spacing which will be used
Location report, table, grid, list, row, text-element
Data type string
Values SpacingCell, indent calculated based on cell padding
SpacingElement, indent calculated based on element padding & margin
SpacingAll, indent calculated based on cell padding & element padding & margin
Default SpacingAll
Since 4.16
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.birt.report.engine.css.engine.value.DataFormatValue;
import org.eclipse.birt.report.engine.css.engine.value.FloatValue;
import org.eclipse.birt.report.engine.css.engine.value.css.CSSConstants;
import org.eclipse.birt.report.engine.ir.DimensionType;
import org.w3c.dom.css.CSSValue;

import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
Expand Down Expand Up @@ -114,7 +115,8 @@ public CSSEngine getCssEngine() {
StyleConstants.STYLE_BORDER_DIAGONAL_WIDTH, StyleConstants.STYLE_BORDER_DIAGONAL_COLOR,
StyleConstants.STYLE_BORDER_ANTIDIAGONAL_NUMBER, StyleConstants.STYLE_BORDER_ANTIDIAGONAL_STYLE,
StyleConstants.STYLE_BORDER_ANTIDIAGONAL_WIDTH, StyleConstants.STYLE_BORDER_ANTIDIAGONAL_COLOR,
StyleConstants.STYLE_WHITE_SPACE, StyleConstants.STYLE_VERTICAL_ALIGN, };
StyleConstants.STYLE_WHITE_SPACE, StyleConstants.STYLE_VERTICAL_ALIGN, StyleConstants.STYLE_MARGIN_LEFT,
StyleConstants.STYLE_MARGIN_RIGHT, StyleConstants.STYLE_PADDING_LEFT, StyleConstants.STYLE_PADDING_RIGHT, };

/**
* Test whether two BIRT styles are equivalent, as far as the attributes
Expand Down Expand Up @@ -146,9 +148,11 @@ private boolean stylesEquivalent(BirtStyle style1, BirtStyle style2) {
// Number format
// Font
if (!StyleManagerUtils.objectsEqual(style1.getProperty(BirtStyle.TEXT_ROTATION),
style2.getProperty(BirtStyle.TEXT_ROTATION)) || !StyleManagerUtils.dataFormatsEquivalent(
style2.getProperty(BirtStyle.TEXT_ROTATION))
|| !StyleManagerUtils.dataFormatsEquivalent(
(DataFormatValue) style1.getProperty(StyleConstants.STYLE_DATA_FORMAT),
(DataFormatValue) style2.getProperty(StyleConstants.STYLE_DATA_FORMAT)) || !FontManager.fontsEquivalent(style1, style2)) {
(DataFormatValue) style2.getProperty(StyleConstants.STYLE_DATA_FORMAT))
|| !FontManager.fontsEquivalent(style1, style2)) {
// System.out.println( "Differ on font" );
return false;
}
Expand Down Expand Up @@ -177,6 +181,38 @@ private CellStyle createStyle(BirtStyle birtStyle) {
// Background colour
smu.addBackgroundColourToStyle(workbook, poiStyle, birtStyle.getString(StyleConstants.STYLE_BACKGROUND_COLOR));

// Indent, based on the text margin & padding
// excel supports for indent the text-align LEFT / RIGHT
if (birtStyle.isTextIndentInUse() && alignment != null
&& !(alignment.equals(HorizontalAlignment.CENTER)
|| alignment.equals(HorizontalAlignment.CENTER_SELECTION))) {
short indent = 0;
CSSValue marginCSSValue;
CSSValue paddingCSSValue;
String marginCSS = "";
String paddingCSS = "";
if (alignment.equals(HorizontalAlignment.RIGHT)) {
marginCSSValue = birtStyle.getProperty(StyleConstants.STYLE_MARGIN_RIGHT);
paddingCSSValue = birtStyle.getProperty(StyleConstants.STYLE_PADDING_RIGHT);
} else {
marginCSSValue = birtStyle.getProperty(StyleConstants.STYLE_MARGIN_LEFT);
paddingCSSValue = birtStyle.getProperty(StyleConstants.STYLE_PADDING_LEFT);
}
marginCSS = (marginCSSValue != null) ? marginCSSValue.getCssText() : "0";
paddingCSS = (paddingCSSValue != null) ? paddingCSSValue.getCssText() : "0";
double marginIndent = smu.convertDimensionToMillimetres(DimensionType.parserUnit(marginCSS));
double paddingIndent = smu.convertDimensionToMillimetres(DimensionType.parserUnit(paddingCSS));
int tmpIndent = smu.poiIndentUnit(new DimensionType(marginIndent + paddingIndent, DimensionType.UNITS_MM));
if (tmpIndent < 0) {
indent = 0;
} else if (tmpIndent > Short.MAX_VALUE) {
indent = Short.MAX_VALUE;
} else {
indent = (short) tmpIndent;
}
poiStyle.setIndention(indent);
}

if (smu instanceof StyleManagerXUtils) {

// border will be applied at once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,52 @@ public int poiColumnWidthFromDimension(DimensionType dim) {
return 0;
}

/**
* Calculation of millimetres based on dimension
*
* @param dim The BIRT dimension, which must be in absolute units.
* @return The calculated millimetres unit
*/
public double convertDimensionToMillimetres(DimensionType dim) {
if (dim != null) {
double mmWidth = dim.getMeasure();
if ((DimensionType.UNITS_CM.equals(dim.getUnits())) || (DimensionType.UNITS_IN.equals(dim.getUnits()))
|| (DimensionType.UNITS_PT.equals(dim.getUnits()))
|| (DimensionType.UNITS_PC.equals(dim.getUnits()))) {
mmWidth = dim.convertTo("mm");
} else if ((DimensionType.UNITS_PX.equals(dim.getUnits()))) {
mmWidth = ClientAnchorConversions.pixels2Millimetres(mmWidth);
}
return mmWidth;
}
return 0;
}

/**
* Converting from millimetres to indent width
*
* @param mmWidth The BIRT millimetres
* @return The calculated indent with unit
*/
public int poiIndentUnit(double mmWidth) {
return ClientAnchorConversions.millimetres2IndentUnits(mmWidth);
}

/**
* Converting from millimetres to indent width
*
* @param dim The BIRT dimension millimetres
* @return The calculated indent with unit
*/
public int poiIndentUnit(DimensionType dim) {
if (dim != null) {
double mmWidth = dim.getMeasure();
if (DimensionType.UNITS_MM.equals(dim.getUnits())) {
return ClientAnchorConversions.millimetres2IndentUnits(mmWidth);
}
}
return 0;
}
/**
* Object a POI font weight from a BIRT string.
*
Expand Down
Loading

0 comments on commit 71ce2d9

Please sign in to comment.