Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed May 22, 2024
2 parents 0d84ee6 + 8529d8f commit 56eac9b
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,71 @@ public virtual void BigCellMinContentTest() {
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
));
}

[NUnit.Framework.Test]
public virtual void ColumnRowGapTest() {
String filename = DESTINATION_FOLDER + "columnRowGapTest.pdf";
String cmpName = SOURCE_FOLDER + "cmp_columnRowGapTest.pdf";
IList<UnitValue> template = new List<UnitValue>();
template.Add(new UnitValue(UnitValue.POINT, 50.0f));
template.Add(new UnitValue(UnitValue.POINT, 50.0f));
using (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
GridContainer grid = new GridContainer();
grid.SetProperty(Property.GRID_TEMPLATE_COLUMNS, template);
grid.SetProperty(Property.GRID_TEMPLATE_ROWS, template);
grid.SetProperty(Property.GRID_AUTO_ROWS, new UnitValue(UnitValue.POINT, 70.0f));
grid.SetProperty(Property.COLUMN_GAP, 20.0f);
grid.SetProperty(Property.ROW_GAP, 20.0f);
grid.Add(new Paragraph("One").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Two").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Tree").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Four").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Five").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Six").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Seven").SetBackgroundColor(ColorConstants.CYAN));
document.Add(grid);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
));
}

[NUnit.Framework.Test]
public virtual void FewBigCellsWithGapTest() {
String filename = DESTINATION_FOLDER + "fewBigCellsWithGapTest.pdf";
String cmpName = SOURCE_FOLDER + "cmp_fewBigCellsWithGapTest.pdf";
IList<UnitValue> template = new List<UnitValue>();
template.Add(new UnitValue(UnitValue.POINT, 50.0f));
template.Add(new UnitValue(UnitValue.POINT, 50.0f));
template.Add(new UnitValue(UnitValue.POINT, 50.0f));
using (Document document = new Document(new PdfDocument(new PdfWriter(filename)))) {
GridContainer grid = new GridContainer();
grid.SetProperty(Property.GRID_TEMPLATE_COLUMNS, template);
grid.SetProperty(Property.GRID_TEMPLATE_ROWS, template);
grid.SetProperty(Property.COLUMN_GAP, 10.0f);
grid.SetProperty(Property.ROW_GAP, 10.0f);
Paragraph one = new Paragraph("One").SetBackgroundColor(ColorConstants.CYAN);
one.SetProperty(Property.GRID_COLUMN_START, 1);
one.SetProperty(Property.GRID_COLUMN_END, 3);
one.SetProperty(Property.GRID_ROW_START, 1);
one.SetProperty(Property.GRID_ROW_END, 3);
grid.Add(one);
grid.Add(new Paragraph("Two").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Tree").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Four").SetBackgroundColor(ColorConstants.CYAN));
Paragraph five = new Paragraph("Five").SetBackgroundColor(ColorConstants.CYAN);
five.SetProperty(Property.GRID_COLUMN_START, 1);
five.SetProperty(Property.GRID_COLUMN_END, 4);
five.SetProperty(Property.GRID_ROW_START, 3);
five.SetProperty(Property.GRID_ROW_END, 5);
grid.Add(five);
grid.Add(new Paragraph("Six").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Seven").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Eight").SetBackgroundColor(ColorConstants.CYAN));
grid.Add(new Paragraph("Nine").SetBackgroundColor(ColorConstants.CYAN));
document.Add(grid);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpName, DESTINATION_FOLDER, "diff_"
));
}
}
}
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion itext/itext.layout/itext/layout/properties/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ public sealed class Property {

public const int ROWSPAN = 60;

public const int ROW_GAP = 153;

public const int SPACING_RATIO = 61;

public const int SPLIT_CHARACTERS = 62;
Expand Down Expand Up @@ -398,7 +400,7 @@ public sealed class Property {
/// </remarks>
private static readonly bool[] INHERITED_PROPERTIES;

private const int MAX_INHERITED_PROPERTY_ID = 152;
private const int MAX_INHERITED_PROPERTY_ID = 153;

static Property() {
INHERITED_PROPERTIES = new bool[MAX_INHERITED_PROPERTY_ID + 1];
Expand Down
16 changes: 16 additions & 0 deletions itext/itext.layout/itext/layout/renderer/AbstractRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ protected internal AbstractRenderer(IElement modelElement) {
this.modelElement = modelElement;
}

/// <summary>Creates a new renderer based on an instance of another renderer.</summary>
/// <param name="other">renderer from which to copy essential properties</param>
protected internal AbstractRenderer(iText.Layout.Renderer.AbstractRenderer other) {
this.childRenderers = other.childRenderers;
this.positionedRenderers = other.positionedRenderers;
Expand Down Expand Up @@ -462,6 +464,16 @@ public virtual void Draw(DrawContext drawContext) {
flushed = true;
}

/// <summary>
/// Apply
/// <c>Property.OPACITY</c>
/// property if specified by setting corresponding values in graphic state dictionary
/// opacity will be applied to all elements drawn after calling this method and before
/// calling
/// <see cref="EndElementOpacityApplying(DrawContext)"/>
/// ()}.
/// </summary>
/// <param name="drawContext">the context (canvas, document, etc) of this drawing operation.</param>
protected internal virtual void BeginElementOpacityApplying(DrawContext drawContext) {
float? opacity = this.GetPropertyAsFloat(Property.OPACITY);
if (opacity != null && opacity < 1f) {
Expand All @@ -471,6 +483,10 @@ protected internal virtual void BeginElementOpacityApplying(DrawContext drawCont
}
}

/// <summary>
/// <see cref="BeginElementOpacityApplying(DrawContext)"/>.
/// </summary>
/// <param name="drawContext">the context (canvas, document, etc) of this drawing operation.</param>
protected internal virtual void EndElementOpacityApplying(DrawContext drawContext) {
float? opacity = this.GetPropertyAsFloat(Property.OPACITY);
if (opacity != null && opacity < 1f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public static Border GetCollapsedBorder(Border cellBorder, Border tableBorder) {

public static IList<Border> GetCollapsedList(IList<Border> innerList, IList<Border> outerList) {
int size = Math.Min(null == innerList ? 0 : innerList.Count, null == outerList ? 0 : outerList.Count);
IList<Border> collapsedList = new List<Border>();
IList<Border> collapsedList = new List<Border>(size);
for (int i = 0; i < size; i++) {
collapsedList.Add(GetCollapsedBorder(innerList[i], outerList[i]));
}
Expand Down
24 changes: 24 additions & 0 deletions itext/itext.layout/itext/layout/renderer/DrawContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,59 @@ You should have received a copy of the GNU Affero General Public License
using iText.Kernel.Pdf.Canvas;

namespace iText.Layout.Renderer {
/// <summary>This class holds instances which required for drawing on pdf document.</summary>
public class DrawContext {
private PdfDocument document;

private PdfCanvas canvas;

private bool taggingEnabled;

/// <summary>Create drawing context by setting document and pdf canvas on which drawing will be performed.</summary>
/// <param name="document">pdf document</param>
/// <param name="canvas">canvas to draw on</param>
public DrawContext(PdfDocument document, PdfCanvas canvas)
: this(document, canvas, false) {
}

/// <summary>Create drawing context by setting document and pdf canvas on which drawing will be performed.</summary>
/// <param name="document">pdf document</param>
/// <param name="canvas">canvas to draw on</param>
/// <param name="enableTagging">if true document drawing operations will be appropriately tagged</param>
public DrawContext(PdfDocument document, PdfCanvas canvas, bool enableTagging) {
this.document = document;
this.canvas = canvas;
this.taggingEnabled = enableTagging;
}

/// <summary>Get pdf document.</summary>
/// <returns>
///
/// <c>PdfDocument</c>
/// instance
/// </returns>
public virtual PdfDocument GetDocument() {
return document;
}

/// <summary>Get pdf canvas.</summary>
/// <returns>
///
/// <c>PdfCanvas</c>
/// instance
/// </returns>
public virtual PdfCanvas GetCanvas() {
return canvas;
}

/// <summary>Get document tagging property.</summary>
/// <returns>true if tagging is enabled, false otherwise</returns>
public virtual bool IsTaggingEnabled() {
return taggingEnabled;
}

/// <summary>Set document tagging property.</summary>
/// <param name="taggingEnabled">true if to enable tagging, false to disable it</param>
public virtual void SetTaggingEnabled(bool taggingEnabled) {
this.taggingEnabled = taggingEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ private static Grid ConstructGrid(iText.Layout.Renderer.GridContainerRenderer re
)((UnitValue)renderer.GetProperty<UnitValue>(Property.GRID_AUTO_COLUMNS)).GetValue();
float? rowAutoHeight = renderer.GetProperty<UnitValue>(Property.GRID_AUTO_ROWS) == null ? null : (float?)(
(UnitValue)renderer.GetProperty<UnitValue>(Property.GRID_AUTO_ROWS)).GetValue();
float? columnGap = renderer.GetProperty<float?>(Property.COLUMN_GAP);
float? rowGap = renderer.GetProperty<float?>(Property.ROW_GAP);
//Grid Item Placement Algorithm
int initialRowsCount = templateRows == null ? 1 : templateRows.Count;
int initialColumnsCount = templateColumns == null ? 1 : templateColumns.Count;
Expand All @@ -297,7 +299,8 @@ private static Grid ConstructGrid(iText.Layout.Renderer.GridContainerRenderer re
//TODO DEVSIX-8325 eliminate null rows/columns
// for rows it's easy: grid.getCellsRows().removeIf(row -> row.stream().allMatch(cell -> cell == null));
// shrinkNullAxis(grid);
GridSizer gridSizer = new GridSizer(grid, templateRows, templateColumns, rowAutoHeight, columnAutoWidth);
GridSizer gridSizer = new GridSizer(grid, templateRows, templateColumns, rowAutoHeight, columnAutoWidth, columnGap
, rowGap);
gridSizer.SizeCells();
//calculating explicit height to ensure that even empty rows which covered by template would be considered
//TODO DEVSIX-8324 improve those methods in future for working correctly with minmax/repeat/etc.
Expand Down
41 changes: 39 additions & 2 deletions itext/itext.layout/itext/layout/renderer/GridSizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ internal class GridSizer {
//TODO DEVSIX-8326 here should be a list/map of different resolvers
private readonly GridSizer.SizeResolver sizeResolver;

private readonly float columnGap;

private readonly float rowGap;

internal GridSizer(Grid grid, IList<float> templateRows, IList<float> templateColumns, float? rowAutoHeight
, float? columnAutoWidth) {
, float? columnAutoWidth, float? columnGap, float? rowGap) {
this.grid = grid;
this.templateRows = templateRows;
this.templateColumns = templateColumns;
this.rowAutoHeight = rowAutoHeight;
this.columnAutoWidth = columnAutoWidth;
this.sizeResolver = new GridSizer.MinContentResolver(grid);
this.columnGap = columnGap == null ? 0.0f : (float)columnGap;
this.rowGap = rowGap == null ? 0.0f : (float)rowGap;
}

//Grid Sizing Algorithm
Expand Down Expand Up @@ -86,7 +92,7 @@ private float CalculateCellY(GridCell cell) {
//is a null row) and all cells in a row above have the same top.
GridCell topNeighbor = grid.GetClosestTopNeighbor(cell);
if (topNeighbor != null) {
return topNeighbor.GetLayoutArea().GetTop();
return topNeighbor.GetLayoutArea().GetTop() + rowGap;
}
return 0.0f;
}
Expand All @@ -100,6 +106,7 @@ private float CalculateCellX(GridCell cell) {
if (templateColumns != null) {
for (; currentColumn < Math.Min(templateColumns.Count, cell.GetColumnStart()); ++currentColumn) {
x += (float)templateColumns[currentColumn];
x += columnGap;
}
if (currentColumn == cell.GetColumnStart()) {
return x;
Expand All @@ -108,6 +115,7 @@ private float CalculateCellX(GridCell cell) {
if (columnAutoWidth != null) {
for (; currentColumn < cell.GetColumnStart(); ++currentColumn) {
x += (float)columnAutoWidth;
x += columnGap;
}
return x;
}
Expand All @@ -120,6 +128,7 @@ private float CalculateCellX(GridCell cell) {
else {
x = leftNeighbor.GetLayoutArea().GetRight();
}
x += columnGap;
}
return x;
}
Expand Down Expand Up @@ -148,6 +157,9 @@ private float CalculateCellHeight(GridCell cell) {
}
}
}
if (counter > 1) {
cellHeight += rowGap * (counter - 1);
}
if (counter == cell.GetGridHeight()) {
return cellHeight;
}
Expand Down Expand Up @@ -184,6 +196,9 @@ private float CalculateCellWidth(GridCell cell) {
}
}
}
if (counter > 1) {
cellWidth += columnGap * (counter - 1);
}
if (counter == cell.GetGridWidth()) {
return cellWidth;
}
Expand All @@ -202,9 +217,22 @@ private float CalculateCellWidth(GridCell cell) {
return cellWidth;
}

/// <summary>
/// The
/// <c>SizeResolver</c>
/// is used to calculate cell width and height on layout area.
/// </summary>
protected internal abstract class SizeResolver {
protected internal Grid grid;

/// <summary>
/// Create a new
/// <c>SizeResolver</c>
/// instance for the given
/// <c>Grid</c>
/// instance.
/// </summary>
/// <param name="grid">grid which cells sizes will be resolved</param>
public SizeResolver(Grid grid) {
this.grid = grid;
}
Expand Down Expand Up @@ -249,11 +277,19 @@ protected internal virtual float CalculateMinRequiredCellWidth(GridCell cell) {
}
}

/// <summary>
/// The
/// <c>MinContentResolver</c>
/// is used to calculate cell width and height on layout area by calculating their
/// min required size.
/// </summary>
protected internal class MinContentResolver : GridSizer.SizeResolver {
/// <summary><inheritDoc/></summary>
public MinContentResolver(Grid grid)
: base(grid) {
}

/// <summary><inheritDoc/></summary>
public override float ResolveHeight(GridCell cell, float cellHeight) {
float maxRowTop = grid.GetMaxRowTop(cell.GetRowStart(), cell.GetColumnStart());
cellHeight = Math.Max(cellHeight, CalculateImplicitCellHeight(cell));
Expand All @@ -266,6 +302,7 @@ public override float ResolveHeight(GridCell cell, float cellHeight) {
return cellHeight;
}

/// <summary><inheritDoc/></summary>
public override float ResolveWidth(GridCell cell, float cellWidth) {
float maxColumnRight = grid.GetMaxColumnRight(cell.GetRowStart(), cell.GetColumnStart());
cellWidth = Math.Max(cellWidth, CalculateMinRequiredCellWidth(cell));
Expand Down
6 changes: 5 additions & 1 deletion itext/itext.layout/itext/layout/renderer/TableBorderUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public static IList<Border> CreateAndFillBorderList(Border border, int size) {

public static IList<Border> CreateAndFillBorderList(IList<Border> originalList, Border borderToCollapse, int
size) {
IList<Border> borderList = new List<Border>();
IList<Border> borderList;
if (null != originalList) {
borderList = new List<Border>(originalList.Count + size);
borderList.AddAll(originalList);
}
else {
borderList = new List<Border>(size);
}
while (borderList.Count < size) {
borderList.Add(borderToCollapse);
}
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e5aea995f8d5d2db4aafbc0109c6889318f57966
15951163c968bb91b805b915388550f2e4296bcf

0 comments on commit 56eac9b

Please sign in to comment.