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 Jan 1, 2025
2 parents 57e9347 + 5655d6a commit dea09da
Show file tree
Hide file tree
Showing 47 changed files with 582 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ You should have received a copy of the GNU Affero General Public License
namespace iText.Layout.Properties {
[NUnit.Framework.Category("IntegrationTest")]
public class BackgroundImageTest : ExtendedITextTest {
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
private const float DELTA = 0.0001f;

private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/layout/BackgroundImageTest/";

public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/layout/BackgroundImageTest/";

[NUnit.Framework.OneTimeSetUp]
Expand Down Expand Up @@ -623,6 +625,58 @@ public virtual void BackgroundImageWithLinearGradientAndLuminosityBlendModeTest(
BlendModeTest(BlendMode.LUMINOSITY);
}

[NUnit.Framework.Test]
public virtual void CalculateImageSizeTest() {
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
));
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
Build();
float[] widthAndHeight = backgroundImage.CalculateBackgroundImageSize(200f, 300f);
iText.Test.TestUtil.AreEqual(new float[] { 45f, 45f }, widthAndHeight, DELTA);
}

[NUnit.Framework.Test]
public virtual void CalculateImageSizeWithCoverPropertyTest() {
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
));
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
Build();
backgroundImage.GetBackgroundSize().SetBackgroundSizeToCover();
float[] widthAndHeight = backgroundImage.CalculateBackgroundImageSize(200f, 300f);
iText.Test.TestUtil.AreEqual(new float[] { 300f, 300f }, widthAndHeight, DELTA);
}

[NUnit.Framework.Test]
public virtual void CalculateSizeWithContainPropertyTest() {
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "pattern-grg-rrg-rgg.png"
));
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
Build();
backgroundImage.GetBackgroundSize().SetBackgroundSizeToContain();
float[] widthAndHeight = backgroundImage.CalculateBackgroundImageSize(200f, 300f);
iText.Test.TestUtil.AreEqual(new float[] { 200f, 200.000015f }, widthAndHeight, DELTA);
}

[NUnit.Framework.Test]
public virtual void CalculateSizeWithContainAndImageWeightMoreThatHeightTest() {
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
Build();
backgroundImage.GetBackgroundSize().SetBackgroundSizeToContain();
float[] widthAndHeight = backgroundImage.CalculateBackgroundImageSize(200f, 300f);
iText.Test.TestUtil.AreEqual(new float[] { 200f, 112.5f }, widthAndHeight, DELTA);
}

[NUnit.Framework.Test]
public virtual void CalculateSizeWithCoverAndImageWeightMoreThatHeightTest() {
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "itis.jpg"));
iText.Layout.Properties.BackgroundImage backgroundImage = new BackgroundImage.Builder().SetImage(xObject).
Build();
backgroundImage.GetBackgroundSize().SetBackgroundSizeToCover();
float[] widthAndHeight = backgroundImage.CalculateBackgroundImageSize(200f, 300f);
iText.Test.TestUtil.AreEqual(new float[] { 533.3333f, 300f }, widthAndHeight, DELTA);
}

private void BlendModeTest(BlendMode blendMode) {
AbstractLinearGradientBuilder gradientBuilder = new StrategyBasedLinearGradientBuilder().AddColorStop(new
GradientColorStop(ColorConstants.BLACK.GetColorValue())).AddColorStop(new GradientColorStop(ColorConstants
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public virtual void SvgWidthAndHeightEmAndRemTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "svgWidthAndHeightEmAndRemTest");
}

[NUnit.Framework.Test]
public virtual void SvgRelativeSizeWithViewBox1() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "svgRelativeSizeWithViewBox1");
}

//-------------- use
[NUnit.Framework.Test]
public virtual void UseXPercentTest() {
Expand Down Expand Up @@ -169,6 +174,32 @@ public virtual void SymbolWidthAndHeightEmAndRemTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "symbolWidthAndHeightEmAndRemTest");
}

//-------------- SVGs with absolute or missing size, but with viewBox and or preserveAspectRatio="none"
[NUnit.Framework.Test]
public virtual void AbsoluteWidthHeightViewBoxTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "absoluteWidthHeightViewBoxTest");
}

[NUnit.Framework.Test]
public virtual void AbsoluteHeightViewBoxMissWidthTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "absoluteHeightViewBoxMissWidthTest");
}

[NUnit.Framework.Test]
public virtual void AbsoluteWidthNoHeightNoViewBoxTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "absoluteWidthNoHeightNoViewBoxTest");
}

[NUnit.Framework.Test]
public virtual void AbsoluteWidthViewBoxNoHeightTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "absoluteWidthViewBoxNoHeightTest");
}

[NUnit.Framework.Test]
public virtual void AbsWidthViewBoxNoneRatioTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "absWidthViewBoxNoneRatioTest");
}

//-------------- misc
[NUnit.Framework.Test]
public virtual void LinePercentTest() {
Expand Down Expand Up @@ -203,5 +234,15 @@ public virtual void ViewportFromConverterPropertiesTest() {
// browsers the result should be bigger but with the same proportions
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "viewportFromConverterPropertiesTest", properties);
}

[NUnit.Framework.Test]
public virtual void ViewBoxMissWidthHeightTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "viewBoxMissWidthHeightTest");
}

[NUnit.Framework.Test]
public virtual void PercentHeightMissWidthViewBoxTest() {
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "percentHeightMissWidthViewBoxTest");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ You should have received a copy of the GNU Affero General Public License
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Logs;
using iText.Layout.Properties;
using iText.StyledXmlParser.Node;
using iText.StyledXmlParser.Resolver.Resource;
using iText.Svg.Converter;
Expand All @@ -34,6 +37,7 @@ You should have received a copy of the GNU Affero General Public License
using iText.Svg.Processors.Impl;
using iText.Svg.Utils;
using iText.Svg.Xobject;
using iText.Test.Attributes;

namespace iText.Svg.Renderers {
[NUnit.Framework.Category("IntegrationTest")]
Expand Down Expand Up @@ -107,5 +111,100 @@ public virtual void NoSpecifiedWidthHeightImageTest() {
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}

[NUnit.Framework.Test]
public virtual void RelativeSizedSvg1Test() {
String svgName = "fixed_height_percent_width";
String svgFileName = SOURCE_FOLDER + svgName + ".svg";
String cmpFileName = SOURCE_FOLDER + "cmp_" + svgName + ".pdf";
String outFileName = DESTINATION_FOLDER + svgName + ".pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(FileUtil.GetInputStreamForFile(svgFileName));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, new SvgConverterProperties().SetBaseUri
(svgFileName));
SvgDrawContext svgDrawContext = new SvgDrawContext(new ResourceResolver(SOURCE_FOLDER), null);
SvgImageXObject svgImageXObject = new SvgImageXObject(result, svgDrawContext, 12, document.GetPdfDocument(
));
SvgImage svgImage = new SvgImage(svgImageXObject);
svgImage.SetWidth(100);
svgImage.SetHeight(300);
document.Add(svgImage);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}

[NUnit.Framework.Test]
public virtual void RelativeSizedSvg3Test() {
String svgName = "viewbox_fixed_height_percent_width";
String svgFileName = SOURCE_FOLDER + svgName + ".svg";
String cmpFileName = SOURCE_FOLDER + "cmp_" + svgName + ".pdf";
String outFileName = DESTINATION_FOLDER + svgName + ".pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(FileUtil.GetInputStreamForFile(svgFileName));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, new SvgConverterProperties().SetBaseUri
(svgFileName));
SvgDrawContext svgDrawContext = new SvgDrawContext(new ResourceResolver(SOURCE_FOLDER), null);
SvgImageXObject svgImageXObject = new SvgImageXObject(result, svgDrawContext, 12, document.GetPdfDocument(
));
SvgImage svgImage = new SvgImage(svgImageXObject);
svgImage.SetWidth(100);
svgImage.SetHeight(300);
document.Add(svgImage);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}

[NUnit.Framework.Test]
[LogMessage(LayoutLogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA)]
public virtual void RelativeSizedSvg4Test() {
String svgName = "viewbox_percent_height_percent_width";
String svgFileName = SOURCE_FOLDER + svgName + ".svg";
String cmpFileName = SOURCE_FOLDER + "cmp_" + svgName + ".pdf";
String outFileName = DESTINATION_FOLDER + svgName + ".pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(FileUtil.GetInputStreamForFile(svgFileName));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, new SvgConverterProperties().SetBaseUri
(svgFileName));
SvgDrawContext svgDrawContext = new SvgDrawContext(new ResourceResolver(SOURCE_FOLDER), null);
SvgImageXObject svgImageXObject = new SvgImageXObject(result, svgDrawContext, 12, document.GetPdfDocument(
));
SvgImage svgImage = new SvgImage(svgImageXObject);
document.Add(svgImage);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}

[NUnit.Framework.Test]
public virtual void RelativeSizedSvg5Test() {
String svgName = "viewbox_percent_height_percent_width_prRatio_none";
String svgFileName = SOURCE_FOLDER + svgName + ".svg";
String cmpFileName = SOURCE_FOLDER + "cmp_" + svgName + ".pdf";
String outFileName = DESTINATION_FOLDER + svgName + ".pdf";
using (Document document = new Document(new PdfDocument(new PdfWriter(outFileName, new WriterProperties().
SetCompressionLevel(0))))) {
INode parsedSvg = SvgConverter.Parse(FileUtil.GetInputStreamForFile(svgFileName));
ISvgProcessorResult result = new DefaultSvgProcessor().Process(parsedSvg, new SvgConverterProperties().SetBaseUri
(svgFileName));
SvgDrawContext svgDrawContext = new SvgDrawContext(new ResourceResolver(SOURCE_FOLDER), null);
SvgImageXObject svgImageXObject = new SvgImageXObject(result, svgDrawContext, 12, document.GetPdfDocument(
));
Div div = new Div();
div.SetWidth(100);
div.SetHeight(300);
SvgImage svgImage = new SvgImage(svgImageXObject);
svgImage.SetWidth(UnitValue.CreatePercentValue(100));
svgImage.SetHeight(UnitValue.CreatePercentValue(100));
div.Add(svgImage);
document.Add(div);
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, DESTINATION_FOLDER
, "diff"));
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dea09da

Please sign in to comment.