Skip to content

Commit d7564c9

Browse files
committed
Moved ExtGState dictionaries to indirect objects, fixed prefix, and added to example
1 parent 1972355 commit d7564c9

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ public static void Main(string[] args)
216216

217217
page.Content.AddShapes(trimBox, static (trim, context) =>
218218
{
219+
context.SetExtendedGraphicsState(new ExtendedGraphicsState()
220+
{
221+
Overprint = true
222+
});
219223
context.SetStroke(new SpotColor(new Separation(LowLevel.PdfName.Get("CutContour"), PredefinedColors.Magenta), 1));
220224
context.Rectangle(trim);
221225
context.Stroke();

src/Synercoding.FileFormats.Pdf/LowLevel/Internal/PageResources.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal sealed class PageResources : IDisposable
1515
private readonly Map<PdfName, Image> _images;
1616
private readonly Dictionary<Separation, (PdfName Name, PdfReference Id)> _separations;
1717
private readonly Dictionary<Type1StandardFont, PdfReference> _standardFonts;
18-
private readonly Dictionary<ExtendedGraphicsState, PdfName> _extendedGraphicsStates;
18+
private readonly Dictionary<ExtendedGraphicsState, (PdfName Name, PdfReference Id)> _extendedGraphicsStates;
1919

2020
private int _stateCounter = 0;
2121
private int _separationCounter = 0;
@@ -27,13 +27,13 @@ internal PageResources(TableBuilder tableBuilder)
2727
_images = new Map<PdfName, Image>();
2828
_separations = new Dictionary<Separation, (PdfName Name, PdfReference Id)>();
2929
_standardFonts = new Dictionary<Type1StandardFont, PdfReference>();
30-
_extendedGraphicsStates = new Dictionary<ExtendedGraphicsState, PdfName>();
30+
_extendedGraphicsStates = new Dictionary<ExtendedGraphicsState, (PdfName Name, PdfReference Id)>();
3131
}
3232

3333
public IReadOnlyDictionary<PdfName, Image> Images
3434
=> _images.Forward;
3535

36-
public IReadOnlyDictionary<ExtendedGraphicsState, PdfName> ExtendedGraphicsStates
36+
public IReadOnlyDictionary<ExtendedGraphicsState, (PdfName Name, PdfReference Id)> ExtendedGraphicsStates
3737
=> _extendedGraphicsStates;
3838

3939
internal IReadOnlyDictionary<Separation, (PdfName Name, PdfReference Id)> SeparationReferences
@@ -115,12 +115,12 @@ internal PdfName AddSeparation(Separation separation)
115115

116116
internal PdfName AddExtendedGraphicsState(ExtendedGraphicsState extendedGraphicsState)
117117
{
118-
if (_extendedGraphicsStates.TryGetValue(extendedGraphicsState, out var name))
119-
return name;
118+
if (_extendedGraphicsStates.TryGetValue(extendedGraphicsState, out var tuple))
119+
return tuple.Name;
120120

121-
var key = PREFIX_SEPARATION + Interlocked.Increment(ref _stateCounter).ToString().PadLeft(6, '0');
122-
name = PdfName.Get(key);
123-
_extendedGraphicsStates[extendedGraphicsState] = name;
121+
var key = PREFIX_EXTGSTATE + Interlocked.Increment(ref _stateCounter).ToString().PadLeft(6, '0');
122+
var name = PdfName.Get(key);
123+
_extendedGraphicsStates[extendedGraphicsState] = (name, _tableBuilder.ReserveId());
124124

125125
return name;
126126
}

src/Synercoding.FileFormats.Pdf/LowLevel/ObjectStream.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,11 @@ public ObjectStream Write(PdfPage page)
162162

163163
if (resources.ExtendedGraphicsStates.Count != 0)
164164
{
165-
stream.Write(PdfName.Get("ExtGState"), resources.ExtendedGraphicsStates, static (extGStates, stream) => stream.Dictionary(extGStates, static (extendedGStates, extgstateDictionary) =>
165+
stream.Write(PdfName.Get("ExtGState"), resources.ExtendedGraphicsStates.Values, static (extGStates, stream) => stream.Dictionary(extGStates, static (extendedGStates, dict) =>
166166
{
167-
foreach(var (state, name) in extendedGStates)
167+
foreach (var (name, reference) in extendedGStates)
168168
{
169-
extgstateDictionary.Write(name, state, static (state, raw) =>
170-
{
171-
raw.Dictionary(state, static (state, dict) =>
172-
{
173-
if (state.Overprint.HasValue)
174-
dict.Write(PdfName.Get("OP"), state.Overprint.Value);
175-
if (state.OverprintNonStroking.HasValue)
176-
dict.Write(PdfName.Get("op"), state.OverprintNonStroking.Value);
177-
});
178-
});
169+
dict.Write(name, reference);
179170
}
180171
}));
181172
}
@@ -238,6 +229,22 @@ public ObjectStream Write(PdfReference reference, Separation separation)
238229
return this;
239230
}
240231

232+
public ObjectStream Write(PdfReference reference, ExtendedGraphicsState state)
233+
{
234+
if (!_tableBuilder.TrySetPosition(reference, InnerStream.Position))
235+
return this;
236+
237+
_indirectDictionary(reference, state, static (state, dict) =>
238+
{
239+
if (state.Overprint.HasValue)
240+
dict.Write(PdfName.Get("OP"), state.Overprint.Value);
241+
if (state.OverprintNonStroking.HasValue)
242+
dict.Write(PdfName.Get("op"), state.OverprintNonStroking.Value);
243+
});
244+
245+
return this;
246+
}
247+
241248
private void _indirectDictionary<T>(PdfReference reference, T data, Action<T, PdfDictionary> dictionaryAction)
242249
{
243250
InnerStream

src/Synercoding.FileFormats.Pdf/PdfWriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ private void _writePageAndResourcesToObjectStream(PdfPage page)
233233
foreach (var (separation, (_, refId)) in page.Resources.SeparationReferences)
234234
_objectStream.Write(refId, separation);
235235

236+
foreach (var (state, (_, refId)) in page.Resources.ExtendedGraphicsStates)
237+
_objectStream.Write(refId, state);
238+
236239
_objectStream.Write(page.Content.RawContentStream);
237240
}
238241

0 commit comments

Comments
 (0)