Skip to content

Commit

Permalink
MeasureText: search for the best font size that snaps to the hardware…
Browse files Browse the repository at this point in the history
… pixels
  • Loading branch information
Yatao Li committed Jul 29, 2019
1 parent 7bf9448 commit 09f8531
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
8 changes: 4 additions & 4 deletions ViewModels/EditorViewModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ type EditorViewModel(GridId: int, ?parent: EditorViewModel, ?_gridsize: GridSize
font_size <- max font_size 1.0
// It turns out the space " " advances farest...
// So we measure it as the width.
let w, h = MeasureText(" ", _guifont, _guifontwide, font_size)
// Snap to device pixels
let _s = Point(float w, floor(float h * grid_scale)/grid_scale)
glyph_size <- Size(_s.X, _s.Y)
let s, w, h = MeasureText(" ", _guifont, _guifontwide, font_size, grid_scale)
glyph_size <- Size(w, h)
font_size <- s

trace "fontConfig: guifont=%s guifontwide=%s size=%A" _guifont _guifontwide glyph_size
this.cursorConfig()
markAllDirty()
Expand Down
28 changes: 24 additions & 4 deletions ui.fs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module ui =
| CharType.Powerline -> nerd_typeface
| _ -> _get font

let MeasureText (str: string, font: string, wfont: string, fontSize: float) =
let MeasureText (str: string, font: string, wfont: string, fontSize: float, scaling: float) =
use paint = new SKPaint()
paint.Typeface <- GetTypeface(str, false, false, font, wfont)
paint.TextSize <- single fontSize
Expand All @@ -271,10 +271,30 @@ module ui =
paint.DeviceKerningEnabled <- false
paint.TextEncoding <- SKTextEncoding.Utf16

let w = paint.MeasureText str
let h = paint.FontSpacing
let mutable score = 999999999999.0
let mutable s = fontSize
let mutable w = 0.0
let mutable h = 0.0

w, h
for sizeStep = -50 to 50 do
let s' = fontSize + float(sizeStep) * 0.01
paint.TextSize <- single s'

let w' = float(paint.MeasureText str)
let h' = round(float(paint.FontSpacing) * scaling) / scaling

// calculate score
let score' =
abs(w' * scaling - round(w' * scaling)) +
abs(h' * scaling - round(float(paint.FontSpacing) * scaling))

if score' < score then
score <- score'
w <- w'
h <- h'
s <- s'

s, w, h

let AllocateFramebuffer w h scale =
let pxsize = PixelSize(int <| (w * scale), int <| (h * scale))
Expand Down

0 comments on commit 09f8531

Please sign in to comment.