forked from Firedrake/dymo-labelmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2img
executable file
·99 lines (85 loc) · 2.1 KB
/
txt2img
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/perl -w
use strict;
use Getopt::Std;
use Imager;
use List::Util qw(sum max);
use feature 'unicode_strings';
my %o=(
f => '/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
h => 64,
a => 'c',
g => 0,
o => 'txt2img.png',
);
my $testsize=256;
getopt('fhago',\%o);
my @lines=@ARGV;
my @m;
{
my $fn=Imager::Font->new(file => $o{f},
size => $testsize);
foreach (@lines) {
if (length $_ < 1) {
$_ = "|--|"; # Spacer
}
my @metrics=$fn->bounding_box(string => $_);
push @m,$metrics[5]-$metrics[4];
my $tv = $metrics[5]-$metrics[4];
}
}
my %col=(
white => Imager::Color->new(255,255,255),
black => Imager::Color->new(0,0,0)
);
my $targetsize=$testsize/sum(@m)*($o{h}-((scalar @m)-1)*$o{g});
my @i;
my $fn;
$fn=Imager::Font->new(file => $o{f},
size => $targetsize);
my $blankCount = 0;
my $lineCount = 0;
foreach (@lines) {
++$lineCount;
my @metrics;
@metrics=$fn->bounding_box(string => $_);
if (length $_ < 1) {
@metrics = $fn->bounding_box(string => "")
}
my $i=Imager->new(xsize => $metrics[2]-$metrics[0],
ysize => $metrics[5]-$metrics[4]);
$i->box(color => $col{white}, filled => 1);
if ($_ eq "|--|") {
$_ = " ";
++$blankCount
}
$i->string(align => 0,
x => 0,
y => 0,
string => $_,
color => $col{black},
font => $fn,
utf8 => 1);
push @i,$i;
}
my $out=Imager->new(xsize => max(map {$_->getwidth} @i),
ysize => $o{h});
$out->box(color => $col{white},filled => 1);
my $y=0;
print "BC = $blankCount, LC = $lineCount";
if (($blankCount == 1) && ($lineCount == 2)) {
$y = 18;
}
if (($blankCount == 2) && ($lineCount == 3)) {
$y = 22;
}
if (($blankCount == 1) && ($lineCount == 3)) {
$y = 11;
}
foreach (@i) {
$out->paste(left => ($o{a} eq 'c')?($out->getwidth-$_->getwidth)/2:
($o{a} eq 'r')?($out->getwidth-$_->getwidth):0,
top => $y,
src => $_);
$y+=$_->getheight+$o{g};
}
$out->write(file => $o{o});