Skip to content

Commit

Permalink
Add pdf dictionary output methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Aug 17, 2024
1 parent 6057b06 commit c7e197c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.1
2.4.0
42 changes: 42 additions & 0 deletions src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,48 @@ public function getFontsBlock(): string
return $this->out;
}

/**
* Get the PDF output string for Font resources dictionary.
*
* @return string
*/
public function getOutFontDict(): string
{
if (empty($this->fonts)) {
return '';
}

$out = ' /Font <<';

foreach ($this->fonts as $font) {
$out .= ' /F' . $font['i'] . ' ' . $font['n'] . ' 0 R';
}

return $out . ' >>';
}

/**
* Get the PDF output string for XOBject Font resources dictionary.
*
* @param array<string> $keys Array of font keys.
*
* @return string
*/
public function getOutFontDictByKeys(array $keys): string
{
if (empty($keys)) {
return '';
}

$out = ' /Font <<';

foreach ($keys as $key) {
$out .= ' /F' . $this->fonts[$key]['i'] . ' ' . $this->fonts[$key]['n'] . ' 0 R';
}

return $out . ' >>';
}

/**
* Get the PDF output string for font encoding diffs
*
Expand Down
4 changes: 0 additions & 4 deletions src/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ protected function getFontMetric(array $font): array
$wratio = ($cratio * $font['stretching']); // horizontal ratio
$data = $this->getFont($font['key']);
$outfont = sprintf('/F%d %F Tf', $data['i'], $font['size']); // PDF output string
$objname = 'F' . $data['i'];
// add this font in the stack wit metrics in internal units
$this->metric[$mkey] = [
'ascent' => ((float) $data['desc']['Ascent'] * $cratio),
Expand All @@ -469,9 +468,6 @@ protected function getFontMetric(array $font): array
'maxwidth' => ((float) $data['desc']['MaxWidth'] * $cratio * $font['stretching']),
'midpoint' => ((float) ($data['desc']['Ascent'] + $data['desc']['Descent']) * $cratio / 2),
'missingwidth' => ((float) $data['desc']['MissingWidth'] * $cratio * $font['stretching']),
'objdic' => ' /' . $objname . ' ' . $data['n'] . ' 0 R',
'objid' => $data['n'],
'objname' => $objname,
'out' => 'BT ' . $outfont . ' ET' . "\r",
'outraw' => $outfont,
'size' => $size,
Expand Down
9 changes: 9 additions & 0 deletions test/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,14 @@ public function testOutput(): void
$this->assertEquals(37, $output->getObjectNumber());

$this->assertNotEmpty($output->getFontsBlock());

$this->assertNotEmpty($output->getOutFontDict());

$keys = [];
foreach ($fonts as $font) {
$keys[] = $font['key'];
}

$this->assertNotEmpty($output->getOutFontDictByKeys($keys));
}
}

0 comments on commit c7e197c

Please sign in to comment.