using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig.Geometry;
namespace UglyToad.PdfPig.Content
{
///
/// A line of text.
///
public class TextLine
{
///
/// The text of the line.
///
public string Text { get; }
///
/// The text direction of the line.
///
public TextDirection TextDirection { get; }
///
/// The rectangle completely containing the line.
///
public PdfRectangle BoundingBox { get; }
///
/// The words contained in the line.
///
public IReadOnlyList Words { get; }
///
/// Create a new .
///
/// The words contained in the word.
public TextLine(IReadOnlyList words)
{
if (words == null)
{
throw new ArgumentNullException(nameof(words));
}
if (words.Count == 0)
{
throw new ArgumentException("Empty words provided.", nameof(words));
}
Words = words;
Text = string.Join(" ", words.Where(s => !string.IsNullOrWhiteSpace(s.Text)).Select(x => x.Text));
var minX = words.Min(x => x.BoundingBox.Left);
var minY = words.Min(x => x.BoundingBox.Bottom);
var maxX = words.Max(x => x.BoundingBox.Right);
var maxY = words.Max(x => x.BoundingBox.Top);
BoundingBox = new PdfRectangle(minX, minY, maxX, maxY);
if (words.All(x => x.TextDirection == words[0].TextDirection))
{
TextDirection = words[0].TextDirection;
}
else
{
TextDirection = TextDirection.Unknown;
}
}
///
public override string ToString()
{
return Text;
}
}
}