Skip to content

Commit b4c96f0

Browse files
committed
add font stack size utility methods:
- hasCurrentFont() - getStackSize() - getCurrentFontIndex()
1 parent 04630d1 commit b4c96f0

3 files changed

Lines changed: 74 additions & 11 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.11.0
1+
2.12.0

src/Stack.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,22 @@ public function getCurrentFont(): array
223223
*
224224
* @param int $objnum Current PDF object number.
225225
* @param ?int $idx Font index. Leave it null to use the current font.
226-
* @param ?string $style Font style.
227-
* Possible values are (case-insensitive):
228-
* regular (default)
226+
* @param ?string $style Font style.
227+
* Possible values are (case-insensitive):
228+
* regular (default)
229229
* B: bold
230230
* I: italic
231231
* U: underline
232232
* D: strikeout (linethrough)
233233
* O: overline
234234
* @param ?float $size Font size in points (set to null to inherit the last font size).
235-
* @param ?float $spacing Extra spacing between characters.
236-
* @param ?float $stretching Horizontal character stretching ratio.
237-
*
238-
* @return TFontMetric
239-
*
240-
* @throws FontException
241-
*/
235+
* @param ?float $spacing Extra spacing between characters.
236+
* @param ?float $stretching Horizontal character stretching ratio.
237+
*
238+
* @return TFontMetric
239+
*
240+
* @throws FontException
241+
*/
242242
public function cloneFont(
243243
int &$objnum,
244244
?int $idx = null,
@@ -307,6 +307,36 @@ public function getCurrentFontType(): string
307307
return $this->getFont($this->stack[$this->index]['key'])['type'];
308308
}
309309

310+
/**
311+
* Returns true if a current font is available on the stack.
312+
*
313+
* @return bool
314+
*/
315+
public function hasCurrentFont(): bool
316+
{
317+
return ($this->index >= 0) && ($this->stack !== []);
318+
}
319+
320+
/**
321+
* Returns the number of fonts currently stored in the stack.
322+
*
323+
* @return int
324+
*/
325+
public function getStackSize(): int
326+
{
327+
return \count($this->stack);
328+
}
329+
330+
/**
331+
* Returns the current font index in the stack.
332+
*
333+
* @return int
334+
*/
335+
public function getCurrentFontIndex(): int
336+
{
337+
return $this->index;
338+
}
339+
310340
/**
311341
* Returns the PDF code to use the current font.
312342
*

test/StackTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,39 @@ public function testStackMissingFont(): void
186186
$stack->insert($objnum, 'missing');
187187
}
188188

189+
public function testHasCurrentFont(): void
190+
{
191+
$this->setupTest();
192+
$indir = \dirname(__DIR__) . '/util/vendor/tecnickcom/tc-font-mirror/';
193+
194+
$objnum = 1;
195+
$stack = new \Com\Tecnick\Pdf\Font\Stack(0.75, true, true, true);
196+
$this->assertFalse($stack->hasCurrentFont());
197+
$this->assertSame(0, $stack->getStackSize());
198+
$this->assertSame(-1, $stack->getCurrentFontIndex());
199+
200+
new \Com\Tecnick\Pdf\Font\Import($indir . 'freefont/FreeSans.ttf');
201+
$font = $stack->insert($objnum, 'freesans', '', 12);
202+
$this->assertTrue($stack->hasCurrentFont());
203+
$this->assertSame(1, $stack->getStackSize());
204+
$this->assertSame(0, $stack->getCurrentFontIndex());
205+
$this->assertSame($font['out'], $stack->getOutCurrentFont());
206+
207+
$stack->cloneFont($objnum, null, null, 13);
208+
$this->assertSame(2, $stack->getStackSize());
209+
$this->assertSame(1, $stack->getCurrentFontIndex());
210+
211+
$stack->popLastFont();
212+
$this->assertTrue($stack->hasCurrentFont());
213+
$this->assertSame(1, $stack->getStackSize());
214+
$this->assertSame(0, $stack->getCurrentFontIndex());
215+
216+
$stack->popLastFont();
217+
$this->assertFalse($stack->hasCurrentFont());
218+
$this->assertSame(0, $stack->getStackSize());
219+
$this->assertSame(-1, $stack->getCurrentFontIndex());
220+
}
221+
189222
public function testUnicodeOrdAddedToSubsetChars(): void
190223
{
191224
$this->setupTest();

0 commit comments

Comments
 (0)