mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-18 18:27:55 +08:00
Implement DuplicateOverlappingTextProcessor
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
namespace UglyToad.PdfPig.DocumentLayoutAnalysis
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UglyToad.PdfPig.Content;
|
||||
|
||||
/// <summary>
|
||||
/// Checks if each letter is a duplicate and overlaps any other letter and remove the duplicate. Duplicate overlapping letters might be used to simulate bold.
|
||||
/// <para>Logic inspired from PdfBox's PDFTextStripper class.</para>
|
||||
/// </summary>
|
||||
public static class DuplicateOverlappingTextProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if each letter is a duplicate and overlaps any other letter and remove the duplicate. Duplicate overlapping letters might be used to simulate bold.
|
||||
/// <para>Logic inspired from PdfBox's PDFTextStripper class.</para>
|
||||
/// </summary>
|
||||
/// <param name="letters">Letters to be processed.</param>
|
||||
/// <returns>Letters with no duplicate overlapping.</returns>
|
||||
public static IReadOnlyList<Letter> Get(IEnumerable<Letter> letters)
|
||||
{
|
||||
if (letters?.Any() != true)
|
||||
{
|
||||
return letters?.ToList();
|
||||
}
|
||||
|
||||
var queue = new Queue<Letter>(letters);
|
||||
var cleanLetters = new List<Letter>() { queue.Dequeue() }; // dequeue the first letter
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var letter = queue.Dequeue();
|
||||
bool addLetter = true;
|
||||
|
||||
var duplicates = cleanLetters.Where(l => l.Value.Equals(letter.Value) && l.FontName.Equals(letter.FontName)); // do other checks?
|
||||
|
||||
if (duplicates.Any())
|
||||
{
|
||||
double tolerance = letter.GlyphRectangle.Width / (letter.Value.Length == 0 ? 1 : letter.Value.Length) / 3.0;
|
||||
double minX = letter.GlyphRectangle.BottomLeft.X - tolerance;
|
||||
double maxX = letter.GlyphRectangle.BottomLeft.X + tolerance;
|
||||
double minY = letter.GlyphRectangle.BottomLeft.Y - tolerance;
|
||||
double maxY = letter.GlyphRectangle.BottomLeft.Y + tolerance;
|
||||
|
||||
var duplicatesOverlapping = duplicates.FirstOrDefault(l => minX <= l.GlyphRectangle.BottomLeft.X &&
|
||||
maxX >= l.GlyphRectangle.BottomLeft.X &&
|
||||
minY <= l.GlyphRectangle.BottomLeft.Y &&
|
||||
maxY >= l.GlyphRectangle.BottomLeft.Y);
|
||||
|
||||
if (duplicatesOverlapping != default)
|
||||
{
|
||||
// duplicate overlapping letter was found, keeping the existing one and not adding this one.
|
||||
addLetter = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (addLetter)
|
||||
{
|
||||
cleanLetters.Add(letter);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: update font details to bold
|
||||
// TODO: need to update the bounding box
|
||||
// TODO: need to update bottom left/right
|
||||
// TODO: need to update width
|
||||
// update textSequence?
|
||||
}
|
||||
}
|
||||
|
||||
return cleanLetters;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user