File Coverage

blib/lib/Term/Graille/Font.pm
Criterion Covered Total %
statement 24 66 36.3
branch 0 10 0.0
condition 0 2 0.0
subroutine 8 12 66.6
pod 4 4 100.0
total 36 94 38.3


line stmt bran cond sub pod time code
1             package Term::Graille::Font;
2 1     1   1143 use strict; use warnings;
  1     1   2  
  1         28  
  1         4  
  1         2  
  1         24  
3 1     1   7 use utf8;
  1         1  
  1         5  
4 1     1   706 use Data::Dumper;
  1         6264  
  1         62  
5 1     1   11 use Term::Graille qw/block2braille/;
  1         2  
  1         40  
6 1     1   5 use base 'Exporter';
  1         3  
  1         110  
7             our @EXPORT_OK = qw/convertDG saveGrf loadGrf fontWrite/;
8 1     1   6 use open ":std", ":encoding(UTF-8)";
  1         2  
  1         6  
9              
10             our $VERSION="0.09";
11              
12              
13             =head1 NAME
14              
15             Term::Graille::Font - convert and use ZX Spectrum fonts in Term::Graille
16            
17             =head1 SYNOPSIS
18              
19             use Term::Graille::Font qw/convertDG saveGrf fontWrite/;
20             my $grf=convertDG("$fontName.z80.asm");
21             $canvas->blockBlit($grf->{A},$gridX,$gridY); # old method single character
22             fontWrite($canvas,$grf,$gridX,$gridY,$text); # new method text string
23             saveGrf($grf,"$fontName.grf");
24            
25             =head1 DESCRIPTION
26              
27             This allows the creation, saving and usage of Braille Character to form
28             Bitmap 8x8 font. It currently only accepts DameinGuards z80.asm files
29             as found in damieng.com/typography/zx-origins/ where there a couple of
30             hundred fonts available.
31              
32             =begin html
33              
34            
35              
36             =end html
37              
38              
39             =head1 FUNCTIONS
40              
41             =cut
42              
43             =head3 C
44              
45             Reads an ASM file as on Damien Guard's typography pages and returns a hashref
46             containing the extracted 8x8 fonts converted into 4x2 Braille characters.
47              
48             =cut
49              
50             sub convertDG{
51 0     0 1   my $file=shift;
52 0           my $font="";
53 0 0         open my $zxFont,"<:utf8",$file or die "Unable to open fontfile $file $!;\n";
54 0           $font.=$_ while(<$zxFont>);
55 0           close $zxFont;
56            
57 0           $font=~s/^[\s\t]?;([^\n]+)/{/g;
58 0           my $info=$1;
59 0           $font=~s/defb &/[[0x/g;
60 0           $font=~s/,&/],[0x/g;
61 0           $font=~s/ ; /]],# /g;
62 0           $font=~s/\s([^#\s]*)#\s(.)/ '$2'=>$1/g;
63 0           $font=~s/\'\'\'/\"\'\"/g;
64 0           $font=~s/\'\\\'/\'\\\\\'/g;
65 0           $font.="\n}\n";
66             # print $font;
67 0           my $binFont=eval($font);
68 0           my $grlFont={};
69 0           for (keys %$binFont){
70 1     1   836 use utf8;
  1         2  
  1         15  
71 0           $grlFont->{$_}=block2braille($binFont->{$_}) ;
72             }
73 0   0       $grlFont->{info}=$info||"";
74 0           return $grlFont;
75             }
76              
77             =head3 C
78              
79             Uses Data::Dumper to produce convert a font hashref into a file that can
80             be read using loadGrf
81              
82             =cut
83              
84             sub saveGrf{
85 0     0 1   my ($grf,$file)=@_;
86 0           my $output=Dumper([$grf]);
87 0           $output=~ s/\\x\{([0-9a-f]{2,})\}/chr hex $1/ge;
  0            
88 0           $output=~s/^\$VAR1 = \[\n\s+| \];\n?$//g;
89 0           $output=~s/\[\n\s+\[/\[\[/g;
90 0           $output=~s/\n\s+([^\s])/$1/g;
91 0           $output=~s/\]\],/\]\],\n/g;
92 0 0         die "Conversion failed\n" if (length $output <100);
93 0 0         open my $dat,">:utf8","$file" or die "Unable to save fontfile $file $!;\n";
94 0           print $dat $output;
95 0           close $dat;
96             }
97              
98              
99             =head3 C
100              
101             Loads a font hashref from a file. The hashRef indexes 4x2 braille character
102             blocks by character they represent; This may be used to represent any kind of
103             character block, including tilemaps, sprites etc.
104              
105             =cut
106              
107             #loads a hashfile representing a 2D Array ref blocks of data
108             #can be used to load fonts or other graphical blocks
109             sub loadGrf{
110 0     0 1   my $file=shift;
111 0 0         open my $grf,"<:utf8",$file or
112             die "Unable to open file $file $!;\n" ;
113 0           my $data="";
114 0           $data.=$_ while(<$grf>);
115 0           close $grf;
116 0 0         my $g=eval($data) or die "unable to load external data from $file $!";;
117 0           return $g;
118             }
119              
120              
121             =head3 C<$fontWrite($canvas,$font,$gridX,$gridY,$text);>
122              
123             Writes a C<$text> string using a loaded font into a particular grid
124             location in a C<$canvas>
125              
126             =cut
127              
128             sub fontWrite{
129 0     0 1   my ($canvas,$font,$gridX,$gridY,$text)=@_;
130 0           for my $chPos(0..(length $text)-1){
131 0           $canvas->blockBlit($font->{substr($text,$chPos, 1)},$gridX+4*$chPos, $gridY)
132             }
133             }
134              
135             1;