Skip to content

Commit ba33379

Browse files
committed
Added syntax tag for changing the page format, orientation and page margins.
E.g. '{{odt>page:A3,landscape,1,1,1,1}}' sets the page format to A3, landscape orientation with a 1cm margin on each side. If the margins are omitted, 2cm margins are assumed.
1 parent 3ff5163 commit ba33379

File tree

9 files changed

+285
-74
lines changed

9 files changed

+285
-74
lines changed

ChangeLog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2015-07-18 LarsDW223
2+
3+
* Added syntax tag for changing the page format, orientation and page margins.
4+
E.g. '{{odt>page:A3,landscape,1,1,1,1}}' sets the page format to A3, landscape orientation
5+
with a 1cm margin on each side. If the margins are omitted, 2cm margins are assumed.
6+
17
2015-07-09 LarsDW223
28

39
* Changed the locallink() implementation from just inserting text to inserting a link

ODT/ODTTemplateDH.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function setDirectory($directory) {
6262
* @param ODTDefaultStyles $styleset
6363
* @return mixed
6464
*/
65-
public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null){
65+
public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null, $pagestyles=null){
6666
// for the temp dir
6767
global $conf, $ID;
6868

@@ -118,6 +118,15 @@ public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=nul
118118
$this->_odtReplaceInFile('</office:styles>', $missingstyles.'</office:styles>', $temp_dir.'/styles.xml');
119119
$this->_odtReplaceInFile('</office:font-face-decls>', $missingfonts.'</office:font-face-decls>', $temp_dir.'/styles.xml');
120120

121+
// Insert page styles
122+
$page = '';
123+
foreach ($pagestyles as $name => $layout_name) {
124+
$page .= '<style:master-page style:name="'.$name.'" style:page-layout-name="'.$layout_name.'"/>';
125+
}
126+
if ( !empty($page) ) {
127+
$this->_odtReplaceInFile('</office:master-styles>', $page.'</office:master-styles>', $temp_dir.'/styles.xml');
128+
}
129+
121130
// Add manifest data
122131
$this->_odtReplaceInFile('</manifest:manifest>', $this->manifest->getExtraContent() . '</manifest:manifest>', $temp_dir . '/META-INF/manifest.xml');
123132

ODT/docHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function addFile($name, $mime, $content) {
7373
* @param ODTStyleSet $styleset
7474
* @return mixed
7575
*/
76-
abstract public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null);
76+
abstract public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null, $pagestyles=null);
7777

7878
/**
7979
* Get ODT document file.

ODT/page.php

Lines changed: 150 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
class pageFormat
1515
{
16+
var $format = 'A4';
17+
var $orientation = 'portrait';
18+
1619
// Page parameters.
1720
// All values are assumed to be in 'cm' units.
1821
var $width = 21;
@@ -23,171 +26,250 @@ class pageFormat
2326
var $margin_right = 2;
2427

2528
/**
26-
* Set format. Sets all vlaues according to $format.
29+
* Return given page format parameters as a single string.
30+
*
31+
* @param string $format
32+
* @param string $orientation
33+
* @param string $margin_top
34+
* @param string $margin_right
35+
* @param string $margin_bottom
36+
* @param string $margin_left
37+
* @return string
38+
*/
39+
public static function formatToString ($format, $orientation, $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
40+
$margins = $margin_top.'-'.$margin_right.'-'.$margin_bottom.'-'.$margin_left;
41+
$margins = str_replace (',', '', $margins);
42+
$margins = str_replace ('.', '', $margins);
43+
return $format.'-'.$orientation.'-'.$margins;
44+
}
45+
46+
/**
47+
* Return currently set format parameters as a single string.
48+
*
49+
* @return string
50+
*/
51+
public function toString () {
52+
$margins = $this->margin_top.'-'.$this->margin_right.'-'.$this->margin_bottom.'-'.$this->margin_left;
53+
$margins = str_replace (',', '', $margins);
54+
$margins = str_replace ('.', '', $margins);
55+
return $this->format.'-'.$this->orientation.'-'.$margins;
56+
}
57+
58+
/**
59+
* Query format data. Returns data in assoziative array $dest.
60+
* Returned fields are 'format', 'orientation', 'width', 'height',
61+
* 'margin-top', 'margin-bottom', 'margin-left' and 'margin-right'.
62+
* If $format is unknown, then format 'A4' will be assumed.
2763
*
2864
* @param string $format
2965
* @param string $orientation
3066
*/
31-
public function setFormat($format, $orientation='portrait') {
67+
public static function queryFormat (&$dest, $format, $orientation='portrait', $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
3268
switch ($format) {
3369
case 'A6':
34-
$this->width = 10.5;
35-
$this->height = 14.8;
70+
$width = 10.5;
71+
$height = 14.8;
3672
break;
3773

3874
case 'A5':
39-
$this->width = 14.8;
40-
$this->height = 21;
75+
$width = 14.8;
76+
$height = 21;
4177
break;
4278

4379
case 'A3':
44-
$this->width = 29.7;
45-
$this->height = 42;
80+
$width = 29.7;
81+
$height = 42;
4682
break;
4783

4884
case 'B6 (ISO)':
49-
$this->width = 12.5;
50-
$this->height = 17.6;
85+
$width = 12.5;
86+
$height = 17.6;
5187
break;
5288

5389
case 'B5 (ISO)':
54-
$this->width = 17.6;
55-
$this->height = 25;
90+
$width = 17.6;
91+
$height = 25;
5692
break;
5793

5894
case 'B4 (ISO)':
59-
$this->width = 25;
60-
$this->height = 35.3;
95+
$width = 25;
96+
$height = 35.3;
6197
break;
6298

6399
case 'Letter':
64-
$this->width = 21.59;
65-
$this->height = 27.94;
100+
$width = 21.59;
101+
$height = 27.94;
66102
break;
67103

68104
case 'Legal':
69-
$this->width = 21.59;
70-
$this->height = 35.56;
105+
$width = 21.59;
106+
$height = 35.56;
71107
break;
72108

73109
case 'Long Bond':
74-
$this->width = 21.59;
75-
$this->height = 33.02;
110+
$width = 21.59;
111+
$height = 33.02;
76112
break;
77113

78114
case 'Tabloid':
79-
$this->width = 27.94;
80-
$this->height = 43.18;
115+
$width = 27.94;
116+
$height = 43.18;
81117
break;
82118

83119
case 'B6 (JIS)':
84-
$this->width = 12.8;
85-
$this->height = 18.2;
120+
$width = 12.8;
121+
$height = 18.2;
86122
break;
87123

88124
case 'B5 (JIS)':
89-
$this->width = 18.2;
90-
$this->height = 25.7;
125+
$width = 18.2;
126+
$height = 25.7;
91127
break;
92128

93129
case 'B4 (JIS)':
94-
$this->width = 25.7;
95-
$this->height = 36.4;
130+
$width = 25.7;
131+
$height = 36.4;
96132
break;
97133

98134
case '16 Kai':
99-
$this->width = 18.4;
100-
$this->height = 26;
135+
$width = 18.4;
136+
$height = 26;
101137
break;
102138

103139
case '32 Kai':
104-
$this->width = 13;
105-
$this->height = 18.4;
140+
$width = 13;
141+
$height = 18.4;
106142
break;
107143

108144
case 'Big 32 Kai':
109-
$this->width = 14;
110-
$this->height = 20.3;
145+
$width = 14;
146+
$height = 20.3;
111147
break;
112148

113149
case 'DL Envelope':
114-
$this->width = 11;
115-
$this->height = 22;
150+
$width = 11;
151+
$height = 22;
116152
break;
117153

118154
case 'C6 Envelope':
119-
$this->width = 11.4;
120-
$this->height = 16.2;
155+
$width = 11.4;
156+
$height = 16.2;
121157
break;
122158

123159
case 'C6/5 Envelope':
124-
$this->width = 11.4;
125-
$this->height = 22.9;
160+
$width = 11.4;
161+
$height = 22.9;
126162
break;
127163

128164
case 'C5 Envelope':
129-
$this->width = 16.2;
130-
$this->height = 22.9;
165+
$width = 16.2;
166+
$height = 22.9;
131167
break;
132168

133169
case 'C4 Envelope':
134-
$this->width = 22.9;
135-
$this->height = 32.4;
170+
$width = 22.9;
171+
$height = 32.4;
136172
break;
137173

138174
case '#6 3/4 Envelope':
139-
$this->width = 9.21;
140-
$this->height = 16.51;
175+
$width = 9.21;
176+
$height = 16.51;
141177
break;
142178

143179
case '#7 3/4 (Monarch) Envelope':
144-
$this->width = 9.84;
145-
$this->height = 19.05;
180+
$width = 9.84;
181+
$height = 19.05;
146182
break;
147183

148184
case '#9 Envelope':
149-
$this->width = 9.84;
150-
$this->height = 22.54;
185+
$width = 9.84;
186+
$height = 22.54;
151187
break;
152188

153189
case '#10 Envelope':
154-
$this->width = 10.48;
155-
$this->height = 24.13;
190+
$width = 10.48;
191+
$height = 24.13;
156192
break;
157193

158194
case '#11 Envelope':
159-
$this->width = 11.43;
160-
$this->height = 26.35;
195+
$width = 11.43;
196+
$height = 26.35;
161197
break;
162198

163199
case '#12 Envelope':
164-
$this->width = 12.07;
165-
$this->height = 27.94;
200+
$width = 12.07;
201+
$height = 27.94;
166202
break;
167203

168204
case 'Japanese Postcard':
169-
$this->width = 10;
170-
$this->height = 14.8;
205+
$width = 10;
206+
$height = 14.8;
171207
break;
172208

173209
case 'A4':
174210
default:
175-
$this->width = 21;
176-
$this->height = 29.7;
211+
$format = 'A4';
212+
$width = 21;
213+
$height = 29.7;
177214
break;
178215
}
179216

180217
if ( $orientation != 'portrait' ) {
181-
$help = $this->width;
182-
$this->width = $this->height;
183-
$this->height = $help;
218+
$orientation = 'landscape';
219+
$help = $width;
220+
$width = $height;
221+
$height = $help;
184222
}
185223

186-
// Margins are always the same
187-
$this->margin_top = 2;
188-
$this->margin_bottom = 2;
189-
$this->margin_left = 2;
190-
$this->margin_right = 2;
224+
// Return format data.
225+
$dest ['format'] = $format;
226+
$dest ['orientation'] = $orientation;
227+
$dest ['width'] = $width;
228+
$dest ['height'] = $height;
229+
230+
// Margins are currently accepted 'as is'
231+
// but could be subject to further checks/adjustments in the future.
232+
$dest ['margin-top'] = $margin_top;
233+
$dest ['margin-bottom'] = $margin_bottom;
234+
$dest ['margin-left'] = $margin_left;
235+
$dest ['margin-right'] = $margin_right;
236+
}
237+
238+
/**
239+
* Set format. Sets all values according to $format.
240+
*
241+
* @param string $format
242+
* @param string $orientation
243+
*/
244+
public function setFormat($format, $orientation='portrait', $margin_top=2, $margin_right=2, $margin_bottom=2, $margin_left=2) {
245+
$data = array();
246+
247+
// Query format data
248+
$this->queryFormat ($data, $format, $orientation, $margin_top, $margin_right, $margin_bottom, $margin_left);
249+
250+
// Save as page settings
251+
$this->format = $data ['format'];
252+
$this->orientation = $data ['orientation'];
253+
$this->width = $data ['width'];
254+
$this->height = $data ['height'];
255+
$this->margin_top = $data ['margin-top'];
256+
$this->margin_bottom = $data ['margin-bottom'];
257+
$this->margin_left = $data ['margin-left'];
258+
$this->margin_right = $data ['margin-right'];
259+
}
260+
261+
/**
262+
* @return string
263+
*/
264+
public function getFormat() {
265+
return $this->format;
266+
}
267+
268+
/**
269+
* @return string
270+
*/
271+
public function getOrientation() {
272+
return $this->orientation;
191273
}
192274

193275
/**

ODT/scratchDH.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct() {
4545
* @param ODTStyleSet $styleset
4646
* @return mixed
4747
*/
48-
public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null){
48+
public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=null, $userfields=null, $styleset=null, $pagestyles=null){
4949
// add defaults
5050
$this->ZIP->add_File('application/vnd.oasis.opendocument.text', 'mimetype', 0);
5151
$this->ZIP->add_File($meta,'meta.xml');
@@ -101,6 +101,15 @@ public function build($doc=null, $autostyles=null, $commonstyles=null, $meta=nul
101101

102102
$value = io_readFile(DOKU_PLUGIN.'odt/styles.xml');
103103

104+
// Add page styles
105+
$page = '';
106+
foreach ($pagestyles as $name => $layout_name) {
107+
$page .= '<style:master-page style:name="'.$name.'" style:page-layout-name="'.$layout_name.'"/>';
108+
}
109+
if ( !empty($page) ) {
110+
$value = str_replace('</office:master-styles>', $page.'</office:master-styles>', $value);
111+
}
112+
104113
// Add common styles.
105114
$common = '';
106115
foreach ($commonstyles as $style) {

0 commit comments

Comments
 (0)