michaelrommel / nvim-silicon

neovim plugin to create code images using the external silicon tool.
MIT License
110 stars 8 forks source link

Panics on Apple M1, homebrew installed Silicon #10

Closed samleibowitz closed 4 months ago

samleibowitz commented 4 months ago

I'm getting a panic when trying to run :Silicon on an Apple M1. Silicon was installed via brew install silicon. Here's the relevant section of my lazy config:

  {
    "michaelrommel/nvim-silicon",
    lazy = true,
    cmd = "Silicon",
    config = function()
      require("silicon").setup {
        -- Configuration here, or leave empty to use defaults
        font = "FiraCode Nerd Font=34;Apple Color Emoji=34",
      }
    end,
  },

And the stack trace:

silicon returned with: thread 'main' panicked at /Users/brew/Library/Caches/Homebrew/cargo_cache/registry/src/index.crates.io-6f17d22bba15001f/font-kit-0.11.0/src/loaders/freetype.rs:1146:9:
assertion `left == right` failed
  left: 23
 right: 0
stack backtrace:
   0:        0x100ad6d24 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3c8b3da4c3ca3a14
   1:        0x100ae50fc - core::fmt::write::hfb70cbdb2260ac51
   2:        0x100acd5bc - std::io::Write::write_fmt::hc13c5ba5d088bd95
   3:        0x100ad6b60 - std::sys_common::backtrace::print::hb0798cc2b68a4b36
   4:        0x100ac4498 - std::panicking::default_hook::{{closure}}::hb6b09a0c32b10ee5
   5:        0x100ac41f0 - std::panicking::default_hook::hc5c3799b46fe276a
   6:        0x100ac495c - std::panicking::rust_panic_with_hook::h491fddbcf07c6736
   7:        0x100ad71ec - std::panicking::begin_panic_handler::{{closure}}::ha2bc72305b00ceb6
   8:        0x100ad6f2c - std::sys_common::backtrace::__rust_end_short_backtrace::hb9c89d964676cd3d
   9:        0x100ac46f4 - _rust_begin_unwind
  10:        0x100b00560 - core::panicking::panic_fmt::h5de4b603c189570c
  11:        0x100b0084c - core::panicking::assert_failed_inner::hd45aa4f8a466ac39
  12:        0x100af449c - core::panicking::assert_failed::h91175a1e4a2ccd83
  13:        0x100995ef4 - font_kit::loaders::freetype::Font::from_bytes::hd43b61fb026e54aa
  14:        0x100983e3c - font_kit::loader::Loader::from_handle::h38a7484a6226a7a5
  15:        0x100981468 - silicon::font::ImageFont::new::h50f7e2cd410ab680
  16:        0x100959d88 - silicon::formatter::ImageFormatterBuilder<S>::build::h9bf8e00eb98ccb0b
  17:        0x10094cd40 - silicon::config::Config::get_formatter::h2b6150d4abf40c34
  18:        0x1009531ec - silicon::main::h98b9af44201cba62
  19:        0x10095e938 - std::sys_common::backtrace::__rust_begin_short_backtrace::h12d5eed34e761a40
  20:        0x100954860 - std::rt::lang_start::{{closure}}::hdf5105a30688e14d
  21:        0x100ac45e4 - std::panicking::try::h661978b490c2c632
  22:        0x100abccc4 - std::rt::lang_start_internal::h83d8f808a741aebf
  23:        0x10095385c - _main
michaelrommel commented 4 months ago

Hi there,

hm, we need to track down, which parameter gives silicon the problems. Could you try out the new bare minimum config like so:

{
    "michaelrommel/nvim-silicon",
    lazy = true,
    cmd = "Silicon",
    opts = {
        debug = true,
        disable_defaults = true,
        -- command = "/opt/homebrew/bin/silicon",
        -- command = "/Users/rommel/.local/share/mise/installs/rust/latest/bin/silicon"
    }
},

or like you have it in a function:

  {
    "michaelrommel/nvim-silicon",
    lazy = true,
    cmd = "Silicon",
    config = function()
      require("silicon").setup {
        debug = true,
        disable_defaults = true,
        -- command = "/opt/homebrew/bin/silicon",
        -- command = "/Users/rommel/.local/share/mise/installs/rust/latest/bin/silicon"
    }
    end,
  },

Then we can track down one by one, which argument poses the problem. I suppose it is actually the font...

Michael.

samleibowitz commented 4 months ago

Sure thing! Here is the minimum configuration that I was able to get to run without errors:

  {
    "michaelrommel/nvim-silicon",
    lazy = true,
    cmd = "Silicon",
    config = function()
      require("silicon").setup {
        -- Configuration here, or leave empty to use defaults
        --     font = "FiraCode Nerd Font=34;Apple Color Emoji=34",
        -- command = "/Users/sam/.cargo/bin/silicon",
        command = "/opt/homebrew/bin/silicon",
        debug = true,
        disable_defaults = true,
        language = "lua",
        output = "/Users/sam/Desktop/silicon.png",
      }
    end,
  },

That worked with both the homebrew and cargo installed versions.

Interestingly, adding font = "FiraCode Nerd Font=34", also works, but font = "FiraCode Nerd Font=34;Apple Color Emoji=34", does not. It looks to me like there is some special logic in the font-kit library which is supposed to handle Apple Color Emoji font specifically, but maybe it's not working?

unsafe fn reset_freetype_face_char_size(face: FT_Face) {
    // Apple Color Emoji has 0 units per em. Whee!
    let units_per_em = (*face).units_per_EM as i64;
    if units_per_em > 0 {
        assert_eq!(
            FT_Set_Char_Size(face, ((*face).units_per_EM as FT_Long) << 6, 0, 0, 0),
            0
        );
    }
}

If I do silicon -f "FiraCode Nerd Font=34;Apple Color Emoji=34" --output ~/Desktop/silicon.png treesitter.lua at the command line, it also panics, so maybe this is really a font-kit issue?

michaelrommel commented 4 months ago

I am not sure, which font parsing create silicon uses - I though I saw some harfbuzz references. But I agree, it seems to be related to the Emoji Font - there are alwayse some strange things going on, that's why I also settled on the monochrome Emoji font, I referenced in the README.

This should be taken then upstream to the sillicon maintainer, https://github.com/Aloxaf/silicon

If you agree with this analysis, I would be very grateful, if you could close the issue, because I cannot really do anything here.

Thanks for reporting this issue, it certainly is good to understand, where problems come from, highly appreciated!

Have a good time!

Michael.

michaelrommel commented 4 months ago

Just out of curiosity: are there any Emoji characters in the treesitter.lua file, you were trying? Is it panicking on the rendering of a specific glyph or generally, right at the time of initialization? Maybe when we try with a plain text file, we can narrow down the source...

samleibowitz commented 4 months ago

Just out of curiosity: are there any Emoji characters in the treesitter.lua file, you were trying? Is it panicking on the rendering of a specific glyph or generally, right at the time of initialization? Maybe when we try with a plain text file, we can narrow down the source...

Nope! No emoji. But since you asked, I tried putting an emoji in there.

silicon

I'll take a look upstream and see if I can find the right place to open this issue. Thanks for taking a look!