Skip to content

Instantly share code, notes, and snippets.

@taiyoh
Created November 29, 2010 12:41
Show Gist options
  • Save taiyoh/719910 to your computer and use it in GitHub Desktop.
Save taiyoh/719910 to your computer and use it in GitHub Desktop.
rewrite of Imager::DTP::Letter::draw
sub draw {
my($self) = shift;
my %o = $self->_draw_init(@_);
# recalculate bounding box
$self->_calcWidthHeight();
# draw frame - for debug
if($o{debug}){
# real body frame
$o{target}->box(
filled=>1,aa=>0,color=>'#FFAA99',
xmin=>$o{x} + $self->getLeftBearing(),
ymin=>$o{y} + $self->getGlobalAscent() - $self->getAscent(),
xmax=>$o{x} + $self->getEndOffset(),
ymax=>$o{y} + $self->getGlobalAscent() - $self->getDescent(),
) if($o{debug} > 1);
# virtual (outer) body frame
$o{target}->box(
filled=>0,aa=>0,color=>'#999999',
xmin=>$o{x},
ymin=>$o{y},
xmax=>$o{x} + $self->getAdvancedWidth(),
ymax=>$o{y} + $self->getGlobalAscent() - $self->getGlobalDescent(),
);
}
# scale transformation
my($sx,$sy) = $self->getScale();
if($sx != 1 || $sy != 1){
my $m = Imager::Matrix2d->scale( x => $sx, y => $sy );
$self->getFont->transform( matrix => $m );
}
# draw letter - using Imager::String method
my $font = $self->getFont;
my $x = $o{x} + $self->getLeftBearing();
my $y = $o{y} + $self->getGlobalAscent() - $self->getAscent();
if($self->getText() eq 'ー'){
$font->transform(matrix => Imager::Matrix2d->rotate(degrees => 270));
my $bbox = $font->bounding_box(
string => $self->getText,
size => $font->{size}
);
$x = $x + $bbox->total_width / 2;
$y = $y - $self->getAscent() + $self->getAscent() / 4 + 2;
}
else{
my $matrix = Imager::Matrix2d->rotate(degrees => 0);
$font->transform(matrix => $matrix);
}
$o{target}->string(
%{$o{others}},
x => $x,
y => $y,
text => $self->getText(),
font => $font,
utf8 => 1, vlayout => 0, align => 0
) or die $o{target}->errstr;
# draw baseline position - for debug
if($o{debug}){
$o{target}->box(
filled=>1,aa=>0,color=>'#880000',
xmin=>$o{x},
xmax=>$o{x}+$self->getAdvancedWidth(),
ymin=>$o{y}+$self->getGlobalAscent(),
ymax=>$o{y}+$self->getGlobalAscent()
);
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment