Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #372 from JuliaPlots/sd/bbs
Browse files Browse the repository at this point in the history
improve text bbs
  • Loading branch information
SimonDanisch authored Mar 30, 2020
2 parents 0f48a43 + b8ea6bf commit fd269d3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/basic_recipes/axis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ $(ATTRIBUTES)
showgrid = true,
showticks = true,
showtickmarks = true,
padding = 0.1,
padding = 0.0,

ticks = Theme(

ranges_labels = (automatic, automatic),
formatter = Formatters.plain,

gap = 3,
gap = 1,
title_gap = 3,

linewidth = (1, 1),
Expand All @@ -56,9 +55,10 @@ $(ATTRIBUTES)
align = ((:center, :top), (:right, :center)),
font = lift(dim2, theme(scene, :font)),
),

tickmarks = Theme(
length = (3.0, 3.0),
linewidth = (1,1),
length = (1.0, 1.0),
linewidth = (1, 1),
linecolor = ((:black, 0.4), (:black, 0.2)),
linestyle = (nothing, nothing)
),
Expand Down Expand Up @@ -383,6 +383,7 @@ function draw_titles(
textcolor, textsize, rotation, align, font,
title
)

tickspace_x = maximum(map(yticks) do tick
str = last(tick)
tick_bb = text_bb(str, to_font(tickfont[2]), tick_size[2])
Expand Down Expand Up @@ -411,7 +412,6 @@ function draw_titles(
)
end
end

if title !== nothing
# TODO give title own text attributes
push!(
Expand Down
18 changes: 13 additions & 5 deletions src/layouting/boundingbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ function boundingbox(
text::String, position, textsize;
font = "default", align = (:left, :bottom), rotation = 0.0
)
boundingbox(
return boundingbox(
text, position, textsize,
to_font(font), to_align(align), to_rotation(rotation)
)

end

# function boundingbox(
Expand Down Expand Up @@ -138,6 +137,7 @@ end
# return bb
# end


function boundingbox(
text::String, position, textsize, font,
align, rotation, model = Mat4f0(I)
Expand All @@ -157,12 +157,20 @@ function boundingbox(
ctext_state = iterate(text, text_state)
# TODO fix center + align + rotation
if c != '\r'
pos = if pos_per_char
to_ndim(Vec3f0, position[i], 0.0)
posnd = if pos_per_char
position[i]
else
last_pos = calc_position(last_pos, Point2f0(0, 0), atlas, c, font, scale)
start_pos3d .+ (rotation * to_ndim(Vec3f0, last_pos, 0.0))
advance_x, advance_y = glyph_advance!(atlas, c, font, scale)
without_advance = if c == '\n'
# advance doesn't get added for newlines
last_pos
else
last_pos .- Point2f0(advance_x, 0)
end
start_pos3d .+ (rotation * to_ndim(Vec3f0, without_advance, 0.0))
end
pos = to_ndim(Vec3f0, posnd, 0.0)
s = glyph_scale!(atlas, c, font, scale)
srot = rotation * to_ndim(Vec3f0, s, 0.0)
bb = GeometryTypes.update(bb, pos)
Expand Down
2 changes: 1 addition & 1 deletion src/theming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const minimal_default = Attributes(
axis_type = automatic,
camera = automatic,
limits = automatic,
padding = Vec3f0(0.1),
padding = Vec3f0(0.05),
raw = false
)

Expand Down
23 changes: 17 additions & 6 deletions src/utilities/texture_atlas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,23 @@ function calc_position(glyphs, start_pos, scales, fonts, atlas)
positions = zeros(Point2f0, length(glyphs))
last_pos = Point2f0(start_pos)
s, f = iter_or_array(scales), iter_or_array(fonts)
c1 = first(glyphs)
for (i, (c2, scale, font)) in enumerate(zip(glyphs, s, f))
c2 == '\r' && continue # stupid windows!
b = glyph_bearing!(atlas, c2, font, scale)
positions[i] = last_pos .+ b
last_pos = calc_position(last_pos, start_pos, atlas, c2, font, scale)
iter = enumerate(zip(glyphs, s, f))
next = iterate(iter)
if next !== nothing
(i, (char, scale, font)), state = next
first_bearing = glyph_bearing!(atlas, char, font, scale)
while next !== nothing
(i, (char, scale, font)), state = next
next = iterate(iter, state)
char == '\r' && continue # stupid windows!
# we draw the glyph at the last position we calculated
bearing = glyph_bearing!(atlas, char, font, scale)
# we substract the first bearing, since we want to start at
# startposition without any additional offset!
positions[i] = last_pos .+ bearing .- first_bearing
# then we add the advance for the next glyph to start
last_pos = calc_position(last_pos, start_pos, atlas, char, font, scale)
end
end
return positions
end
Expand Down
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using Test
using GLMakie

# Download reference images from master
MakieGallery.current_ref_version[] = "master"

const MINIMAL = get(ENV, "ABSTRACTPLOTTING_MINIMAL", "false")

Expand Down Expand Up @@ -35,7 +34,7 @@ include("shorthands.jl")
@test scene[Axis].ticks.title_gap[] == 3
scene[Axis].ticks.title_gap = 4
@test scene[Axis].ticks.title_gap[] == 4
@test scene[Axis].tickmarks.length[] == (3, 3)
@test scene[Axis].tickmarks.length[] == (1, 1)
end


Expand Down

0 comments on commit fd269d3

Please sign in to comment.