root/gs3-extensions/html-to-expeditee/trunk/src/perllib/ExpediteeFrameIO.pm @ 24941

Revision 24941, 10.9 KB (checked in by davidb, 8 years ago)

Work on converting CSS style attributes to Expeditee attributes

Line 
1###########################################################################
2#
3# ExpediteeFrameIO.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2009 New Zealand Digital Library Project
9#
10# This program is free software; you can redistr   te it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package ExpediteeFrameIO;
27
28use strict;
29
30use CssStyleToExpAttr;
31
32sub new
33{
34    my $class = shift(@_);
35    my $output_dir = shift(@_);
36    my $username   = shift(@_) || "greenstone";
37
38    my $self = { 'items' => [], 'lines' => [], 'constraints' => [] };
39
40    $self->{'output_dir'} = $output_dir;
41    $self->{'username'} = $username;
42
43    return bless $self, $class;
44}
45
46sub getFormatedDate
47{
48    my ($opt_mode) = @_;
49
50    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
51
52    my @mabbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
53
54    $year += 1900;
55
56    my $fdate;
57
58    if ((defined $opt_mode) && ($opt_mode eq "highPrecision")) {
59    $fdate = sprintf("%02d%s%04d[%02d:%0d2.%02d]",
60             $mday, $mabbr[$mon],$year,$hour,$min,$sec);
61    }
62    else {
63    $fdate = sprintf("%02d%s%04d[%02d:%0d2]",
64             $mday, $mabbr[$mon],$year,$hour,$min);
65    }
66
67    return $fdate;
68}
69
70sub convertStyleToAttr
71{
72    my ($css_attr) = @_;
73
74    my $exp_attr = {};
75
76    if (defined $css_attr->{'background-color'}) {
77    $exp_attr->{'e'} = $css_attr->{'background-color'};
78    }
79
80    # load up some defaults for font information
81    my $exp_font_family = "s"; # t
82    my $exp_font_face = "r";
83    my $exp_font_size = "14";
84
85#    if (defined $css_attr->{'font-family'}) {
86#   $font_family = $font_family_lookup->[$css_attr->{'font-family'}];
87#    }
88
89    if (defined $css_attr->{'font-size'}) {
90    my $css_font_size = $css_attr->{'font-size'};
91    $exp_font_size = CssStyleToExpAttr::convert_font_size($css_font_size);
92    }
93
94#    if (defined $css_attr->{'font-face'}) {
95#   $font = conver_font_face($css_attr->{'font-face'});
96#    }
97
98#    $exp_attr->{'f'} = $exp_font_family.$exp_font_face.$exp_font_size;
99
100    return $exp_attr;
101}
102
103
104sub _nextFreeId
105{
106    my $self = shift @_;
107
108    my $items = $self->{'items'};
109    my $lines = $self->{'lines'};
110    my $constraints = $self->{'constraints'};
111   
112    # Ids start at base of 1
113    return 1+(scalar(@$items) + scalar(@$lines) + scalar(@$constraints));
114}
115
116
117sub _addItem
118{
119    my $self = shift @_;
120    my ($type,$attr) = @_;
121
122    # By this point 'attr' is synonymous with being an item
123
124    my $items = $self->{'items'};
125
126    my $next_free_id = $self->_nextFreeId();
127
128    $attr->{'_type'} = $type;
129    $attr->{'_id'} = $next_free_id;
130
131    push(@$items,$attr);
132
133    return ($attr,$next_free_id);
134}
135
136
137
138sub _setBaseDefaultAttributes
139{
140    my $self = shift @_;
141    my ($attr) = @_;
142
143    $attr->{'o'} = $self->{'username'};
144    $attr->{'s'} = getFormatedDate("highPrecision");
145    $attr->{'Q'} = "0";     # gradient
146    $attr->{'v'} = "S";     # dot type
147}
148
149
150sub setPointDefaultAttributes
151{
152    my $self = shift @_;
153    my ($attr) = @_;
154
155    $self->_setBaseDefaultAttributes($attr);
156}
157
158sub setTextDefaultAttributes
159{
160    my $self = shift @_;
161    my ($attr) = @_;
162
163    $self->_setBaseDefaultAttributes($attr);
164
165    $attr->{'d'} = "0 0 0"; # black color
166}
167
168
169sub setRectPointDefaultAttributes
170{
171    my $self = shift @_;
172    my ($attr) = @_;
173
174    $self->setPointDefaultAttributes($attr);
175
176    $attr->{'d'} = "80 80 80"; # green color for rect lines
177    $attr->{'h'} = "1.0";     # line thickness
178}
179
180
181sub addRectPoint
182{
183    my $self = shift @_;
184    my ($x, $y, $attr) = @_;
185
186    my %attr_copy = %$attr; # make a private copy of 'attr'
187
188    $self->setRectPointDefaultAttributes(\%attr_copy);
189
190    my $items = $self->{'items'};
191
192    $attr_copy{'P'} = "$x $y";
193
194    return $self->_addItem("P",\%attr_copy);
195}
196
197sub addText
198{
199    my $self = shift @_;
200    my ($x, $y, $text, $w, $attr) = @_;
201
202    my %attr_copy = %$attr; # make a private copy of 'attr'
203
204    $self->setTextDefaultAttributes(\%attr_copy);
205
206    my $items = $self->{'items'};
207
208    $attr_copy{'P'} = "$x $y";
209    $attr_copy{'T'} = $text;
210    $attr_copy{'w'} = "-$w" if (defined $w);
211
212    return $self->_addItem("T",\%attr_copy);
213}
214
215sub addLine
216{
217    my $self = shift @_;
218
219    my ($item_id1,$item_id2) = @_;
220
221    my $lines = $self->{'lines'};
222    my $line_type = 1;
223
224    my $next_free_id = $self->_nextFreeId();
225
226    my $attr = { 'L' => "$next_free_id $line_type" };
227
228    $attr->{'s'} = "$item_id1 $item_id2";
229
230    push(@$lines,$attr);
231
232    return ($attr,$next_free_id);
233}
234
235
236sub addConstraint
237{
238    my $self = shift @_;
239
240    my ($orientation,$item_id1,$item_id2) = @_;
241
242    my $constraints = $self->{'constraints'};
243
244    my $orientation_type = undef;
245    if ($orientation eq "vertical") {
246    $orientation_type = 2;
247    }
248    else {
249    # assume horizontal for now
250    $orientation_type = 3;
251    }
252
253    my $next_free_id = $self->_nextFreeId();
254
255    my $attr = { 'C' => "$next_free_id $orientation_type" };
256
257    $attr->{'s'} = "$item_id1 $item_id2";
258
259    push(@$constraints,$attr);
260
261    return ($attr,$next_free_id);
262}
263
264
265sub addRect
266{
267    my $self = shift @_;
268
269    my ($xl, $yt, $xr, $yb, $attr) = @_;
270   
271    # do point in same order Expeditee puts them in
272    my ($p_tr,$p_tr_id) = $self->addRectPoint($xr,$yt,$attr);
273    my ($p_tl,$p_tl_id) = $self->addRectPoint($xl,$yt,$attr);
274    my ($p_bl,$p_bl_id) = $self->addRectPoint($xl,$yb,$attr);
275    my ($p_br,$p_br_id) = $self->addRectPoint($xr,$yb,$attr);
276
277    my ($l_t,$l_t_id) = $self->addLine($p_tr_id,$p_tl_id);
278    my ($l_l,$l_l_id) = $self->addLine($p_tl_id,$p_bl_id);
279    my ($l_b,$l_b_id) = $self->addLine($p_bl_id,$p_br_id);
280    my ($l_r,$l_r_id) = $self->addLine($p_br_id,$p_tr_id);
281
282    my ($c_t,$c_t_id) = $self->addConstraint("horizontal",$p_tr_id,$p_tl_id);
283    my ($c_l,$c_l_id) = $self->addConstraint("vertical"  ,$p_tl_id,$p_bl_id);
284    my ($c_b,$c_b_id) = $self->addConstraint("horizontal",$p_bl_id,$p_br_id);
285    my ($c_r,$c_r_id) = $self->addConstraint("vertical"  ,$p_br_id,$p_tr_id);
286
287    $p_tr->{'l'} = "$l_t_id $l_r_id";
288    $p_tl->{'l'} = "$l_t_id $l_l_id";
289    $p_bl->{'l'} = "$l_l_id $l_b_id";
290    $p_br->{'l'} = "$l_b_id $l_r_id";
291
292    $p_tr->{'c'} = "$c_t_id $c_r_id";
293    $p_tl->{'c'} = "$c_t_id $c_l_id";
294    $p_bl->{'c'} = "$c_l_id $c_b_id";
295    $p_br->{'c'} = "$c_b_id $c_r_id";
296
297}
298
299sub writeHeaderSection
300{
301    my $self = shift @_;
302
303    # Example header:
304    #   V 1
305    #   p 4
306    #   U davidb
307    #   D 09Jan2012[13:33]
308    #   M davidb
309    #   d 09Jan2012[13:33]
310    #   Z
311    #   
312
313    # Legend:
314    #   V = version
315    #   p = permision level
316    #   U = username (owner)
317    #   M = last modified by
318    #   D, d = date information
319    #   Z => end of section
320
321
322    my $username = $self->{'username'};
323
324    my $fdate = getFormatedDate();
325
326    print FOUT "V 1\n";
327    print FOUT "p 4\n";
328    print FOUT "U $username\n";
329    print FOUT "D $fdate\n";
330    print FOUT "M $username\n";
331    print FOUT "d $fdate\n";
332    print FOUT "Z\n\n";
333
334}
335
336
337sub writeItemsSection
338{
339    my $self = shift @_;
340
341    my $items = $self->{'items'};
342
343    foreach my $item (@$items) {
344    my $type = delete $item->{'_type'};
345    my $id   = delete $item->{'_id'};
346
347    print FOUT "S $type $id\n";
348    foreach my $a (keys %$item) {
349        print FOUT "$a ", $item->{$a}, "\n";
350    }
351   
352    print FOUT "\n";
353    }
354
355    print FOUT "Z\n\n";
356}
357
358sub writeLinesSection
359{
360    my $self = shift @_;
361
362    my $lines = $self->{'lines'};
363
364    foreach my $line (@$lines) {
365
366    print FOUT "L ", $line->{'L'}, "\n";
367    print FOUT "s ", $line->{'s'}, "\n";
368   
369    print FOUT "\n";
370    }
371
372    print FOUT "Z\n\n";
373
374}
375
376sub writeConstraintsSection
377{
378    my $self = shift @_;
379
380    my $constraints = $self->{'constraints'};
381
382    foreach my $constraint (@$constraints) {
383    print FOUT "C ", $constraint->{'C'}, "\n";
384    print FOUT "s ", $constraint->{'s'}, "\n";
385   
386    print FOUT "\n";
387    }
388
389    print FOUT "Z\n\n";
390}
391
392sub writeStatisticsSection
393{
394    my $self = shift @_;
395
396    # Currently do nothing
397}
398
399sub saveFrame
400{
401    my $self = shift @_;
402    my ($file) = @_;
403
404    my $filename = &util::filename_cat($self->{'output_dir'},$file);
405
406    my $status = undef;
407
408    if (open(FOUT,">$filename")) {
409    binmode(FOUT,":utf8");
410    $self->writeHeaderSection();
411    $self->writeItemsSection();
412    $self->writeLinesSection();
413    $self->writeConstraintsSection();
414    $self->writeStatisticsSection();
415
416    close(FOUT);
417    $status = 1;
418    }
419    else {
420    print STDERR "ExpediteeFrameIO::saveFrame() Failed to open $filename for output\n";
421    $status = 0;
422    }
423
424    return $status;
425}
426
427sub buildFrame
428{
429    my $self = shift @_;
430    my ($html_node) = @_;
431 
432    my $type = $html_node->{'type'};
433
434    if ($type eq "rect") {
435
436    my $rect = $html_node->{'rect'};
437    my $xl = $rect->{'xl'};
438    my $xr = $rect->{'xr'};
439    my $yt = $rect->{'yt'};
440    my $yb = $rect->{'yb'};
441
442    my $attr = convertStyleToAttr($html_node->{'style'});
443
444    $self->addRect($xl,$yt,$xr,$yb,$attr);
445
446    if (defined $html_node->{'img'}) {
447
448        my $img_url = $html_node->{'img'};
449        $img_url =~ s/^http:\/\/www\.nzdl\.org\/greenstone3-showcase\///;
450        if ($img_url =~ m/^interfaces\//) {
451        $img_url = "images/$img_url";
452        }
453        elsif ($img_url =~ m/^sites\//) {
454        $img_url =~ s/^sites\/(.*?)\//images\//;
455        }
456
457        my $x = $xl;
458        my $y = $yt;
459
460        my $attr = {};
461
462        my $img_text = "\@i: $img_url";
463
464        $self->addText($x,$y,$img_text,undef,$attr);
465    }
466
467    }
468    elsif ($type eq "text") {
469    my $text = $html_node->{'text'};
470
471    my $x = $html_node->{'xl'};
472    my $y = $html_node->{'yt'};
473    my $w = $html_node->{'xr'} - $x +1;
474
475    my $attr = convertStyleToAttr($html_node->{'style'});
476
477    # fudge factor for now (based on default font size used)
478    $y += 16; # y-value of text item in Expeditee is it's base line
479    $x += 4;
480
481    $self->addText($x,$y,$text,$w,$attr);
482    }
483    else {
484    print STDERR "ExpediteeFrameIO::buildFrame(): Warning, unrecognized type '$type'\n";
485    }
486
487    my $childNodes = $html_node->{'childNodes'};
488    foreach my $child_node (@$childNodes) {
489    $self->buildFrame($child_node);
490    }
491}
492
493
494sub saveLastFrameNumber
495{
496    my $self = shift @_;
497    my ($last_frame_number) = @_;
498
499    my $filename = &util::filename_cat($self->{'output_dir'},"frame.inf");
500
501    my $status = undef;
502
503    if (open(FNOUT,">$filename")) {
504    binmode(FNOUT,":utf8");
505   
506    print FNOUT "$last_frame_number\n";
507
508    close(FNOUT);
509    $status = 1;
510    }
511    else {
512    print STDERR "ExpediteeFrameIO::saveLastFrameNumber() Failed to open $filename for output\n";
513    $status = 0;
514    }
515
516    return $status;
517
518}
519
5201;
Note: See TracBrowser for help on using the browser.