Skip to content

Commit cdcddd0

Browse files
authored
Merge pull request #70 from t-bltg/bgcols
Support per char background color
2 parents 4043923 + 465e5f9 commit cdcddd0

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/rendering.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ Render `str` into `img` using the font `face` of size `pixelsize` at coordinates
3838
# Arguments
3939
* `y0,x0`: origin is in upper left with positive `y` going down
4040
* `fcolor`: foreground color; AbstractVector{T}, typemax(T) for T<:Integer, otherwise one(T)
41-
* `bcolor`: background color; set to `nothing` for transparent
41+
* `gcolor`: background color; AbstractVector{T}, typemax(T) for T<:Integer, otherwise one(T)
42+
* `bcolor`: canvas background color; set to `nothing` for transparent
4243
* `halign`: :hleft, :hcenter, or :hright
4344
* `valign`: :vtop, :vcenter, :vbaseline, or :vbottom
4445
"""
4546
function renderstring!(
4647
img::AbstractMatrix{T}, str::Union{AbstractVector{Char},String}, face::FTFont, pixelsize::Union{Int, Tuple{Int, Int}}, y0, x0;
47-
fcolor::Union{AbstractVector{T},T} = one_or_typemax(T), bcolor::Union{T,Nothing} = zero(T),
48+
fcolor::Union{AbstractVector{T},T} = one_or_typemax(T),
49+
gcolor::Union{AbstractVector{T},T,Nothing} = nothing,
50+
bcolor::Union{T,Nothing} = zero(T),
4851
halign::Symbol = :hleft, valign::Symbol = :vbaseline
4952
) where T<:Union{Real,Colorant}
5053

@@ -106,23 +109,28 @@ function renderstring!(
106109
end
107110

108111
fcol = fcolor isa AbstractVector ? fcolor[istr] : fcolor
112+
gcol = gcolor isa AbstractVector ? gcolor[istr] : gcolor
109113

110114
# trim parts of glyph images that are outside the destination
111115
cliprowlo, cliprowhi = max(0, -(py-by)), max(0, py - by + h - imgh)
112116
clipcollo, clipcolhi = max(0, -bx-px), max(0, px + bx + w - imgw)
113117

114-
if bcolor === nothing
118+
if gcol === nothing
115119
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
116-
bitmaps[istr][col,row]==0 && continue
120+
bitmaps[istr][col,row] == 0 && continue
117121
c1 = bitmaps[istr][col,row] / bitmapmax * fcol
118122
img[row+py-by, col+px+bx] = T <: Integer ? round(T, c1) : T(c1)
119123
end
120124
else
125+
img[
126+
clamp(py-ymax+1, 1, imgh) : clamp(py-ymin-1, 1, imgh),
127+
clamp(px-1, 1, imgw) : clamp(px+sumadvancex-1, 1, imgw)
128+
] .= gcol
121129
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
122130
bitmaps[istr][col, row] == 0 && continue
123131
w1 = bitmaps[istr][col, row] / bitmapmax
124132
c1 = w1 * fcol
125-
c0 = (1.0 - w1) * bcolor
133+
c0 = (1.0 - w1) * gcol
126134
img[row + py - by, col + px + bx] = T <: Integer ? round(T, c1 + c0) : T(c1 + c0)
127135
end
128136
end

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ renderstring!(
170170
renderstring!(zeros(UInt8, 20, 100), "helgo", face, 10, 25, 80)
171171
for str in ("helgo", collect("helgo"))
172172
fcolor = [RGB{Float32}(rand(3)...) for _ 1:length(str)]
173-
renderstring!(zeros(RGB{Float32}, 20, 100), str, face, 10, 0, 0; fcolor = fcolor)
173+
gcolor = [RGB{Float32}(rand(3)...) for _ 1:length(str)]
174+
renderstring!(zeros(RGB{Float32}, 20, 100), str, face, 10, 0, 0; fcolor = fcolor, gcolor = gcolor)
174175
end
175176

176177
# Find fonts

0 commit comments

Comments
 (0)