Skip to content
Open
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
2 changes: 1 addition & 1 deletion PdfSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ Global
{7C753636-7947-46E0-95E0-135EAA7BFEB3} = {5912CE0D-0DCE-479F-944A-0DFA4CE95A88}
{E97F1455-620B-4BF2-B839-08F7AE2FD772} = {E1933982-1D12-497F-B465-67D5E5C74CA1}
{0EDABDD2-05D5-43D0-A635-645DD8127201} = {18632E7D-54DF-4933-A8DF-A7ECE8666E9B}
{8770F3E1-3FEE-4611-89D4-AF80DFB3B3EA} = {0EDABDD2-05D5-43D0-A635-645DD8127201}
{8770F3E1-3FEE-4611-89D4-AF80DFB3B3EA} = {FD3FEC8C-C8C8-47F1-AF08-F77EC937ACFE}
{FD3FEC8C-C8C8-47F1-AF08-F77EC937ACFE} = {18632E7D-54DF-4933-A8DF-A7ECE8666E9B}
{EF9C1FA7-24B3-438A-BD21-400C1500E164} = {18632E7D-54DF-4933-A8DF-A7ECE8666E9B}
{71BD4587-A8B0-4653-A3B2-93578F71ABB7} = {5912CE0D-0DCE-479F-944A-0DFA4CE95A88}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,18 @@ public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEn
/// </summary>
protected override CObject Copy()
{
CObject obj = base.Copy();
_items = new List<CObject>(_items);
//Commit note: I may be wrong but this didn't seem right to me.
//If we want to make a copy, the clone should contain cloned items, not the original object i.e.
//public bool doingSomething(CSequence s) {
// var i = s[0];
// var _ = s.Clone();
// return ReferenceEquals(s[0], i); //this should return true
//}
var c = (CSequence)base.Copy();
c._items = new List<CObject>(_items.Count);
for (int idx = 0; idx < _items.Count; idx++)
_items[idx] = _items[idx].Clone();
return obj;
c._items.Add(_items[idx].Clone());
return c;
}

/// <summary>
Expand Down Expand Up @@ -179,22 +186,6 @@ public void Insert(int index, CObject value)
_items.Insert(index, value);
}

/////// <summary>
/////// Gets a value indicating whether the sequence has a fixed size.
/////// </summary>
////public bool IsFixedSize
////{
//// get { return items.IsFixedSize; }
////}

/////// <summary>
/////// Gets a value indicating whether the sequence is read-only.
/////// </summary>
////public bool IsReadOnly
////{
//// get { return items.IsReadOnly; }
////}

/// <summary>
/// Removes the specified value from the sequence.
/// </summary>
Expand Down Expand Up @@ -237,22 +228,6 @@ public void CopyTo(CObject[] array, int index)
/// </summary>
public int Count => _items.Count;

///// <summary>
///// Gets a value indicating whether access to the sequence is synchronized (thread safe).
///// </summary>
//public bool IsSynchronized
//{
// get { return items.IsSynchronized; }
//}

///// <summary>
///// Gets an object that can be used to synchronize access to the sequence.
///// </summary>
//public object SyncRoot
//{
// get { return items.SyncRoot; }
//}

#endregion

#region IEnumerable Members
Expand Down Expand Up @@ -318,23 +293,23 @@ internal override void WriteObject(ContentWriter writer)

int IList<CObject>.IndexOf(CObject item)
{
throw new NotImplementedException();
return _items.IndexOf(item);
}

void IList<CObject>.Insert(int index, CObject item)
{
throw new NotImplementedException();
_items.Insert(index, item);
}

void IList<CObject>.RemoveAt(int index)
{
throw new NotImplementedException();
_items.RemoveAt(index);
}

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

#endregion
Expand All @@ -343,31 +318,37 @@ CObject IList<CObject>.this[int index]

void ICollection<CObject>.Add(CObject item)
{
throw new NotImplementedException();
Add(item);
}

void ICollection<CObject>.Clear()
{
throw new NotImplementedException();
Clear();
}

bool ICollection<CObject>.Contains(CObject item)
{
throw new NotImplementedException();
return Contains(item);
}

void ICollection<CObject>.CopyTo(CObject[] array, int arrayIndex)
{
throw new NotImplementedException();
array = array ?? throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0) throw new ArgumentOutOfRangeException();
if (_items.Count > array.Length - arrayIndex)
throw new ArgumentException("The number of elements in the source System.Collections.Generic.ICollection`1 is greater than the available space from arrayIndex to the end of the destination array.");

for (int i = arrayIndex; i < _items.Count; i++)
array[i] = _items[i];
}

int ICollection<CObject>.Count => throw new NotImplementedException();
int ICollection<CObject>.Count => _items.Count;

bool ICollection<CObject>.IsReadOnly => throw new NotImplementedException();
bool ICollection<CObject>.IsReadOnly => false;

bool ICollection<CObject>.Remove(CObject item)
{
throw new NotImplementedException();
return _items.Remove(item);
}

#endregion
Expand All @@ -376,7 +357,7 @@ bool ICollection<CObject>.Remove(CObject item)

IEnumerator<CObject> IEnumerable<CObject>.GetEnumerator()
{
throw new NotImplementedException();
return _items.GetEnumerator();
}

#endregion
Expand Down Expand Up @@ -790,7 +771,8 @@ internal override void WriteObject(ContentWriter writer)
/// <summary>
/// Represents an operator a PDF content stream.
/// </summary>
[DebuggerDisplay("({Name}, operands={Operands.Count})")]
[DebuggerDisplay($@"{{{nameof(_debuggerDisplay)},nq}}")]
[DebuggerTypeProxy(typeof(COperatorDebuggerDisplay))]
public class COperator : CObject
{
/// <summary>
Expand Down Expand Up @@ -855,7 +837,33 @@ public override string ToString()
return Name;
}

internal override void WriteObject(ContentWriter writer)
#region Printing/Debugger display
/// <summary>Function returning string that will be used to displayed object's value in debugger for this type of objects.</summary>
public static Func<COperator, string> debuggerDisplay { get; set; } = o => o.ToString(15);
internal string _debuggerDisplay => debuggerDisplay(this);

/// <summary>Prints longer version of string including name, operands list and operator description.</summary>
/// <param name="maxOperandsStringLength">Maximal number of characters in operands portion of the string that could be displayed.
/// If printing all operands would require greater number of characters, a sting in form like "15 operands" will be put in the result instead.</param>
public string ToString(int maxOperandsStringLength) {
if (maxOperandsStringLength < 1) return ToString();

var ops = ""; var sep = ", "; //operands, separator
foreach (var op in Operands) {
var os = op + sep; //this should be optimized and checking the size of before converting to string, I guess some object may be really long...
ops += os;
if (ops.Length > maxOperandsStringLength + sep.Length) {
ops = Operands.Count + " operands" + sep;
break;
}
}
if (ops.Length > 0) ops = ops.Substring(0, ops.Length - sep.Length);

return $"{Name, -4}({ops}) {OpCode.Description}";
}
#endregion

internal override void WriteObject(ContentWriter writer)
{
if (_sequence != null)
{
Expand All @@ -869,4 +877,18 @@ internal override void WriteObject(ContentWriter writer)
writer.WriteLineRaw(ToString());
}
}

internal class COperatorDebuggerDisplay {
private readonly COperator o;

public string Name => $"{o.Name} - {o.OpCode.Description}";
public string _Postscript => o.OpCode.Postscript;
public OpCodeFlags __Flags => o.OpCode.Flags;

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public CObject[] ___Operands => o.Operands.ToArray();

public COperatorDebuggerDisplay(COperator o) => this.o = o;

}
}
13 changes: 9 additions & 4 deletions src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ protected override object Copy()
// ReSharper disable once InconsistentNaming
internal DictionaryElements? _elements;

/// <summary>
/// Returns an enumerator that iterates through the dictionary elements.
/// </summary>
public IEnumerator<KeyValuePair<string, PdfItem?>> GetEnumerator()
protected override PdfItem getChildByName(String name)
=> Elements[name];
protected override void addChildWithName(String name, PdfItem value)
=> Elements[name] = value;

/// <summary>
/// Returns an enumerator that iterates through the dictionary elements.
/// </summary>
public IEnumerator<KeyValuePair<string, PdfItem?>> GetEnumerator()
=> Elements.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
Expand Down
21 changes: 19 additions & 2 deletions src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENSE file in the solution root for more information.

using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.Advanced;

namespace PdfSharp.Pdf
{
Expand All @@ -10,9 +11,25 @@ namespace PdfSharp.Pdf
/// </summary>
public abstract class PdfItem : ICloneable
{
// All simple types (i.e. derived from PdfItem but not from PdfObject) must be immutable.
/// <summary>Returns this object or if this object is a reference, returns <see cref="PdfReference.Value"/> of the reference.</summary>
public PdfItem Value => this is PdfReference r ? r.Value : this;
/// <summary>Returns or creates child with given name.
/// Note that implementation for this may be provided by specific descendants of the <see cref="PdfItem"/> class but it's not mandatory.
/// If implementation is not provided by specific type a <see cref="NotSupportedException"/> will be thrown when attempting to use this accessors.</summary>
public PdfItem this[string name] {
get => Value.getChildByName(name);
set => Value.addChildWithName(name, value);
}

protected virtual PdfItem getChildByName(String name)
=> throw new NotSupportedException($@"{this.GetType()} does not allow getting children by name.");
protected virtual void addChildWithName(String name, PdfItem value)
=> throw new NotSupportedException($@"{this.GetType()} does not allow adding children.");


// All simple types (i.e. derived from PdfItem but not from PdfObject) must be immutable.

object ICloneable.Clone()
object ICloneable.Clone()
{
return Copy();
}
Expand Down