From 1e06ddda9937d57ff2993f523de6899f3136c600 Mon Sep 17 00:00:00 2001 From: borisneubert <> Date: Thu, 14 Nov 2013 21:09:24 +0000 Subject: [PATCH] feature: new command halign, valign, condition in 02_RSS.pm (Betateilchen & Boris) git-svn-id: https://svn.fhem.de/fhem/trunk@4219 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 + fhem/FHEM/02_RSS.pm | 124 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 107 insertions(+), 19 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index c00e9a49d..52d3d6f54 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,6 +1,8 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. - SVN + - feature: new command halign, valign, condition in 02_RSS.pm + (Betateilchen & Boris) - bugfix: PRESENCE: Fix nonworking initialization in mode "lan-bluetooth" - bugfix: fhem.pl: write-select to avoid blocking in inform/Event Monitor - bugfix: fix issue with DST changes in 57_Calendar.pm diff --git a/fhem/FHEM/02_RSS.pm b/fhem/FHEM/02_RSS.pm index ab8015dd6..bc25d3f08 100644 --- a/fhem/FHEM/02_RSS.pm +++ b/fhem/FHEM/02_RSS.pm @@ -11,8 +11,13 @@ package main; use strict; use warnings; use GD; +use feature qw/switch/; use vars qw(%data); use HttpUtils; +require "98_SVG.pm"; # enable use of plotAsPng() + +my @valid_valign = qw(top center base bottom); +my @valid_halign = qw(left center right); # we can # use vars qw(%FW_types); # device types, @@ -42,7 +47,6 @@ RSS_Initialize($) { $hash->{AttrList}= "size bg tmin"; $hash->{SetFn} = "RSS_Set"; - RSS_addExtension("RSS_CGI","rss","RSS"); return undef; @@ -84,6 +88,12 @@ RSS_Define($$) { $hash->{fhem}{hostname}= $hostname; $hash->{fhem}{filename}= $filename; + eval "use GD::Text::Align"; + $hash->{fhem}{useTextAlign} = ($@ ? 0 : 1 ); + if(!($hash->{fhem}{useTextAlign})) { + Log3 $hash, 1, "Cannot use text alignment: $@"; + } + RSS_readLayout($hash); $hash->{STATE} = $name; @@ -217,8 +227,21 @@ sub RSS_itemText { my ($S,$x,$y,$text,%params)= @_; return unless(defined($text)); - ($x,$y)= RSS_xy($S,$x,$y); - $S->stringFT(RSS_color($S,$params{rgb}),$params{font},$params{pt},0,$x,$y,$text); + + if($params{useTextAlign}) { + ($x,$y)= RSS_xy($S,$x,$y); + my $align = GD::Text::Align->new($S, + color => RSS_color($S, $params{rgb}), + valign => $params{tvalign}, + halign => $params{thalign}, + ); + $align->set_font($params{font}, $params{pt}); + $align->set_text($text); + $align->draw($x, $y, 0); + } else { + ($x,$y)= RSS_xy($S,$x,$y); + $S->stringFT(RSS_color($S,$params{rgb}),$params{font},$params{pt},0,$x,$y,$text); + } } sub @@ -227,6 +250,7 @@ RSS_itemTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); RSS_itemText($S,$x,$y,sprintf("%02d:%02d", $hour, $min),%params); } + sub RSS_itemSeconds { my ($S,$x,$y,$format,%params)= @_; @@ -237,7 +261,7 @@ RSS_itemSeconds { } else { - RSS_itemText($S,$x,$y,sprintf("%02d", $sec),%params); + RSS_itemText($S,$x,$y,sprintf("%02d", $sec),%params); } } sub @@ -276,7 +300,6 @@ RSS_itemImg { return; } } elsif($srctype eq "data") { - require "98_SVG.pm"; # enable use of plotAsPng() if($imgtype eq "gif") { $I= GD::Image->newFromGifData($arg); } elsif($imgtype eq "png") { @@ -290,18 +313,37 @@ RSS_itemImg { return; } ($x,$y)= RSS_xy($S,$x,$y); - my ($width,$height)= $I->getBounds(); - if ($scale =~ s/([wh])([\d]*)/$2/) { # get the digit from width/hight to pixel entry - #Debug "RSS scale $scale (1: $1 / 2: $2)contais px after Digit - width: $width / height: $height"; - if ($1 eq "w") { - $scale=$scale/$width; - } else { - $scale=$scale/$height; + + eval { + my ($width,$height)= $I->getBounds(); + if ($scale =~ s/([wh])([\d]*)/$2/) { # get the digit from width/hight to pixel entry + #Debug "RSS scale $scale (1: $1 / 2: $2)contais px after Digit - width: $width / height: $height"; + if ($1 eq "w") { + $scale=$scale/$width; + } else { + $scale=$scale/$height; + } } + my ($swidth,$sheight)= (int($scale*$width), int($scale*$height)); + + given ($params{ihalign}) { + when('center') { $x -= $swidth/2; } + when('right') { $x -= $swidth; } + default { } # nothing to do + } + given ($params{ivalign}) { + when('center') { $y -= $sheight/2; } + when('base') { $y -= $sheight; } + when('bottom') { $y -= $sheight; } + default { } # nothing to do + } + + #Debug "RSS placing $arg ($swidth x $sheight) at ($x,$y)"; + $S->copyResampled($I,$x,$y,0,0,$swidth,$sheight,$width,$height); + }; + if($@) { + Log3 undef, 2, "RSS: cannot create image $srctype $imgtype '$arg': $@"; } - my ($swidth,$sheight)= (int($scale*$width), int($scale*$height)); - #Debug "RSS placing $arg ($swidth x $sheight) at ($x,$y)"; - $S->copyResampled($I,$x,$y,0,0,$swidth,$sheight,$width,$height); } sub @@ -324,6 +366,16 @@ RSS_evalLayout($$@) { $params{font}= "Arial"; $params{pt}= 12; $params{rgb}= "ffffff"; + $params{halign} = 'left'; + $params{valign} = 'base'; + $params{condition} = '1'; + # we need two pairs of align parameters + # due to different default values for text and img + $params{useTextAlign}= $defs{$name}{fhem}{useTextAlign}; + $params{ihalign} = 'left'; + $params{ivalign} = 'top'; + $params{thalign} = 'left'; + $params{tvalign} = 'base'; my ($x,$y,$x1,$y1,$x2,$y2,$scale,$text,$imgtype,$srctype,$arg,$format); @@ -342,6 +394,8 @@ RSS_evalLayout($$@) { # split line into command and definition my ($cmd, $def)= split("[ \t]+", $line, 2); ##Debug, "CMD= \"$cmd\", DEF= \"$def\""; + $params{condition} = AnalyzePerlCommand(undef, $def) if($cmd eq 'condition'); + next unless($params{condition}); if($cmd eq "rgb") { $def= "\"$def\"" if(length($def) == 6 && $def =~ /[[:xdigit:]]{6}/); $params{rgb}= AnalyzePerlCommand(undef, $def); @@ -349,6 +403,24 @@ RSS_evalLayout($$@) { $params{font}= $def; } elsif($cmd eq "pt") { $params{pt}= $def; + } elsif($cmd eq "halign"){ + my $d = AnalyzePerlCommand(undef, $def); + if($d ~~ @valid_halign) { + #Debug "$name: halign $d found valid in halign_array"; + $params{ihalign}= $d; + $params{thalign}= $d; + } else { + Log3 $name, 2, "$name: Illegal horizontal alignment $d"; + } + } elsif($cmd eq "valign") { + my $d = AnalyzePerlCommand(undef, $def); + if( $d ~~ @valid_valign) { + # Debug "$name: valign $d found valid in valign_array"; + $params{ivalign}= $d; + $params{tvalign}= $d; + } else { + Log3 $name, 2, "$name: Illegal vertical alignment $d"; + } } elsif($cmd eq "text") { ($x,$y,$text)= split("[ \t]+", $def, 3); my $txt= AnalyzePerlCommand(undef, $text); @@ -374,7 +446,6 @@ RSS_evalLayout($$@) { } else { Log3 $name, 1, "$name: Illegal command $cmd in layout definition."; } - } } @@ -627,18 +698,33 @@ RSS_CGI(){ putting a \ at the end.
Layout control commands
+ Notes:
+ (1) Use double quotes to quote literal text if perl specials are allowed.
+ (2) Text alignment requires the Perl module GD::Text::Align to be installed. Debian-based systems can install it with apt-get install libgd-text-perl
.
+
+
Arial
) or the full path to a TrueType font
(e.g. /usr/share/fonts/truetype/arial.ttf
),
whatever works on your system.FF0000
for red, C0C0C0
for light gray). You can use
{ <perl special> }
for <color>.{ <perl special> }
instead of the literal alignment control word.{ <perl special> }
instead of the literal alignment control word.@@ -669,7 +755,7 @@ RSS_CGI(){ This is how a layout definition might look like:
font /usr/share/fonts/truetype/arial.ttf # must be a TrueType font
- rgb c0c0c0 # HTML color notation, RGB
+ rgb "c0c0c0" # HTML color notation, RGB
pt 48 # font size in points
time 0.10 0.90
pt 24