Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions PdfSharpCore.Test/Pdfs/Content/Objects/CNameTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using FluentAssertions;
using PdfSharpCore.Pdf.Content.Objects;
using Xunit;

namespace PdfSharpCore.Test.Pdfs.Content.Objects
{
public class CNameTests
{
[Theory]
[InlineData("/Foo")]
public void SetNameTests(string name)
{
var cName = new CName
{
Name = name
};

cName.Name.Should().Be(name);
}

[Fact]
public void SetNameNullThrowsException()
{
Action act = () => new CName
{
Name = null
};
act.Should().Throw<ArgumentNullException>();
}

[Theory]
[InlineData("Foo")]
[InlineData("")]
public void SetNameWithoutPrefixThrowsException(string name)
{
Action act = () => new CName
{
Name = name
};
act.Should().Throw<ArgumentException>();
}
}
}
58 changes: 36 additions & 22 deletions PdfSharpCore/Pdf.Content.Objects/CObjects.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region PDFsharp - A .NET library for processing PDF

//
// Authors:
// Stefan Lange
Expand All @@ -25,6 +26,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#endregion

using System;
Expand All @@ -35,7 +37,7 @@
using System.IO;
using System.Text;

namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
{
/// <summary>
/// Base class for all PDF content stream objects.
Expand All @@ -46,7 +48,8 @@ public abstract class CObject : ICloneable
/// Initializes a new instance of the <see cref="CObject"/> class.
/// </summary>
protected CObject()
{ }
{
}

/// <summary>
/// Creates a new object that is a copy of the current instance.
Expand Down Expand Up @@ -109,6 +112,7 @@ public string Text
get { return _text; }
set { _text = value; }
}

string _text;

/// <summary>
Expand All @@ -129,7 +133,7 @@ internal override void WriteObject(ContentWriter writer)
/// Represents a sequence of objects in a PDF content stream.
/// </summary>
[DebuggerDisplay("(count={Count})")]
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
{
/// <summary>
/// Creates a new object that is a copy of the current instance.
Expand Down Expand Up @@ -250,6 +254,7 @@ public CObject this[int index]
get { return (CObject)_items[index]; }
set { _items[index] = value; }
}

#endregion

#region ICollection Members
Expand Down Expand Up @@ -362,14 +367,8 @@ void IList<CObject>.RemoveAt(int index)

CObject IList<CObject>.this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}

#endregion
Expand Down Expand Up @@ -484,6 +483,7 @@ public int Value
get { return _value; }
set { _value = value; }
}

int _value;

/// <summary>
Expand Down Expand Up @@ -531,6 +531,7 @@ public double Value
get { return _value; }
set { _value = value; }
}

double _value;

/// <summary>
Expand Down Expand Up @@ -611,6 +612,7 @@ public string Value
get { return _value; }
set { _value = value; }
}

string _value;

/// <summary>
Expand All @@ -621,6 +623,7 @@ public CStringType CStringType
get { return _cStringType; }
set { _cStringType = value; }
}

CStringType _cStringType;

/// <summary>
Expand Down Expand Up @@ -687,6 +690,7 @@ public override string ToString()
break;
}
}

s.Append(')');
break;

Expand All @@ -710,6 +714,7 @@ public override string ToString()
default:
throw new ArgumentOutOfRangeException();
}

return s.ToString();
}

Expand All @@ -725,12 +730,14 @@ internal override void WriteObject(ContentWriter writer)
[DebuggerDisplay("({Name})")]
public class CName : CObject
{
private const string NamePrefix = "/";

/// <summary>
/// Initializes a new instance of the <see cref="CName"/> class.
/// </summary>
public CName()
{
_name = "/";
_name = NamePrefix;
}

/// <summary>
Expand Down Expand Up @@ -760,21 +767,24 @@ protected override CObject Copy()
}

/// <summary>
/// Gets or sets the name. Names must start with a slash.
/// Gets or sets the content stream name. Names must start with a slash.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If <paramref name="value"/> does not start with a forward slash</exception>
public string Name
{
get { return _name; }
get => _name;
set
{
if (String.IsNullOrEmpty(_name))
throw new ArgumentNullException("name");
if (_name[0] != '/')
throw new ArgumentException(PSSR.NameMustStartWithSlash);
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException(nameof(value));
if (!value.StartsWith(NamePrefix))
throw new ArgumentException(PSSR.NameMustStartWithSlash, nameof(value));
_name = value;
}
}
string _name;

private string _name;

/// <summary>
/// Returns a string that represents the current value.
Expand Down Expand Up @@ -837,7 +847,8 @@ public class COperator : CObject
/// Initializes a new instance of the <see cref="COperator"/> class.
/// </summary>
protected COperator()
{ }
{
}

internal COperator(OpCode opcode)
{
Expand Down Expand Up @@ -878,6 +889,7 @@ public CSequence Operands
{
get { return _seqence ?? (_seqence = new CSequence()); }
}

CSequence _seqence;

/// <summary>
Expand All @@ -887,6 +899,7 @@ public OpCode OpCode
{
get { return _opcode; }
}

readonly OpCode _opcode;


Expand All @@ -903,13 +916,14 @@ public override string ToString()

internal override void WriteObject(ContentWriter writer)
{
int count = _seqence != null ? _seqence.Count : 0;
int count = _seqence?.Count ?? 0;
for (int idx = 0; idx < count; idx++)
{
// ReSharper disable once PossibleNullReferenceException because the loop is not entered if _sequence is null
_seqence[idx].WriteObject(writer);
}

writer.WriteLineRaw(ToString());
}
}
}
}