Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jk/glyph indices #2139

Merged
merged 25 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cf3f228
switch from chars to Culong glyph indices
jkrumbiegel Jul 14, 2022
e63259a
save glyph indices in glyphcollection
jkrumbiegel Jul 14, 2022
7803e88
draw glyphs by index in cairomakie
jkrumbiegel Jul 14, 2022
a39f3bf
remove new path
jkrumbiegel Jul 14, 2022
c6ffc78
add stub for MathTeXEngine
jkrumbiegel Jul 14, 2022
c3f0246
switch atlas etc to UInt64 glyph indices
jkrumbiegel Jul 16, 2022
800228f
remove show
jkrumbiegel Jul 16, 2022
f2e619c
Update CairoMakie/src/primitives.jl
jkrumbiegel Jul 18, 2022
9b271ab
remove deleted function
jkrumbiegel Jul 18, 2022
3c995c7
bump compat
jkrumbiegel Jul 19, 2022
3ab03c1
switch to FreeTypeAbstraction.glyph_index
jkrumbiegel Jul 19, 2022
5ca39fb
remove show [skip-ci]
jkrumbiegel Jul 19, 2022
ac024bc
Merge branch 'master' into jk/glyph-indices
jkrumbiegel Jul 22, 2022
96ecd72
add mathtexengine branch to ci everywhere
jkrumbiegel Jul 22, 2022
f34ae29
Merge branch 'master' into jk/glyph-indices
jkrumbiegel Aug 16, 2022
acb227e
remove special mathtex branch from ci
jkrumbiegel Aug 21, 2022
b86d574
bump wglmakie freetypeabstraction
jkrumbiegel Aug 21, 2022
4b3aab3
fix test
jkrumbiegel Aug 21, 2022
2b444dc
use represented_char for word wrapping logic
jkrumbiegel Aug 22, 2022
9ec448e
Merge branch 'master' into jk/glyph-indices
jkrumbiegel Aug 22, 2022
660cc41
Merge branch 'master' into jk/glyph-indices
jkrumbiegel Aug 24, 2022
d210568
reintroduce '\n' render hack
jkrumbiegel Aug 24, 2022
371aa5d
remove redundant glyph_index()
jkrumbiegel Aug 24, 2022
15ff283
revert autoformatter changes
jkrumbiegel Aug 24, 2022
63648cd
flip wrong argument order
jkrumbiegel Aug 24, 2022
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
27 changes: 21 additions & 6 deletions CairoMakie/src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,15 @@ function draw_glyph_collection(scene, ctx, position, glyph_collection, rotation,
)

Cairo.save(ctx)
Cairo.move_to(ctx, glyphpos...)
set_font_matrix(ctx, mat)
Cairo.show_text(ctx, string(glyph))
# show_text makes an implicite move_to at the end, which starts a new one point path.
# `new_path` clears that path so it doesn't end up as an artifact in the next stroke call
Cairo.new_path(ctx)
show_glyph(ctx, glyph, glyphpos...)
Cairo.restore(ctx)

if strokewidth > 0 && strokecolor != RGBAf(0, 0, 0, 0)
Cairo.save(ctx)
Cairo.move_to(ctx, glyphpos...)
set_font_matrix(ctx, mat)
Cairo.text_path(ctx, string(glyph))
glyph_path(ctx, glyph, glyphpos...)
Cairo.set_source_rgba(ctx, rgbatuple(strokecolor)...)
Cairo.set_line_width(ctx, strokewidth)
Cairo.stroke(ctx)
Expand All @@ -453,6 +449,25 @@ function draw_glyph_collection(scene, ctx, position, glyph_collection, rotation,
return
end

struct CairoGlyph
index::Culong
x::Cdouble
y::Cdouble
end

function show_glyph(ctx, glyph::Culong, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_show_glyphs, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
Comment on lines +460 to +462
Copy link
Member

@asinghvi17 asinghvi17 Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case you might use cairo_show_text_glyphs, which allow text to be highlighted. We had glyph support earlier but AFAIK the text highlighting issue is why we switched away from it. It would also allow the whole GlyphCollection to be rendered at once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point I don't have the utf 8 anymore and it's not guaranteed that there is any, as for Mathtexengine

end
function glyph_path(ctx, glyph::Culong, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_glyph_path, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
end

################################################################################
# Heatmap, Image #
################################################################################
Expand Down
38 changes: 6 additions & 32 deletions src/basic_recipes/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function texelems_and_glyph_collection(str::LaTeXString, fontscale_px, halign, v
scales_2d = [Vec2f(x[3] * Vec2f(fs)) for x in els]

texchars = [x[1] for x in els]
chars = [texchar.char for texchar in texchars]
glyphindices = [glyph_index(texchar) for texchar in texchars]
jkrumbiegel marked this conversation as resolved.
Show resolved Hide resolved
fonts = [texchar.font for texchar in texchars]
extents = GlyphExtent.(texchars)

Expand Down Expand Up @@ -229,38 +229,8 @@ function texelems_and_glyph_collection(str::LaTeXString, fontscale_px, halign, v
positions = basepositions .- Ref(shift)
positions .= Ref(rot) .* positions

# # we replace VLine and HLine with characters that are specifically scaled and positioned
# # such that they match line length and thickness
# for (el, position, _) in all_els
# el isa MathTeXEngine.VLine || el isa MathTeXEngine.HLine || continue
# if el isa MathTeXEngine.HLine
# w, h = el.width, el.thickness
# else
# w, h = el.thickness, el.height
# end
# font = to_font("TeX Gyre Heros Makie")
# c = el isa MathTeXEngine.HLine ? '_' : '|'
# fext = get_extent(font, c)
# inkbb = FreeTypeAbstraction.inkboundingbox(fext)
# w_ink = width(inkbb)
# h_ink = height(inkbb)
# ori = inkbb.origin

# char_scale = Vec2f(w / w_ink, h / h_ink) * fs

# pos_scaled = fs * Vec2f(position)
# pos_inkshifted = pos_scaled - char_scale * ori - Vec2f(0, h_ink / 2) # TODO fix for VLine
# pos_final = rot * Vec3f((pos_inkshifted - Vec2f(shift[Vec(1, 2)]))..., 0)

# push!(positions, pos_final)
# push!(chars, c)
# push!(fonts, font)
# push!(extents, GlyphExtent(font, c))
# push!(scales_2d, char_scale)
# end

pre_align_gl = GlyphCollection(
chars,
glyphindices,
fonts,
Point3f.(positions),
extents,
Expand All @@ -274,4 +244,8 @@ function texelems_and_glyph_collection(str::LaTeXString, fontscale_px, halign, v
all_els, pre_align_gl, Point2f(xshift, yshift)
end

function glyph_index(t::TeXChar)
error("Correct glyph index needs to be picked on MathTeXEngine's side and returned here")
end

iswhitespace(l::LaTeXString) = iswhitespace(replace(l.s, '$' => ""))
6 changes: 5 additions & 1 deletion src/layouting/layouting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function glyph_collection(
# interactive features that need to know where characters begin and end
per_char(attr) = collect(attribute_per_char(str, attr)) # attribute_per_char returns generators
return GlyphCollection(
[x.char for x in charinfos],
[glyph_index(x.char, x.font) for x in charinfos],
[x.font for x in charinfos],
reduce(vcat, charorigins),
[x.extent for x in charinfos],
Expand All @@ -269,6 +269,10 @@ function glyph_collection(
)
end

function glyph_index(char::Char, font)
FreeType.FT_Get_Char_Index(font, char)
end

# function to concatenate vectors with a value between every pair
function padded_vcat(arrs::AbstractVector{T}, fillvalue) where T <: AbstractVector{S} where S
n = sum(length.(arrs))
Expand Down
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ end
Stores information about the glyphs in a string that had a layout calculated for them.
"""
struct GlyphCollection
glyphs::Vector{Char}
glyphs::Vector{Culong}
fonts::Vector{FTFont}
origins::Vector{Point3f}
extents::Vector{GlyphExtent}
Expand Down