Quiq/Gd/Font.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Quiq/Xml.pm ↑ Summary Quiq/Smb/Client.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Gd::Font - GD- oder TrueType-Font
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Abstraktion für GD- und TrueType-Fonts, die von GD nicht gemacht wird.
16  
17 =head2 Geometrie der (monospaced) GD-Fonts
18  
19 Name Breite Höhe
20 ---------------- ------ ----
21 gdTinyFont 5 8
22 gdSmallFont 6 13
23 gdMediumBoldFont 7 13
24 gdLargeFont 8 16
25 gdGiantFont 9 15
26  
27 =head1 EXAMPLES
28  
29 =over 2
30  
31 =item *
32  
33 GD-Font instantiieren
34  
35 $fnt = Quiq::Gd::Font->new('gdSmallFont');
36  
37 =item *
38  
39 TrueType-Font instantiieren
40  
41 $fnt = Quiq::Gd::Font->new('/opt/fonts/pala.ttf',20);
42  
43 =back
44  
45 =cut
46  
47 # -----------------------------------------------------------------------------
48  
49 package Quiq::Gd::Font;
50 3 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 our $VERSION = '1.240';
58  
59 0 ▶use GD ();
Subroutine BEGIN: called CC=1 SCAR=2.6
60 use Quiq::Math;
61  
62 # -----------------------------------------------------------------------------
63  
64 # In neueren Versionen von GD ist auch ein Aufruf als Klassenmethode
65 # möglich. Aus Portabilitätsgründen instantiieren wir aber ein Bildobjekt.
66  
67 my $r = GD::Image->new(1,1)->useFontConfig(1);
68 unless ($r) {
69 Quiq::Gd::Font->throw(
70 'GD-00006: FontConfig-Unterstützung fehlt',
71 );
72 }
73  
74 # -----------------------------------------------------------------------------
75  
76 =head1 METHODS
77  
78 =head2 Konstruktor
79  
80 =head3 new() - Instantiiere GD- oder TrueType-Font
81  
82 =head4 Synopsis
83  
84 $fnt = $class->new($name);
85 $fnt = $class->new($name,$pt);
86 $fnt = $class->new("$name,$pt");
87 $fnt = $class->new($fnt);
88  
89 =head4 Description
90  
91 Instantiiere GD- oder TrueType-Font und liefere eine Referenz auf
92 dieses Objekt zurück. Wird bei einem TrueType-Font keine Fontgröße
93 angegeben, wird 10pt angenommen.
94  
95 Ein TrueType-Font kann auch mit einem einzigen Argument - als
96 Arrayreferenz [$name,$pt] - angegeben werden.
97  
98 Wird ein bereits instantiiertes Font-Objekt als Parameter übergeben,
99 wird dieses einfach zurückgeliefert.
100  
101 =cut
102  
103 # -----------------------------------------------------------------------------
104  
105 sub new {
106 my $class = shift;
107 # @_: s.o.
108  
109 # * Argumente verarbeiten *
110  
111 my ($name,$pt);
112 if (ref $_[0]) {
113 # bereits instantiierter Font: $fnt
114 return $_[0];
115 }
116 elsif ($_[0] =~ /^(.*),(\d+)$/) {
117 # TrueType-Font als einzelner String: "$name,$pt"
118 ($name,$pt) = ($1,$2);
119 }
120 else {
121 ($name,$pt) = @_;
122 }
123  
124 # * Fontobjekt instantiieren *
125  
126 if ($name =~ /^gd(.*)Font$/) {
127 # * GD-Font *
128  
129 my $meth = ucfirst $1;
130 return $class->SUPER::new(
131 trueType => 0,
132 name => $name,
133 font => GD::Font->$meth,
134 pt => undef,
135 );
136 }
137  
138 # * TrueType-Font *
139  
140 my $path = $name; # Name eines TrueType-Font ist ein Pfad
141 $pt ||= 10; # Default-Fontgröße
142  
143 # Prüfen, ob Font vorhanden ist
144  
145 if (!-e $path) {
146 $class->throw(
147 'GDFONT-00001: Font existiert nicht',
148 Error => $@,
149 Font => $path,
150 );
151 }
152  
153 # Fontname ist Basename der Fontdatei plus Fontgröße
154  
155 $name =~ s|.*/||;
156 $name =~ s|\.ttf$||;
157 $name .= $pt;
158  
159 return $class->SUPER::new(
160 trueType => 1,
161 name => $name,
162 font => $path,
163 pt => $pt,
164 );
165 }
166  
167 # -----------------------------------------------------------------------------
168  
169 =head2 Primitive Operationen
170  
171 =head3 name() - Name des Font
172  
173 =head4 Synopsis
174  
175 $name = $fnt->name;
176  
177 =head4 Description
178  
179 Liefere den Namen des Font. Bei einem GD-Font ist dies der Name, wie er
180 beim Konstruktor angegeben wurde. Bei einem TrueType-Font ist es der
181 Grundname der Datei, also ohne Pfad und Endung C<.ttf>.
182  
183 =cut
184  
185 # -----------------------------------------------------------------------------
186  
187 sub name {
188 return shift->{'name'};
189 }
190  
191 # -----------------------------------------------------------------------------
192  
193 =head3 pt() - Größe des Font
194  
195 =head4 Synopsis
196  
197 $pt = $fnt->pt;
198  
199 =head4 Description
200  
201 Liefere die Größe des Font. Bei einem GD-Font C<undef>, bei einem
202 TrueType-Font die Größe in pt.
203  
204 =cut
205  
206 # -----------------------------------------------------------------------------
207  
208 sub pt {
209 return shift->{'pt'};
210 }
211  
212 # -----------------------------------------------------------------------------
213  
214 =head3 isTrueType() - Prüfe auf TrueType-Font
215  
216 =head4 Synopsis
217  
218 $bool = $fnt->isTrueType;
219  
220 =head4 Description
221  
222 Liefere 1 wenn der Font ein TrueType-Font ist, liefere 0, wenn er
223 ein GD-Font ist.
224  
225 =cut
226  
227 # -----------------------------------------------------------------------------
228  
229 sub isTrueType {
230 return shift->{'trueType'};
231 }
232  
233 # -----------------------------------------------------------------------------
234  
235 =head3 stringGeometry() - Liefere den Platzbedarf einer Zeichenkette
236  
237 =head4 Synopsis
238  
239 ($width,$height,$xOffset,$yOffset) = $fnt->stringGeometry($str,@opt);
240  
241 =head4 Options
242  
243 =over 4
244  
245 =item -up => $bool (Default: 0)
246  
247 Schreibe den Text aufrecht.
248  
249 =item -debug => $bool (Default: 0)
250  
251 Gib Informationen auf STDDER aus.
252  
253 =back
254  
255 =head4 Description
256  
257 Liefere den Platzbedarf (Breite, Höhe) und den x- und y-Offset
258 der Zeichenkette $str.
259  
260 B<Geometrie>
261  
262 TrueType-Fonts können um einen beliebigen Winkel gedreht werden.
263  
264 4,5
265 /\
266 / \
267 / T \ 2,3
268 / X /
269 6,7 / E /
270 \ T /
271 \ /
272 \/
273 0,1
274
275 $width = $bounds[2]-$bounds[6]
276 $height = $bounds[1]-$bounds[5]
277 $xOffset = -$bounds[6]
278 $yOffset = $up? 0: -$bounds[5]
279  
280 Von dieser Möglichkeit machen wir allerdings keinen Gebrauch. Wir
281 lassen lediglich 0 und 90 Grad zu. Das sind die Möglichkeiten, die
282 die GD-Fonts erlauben.
283  
284 =cut
285  
286 # -----------------------------------------------------------------------------
287  
288 sub stringGeometry {
289 my $self = shift;
290 my $string = shift;
291 # @_: @opt
292  
293 # Optionen
294  
295 my $up = 0;
296 my $debug = 0;
297  
298 $self->parameters(\@_,
299 -up => \$up,
300 -debug => \$debug,
301 );
302  
303 my $font = $self->{'font'};
304 my $width = 0;
305 my $height = 0;
306 my $xOffset = 0;
307 my $yOffset = 0;
308  
309 if ($debug) {
310 warn "font=$font, up=$up\n";
311 }
312  
313 if (!$self->{'trueType'}) {
314 # GD-Font
315  
316 $width = $font->width*length($string);
317 $height = $font->height;
318  
319 if ($up) {
320 # Höhe und Breite vertauschen
321 ($height,$width) = ($width,$height);
322 }
323 }
324 else {
325 # TrueType-Font
326  
327 my $pt = $self->{'pt'};
328 my $angle = $up? Quiq::Math->degreeToRad(90): 0;
329  
330 my @a = GD::Image->stringFT(0,$font,$pt,$angle,0,0,$string);
331 unless (@a) {
332 $self->throw(
333 'GDFONT-00002: String kann nicht gerendert werden',
334 Error => $@,
335 Font => $font,
336 );
337 }
338  
339 if ($debug) {
340 warn "pt=$pt, angle=$angle\n";
341 warn "bounds=($a[0],$a[1]) ($a[2],$a[3])",
342 " ($a[4],$a[5]) ($a[6],$a[7])\n";
343 }
344  
345 $width = $a[2]-$a[6];
346 $height = $a[1]-$a[5];
347 $xOffset = -$a[6];
348 $yOffset = $up? 0: -$a[5];
349 }
350  
351 if ($debug) {
352 warn "width=$width, height=$height",
353 " xOffset=$xOffset, yOffset=$yOffset\n";
354 }
355  
356 return ($width,$height,$xOffset,$yOffset);
357 }
358  
359 # -----------------------------------------------------------------------------
360  
361 =head3 charWidth() - Liefere (maximale) Breite eines Fontzeichens
362  
363 =head4 Synopsis
364  
365 $width = $fnt->charWidth(@opt);
366  
367 =head4 Alias
368  
369 width()
370  
371 =head4 Options
372  
373 =over 4
374  
375 =item -up => $bool (Default: 0)
376  
377 Vertikaler Text.
378  
379 =back
380  
381 =head4 Description
382  
383 Liefere die maximale Breite eines Fontzeichens.
384  
385 Da die GD-Fonts fixed/monospaced Fonts sind, ist die Breite aller
386 Zeichen gleich.
387  
388 Bei TrueType-Fonts wird die Breite der Zeichenkette "M" ermittelt.
389  
390 =cut
391  
392 # -----------------------------------------------------------------------------
393  
394 sub charWidth {
395 return (shift->stringGeometry('M',@_))[0];
396 }
397  
398 {
399 no warnings 'once';
400 *width = \&charWidth;
401 }
402  
403 # -----------------------------------------------------------------------------
404  
405 =head3 charHeight() - Liefere (maximale) Höhe eines Fontzeichens
406  
407 =head4 Synopsis
408  
409 $height = $fnt->charHeight(@opt);
410  
411 =head4 Alias
412  
413 height()
414  
415 =head4 Options
416  
417 =over 4
418  
419 =item -up => $bool (Default: 0)
420  
421 Vertikaler Text.
422  
423 =back
424  
425 =head4 Description
426  
427 Liefere die maximale Höhe eines Fontzeichnes.
428  
429 Da die GD-Fonts fixed/monospaced Fonts sind, ist die Höhe
430 aller Zeichen gleich.
431  
432 Ist der Font ein TrueType-Font wird die Höhe der Zeichenkette
433 "Xy" bestimmt.
434  
435 =cut
436  
437 # -----------------------------------------------------------------------------
438  
439 sub charHeight {
440 return (shift->stringGeometry('Xy',@_))[1];
441 }
442  
443 {
444 no warnings 'once';
445 *height = \&charHeight;
446 }
447  
448 # -----------------------------------------------------------------------------
449  
450 =head3 digitWidth() - Liefere Breite einer Ziffer
451  
452 =head4 Synopsis
453  
454 $width = $fnt->digitWidth(@opt);
455  
456 =head4 Options
457  
458 =over 4
459  
460 =item -up => $bool (Default: 0)
461  
462 Vertikaler Text.
463  
464 =back
465  
466 =head4 Description
467  
468 Liefere die Breite einer Ziffer. Alle Ziffern eines Font sollten
469 dieselbe Breite haben. Für GD-Fonts ist dies ohnehin der Fall.
470 Bei TrueType-Fonts ermitteln wir die Breite der "0".
471  
472 =cut
473  
474 # -----------------------------------------------------------------------------
475  
476 {
477 package GD::Font;
478 no warnings 'once';
479 *digitWidth = *width;
480 }
481  
482 sub digitWidth {
483 return (shift->stringGeometry('0',@_))[0];
484 }
485  
486 # -----------------------------------------------------------------------------
487  
488 =head3 digitHeight() - Liefere Höhe einer Ziffer
489  
490 =head4 Synopsis
491  
492 $height = $fnt->digitHeight(@opt);
493  
494 =head4 Options
495  
496 =over 4
497  
498 =item -up => $bool (Default: 0)
499  
500 Vertikaler Text.
501  
502 =back
503  
504 =head4 Description
505  
506 Liefere die Höhe einer Ziffer. Alle Ziffern eines Font sollten
507 dieselbe Höhe haben. Für GD-Fonts ist dies ohnehin der Fall.
508 Bei TrueType-Fonts ermitteln wir die Höhe der "0".
509  
510 =cut
511  
512 # -----------------------------------------------------------------------------
513  
514 {
515 package GD::Font;
516 no warnings 'once';
517 *digitHeight = *height;
518 }
519  
520 sub digitHeight {
521 return (shift->stringGeometry('0',@_))[1];
522 }
523  
524 # -----------------------------------------------------------------------------
525  
526 =head3 stringWidth() - Horizontaler Platzbedarf einer Zeichenkette
527  
528 =head4 Synopsis
529  
530 $n = $fnt->stringWidth($str,@opt);
531  
532 =head4 Options
533  
534 =over 4
535  
536 =item -up => $bool (Default: 0)
537  
538 Vertikaler Text.
539  
540 =back
541  
542 =head4 Description
543  
544 Liefere den horizontalen Platzbedarf der Zeichenkette $str.
545  
546 =cut
547  
548 # -----------------------------------------------------------------------------
549  
550 {
551 package GD::Font;
552  
553 sub stringWidth {
554 my $self = shift;
555 my $str = shift;
556 return $self->width*length($str);
557 }
558 }
559  
560 sub stringWidth {
561 my $self = shift;
562 my $str = shift;
563 # @_: @opt
564  
565 my ($width) = $self->stringGeometry($str,@_);
566 return $width;
567 }
568  
569 # -----------------------------------------------------------------------------
570  
571 =head3 stringHeight() - Vertikaler Platzbedarf einer Zeichenkette
572  
573 =head4 Synopsis
574  
575 $n = $fnt->stringHeight($str,@opt);
576  
577 =head4 Options
578  
579 =over 4
580  
581 =item -up => $bool (Default: 0)
582  
583 Vertikaler Text.
584  
585 =back
586  
587 =head4 Description
588  
589 Liefere den horizontalen Platzbedarf der Zeichenkette $str.
590  
591 =cut
592  
593 # -----------------------------------------------------------------------------
594  
595 {
596 package GD::Font;
597 no warnings 'once';
598 *stringHeight = *height;
599 }
600  
601 sub stringHeight {
602 my $self = shift;
603 my $str = shift;
604 # @_: @opt
605  
606 my (undef,$height) = $self->stringGeometry($str,@_);
607 return $height;
608 }
609  
610 # -----------------------------------------------------------------------------
611  
612 =head2 Alignment
613  
614 Die folgenden Methoden liefern den Offset, der benötigt wird, wenn
615 ein Text eng an eine rechte oder obere Grenze ausgerichtet werden soll,
616 wie z.B. bei der Beschriftung einer X- oder Y-Achse.
617  
618 Die Offsets sind Font-abhängig. Für die GD-Fonts und den TrueType-Font
619 pala bis 20pt ist der Offset ausgearbeitet. Für andere TrueType-Fonts
620 und Fontgrößen müssen die Methoden u.U. erweitert werden.
621  
622 =head3 alignRightOffset() - Korrektur-Offset für Ausrichtung an rechten Rand
623  
624 =head4 Synopsis
625  
626 $n = $g->alignRightOffset;
627  
628 =head4 Description
629  
630 Der Korrektur-Offset ist so bemessen, dass der Text möglichst
631 dicht an einen rechten Rand angrenzt, z.B. das Label an den Tick
632 einer Y-Achse.
633  
634 =cut
635  
636 # -----------------------------------------------------------------------------
637  
638 sub alignRightOffset {
639 my $self = shift;
640  
641 if (!$self->isTrueType) {
642 # Offset für alle GD-Fonts
643 return 1;
644 }
645 else {
646 my $xOffset = 2;
647  
648 my $pt = $self->pt;
649 if ($pt < 10) {
650 $xOffset = 2;
651 }
652 elsif ($pt <= 20) {
653 $xOffset = 3;
654 }
655 elsif ($pt <= 30) {
656 $xOffset = 4;
657 }
658  
659 return $xOffset;
660 }
661  
662 # not reached
663 }
664  
665 # -----------------------------------------------------------------------------
666  
667 =head3 alignLeftOffset() - Korrektur-Offset für Ausrichtung an linkem Rand
668  
669 =head4 Synopsis
670  
671 $n = $g->alignLeftOffset;
672  
673 =head4 Description
674  
675 Der Korrektur-Offset ist so bemessen, dass der Text möglichst
676 dicht an einen linken Rand angrenzt, z.B. das Label an den Tick
677 einer Y-Achse auf der rechten Seite.
678  
679 =cut
680  
681 # -----------------------------------------------------------------------------
682  
683 sub alignLeftOffset {
684 my $self = shift;
685  
686 if (!$self->isTrueType) {
687 # Offset für alle GD-Fonts
688 return 1;
689 }
690 else {
691 my $xOffset = 2;
692  
693 my $pt = $self->pt;
694 if ($pt < 10) {
695 $xOffset = 2;
696 }
697 elsif ($pt <= 20) {
698 $xOffset = 3;
699 }
700 elsif ($pt <= 30) {
701 $xOffset = 4;
702 }
703  
704 return $xOffset;
705 }
706  
707 # not reached
708 }
709  
710 # -----------------------------------------------------------------------------
711  
712 =head3 alignTopOffset() - Korrektur-Offset für Ausrichtung an oberen Rand
713  
714 =head4 Synopsis
715  
716 $n = $g->alignTopOffset;
717  
718 =head4 Description
719  
720 Der Korrektur-Offset ist so bemessen, dass der Text möglichst
721 dicht an einen oberen Rand angrenzt, z.B. das Label an den Tick
722 einer X-Achse.
723  
724 =cut
725  
726 # -----------------------------------------------------------------------------
727  
728 sub alignTopOffset {
729 my $self = shift;
730  
731 my $fontName = $self->name;
732 if ($fontName eq 'gdTinyFont') {
733 return 0;
734 }
735 elsif (substr($fontName,0,2) eq 'gd') {
736 return -2;
737 }
738  
739 return 0;
740 }
741  
742 # -----------------------------------------------------------------------------
743  
744 =head3 hCenterOffset() - Korrektur für horizontal zentrierten Text
745  
746 =head4 Synopsis
747  
748 $n = $fnt->hCenterOffset;
749  
750 =head4 Description
751  
752 Bei horizontal zentriertem Text ist manchmal eine Korrektur nötig,
753 die diese Methode liefert.
754  
755 =cut
756  
757 # -----------------------------------------------------------------------------
758  
759 sub hCenterOffset {
760 my $self = shift;
761  
762 my $fontName = $self->name;
763 if ($fontName eq 'gdLargeFont') {
764 return 0;
765 }
766  
767 return -1;
768 }
769  
770 # -----------------------------------------------------------------------------
771  
772 =head1 VERSION
773  
774 1.240
775  
776 =head1 AUTHOR
777  
778 Frank Seitz, L<http://fseitz.de/>
779  
780 =head1 COPYRIGHT
781  
782 Copyright (C) 2026 Frank Seitz
783  
784 =head1 LICENSE
785  
786 This code is free software; you can redistribute it and/or modify
787 it under the same terms as Perl itself.
788  
789 =cut
790  
791 # -----------------------------------------------------------------------------
792  
793 1;
794  
795 # eof
« Quiq/Xml.pm ↑ Summary Quiq/Smb/Client.pm »