Is there a way to programmatically determine whether a given unicode character will render as tofu in some context in Emacs?

Some bug-flavored (U+1F41B) tofu renders in a face using DejaVu Sans Mono:

helm-unicode display of U+1F41B

  • 1
    Seems like this is up to the font, not Emacs. Mar 13 '17 at 20:12
  • This is not quite correct, it's also about what fonts Emacs picked for representing ranges of Unicode space, after that comes whether the font can represent it.
    – wasamasa
    Mar 13 '17 at 20:18
  • 2
    What on earth does "tofu" mean in the context of fonts? No soy curds in sight here...
    – phils
    Mar 14 '17 at 21:42
  • 1
    @phils the tiny squares (which sometimes contain the character id) that that are rendered in place of an unavailable character glyph apparently resemble the cuboid vegetarian staple (i.e. they're both rectangles)
    – ebpa
    Mar 14 '17 at 22:02

Like Tianxiang said this is a question of the font being used. Emacs still allows some introspection in that respect.

The internal-char-font function allows you to see if a character is defined in the current font. There is a comment describing the function in font.c. It says

It returns nil in the following cases: (1) The window system doesn't have a font for the character (thus it is displayed by an empty box).

That means that you can do something like:

(internal-char-font nil (string-to-char "\u0034"))

to see if the character represented by "\u0034" has a character in the current font

  • That does the trick! I'd initially walked on by that function on account of subr mystery-meat "For internal use only.".
    – ebpa
    Mar 13 '17 at 23:26
  • I haven't read through the whole function so I'm not sure what it does that would make it "For internal use only"... but hey if it works!
    – Jules
    Mar 13 '17 at 23:59
  • I can't imagine there are side-effects. Probably just avoiding a private method accidentally becoming a permanent API fixture.
    – ebpa
    Mar 14 '17 at 0:08
  • Also note that you can get the int value of a character with ?<char>. So you can see if you can render "😋" by running (internal-char-font nil ?😋) in ielm or similar. This is particularly handy if you just want to paste a character or put it in your configuration.
    – Archenoth
    Mar 14 '17 at 16:59
  • Oh, and if you are iffy about using an internal function, you can also use (describe-char-display nil ?😋) to the same effect.
    – Archenoth
    Mar 14 '17 at 17:08

The function char-displayable-p was added for that kind of purpose, but I'm not sure it does what it purports to in all cases. If it doesn't work for you, please report it as a bug.

  • 1
    Ooh! That's a nice addition! Though it appears as though it can still currently return 'unicode for Emoji that render as tofu. On my box, (char-displayable-p ?🆕) does this.
    – Archenoth
    Mar 14 '17 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.