monomer icon indicating copy to clipboard operation
monomer copied to clipboard

Loading fonts from memory

Open klausweiss opened this issue 2 years ago • 3 comments

I was looking into loading fonts from memory in addition to loading from file, in order to be able to easily distribute monomer programs as standalone binaries. Since it isn't possible at the time of writing, would you consider adding this to monomer? I attempted implementing it.

What I have in this PR doesn't work unfortunately. It compiles, but the text doesn't show up (see the labels in the modified todo example below). It's my first time using c2hs, so I must have got something wrong. I'm out of expertise, so I'm calling for help if anyone is interested in doing so.

The todo changes (and an additional dependency) are only there for testing easily. These changes should be reverted when it's finally working.

Screenshot

image

klausweiss avatar Aug 04 '22 17:08 klausweiss

@klausweiss I'll take a look during the weekend.

What I would check first is if the loading functions are returning a valid status code (i.e., they are not failing to load the embedded binary content). I'd also validate that the file embedding is actually happening. Finally, I'd check the chs bindings (I added a comment there).

fjvallarino avatar Aug 05 '22 03:08 fjvallarino

Thanks @fjvallarino!

What I would check first is if the loading functions are returning a valid status code (i.e., they are not failing to load the embedded binary content).

They do. 0, 1, 2, 3, ... for each consecutive call to the C function.

I'd also validate that the file embedding is actually happening.

Confirmed it's working, exactly the same bytes as in the input file.

Finally, I'd check the chs bindings (I added a comment there).

I've made some progress here. I've tried to use what's in nanovg-hs for this bit: https://github.com/cocreature/nanovg-hs/blob/cc8dfa0dc18a0792786c973b4e9a232fa7d3ecfd/src/NanoVG/Internal/FFIHelpers.hs#L24-L26

Now it randomly renders some dots where the text should be on labels:

Screenshot

image

I tried explicitly allocating memory for the bytestring (updated the PR with this code), but the results are same as above - randomly rendered dots.

klausweiss avatar Aug 05 '22 15:08 klausweiss

Ok, I got this. I think it's the nanovg-hs code that might be a bit off. I modified nanovg-hs to use the same trick with allocating memory for the font on my own to prevent it from being garbage-collected:

copyCUStringLenMemory (from, len) =
  let intLen = fromIntegral len
  in do
    to <- mallocBytes intLen
    copyBytes to from intLen
    return (to, len)

and made the NVGcontext free the font upon destruction. The results are shown in the image below (:

Screenshot

image

I'll open a PR to nanovg-hs to fix this later.

klausweiss avatar Aug 05 '22 16:08 klausweiss

I had a play around locally and it works for me :smile:

I wonder if there should be a pointer in the docs to https://hackage.haskell.org/package/file-embed-0.0.15.0/docs/Data-FileEmbed.html or similar (this is what I used and I assume this is intended).

Also there is appWindowIcon that might benefit from something similar, to enable fully self-contained binaries.

Dretch avatar Sep 10 '22 16:09 Dretch

@klausweiss I'm really sorry I missed this. I didn't see you had switched it to a PR ready for review.

I added a couple of comments. The PR looks great!

fjvallarino avatar Sep 10 '22 20:09 fjvallarino

@klausweiss you did mention switching from Draft to Ready for Review in a previous comment, I just missed it :)

All looks good for me. I added a comment about choosing between your original design for the FontDef type or the alternative I proposed and, based on what you decide, I'm ready for merging.

Thanks!

fjvallarino avatar Sep 11 '22 08:09 fjvallarino

All looks good for me. I added a comment about choosing between your original design for the FontDef type or the alternative I proposed and, based on what you decide, I'm ready for merging.

Ok. I'll revert the change for the sake of not having to hand-craft the lens instance and am happy to merge afterwards. Thanks for the assistance!

klausweiss avatar Sep 11 '22 09:09 klausweiss

Thank you for the patience!

fjvallarino avatar Sep 11 '22 10:09 fjvallarino

Also there is appWindowIcon that might benefit from something similar, to enable fully self-contained binaries.

@Dretch sounds good to me, happy to do this in a separate PR. Will need a couple days to find time for that though.

Sorry @Dretch, I didn't get round to doing this eventually. It's free for anyone to pick up.

klausweiss avatar Dec 29 '22 21:12 klausweiss