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

Revision 24944, 11.0 KB (checked in by davidb, 8 years ago)

More careful recursive traversal to classifier nodes; further developed of CSS to Expeditee attributes (background colour); and slightly less cludgy way to deal with full img URLS

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    # load up some defaults for font information
77    my $exp_font_family = "s"; # t
78    my $exp_font_face = "r";
79    my $exp_font_size = "14";
80
81#    if (defined $css_attr->{'font-family'}) {
82#   $font_family = $font_family_lookup->[$css_attr->{'font-family'}];
83#    }
84
85    if (defined $css_attr->{'font-size'}) {
86    my $css_font_size = $css_attr->{'font-size'};
87    $exp_font_size = CssStyleToExpAttr::convert_font_size($css_font_size);
88    }
89
90#    if (defined $css_attr->{'font-face'}) {
91#   $font = conver_font_face($css_attr->{'font-face'});
92#    }
93
94#    $exp_attr->{'f'} = $exp_font_family.$exp_font_face.$exp_font_size;
95
96    # background color
97
98
99    if (defined $css_attr->{'background-color'}) {
100    my $css_color = $css_attr->{'background-color'};
101
102    my $exp_color = CssStyleToExpAttr::convert_color($css_color);
103
104    $exp_attr->{'e'} = $exp_color;
105    }
106
107    return $exp_attr;
108}
109
110
111sub _nextFreeId
112{
113    my $self = shift @_;
114
115    my $items = $self->{'items'};
116    my $lines = $self->{'lines'};
117    my $constraints = $self->{'constraints'};
118   
119    # Ids start at base of 1
120    return 1+(scalar(@$items) + scalar(@$lines) + scalar(@$constraints));
121}
122
123
124sub _addItem
125{
126    my $self = shift @_;
127    my ($type,$attr) = @_;
128
129    # By this point 'attr' is synonymous with being an item
130
131    my $items = $self->{'items'};
132
133    my $next_free_id = $self->_nextFreeId();
134
135    $attr->{'_type'} = $type;
136    $attr->{'_id'} = $next_free_id;
137
138    push(@$items,$attr);
139
140    return ($attr,$next_free_id);
141}
142
143
144
145sub _setBaseDefaultAttributes
146{
147    my $self = shift @_;
148    my ($attr) = @_;
149
150    $attr->{'o'} = $self->{'username'};
151    $attr->{'s'} = getFormatedDate("highPrecision");
152    $attr->{'Q'} = "0";     # gradient
153    $attr->{'v'} = "S";     # dot type
154}
155
156
157sub setPointDefaultAttributes
158{
159    my $self = shift @_;
160    my ($attr) = @_;
161
162    $self->_setBaseDefaultAttributes($attr);
163}
164
165sub setTextDefaultAttributes
166{
167    my $self = shift @_;
168    my ($attr) = @_;
169
170    $self->_setBaseDefaultAttributes($attr);
171
172    $attr->{'d'} = "0 0 0"; # black color
173}
174
175
176sub setRectPointDefaultAttributes
177{
178    my $self = shift @_;
179    my ($attr) = @_;
180
181    $self->setPointDefaultAttributes($attr);
182
183    $attr->{'d'} = "80 80 80"; # green color for rect lines
184    $attr->{'h'} = "1.0";     # line thickness
185}
186
187
188sub addRectPoint
189{
190    my $self = shift @_;
191    my ($x, $y, $attr) = @_;
192
193    my %attr_copy = %$attr; # make a private copy of 'attr'
194
195    $self->setRectPointDefaultAttributes(\%attr_copy);
196
197    my $items = $self->{'items'};
198
199    $attr_copy{'P'} = "$x $y";
200
201    return $self->_addItem("P",\%attr_copy);
202}
203
204sub addText
205{
206    my $self = shift @_;
207    my ($x, $y, $text, $w, $attr) = @_;
208
209    my %attr_copy = %$attr; # make a private copy of 'attr'
210
211    $self->setTextDefaultAttributes(\%attr_copy);
212
213    my $items = $self->{'items'};
214
215    $attr_copy{'P'} = "$x $y";
216    $attr_copy{'T'} = $text;
217    $attr_copy{'w'} = "-$w" if (defined $w);
218
219    return $self->_addItem("T",\%attr_copy);
220}
221
222sub addLine
223{
224    my $self = shift @_;
225
226    my ($item_id1,$item_id2) = @_;
227
228    my $lines = $self->{'lines'};
229    my $line_type = 1;
230
231    my $next_free_id = $self->_nextFreeId();
232
233    my $attr = { 'L' => "$next_free_id $line_type" };
234
235    $attr->{'s'} = "$item_id1 $item_id2";
236
237    push(@$lines,$attr);
238
239    return ($attr,$next_free_id);
240}
241
242
243sub addConstraint
244{
245    my $self = shift @_;
246
247    my ($orientation,$item_id1,$item_id2) = @_;
248
249    my $constraints = $self->{'constraints'};
250
251    my $orientation_type = undef;
252    if ($orientation eq "vertical") {
253    $orientation_type = 2;
254    }
255    else {
256    # assume horizontal for now
257    $orientation_type = 3;
258    }
259
260    my $next_free_id = $self->_nextFreeId();
261
262    my $attr = { 'C' => "$next_free_id $orientation_type" };
263
264    $attr->{'s'} = "$item_id1 $item_id2";
265
266    push(@$constraints,$attr);
267
268    return ($attr,$next_free_id);
269}
270
271
272sub addRect
273{
274    my $self = shift @_;
275
276    my ($xl, $yt, $xr, $yb, $attr) = @_;
277   
278    # do point in same order Expeditee puts them in
279    my ($p_tr,$p_tr_id) = $self->addRectPoint($xr,$yt,$attr);
280    my ($p_tl,$p_tl_id) = $self->addRectPoint($xl,$yt,$attr);
281    my ($p_bl,$p_bl_id) = $self->addRectPoint($xl,$yb,$attr);
282    my ($p_br,$p_br_id) = $self->addRectPoint($xr,$yb,$attr);
283
284    my ($l_t,$l_t_id) = $self->addLine($p_tr_id,$p_tl_id);
285    my ($l_l,$l_l_id) = $self->addLine($p_tl_id,$p_bl_id);
286    my ($l_b,$l_b_id) = $self->addLine($p_bl_id,$p_br_id);
287    my ($l_r,$l_r_id) = $self->addLine($p_br_id,$p_tr_id);
288
289    my ($c_t,$c_t_id) = $self->addConstraint("horizontal",$p_tr_id,$p_tl_id);
290    my ($c_l,$c_l_id) = $self->addConstraint("vertical"  ,$p_tl_id,$p_bl_id);
291    my ($c_b,$c_b_id) = $self->addConstraint("horizontal",$p_bl_id,$p_br_id);
292    my ($c_r,$c_r_id) = $self->addConstraint("vertical"  ,$p_br_id,$p_tr_id);
293
294    $p_tr->{'l'} = "$l_t_id $l_r_id";
295    $p_tl->{'l'} = "$l_t_id $l_l_id";
296    $p_bl->{'l'} = "$l_l_id $l_b_id";
297    $p_br->{'l'} = "$l_b_id $l_r_id";
298
299    $p_tr->{'c'} = "$c_t_id $c_r_id";
300    $p_tl->{'c'} = "$c_t_id $c_l_id";
301    $p_bl->{'c'} = "$c_l_id $c_b_id";
302    $p_br->{'c'} = "$c_b_id $c_r_id";
303
304}
305
306sub writeHeaderSection
307{
308    my $self = shift @_;
309
310    # Example header:
311    #   V 1
312    #   p 4
313    #   U davidb
314    #   D 09Jan2012[13:33]
315    #   M davidb
316    #   d 09Jan2012[13:33]
317    #   Z
318    #   
319
320    # Legend:
321    #   V = version
322    #   p = permision level
323    #   U = username (owner)
324    #   M = last modified by
325    #   D, d = date information
326    #   Z => end of section
327
328
329    my $username = $self->{'username'};
330
331    my $fdate = getFormatedDate();
332
333    print FOUT "V 1\n";
334    print FOUT "p 4\n";
335    print FOUT "U $username\n";
336    print FOUT "D $fdate\n";
337    print FOUT "M $username\n";
338    print FOUT "d $fdate\n";
339    print FOUT "Z\n\n";
340
341}
342
343
344sub writeItemsSection
345{
346    my $self = shift @_;
347
348    my $items = $self->{'items'};
349
350    foreach my $item (@$items) {
351    my $type = delete $item->{'_type'};
352    my $id   = delete $item->{'_id'};
353
354    print FOUT "S $type $id\n";
355    foreach my $a (keys %$item) {
356        print FOUT "$a ", $item->{$a}, "\n";
357    }
358   
359    print FOUT "\n";
360    }
361
362    print FOUT "Z\n\n";
363}
364
365sub writeLinesSection
366{
367    my $self = shift @_;
368
369    my $lines = $self->{'lines'};
370
371    foreach my $line (@$lines) {
372
373    print FOUT "L ", $line->{'L'}, "\n";
374    print FOUT "s ", $line->{'s'}, "\n";
375   
376    print FOUT "\n";
377    }
378
379    print FOUT "Z\n\n";
380
381}
382
383sub writeConstraintsSection
384{
385    my $self = shift @_;
386
387    my $constraints = $self->{'constraints'};
388
389    foreach my $constraint (@$constraints) {
390    print FOUT "C ", $constraint->{'C'}, "\n";
391    print FOUT "s ", $constraint->{'s'}, "\n";
392   
393    print FOUT "\n";
394    }
395
396    print FOUT "Z\n\n";
397}
398
399sub writeStatisticsSection
400{
401    my $self = shift @_;
402
403    # Currently do nothing
404}
405
406sub saveFrame
407{
408    my $self = shift @_;
409    my ($file) = @_;
410
411    my $filename = &util::filename_cat($self->{'output_dir'},$file);
412
413    my $status = undef;
414
415    if (open(FOUT,">$filename")) {
416    binmode(FOUT,":utf8");
417    $self->writeHeaderSection();
418    $self->writeItemsSection();
419    $self->writeLinesSection();
420    $self->writeConstraintsSection();
421    $self->writeStatisticsSection();
422
423    close(FOUT);
424    $status = 1;
425    }
426    else {
427    print STDERR "ExpediteeFrameIO::saveFrame() Failed to open $filename for output\n";
428    $status = 0;
429    }
430
431    return $status;
432}
433
434sub buildFrame
435{
436    my $self = shift @_;
437    my ($html_node) = @_;
438 
439    my $type = $html_node->{'type'};
440
441    if ($type eq "rect") {
442
443    my $rect = $html_node->{'rect'};
444    my $xl = $rect->{'xl'};
445    my $xr = $rect->{'xr'};
446    my $yt = $rect->{'yt'};
447    my $yb = $rect->{'yb'};
448
449    my $attr = convertStyleToAttr($html_node->{'style'});
450
451    $self->addRect($xl,$yt,$xr,$yb,$attr);
452
453    if (defined $html_node->{'img'}) {
454
455        my $img_url = $html_node->{'img'};
456        $img_url =~ s/^http:\/\/(.*?)\/greenstone3(.*?)\///;
457        if ($img_url =~ m/^interfaces\//) {
458        $img_url = "images/$img_url";
459        }
460        elsif ($img_url =~ m/^sites\//) {
461        $img_url =~ s/^sites\/(.*?)\//images\//;
462        }
463
464        my $x = $xl;
465        my $y = $yt;
466
467        my $attr = {};
468
469        my $img_text = "\@i: $img_url";
470
471        $self->addText($x,$y,$img_text,undef,$attr);
472    }
473
474    }
475    elsif ($type eq "text") {
476    my $text = $html_node->{'text'};
477
478    my $x = $html_node->{'xl'};
479    my $y = $html_node->{'yt'};
480    my $w = $html_node->{'xr'} - $x +1;
481
482    my $attr = convertStyleToAttr($html_node->{'style'});
483
484    # fudge factor for now (based on default font size used)
485    $y += 16; # y-value of text item in Expeditee is it's base line
486    $x += 4;
487
488    $self->addText($x,$y,$text,$w,$attr);
489    }
490    else {
491    print STDERR "ExpediteeFrameIO::buildFrame(): Warning, unrecognized type '$type'\n";
492    }
493
494    my $childNodes = $html_node->{'childNodes'};
495    foreach my $child_node (@$childNodes) {
496    $self->buildFrame($child_node);
497    }
498}
499
500
501sub saveLastFrameNumber
502{
503    my $self = shift @_;
504    my ($last_frame_number) = @_;
505
506    my $filename = &util::filename_cat($self->{'output_dir'},"frame.inf");
507
508    my $status = undef;
509
510    if (open(FNOUT,">$filename")) {
511    binmode(FNOUT,":utf8");
512   
513    print FNOUT "$last_frame_number\n";
514
515    close(FNOUT);
516    $status = 1;
517    }
518    else {
519    print STDERR "ExpediteeFrameIO::saveLastFrameNumber() Failed to open $filename for output\n";
520    $status = 0;
521    }
522
523    return $status;
524
525}
526
5271;
Note: See TracBrowser for help on using the browser.