From cd83c4b79bc49bd45c1c58e7b8dcec785bb9222c Mon Sep 17 00:00:00 2001 From: cos Date: Sun, 3 Dec 2017 17:08:02 +0100 Subject: Allow some unicode and tracking of nutritients. --- energyadder.pl | 25 ++++++++-- mat | 141 +++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 108 insertions(+), 58 deletions(-) diff --git a/energyadder.pl b/energyadder.pl index 730a953..e91a4b4 100755 --- a/energyadder.pl +++ b/energyadder.pl @@ -66,6 +66,12 @@ print "Goal energy: ", GOAL_ENERGY, " kJ\n\n"; my $energy = 0; +my $proteins = 0; +my $carbs = 0; +my $fats = 0; + +my $total_ingredient_weight = 0; + for my $ingredient ( @ingredients ) { my $sql = "SELECT * FROM LivsmedelsDatabas WHERE ". @@ -74,15 +80,24 @@ for my $ingredient ( @ingredients ) { if ($row->{'Livsmedelsnamn'}) { # print $row->{'Livsmedelsnamn'}; $energy += $ingredient->{quantity} * $row->{'Energi'} / 100; + + $proteins += $ingredient->{quantity} * $row->{'Protein'}; + $carbs += $ingredient->{quantity} * $row->{'Kolhydrater'}; + $fats += $ingredient->{quantity} * $row->{'Fett'}; + + $total_ingredient_weight += $ingredient->{quantity}; } else { $sql = "SELECT * FROM ingredients WHERE id=$ingredient->{ingredient}"; ( $row ) = $db->selectrow_hashref($sql); print "Could not find ingredient $ingredient->{ingredient} ". "($row->{'name'}) in LivsmedelsDatabas.\n"; } - printf "%5d%5d g|%-40s%10d kJ\n", $ingredient->{ingredient}, + printf "%5d%5d g|%-40s%10d kJ (P: %5.2f C: %5.2f F: %5.2f)\n", $ingredient->{ingredient}, $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, - $ingredient->{quantity} * $row->{'Energi'} / 100; + $ingredient->{quantity} * $row->{'Energi'} / 100, + $row->{Protein}, + $row->{Kolhydrater}, + $row->{Fett}; # printf "%5d|%5d g|%-50s%10s kJ|%10s kJ\n", $ingredient->{ingredient}, # $ingredient->{quantity}, $row->{'Livsmedelsnamn'}, # $ingredient->{quantity} * $row->{'Energi'} / 100, $energy; @@ -96,6 +111,10 @@ my $specific_energy = $energy / $total_food_weight * 100; # kJ/100 g #my $reduced = $left / 2.980; +$proteins /= $total_ingredient_weight; +$carbs /= $total_ingredient_weight; +$fats /= $total_ingredient_weight; + print "\n"; printf "Total energy (all %d g): %d kJ\n", $total_food_weight, $energy; printf "Specific energi: %d kJ/100 g\n", $specific_energy; @@ -112,7 +131,7 @@ print $portions, " matlådor på ", int($total_food_weight/$portions), " g ger " print "\n"; my $database_path = (split(":", $Config{'database'}))[2]; -add_to_cooking($recipe_id, $specific_energy, $database_path); +add_to_cooking($recipe_id, $database_path, $specific_energy, $proteins, $carbs, $fats); print "echo \"INSERT INTO cookings VALUES ($recipe_id, '". DateTime->now()->ymd()."', ".int($specific_energy+0.5)."); \" | sqlite3 ". $database_path."\n"; diff --git a/mat b/mat index 451c9b0..65388b6 100755 --- a/mat +++ b/mat @@ -38,7 +38,7 @@ sub read_ingredients { @ingredients = (); my $all = $db->selectall_hashref("SELECT * FROM ingredients", 'id'); - foreach my $id (keys($all)) { + foreach my $id (keys(%{$all})) { $ingredients[$id] = $all->{$id}; } } @@ -381,9 +381,11 @@ sub cmd_postpone { } sub print_label { - my ( $id, $dish_name, $amount, $preparation_date, $energy ) = @_; + my ( $id, $dish_name, $amount, $preparation_date, $energy, + $proteins, $carbs, $fats ) = @_; - my $fontname = "/usr/share/fonts/truetype/freefont/FreeSans.ttf"; + my $western_font = "/usr/share/fonts/truetype/freefont/FreeSans.ttf"; + my $fallback_font = "/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf"; my $fontsize = 15; my $x_border = 18; # Obtained with papersize.sh my $y_border = 35; @@ -395,23 +397,50 @@ sub print_label { my $qr_border = 4; my $iconv = Text::Iconv->new("UTF-8", "ISO8859-1"); - my $latin1_dish = $iconv->convert($dish_name); - my $row0 = $Config{'label_name'}." ".$preparation_date; - my $row1; - my $row2; - if( length($latin1_dish) < 15) { - $row1 = $latin1_dish; - $row2 = "(".$amount."g)"; + my @texts; + $texts[0]->{value} = $Config{'label_name'}." ".$preparation_date; + $texts[0]->{encoding} = "latin1"; + push @texts, { value => "\n", encoding=>'none' }; + #my $row0 = $Config{'label_name'}." ".$preparation_date; + #my $row1; + #my $row2; + + for ( split(/ /, $dish_name )) { + if ( $iconv->convert($_) ) { + push @texts, { value => $_, encoding=>'latin1' }; + } else { + push @texts, { value => $_, encoding=>'utf8' }; + } + } + + if ( $amount ) { + push @texts, { value => "(".$amount."g)", encoding => "latin1" }; + } + if ( $energy ) { + push @texts, { + value => $energy.' kJ ('.int($energy * 100 / $amount).' kJ/hg)', + encoding=>'latin1' }; + } else { + push @texts, { value => '', encoding=>'latin1' }; + } + + push @texts, { value => "\n", encoding=>'none' }; + if ( $proteins ) { + push @texts, { value => sprintf('P: %.0f%%', $proteins), + encoding=>'latin1' }; + } else { + push @texts, { value => 'P: ?', encoding=>'latin1' }; + } + if ( $carbs ) { + push @texts, { value => sprintf('C: %.0f%%', $carbs), encoding=>'latin1' }; } else { - $row1 = $latin1_dish; - $row1 =~ s/(.{15,23}) .*/$1/; - $row2 = substr($latin1_dish, (length $row1) + 1).' ('.$amount.'g)'; + push @texts, { value => 'C: ?', encoding=>'latin1' }; } - my $row3; - if ($energy) { - $row3 = $energy.' kJ ('.int($energy * 100 / $amount).' kJ/hg)'; + + if ( $fats ) { + push @texts, { value => sprintf('F: %.0f%%', $fats), encoding=>'latin1' }; } else { - $row3 = ''; + push @texts, { value => 'F: ?', encoding=>'latin1' }; } my $idbarcode = GD::Barcode::QRcode->new(sprintf("%s%d", $Config{'label_id_prefix'}, $id), { Version=>3 }); @@ -434,37 +463,6 @@ sub print_label { $idtext->fill(1, 1, $white); $idtext->string(gdGiantFont, 0, 0, $id, $black); - my $text0 = new GD::Image(500, $fontsize * 3); - $white = $text0->colorAllocate(255,255,255); - $black = $text0->colorAllocate(0,0,0); - $text0->fill(1, 1, $white); - my @bounds0 = $text0->stringFT(-1 * $black, $fontname, $fontsize, 0, 0, 2 * $fontsize, $row0); - my $text0_image=$text0->copyRotate90(); - - my $text1 = new GD::Image(500, $fontsize * 3); - $white = $text1->colorAllocate(255,255,255); - $black = $text1->colorAllocate(0,0,0); - $text1->fill(1, 1, $white); - $text1->filledRectangle(0, 0, $text1->width(), $text1->height(), $white); - my @bounds1 = $text1->stringFT(-1 * $black, $fontname, $fontsize, 0, 0, 2 * $fontsize, $row1); - my $text1_image=$text1->copyRotate90(); - - my $text2 = new GD::Image(500, $fontsize * 3); - $white = $text2->colorAllocate(255,255,255); - $black = $text2->colorAllocate(0,0,0); - $text2->fill(1, 1, $white); - $text2->filledRectangle(0, 0, $text2->width(), $text2->height(), $white); - my @bounds2 = $text2->stringFT(-1 * $black, $fontname, $fontsize, 0, 0, 2 * $fontsize, $row2); - my $text2_image=$text2->copyRotate90(); - - my $text3 = new GD::Image(500, $fontsize * 3); - $white = $text3->colorAllocate(255,255,255); - $black = $text3->colorAllocate(0,0,0); - $text3->fill(1, 1, $white); - $text3->filledRectangle(0, 0, $text3->width(), $text3->height(), $white); - my @bounds3 = $text3->stringFT(-1 * $black, $fontname, $fontsize, 0, 0, 2 * $fontsize, $row3); - my $text3_image=$text3->copyRotate90(); - my $label = new GD::Image($x_max, $y_max); $white = $label->colorAllocate(255,255,255); $black = $label->colorAllocate(0,0,0); @@ -472,10 +470,33 @@ sub print_label { $label->copyResized($qr_cropped, 0, 0, 0, 0, $qr_cropped->width() * 5, $qr_cropped->height() * 5, $qr_cropped->width(), $qr_cropped->height()); $label->copyResized($idtext, 0, 0 + $qr_cropped->height() * 5, 0, 0, $idtext->width() * 2, $idtext->height() * 2, $idtext->width(), $idtext->height()); - $label->copyResized($text0_image, $x_max - 2 * $fontsize, 0, 0, 0, $bounds0[1] * 1, $bounds0[2], $bounds0[1], $bounds0[2]); - $label->copyResized($text1_image, $x_max - 4 * $fontsize, 0, 0, 0, $bounds1[1] * 1, $bounds1[2], $bounds1[1], $bounds1[2]); - $label->copyResized($text2_image, $x_max - 6 * $fontsize, 0, 0, 0, $bounds2[1] * 1, $bounds2[2], $bounds2[1], $bounds2[2]); - $label->copyResized($text3_image, $x_max - 8 * $fontsize, 0, 0, 0, $bounds3[1] * 1, $bounds3[2], $bounds3[1], $bounds3[2]); + + my %text_pos = ( x => 275, y => 0 ); + for ( @texts ) { + if ( $_->{value} eq "\n" ) { + $text_pos{y} = 0; + $text_pos{x} -= $fontsize * 1.6; + } else { + my $text0 = new GD::Image(500, $fontsize * 3); + $white = $text0->colorAllocate(255,255,255); + $black = $text0->colorAllocate(0,0,0); + $text0->fill(1, 1, $white); + my @bounds0; + if ( $_->{encoding} eq "latin1" ) { + @bounds0 = $text0->stringFT(-1 * $black, $western_font, $fontsize, 0, 0, 2 * $fontsize, $_->{value}); + } else { + @bounds0 = $text0->stringFT(-1 * $black, $fallback_font, $fontsize, 0, 0, 2 * $fontsize, $_->{value}); + } + + my $text0_image=$text0->copyRotate90(); + if ( ($text_pos{y} + $bounds0[2]) > 275 ) { + $text_pos{y} = 0; + $text_pos{x} -= $fontsize * 1.6; + } + $label->copyResized($text0_image, $text_pos{x}, $text_pos{y}, 0, 0, $bounds0[1] * 1, $bounds0[2], $bounds0[1], $bounds0[2]); + $text_pos{y} += $bounds0[2] + 5; + } + } if ($ENV{'PRINT_WITH_LOGO'}) { my ($logo_w, $logo_h ) = $logo->getBounds(); @@ -499,11 +520,15 @@ sub cmd_storeportion { my ( $recipe_id, $amount, $storage ) = @_; my $energy; + my ( $proteins, $carbs, $fats ); my $sql = "SELECT * FROM cookings WHERE recipe_id=$recipe_id AND ". "julianday('now')-julianday(date) < 2;"; my $row = $db->selectrow_hashref($sql); if ($row) { $energy = int($amount * $row->{'specific_energy'} / 100); + $proteins = $row->{'proteins'}; + $carbs = $row->{'carbs'}; + $fats = $row->{'fats'}; } $sql = "INSERT INTO inventory (recipe_id, preparation_date, amount, ". @@ -513,7 +538,7 @@ sub cmd_storeportion { $db->do($sql); my $inventory_id = $db->last_insert_id(undef, undef, undef, undef); print_label($inventory_id, get_recipe_name($recipe_id), $amount, - `date +%Y%m%d|tr -d '\n'`, $energy); + `date +%Y%m%d|tr -d '\n'`, $energy, $proteins, $carbs, $fats); } @@ -524,8 +549,14 @@ sub cmd_reprintlabel { "inventory WHERE id=$id"; my $row = $db->selectrow_hashref($sql); + + $sql = "SELECT proteins, carbs, fats FROM cookings WHERE recipe_id=? AND date=?;"; + my $sql_date_format = $row->{preparation_date}; + $sql_date_format =~ s/(....)(..)(..)/$1-$2-$3/; + my $cookings = $db->selectrow_hashref($sql, undef, $row->{recipe_id}, $sql_date_format); print_label($id, get_recipe_name($row->{'recipe_id'}), $row->{'amount'}, - $row->{'preparation_date'}, $row->{'energy'}); + $row->{'preparation_date'}, $row->{'energy'}, $cookings->{'proteins'}, + $cookings->{'carbs'}, $cookings->{'fats'}); return 0; } @@ -817,7 +848,7 @@ sub cmd_showrecipe { my $cooking_comments = $db->selectall_hashref("SELECT date, comment FROM cookings WHERE recipe_id=".$recipe_id." AND comment IS NOT NULL;", 'date'); my $eating_comments = $db->selectall_hashref("SELECT date, comment FROM eatings AS e JOIN inventory AS i ON e.inventory_id=i.id WHERE recipe_id=".$recipe_id." AND comment IS NOT NULL;", 'date'); - if ( keys($cooking_comments) or keys($eating_comments) ) { + if ( keys(%{$cooking_comments}) or keys(%{$eating_comments}) ) { print "\n"; } for my $date ( keys(%$cooking_comments) ) { -- cgit v1.2.3