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

fix: include widths.json as a julia file #8

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 1 addition & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
name = "Badges"
uuid = "f8f3bd14-6f44-485b-add6-c2abd0d3421f"
authors = ["Avik Sengupta"]
version = "1.0.0"

[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
version = "1.0.1"

[compat]
JSON3 = "1.0"
julia = "1.3"

[extras]
Expand Down
26 changes: 10 additions & 16 deletions src/Badges.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Badges
using JSON3

include("widths.jl")

export Badge
global const gFontFamily = "font-family='Verdana,Geneva,DejaVu Sans,sans-serif'"
const gFontFamily = "font-family='Verdana,Geneva,DejaVu Sans,sans-serif'"

function roundUpToOdd(x)
x = round(Int, x)
Expand Down Expand Up @@ -263,11 +264,6 @@ function escapeXml(s)
x -> replace(x, '\'' => "'")
end

# Verdana font metrics precalculated from the npm package anafanafo
# =================================================================

global const data = Ref{Any}()
global const em = Ref{Float64}()

"""
`widthOfCharCode(charCode; approx=true)`
Expand All @@ -277,13 +273,13 @@ If `approx` is true, any unknwon character will be measured as 'm'. Otherwise 0.
"""
function widthOfCharCode(charCode; approx=true)
if isControlChar(charCode); return 0.0; end
res = findfirst(data[]) do x
charCode >= x[1] && charCode <= x[2]
res = findfirst(WIDTHS) do x
charCode >= x[1][1] && charCode <= x[1][2]
end
if isnothing(res)
if approx; return em[]; else return 0.0; end
if approx; return EM; else return 0.0; end
else
return data[][res][3]
return WIDTHS[res][2]
end
end

Expand All @@ -294,11 +290,9 @@ widthOf(text::AbstractString; approx=true) = reduce(+, [widthOfCharCode(Int(x),

isControlChar(charCode) = charCode <=31 || charCode == 127

# Verdana font metrics precalculated from the npm package anafanafo
# =================================================================

function __init__()
modulepath = dirname(pathof(Badges))
data[] = JSON3.read(read("$modulepath/widths.json", String))
em[] = widthOfCharCode(Int('m'))
end
const EM = widthOfCharCode(Int('m'))::Float64

end
3 changes: 3 additions & 0 deletions src/widths.jl

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/widths.json

This file was deleted.

Loading