2018-11-25 04:51:27 +08:00
|
|
|
|
namespace UglyToad.PdfPig.Content
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-08-25 22:06:37 +08:00
|
|
|
|
using System.Text;
|
2018-11-25 04:51:27 +08:00
|
|
|
|
using Geometry;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Word
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text of the word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Text { get; }
|
|
|
|
|
|
2019-05-13 02:34:00 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The text direction of the word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TextDirection TextDirection { get; }
|
|
|
|
|
|
2018-11-25 04:51:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The rectangle completely containing the word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PdfRectangle BoundingBox { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The name of the font for the word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string FontName { get; }
|
|
|
|
|
|
2019-05-13 02:34:00 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The letters contained in the word.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<Letter> Letters { get; }
|
|
|
|
|
|
2018-11-25 04:51:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new <see cref="Word"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="letters">The letters contained in the word.</param>
|
|
|
|
|
public Word(IReadOnlyList<Letter> letters)
|
|
|
|
|
{
|
|
|
|
|
if (letters == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(letters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (letters.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Empty letters provided.", nameof(letters));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 02:34:00 +08:00
|
|
|
|
Letters = letters;
|
|
|
|
|
|
2019-08-25 22:06:37 +08:00
|
|
|
|
var builder = new StringBuilder();
|
2018-11-25 04:51:27 +08:00
|
|
|
|
|
2019-08-25 22:06:37 +08:00
|
|
|
|
var minX = decimal.MaxValue;
|
|
|
|
|
var minY = decimal.MaxValue;
|
|
|
|
|
var maxX = decimal.MinValue;
|
|
|
|
|
var maxY = decimal.MinValue;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < letters.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var letter = letters[i];
|
|
|
|
|
builder.Append(letter.Value);
|
|
|
|
|
|
|
|
|
|
if (letter.Location.X < minX)
|
|
|
|
|
{
|
|
|
|
|
minX = letter.Location.X;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (letter.Location.Y < minY)
|
|
|
|
|
{
|
|
|
|
|
minY = letter.Location.Y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var right = letter.Location.X + letter.Width;
|
|
|
|
|
if (right > maxX)
|
|
|
|
|
{
|
|
|
|
|
maxX = right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (letter.GlyphRectangle.Top > maxY)
|
|
|
|
|
{
|
|
|
|
|
maxY = letter.GlyphRectangle.Top;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Text = builder.ToString();
|
2018-11-25 04:51:27 +08:00
|
|
|
|
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
|
2019-05-13 02:34:00 +08:00
|
|
|
|
|
2018-11-25 04:51:27 +08:00
|
|
|
|
FontName = letters[0].FontName;
|
2019-05-13 02:34:00 +08:00
|
|
|
|
TextDirection = letters[0].TextDirection;
|
2018-11-25 04:51:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|