glejeune / Ruby-Graphviz

[MIRROR] Ruby interface to the GraphViz graphing tool
https://gitlab.com/glejeune/ruby-graphviz
Other
608 stars 116 forks source link

GraphViz::parse_string and unicode lables #115

Open vizvamitra opened 8 years ago

vizvamitra commented 8 years ago

I've found some strange behavior:

dot_str = "digraph G {\nHello [label = \"안녕하세요\"];\nWorld [label = \"Мир\"];\n  Hello -> World;\n}\n"

GraphViz.parse_string(dot_str).output(none: String)
# => "digraph G {\nHello [label = \"\\xEC\\x95\\x88\\xEB\\x85\\x95\\xED\\x95\\x98\\xEC\\x84\\xB8\\xEC\\x9A\\x94\"];\nWorld [label = \"\\xD0\\x9C\\xD0\\xB8\\xD1\\x80\"];\n  Hello -> World;\n}\n""

GraphViz.parse_string(dot_str).output(png: 'test.png') # =>

test

(ruby 2.0.0p353, ruby-graphviz 1.2.2, graphviz 2.36.0)

froderik commented 8 years ago

Same issue here but with swedish characters. Any progress? As far as I can tell GraphViz supports unicode so I guess it is called in the wrong way somehow.

vizvamitra commented 8 years ago

@froderik I ended up writing my own simple implementation of a tool that converts directed graph to dot string and feeding this string directly to dot command.

froderik commented 8 years ago

aah - thanks for posting! We have a dot file already that is generated from another source so maybe we should try to just pass it directly to dot instead of using graphviz. What do you think?

vizvamitra commented 8 years ago

@froderik it does work with russian so I think it'll do so with swedish too =)

system('dot /path/to/dot/file.dot -Tsvg -o /path/to/output/file.svg')

But you will probably need to handle all posible errors from dot command (I believe in this gem they handle errors here with just raising "Error from #{cmd}:\n#{errors}")

Also Ruby-Graphviz gem is quite heavy and I guess it'll be beter not to use it if your use cases are as simple as just format simple graph to dot or convert dot to image

froderik commented 8 years ago

great! will try it right away!

froderik commented 8 years ago

@vizvamitra - works like a charm - thanks for the help!

abruzzi commented 7 years ago

Any update about this?

I got a similar issue with Chinese character in the label of nodes.

script as the following

require "graphviz"

g = GraphViz::new( "G" )
g.add_nodes "chinese", label: '你好'
g.output( :dot => "x.dot" )

would generate dot as this:

digraph G {
    graph [bb="0,0,115.69,36"];
    node [label="\N"];
    chinese  [height=0.5,
        label="\u4F60\u597D",
        pos="57.845,18",
        width=1.6068];
}

and will generate graph contains node with label as u4F60u597D, any solutions or walk around please ?

atitan commented 7 years ago

It's platform-related issue. to_gv uses 'string'.to_s.inspect.gsub( "\\\\", "\\" ) to escape string, and inspect behaves differently on Windows.

c:\script>ruby test.rb
"\u4F60\u597D"
[user@i2 ~]$ ruby test.rb
"你好"

reference: http://stackoverflow.com/questions/34454084/ruby-sometimes-prints-unicode-escaped-chars-instead-of-the-chars-themselves-why

ghost commented 7 years ago

The problem is in lib/graphviz/utils.rb:46 function output_and_errors_from_command: Open3 works in binmode, that force Encoding:ASCII-8BIT

davidmoshal commented 6 years ago

this worked for me: < for the less than sign.

vanboom commented 3 years ago

This issue is still present in 2021 when labels have UTF-8 character codes, in our case French names with é character.

vanboom commented 3 years ago

The problem is in lib/graphviz/utils.rb:46 function output_and_errors_from_command: Open3 works in binmode, that force Encoding:ASCII-8BIT

When producing a SVG using -Tsvg, binode should be false. Working on a PR with hopes to finally resolve this. Is this gem actively maintained?

vanboom commented 3 years ago

PR submitted - not sure if anyone is listening though.

To contributors: Thank you for your consideration on my PR and for supporting this great gem.