PdfPig/src/UglyToad.PdfPig/Graphics/Operations/TextState/SetFontAndSize.cs

66 lines
2.1 KiB
C#
Raw Normal View History

namespace UglyToad.PdfPig.Graphics.Operations.TextState
{
using System;
using System.Globalization;
using System.IO;
using Tokens;
2019-01-04 06:20:53 +08:00
/// <inheritdoc />
/// <summary>
/// Set the font and the font size.
2019-01-04 06:20:53 +08:00
/// Font is the name of a font resource in the Font subdictionary of the current resource dictionary.
/// Size is a number representing a scale factor.
/// </summary>
public class SetFontAndSize : IGraphicsStateOperation
{
2019-01-04 06:20:53 +08:00
/// <summary>
/// The symbol for this operation in a stream.
/// </summary>
public const string Symbol = "Tf";
2019-01-04 06:20:53 +08:00
/// <inheritdoc />
public string Operator => Symbol;
/// <summary>
/// The name of the font as defined in the resource dictionary.
/// </summary>
public NameToken Font { get; }
/// <summary>
/// The font program defines glyphs for a standard size. This standard size is set so that each line of text will occupy 1 unit in user space.
/// The size is the scale factor used to scale glyphs from the standard size to the display size rather than the font size in points.
/// </summary>
public double Size { get; }
2019-01-04 06:20:53 +08:00
/// <summary>
/// Create a new <see cref="SetFontAndSize"/>.
/// </summary>
/// <param name="font">The font name.</param>
/// <param name="size">The font size.</param>
public SetFontAndSize(NameToken font, double size)
{
Font = font ?? throw new ArgumentNullException(nameof(font));
Size = size;
}
2019-01-04 06:20:53 +08:00
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
operationContext.SetFontAndSize(Font, Size);
}
2019-01-04 06:20:53 +08:00
/// <inheritdoc />
public void Write(Stream stream)
{
stream.WriteText(Font.ToString());
stream.WriteWhiteSpace();
2019-01-04 06:20:53 +08:00
stream.WriteNumberText(Size, Symbol);
}
2019-01-04 06:20:53 +08:00
/// <inheritdoc />
public override string ToString()
{
return $"{Font} {Size.ToString("G", CultureInfo.InvariantCulture)} {Symbol}";
}
}
}