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 21, 2025
2 parents 20c3f35 + be1bc77 commit 7b0d2ce
Show file tree
Hide file tree
Showing 25 changed files with 267 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,27 @@ public virtual void GlyphlineActualTextTest() {
}
}

[NUnit.Framework.Test]
public virtual void LineDashTest() {
String outPdf = DESTINATION_FOLDER + "lineDash.pdf";
String cmpPdf = SOURCE_FOLDER + "cmp_lineDash.pdf";
using (PdfDocument pdfDocument = new PdfDocument(CompareTool.CreateTestPdfWriter(outPdf))) {
PdfCanvas canvas = new PdfCanvas(pdfDocument.AddNewPage());
canvas.SaveState().SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode.FILL_STROKE).SetStrokeColor(ColorConstants
.BLUE).SetLineWidth(2).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 30);
canvas.SetLineDash(3);
canvas.BeginText().MoveText(180, 250).ShowText("phase 3").EndText();
canvas.SetLineDash(new float[] { 0, 0, 0 }, 2);
canvas.BeginText().MoveText(180, 350).ShowText("dashArray [0, 0, 0]").EndText();
canvas.SetLineDash(new float[] { 5, -5 }, 1);
canvas.BeginText().MoveText(180, 450).ShowText("dashArray [5, -5]").EndText();
canvas.SetLineDash(5, -10);
canvas.BeginText().MoveText(180, 550).ShowText("phase -10").EndText().RestoreState();
}
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
));
}

private void CreateStandardDocument(PdfWriter writer, int pageCount, PdfCanvasTest.ContentProvider contentProvider
) {
PdfDocument pdfDoc = new PdfDocument(writer);
Expand Down
Binary file not shown.
14 changes: 10 additions & 4 deletions itext.tests/itext.layout.tests/itext/layout/TextWritingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public virtual void TextRenderingModeTest01() {
.FILL_STROKE).SetStrokeColor(ColorConstants.BLUE).SetStrokeWidth(0.3f).SetFontColor(ColorConstants.GREEN
).SetFontSize(20);
document.Add(new Paragraph(text3));
Text text4 = new Text("This is a stroke with dashes text").SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode
.FILL_STROKE).SetStrokeColor(ColorConstants.BLUE).SetStrokeWidth(0.5f).SetFontColor(ColorConstants.PINK
).SetFontSize(20);
text4.SetDashPattern(new float[] { 0.5f, 1f }, 0f);
document.Add(new Paragraph(text4));
document.Close();
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
, "diff"));
Expand Down Expand Up @@ -281,18 +286,19 @@ public virtual void StrokedUnderlineTest() {
String cmpFileName = sourceFolder + "cmp_strokedUnderline.pdf";
using (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName))) {
using (Document document = new Document(pdfDocument)) {
Paragraph p = new Paragraph("Yellow text with pink stroked underline.").SetFontSize(50).SetFontColor(ColorConstants
.YELLOW);
Paragraph p = new Paragraph("Yellow text with pink stroked dashed underline.").SetFontSize(45).SetFontColor
(ColorConstants.YELLOW);
Underline underline = new Underline(null, 0, 0.1f, 0, -0.1f, PdfCanvasConstants.LineCapStyle.BUTT).SetStrokeWidth
(2).SetStrokeColor(new TransparentColor(ColorConstants.PINK, 0.5f));
(2).SetStrokeColor(new TransparentColor(ColorConstants.PINK, 0.5f)).SetDashPattern(new float[] { 5, 5,
10, 5 }, 5);
p.SetUnderline(underline);
Paragraph p2 = new Paragraph("Text with line-through and default underline.").SetFontSize(50).SetStrokeWidth
(1).SetFontColor(ColorConstants.DARK_GRAY).SetStrokeColor(ColorConstants.GREEN);
Underline underline2 = new Underline(ColorConstants.DARK_GRAY, 0, 0.1f, 0, 0.3f, PdfCanvasConstants.LineCapStyle
.BUTT).SetStrokeWidth(1).SetStrokeColor(new TransparentColor(ColorConstants.GREEN));
p2.SetUnderline(underline2);
p2.SetUnderline();
Paragraph p3 = new Paragraph("Text with transparent font color and default overline.").SetFontSize(50).SetFontColor
Paragraph p3 = new Paragraph("Text with transparent color and default overline.").SetFontSize(50).SetFontColor
(new TransparentColor(ColorConstants.BLUE, 0));
Underline underline3 = new Underline(null, 0, 0.1f, 0, 0.9f, PdfCanvasConstants.LineCapStyle.BUTT);
p3.SetUnderline(underline3);
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public virtual void NoLineStrokeWidthTest() {
[NUnit.Framework.Test]
public virtual void StrokeWithDashesTest() {
// TODO DEVSIX-8774 support stroke-opacity for text at layout level
// TODO DEVSIX-8776 support dash-pattern in layout
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "strokeWithDashes");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public virtual void TextDecorationStyleTest() {
ConvertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "textDecorationStyle");
}

[NUnit.Framework.Test]
public virtual void TextDecorationDashedStrokeTest() {
ConvertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "textDecorationDashedStroke");
}

[NUnit.Framework.Test]
public virtual void TspanDefaultFontSizeTest() {
ConvertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "tspanDefaultFontSize");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,11 @@ public virtual void TextCombinedAttributesTest() {

[NUnit.Framework.Test]
public virtual void TextStrokeDasharrayTest() {
//TODO: DEVSIX-8776 Support stroke dash pattern in layout
ConvertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "textStrokeDasharray");
}

[NUnit.Framework.Test]
public virtual void TextComplexStrokeDasharrayTest() {
//TODO: DEVSIX-8776 Support stroke dash pattern in layout
ConvertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "textComplexStrokeDasharray");
}

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
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.
34 changes: 30 additions & 4 deletions itext/itext.kernel/itext/kernel/pdf/canvas/PdfCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,11 @@ public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetMiterLimit(float miterLimit)
/// <param name="phase">the value of the phase</param>
/// <returns>current canvas.</returns>
public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float phase) {
currentGs.SetDashPattern(GetDashPatternArray(phase));
PdfArray dashPattern = GetDashPatternArray(phase);
if (dashPattern == null) {
return this;
}
currentGs.SetDashPattern(dashPattern);
contentStream.GetOutputStream().WriteByte('[').WriteByte(']').WriteSpace().WriteFloat(phase).WriteSpace().
WriteBytes(d);
return this;
Expand All @@ -1310,7 +1314,11 @@ public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float phase) {
/// </param>
/// <returns>current canvas.</returns>
public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float unitsOn, float phase) {
currentGs.SetDashPattern(GetDashPatternArray(new float[] { unitsOn }, phase));
PdfArray dashPattern = GetDashPatternArray(new float[] { unitsOn }, phase);
if (dashPattern == null) {
return this;
}
currentGs.SetDashPattern(dashPattern);
contentStream.GetOutputStream().WriteByte('[').WriteFloat(unitsOn).WriteByte(']').WriteSpace().WriteFloat(
phase).WriteSpace().WriteBytes(d);
return this;
Expand All @@ -1330,7 +1338,11 @@ public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float unitsOn, floa
/// <param name="unitsOff">the number of units that must be 'off'</param>
/// <returns>current canvas.</returns>
public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float unitsOn, float unitsOff, float phase) {
currentGs.SetDashPattern(GetDashPatternArray(new float[] { unitsOn, unitsOff }, phase));
PdfArray dashPattern = GetDashPatternArray(new float[] { unitsOn, unitsOff }, phase);
if (dashPattern == null) {
return this;
}
currentGs.SetDashPattern(dashPattern);
contentStream.GetOutputStream().WriteByte('[').WriteFloat(unitsOn).WriteSpace().WriteFloat(unitsOff).WriteByte
(']').WriteSpace().WriteFloat(phase).WriteSpace().WriteBytes(d);
return this;
Expand All @@ -1349,7 +1361,11 @@ public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float unitsOn, floa
/// <param name="phase">the value of the phase</param>
/// <returns>current canvas.</returns>
public virtual iText.Kernel.Pdf.Canvas.PdfCanvas SetLineDash(float[] array, float phase) {
currentGs.SetDashPattern(GetDashPatternArray(array, phase));
PdfArray dashPattern = GetDashPatternArray(array, phase);
if (dashPattern == null) {
return this;
}
currentGs.SetDashPattern(dashPattern);
PdfOutputStream @out = contentStream.GetOutputStream();
@out.WriteByte('[');
for (int iter = 0; iter < array.Length; iter++) {
Expand Down Expand Up @@ -2355,9 +2371,19 @@ private PdfArray GetDashPatternArray(float[] dashArray, float phase) {
PdfArray dashPatternArray = new PdfArray();
PdfArray dArray = new PdfArray();
if (dashArray != null) {
float sum = 0;
foreach (float fl in dashArray) {
if (fl < 0) {
// Negative values are not allowed.
return null;
}
sum += fl;
dArray.Add(new PdfNumber(fl));
}
if (sum < 1e-6) {
// All 0 values are not allowed.
return null;
}
}
dashPatternArray.Add(dArray);
dashPatternArray.Add(new PdfNumber(phase));
Expand Down
32 changes: 32 additions & 0 deletions itext/itext.layout/itext/layout/ElementPropertyContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,38 @@ public virtual T SetStrokeColor(Color strokeColor) {
return (T)(Object)this;
}

/// <summary>Sets the stroke dash pattern for the current text.</summary>
/// <remarks>
/// Sets the stroke dash pattern for the current text. Dash pattern is an array of the form [ dashArray dashPhase ],
/// where
/// <paramref name="dashArray"/>
/// is a float array that specifies the length of the alternating dashes and gaps,
/// <paramref name="dashPhase"/>
/// is a float that specifies the distance into the dash pattern to start the dash.
/// </remarks>
/// <param name="dashArray">
/// float array that specifies the length of the alternating dashes and gaps,
/// use
/// <see langword="null"/>
/// for solid line
/// </param>
/// <param name="dashPhase">
/// float that specifies the distance into the dash pattern to start the dash,
/// use 0 in case offset isn't needed
/// </param>
/// <returns>this element</returns>
public virtual T SetDashPattern(float[] dashArray, float dashPhase) {
IList<float> dashPattern = new List<float>();
if (dashArray != null) {
foreach (float fl in dashArray) {
dashPattern.Add(fl);
}
}
dashPattern.Add(dashPhase);
SetProperty(Property.STROKE_DASH_PATTERN, dashPattern);
return (T)(Object)this;
}

/// <summary>Gets the stroke width for the current element.</summary>
/// <remarks>
/// Gets the stroke width for the current element.
Expand Down
16 changes: 15 additions & 1 deletion itext/itext.layout/itext/layout/properties/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ public sealed class Property {

public const int STROKE_COLOR = 63;

/// <summary>
/// STROKE_DASH_PATTERN property specifies dash pattern for the text stroke and stores the
/// <see cref="System.Collections.IList{E}"/>
/// as float array of the form [ dashArray dashPhase ], where
/// <c>dashArray</c>
/// is a float array that specifies
/// the length of the alternating dashes and gaps,
/// <c>dashPhase</c>
/// is a float that specifies the distance into
/// the dash pattern to start the dash.
/// </summary>
public const int STROKE_DASH_PATTERN = 156;

public const int STROKE_WIDTH = 64;

public const int SKEW = 65;
Expand Down Expand Up @@ -404,7 +417,7 @@ public sealed class Property {
/// </remarks>
private static readonly bool[] INHERITED_PROPERTIES;

private const int MAX_INHERITED_PROPERTY_ID = 155;
private const int MAX_INHERITED_PROPERTY_ID = 156;

static Property() {
INHERITED_PROPERTIES = new bool[MAX_INHERITED_PROPERTY_ID + 1];
Expand Down Expand Up @@ -434,6 +447,7 @@ static Property() {
INHERITED_PROPERTIES[iText.Layout.Properties.Property.SPACING_RATIO] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.SPLIT_CHARACTERS] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.STROKE_COLOR] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.STROKE_DASH_PATTERN] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.STROKE_WIDTH] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.TEXT_ALIGNMENT] = true;
INHERITED_PROPERTIES[iText.Layout.Properties.Property.TEXT_ANCHOR] = true;
Expand Down
Loading

0 comments on commit 7b0d2ce

Please sign in to comment.