blib/lib/OpenOffice/OODoc/HeadingStyles.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package OpenOffice::OODoc::HeadingStyles;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 OpenOffice::OODoc::HeadingStyles - utilities for manipulating OpenOffice::OODoc objects
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14 =cut
15  
16 our $VERSION = '0.03';
17  
18 3 ▶use OpenOffice::OODoc;
Subroutine BEGIN: called CC=1 SCAR=2.6
19  
20 use List::Util 'max';
21 use Scalar::Util 'looks_like_number';
22  
23 =head1 DESCRIPTION
24  
25 This module helps to create Heading Styles in L<OpenOfice::OODoc> documents.
26 Instead of blindly creating new styles at will, one can call
27 C<establishHeadingStyle> that will honour any exisiting style, but will create
28 a new one if needed.
29  
30 =cut
31  
32 # patch the original OpenOffice::OODoc module and add these subroutines
33 #
34 *OpenOffice::OODoc::Styles::establishHeadingStyle =
35 \&OpenOffice::OODoc::HeadingStyles::establishHeadingStyle;
36  
37 *OpenOffice::OODoc::Styles::createHeadingStyle =
38 \&OpenOffice::OODoc::HeadingStyles::createHeadingStyle;
39  
40  
41 # the tabel below comes from introspecting the xml generated by Libre Office,
42 # using the defaults, without touching any preferences or style settings.
43 #
44 # Libre Office for MacOS, Version: 6.0.4.2
45 #
46 # this tabel can be overwritten, see below in the POD
47 #
48 our $HEADING_DEFINITIONS = {
49 'Heading 1' => {
50 paragraph => { top => '0.1665in', bottom => '0.0835in' },
51 text => { size => '130%', weight => 'bold' },
52 },
53 'Heading 2' => {
54 paragraph => { top => '0.1390in', bottom => '0.0835in' },
55 text => { size => '115%', weight => 'bold' },
56 },
57 'Heading 3' => {
58 paragraph => { top => '0.0972in', bottom => '0.0835in' },
59 text => { size => '101%', weight => 'bold' },
60 },
61 'Heading 4' => {
62 paragraph => { top => '0.0835in', bottom => '0.0835in' },
63 text => { size => '95%', weight => 'bold', style => 'italic' },
64 },
65 'Heading 5' => {
66 paragraph => { top => '0.0835in', bottom => '0.0417in' },
67 text => { size => '85%', weight => 'bold' },
68 },
69 'Heading 6' => {
70 paragraph => { top => '0.0417in', bottom => '0.0417in' },
71 text => { size => '85%', weight => 'bold', style => 'italic' },
72 },
73 };
74  
75 =head1 METHODS
76  
77 =head2 establishHeadingStyle
78  
79 returns an OpenOffice::OODoc Heading Style Element for a given level
80  
81 my $level = 2;
82 my $style_definition = {
83 paragraph => { top => '0.1390in', bottom => '0.0835in' },
84 text => { size => '115%', weight => 'bold' },
85 };
86 my $heading_style = $oodoc_style
87 ->establishHeadingStyle( $level, $style_definition );
88  
89 If the style was not already present in the 'Styles' part of the document, it
90 will be created and added into the document.
91  
92 The style-definition is an optional argument. If not provided, it will use what
93 is found in C<HEADING_DEFINITIONS> package HashRef. That is pre-populated with
94 the defaults from Libre Office.
95  
96 See below.
97  
98 A newly created heading style inherrits from the C<Heading> style and will apply
99 font settings like Libre Office does: relative C<font-size>, C<font-weight> and
100 C<font-style> and more.
101  
102 CAVEAT: C<$level> will be treated turned into integer values. This means that if
103 it does not start with a number will be treated as "Heading 0" styles and
104 decimals will be truncated. See C<int>
105  
106 =cut
107  
108 sub establishHeadingStyle {
109 my $oodoc_styles = shift; # $self
110 my $level_param = shift;
111 my $style_definition = shift;
112  
113 my $level = looks_like_number $level_param ? int $level_param : 0;
114 warn "Changed level '$level_param' to '$level'" unless $level eq $level_param;
115  
116 my $oodoc_style_name = "Heading_20_$level";
117  
118 return (
119 $oodoc_styles->getStyleElement($oodoc_style_name)
120 or $oodoc_styles->createHeadingStyle( $level, $style_definition )
121 )
122 }
123  
124  
125 =head2 createHeadingStyle
126  
127 Creates a new Heading Style in the 'styles' part for a given level. It accepts
128 an optional style-definition HashRef like the above.
129  
130 =cut
131  
132 sub createHeadingStyle {
133 my $oodoc_styles = shift; # $self
134 my $level_param = shift;
135 my $style_definition = shift;
136  
137 my $level = looks_like_number $level_param ? int $level_param : 0;
138 warn "Changed level '$level_param' to '$level'" unless $level eq $level_param;
139  
140 my $oodoc_style_name = "Heading_20_$level";
141 my $display_name = "Heading $level";
142  
143 $style_definition //= $HEADING_DEFINITIONS->{$display_name};
144  
145 $oodoc_styles->createStyle( $oodoc_style_name =>
146 'class' => 'text',
147 'display-name' => $display_name,
148 'family' => 'paragraph',
149 'next' => 'Text_20_body',
150 'parent' => 'Heading',
151 );
152 $oodoc_styles->updateStyle( $oodoc_style_name =>
153 'properties' => { '-area' => $_,
154 _propertiesFromStyleArea( $style_definition, $_ )
155 }
156 ) for qw /paragraph text/;
157  
158 return $oodoc_styles->getStyleElement($display_name)
159 }
160  
161 # following 'pseudo attributes' were found when doing some reverse engineering
162 # and by examining some documents that had been created, without changing the
163 # styling.
164 use constant PROPERTIES_MAP => {
165 paragraph => {
166 top => 'fo:margin-top',
167 bottom => 'fo:margin-bottom',
168 },
169 text => {
170 size => 'fo:font-size',
171 weight => 'fo:font-weight',
172 style => 'fo:font-style',
173 family => 'fo:font-family',
174 name => 'style:font-name',
175 font_style => 'style:font-style-name',
176 },
177 };
178  
179 # _propertiesFromStyle
180 #
181 # return a set of properties for a given 'area', mapped from simple format into
182 # horrible OpenOffice::OODoc xml attributes
183 #
184 sub _propertiesFromStyleArea {
185 my $style_definition = shift;
186 my $area = shift;
187  
188 my %properties = map {
189 PROPERTIES_MAP->{$area}{$_} => $style_definition->{$area}{$_}
190 } keys %{ $style_definition->{$area} };
191 return %properties
192 }
193  
194 1;
195  
196 __END__
↑ Summary