Skip to content

Commit c9af337

Browse files
authored
Merge pull request #42 from synercoder/features/small-refactors
Lots of small refactors & cleanup
2 parents 949ea81 + 0e34c1d commit c9af337

15 files changed

+81
-622
lines changed

.github/workflows/dotnet-core.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Setup .NET Core
3434
uses: actions/setup-dotnet@v1
3535
with:
36-
dotnet-version: 5.0.x
36+
dotnet-version: 6.0.x
3737
- name: Restore
3838
run: dotnet restore
3939
- name: Build
@@ -73,7 +73,7 @@ jobs:
7373
- name: Setup .NET Core
7474
uses: actions/setup-dotnet@v1
7575
with:
76-
dotnet-version: 5.0.x
76+
dotnet-version: 6.0.x
7777
- name: Create Release NuGet package
7878
run: |
7979
arrTag=(${GITHUB_REF//\// })

Directory.Build.props

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<SignAssembly>false</SignAssembly>
1717
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
1818
</PropertyGroup>
19-
19+
2020
<PropertyGroup>
2121
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -41,39 +41,15 @@
4141
</PropertyGroup>
4242

4343
<PropertyGroup>
44-
<LangVersion>9.0</LangVersion>
44+
<LangVersion>10.0</LangVersion>
4545
<Nullable>enable</Nullable>
4646
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
4747
<Features>strict</Features>
4848
</PropertyGroup>
4949

50-
<Choose>
51-
<When Condition="'$(TargetFramework)' == 'netstandard2.1'">
52-
<PropertyGroup>
53-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
54-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
55-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
56-
<DefineConstants>$(DefineConstants);SUPPORTS_NULLABLEREFATTRIBUTES</DefineConstants>
57-
</PropertyGroup>
58-
</When>
59-
<When Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
60-
<PropertyGroup>
61-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
62-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
63-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
64-
<DefineConstants>$(DefineConstants);SUPPORTS_NULLABLEREFATTRIBUTES</DefineConstants>
65-
</PropertyGroup>
66-
</When>
67-
<When Condition="'$(TargetFramework)' == 'net5.0'">
68-
<PropertyGroup>
69-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
70-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
71-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
72-
<DefineConstants>$(DefineConstants);SUPPORTS_NULLABLEREFATTRIBUTES</DefineConstants>
73-
<DefineConstants>$(DefineConstants);SUPPORTS_MEMBERNOTNULL</DefineConstants>
74-
<DefineConstants>$(DefineConstants);SUPPORTS_INIT</DefineConstants>
75-
</PropertyGroup>
76-
</When>
77-
</Choose>
50+
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)','net5.0'))">
51+
<DefineConstants>$(DefineConstants);SUPPORTS_MEMBERNOTNULL</DefineConstants>
52+
<DefineConstants>$(DefineConstants);SUPPORTS_INIT</DefineConstants>
53+
</PropertyGroup>
7854

7955
</Project>

Directory.Build.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
<DefineConstants>$(DefineConstants);$(OS)</DefineConstants>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<PackageReference Update="SixLabors.ImageSharp" Version="1.0.4" />
11+
<PackageReference Update="Synercoding.Primitives" Version="1.0.0-rc08" />
12+
</ItemGroup>
13+
914
</Project>

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public static void Main(string[] args)
3232
info.Title = "Example 1";
3333
info.ExtraInfo.Add("CutContourProgramId", "cloud-shape");
3434
})
35+
// Add image to writer directly and then use that image in the page
36+
.AddPage(page =>
37+
{
38+
page.MediaBox = mediaBox;
39+
page.TrimBox = trimBox;
40+
41+
using (var blurStream = File.OpenRead("Pexels_com/4k-wallpaper-blur-bokeh-1484253.jpg"))
42+
{
43+
var addedImage = writer.AddJpgImageUnsafe(blurStream, 7000, 4672);
44+
var scale = (double)addedImage.Width / addedImage.Height;
45+
page.AddImage(addedImage, new Rectangle(0, 0, scale * 303, 303, Unit.Millimeters));
46+
}
47+
})
3548
// Test placement using rectangle
3649
.AddPage(page =>
3750
{
@@ -119,7 +132,7 @@ public static void Main(string[] args)
119132
{
120133
var reusedImage = writer.AddImage(blurImage);
121134

122-
for(int i = 0; i < 4; i++)
135+
for (int i = 0; i < 4; i++)
123136
{
124137
writer.AddPage(page =>
125138
{

samples/Synercoding.FileFormats.Pdf.ConsoleTester/Synercoding.FileFormats.Pdf.ConsoleTester.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
5+
<TargetFramework>net6.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33

44
<PropertyGroup>
5-
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
66
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props</MSBuildAllProjects>
77
<SynercodingProjectCategory>src</SynercodingProjectCategory>
88
</PropertyGroup>

src/Directory.Build.targets

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@
77

88
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.targets" />
99

10-
<!-- Empty target so that `dotnet test` will work on the solution -->
11-
<!-- https://github.com/Microsoft/vstest/issues/411 -->
12-
<Target Name="VSTest" />
13-
1410
</Project>

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Synercoding.FileFormats.Pdf.Internals;
2-
using Synercoding.FileFormats.Pdf.LowLevel;
32
using Synercoding.Primitives;
43
using System;
54
using System.IO;
@@ -98,13 +97,16 @@ public static PdfPage AddImage(this PdfPage page, Stream imageStream, Rectangle
9897
/// <summary>
9998
/// Add an image to the pdf page
10099
/// </summary>
100+
/// <remarks>
101+
/// The <paramref name="jpgStream"/> is not checked, and is used as is. Make sure only streams that represent a JPG are used.
102+
/// </remarks>
101103
/// <param name="page">The page to add the image to</param>
102104
/// <param name="jpgStream">The image to add</param>
103105
/// <param name="originalWidth">The original width of the image</param>
104106
/// <param name="originalHeight">The original height of the image</param>
105107
/// <param name="matrix">The placement matrix</param>
106108
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
107-
public static PdfPage AddImage(this PdfPage page, Stream jpgStream, int originalWidth, int originalHeight, Matrix matrix)
109+
public static PdfPage AddJpgImageUnsafe(this PdfPage page, Stream jpgStream, int originalWidth, int originalHeight, Matrix matrix)
108110
{
109111
page.ContentStream
110112
.SaveState()
@@ -118,14 +120,17 @@ public static PdfPage AddImage(this PdfPage page, Stream jpgStream, int original
118120
/// <summary>
119121
/// Add an image to the pdf page
120122
/// </summary>
123+
/// <remarks>
124+
/// The <paramref name="jpgStream"/> is not checked, and is used as is. Make sure only streams that represent a JPG are used.
125+
/// </remarks>
121126
/// <param name="page">The page to add the image to</param>
122127
/// <param name="jpgStream">The image to add</param>
123128
/// <param name="originalWidth">The original width of the image</param>
124129
/// <param name="originalHeight">The original height of the image</param>
125130
/// <param name="rectangle">The placement rectangle</param>
126131
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
127-
public static PdfPage AddImage(this PdfPage page, Stream jpgStream, int originalWidth, int originalHeight, Rectangle rectangle)
128-
=> page.AddImage(jpgStream, originalWidth, originalHeight, rectangle.AsPlacementMatrix());
132+
public static PdfPage AddJpgImageUnsafe(this PdfPage page, Stream jpgStream, int originalWidth, int originalHeight, Rectangle rectangle)
133+
=> page.AddJpgImageUnsafe(jpgStream, originalWidth, originalHeight, rectangle.AsPlacementMatrix());
129134

130135
/// <summary>
131136
/// Add shapes to the pdf page

src/Synercoding.FileFormats.Pdf/Image.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ namespace Synercoding.FileFormats.Pdf
1212
public sealed class Image : IPdfObject, IDisposable
1313
{
1414
private readonly Stream _imageStream;
15-
private readonly int _width;
16-
private readonly int _height;
17-
1815
private bool _disposed;
1916
private bool _isWritten;
2017

@@ -28,8 +25,8 @@ internal Image(PdfReference id, SixLabors.ImageSharp.Image image)
2825
Quality = 100,
2926
Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio420
3027
});
31-
_width = image.Width;
32-
_height = image.Height;
28+
Width = image.Width;
29+
Height = image.Height;
3330
ms.Position = 0;
3431
_imageStream = ms;
3532
}
@@ -38,14 +35,24 @@ internal Image(PdfReference id, Stream jpgStream, int width, int height)
3835
{
3936
Reference = id;
4037

41-
_width = width;
42-
_height = height;
38+
Width = width;
39+
Height = height;
4340
_imageStream = jpgStream;
4441
}
4542

4643
/// <inheritdoc />
4744
public PdfReference Reference { get; private set; }
4845

46+
/// <summary>
47+
/// The width of this <see cref="Image"/>
48+
/// </summary>
49+
public int Width { get; }
50+
51+
/// <summary>
52+
/// The height of this <see cref="Image"/>
53+
/// </summary>
54+
public int Height { get; }
55+
4956
/// <inheritdoc />
5057
public void Dispose()
5158
{
@@ -72,8 +79,8 @@ internal bool TryWriteToStream(PdfStream stream, out uint position)
7279
dictionary
7380
.Type(ObjectType.XObject)
7481
.SubType(XObjectSubType.Image)
75-
.Write(PdfName.Get("Width"), image._width)
76-
.Write(PdfName.Get("Height"), image._height)
82+
.Write(PdfName.Get("Width"), image.Width)
83+
.Write(PdfName.Get("Height"), image.Height)
7784
.Write(PdfName.Get("ColorSpace"), PdfName.Get("DeviceRGB"))
7885
.Write(PdfName.Get("BitsPerComponent"), 8)
7986
.Write(PdfName.Get("Decode"), 0f, 1f, 0f, 1f, 0f, 1f);

src/Synercoding.FileFormats.Pdf/PdfWriter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ public Image AddImage(SixLabors.ImageSharp.Image image)
111111
return pdfImage;
112112
}
113113

114+
/// <summary>
115+
/// Add an jpg <see cref="Stream"/> to the pdf file and get the <see cref="Image"/> reference returned
116+
/// </summary>
117+
/// <remarks>
118+
/// The <paramref name="jpgStream"/> is not checked, and is used as is. Make sure only streams that represent a JPG are used.
119+
/// </remarks>
120+
/// <param name="jpgStream">The <see cref="Stream"/> of the jpg image that needs to be added.</param>
121+
/// <param name="originalWidth">The width of the image in the <paramref name="jpgStream"/>.</param>
122+
/// <param name="originalHeight">The height of the image in the <paramref name="jpgStream"/>.</param>
123+
/// <returns>The image reference that can be used in pages</returns>
124+
public Image AddJpgImageUnsafe(Stream jpgStream, int originalWidth, int originalHeight)
125+
{
126+
var id = _tableBuilder.ReserveId();
127+
128+
var pdfImage = new Image(id, jpgStream, originalWidth, originalHeight);
129+
130+
if (!pdfImage.TryWriteToStream(_stream, out uint position))
131+
throw new InvalidOperationException("Image was just created but could not be written to stream.");
132+
133+
_tableBuilder.SetPosition(id, position);
134+
135+
return pdfImage;
136+
}
137+
114138
/// <inheritdoc />
115139
public void Dispose()
116140
{

src/Synercoding.FileFormats.Pdf/Synercoding.FileFormats.Pdf.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
<Import Project="$(MSBuildThisFileDirectory)\PackageDetails.props" />
44

5-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
6-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
7-
</ItemGroup>
8-
95
<ItemGroup>
10-
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
11-
<PackageReference Include="Synercoding.Primitives" Version="1.0.0-rc03" />
6+
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
7+
<PackageReference Include="Synercoding.Primitives" Version="1.0.0-rc08" />
128
</ItemGroup>
139

1410
</Project>

src/Synercoding.FileFormats.Pdf/System/ExcludeFromCodeCoverageAttribute.cs

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

0 commit comments

Comments
 (0)