Skip to content

Commit 9f5c58a

Browse files
committed
Initial text support
1 parent 69f3128 commit 9f5c58a

24 files changed

+890
-36
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: 6.0.x
36+
dotnet-version: 7.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: 6.0.x
76+
dotnet-version: 7.0.x
7777
- name: Create Release NuGet package
7878
run: |
7979
arrTag=(${GITHUB_REF//\// })

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</PropertyGroup>
4242

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

Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Update="SixLabors.ImageSharp" Version="1.0.4" />
10+
<PackageReference Update="SixLabors.ImageSharp" Version="2.1.*" />
1111
<PackageReference Update="Synercoding.Primitives" Version="1.0.0-rc08" />
1212
</ItemGroup>
1313

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using Synercoding.FileFormats.Pdf.Extensions;
22
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
33
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
4+
using Synercoding.FileFormats.Pdf.LowLevel.Text;
45
using Synercoding.Primitives;
56
using Synercoding.Primitives.Extensions;
67
using System;
78
using System.IO;
9+
using static Synercoding.Primitives.ValueCreator;
810

911
namespace Synercoding.FileFormats.Pdf.ConsoleTester
1012
{
@@ -30,6 +32,7 @@ public static void Main(string[] args)
3032
{
3133
info.Author = "Gerard Gunnewijk";
3234
info.Title = "Example 1";
35+
info.Creator = "Synercoding.FileFormats.Pdf";
3336
info.ExtraInfo.Add("CutContourProgramId", "cloud-shape");
3437
})
3538
// Add image to writer directly and then use that image in the page
@@ -73,6 +76,9 @@ public static void Main(string[] args)
7376
// Test shape graphics
7477
.AddPage(page =>
7578
{
79+
page.MediaBox = mediaBox;
80+
page.TrimBox = trimBox;
81+
7682
page.AddShapes(ctx =>
7783
{
7884
ctx.DefaultState(g =>
@@ -109,6 +115,31 @@ public static void Main(string[] args)
109115
.Close();
110116
});
111117
})
118+
// Test pages with text
119+
.AddPage(page =>
120+
{
121+
page.MediaBox = mediaBox;
122+
page.TrimBox = trimBox;
123+
124+
page.AddText("The quick brown fox jumps over the lazy dog.", new Point(Mm(10), Mm(10)), new TextState(StandardFonts.Helvetica, 12)
125+
{
126+
Fill = PredefinedColors.Blue
127+
});
128+
page.AddText("Text with a newline" + Environment.NewLine + "in it.", new Point(Mm(10), Mm(20)));
129+
})
130+
.AddPage(page =>
131+
{
132+
page.MediaBox = mediaBox;
133+
page.TrimBox = trimBox;
134+
135+
page.AddText("This page also used Helvetica", new Point(Mm(10), Mm(10)), state =>
136+
{
137+
state.FontSize = 32;
138+
state.Font = StandardFonts.Helvetica;
139+
state.RenderingMode = TextRenderingMode.Stroke;
140+
state.Stroke = PredefinedColors.Red;
141+
});
142+
})
112143
// Test placement using matrix
113144
.AddPage(page =>
114145
{

src/Directory.Build.props

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

44
<PropertyGroup>
5-
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net7.0;net6.0;netstandard2.1</TargetFrameworks>
66
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props</MSBuildAllProjects>
77
<SynercodingProjectCategory>src</SynercodingProjectCategory>
88
</PropertyGroup>
@@ -13,7 +13,7 @@
1313
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1414
</PropertyGroup>
1515

16-
<PropertyGroup>
16+
<PropertyGroup Condition="'$(Configuration)'=='DEBUG'">
1717
<WarningsNotAsErrors>CS1591</WarningsNotAsErrors>
1818
</PropertyGroup>
1919

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
using Synercoding.FileFormats.Pdf.Internals;
2+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
3+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
4+
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color;
5+
using Synercoding.FileFormats.Pdf.LowLevel.Operators.State;
6+
using Synercoding.FileFormats.Pdf.LowLevel.Text;
27
using Synercoding.Primitives;
38
using System;
49
using System.IO;
@@ -10,6 +15,84 @@ namespace Synercoding.FileFormats.Pdf.Extensions
1015
/// </summary>
1116
public static class PdfPageExtensions
1217
{
18+
/// <summary>
19+
/// Add text to the page.
20+
/// </summary>
21+
/// <param name="page">The page to add the text to.</param>
22+
/// <param name="text">The text to add.</param>
23+
/// <param name="point">The location of the text.</param>
24+
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
25+
public static PdfPage AddText(this PdfPage page, string text, Point point)
26+
=> page.AddText(text, point, new TextState());
27+
28+
/// <summary>
29+
/// Add text to the page.
30+
/// </summary>
31+
/// <param name="page">The page to add the text to.</param>
32+
/// <param name="text">The text to add.</param>
33+
/// <param name="point">The location of the text.</param>
34+
/// <param name="configureState">Configure the state of the text to place.</param>
35+
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
36+
public static PdfPage AddText(this PdfPage page, string text, Point point, Action<TextState> configureState)
37+
{
38+
var state = new TextState();
39+
configureState(state);
40+
41+
return page.AddText(text, point, state);
42+
}
43+
44+
/// <summary>
45+
/// Add text to the page.
46+
/// </summary>
47+
/// <param name="page">The page to add the text to.</param>
48+
/// <param name="text">The text to add.</param>
49+
/// <param name="point">The location of the text.</param>
50+
/// <param name="state">The state of the text to place.</param>
51+
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
52+
public static PdfPage AddText(this PdfPage page, string text, Point point, TextState state)
53+
{
54+
page.MarkStdFontAsUsed(state.Font);
55+
56+
page.ContentStream
57+
.SaveState()
58+
.BeginText()
59+
.SetTextPosition(point)
60+
.SetFontAndSize(state.Font.LookupName, state.FontSize)
61+
.SetTextLeading(state.Leading ?? state.FontSize);
62+
63+
if (state.Fill is Color fill)
64+
page.ContentStream.SetColorFill(fill);
65+
if (state.Stroke is Color stroke)
66+
page.ContentStream.SetColorStroke(stroke);
67+
if (state.LineWidth is double lineWidth)
68+
page.ContentStream.Write(new LineWidthOperator(lineWidth));
69+
if (state.LineCap is LineCapStyle lineCapStyle)
70+
page.ContentStream.Write(new LineCapOperator(lineCapStyle));
71+
if (state.LineJoin is LineJoinStyle lineJoinStyle)
72+
page.ContentStream.Write(new LineJoinOperator(lineJoinStyle));
73+
if (state.MiterLimit is double miterLimit)
74+
page.ContentStream.Write(new MiterLimitOperator(miterLimit));
75+
if (state.Dash is Dash dash)
76+
page.ContentStream.Write(new DashOperator(dash.Array, dash.Phase));
77+
if (state.CharacterSpacing is float charSpace)
78+
page.ContentStream.SetCharacterSpacing(charSpace);
79+
if (state.WordSpacing is float wordSpace)
80+
page.ContentStream.SetWordSpacing(wordSpace);
81+
if (state.HorizontalScaling is float horizontalScaling)
82+
page.ContentStream.SetHorizontalScaling(horizontalScaling);
83+
if (state.TextRise is float textRise)
84+
page.ContentStream.SetTextRise(textRise);
85+
if (state.RenderingMode is TextRenderingMode textRenderingMode)
86+
page.ContentStream.SetTextRenderMode(textRenderingMode);
87+
88+
page.ContentStream
89+
.ShowText(text)
90+
.EndText()
91+
.RestoreState();
92+
93+
return page;
94+
}
95+
1396
/// <summary>
1497
/// Add an image to the pdf page
1598
/// </summary>

src/Synercoding.FileFormats.Pdf/Image.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal Image(PdfReference id, SixLabors.ImageSharp.Image image)
2323
image.SaveAsJpeg(ms, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder()
2424
{
2525
Quality = 100,
26-
Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio420
26+
ColorType = SixLabors.ImageSharp.Formats.Jpeg.JpegColorType.YCbCrRatio444
2727
});
2828
Width = image.Width;
2929
Height = image.Height;

0 commit comments

Comments
 (0)