Quiq/TeX/Code.pm

filter: Statementstmt 95.5% 85 / 89 Branchbran 69.2% 18 / 26 Conditioncond 45.4% 5 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 84.6% 132 / 156 CC 19 SCAR 17.4
CC
19
Cov
86%
macro
12.5
modifyLength
4.5
c
4.0
CRAP 5.7
« Quiq/Sdoc/Producer.pm ↑ Summary Quiq/Html/Verbatim.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::TeX::Code - Generator für TeX Code
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen TeX Code-Generator. Mit
16 den Methoden der Klasse kann aus einem Perl-Programm heraus
17 TeX-Code erzeugt werden.
18  
19 =cut
20  
21 # -----------------------------------------------------------------------------
22  
23 package Quiq::TeX::Code;
24 5 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 5 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 our $VERSION = '1.240';
31  
32 5 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use Quiq::Converter;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # -----------------------------------------------------------------------------
39  
40 =head1 METHODS
41  
42 =head2 Konstruktor
43  
44 =head3 new() - Instantiiere TeX Code-Generator
45  
46 =head4 Synopsis
47  
48 $t = $class->new;
49  
50 =head4 Description
51  
52 Instantiiere einen TeX Code-Generator und liefere eine Referenz
53 auf dieses Objekt zurück.
54  
55 =cut
56  
57 # -----------------------------------------------------------------------------
58  
59 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60 14 return shift->SUPER::new;
61 }
62  
63 # -----------------------------------------------------------------------------
64  
65 =head2 Objektmethoden
66  
67 =head3 c() - Erzeuge TeX Codezeile
68  
69 =head4 Synopsis
70  
71 $code = $t->c($fmt,@args,@opts);
72  
73 =head4 Arguments
74  
75 =over 4
76  
77 =item $fmt
78  
79 Codezeile mit sprintf Formatelementen.
80  
81 =item @args
82  
83 Argumente, die für die Formatelemente in $fmt eingesetzt
84 werden. Kommt eine Arrayreferenz vor, wird diese zu einem
85 kommaseparierten String expandiert.
86  
87 =back
88  
89 =head4 Options
90  
91 =over 4
92  
93 =item -nl => $n (Default: 1)
94  
95 Beende den Code mit $n Zeilenumbrüchen.
96  
97 =item -pnl => $n (Default: 0)
98  
99 Beginne den Code mit $n Zeilenumbrüchen.
100  
101 =back
102  
103 =head4 Returns
104  
105 TeX Code (String)
106  
107 =head4 Description
108  
109 Erzeuge eine TeX Codezeile und liefere das Resultat zurück.
110  
111 =head4 Example
112  
113 B<Makro mit Option und Parameter>
114  
115 $documentClass = 'article';
116 $fontSize = '12pt';
117 ...
118 $t->c('\documentclass[%s]{%s}',$fontSize,$documentClass);
119  
120 produziert
121  
122 \documentclass[12pt]{article}\n
123  
124 B<Expansion von Array-Parameter>
125  
126 my @opt;
127 push @opt,'labelsep=colon';
128 push @opt,'labelfont=bf';
129 push @opt,'skip=1.5ex';
130 $t->c('\usepackage[%s]{caption}',\@opt);
131  
132 produziert
133  
134 \usepackage[labelsep=colon,labelfont=bf,skip=1.5ex]{caption}
135  
136 =cut
137  
138 # -----------------------------------------------------------------------------
139  
140 32 ▶sub c {
Subroutine c: called CC=4 SCAR=13.9
Pod documented
141 32 my $self = shift;
142 32 my $fmt = shift;
143 # @_: @args,@opts
144  
145 # Optionen
146  
147 32 my $nl = 1;
148 32 my $pnl = 0;
149  
150 32 Quiq::Option->extract(-mode=>'sloppy',\@_,
151 -nl => \$nl,
152 -pnl => \$pnl,
153 );
154  
155 # Arrayreferenz zu kommasepariertem String expandieren
156  
157 32 for (@_) {
158 20 my $type = Scalar::Util::reftype($_);
159 20 ▶ if (defined($type) && $type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "ARRAY") 1 19 20
Condition66%2/3
defined $type and $type eq "ARRAY"
!ll&&!rl&&r
1901
MC/DC50%1/2
defined $type and $type eq "ARRAY"
defined $type $type eq "ARRAY"
Truth table66%2/3
A and B
A= defined $typeB= $type eq "ARRAY"
ABresult
0X0
100
111
160 1 $_ = join ',',@$_;
161 }
162 }
163  
164 # Codezeile erzeugen und zurückliefern
165 32 return ("\n" x $pnl).sprintf($fmt,@_).("\n" x $nl);
166 }
167  
168 # -----------------------------------------------------------------------------
169  
170 =head3 ci() - Erzeuge TeX Code inline
171  
172 =head4 Synopsis
173  
174 $code = $t->ci($fmt,@args,@opts);
175  
176 =head4 Arguments
177  
178 =over 4
179  
180 =item $fmt
181  
182 Codezeile mit sprintf Formatelementen.
183  
184 =item @args
185  
186 Argumente, die in den Formatstring eingesetzt werden. Kommt unter
187 den Argumenten eine Arrayreferenz vor, wird diese zu einem
188 kommaseparierten String expandiert.
189  
190 =back
191  
192 =head4 Options
193  
194 =over 4
195  
196 =item -nl => $n (Default: 0)
197  
198 Beende den Code mit $n Zeilenumbrüchen.
199  
200 =back
201  
202 =head4 Returns
203  
204 TeX Code (String)
205  
206 =head4 Description
207  
208 Erzeuge TeX Code und liefere das Resultat zurück. Die Methode
209 ist identisch zu Methode $t->c(), nur dass per Default kein
210 Newline am Ende des Code hinzugefügt wird. Das C<i> im
211 Methodennamen steht für "inline".
212  
213 =head4 Example
214  
215 B<< Vergleich von $t->ci(), sprintf(), $t->c() >>
216  
217 $t->ci('\thead[%sb]{%s}','c','Ein Text');
218  
219 ist identisch zu
220  
221 sprintf '\thead[%sb]{%s}','c','Ein Text';
222  
223 ist identisch zu
224  
225 $t->c('\thead[%sb]{%s}','c','Ein Text',-nl=>0);
226  
227 und produziert
228  
229 \thead[cb]{Ein Text}
230  
231 =cut
232  
233 # -----------------------------------------------------------------------------
234  
235 1 ▶sub ci {
Subroutine ci: called CC=1 SCAR=0.0
Pod documented
236 1 my $self = shift;
237 1 my $fmt = shift;
238 # @_: @args,@opts
239 1 return $self->c($fmt,-nl=>0,@_);
240 }
241  
242 # -----------------------------------------------------------------------------
243  
244 =head3 macro() - Erzeuge TeX Macro
245  
246 =head4 Synopsis
247  
248 $code = $t->macro($name,@args);
249  
250 =head4 Options
251  
252 =over 4
253  
254 =item -nl => $n (Default: 1)
255  
256 I<Newline>, füge $n Zeilenumbrüche am Ende hinzu.
257  
258 =item -o => $options
259  
260 =item -o => \@options
261  
262 Füge eine Option/Optionsliste [...] hinzu. Ein Array wird in eine
263 kommaseparierte Liste von Werten übersetzt.
264  
265 =item -p => $parameters
266  
267 =item -p => \@parameters
268  
269 Füge einen Parameter/eine Parameterliste {...} hinzu. Ein Array
270 wird in eine kommaseparierte Liste von Werten übersetzt.
271  
272 =item -pnl => $n (Default: 0)
273  
274 I<Preceeding newline>, setze $n Zeilenumbrüche an den Anfang.
275  
276 =back
277  
278 =head4 Description
279  
280 Erzeuge ein TeX Macro und liefere den resultierenden Code
281 zurück. Diese Methode zeichnet sich gegenüber den Methoden $t->c()
282 und $t->ci() dadurch aus, dass undefinierte/leere Optionen und
283 Parameter vollständig weggelassen werden.
284  
285 =head4 Examples
286  
287 B<Macro ohne Argumente>
288  
289 $t->macro('\LaTeX');
290  
291 produziert
292  
293 \LaTeX
294  
295 B<Kommando mit undefiniertem Argument>
296  
297 $t->macro('\LaTeX',-p=>undef);
298  
299 produziert
300  
301 \LaTeX
302  
303 B<Macro mit Leerstring-Argument >
304  
305 $t->macro('\LaTeX',-p=>'');
306  
307 produziert
308  
309 \LaTeX{}
310  
311 B<Macro mit leerer Optionsliste und Parameter>
312  
313 @opt = ();
314 $t->macro('\documentclass',-o=>\@opt,-p=>'article');
315  
316 produziert
317  
318 \documentclass{article}
319  
320 B<Macro mit Opton und Parameter>
321  
322 $t->macro('\documentclass',
323 -o => '12pt',
324 -p => 'article',
325 );
326  
327 produziert
328  
329 \documentclass[12pt]{article}
330  
331 B<Macro mit Parameter und mehreren Optionen (Variante 1)>
332  
333 $t->macro('\documentclass',
334 -o => 'a4wide,12pt',
335 -p => 'article',
336 );
337  
338 produziert
339  
340 \documentclass[a4wide,12pt]{article}
341  
342 B<Macro mit Parameter und mehreren Optionen (Variante 2)>
343  
344 @opt = ('a4wide','12pt');
345 $t->macro('\documentclass',
346 -o => \@opt,
347 -p => 'article',
348 );
349  
350 produziert
351  
352 \documentclass[a4wide,12pt]{article}
353  
354 =cut
355  
356 # -----------------------------------------------------------------------------
357  
358 16 ▶sub macro {
Subroutine macro: called CC=11 SCAR=25.3
Pod documented
359 16 my $self = shift;
360 16 my $name = shift;
361 # @_: @args
362  
363 16 my $nl = 1;
364 16 my $pnl = 0;
365  
366 16 my $cmd = $name;
367 16 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 28 16 44
368 28 my $opt = shift;
369 28 my $val = shift;
370  
371 # Wandele Array in kommaseparierte Liste von Werten
372  
373 28 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 3 25 28
374 3 my $refType = Scalar::Util::reftype($val);
375 3 ▶ if ($refType eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($refType eq 'ARRAY') { } 3 0 3
376 3 $val = join ',',@$val;
377 }
378 else {
379 0 $self->throw(
380 'TEX-00001: Unexpected reference',
381 RefType => $refType,
382 );
383 }
384 }
385  
386 # Behandele Parameter und Optionen
387  
388 28 ▶ if ($opt eq '-p') {
Branch: 4 branches
BranchTrueFalseTotal
if ($opt eq '-p') { } 16 12 28
elsif ($opt eq '-pnl') { } 0 12 12
elsif ($opt eq '-o') { } 6 6 12
elsif ($opt eq '-nl') { } 6 0 6
389 # Ein Parameter-Wert wird immer gesetzt, ggf. leer
390 16 ▶ $val //= '';
Condition50%1/2
$val //= ""
l!l
160
MC/DC0%0/1
$val //= ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
391 16 $cmd .= "{$val}";
392 }
393 elsif ($opt eq '-pnl') {
394 0 $pnl = $val;
395 }
396 elsif ($opt eq '-o') {
397 # Eine Options-Angabe entfällt, wenn leer
398 6 ▶ if (defined $val && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne "") 6 0 6
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
006
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
399 6 $cmd .= "[$val]";
400 }
401 }
402 elsif ($opt eq '-nl') {
403 6 $nl = $val;
404 }
405 else {
406 0 $self->throw(
407 'LATEX-00001: Unknown Option',
408 Option => $opt,
409 );
410 }
411 }
412  
413 16 return ("\n" x $pnl).$cmd.("\n" x $nl);
414 }
415  
416 # -----------------------------------------------------------------------------
417  
418 =head3 comment() - Erzeuge TeX-Kommentar
419  
420 =head4 Synopsis
421  
422 $code = $l->comment($text,@opt);
423  
424 =head4 Options
425  
426 =over 4
427  
428 =item -nl => $n (Default: 1)
429  
430 Füge $n Zeilenumbrüche am Ende hinzu.
431  
432 =item -pnl => $n (Default: 0)
433  
434 Setze $n Zeilenumbrüche an den Anfang.
435  
436 =back
437  
438 =head4 Description
439  
440 Erzeuge einen TeX-Kommentar und liefere den resultierenden Code
441 zurück.
442  
443 =head4 Examples
444  
445 B<Kommentar erzeugen>
446  
447 $l->comment("Dies ist\nein Kommentar");
448  
449 produziert
450  
451 % Dies ist
452 % ein Kommentar
453  
454 =cut
455  
456 # -----------------------------------------------------------------------------
457  
458 1 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
459 1 my $self = shift;
460 # @_: $text,@opt
461  
462 # Optionen
463  
464 1 my $nl = 1;
465 1 my $pnl = 0;
466  
467 1 Quiq::Option->extract(\@_,
468 -nl => \$nl,
469 -pnl => \$pnl,
470 );
471  
472 # Argumente
473 1 my $text = shift;
474  
475 # Kommentar erzeugen
476  
477 1 $text = Quiq::Unindent->trim($text);
478 1 $text =~ s/^/% /mg;
479 1 $text = ("\n" x $pnl).$text;
480 1 $text .= ("\n" x $nl);
481
482 1 return $text;
483 }
484  
485 # -----------------------------------------------------------------------------
486  
487 =head3 modifyLength() - Wende Berechnung auf Länge an
488  
489 =head4 Synopsis
490  
491 $newLength = $l->modifyLength($length,$expr);
492  
493 =head4 Arguments
494  
495 =over 4
496  
497 =item $length
498  
499 Eine einfache TeX-Länge. Beispiel: '1ex'.
500  
501 =item $expr
502  
503 Ein arithmetischer Ausdruck, der auf den Zahlenwert der Länge
504 angewendet wird. Beispiel: '*2' (multipliziere Länge mit 2).
505  
506 =back
507  
508 =head4 Returns
509  
510 TeX-Länge (String)
511  
512 =head4 Description
513  
514 Wende den arithmetischen Ausdruck $expr auf TeX-Länge $length an
515 und liefere das Resultat zurück. Leerstring oder C<undef> werden
516 unverändert geliefert.
517  
518 =head4 Example
519  
520 $l->modifyLength('1.5ex','*1.5');
521 # 2.25ex
522  
523 =cut
524  
525 # -----------------------------------------------------------------------------
526  
527 1 ▶sub modifyLength {
Subroutine modifyLength: called CC=4 SCAR=15.0
Pod documented
528 1 my ($self,$length,$expr) = @_;
529  
530 1 ▶ if (defined($length) && $length ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length ne "") 1 0 1
Condition33%1/3
defined $length and $length ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $length and $length ne ""
defined $length $length ne ""
Truth table33%1/3
A and B
A= defined $lengthB= $length ne ""
ABresult
0X0
100
111
531 1 my ($len,$unit) = $length =~ /^([\d.]+)([a-z]+)$/;
532 1 my $expr = "$len$expr";
533 1 $len = eval $expr;
534 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
535 0 $self->throw(
536 'TEX-00001: Illegal expression',
537 Expression => $expr,
538 );
539 }
540 1 $length = "$len$unit";
541 }
542  
543 1 return $length;
544 }
545  
546 # -----------------------------------------------------------------------------
547  
548 =head3 toLength() - Wandele Länge in TeX-Länge
549  
550 =head4 Synopsis
551  
552 $length = $this->toLength($val);
553  
554 =head4 Arguments
555  
556 =over 4
557  
558 =item $val
559  
560 Länge, die in die TeX-Länge umgerechnet wird.
561  
562 =back
563  
564 =head4 Returns
565  
566 TeX-Länge (String)
567  
568 =head4 Examples
569  
570 Keine Angabe:
571  
572 $class->toLength(undef);
573 # undef
574  
575 Angabe in Pixeln ohne Einheit:
576  
577 $class->toLength(100);
578 # '75pt'
579  
580 Angabe in Pixeln mit Einheit:
581  
582 $class->toLength('100px');
583 # '75pt'
584  
585 Alle anderen Werte bleiben unverändert:
586  
587 $class->toLength($val);
588 # $val
589  
590 =cut
591  
592 # -----------------------------------------------------------------------------
593  
594 1 ▶sub toLength {
Subroutine toLength: called CC=3 SCAR=11.2
Pod documented
595 1 my ($this,$val) = @_;
596  
597 1 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1 0 1
598 1 $val =~ s/px$//; # Einheit Pixel entfernen wir
599 1 ▶ if (Quiq::Math->isNumber($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Quiq::Math"->isNumber($val)) 1 0 1
600 # Keine Einheit: Pixel -> Punkt
601 1 $val = Quiq::Converter->pxToPt($val).'pt';
602 }
603 }
604  
605 1 return $val;
606 }
607  
608 # -----------------------------------------------------------------------------
609  
610 =head1 VERSION
611  
612 1.240
613  
614 =head1 AUTHOR
615  
616 Frank Seitz, L<http://fseitz.de/>
617  
618 =head1 COPYRIGHT
619  
620 Copyright (C) 2026 Frank Seitz
621  
622 =head1 LICENSE
623  
624 This code is free software; you can redistribute it and/or modify
625 it under the same terms as Perl itself.
626  
627 =cut
628  
629 # -----------------------------------------------------------------------------
630  
631 5 1;
632  
633 # eof
« Quiq/Sdoc/Producer.pm ↑ Summary Quiq/Html/Verbatim.pm »