From b4c7485154f73a1cf6b1fc78013f481c5f790c2d Mon Sep 17 00:00:00 2001 From: dakkar Date: Wed, 22 Dec 2010 15:46:54 +0000 Subject: font choice, and proper vertical rendering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NOTE: hinting:none and antialias:full is needed to get Japanese displayed correctly… bug? --- lib/GridFiller/Result/Pango.pm | 45 ++++++++++++++++++++++++++++++++++++++++++ lib/GridFiller/Types.pm | 15 ++++++++++++++ script/qr-color.pl | 1 + 3 files changed, 61 insertions(+) diff --git a/lib/GridFiller/Result/Pango.pm b/lib/GridFiller/Result/Pango.pm index 28c697b..a1df972 100644 --- a/lib/GridFiller/Result/Pango.pm +++ b/lib/GridFiller/Result/Pango.pm @@ -3,6 +3,7 @@ use Moose; use namespace::autoclean; use GridFiller::Constants ':directions'; use MooseX::Types::Moose qw(Int); +use GridFiller::Types qw(FontDescrT FontOptT); use Cairo; use Pango; use Carp; @@ -77,6 +78,44 @@ sub _build__cairo_c { return $cr; } +has font_description => ( + is => 'ro', + isa => FontDescrT, + coerce => 1, + lazy_build => 1, + trigger => \&_adjust_font_description, +); + +sub _build_font_description { + my ($self)=@_; + + my $fd = Pango::FontDescription->from_string('Sans 10'); + return $self->_adjust_font_description($fd); +} + +sub _adjust_font_description { + my ($self,$fd) = @_; + + $fd->set_absolute_size($self->cell_size * Pango->scale * 0.8); + + return $fd; +} + +has font_options => ( + is => 'ro', + isa => FontOptT, + lazy_build => 1, +); + +sub _build_font_options { + my ($self) = @_; + + my $fo = Cairo::FontOptions->create(); + $fo->set_hint_style('none'); + + return $fo; +} + sub _put_squares { my ($self,$cr) = @_; @@ -113,6 +152,10 @@ sub layout_for_string { my ($self,$string) = @_; my $p = Pango::Cairo::create_layout($self->_cairo_c); + Pango::Cairo::Context::set_font_options($p->get_context,$self->font_options); + Pango::Cairo::update_context($self->_cairo_c,,$p->get_context); + $p->context_changed; + $p->set_font_description($self->font_description); $p->set_text($string); return $p; @@ -187,6 +230,8 @@ sub place_word_at { $cr->move_to($x,$y); } else { + $p->get_context->set_base_gravity('east'); + $p->context_changed; $y+=$adjustment; $cr->move_to($x+$size,$y); $cr->rotate($PI/2); diff --git a/lib/GridFiller/Types.pm b/lib/GridFiller/Types.pm index 92834ba..90e094f 100644 --- a/lib/GridFiller/Types.pm +++ b/lib/GridFiller/Types.pm @@ -1,9 +1,13 @@ package GridFiller::Types; +use strict; +use warnings; +use namespace::autoclean; use MooseX::Types -declare => [qw( GridT GridStatusT CharT LetterCellT TextResultT WordListT + FontDescrT FontOptT )]; use MooseX::Types::Moose qw(Str ArrayRef Int); use MooseX::Types::Structured qw(Tuple); @@ -23,4 +27,15 @@ subtype WordListT, as ArrayRef[Str]; subtype TextResultT, as ArrayRef[ArrayRef[LetterCellT]]; +class_type FontDescrT, { class => 'Pango::FontDescription' }; + +coerce FontDescrT, + from Str, + via { + require Pango; + Pango::FontDescription->from_string($_); + }; + +class_type FontOptT, { class => 'Cairo::FontOptions' }; + 1; diff --git a/script/qr-color.pl b/script/qr-color.pl index 05f95db..3081bd3 100644 --- a/script/qr-color.pl +++ b/script/qr-color.pl @@ -34,6 +34,7 @@ my $filler=GridFiller->new({ my $result = GridFiller::Result::Pango->new({ source_grid => $filler->grid, + ( @ARGV ? (font_description => $ARGV[0]) : () ), }); $filler->result($result); -- cgit v1.2.3