Using hashes instead of names in fileformat fonts
to easier manipulation.
This commit is contained in:
parent
bb653e91ed
commit
08ff96cdc6
@ -14,6 +14,7 @@ use Net::DBus;
|
||||
has path => ( is => 'ro', required => 1 );
|
||||
has current_image => ( is => 'rw', );
|
||||
has _gatt => ( is => 'lazy' );
|
||||
has _device => ( is => 'lazy' );
|
||||
|
||||
sub image( $self, $image ) {
|
||||
my $current_image = $self->current_image;
|
||||
@ -40,21 +41,42 @@ sub serialize($self) {
|
||||
return $hash;
|
||||
}
|
||||
|
||||
sub _build__device($self) {
|
||||
my $session = Net::DBus->system;
|
||||
my $service = $session->get_service('org.bluez');
|
||||
my $device_path = $self->path =~ s{/service.*$}{}r;
|
||||
return $service->get_object( $device_path, 'org.bluez.Device1' );
|
||||
}
|
||||
|
||||
sub _build__gatt($self) {
|
||||
my $session = Net::DBus->system;
|
||||
my $service = $session->get_service('org.bluez');
|
||||
my $gatt_characteristic_1 =
|
||||
$service->get_object( $self->path, 'org.bluez.GattCharacteristic1' );
|
||||
return $service->get_object( $self->path, 'org.bluez.GattCharacteristic1' );
|
||||
}
|
||||
|
||||
sub try_to_connect($self) {
|
||||
my $device_api = $self->_device;
|
||||
say $device_api->Connected;
|
||||
return if $device_api->Connected;
|
||||
$device_api->Connect;
|
||||
|
||||
for (my $i = 0; $i < 3; $i++) {
|
||||
sleep 1;
|
||||
return if $device_api->Connected;
|
||||
}
|
||||
die 'Unable to connect to Cat Printer';
|
||||
}
|
||||
|
||||
sub print($self) {
|
||||
my $gatt_characteristic_1 = $self->_gatt;
|
||||
my $tempdir = Path::Tiny->tempdir;
|
||||
my $in = $tempdir->child('in.png');
|
||||
my $out = $tempdir->child('out.bmp');
|
||||
$self->try_to_connect;
|
||||
my $tempdir = Path::Tiny->tempdir;
|
||||
my $in = $tempdir->child('in.png');
|
||||
my $out = $tempdir->child('out.bmp');
|
||||
$self->current_image->_file( '' . $in );
|
||||
die 'Couldn\'t invert image'
|
||||
if system qw/convert -flop -rotate 180/, $in, qw/-negate -monochrome/, $out;
|
||||
if system qw/convert -flop -rotate 180/, $in, qw/-negate -monochrome/,
|
||||
$out;
|
||||
open my $fh, '<', $out;
|
||||
$self->_send_command(
|
||||
$self->_bytestring(
|
||||
@ -63,10 +85,7 @@ sub print($self) {
|
||||
);
|
||||
$self->_send_command(
|
||||
$self->_make_command( 0xa4, $self->_bytestring(50) ) );
|
||||
$self->_send_command(
|
||||
$self->_make_command( 0xa4, $self->_bytestring(50) ) );
|
||||
$self->_send_command(
|
||||
$self->_make_command( 0xbd, $self->_bytestring(40) ) );
|
||||
$self->_send_command( $self->_make_command( 0xbd, $self->_bytestring(5) ) );
|
||||
$self->_send_command(
|
||||
$self->_make_command( 0xaf, $self->_bytestring( 0x00, 0x40 ) ) );
|
||||
$self->_send_command(
|
||||
@ -78,6 +97,7 @@ sub print($self) {
|
||||
$buffer .=
|
||||
$self->_bytestring( 0xaa, 0x55, 0x17, 0x38, 0x44, 0x5f, 0x5f, 0x5f, 0x44,
|
||||
0x38, 0x2c );
|
||||
$buffer .= $self->_make_command( 0xa0, $self->_bytestring( 255, 0x00 ) );
|
||||
|
||||
seek $fh, 0xa, 0;
|
||||
read $fh, my $offset, 4;
|
||||
@ -98,7 +118,7 @@ sub print($self) {
|
||||
$self->_flush( \$buffer, $mtu );
|
||||
}
|
||||
|
||||
sub _reverse_byte($self, $i) {
|
||||
sub _reverse_byte( $self, $i ) {
|
||||
$i = ( ( $i & 0b10101010 ) >> 1 ) | ( ( $i & 0b01010101 ) << 1 );
|
||||
$i = ( ( $i & 0b11001100 ) >> 2 ) | ( ( $i & 0b00110011 ) << 2 );
|
||||
return ( ( $i & 0b11110000 ) >> 4 ) | ( ( $i & 0b00001111 ) << 4 );
|
||||
|
@ -19,6 +19,7 @@ use Exd::FileFormat::Fontconfig;
|
||||
|
||||
has dir => ( is => 'rw' );
|
||||
has _fontconfig => (is => 'lazy');
|
||||
has dbh => (is => 'lazy');
|
||||
|
||||
sub _build__fontconfig($self) {
|
||||
return Exd::FileFormat::Fontconfig->new(exd => $self);
|
||||
@ -61,7 +62,7 @@ sub _image_hashes($self) {
|
||||
sub image_hashes_to_label($self) {
|
||||
my %images;
|
||||
for my $image_hash ( $self->_image_hashes->@* ) {
|
||||
my $dbh = $self->_dbh;
|
||||
my $dbh = $self->dbh;
|
||||
my $hashes = $dbh->selectall_arrayref(
|
||||
'SELECT label FROM label_to_image_hash WHERE hash = ?',
|
||||
{Slice => {}}, $image_hash );
|
||||
@ -70,12 +71,12 @@ sub image_hashes_to_label($self) {
|
||||
return \%images;
|
||||
}
|
||||
|
||||
sub _dbh($self) {
|
||||
sub _build_dbh($self) {
|
||||
return Exd::FileFormat::DB->connect( $self );
|
||||
}
|
||||
|
||||
sub _register_label( $self, $label, $sha_image ) {
|
||||
my $dbh = $self->_dbh;
|
||||
my $dbh = $self->dbh;
|
||||
eval {
|
||||
$dbh->do(
|
||||
'INSERT INTO label_to_image_hash (label, hash) VALUES (?, ?);',
|
||||
@ -91,7 +92,7 @@ sub get_image_gd_from_label ( $self, $label ) {
|
||||
}
|
||||
|
||||
sub _get_hash_from_label($self, $label) {
|
||||
my $dbh = $self->_dbh;
|
||||
my $dbh = $self->dbh;
|
||||
my $row = $dbh->selectrow_hashref(
|
||||
'SELECT hash FROM label_to_image_hash WHERE label = ?',
|
||||
{}, $label );
|
||||
@ -141,7 +142,7 @@ sub from_zip_file( $class, $zip_file ) {
|
||||
|
||||
sub execute( $self, $printer ) {
|
||||
my $script = $self->get_script;
|
||||
$self->_dbh;
|
||||
$self->dbh;
|
||||
my $sub =
|
||||
eval
|
||||
'use v5.40.0; use strict; use warnings; use utf8; no Carp::Always; use Cairo; use Pango;'
|
||||
|
@ -22,6 +22,14 @@ sub MIGRATIONS {
|
||||
sub ($exd, $dbh) {
|
||||
my $fontconfig = Exd::FileFormat::Fontconfig->new(exd => $exd);
|
||||
$fontconfig->migration1;
|
||||
},
|
||||
'CREATE TABLE font_hash_to_name (
|
||||
hash TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);',
|
||||
sub ($exd, $dbh) {
|
||||
my $fontconfig = Exd::FileFormat::Fontconfig->new(exd => $exd);
|
||||
$fontconfig->migration2;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ use v5.40.0;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Digest::SHA qw/sha256_hex/;
|
||||
use Moo;
|
||||
use Path::Tiny;
|
||||
|
||||
@ -12,13 +13,17 @@ use Data::Dumper;
|
||||
|
||||
use Inline C => DATA => LIBS => '-lfontconfig -lfreetype';
|
||||
|
||||
has exd => (is => 'ro', required => 1, weak_ref => 1);
|
||||
has exd => ( is => 'ro', required => 1, weak_ref => 1 );
|
||||
|
||||
sub set_current($self) {
|
||||
my $exd_font_dir = $self->_font_dir;
|
||||
my $dir = $exd_font_dir->child('dir');
|
||||
my $config = $exd_font_dir->child('config');
|
||||
$self->_set_current_c(''.$dir);
|
||||
my $dir = $exd_font_dir->child('dir');
|
||||
my $config = $exd_font_dir->child('config');
|
||||
$self->_set_current_c( '' . $dir );
|
||||
}
|
||||
|
||||
sub restore_system_config($self) {
|
||||
$self->_restore_system_config_c;
|
||||
}
|
||||
|
||||
sub list_fonts($self) {
|
||||
@ -31,17 +36,48 @@ sub _font_dir($self) {
|
||||
|
||||
sub migration1($self) {
|
||||
my $exd = $self->exd;
|
||||
my $app_font_dir = path(__FILE__)->parent->parent->parent->parent->child('fonts/dir');
|
||||
my $app_font_dir =
|
||||
path(__FILE__)->parent->parent->parent->parent->child('fonts/dir');
|
||||
my $exd_font_dir = $self->_font_dir->child('dir');
|
||||
$exd_font_dir->mkpath;
|
||||
$app_font_dir->visit(sub ($path, $state) {
|
||||
return if !-f $path;
|
||||
my $new_file = $exd_font_dir->child($path->relative($app_font_dir).'');
|
||||
$new_file->parent->mkpath;
|
||||
system 'cp', '-v', $path, $new_file;
|
||||
});
|
||||
# $self->set_current;
|
||||
# print Data::Dumper::Dumper $self->list_fonts;
|
||||
$app_font_dir->visit(
|
||||
sub ( $path, $state ) {
|
||||
return if !-f $path;
|
||||
my $new_file =
|
||||
$exd_font_dir->child( $path->relative($app_font_dir) . '' );
|
||||
$new_file->parent->mkpath;
|
||||
system 'cp', '-v', $path, $new_file;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
sub migration2($self) {
|
||||
my $exd = $self->exd;
|
||||
my $exd_font_dir = $self->_font_dir->child('dir');
|
||||
my @font_files;
|
||||
|
||||
$exd_font_dir->visit(
|
||||
sub ( $path, $state ) {
|
||||
return if !-f $path;
|
||||
push @font_files, $path;
|
||||
},
|
||||
{ recurse => 1 }
|
||||
);
|
||||
|
||||
for my $font_file (@font_files) {
|
||||
my $sha = sha256_hex $font_file->slurp_raw;
|
||||
system 'cp', '-v', $font_file, $exd_font_dir->child($sha);
|
||||
system 'rm', $font_file;
|
||||
my $name = $font_file->basename;
|
||||
$name =~ s/\..*?$//;
|
||||
$self->_add_font_to_database($name, $sha);
|
||||
}
|
||||
}
|
||||
|
||||
sub _add_font_to_database($self, $font_name, $hash) {
|
||||
my $dbh = $self->exd->dbh;
|
||||
$dbh->do('INSERT INTO font_hash_to_name (hash, name) VALUES (?, ?);', {},
|
||||
$hash, $font_name);
|
||||
}
|
||||
1;
|
||||
__DATA__
|
||||
@ -49,8 +85,20 @@ __C__
|
||||
#include <stdio.h>
|
||||
#include <fontconfig/fontconfig.h>
|
||||
|
||||
FcConfig *old_system_config = NULL;
|
||||
|
||||
void
|
||||
_restore_system_config_c(SV *self) {
|
||||
if (old_system_config != NULL) {
|
||||
FcConfigSetCurrent(old_system_config);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_set_current_c(SV *self, char *font_dir) {
|
||||
if (old_system_config == NULL) {
|
||||
old_system_config = FcConfigGetCurrent();
|
||||
}
|
||||
FcConfig *new_config = FcConfigCreate();
|
||||
FcConfigAppFontAddDir(new_config, font_dir);
|
||||
FcConfigBuildFonts(new_config);
|
||||
|
Loading…
Reference in New Issue
Block a user