File: TypeI.pm

package info (click to toggle)
liblatex-table-perl 1.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 784 kB
  • sloc: perl: 2,421; makefile: 2
file content (412 lines) | stat: -r--r--r-- 11,447 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package LaTeX::Table::Types::TypeI;

use strict;
use warnings;

use Moose::Role;
use Template;

use version; our $VERSION = qv('1.0.6');

use Carp;

has '_table_obj' => ( is => 'rw', isa => 'LaTeX::Table', required => 1 );
has '_tabular_environment' => ( is => 'ro', required => 1 );
has '_template'            => ( is => 'ro', required => 1 );
has '_is_floating'         => ( is => 'ro', default  => 1, required => 1 );

sub generate_latex_code {
    my ($self) = @_;

    $self->_check_options();

    my $tbl   = $self->_table_obj;
    my $theme = $tbl->get_theme_settings;

    if ( !$tbl->get_tabletail() ) {
        $tbl->set_tabletail( $self->_get_default_tabletail_code() );
    }

    my $template_vars = {
        'CENTER' => $tbl->_get_default_align ? 1 : $tbl->get_center,
        'LEFT'   => $tbl->get_left(),
        'RIGHT'  => $tbl->get_right(),
        'ENVIRONMENT'  => $tbl->get_environment,
        'FONTFAMILY'   => $tbl->get_fontfamily(),
        'FONTSIZE'     => $tbl->get_fontsize(),
        'FOOTTABLE'    => $tbl->get_foottable(),
        'POSITION'     => $tbl->get_position(),
        'CAPTION_TOP'  => $tbl->get_caption_top(),
        'CAPTION'      => $self->_get_caption(),
        'CAPTION_CMD'  => $self->_get_caption_command(),
        'CONTINUED'    => $tbl->get_continued(),
        'CONTINUEDMSG' => $tbl->get_continuedmsg(),
        'SHORTCAPTION' => (
              $tbl->get_maincaption  ? $tbl->get_maincaption
            : $tbl->get_shortcaption ? $tbl->get_shortcaption
            : 0
        ),
        'SIDEWAYS' => $tbl->get_sideways(),
        'STAR'     => $tbl->get_star(),
        'WIDTH'    => $tbl->get_width(),
        'MAXWIDTH' => $tbl->get_maxwidth(),
        'COLDEF'   => $tbl->get_coldef ? $tbl->get_coldef
        : $tbl->_get_coldef_code( $tbl->get_data ),
        'LABEL'         => $tbl->get_label(),
        'TABLEHEADMSG'  => $tbl->get_tableheadmsg(),
        'TABLETAIL'     => $tbl->get_tabletail(),
        'TABLELASTTAIL' => $tbl->get_tablelasttail(),
        'XENTRYSTRETCH' => $tbl->get_xentrystretch(),
        'HEADER_CODE' => $tbl->_get_matrix_latex_code( $tbl->get_header, 1 ),
        'DATA_CODE' => $tbl->_get_matrix_latex_code( $tbl->get_data, 0 ),
        'TABULAR_ENVIRONMENT'   => $self->_get_tabular_environment(),
        'EXTRA_ROW_HEIGHT_CODE' => (
            defined $theme->{EXTRA_ROW_HEIGHT}
            ? '\setlength{\extrarowheight}{'
                . $theme->{EXTRA_ROW_HEIGHT} . "}\n"
            : q{}
        ),
        'RULES_COLOR_GLOBAL_CODE' => (
            defined $theme->{RULES_COLOR_GLOBAL}
            ? $theme->{RULES_COLOR_GLOBAL} . "\n"
            : q{}
        ),
        'RULES_WIDTH_GLOBAL_CODE' => (
            defined $theme->{RULES_WIDTH_GLOBAL}
            ? $theme->{RULES_WIDTH_GLOBAL} . "\n"
            : q{}
        ),
        'RESIZEBOX_BEGIN_CODE' => $self->_get_begin_resizebox_code(),
        'RESIZEBOX_END_CODE'   => (
            $self->_table_obj->get_resizebox ? "}\n"
            : q{}
        ),
        'DEFINE_COLORS_CODE' => (
            defined $tbl->get_theme_settings->{DEFINE_COLORS}
            ? $tbl->get_theme_settings->{DEFINE_COLORS} . "\n"
            : q{}
        ),
        'LT_NUM_COLUMNS' => scalar( @{ $tbl->_get_data_summary() } ),
        'LT_BOTTOM_RULE_CODE' =>
            $tbl->_get_hline_code( $tbl->_get_RULE_BOTTOM_ID ),
    };

    my $template_obj = Template->new();
    my $template
        = $tbl->get_custom_template
        ? $tbl->get_custom_template
        : $self->_template;

    my $template_output;

    $template_obj->process( \$template, $template_vars, \$template_output )
        or croak $template_obj->error();
    return $template_output;
}

sub _check_options {
    my ($self) = @_;
    my $tbl = $self->_table_obj;

    # default floating enviromnent is table
    if ( $tbl->get_environment eq '1' ) {
        $tbl->set_environment('table');
    }

    if ( !$self->_is_floating ) {
        if ( !$tbl->get_environment ) {
            $tbl->_invalid_option_usage( 'environment',
                $tbl->get_type
                    . ' is non-floating and requires an environment' );
        }
        if ( $tbl->get_position ) {
            $tbl->_invalid_option_usage( 'position',
                $tbl->get_type
                    . ' is non-floating and thus does not support position' );
        }
    }

    # check center, right, left options
    my $cnt_true_alignments = 0;
    for my $align ( $tbl->get_center, $tbl->get_right, $tbl->get_left ) {
        if ($align) {
            $cnt_true_alignments++;
        }
    }
    if ( $cnt_true_alignments > 1 ) {
        $tbl->_invalid_option_usage( 'center, left, right',
            'only one allowed.' );
    }
    if ( $tbl->has_center || $tbl->has_right || $tbl->has_left ) {
        $tbl->_set_default_align(0);
    }
    else {
        $tbl->_set_default_align(1);
    }

    if ( $tbl->get_maincaption && $tbl->get_shortcaption ) {
        $tbl->_invalid_option_usage( 'maincaption, shortcaption',
            'only one allowed.' );
    }

    # handle default values by ourselves
    if ( $tbl->get_width_environment eq 'tabular*' ) {
        $tbl->set_width_environment(0);
    }
    if ( !$tbl->get_width ) {
        if ( $tbl->get_width_environment eq 'tabularx' ) {
            $tbl->_invalid_option_usage( 'width_environment',
                'Is tabularx and width is unset' );
        }
        elsif ( $tbl->get_width_environment eq 'tabulary' ) {
            $tbl->_invalid_option_usage( 'width_environment',
                'Is tabulary and width is unset' );
        }
    }
    return;
}

sub _get_caption_command {
    my ($self)    = @_;
    my $tbl       = $self->_table_obj;
    my $c_caption = 'caption';
    if ( $tbl->get_caption_top && $tbl->get_caption_top ne '1' ) {
        $c_caption = $tbl->get_caption_top;
        $c_caption =~ s{ \A \\ }{}xms;
    }
    return $c_caption;
}

sub _get_begin_resizebox_code {
    my ($self) = @_;
    if ( $self->_table_obj->get_resizebox ) {
        my $rb_width  = $self->_table_obj->get_resizebox->[0];
        my $rb_height = q{!};
        if ( defined $self->_table_obj->get_resizebox->[1] ) {
            $rb_height = $self->_table_obj->get_resizebox->[1];
        }
        return "\\resizebox{$rb_width}{$rb_height}{\n";
    }
    return q{};
}

sub _get_caption {
    my ($self)  = @_;
    my $caption = q{};
    my $tbl     = $self->_table_obj;

    if ( !$tbl->get_caption ) {
        if ( !$tbl->get_maincaption ) {
            return 0;
        }
    }
    else {
        $caption = $tbl->get_caption;
    }

    my $tmp = q{};
    if ( $tbl->get_maincaption ) {
        $tmp = $tbl->get_maincaption . '. ';
        if ( defined $tbl->get_theme_settings->{CAPTION_FONT_STYLE} ) {
            $tmp = $tbl->_add_font_family( $tmp,
                $tbl->get_theme_settings->{CAPTION_FONT_STYLE} );
        }
    }

    return $tmp . $caption;
}

sub _get_tabular_environment {
    my ($self) = @_;
    my $tbl = $self->_table_obj;

    my $res
        = $tbl->get_custom_tabular_environment
        ? $tbl->get_custom_tabular_environment
        : $self->_tabular_environment;

    if ( $tbl->get_width ) {
        if ( !$tbl->get_width_environment ) {
            $res .= q{*};
        }
        else {
            $res = $tbl->get_width_environment;
        }
    }
    return $res;
}

sub _get_default_tabletail_code {
    my ($self) = @_;

    my $tbl = $self->_table_obj;
    my $v0  = q{|} x $tbl->get_theme_settings->{'VERTICAL_RULES'}->[0];

    return
          $tbl->_get_hline_code( $tbl->_get_RULE_MID_ID )
        . '\multicolumn{'
        . @{ $tbl->_get_data_summary() }
        . "}{${v0}r$v0}{{"
        . $tbl->get_tabletailmsg
        . "}} \\\\\n";
}

1;
__END__

=head1 NAME

LaTeX::Table::Types::TypeI - Interface for LaTeX table types.

=head1 DESCRIPTION

This is the type interface (or L<Moose> role), that all type objects must use.
L<LaTeX::Table> delegates the LaTeX code generation to type
objects. It stores all information we have in easy to use L<"TEMPLATE
VARIABLES">. L<LaTeX::Table> ships with very flexible templates, but it is
possible to use the template variables defined here to build custom templates.

=head1 INTERFACE

=over

=item C<generate_latex_code>

=back

=head1 TEMPLATE VARIABLES

Most options are accessible here:

=over

=item C<CENTER, LEFT, RIGHT>

Example:

  [% IF CENTER %]\centering
  [% END %]

=item C<ENVIRONMENT, STAR, POSITION, SIDEWAYS>

These options for floating environments are typically used like:

  [% IF ENVIRONMENT %]\begin{[% ENVIRONMENT %][% IF STAR %]*[% END %]}[% IF POSITION %][[% POSITION %]][% END %]
  ...
  [% END %]
  # the tabular environment here
  ...
  [% IF ENVIRONMENT %] ...
  \end{[% ENVIRONMENT %][% IF STAR %]*[% END %]}[% END %]

=item C<CAPTION_TOP, CAPTION_CMD, SHORTCAPTION, CAPTION, CONTINUED, CONTINUEDMSG>

The variables to build the caption command. Note that there is NO template for
the C<maincaption> option. C<CAPTION> already includes this maincaption if
specified.

=item C<LABEL>

The label:

 [% IF LABEL %]\label{[% LABEL %]}[% END %]

=item C<TABULAR_ENVIRONMENT, WIDTH, COLDEF>

These three options define the tabular environment:

  \begin{[% TABULAR_ENVIRONMENT %]}[% IF WIDTH %]{[% WIDTH %]}[% END %]{[% COLDEF %]}

=item C<FONTFAMILY, FONTSIZE>

Example: 

  [% IF FONTSIZE %]\[% FONTSIZE %]
  [% END %][% IF FONTFAMILY %]\[% FONTFAMILY %]family
  [% END %]

=item C<TABLEHEADMSG, TABLETAIL, TABLELASTTAIL, XENTRYSTRETCH>

For the multi-page tables.

=item C<MAXWIDTH, FOOTTABLE>

Currently only used by L<LaTeX::Table::Types::Ctable>.

=back

In addition, some variables already contain formatted LaTeX code:

=over 

=item C<HEADER_CODE>

The formatted header:

  \toprule
  \multicolumn{2}{c}{Item} &             \\
  \cmidrule(r){1-2}
  Animal                   & Description & Price \\
  \midrule

=item C<DATA_CODE> 

The formatted data:

  Gnat      & per gram & 13.65 \\
            & each     & 0.01  \\
  Gnu       & stuffed  & 92.59 \\
  Emu       & stuffed  & 33.33 \\
  Armadillo & frozen   & 8.99  \\
  \bottomrule

=item C<RESIZEBOX_BEGIN_CODE, RESIZEBOX_END_CODE>

Everything between these two template variables is resized according the
C<resizebox> option.

=item C<EXTRA_ROW_HEIGHT_CODE, DEFINE_COLORS_CODE, RULES_COLOR_GLOBAL_CODE, RULES_WIDTH_GLOBAL_CODE>

Specified by the theme. C<EXTRA_ROW_HEIGHT_CODE> will contain the
corresponding LaTeX extrarowheight command, e.g for '1pt':

    \setlength{\extrarowheight}{1pt}

Otherwise it will contain the empty string. The other template variables will
contain the command specified by the corresponding theme option.

=back

Finally, some variables allow access to internal C<LaTeX::Table> variables:

=over

=item C<LT_NUM_COLUMNS>

Contains the number of columns of the table.

=item C<LT_BOTTOM_RULE_CODE>

Code that draws the rules at the bottom of the table according the theme
options.

=back 

=head1 SEE ALSO

L<LaTeX::Table>

The predefined templates: L<LaTeX::Table::Types::Std>,
L<LaTeX::Table::Types::Ctable>, L<LaTeX::Table::Types::Longtable>,
L<LaTeX::Table::Types::Xtab>

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2006-2010 C<< <limaone@cpan.org> >>

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=cut

# vim: ft=perl sw=4 ts=4 expandtab