Skip to content

Commit

Permalink
Fix differences in glyphs positions used by FontManager and nanovg (#105
Browse files Browse the repository at this point in the history
)

* Use ignored dpr argument in makeFontManager

* Add scaling factor to FontManager initialization to attempt to match that of nanovg's nvg__getFontScale

* Set adjustedDpr to 4; it was the only possible outcome from the previous formula

* Update Changelog
  • Loading branch information
fjvallarino committed Apr 2, 2022
1 parent 4b32da8 commit b80e2ca
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Properly handle `SetFocusOnKey` for `textArea` ([#80](https://github.com/fjvallarino/monomer/issues/80)).
- Lens tutorial sample code ([PR #95](https://github.com/fjvallarino/monomer/pull/95) and [PR #98](https://github.com/fjvallarino/monomer/pull/98)). Thanks @Clindbergh!
- ColorPicker's numericFields vertical alignment ([PR #108](https://github.com/fjvallarino/monomer/pull/108)).
- Differences in glyphs positions used by FontManager and nanovg ([PR #105](https://github.com/fjvallarino/monomer/pull/105)).

### Added

Expand All @@ -25,8 +26,8 @@
in `handleEvent` to know when the model changed. Widgets that want to report model changes to its parent can
use `Report`/`RequestParent`; an example can be found in `ColorPicker` ([PR #71](https://github.com/fjvallarino/monomer/pull/71)).
- The `keystroke` widget now supports the `Backspace` key ([PR #74](https://github.com/fjvallarino/monomer/pull/74)).
- `Timestamp` is now a newtype. Enforce use of this type instead of `Int` when appropriate ([PR #103](https://github.com/fjvallarino/monomer/pull/103)).
- `style...` family of functions now combine new attributes with the existing ones ([PR #104](https://github.com/fjvallarino/monomer/pull/104)).
- `Timestamp` is now a newtype. Enforce use of this type instead of `Int` when appropriate ([PR #103](https://github.com/fjvallarino/monomer/pull/103)).
- `Timestamp` was renamed to `Millisecond`. The rationale is that since both timestamps and durations are used frequently in calculations (and in the context of Monomer timestamps and durations indeed represent time in milliseconds), having separate types for Timestamp and Duration caused more harm than good ([PR #107](https://github.com/fjvallarino/monomer/pull/107)).

### Renamed
Expand Down
15 changes: 12 additions & 3 deletions src/Monomer/Graphics/FontManager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ makeFontManager
-> Double -- ^ The device pixel rate.
-> IO FontManager -- ^ The created renderer.
makeFontManager fonts dpr = do
ctx <- fmInit 1 --dpr
{-
Awful fix/workaround to account for rounding errors/differences in how scaling
is performed. The nvg__getFontScale function calculates a scale based on
internal state and seems to be the source of the differences.
This should be reviewed/improved.
-}
let adjustedDpr = 4

ctx <- fmInit adjustedDpr

validFonts <- foldM (loadFont ctx) [] fonts

when (null validFonts) $
putStrLn "Could not find any valid fonts. Text size calculations will fail."
return $ newManager ctx dpr

return $ newManager ctx adjustedDpr

newManager :: FMContext -> Double -> FontManager
newManager ctx dpr = FontManager {..} where
Expand Down
4 changes: 4 additions & 0 deletions src/Monomer/Graphics/NanoVGRenderer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ makeRenderer fonts dpr = do

newRenderer :: VG.Context -> Double -> IORef Env -> Renderer
newRenderer c rdpr envRef = Renderer {..} where
{-
rdpr is used to let nanovg know the real device pixel rate.
dpr is set to 1 to disable all NanoVGRenderer internal calculations.
-}
dpr = 1

beginFrame w h = do
Expand Down

0 comments on commit b80e2ca

Please sign in to comment.