Graille/Font.pm untested

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