Skip to content

Commit

Permalink
Replace System.Drawing by SkiaSharp in Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Jan 14, 2024
1 parent 5953ca9 commit 0a3bf9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// ReSharper disable CompareOfFloatsByEqualityOperator
namespace UglyToad.PdfPig.Tests.Fonts.TrueType.Parser
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using PdfPig.Core;
using PdfPig.Fonts.TrueType;
using PdfPig.Fonts.TrueType.Parser;
using PdfPig.Fonts.TrueType.Tables;
using UglyToad.PdfPig.Fonts.TrueType.Glyphs;
using UglyToad.PdfPig.Graphics;
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Xunit;

public class TrueTypeFontParserTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace UglyToad.PdfPig.Tests.Integration.VisualVerification
{
using PdfPig.Core;
using SkiaSharp;
using System;
using System.Drawing;
using System.IO;
using Xunit;

Expand All @@ -24,6 +24,12 @@ public class GenerateLetterBoundingBoxImages
private const string MOZILLA_10372_2File = "MOZILLA-10372-2";
private const string Type3FontZeroHeight = "type3-font-zero-height";

private const string OutputPath = "Images";

private static readonly SKPaint violetPen = new SKPaint() { Color = SKColors.BlueViolet, StrokeWidth = 1 };
private static readonly SKPaint redPen = new SKPaint() { Color = SKColors.Crimson, StrokeWidth = 1 };
private static readonly SKPaint bluePen = new SKPaint() { Color = SKColors.GreenYellow, StrokeWidth = 1 };

private static string GetFilename(string name)
{
var documentFolder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Integration", "Documents"));
Expand Down Expand Up @@ -155,12 +161,8 @@ private static void Run(string file, int imageHeight = 792, int pageNo = 1)

double scale = imageHeight / page.Height;

var violetPen = new Pen(Color.BlueViolet, 1);
var redPen = new Pen(Color.Crimson, 1);
var bluePen = new Pen(Color.GreenYellow, 1);

using (var bitmap = new Bitmap(image))
using (var graphics = Graphics.FromImage(bitmap))
using (var bitmap = SKBitmap.FromImage(image))
using (var graphics = new SKCanvas(bitmap))
{
foreach (var word in page.GetWords())
{
Expand All @@ -177,46 +179,51 @@ private static void Run(string file, int imageHeight = 792, int pageNo = 1)
DrawRectangle(annotation.Rectangle, graphics, bluePen, imageHeight, scale);
}

graphics.Flush();

var imageName = $"{file}.jpg";

if (!Directory.Exists("Images"))
if (!Directory.Exists(OutputPath))
{
Directory.CreateDirectory("Images");
Directory.CreateDirectory(OutputPath);
}

var savePath = Path.Combine("Images", imageName);
var savePath = Path.Combine(OutputPath, imageName);

bitmap.Save(savePath);
using (var fs = new FileStream(savePath, FileMode.Create))
using (SKData d = bitmap.Encode(SKEncodedImageFormat.Png, 100))
{
d.SaveTo(fs);
}
}
}
}

private static void DrawRectangle(PdfRectangle rectangle, Graphics graphics, Pen pen,
int imageHeight, double scale)
private static void DrawRectangle(PdfRectangle rectangle, SKCanvas graphics, SKPaint pen, int imageHeight, double scale)
{
int GetY(PdfPoint p)
{
return imageHeight - (int)(p.Y * scale);
}

Point GetPoint(PdfPoint p)
SKPoint GetPoint(PdfPoint p)
{
return new Point((int)(p.X * scale), GetY(p));
return new SKPoint((int)(p.X * scale), GetY(p));
}

graphics.DrawLine(pen, GetPoint(rectangle.BottomLeft), GetPoint(rectangle.BottomRight));
graphics.DrawLine(pen, GetPoint(rectangle.BottomRight), GetPoint(rectangle.TopRight));
graphics.DrawLine(pen, GetPoint(rectangle.TopRight), GetPoint(rectangle.TopLeft));
graphics.DrawLine(pen, GetPoint(rectangle.TopLeft), GetPoint(rectangle.BottomLeft));
graphics.DrawLine(GetPoint(rectangle.BottomLeft), GetPoint(rectangle.BottomRight), pen);
graphics.DrawLine(GetPoint(rectangle.BottomRight), GetPoint(rectangle.TopRight), pen);
graphics.DrawLine(GetPoint(rectangle.TopRight), GetPoint(rectangle.TopLeft), pen);
graphics.DrawLine(GetPoint(rectangle.TopLeft), GetPoint(rectangle.BottomLeft), pen);
}

private static Image GetCorrespondingImage(string filename)
private static SKImage GetCorrespondingImage(string filename)
{
var pdf = GetFilename(filename);

pdf = pdf.Replace(".pdf", ".jpg");

return Image.FromFile(pdf);
return SKImage.FromEncodedData(pdf);
}
}
}
2 changes: 1 addition & 1 deletion src/UglyToad.PdfPig.Tests/UglyToad.PdfPig.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
<PackageReference Include="SkiaSharp" Version="2.88.7" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
Expand Down

0 comments on commit 0a3bf9c

Please sign in to comment.