<commit_before>package Bio::EnsEMBL::Renderer;
use strict;
use Exporter;
use vars qw(@ISA);
use lib "../../../../modules";
use ColourMap;
use Bio::EnsEMBL::Glyph::Rect;

@ISA = qw(Exporter);

sub new {
    my ($class, $config, $container, $glyphsets_ref, $transform_ref) = @_;

    my $spacing = 5;

    #########
    # set up the type to be gif|wmf|ps|whatever
    # 
    my $type = $class;
    $type =~ s/.*:://;

    #########
    # calculate scaling factors and canvas dimensions
    #
    my $im_height = $spacing;
    for my $glyphset (@{$glyphsets_ref}) {
	$im_height += $glyphset->height() + $spacing;
    }

    my $im_width = $config->image_width();
    $$transform_ref{'scalex'} = $config->scalex();

    my $self = {
	'glyphsets' => $glyphsets_ref,
	'transform' => $transform_ref,
	'canvas'    => undef,
	'colourmap' => $config->colourmap(),
	'config'    => $config,
	'container' => $container,
	'type'      => undef,
	'spacing'   => $spacing,
    };

    bless($self, $class);

    #########
    # create a fresh canvas
    #
    if($self->can('init_canvas')) {
	$self->init_canvas($config, $im_width, $im_height);
    }

    $self->render();

    return $self;
}

sub render {
    my ($this) = @_;

    my ($cstart, $cend);

    $cstart = 0;
    $cend   = $this->{'container'}->length();

    #########
    # give us a top margin
    #
#    $this->{'transform'}->{'translatey'} += $this->{'transform'}->{'spacing'};

    #########
    # pull out alternating background colours for this script
    #
    my $config = $this->{'config'};
    my $white  = $config->bgcolour() || $config->colourmap->id_by_name('white');
    my $bgcolours = {
	'0' => $config->get($config->script(), '_settings', 'bgcolour1') || $white,
	'1' => $config->get($config->script(), '_settings', 'bgcolour2') || $white,
    };

    my $yoffset = $this->{'spacing'};
    my $iteration = 0;
    for my $glyphset (@{$this->{'glyphsets'}}) {

	#########
	# remove any whitespace at the top of this row
	#
	my $gminy = $glyphset->miny();

	$this->{'transform'}->{'translatey'} = -$gminy + $yoffset + ($iteration * $this->{'spacing'});

	#########
	# colour the area behind this strip
	#
	my $background = new Bio::EnsEMBL::Glyph::Rect({
	    'x'         => 0,
	    'y'         => $glyphset->miny(),
	    'width'     => $this->{'config'}->image_width(),
	    'height'    => $glyphset->maxy() - $glyphset->miny(),
	    'colour'    => $$bgcolours{$iteration % 2},
	    'absolutex' => 1,
	});

	$glyphset->unshift($background);

	for my $glyph ($glyphset->glyphs()) {

	    my $method = $this->method($glyph);
	    if($this->can($method)) {
		$this->$method($glyph);
	    } else {
		print STDERR qq(Bio::EnsEMBL::Renderer::render: Don't know how to $method\n);
	    }
	}

	#########
	# translate the top of the next row to the bottom of this one
	#
	$yoffset += $glyphset->height();
	$iteration ++;<commit_msg>margins, fixes and other bits<commit_after>package Bio::EnsEMBL::Renderer;
use strict;
use Exporter;
use vars qw(@ISA);
use lib "../../../../modules";
use ColourMap;
use Bio::EnsEMBL::Glyph::Rect;

@ISA = qw(Exporter);

sub new {
    my ($class, $config, $container, $glyphsets_ref, $transform_ref) = @_;

    my $self = {
	'glyphsets' => $glyphsets_ref,
	'transform' => $transform_ref,
	'canvas'    => undef,
	'colourmap' => $config->colourmap(),
	'config'    => $config,
	'container' => $container,
	'spacing'   => 5,
    };

    bless($self, $class);

    $self->render();

    return $self;
}

sub render {
    my ($this) = @_;

    #########
    # pull out alternating background colours for this script
    #
    my $config = $this->{'config'};
    my $white  = $config->bgcolour() || $config->colourmap->id_by_name('white');
    my $bgcolours = {
	'0' => $config->get($config->script(), '_settings', 'bgcolour1') || $white,
	'1' => $config->get($config->script(), '_settings', 'bgcolour2') || $white,
    };

    my $bgcolour_flag;
    $bgcolour_flag = 1 if($$bgcolours{0} ne $$bgcolours{1});

    my $spacing = $this->{'spacing'};

    #########
    # calculate the maximum label width (plus margin)
    #
    my $label_length_px = 0;

    for my $glyphset (@{$this->{'glyphsets'}}) {
	next if(!defined $glyphset->label());

	$glyphset->label->text($glyphset->label->text());

	my $chars  = length($glyphset->label->text());
	my $pixels = $chars * $config->texthelper->width($glyphset->label->font());
	
	$label_length_px = $pixels if($pixels > $label_length_px);
    }

    #########
    # add spacing before and after labels
    #
    $label_length_px += $spacing * 2;

    #########
    # calculate scaling factors
    #
    my $pseudo_im_width = $config->image_width() - $label_length_px - $spacing;

    #########
    # set scaling factor for base-pairs -> pixels
    #
    $this->{'transform'}->{'scalex'}         = $pseudo_im_width / $config->container_width();

    #########
    # set scaling factor for 'absolutex' coordinates -> real pixel coords
    #
    $this->{'transform'}->{'absolutescalex'} = $pseudo_im_width / $config->image_width();

    #########
    # because our text label starts are < 0, translate everything back onto the canvas
    #
    my $extra_translation = $label_length_px;
    $this->{'transform'}->{'translatex'} += $extra_translation;

    #########
    # now set all our labels up with scaled negative coords
    # and while we're looping, tot up the image height
    #
    my $im_height = 0;

    for my $glyphset (@{$this->{'glyphsets'}}) {
	
	my $fntheight = (defined $glyphset->label())?$config->texthelper->height($glyphset->label->font()):0;
	my $gstheight = $glyphset->height();

	if($gstheight > $fntheight) {
	    $im_height += $gstheight + $spacing;
	} else {
	    $im_height += $fntheight + $spacing;
	}

	next if(!defined $glyphset->label());
	$glyphset->label->x(-($extra_translation - $spacing) / $this->{'transform'}->{'scalex'});
    }

    my $im_width = $config->image_width();

    #########
    # create a fresh canvas
    #
    if($this->can('init_canvas')) {
	$this->init_canvas($config, $im_width, $im_height);
    }

    my $yoffset = $spacing;
    my $iteration = 0;
    for my $glyphset (@{$this->{'glyphsets'}}) {

	#########
	# remove any whitespace at the top of this row
	#
	my $gminy = $glyphset->miny();

	$this->{'transform'}->{'translatey'} = -$gminy + $yoffset + ($iteration * $spacing);

	if(defined $bgcolour_flag) {
	    #########
	    # colour the area behind this strip
	    #
	    my $background = new Bio::EnsEMBL::Glyph::Rect({
		'x'         => 0,
		'y'         => $gminy,
		'width'     => $this->{'config'}->image_width(),
		'height'    => $glyphset->maxy() - $gminy,
		'colour'    => $$bgcolours{$iteration % 2},
		'absolutex' => 1,
	    });
	    $glyphset->unshift($background);
	}

	#########
	# set up the label for this strip
	#
	if(defined $glyphset->label()) {
	    my $gh = $config->texthelper->height($glyphset->label->font());
	    $glyphset->label->y((($glyphset->maxy() - $glyphset->miny() - $gh) / 2) + $glyphset->miny());
	    $glyphset->label->height($gh);
	    $glyphset->push($glyphset->label());
	}

	#########
	# loop through everything and draw it
	#
	for my $glyph ($glyphset->glyphs()) {
	    my $method = $this->method($glyph);
	    if($this->can($method)) {
		$this->$method($glyph);
	    } else {
		print STDERR qq(Bio::EnsEMBL::Renderer::render: Don't know how to $method\n);
	    }
	}

	#########
	# translate the top of the next row to the bottom of this one
	#
	$yoffset += $glyphset->height();
	$iteration ++;
{'fraction_non_alphanumeric': 0.21913251729643426, 'fraction_numerical': 0.0037253858435337944, 'mean_word_length': 3.2734508243320066, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 150, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1055569', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2608, 'n_words': 573}