From 9032bac276238b844fdd0d4ccd01691dc9f587a1 Mon Sep 17 00:00:00 2001 From: David Cook Date: Wed, 6 May 2020 03:23:44 +0000 Subject: [PATCH] Bug 25384: Use koha-conf.xml to configure label maker font list This patch fetches the "ttf" element in koha-conf.xml, and looks for entries with a "name" attribute. If it's an existing default font, it will rename that font. If it's a new font, it will add it to the font list in the label creator pages. If you don't change anything in koha-conf.xml, then there is no behaviour change and the existing unit tests will all complete as normal. Test plan: 1) Apply patch 2) Edit koha-conf.xml and add a "name" attribute of "Times New Awesome" next to the font "type" "TR". 3) Edit koha-conf.xml and add the following entry to the "ttf" element: /usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf 4) Clear memcached (if using memcached) a. e.g. echo 'flush_all' | nc -q 1 memcached 11211 5) Restart Plack (if using Plack) a. e.g. koha-plack --restart kohadev 6) Create or edit a Label maker layout a. e.g. /cgi-bin/koha/labels/label-edit-layout.pl?op=edit&element_id=17 7) Note that "Times-Roman" now says "Times New Awesome" instead 8) Note that there is now an "Arial" option at the bottom of the list 9) Try to export a label batch using a layout using "Arial" 10) Note that the text is output with a font style similar to Arial --- C4/Creators/Lib.pm | 89 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 15 deletions(-) diff --git a/C4/Creators/Lib.pm b/C4/Creators/Lib.pm index c16a2c81a6..09a488a54a 100644 --- a/C4/Creators/Lib.pm +++ b/C4/Creators/Lib.pm @@ -121,20 +121,35 @@ my $label_types = [ {type => 'BAR', name => 'Barcode', desc => 'Only the barcode is printed.', selected => 0}, ]; -my $font_types = [ - {type => 'TR', name => 'Times-Roman', selected => 0}, - {type => 'TB', name => 'Times-Bold', selected => 0}, - {type => 'TI', name => 'Times-Italic', selected => 0}, - {type => 'TBI', name => 'Times-Bold-Italic', selected => 0}, - {type => 'C', name => 'Courier', selected => 0}, - {type => 'CB', name => 'Courier-Bold', selected => 0}, - {type => 'CO', name => 'Courier-Oblique', selected => 0}, - {type => 'CBO', name => 'Courier-Bold-Oblique', selected => 0}, - {type => 'H', name => 'Helvetica', selected => 0}, - {type => 'HO', name => 'Helvetica-Oblique', selected => 0}, - {type => 'HB', name => 'Helvetica-Bold', selected => 0}, - {type => 'HBO', name => 'Helvetica-Bold-Oblique', selected => 0}, -]; +#NOTE: this is needed to maintain current font ordering +my @default_font_type_codes = ( + "TR", + "TB", + "TI", + "TBI", + "C", + "CB", + "CO", + "CBO", + "H", + "HO", + "HB", + "HBO" +); +my %default_font_types = ( + TR => {name => 'Times-Roman', }, + TB => {name => 'Times-Bold', }, + TI => {name => 'Times-Italic', }, + TBI => {name => 'Times-Bold-Italic', }, + C => {name => 'Courier', }, + CB => {name => 'Courier-Bold', }, + CO => {name => 'Courier-Oblique', }, + CBO => {name => 'Courier-Bold-Oblique', }, + H => {name => 'Helvetica', }, + HO => {name => 'Helvetica-Oblique', }, + HB => {name => 'Helvetica-Bold', }, + HBO => {name => 'Helvetica-Bold-Oblique', }, +); my $text_justification_types = [ {type => 'L', name => 'Left', selected => 0}, @@ -429,7 +444,51 @@ This function returns a reference to an array of hashes containing all font type =cut sub get_font_types { - return $font_types; + my @available_fonts = (); + my %available_font_lookup = %default_font_types; + + #Add new fonts or rename default fonts + my $ttf = C4::Context->config('ttf'); + if ($ttf && $ttf->{font} && ref $ttf->{font} eq 'ARRAY'){ + foreach my $font ( @{$ttf->{font}} ){ + if ($font->{type} && $font->{name} && $font->{content}){ + $available_font_lookup{ $font->{type} } = { name => $font->{name}, }; + } + } + } + + #Output default font types first (in default order) + _use_font({ + font_types => \@default_font_type_codes, + font_lookup => \%available_font_lookup, + available_fonts => \@available_fonts, + }); + + #Output configured font types last (in alphabetical order) + my @remaining_types = sort keys %available_font_lookup; + _use_font({ + font_types => \@remaining_types, + font_lookup => \%available_font_lookup, + available_fonts => \@available_fonts, + }); + + return \@available_fonts; +} + +sub _use_font { + my ($args) = @_; + my $font_types = $args->{font_types}; + my $font_lookup = $args->{font_lookup}; + my $available_fonts = $args->{available_fonts}; + if ($font_types && $font_lookup && $available_fonts){ + foreach my $font_type (@$font_types){ + my $font = delete $font_lookup->{$font_type}; + $font->{type} = $font_type; + #FIXME: in future it would be good to remove this selected requirement, but needs template changes + $font->{selected} = 0; + push(@$available_fonts,$font); + } + } } =head2 C4::Creators::Lib::get_text_justification_types() -- 2.11.0