Skip to content

Commit

Permalink
add and fix som tests for Image.text
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Dec 9, 2023
1 parent f644660 commit 552c11b
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ t/Image/Path.t
t/Image/ROP.t
t/Image/Stream.t
t/Image/Stretch.t
t/Image/Text.t
t/Image/Tile.t
t/Image/Transform.t
t/Object/Application.xt
Expand Down
2 changes: 1 addition & 1 deletion Prima/sys/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Test::More;

our @ISA = qw(Exporter);
our @EXPORT = qw(create_window set_flag get_flag reset_flag wait_flag);
our $noX11 = 1 if defined Prima::XOpenDisplay();
our $noX11 = defined Prima::XOpenDisplay();
our $flag;
our $timeout = 500;

Expand Down
2 changes: 2 additions & 0 deletions class/Drawable.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Drawable_get_paint_state( Handle self)
return psEnabled;
else if ( is_opt( optInDrawInfo))
return psInformation;
else if ( is_opt( optInFontQuery))
return psFontQuery;
else
return psDisabled;
}
Expand Down
1 change: 1 addition & 0 deletions class/Image.cls
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ object Prima::Image( Prima::Drawable)
method Bool flood_fill( int x, int y, Color color, Bool singleBorder = 1);
static Font* font_match( SV * dummy, Font * source, Font * dest, Bool pick = true);
method SV* fonts( char * name = "", char * encoding = "");
method SV* font_encodings( char * encoding = "");
method int get_bpp();
method SV * get_handle();
method Color get_nearest_color( Color color);
Expand Down
27 changes: 25 additions & 2 deletions class/Image/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ plot_glyphs( Handle self, PGlyphsOutRec t, int x, int y )
ctx.region = var-> regionData;

ctx.rop = var-> extraROP;
color = my->get_color(self);
if ( flags & ggoMonochrome) {
if ( ctx.rop > ropWhiteness )
ctx.rop = ropCopyPut;
Expand All @@ -141,15 +142,29 @@ plot_glyphs( Handle self, PGlyphsOutRec t, int x, int y )
a = var-> alpha * a / 255;
ctx.rop &= ~(0xff << ropSrcAlphaShift);
ctx.rop |= ropSrcAlpha | ( a << ropSrcAlphaShift );
} else if ( ctx.rop <= ropWhiteness ) {
switch (ctx.rop) {
case ropCopyPut:
break;
case ropNoOper:
return true;
case ropBlackness:
color = 0;
break;
case ropWhiteness:
color = 0xffffff;
break;
default:
return false;
}
ctx.rop = ropBlend | ropSrcAlpha | (var-> alpha << ropSrcAlphaShift);
}

if ( !apc_gp_get_text_out_baseline( self))
y += var-> font. descent;
o.x = x;
o.y = y;


color = my->get_color(self);
if ( !( flags & ggoMonochrome))
color = Image_premultiply_color(self, ctx.rop, color);
Image_color2pixel( self, color, ctx.color);
Expand Down Expand Up @@ -313,6 +328,14 @@ Image_fonts( Handle self, char * name, char * encoding)
return Application_fonts( self, name, encoding);
}

SV*
Image_font_encodings( Handle self, char * encoding)
{
if (!opt_InPaint && !my-> begin_font_query(self) )
return NULL_SV;
return Application_font_encodings( self, encoding);
}

#ifdef __cplusplus
}
#endif
3 changes: 3 additions & 0 deletions include/Image_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Image_premultiply_color( Handle self, int rop, Color color);
SV*
Application_fonts( Handle self, char * name, char * encoding);

SV*
Application_font_encodings( Handle self, char * encoding);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/apricot.h
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,8 @@ PS(Disabled)
PS(Enabled)
#define psInformation 2
PS(Information)
#define psFontQuery 3
PS(FontQuery)
END_TABLE(ps,UV)
#undef PS

Expand Down
87 changes: 87 additions & 0 deletions t/Image/Text.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use strict;
use warnings;
use utf8;

use Test::More;
use Prima::sys::Test;
use Prima::noX11;
use Prima qw(Config);

my $noX11 = defined Prima::XOpenDisplay();

if ( $^O !~ /win32/i && 0 == grep { $_ eq 'freetype' } @{ $Prima::Config::Config{ldlibs} } ) {
plan skip_all => 'not compiled with freetype';
}

my $i = Prima::Image->new( size => [50,50], type => im::BW );

is( $i-> get_paint_state, ps::Disabled, 'get_paint_state == ps::Disabled');
ok( $i->begin_font_query, 'begin font query');
is( $i-> get_paint_state, ps::FontQuery, 'get_paint_state == ps::FontQuery');
$i->end_font_query;
is( $i-> get_paint_state, ps::Disabled, 'end_font_query == ps::Disabled');

unless ( $noX11 ) {
ok( $i->begin_font_query, 'begin font query again');
ok( $i->begin_paint_info, 'begin paint info');
is( $i-> get_paint_state, ps::Information, 'get_paint_state == ps::Information');
$i->end_paint_info;
is( $i-> get_paint_state, ps::Disabled, 'end_font_query == ps::Disabled');
}

my $x = scalar @{ $i->fonts // [] };
cmp_ok( $x, '>=', 1, "$x direct access fonts are reported");

$x = $i->font_mapper->count;
cmp_ok($x, '>=', 1, "$x fonts for shaping are reported");

$x = scalar @{ $i->font_encodings // [] };
cmp_ok($x, '>=', 1, "$x font encodings reported");

$i->clear;
is( $i->clone(type => im::Byte)->sum, $i->width * $i->height * 255, 'white background');
$i->text_out( '123', 10, 10);
isnt( $i->clone(type => im::Byte)->sum, $i->width * $i->height * 255, 'black text');

if (my $s = $i->text_shape("f\x{444}\x{555}")) {
is( scalar @{$s->glyphs}, 3, "text shape");
$i->clear;
$i->text_out( $s, 10, 10);
isnt( $i->clone(type => im::Byte)->sum, $i->width * $i->height * 255, 'shaped text');
}

for my $str (qw(im::bpp4 im::Byte im::RGB im::Short)) {
my $type = eval $str;
$i = Prima::Image->new( size => [50,50], type => $type);
$i->clear;
$i->text_out('123', 10, 10);
cmp_ok( $i->clone(type => im::Byte)-> sum / $i->width / $i->height, '<', 255, "mono text on $str");
}

$i = Prima::Image->new( size => [50,50], type => im::Byte);
$i->clear;
$i->antialias(1);
$i->text_out('123', 10, 10);
cmp_ok( $i->sum / $i->width / $i->height, '<', 255, "blended black text on graybyte image");

$i = Prima::Image->new( size => [50,50], type => im::RGB);
$i->clear;
$i->antialias(1);
$i->color(cl::Black);
$i->text_out('123', 10, 10);
my $black = $i->clone(type => im::Byte)->sum / $i->width / $i->height;
cmp_ok( $black, '<', 255, "blended black text on RGB image");
$i->clear;
$i->color(0x808080);
$i->text_out('123', 10, 10);
my $gray = $i->clone(type => im::Byte)->sum / $i->width / $i->height;
cmp_ok($gray, '>', $black, 'blended gray text on RGB image is brighter than black');
cmp_ok($gray, '<', 255, 'blended gray text on RGB image is darker than white');
$i->clear;
$i->color(cl::Black);
$i->alpha(0x80);
$i->text_out('123', 10, 10);
my $alpha = $i->clone(type => im::Byte)->sum / $i->width / $i->height;
cmp_ok(abs($alpha-$gray), '<', 0.5, "gray(a=1) and black(a=0.5) are basically the same");

done_testing;
4 changes: 4 additions & 0 deletions win32/stock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,8 @@ apc_fonts( Handle self, const char* facename, const char *encoding, int * retCou
CPrinter( self)-> begin_paint_info( self);
}
dc = sys ps;
} else if ( is_opt(optInFontQuery)) {
dc = sys ps;
} else
return NULL;

Expand Down Expand Up @@ -1677,6 +1679,8 @@ apc_font_encodings( Handle self )
CPrinter( self)-> begin_paint_info( self);
}
dc = sys ps;
} else if ( is_opt(optInFontQuery)) {
dc = sys ps;
} else
return NULL;

Expand Down

0 comments on commit 552c11b

Please sign in to comment.