Skip to content

Commit

Permalink
Merge pull request #115 from Kolaru/prime
Browse files Browse the repository at this point in the history
Put primes as superscripts
  • Loading branch information
Kolaru authored Dec 30, 2023
2 parents c8cb679 + 5a27bfd commit 36e2842
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/engine/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ function tex_layout(expr, state)
elseif head == :decorated
core, sub, super = tex_layout.(args, state)

if !isnothing(args[3]) && args[3].head == :primes
super_x = min(hadvance(core), rightinkbound(core)) - 0.1
super_y = 0.1
super_shrink = 1
else
super_x = max(hadvance(core), rightinkbound(core))
super_y = xheight(font_family)
super_shrink = shrink
end

return Group(
[core, sub, super],
Point2f[
Expand All @@ -64,13 +74,10 @@ function tex_layout(expr, state)
# The logic is to have the ink of the subscript starts
# where the ink of the unshrink glyph would
hadvance(core) + (1 - shrink) * leftinkbound(sub),
-0.1
-0.2
),
(
max(hadvance(core), rightinkbound(core)),
xheight(font_family)
)],
[1, shrink, shrink]
( super_x, super_y)],
[1, shrink, super_shrink]
)
elseif head == :delimited
elements = tex_layout.(args, state)
Expand Down Expand Up @@ -168,6 +175,9 @@ function tex_layout(expr, state)
(0, 0)
]
)
elseif head == :primes
primes = [TeXExpr(:char, ''') for _ in 1:only(args)]
return horizontal_layout(tex_layout.(primes, state))
elseif head == :space
return Space(args[1])
elseif head == :spaced
Expand Down
11 changes: 8 additions & 3 deletions src/parser/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ function texparse(tex ; root = TeXExpr(:expr), showdebug = false)
com_str = tex[pos:pos+len-1]
push!(stack, TeXExpr(:command, [com_str]))
conclude_command!!(stack)
elseif token == underscore || token == caret
dec = token == underscore ? :subscript : :superscript
elseif token == underscore || token == caret || token == primes
dec = (token == underscore) ? :subscript : :superscript

if isempty(first(stack).args)
core = TeXExpr(:space, 0.0)
Expand All @@ -173,7 +173,12 @@ function texparse(tex ; root = TeXExpr(:expr), showdebug = false)
end

push!(first(stack), core)
push!(stack, TeXExpr(dec))

if token == primes
core.args[subsuperindex(dec)] = TeXExpr(:primes, len)
else
push!(stack, TeXExpr(dec))
end
elseif token == char
push!(stack, canonical_expr(tex[pos]))
push_down!(stack)
Expand Down
1 change: 1 addition & 0 deletions src/parser/tokenizer.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tex_tokens = [
:char => re".",
:primes => re"'+",
:caret => re"\^",
:underscore => re"_",
:rcurly => re"}",
Expand Down

0 comments on commit 36e2842

Please sign in to comment.