Skip to content

Commit

Permalink
Merge pull request #49 from synercoder/features/initial-text-support
Browse files Browse the repository at this point in the history
Initial text support
  • Loading branch information
synercoder authored Jan 18, 2023
2 parents 69f3128 + 9f5c58a commit b83be94
Show file tree
Hide file tree
Showing 24 changed files with 890 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Restore
run: dotnet restore
- name: Build
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Create Release NuGet package
run: |
arrTag=(${GITHUB_REF//\// })
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</PropertyGroup>

<PropertyGroup>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Features>strict</Features>
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Update="SixLabors.ImageSharp" Version="1.0.4" />
<PackageReference Update="SixLabors.ImageSharp" Version="2.1.*" />
<PackageReference Update="Synercoding.Primitives" Version="1.0.0-rc08" />
</ItemGroup>

Expand Down
31 changes: 31 additions & 0 deletions samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Synercoding.FileFormats.Pdf.Extensions;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
using Synercoding.FileFormats.Pdf.LowLevel.Text;
using Synercoding.Primitives;
using Synercoding.Primitives.Extensions;
using System;
using System.IO;
using static Synercoding.Primitives.ValueCreator;

namespace Synercoding.FileFormats.Pdf.ConsoleTester
{
Expand All @@ -30,6 +32,7 @@ public static void Main(string[] args)
{
info.Author = "Gerard Gunnewijk";
info.Title = "Example 1";
info.Creator = "Synercoding.FileFormats.Pdf";
info.ExtraInfo.Add("CutContourProgramId", "cloud-shape");
})
// Add image to writer directly and then use that image in the page
Expand Down Expand Up @@ -73,6 +76,9 @@ public static void Main(string[] args)
// Test shape graphics
.AddPage(page =>
{
page.MediaBox = mediaBox;
page.TrimBox = trimBox;
page.AddShapes(ctx =>
{
ctx.DefaultState(g =>
Expand Down Expand Up @@ -109,6 +115,31 @@ public static void Main(string[] args)
.Close();
});
})
// Test pages with text
.AddPage(page =>
{
page.MediaBox = mediaBox;
page.TrimBox = trimBox;
page.AddText("The quick brown fox jumps over the lazy dog.", new Point(Mm(10), Mm(10)), new TextState(StandardFonts.Helvetica, 12)
{
Fill = PredefinedColors.Blue
});
page.AddText("Text with a newline" + Environment.NewLine + "in it.", new Point(Mm(10), Mm(20)));
})
.AddPage(page =>
{
page.MediaBox = mediaBox;
page.TrimBox = trimBox;
page.AddText("This page also used Helvetica", new Point(Mm(10), Mm(10)), state =>
{
state.FontSize = 32;
state.Font = StandardFonts.Helvetica;
state.RenderingMode = TextRenderingMode.Stroke;
state.Stroke = PredefinedColors.Red;
});
})
// Test placement using matrix
.AddPage(page =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0;netstandard2.1</TargetFrameworks>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props</MSBuildAllProjects>
<SynercodingProjectCategory>src</SynercodingProjectCategory>
</PropertyGroup>
Expand All @@ -13,7 +13,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='DEBUG'">
<WarningsNotAsErrors>CS1591</WarningsNotAsErrors>
</PropertyGroup>

Expand Down
83 changes: 83 additions & 0 deletions src/Synercoding.FileFormats.Pdf/Extensions/PdfPageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Synercoding.FileFormats.Pdf.Internals;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
using Synercoding.FileFormats.Pdf.LowLevel.Operators.Color;
using Synercoding.FileFormats.Pdf.LowLevel.Operators.State;
using Synercoding.FileFormats.Pdf.LowLevel.Text;
using Synercoding.Primitives;
using System;
using System.IO;
Expand All @@ -10,6 +15,84 @@ namespace Synercoding.FileFormats.Pdf.Extensions
/// </summary>
public static class PdfPageExtensions
{
/// <summary>
/// Add text to the page.
/// </summary>
/// <param name="page">The page to add the text to.</param>
/// <param name="text">The text to add.</param>
/// <param name="point">The location of the text.</param>
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
public static PdfPage AddText(this PdfPage page, string text, Point point)
=> page.AddText(text, point, new TextState());

/// <summary>
/// Add text to the page.
/// </summary>
/// <param name="page">The page to add the text to.</param>
/// <param name="text">The text to add.</param>
/// <param name="point">The location of the text.</param>
/// <param name="configureState">Configure the state of the text to place.</param>
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
public static PdfPage AddText(this PdfPage page, string text, Point point, Action<TextState> configureState)
{
var state = new TextState();
configureState(state);

return page.AddText(text, point, state);
}

/// <summary>
/// Add text to the page.
/// </summary>
/// <param name="page">The page to add the text to.</param>
/// <param name="text">The text to add.</param>
/// <param name="point">The location of the text.</param>
/// <param name="state">The state of the text to place.</param>
/// <returns>The same <see cref="PdfPage"/> to chain other calls.</returns>
public static PdfPage AddText(this PdfPage page, string text, Point point, TextState state)
{
page.MarkStdFontAsUsed(state.Font);

page.ContentStream
.SaveState()
.BeginText()
.SetTextPosition(point)
.SetFontAndSize(state.Font.LookupName, state.FontSize)
.SetTextLeading(state.Leading ?? state.FontSize);

if (state.Fill is Color fill)
page.ContentStream.SetColorFill(fill);
if (state.Stroke is Color stroke)
page.ContentStream.SetColorStroke(stroke);
if (state.LineWidth is double lineWidth)
page.ContentStream.Write(new LineWidthOperator(lineWidth));
if (state.LineCap is LineCapStyle lineCapStyle)
page.ContentStream.Write(new LineCapOperator(lineCapStyle));
if (state.LineJoin is LineJoinStyle lineJoinStyle)
page.ContentStream.Write(new LineJoinOperator(lineJoinStyle));
if (state.MiterLimit is double miterLimit)
page.ContentStream.Write(new MiterLimitOperator(miterLimit));
if (state.Dash is Dash dash)
page.ContentStream.Write(new DashOperator(dash.Array, dash.Phase));
if (state.CharacterSpacing is float charSpace)
page.ContentStream.SetCharacterSpacing(charSpace);
if (state.WordSpacing is float wordSpace)
page.ContentStream.SetWordSpacing(wordSpace);
if (state.HorizontalScaling is float horizontalScaling)
page.ContentStream.SetHorizontalScaling(horizontalScaling);
if (state.TextRise is float textRise)
page.ContentStream.SetTextRise(textRise);
if (state.RenderingMode is TextRenderingMode textRenderingMode)
page.ContentStream.SetTextRenderMode(textRenderingMode);

page.ContentStream
.ShowText(text)
.EndText()
.RestoreState();

return page;
}

/// <summary>
/// Add an image to the pdf page
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Synercoding.FileFormats.Pdf/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal Image(PdfReference id, SixLabors.ImageSharp.Image image)
image.SaveAsJpeg(ms, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder()
{
Quality = 100,
Subsample = SixLabors.ImageSharp.Formats.Jpeg.JpegSubsample.Ratio420
ColorType = SixLabors.ImageSharp.Formats.Jpeg.JpegColorType.YCbCrRatio444
});
Width = image.Width;
Height = image.Height;
Expand Down
Loading

0 comments on commit b83be94

Please sign in to comment.