Skip to content

Commit

Permalink
fix(wordcloud): improve mask handling
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Sep 30, 2024
1 parent 3bda37a commit 26b4a8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/wc-class.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ end
wordcloud(words, weight::Number; kargs...) = wordcloud(words, repeat([weight], length(words)); kargs...)
function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVector{<:Real};
colors=:auto, angles=:auto,
mask=:auto, svgmask=nothing, edit_mask=true, masksize=:auto, fonts=:auto, language=:auto,
mask=:auto, svgmask=nothing, editmask=true, masksize=:auto, fonts=:auto, language=:auto,
transparent=:auto, minfontsize=:auto, maxfontsize=:auto, avgfontsize=12,
spacing=:auto, density=0.5, state=layout!,
style=:auto, centralword=:auto, reorder=:auto, level=:auto, rt=:auto, kargs...)
Expand All @@ -97,7 +97,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
level != :auto && (params[:level] = level)
rt != :auto && (params[:rt] = rt)

colors, angles, mask, svgmask, fonts, transparent = processscheme(words, weights; colors=colors, angles=angles, mask=mask, svgmask=svgmask, edit_mask=edit_mask, masksize=masksize,
colors, angles, mask, svgmask, fonts, transparent = processscheme(words, weights; colors=colors, angles=angles, mask=mask, svgmask=svgmask, editmask=editmask, masksize=masksize,
fonts=fonts, avgfontsize=avgfontsize, language=language, transparent=transparent, params=params, kargs...)
params[:colors] = Any[colors...]
params[:angles] = angles
Expand Down Expand Up @@ -147,7 +147,7 @@ function wordcloud(words::AbstractVector{<:AbstractString}, weights::AbstractVec
end
wc
end
function processscheme(words, weights; colors=:auto, angles=:auto, mask=:auto, svgmask=nothing, edit_mask=true,
function processscheme(words, weights; colors=:auto, angles=:auto, mask=:auto, svgmask=nothing, editmask=true,
masksize=:auto, maskcolor=:default, keepmaskarea=:auto,
backgroundcolor=:default, padding=:default,
outline=:default, linecolor=:auto, fonts=:auto, avgfontsize=12, language=:auto,
Expand Down Expand Up @@ -201,7 +201,7 @@ function processscheme(words, weights; colors=:auto, angles=:auto, mask=:auto, s
preservevolume=keepmaskarea, returnkwargs=true, kg..., kargs...)
merge!(params, maskkw)
transparent = c -> c != torgba(maskcolor)
elseif edit_mask
elseif editmask
if masksize == :auto
ms = volumeproposal(words, weights, avgfontsize)
elseif masksize in DEFAULTSYMBOLS
Expand Down Expand Up @@ -242,11 +242,17 @@ function processscheme(words, weights; colors=:auto, angles=:auto, mask=:auto, s
linecolor = randomlinecolor(colors)
end
padding in DEFAULTSYMBOLS && (padding = outline)
if svgmask !== nothing
svgmask = loadmask(svgmask, ms...; color=maskcolor, transparent=transparent, backgroundcolor=bc,
outline=outline, linecolor=linecolor, padding=padding, preservevolume=keepmaskarea, kargs...)
end
mask, binarymask = loadmask(mask, ms...; color=maskcolor, transparent=transparent, backgroundcolor=bc,
outline=outline, linecolor=linecolor, padding=padding, return_bitmask=true, preservevolume=keepmaskarea, kargs...)
binarymask === nothing || (transparent = .!binarymask)
else
mask = loadmask(mask)
svgmask = svgmask === nothing ? nothing : loadmask(svgmask)
transparent = transparent
end
# under this line: both mask == :auto or not
if transparent == :auto
Expand Down Expand Up @@ -286,7 +292,7 @@ function getscheme(wc::WC)
:svgmask => wc.svgmask,
:maskcolor => getparameter(wc, :maskcolor),
:backgroundcolor => getparameter(wc, :backgroundcolor),
:edit_mask => false,
:editmask => false,
:transparent => getparameter(wc, :transparent),
]
for p in (:style, :centralword, :reorder, :level, :rt)
Expand Down
2 changes: 1 addition & 1 deletion src/wc-helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function paintsvg(wc::WC; background=true)
bgcolor = (1,1,1,0)
end
if !(background isa SVG)
@warn "embed bitmap into SVG. You can set `background=false` to remove background."
@warn "Embedding bitmap into SVG. Set `background=false` in `paintsvg` to remove the background."
background = tosvg(background)
end
imgs = Iterators.flatten(((background,), imgs))
Expand Down

0 comments on commit 26b4a8c

Please sign in to comment.