Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for UTF-8 strings in labels/title #74

Closed
zmughal opened this issue May 21, 2021 · 2 comments
Closed

Support for UTF-8 strings in labels/title #74

zmughal opened this issue May 21, 2021 · 2 comments

Comments

@zmughal
Copy link
Member

zmughal commented May 21, 2021

Due to using syswrite() strings must be passed as UTF-8 encoded bytes:

An example where this is a problem with a workaround:

#!/usr/bin/env perl

use utf8;
use Encode;
use PDL;
use PDL::Graphics::Gnuplot;
use PDL::Constants qw(PI);

sub plot_sin {
	my ($w, $coeff) = @_;
	my $xrange = [ -2*PI, 2*PI ];
	my $x = zeroes(1e3)->xlinvals(@$xrange); my $y = sin($coeff * 2 * PI * $x );

	my $title = "y = sin( $coeff * 2π  * x )";
	# to get around sending text through syswrite()
	my $title_octets = encode('UTF-8', $title);

	$w->plot(with=>'lines', $x, $y, {
		xrange => $xrange,
		title => $title_octets, # can not pass $title as-is
	});
}
@zmughal
Copy link
Member Author

zmughal commented Dec 20, 2024

I came across this again even though I was the one that created the issue.

Perhaps when the string sent to syswrite() matches /[^\x00-\xFF]/, then we know we are not dealing with octets so a more informative error could be thrown.

Here's something that illustrates another less intrusive workaround:

use utf8;

use PDL;
use PDL::Constants 'PI';
use PDL::Graphics::Gnuplot;

my $gp = gpwin();
{ no utf8; # removing "no utf8" causes it to fail with "Wide character in syswrite"
    $gp->plot({'xlabel', "θ"}, zeros(100)->xlinvals(-PI, PI)->sin);
}
$gp->pause_until_close;

@mohawk2
Copy link
Member

mohawk2 commented Dec 20, 2024

Look good! Could you PR it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants