Skip to content

Commit

Permalink
Introduce Meshes for bounding box computations
Browse files Browse the repository at this point in the history
  • Loading branch information
serenity4 committed Nov 25, 2024
1 parent 179c47f commit 74f72d6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
GeometryExperiments = "1147bd75-18e1-436d-adda-d58e854f897d"
HarfBuzz_jll = "2e76f6c2-a576-52d4-95c1-20adfe4de566"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
SwapStreams = "0b152c4a-7e29-418b-9258-223db38db9d9"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
BinaryParsingTools = "0.1"
Expand All @@ -29,6 +31,7 @@ Reexport = "1"
StaticArrays = "1"
StyledStrings = "1"
SwapStreams = "0.2"
Unitful = "1.21.0"
julia = "1.11"

[extras]
Expand Down
4 changes: 4 additions & 0 deletions src/OpenType.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ using StaticArrays
using Reexport
using HarfBuzz_jll: libharfbuzz
using .Meta: isexpr
using Meshes: Box
using Unitful: @u_str

const cm = u"cm"

const Vec = SVector
const Vec2 = SVector{2,Float64}
Expand Down
4 changes: 2 additions & 2 deletions src/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function segment_geometry(line::Line, segment::LineSegment)
xmin, _ = line.positions[first(segment.indices)]
width = sum(first, @view line.advances[segment.indices]; init = 0.0)
xmax = xmin + width
Box(Point2(xmin, ymin), Point2(xmax, ymax))
Box((xmin, ymin) .* cm, (xmax, ymax) .* cm)
end

function line_geometry(line::Line)
Expand All @@ -267,7 +267,7 @@ function line_geometry(line::Line)
ymax = ascender(line)
width = sum(first, line.advances; init = 0.0)
xmax = xmin + width
Box(Point2(xmin, ymin), Point2(xmax, ymax))
Box((xmin, ymin) .* cm, (xmax, ymax) .* cm)
end

function has_outlines(line::Line, segment::LineSegment)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using OpenType, Test
using OpenType: Text, lines, extract_style_from_text, CharacterStyle, hb_shape, Vec, Vec2, cm
using BinaryParsingTools
using GeometryExperiments: Point2
using Accessors: @set, @reset
using StyledStrings
using Colors
using Meshes: Point, coords

include("utils.jl")

Expand Down
7 changes: 4 additions & 3 deletions test/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@
@test sprint(show, MIME"text/plain"(), segment) isa String
@test segment.indices == eachindex(t.chars)
box = text_geometry(t, [font => options])
@test box.min === Point2(0, 0)
@test 1.82 < box.max[1] < 1.83
@test 0.096 < box.max[2] < 0.097
@test box.min == Point(0, 0)
cs = coords(box.max)
@test 1.82cm < cs.x < 1.83cm
@test 0.096cm < cs.y < 0.097cm

t = Text(styled"The {bold:brown} {red:fox {italic:jumps}} over the {italic:lazy} dog.", TextOptions())
box2 = text_geometry(t, [font => options])
Expand Down

0 comments on commit 74f72d6

Please sign in to comment.