App-Codit

 view release on metacpan or  search on metacpan

lib/App/Codit/Plugins/Exporter.pm  view on Meta::CPAN

		$iv{'themefile'} = $self->extGet('ConfigFolder')->ConfigFolder . '/highlight_theme.ctt';
		$iv{'font'} = $self->configGet('-contentfont');
		$iv{'linenumbers'} = 1;
		$iv{'margins'} = 10;
		$iv{'maxwidth'} = 0;
		$iv{'tabsize'} = 3;
	}
	$iv{'outfile'} = "$name.png";

	#popping the form
	my %options = $self->popForm(
#		-acceptempty => 1,
		-initialvalues => \%iv,
		-oktext => 'Export',
		-structure => [
			'*section' => 'Output',
			outfile => ['file', 'File', -width => 50],
			'*end',
			'*section' => 'Theme',
			'*frame',
			background => ['color', 'Background', -width => 8],
			'*column',
			foreground => ['color', 'Foreground', -width => 8],
			'*end',
			themefile => ['file', 'Theme file'],
			font => ['font', 'Font'],
			'*end',
			'*section' => 'Features',
			linenumbers => ['boolean', 'Line numbers'],
			tabsize => ['spin', 'Tab size', -width => 6],
			'*column',
			maxwidth => ['spin', 'Max width', -width => 6],
			margins => ['spin', 'Margins', -width => 6],
			'*end',
		],
		-title => 'Export to PNG',
	);
	return unless %options;

	#saving options
	my $cff = $self->extGet('ConfigFolder');
	$cff->saveHash('export2pngrc', 'exporterpng', %options);

	#checking data
	my $file = delete $options{'outfile'};
	croak "Outfile not specified" if $file eq '';
	my $tabstr = '';
	for (1 .. $options{'tabsize'}) { $tabstr = "$tabstr " }
	$self->background(Imager::Color->new($options{'background'}));
	$self->imargin($options{'margins'});
	$self->linenumbers($options{'linenumbers'});
	$self->linecolumn(length($widg->linenumber('end - 1c')));
	$self->maxwidth($options{'maxwidth'});
	$self->tabstring($tabstr);

	#converting data
	my $art = $self->extGet('Art');
	my $l = $self->Label(-font => $options{'font'});
	my $f = $l->cget('-font');
	my $fntnorm = $widg->fontCompose($f, -slant => 'roman', -weight => 'normal');
	my $fntbold = $widg->fontCompose($f, -slant => 'roman', -weight => 'bold');
	my $fntbolditalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'bold');
	my $fntitalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'normal');

	my $linespace = $self->fontMetrics($f, '-linespace');
	my $desc = $self->fontMetrics($f, '-descent');

	$l->destroy;

	my $foreground =  Imager::Color->new($options{'foreground'});

	$self->xpos($self->imargin);
	$self->ypos($self->imargin + $linespace - $desc);
	$self->iwidth($self->imargin * 2);
	$self->iheight(($self->imargin * 2) + $linespace);

	$self->image($self->inew($self->iwidth, $self->iheight));

	#setting up tags
	my $thmfl = $options{'themefile'};
	my $theme = Tk::CodeText::Theme->new;
	unless (-e $thmfl) {
		$self->logError("Theme file '$thmfl' does not exist");
		return
	}
	$theme->load($thmfl);
	my %tags = ();
	for ($theme->tagList) {
		my $tag = $_;
#		print "tag: $tag\n";
		my %options = ();
		my $bold = $theme->getItem($tag, '-weight');
		$bold = 0 unless defined $bold;
		my $slant = $theme->getItem($tag, '-slant');
		$slant = 0 unless defined $slant;
		if ($bold and $slant) {
#			print "  setting font bold/italic\n";
			$options{'-font'} = $fntbolditalic
		} elsif ($bold) {
#			print "  setting font bold\n";
			$options{'-font'} = $fntbold
		} elsif ($slant) {
#			print "  setting font italic\n";
			$options{'-font'} = $fntitalic
		} else {
#			print "  normal font remains\n";
			$options{'-font'} = $fntnorm
		}
		my $fg = $theme->getItem($tag, '-foreground');
		if ($fg ne '') {
#			print "  setting foreground $fg\n";
			$options{'-foreground'} = Imager::Color->new($fg);
		} else {
#			print "  default foreground remains\n";
			$options{'-foreground'} = $foreground;
		}
		my $bg = $theme->getItem($tag, '-background');
		if ($bg ne '') {
#			print "  default background $bg\n";
			$options{'-background'} = Imager::Color->new($bg);
		} else {
#			print "  default background remains\n";
#			$options{'-background'} = $self->background;
		}
		$tags{$tag} = \%options;
	}

	#setting up highlighter
	my $syntax = $widg->cget('-syntax');
	my $hl;
	if ($syntax ne 'None') {
		$hl = Syntax::Kamelon->new(
			syntax => $syntax,
			formatter => ['Base',
#				foldingdepth => 'all',
			],
		);
		$hl->Reset;
	}

	#exporting
	my $last = $widg->linenumber('end - 1c');
	my $end = $widg->index('end - 1c');
	my $count = 0;
	$self->mdi->progressAdd('exporter', 'Exporting', $last, \$count);
	for (1 .. $last) {
		my $linenum = $_;
		my $linestart = "$linenum.0";

		my $lineend = $widg->index("$linestart lineend + 1c");
		$lineend = $end if $widg->compare($end,'<',$lineend);

		$self->iprintln($linenum, $fntnorm, $foreground) if $self->linenumbers;

		my $line = $widg->get($linestart, $lineend);
		if ($syntax eq 'None') {
			$self->iprint($line, $fntnorm, $foreground);
		} else {
			my @h = $hl->ParseRaw($line);
			while (@h) {
				my $string = shift @h;



( run in 0.485 second using v1.01-cache-2.11-cpan-d7f47b0818f )