JuliaGraphics / FreeTypeAbstraction.jl

A Julian abstraction layer over FreeType.jl
Other
25 stars 20 forks source link

Various fixes #71

Closed t-bltg closed 2 years ago

t-bltg commented 2 years ago

Needed for https://github.com/JuliaPlots/UnicodePlots.jl/pull/242.

Visual inspection of bounding boxes on individual characters is tested with the following code:

using FreeTypeAbstraction
using ColorTypes
using FileIO

const black = RGBA(0., 0., 0., 1.)
const white = RGBA(1., 1., 1., 1.)
const red = RGBA(1., 0., 0., 1.)
const green = RGBA(0., 1., 0., 1.)
const blue = RGBA(0., 0., 1., 1.)
const cyan = RGBA(0., 1., 1., 1.)
const yellow = RGBA(1., 1., 0., 1.)
const magenta = RGBA(1., 0., 1., 1.)

main() = begin
  face = findfont("JuliaMono")

  for pixelsize in (16, 32, 64, 128)
    for char in vcat(collect('a':'z'), collect('A':'Z'), collect('0':'9'))
      img = fill(RGBA(RGB(black), 0.), pixelsize, pixelsize)
      renderstring!(
        img, [char], face, pixelsize, pixelsize ÷ 2, pixelsize ÷ 2;
        fcolor=red,
        bcolor=nothing,
        gcolor=RGBA(RGB(green), .5),
        bbox_glyph=RGBA(RGB(blue), .5),
        bbox=RGBA(RGB(black), .5),
        valign=:vcenter,
        halign=:hcenter,
      )
      save("char-$pixelsize-$char.png", img)
    end
  end
end

main()

Updated UnicodePlots plots with this PR are show here: https://github.com/JuliaPlots/UnicodePlots.jl/pull/243/files.

Here is an example using bounding boxes and braille or block characters + background filling (height correctly adjusted per row):

using FreeTypeAbstraction
using ColorTypes
using FileIO

const black = RGBA(0., 0., 0., 1.)
const white = RGBA(1., 1., 1., 1.)
const red = RGBA(1., 0., 0., 1.)
const green = RGBA(0., 1., 0., 1.)
const blue = RGBA(0., 0., 1., 1.)
const cyan = RGBA(0., 1., 1., 1.)
const yellow = RGBA(1., 1., 0., 1.)
const magenta = RGBA(1., 0., 1., 1.)

main() = begin
  pixelsize = 64

  kr = 4 / 3  # aspect ratio
  kc = kr / 2

  paragraph = " █▞▀▄▘▝\nAbcdeF \n⡇⢷⠅ ⣿⠠⡏\nGhiJ kL"
  lines = string.(split(paragraph, '\n'))

  nr = length(lines)
  nc = maximum(length.(lines))
  font = "DejaVu Sans"

  img = fill(RGBA(RGB(black), 0.), ceil(Int, (nr + 2) * kr * pixelsize), ceil(Int, (nc + 2) * kc * pixelsize))
  face = findfont(font)

  y0 = round(Int, 2 * kr * pixelsize)
  x0 = pixelsize ÷ 4
  for (r, line) in enumerate(lines)
    y = round(Int, y0 + (kr * pixelsize) * (r - 1))
    renderstring!(
      img, line, face, pixelsize, y, x0;
      fcolor=red,
      bcolor=nothing,
      gcolor=RGBA(RGB(green), .5),
      bbox_glyph=RGBA(RGB(blue), .5),
      bbox=RGBA(RGB(black), .5),
    )
  end
  save("multiple.png", img)
end

main()

multiple

codecov[bot] commented 2 years ago

Codecov Report

Merging #71 (4e381e7) into master (cdcddd0) will increase coverage by 15.35%. The diff coverage is 96.03%.

@@             Coverage Diff             @@
##           master      #71       +/-   ##
===========================================
+ Coverage   75.90%   91.26%   +15.35%     
===========================================
  Files           6        6               
  Lines         303      332       +29     
===========================================
+ Hits          230      303       +73     
+ Misses         73       29       -44     
Impacted Files Coverage Δ
src/types.jl 83.83% <80.00%> (+15.87%) :arrow_up:
src/findfonts.jl 93.65% <88.88%> (+3.02%) :arrow_up:
src/layout.jl 97.67% <100.00%> (+45.40%) :arrow_up:
src/rendering.jl 97.32% <100.00%> (+3.81%) :arrow_up:
src/FreeTypeAbstraction.jl 100.00% <0.00%> (+30.00%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 53b4655...4e381e7. Read the comment docs.

t-bltg commented 2 years ago

@SimonDanisch, if you have 5min to review, it'd be appreciated ...

t-bltg commented 2 years ago

So, I'm failing to understand all the subtleties of the previous bearing computations and why this was chosen, but the examples given in https://github.com/JuliaGraphics/FreeTypeAbstraction.jl/pull/71#issue-1181220071 now work without changing the bearing definition :shrug:, and I'm fine with that.

Code style have been (hopefully) reverted, minus fixing the code style inconsistencies.

SimonDanisch commented 2 years ago

Alright thanks a lot, this works with Makie! :)

t-bltg commented 2 years ago

Thank you very much for your time, saving correct pngs is a nice improvement for UnicodePlots ;)