Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Document/Object/Decorator/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PrinsFrank\PdfParser\Exception\ParseFailureException;
use PrinsFrank\PdfParser\Exception\PdfParserException;
use PrinsFrank\PdfParser\Extraction\Text\TextExtractor;
use PrinsFrank\PdfParser\Extraction\Text\TextGrouping\LineGrouping\TextOverlapStrategy;

class Page extends DecoratedObject {
/**
Expand All @@ -27,7 +26,7 @@ public function getPositionedTextElements(): array {

/** @throws PdfParserException */
public function getText(): string {
return TextExtractor::extractText($this->getPositionedTextElements(), $this->document, $this, new TextOverlapStrategy());
return TextExtractor::extractText($this->getPositionedTextElements(), $this->document, $this);
}

/** @throws PdfParserException */
Expand Down
8 changes: 5 additions & 3 deletions src/Extraction/Text/TextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
use PrinsFrank\PdfParser\Document\Document;
use PrinsFrank\PdfParser\Document\Object\Decorator\Page;
use PrinsFrank\PdfParser\Exception\PdfParserException;
use PrinsFrank\PdfParser\Extraction\Text\TextGrouping\LineGrouping\LineGroupingStrategy;
use PrinsFrank\PdfParser\Extraction\Text\TextGrouping\LineGrouping\TextOverlapStrategy;

class TextExtractor {
/**
* @param list<PositionedTextElement> $positionedTextElements
* @throws PdfParserException
*/
public static function extractText(array $positionedTextElements, Document $document, Page $page, LineGroupingStrategy $lineGroupingStrategy): string {
public static function extractText(array $positionedTextElements, Document $document, Page $page): string {
$lineGroupedElements = TextOverlapStrategy::group($positionedTextElements);

$text = '';
foreach ($lineGroupingStrategy->group($positionedTextElements) as $i => $positionedTextElementsForLine) {
foreach ($lineGroupedElements as $i => $positionedTextElementsForLine) {
if ($i !== 0) {
$text .= "\n";
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PrinsFrank\PdfParser\Extraction\Text\TextGrouping\LineGrouping;

use Override;
use PrinsFrank\PdfParser\Document\ContentStream\PositionedText\PositionedTextElement;

/**
Expand All @@ -17,14 +16,14 @@
* Strategy where we sort all positioned text elements, retrieve the very first text element from the page (highest)
* And for each text element check if there is significant overlap above a threshold. Continue until all elements are processed
*/
class TextOverlapStrategy implements LineGroupingStrategy {
/** @param int<0, 100> $overlapPercentage */
public function __construct(
private readonly int $overlapPercentage = 90,
) {}
class TextOverlapStrategy {
private const OVERLAP_PERCENTAGE = 90;

#[Override]
public function group(array $positionedTextElements): iterable {
/**
* @param list<PositionedTextElement> $positionedTextElements
* @return iterable<list<PositionedTextElement>>
*/
public static function group(array $positionedTextElements): iterable {
usort(
$positionedTextElements,
fn(PositionedTextElement $a, PositionedTextElement $b): int => $b->absoluteMatrix->offsetY <=> $a->absoluteMatrix->offsetY,
Expand Down Expand Up @@ -66,7 +65,7 @@ public function group(array $positionedTextElements): iterable {
}

$overlap = min($highestElementTop, $currentElementTop) - max($highestPositionedTextElementBottom, $currentElementBottom);
$belongsOnLine = $overlap / $smallestElementHeight * 100 >= $this->overlapPercentage;
$belongsOnLine = $overlap / $smallestElementHeight * 100 >= self::OVERLAP_PERCENTAGE;
$isEnclosedSubscript = $overlap > 0.0
&& $positionedTextElementHeight < $highestPositionedTextElementHeight
&& $positionedTextElement->absoluteMatrix->offsetX >= $lineLeftX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testOrdersLinesTopToBottom(): void {

static::assertSame(
[[$top], [$bottom]],
iterator_to_array((new TextOverlapStrategy())->group([$bottom, $top]), false),
iterator_to_array(TextOverlapStrategy::group([$bottom, $top]), false),
);
}

Expand All @@ -33,7 +33,7 @@ public function testOrdersLinesTopToBottomOnANegativeOriginPage(): void {

static::assertSame(
[[$top], [$bottom]],
iterator_to_array((new TextOverlapStrategy())->group([$bottom, $top]), false),
iterator_to_array(TextOverlapStrategy::group([$bottom, $top]), false),
);
}
}
Loading