Skip to content

Commit 9c6b7e2

Browse files
authored
Merge pull request #69 from t-bltg/cols
Support `Vector` of colors and chars
2 parents 6f898ca + d140c77 commit 9c6b7e2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/rendering.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Render `str` into `img` using the font `face` of size `pixelsize` at coordinates
3737
3838
# Arguments
3939
* `y0,x0`: origin is in upper left with positive `y` going down
40-
* `fcolor`: foreground color; typemax(T) for T<:Integer, otherwise one(T)
40+
* `fcolor`: foreground color; AbstractVector{T}, typemax(T) for T<:Integer, otherwise one(T)
4141
* `bcolor`: background color; set to `nothing` for transparent
4242
* `halign`: :hleft, :hcenter, or :hright
4343
* `valign`: :vtop, :vcenter, :vbaseline, or :vbottom
4444
"""
4545
function renderstring!(
46-
img::AbstractMatrix{T}, str::String, face::FTFont, pixelsize::Union{Int, Tuple{Int, Int}}, y0, x0;
47-
fcolor::T = one_or_typemax(T), bcolor::Union{T,Nothing} = zero(T),
46+
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),
4848
halign::Symbol = :hleft, valign::Symbol = :vbaseline
4949
) where T<:Union{Real,Colorant}
5050

@@ -53,6 +53,8 @@ function renderstring!(
5353
pixelsize = pixelsize[1]
5454
end
5555

56+
str = str isa AbstractVector ? String(str) : str
57+
5658
set_pixelsize(face, pixelsize)
5759

5860
bitmaps = Vector{Matrix{UInt8}}(undef, lastindex(str))
@@ -103,21 +105,23 @@ function renderstring!(
103105
px += kx
104106
end
105107

108+
fcol = fcolor isa AbstractVector ? fcolor[istr] : fcolor
109+
106110
# trim parts of glyph images that are outside the destination
107111
cliprowlo, cliprowhi = max(0, -(py-by)), max(0, py - by + h - imgh)
108112
clipcollo, clipcolhi = max(0, -bx-px), max(0, px + bx + w - imgw)
109113

110114
if bcolor === nothing
111115
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
112116
bitmaps[istr][col,row]==0 && continue
113-
c1 = bitmaps[istr][col,row] / bitmapmax * fcolor
117+
c1 = bitmaps[istr][col,row] / bitmapmax * fcol
114118
img[row+py-by, col+px+bx] = T <: Integer ? round(T, c1) : T(c1)
115119
end
116120
else
117121
for row = 1+cliprowlo : h-cliprowhi, col = 1+clipcollo : w-clipcolhi
118122
bitmaps[istr][col, row] == 0 && continue
119123
w1 = bitmaps[istr][col, row] / bitmapmax
120-
c1 = w1 * fcolor
124+
c1 = w1 * fcol
121125
c0 = (1.0 - w1) * bcolor
122126
img[row + py - by, col + px + bx] = T <: Integer ? round(T, c1 + c0) : T(c1 + c0)
123127
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ renderstring!(
168168
valign = :vcenter,
169169
)
170170
renderstring!(zeros(UInt8, 20, 100), "helgo", face, 10, 25, 80)
171+
for str in ("helgo", collect("helgo"))
172+
fcolor = [RGB{Float32}(rand(3)...) for _ 1:length(str)]
173+
renderstring!(zeros(RGB{Float32}, 20, 100), str, face, 10, 0, 0; fcolor = fcolor)
174+
end
171175

172176
# Find fonts
173177
# these fonts should be available on all platforms:

0 commit comments

Comments
 (0)