Skip to content

Commit

Permalink
Merge branch 'master' into ff/3D_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Jul 20, 2023
2 parents 1d4a3d4 + f7635a5 commit 33af4db
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
11 changes: 11 additions & 0 deletions ReferenceTests/src/tests/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ end
for (i, marker) in enumerate(markers)
scatter!(Point2f.(1:5, i), marker = marker, markersize = range(10, 30, length = 5), color = :black)
scatter!(Point2f.(1:5, i), markersize = 4, color = :white)

# # Debug - show bbox outline
# if !(marker isa Char)
# scene = Makie.get_scene(ax)
# bb = Makie.bbox(Makie.DEFAULT_MARKER_MAP[marker])
# w, h = widths(bb)
# ox, oy = origin(bb)
# xy = map(pv -> Makie.project(pv, Vec2f(widths(pixelarea(scene)[])), Point2f(5, i)), scene.camera.projectionview)
# bb = map(xy -> Rect2f(xy .+ 30 * Vec2f(ox, oy), 30 * Vec2f(w, h)), xy)
# lines!(bb, linewidth = 1, color = :orange, space = :pixel, linestyle = :dash)
# end
end

f
Expand Down
7 changes: 4 additions & 3 deletions src/basic_recipes/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,13 @@ function process_rt_node!(stack, lines, s::String, _)
x = 0
push!(lines, GlyphInfo[])
else
gi = FreeTypeAbstraction.glyph_index(gs.font, char)
gext = GlyphExtent(gs.font, char)
bestfont = find_font_for_char(char, gs.font)
gi = FreeTypeAbstraction.glyph_index(bestfont, char)
gext = GlyphExtent(bestfont, char)
ori = Point2f(x, y)
push!(lines[end], GlyphInfo(
gi,
gs.font,
bestfont,
ori,
gext,
gs.size,
Expand Down
18 changes: 13 additions & 5 deletions src/bezier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,24 @@ function render_path(path, bitmap_size_px = 256)
# freetype has no ClosePath and EllipticalArc, so those need to be replaced
path_replaced = replace_nonfreetype_commands(path)

aspect = widths(bbox(path)) / maximum(widths(bbox(path)))
path_unit_rect = fit_to_bbox(path_replaced, Rect2f(Point2f(0), aspect))
# Minimal size that becomes integer when mutliplying by 64 (target size for
# atlas). This adds padding to avoid blurring/scaling factors from rounding
# during sdf generation
path_size = widths(bbox(path)) / maximum(widths(bbox(path)))
w = ceil(Int, 64 * path_size[1])
h = ceil(Int, 64 * path_size[2])
path_size = Vec2f(w, h) / 64f0

path_unit_rect = fit_to_bbox(path_replaced, Rect2f(Point2f(0), path_size))

path_transformed = Makie.scale(path_unit_rect, scale_factor)

outline_ref = make_outline(path_transformed)

# Adjust bitmap size to match path aspect
w = ceil(Int, bitmap_size_px * aspect[1])
h = ceil(Int, bitmap_size_px * aspect[2])
# Adjust bitmap size to match path size
w = ceil(Int, bitmap_size_px * path_size[1])
h = ceil(Int, bitmap_size_px * path_size[2])

pitch = w * 1 # 8 bit gray
pixelbuffer = zeros(UInt8, h * pitch)
bitmap_ref = Ref{FT_Bitmap}()
Expand Down
4 changes: 4 additions & 0 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,7 @@ end
to_color(sampler::ShaderAbstractions.Sampler) = el32convert(sampler)

assemble_colors(::ShaderAbstractions.Sampler, color, plot) = Observable(el32convert(color[]))

# BUFFER OVERLOAD

GeometryBasics.collect_with_eltype(::Type{T}, vec::ShaderAbstractions.Buffer{T}) where {T} = vec
34 changes: 28 additions & 6 deletions src/utilities/texture_atlas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function Base.show(io::IO, atlas::TextureAtlas)
println(io, " font_render_callback: ", length(atlas.font_render_callback))
end

const SERIALIZATION_FORMAT_VERSION = "v2"
const SERIALIZATION_FORMAT_VERSION = "v3"

# basically a singleton for the textureatlas
function get_cache_path(resolution::Int, pix_per_glyph::Int)
Expand Down Expand Up @@ -481,12 +481,15 @@ function bezierpath_pad_scale_factor(atlas::TextureAtlas, bp)
full_pixel_size_in_atlas = uv_width * Vec2f(size(atlas))
# left + right pad - cutoff from pixel centering
full_pad = 2f0 * atlas.glyph_padding - 1
return full_pad ./ (full_pixel_size_in_atlas .- full_pad)
# size without padding
unpadded_pixel_size = full_pixel_size_in_atlas .- full_pad
# See offset_bezierpath
return full_pixel_size_in_atlas ./ maximum(unpadded_pixel_size)
end

function marker_scale_factor(atlas::TextureAtlas, path::BezierPath)
# padded_width = (unpadded_target_width + unpadded_target_width * pad_per_unit)
return (1f0 .+ bezierpath_pad_scale_factor(atlas, path)) .* widths(Makie.bbox(path))
# See offset_bezierpath
return bezierpath_pad_scale_factor(atlas, path) * maximum(widths(bbox(path)))
end

function rescale_marker(atlas::TextureAtlas, pathmarker::BezierPath, font, markersize)
Expand All @@ -510,9 +513,28 @@ function rescale_marker(atlas::TextureAtlas, char::Char, font, markersize)
end

function offset_bezierpath(atlas::TextureAtlas, bp::BezierPath, markersize::Vec2, markeroffset::Vec2)
# - wh = widths(bbox(bp)) is the untouched size of the given bezierpath
# - full_pixel_size_in_atlas is the size of the signed distance field in the
# texture atlas. This includes glyph padding
# - px_size is the size of signed distance field without padding
# To correct scaling on glow, stroke and AA widths in GLMakie we need to
# keep the aspect ratio of the aspect ratio (somewhat) correct when
# generating the sdf. This results in direct proportionality only for the
# longer dimension of wh and px_size. The shorter side becomes inaccurate
# due to integer rounding issues.
# 1. To calculate the width we can use the ratio of the proportional sides
# scale = maximum(wh) / maximum(px_size)
# to scale the padded_size we need to display
# scale * full_pixel_size_in_atlas
# (Part of this is moved to bezierpath_pad_scale_factor)
# 2. To calculate the offset we can simple move to the center of the bezier
# path and consider that the center of the final marker. (From the center
# scaling should be equal in ±x and ±y direction respectively.)

bb = bbox(bp)
pad_offset = origin(bb) .- 0.5f0 .* bezierpath_pad_scale_factor(atlas, bp) .* widths(bb)
return markersize .* pad_offset
scaled_size = bezierpath_pad_scale_factor(atlas, bp) * maximum(widths(bb))
return markersize * (origin(bb) .+ 0.5f0 * widths(bb) .- 0.5f0 .* scaled_size)

end

function offset_bezierpath(atlas::TextureAtlas, bp, scale, offset)
Expand Down

0 comments on commit 33af4db

Please sign in to comment.