Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/draw/draw2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Default setting parameters of the molecule drawing canvas.
`:alongside` for others (default)
- `:atomcolor`(Dict{Symbol,Color}) atom symbol and bond colors for organic atoms
- `:defaul_atom_color`(Dict{Symbol,Color}) colors for other atoms
- `:C_visible`(Bool) if C atoms should be visible
"""
const DRAW_SETTING = Dict(
:display_terminal_carbon => false,
Expand All @@ -48,7 +49,8 @@ const DRAW_SETTING = Dict(
:Br => Color(0, 192, 0),
:I => Color(0, 128, 0)
),
:default_atom_color => Color(0, 192, 192)
:default_atom_color => Color(0, 192, 192),
:C_visible => false
)

# For 3d rendering, we use white for hydrogen
Expand Down Expand Up @@ -87,7 +89,7 @@ Return whether the atom is visible in the 2D drawing.
mul_ = multiplicity(mol)
mas_ = getproperty.(nodeattrs(mol), :mass)
for i in 1:nodecount(mol)
sym_[i] === :C || continue
(sym_[i] === :C && ! setting[:C_visible]) || continue
chg_[i] == 0 || continue
mul_[i] == 1 || continue
mas_[i] === nothing || continue
Expand Down Expand Up @@ -298,12 +300,13 @@ end


function drawatomindex!(canvas::Canvas, mol::UndirectedGraph;
color=Color(0, 0, 0), bgcolor=Color(240, 240, 255))
color=Color(0, 0, 0), bgcolor=Color(240, 240, 255),
opacity=1.0)
isatomvisible_ = isatomvisible(mol)
for i in nodeset(mol)
offset = isatomvisible_[i] ? (0.0, canvas.fontsize/2.0) : (0.0, 0.0)
pos = Point2D(canvas.coords, i) + offset
setatomnote!(canvas, pos, string(i), color, bgcolor)
setatomnote!(canvas, pos, string(i), color, bgcolor, opacity)
end
return
end
Expand Down
23 changes: 19 additions & 4 deletions src/draw/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export
SvgCanvas,
tosvg,
drawsvg,
initcanvas!
initcanvas!,
savesvg

using Printf

Expand Down Expand Up @@ -324,15 +325,15 @@ setatomleft!(canvas::SvgCanvas, pos, sym, color, hcnt, charge) = atomsymbol!(
)


function setatomnote!(canvas::SvgCanvas, pos, text, color, bgcolor)
function setatomnote!(canvas::SvgCanvas, pos, text, color, bgcolor, opacity)
size = round(Int, canvas.fontsize * canvas.annotsizef)
bxy = svgcoords(pos)
txy = svgcoords(pos + (0, size))
c = svgcolor(color)
bc = svgcolor(bgcolor)
elem = """<g>
<rect $(bxy) width="$(size)" height="$(size)" rx="$(size/2)" ry="$(size/2)" fill="$(bc)" />
<text $(txy) font-size="$(size)" fill="$(c)">$(text)</text>
<rect $(bxy) width="$(size)" height="$(size)" rx="$(size/2)" ry="$(size/2)" fill="$(bc)" opacity="$(opacity)" />
<text $(txy) font-size="$(size)" fill="$(c)">$(text) </text>
</g>
"""
push!(canvas.elements, elem)
Expand Down Expand Up @@ -493,3 +494,17 @@ function drawwave!(canvas::SvgCanvas, seg, ucolor, vcolor)
push!(canvas.elements, elem)
return
end

"""
write svg string to file.
"""
function savesvg(the_svg_string::String, filename::String)
if ! contains(filename, ".svg")
filename *= ".svg"
end

f = open(filename, "w")
write(f, the_svg_string)
close(f)
return filename
end