#21 add a test for accented characters and fill in more writing methods for content stream operators

the output is currently incorrect for accented characters
This commit is contained in:
Eliot Jones
2018-12-14 18:33:01 +00:00
parent 924fc7b37f
commit 39786ac00a
12 changed files with 81 additions and 13 deletions

View File

@@ -207,6 +207,23 @@
}
}
[Fact]
public void CanWriteSinglePageWithAccentedCharacters()
{
var builder = new PdfDocumentBuilder();
var page = builder.AddPage(PageSize.A4);
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "TrueType");
var file = Path.Combine(path, "Roboto-Regular.ttf");
var font = builder.AddTrueTypeFont(File.ReadAllBytes(file));
page.AddText("é (lower case, upper case É).", 9,
new PdfPoint(30, page.PageSize.Height - 50), font);
WriteFile(nameof(CanWriteSinglePageWithAccentedCharacters), builder.Build());
}
private static void WriteFile(string name, byte[] bytes)
{
try

View File

@@ -77,7 +77,7 @@
{
using (var memoryStream = new MemoryStream(input))
{
// The first 2 bytes are the header which DelfateStream does not support.
// The first 2 bytes are the header which DeflateStream does not support.
memoryStream.ReadByte();
memoryStream.ReadByte();

View File

@@ -5,6 +5,9 @@
using Content;
using PdfPig.Core;
/// <summary>
/// Modify the current transformation matrix by concatenating the specified matrix.
/// </summary>
internal class ModifyCurrentTransformationMatrix : IGraphicsStateOperation
{
public const string Symbol = "cm";

View File

@@ -4,9 +4,13 @@
using System.IO;
using Content;
/// <summary>
/// Restore the graphics state by removing the most recently saved state from the stack and making it the current state.
/// </summary>
internal class Pop : IGraphicsStateOperation
{
public const string Symbol = "Q";
public static readonly Pop Value = new Pop();
public string Operator => Symbol;
@@ -30,7 +34,8 @@
public void Write(Stream stream)
{
throw new NotImplementedException();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()

View File

@@ -3,6 +3,9 @@
using System.IO;
using Content;
/// <summary>
/// Save the current graphics state on the graphics state stack.
/// </summary>
internal class Push : IGraphicsStateOperation
{
public const string Symbol = "q";
@@ -14,11 +17,6 @@
{
}
public override string ToString()
{
return Symbol;
}
public void Run(IOperationContext context, IResourceStore resourceStore)
{
context.PushState();
@@ -26,7 +24,13 @@
public void Write(Stream stream)
{
throw new System.NotImplementedException();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()
{
return Symbol;
}
}
}

View File

@@ -4,6 +4,9 @@
using Content;
using PdfPig.Core;
/// <summary>
/// Begin a text object, initializing the text matrix and the text line matrix to the identity matrix. Text objects cannot be nested.
/// </summary>
internal class BeginText : IGraphicsStateOperation
{
public const string Symbol = "BT";

View File

@@ -4,6 +4,9 @@
using Content;
using PdfPig.Core;
/// <summary>
/// End a text object, discarding the text matrix.
/// </summary>
internal class EndText : IGraphicsStateOperation
{
public const string Symbol = "ET";

View File

@@ -30,7 +30,8 @@
public void Write(Stream stream)
{
throw new System.NotImplementedException();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()

View File

@@ -4,6 +4,10 @@
using Content;
using TextState;
/// <summary>
/// Move to the start of the next line, offset from the start of the current line by (tx, ty).
/// This operator also sets the leading parameter in the text state.
/// </summary>
internal class MoveToNextLineWithOffsetSetLeading : IGraphicsStateOperation
{
public const string Symbol = "TD";
@@ -33,7 +37,12 @@
public void Write(Stream stream)
{
throw new System.NotImplementedException();
stream.WriteDecimal(Tx);
stream.WriteWhiteSpace();
stream.WriteDecimal(Ty);
stream.WriteWhiteSpace();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()

View File

@@ -5,6 +5,9 @@
using Content;
using PdfPig.Core;
/// <summary>
/// Set the text matrix and the text line matrix.
/// </summary>
internal class SetTextMatrix : IGraphicsStateOperation
{
public const string Symbol = "Tm";
@@ -33,7 +36,20 @@
public void Write(Stream stream)
{
throw new NotImplementedException();
stream.WriteDecimal(Value[0]);
stream.WriteWhiteSpace();
stream.WriteDecimal(Value[1]);
stream.WriteWhiteSpace();
stream.WriteDecimal(Value[2]);
stream.WriteWhiteSpace();
stream.WriteDecimal(Value[3]);
stream.WriteWhiteSpace();
stream.WriteDecimal(Value[4]);
stream.WriteWhiteSpace();
stream.WriteDecimal(Value[5]);
stream.WriteWhiteSpace();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()

View File

@@ -3,6 +3,10 @@
using System.IO;
using Content;
/// <summary>
/// Set the character spacing to a number expressed in unscaled text space units.
/// Initial value: 0.
/// </summary>
internal class SetCharacterSpacing : IGraphicsStateOperation
{
public const string Symbol = "Tc";
@@ -25,7 +29,10 @@
public void Write(Stream stream)
{
throw new System.NotImplementedException();
stream.WriteDecimal(Spacing);
stream.WriteWhiteSpace();
stream.WriteText(Symbol);
stream.WriteNewLine();
}
public override string ToString()

View File

@@ -165,7 +165,7 @@
if (!font.TryGetBoundingAdvancedWidth(characterCode, out var width))
{
throw new InvalidFontFormatException($"Could not find advanced with for character named '{pair.Value}' with character code: {characterCode}.");
width = font.TableRegister.HorizontalMetricsTable.AdvancedWidths[0];
}
widths[pair.Key - firstCharacter] = new NumericToken(width * scaling);