Skip to content

Commit 6de55e9

Browse files
committed
Created a object stream to use, and refactored writing objects to stream
1 parent 490bdde commit 6de55e9

27 files changed

+797
-1484
lines changed

.editorconfig

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ charset = utf-8
1313
end_of_line = crlf
1414
indent_style = space
1515
indent_size = 4
16-
insert_final_newline = false
16+
tab_width = 4
17+
insert_final_newline = true
1718
trim_trailing_whitespace = true
1819

1920
#########################
@@ -43,7 +44,6 @@ trim_trailing_whitespace = false
4344
# Web Files
4445
[*.{htm,html,js,ts,tsx,css,sass,scss,less,svg,vue}]
4546
indent_size = 2
46-
insert_final_newline = true
4747

4848
###########################
4949
# C# code style settings
@@ -72,6 +72,9 @@ csharp_style_expression_bodied_local_functions = false:silent
7272
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#pattern-matching
7373
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
7474
csharp_style_pattern_matching_over_as_with_null_check = true:warning
75+
csharp_style_prefer_pattern_matching = true:silent
76+
csharp_style_prefer_not_pattern = true:suggestion
77+
csharp_style_prefer_extended_property_pattern = true:suggestion
7578

7679
# Inlined variable declarations
7780
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#inlined-variable-declarations
@@ -104,6 +107,20 @@ csharp_style_prefer_range_operator = true:suggestion
104107
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#miscellaneous-preferences
105108
csharp_style_deconstructed_variable_declaration = true:warning
106109
csharp_style_pattern_local_over_anonymous_function = true:none
110+
csharp_prefer_simple_using_statement = false:suggestion
111+
csharp_prefer_static_local_function = true:suggestion
112+
csharp_using_directive_placement = outside_namespace:silent
113+
csharp_style_namespace_declarations = file_scoped:suggestion
114+
csharp_style_prefer_method_group_conversion = true:silent
115+
csharp_style_prefer_null_check_over_type_check = true:suggestion
116+
csharp_style_prefer_local_over_anonymous_function = true:suggestion
117+
csharp_style_implicit_object_creation_when_type_is_apparent = false:suggestion
118+
csharp_style_prefer_tuple_swap = true:suggestion
119+
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
120+
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
121+
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
122+
csharp_style_prefer_parameter_null_checking = true:suggestion
123+
csharp_style_prefer_switch_expression = true:suggestion
107124

108125
###########################
109126
# .NET code style settings
@@ -147,15 +164,23 @@ dotnet_style_prefer_inferred_tuple_names = true:warning
147164
dotnet_style_prefer_inferred_anonymous_type_member_names = false:suggestion
148165
dotnet_style_prefer_auto_properties = true:warning
149166
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
150-
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
151-
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
167+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
168+
dotnet_style_prefer_conditional_expression_over_return = true:silent
152169
dotnet_style_prefer_compound_assignment = true:suggestion
153170

154171
# Null-checking preferences
155172
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#null-checking-preferences
156173
dotnet_style_coalesce_expression = true:warning
157174
dotnet_style_null_propagation = true:warning
158175

176+
# Miscellaneous preferences
177+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
178+
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
179+
dotnet_style_prefer_simplified_interpolation = true:suggestion
180+
dotnet_style_namespace_match_folder = true:suggestion
181+
dotnet_style_allow_multiple_blank_lines_experimental = true:suggestion
182+
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
183+
159184
#############################
160185
# .NET code quality settings
161186
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#net-code-quality-settings
@@ -249,8 +274,13 @@ dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities
249274
dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field
250275
dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = static, readonly
251276

277+
# public_static_readonly_fields - Define static and readonly fields
278+
dotnet_naming_symbols.public_static_readonly_fields.applicable_accessibilities = public
279+
dotnet_naming_symbols.public_static_readonly_fields.applicable_kinds = field
280+
dotnet_naming_symbols.public_static_readonly_fields.required_modifiers = static, readonly
281+
252282
# non_private_static_readonly_fields - Define static and readonly fields
253-
dotnet_naming_symbols.non_private_static_readonly_fields.applicable_accessibilities = public, internal, protected
283+
dotnet_naming_symbols.non_private_static_readonly_fields.applicable_accessibilities = internal, protected
254284
dotnet_naming_symbols.non_private_static_readonly_fields.applicable_kinds = field
255285
dotnet_naming_symbols.non_private_static_readonly_fields.required_modifiers = static, readonly
256286

@@ -317,7 +347,12 @@ dotnet_naming_rule.non_private_static_readonly_fields_must_be_pascal_case.severi
317347
dotnet_naming_rule.non_private_static_readonly_fields_must_be_pascal_case.symbols = non_private_static_readonly_fields
318348
dotnet_naming_rule.non_private_static_readonly_fields_must_be_pascal_case.style = pascal_case
319349

320-
# Public, internal and protected readonly fields must be PascalCase
350+
# Public static readonly fields must be ALL_UPPER_CASE
351+
dotnet_naming_rule.public_static_readonly_fields_must_be_all_upper.severity = warning
352+
dotnet_naming_rule.public_static_readonly_fields_must_be_all_upper.symbols = public_static_readonly_fields
353+
dotnet_naming_rule.public_static_readonly_fields_must_be_all_upper.style = all_upper_with_underscore
354+
355+
# Internal and protected readonly fields must be PascalCase
321356
dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.severity = warning
322357
dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.symbols = non_private_readonly_fields
323358
dotnet_naming_rule.non_private_readonly_fields_must_be_pascal_case.style = pascal_case
@@ -365,4 +400,4 @@ dotnet_naming_rule.non_interface_types_must_be_pascal_case.style = pascal_case
365400
# Interfaces must be PascalCase and start with an 'I'
366401
dotnet_naming_rule.interface_types_must_be_prefixed_with_i.severity = warning
367402
dotnet_naming_rule.interface_types_must_be_prefixed_with_i.symbols = interface_types
368-
dotnet_naming_rule.interface_types_must_be_prefixed_with_i.style = prefix_interface_interface_with_i
403+
dotnet_naming_rule.interface_types_must_be_prefixed_with_i.style = prefix_interface_interface_with_i
Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Synercoding.FileFormats.Pdf.LowLevel;
2-
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
32
using System;
43
using System.Collections.Generic;
54

@@ -10,8 +9,6 @@ namespace Synercoding.FileFormats.Pdf
109
/// </summary>
1110
public class DocumentInformation : IPdfObject
1211
{
13-
private bool _isWritten;
14-
1512
internal DocumentInformation(PdfReference id)
1613
{
1714
Reference = id;
@@ -64,88 +61,5 @@ internal DocumentInformation(PdfReference id)
6461
/// Extra information that will be added to the PDF meta data
6562
/// </summary>
6663
public IDictionary<string, string> ExtraInfo { get; } = new Dictionary<string, string>();
67-
68-
internal uint WriteToStream(PdfStream stream)
69-
{
70-
if (_isWritten)
71-
throw new InvalidOperationException("Object is already written to stream.");
72-
73-
var position = (uint)stream.Position;
74-
75-
stream.IndirectDictionary(this, static (did, dictionary) =>
76-
{
77-
if (!string.IsNullOrWhiteSpace(did.Title))
78-
dictionary.Write(PdfName.Get("Title"), _toPdfHexadecimalString(did.Title!));
79-
if (!string.IsNullOrWhiteSpace(did.Author))
80-
dictionary.Write(PdfName.Get("Author"), _toPdfHexadecimalString(did.Author!));
81-
if (!string.IsNullOrWhiteSpace(did.Subject))
82-
dictionary.Write(PdfName.Get("Subject"), _toPdfHexadecimalString(did.Subject!));
83-
if (!string.IsNullOrWhiteSpace(did.Keywords))
84-
dictionary.Write(PdfName.Get("Keywords"), _toPdfHexadecimalString(did.Keywords!));
85-
if (!string.IsNullOrWhiteSpace(did.Creator))
86-
dictionary.Write(PdfName.Get("Creator"), _toPdfHexadecimalString(did.Creator!));
87-
if (!string.IsNullOrWhiteSpace(did.Producer))
88-
dictionary.Write(PdfName.Get("Producer"), _toPdfHexadecimalString(did.Producer!));
89-
if (did.CreationDate != null)
90-
dictionary.Write(PdfName.Get("CreationDate"), _toPdfDate(did.CreationDate.Value));
91-
if (did.ModDate != null)
92-
dictionary.Write(PdfName.Get("ModDate"), _toPdfDate(did.ModDate.Value));
93-
94-
if(did.ExtraInfo.Count != 0)
95-
foreach(var kv in did.ExtraInfo)
96-
dictionary.Write(PdfName.Get(kv.Key), _toPdfHexadecimalString(kv.Value));
97-
});
98-
99-
_isWritten = true;
100-
101-
return position;
102-
}
103-
104-
private static string _toPdfHexadecimalString(string input)
105-
{
106-
var bytes = System.Text.Encoding.ASCII.GetBytes(input);
107-
var builder = new System.Text.StringBuilder((bytes.Length * 2) + 2);
108-
builder.Append('<');
109-
foreach (var b in bytes)
110-
{
111-
builder.Append(b.ToString("X2"));
112-
}
113-
builder.Append('>');
114-
return builder.ToString();
115-
}
116-
117-
private static string _toPdfDate(DateTimeOffset input)
118-
{
119-
var datePart = input.ToString("yyyyMMddHHmmss");
120-
121-
var builder = new System.Text.StringBuilder(22);
122-
builder.Append("(D:");
123-
builder.Append(datePart);
124-
125-
var hours = input.Offset.Hours;
126-
var minutes = input.Offset.Minutes;
127-
128-
if (hours == 0 && minutes == 0)
129-
{
130-
builder.Append("Z00'00");
131-
}
132-
else
133-
{
134-
if (hours > 0 || (hours == 0 && minutes > 0))
135-
{
136-
builder.Append('+');
137-
}
138-
else
139-
{
140-
builder.Append('-');
141-
}
142-
builder.Append(Math.Abs(hours).ToString().PadLeft(2, '0'));
143-
builder.Append('\'');
144-
builder.Append(minutes.ToString().PadLeft(2, '0'));
145-
}
146-
builder.Append(')');
147-
148-
return builder.ToString();
149-
}
15064
}
15165
}

src/Synercoding.FileFormats.Pdf/Extensions/PdfPageExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using Synercoding.FileFormats.Pdf.Internals;
1+
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
33
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
4+
using Synercoding.FileFormats.Pdf.LowLevel.Internal;
45
using Synercoding.FileFormats.Pdf.LowLevel.Operators.State;
56
using Synercoding.FileFormats.Pdf.LowLevel.Text;
67
using Synercoding.Primitives;

src/Synercoding.FileFormats.Pdf/Image.cs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ namespace Synercoding.FileFormats.Pdf
1111
/// </summary>
1212
public sealed class Image : IPdfObject, IDisposable
1313
{
14-
private readonly Stream _imageStream;
1514
private bool _disposed;
16-
private bool _isWritten;
1715

1816
internal Image(PdfReference id, SixLabors.ImageSharp.Image image)
1917
{
@@ -28,7 +26,7 @@ internal Image(PdfReference id, SixLabors.ImageSharp.Image image)
2826
Width = image.Width;
2927
Height = image.Height;
3028
ms.Position = 0;
31-
_imageStream = ms;
29+
RawStream = ms;
3230
}
3331

3432
internal Image(PdfReference id, Stream jpgStream, int width, int height)
@@ -37,9 +35,11 @@ internal Image(PdfReference id, Stream jpgStream, int width, int height)
3735

3836
Width = width;
3937
Height = height;
40-
_imageStream = jpgStream;
38+
RawStream = jpgStream;
4139
}
4240

41+
internal Stream RawStream { get; private set; }
42+
4343
/// <inheritdoc />
4444
public PdfReference Reference { get; private set; }
4545

@@ -58,38 +58,9 @@ public void Dispose()
5858
{
5959
if (!_disposed)
6060
{
61-
_imageStream.Dispose();
61+
RawStream.Dispose();
6262
_disposed = true;
6363
}
6464
}
65-
66-
internal bool TryWriteToStream(PdfStream stream, out uint position)
67-
{
68-
position = 0;
69-
70-
if (_isWritten)
71-
return false;
72-
if (_disposed)
73-
throw new ObjectDisposedException(nameof(_imageStream), "Internal image is already disposed");
74-
75-
position = (uint)stream.Position;
76-
77-
stream.IndirectStream(this, _imageStream, this, static (image, dictionary) =>
78-
{
79-
dictionary
80-
.Type(ObjectType.XObject)
81-
.SubType(XObjectSubType.Image)
82-
.Write(PdfName.Get("Width"), image.Width)
83-
.Write(PdfName.Get("Height"), image.Height)
84-
.Write(PdfName.Get("ColorSpace"), PdfName.Get("DeviceRGB"))
85-
.Write(PdfName.Get("BitsPerComponent"), 8)
86-
.Write(PdfName.Get("Decode"), 0f, 1f, 0f, 1f, 0f, 1f);
87-
},
88-
StreamFilter.DCTDecode);
89-
90-
_isWritten = true;
91-
92-
return true;
93-
}
9465
}
9566
}

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

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)