ink icon indicating copy to clipboard operation
ink copied to clipboard

Emojis render with trailing spaces within Boxes

Open quinnvoker opened this issue 6 years ago • 14 comments

Hi there! First time using ink, and I'm really enjoying it so far, but I've run into a snag for my project.

I'm making a small terminal-based board game and want to display emojis inside Box elements, but as the title says, every time I render a Box containing a Text element with emojis inside, any row containing emoji gets one space appended per emoji, breaking the right side of the box, which is otherwise appropriately-sized for its content.

The extra spaces do not seem to appear when emojis are printed in a Text element outside of a Box.

Example code:

const Board = () => {
  return (
    <Box flexDirection='column' alignItems='flex-start'>
      <Box borderStyle='round'>
        <Text>🌊</Text>
      </Box>
      <Box borderStyle='round'>
        <Text>🌊🌊</Text>
      </Box>
      <Box borderStyle='round'>
        <Text>🌊🌊🌊</Text>
      </Box>
    </Box>
  )
};

Example output: Screenshot_20200908_084036

quinnvoker avatar Sep 08 '20 15:09 quinnvoker

Also wanted to mention I'm in a programming bootcamp at the moment and looking for open source projects to contribute to.

If this doesn't sound like a very complicated issue to solve, I'd love to take a crack at it! Some assistance in where to look for the cause of the issue would be appreciated too.

quinnvoker avatar Sep 08 '20 17:09 quinnvoker

So, after a little bit of digging, I hacked render-border to work exclusively with double-width characters by setting the x of the right side of the border to x + width / 2, and got some pleasing results with the same example:

ink-emoji-only

Is there any way we could keep track of how many double-width characters exist in a text string? If so, we could make adjustments to that formula to get to a more robust solution.

quinnvoker avatar Sep 09 '20 03:09 quinnvoker

The more I look into this issue and the pieces involved, the less clear-cut the solution is. While my hack above works with single line boxes (or multi-line boxes with the same number of characters per-line), it falls apart on multi-line boxes with a different numbers of characters per-line, causing lines with fewer characters to have right borders which are offset inwards:

broken-again

This is because the x position of the right side of the border is determined as a uniform count of characters from the left side, based on the computed width of the whole box. As soon as variable-width characters are introduced to this, that position can no longer be handled uniformly, and would instead have to be calculated per-line, requiring what I would imagine to be a lot of rewriting of the border drawing code.

I'd still like to continue trying to find a solution for this, if it's at all feasible. If anyone has any ideas, I'd love to hear them.

quinnvoker avatar Sep 09 '20 18:09 quinnvoker

Edit: The more I think about it, the best solution would be to add some kind of overrideLength property in where you can specify a length of 1 for calculation purposes.

Here's the super annoying reason behind this:

https://hsivonen.fi/string-length/

From the sound of it, V8 itself would need to be fixed. Although, a hack-work-around would be to find all emoji matches in a string, and change the length to 1 would work too. I don't know how performance intensive this would be, but if any given string includes any of these symbols:

https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json

then, the fix would be to individually check the length of that symbol, let's say it's 7, and subtract 6 from what the full length of the string is computed to be. I don't know.

If it is computationally expensive, perhaps adding a new property to <Text like containsEmoji="true" would be helpful! Alternatively, include a new top-level tag that emits the length 1 somehow and contains just 1 emoji within, then no calculations would be needed at all.

ravihugo avatar Sep 13 '20 16:09 ravihugo

That's the quirkiness of some emojis, sometimes they take 2 characters to display for some reason. Not super educated about this subject, but if anyone feels adventurous, go ahead and take a stab a this!

vadimdemedes avatar Sep 19 '20 10:09 vadimdemedes

Hey, I tried to take a stab a few days ago, but I couldn't find in the code where length of a particular "Text" node is output... Do you have any hints?

ravihugo avatar Sep 21 '20 20:09 ravihugo

See https://github.com/vadimdemedes/ink/blob/master/src/measure-text.ts.

vadimdemedes avatar Sep 25 '20 18:09 vadimdemedes

For a temporary workaround, I'm wrapping my emoji in a transform that adds an additional space. It only works if you have the box at a defined width though. I also haven't tried this out with multiple lines inside a single box yet.

const Heart = () => (
	<Transform transform={(output) => output + " "}>
		<Text>❤️ </Text>
	</Transform>
);

geoolson avatar Oct 03 '20 23:10 geoolson

Another workaround if the workaround mentioned by @geoolson doesn't work for the emoji you are trying to use. (for example 🔍)

<Transform transform={(output) => output.slice(0, -1)}>
  <Text>🔍 {text}</Text>
</Transform>

This one also seems to work well with % based width.

molnarmark avatar Oct 30 '20 02:10 molnarmark

I found a fix for this in #444 and it renders correctly without transform workarounds:

image

dundalek avatar May 22 '21 09:05 dundalek

@dundalek this is great! I am having the same issue, but I noticed your PR never passed. Any word on the work to get this incorporated?

doug-shontz avatar Jun 15 '21 19:06 doug-shontz

@vadimdemedes Why are you closing these line of issues without any explanation?

Edit: my bad, watched this using mobile and it looked like 'close' message without related PR

goldbergyoni avatar Mar 20 '22 20:03 goldbergyoni

@goldbergyoni The explanation is provided by the close message: it was closed because PR #444 resolved the issue and was merged.

quinnvoker avatar Mar 21 '22 17:03 quinnvoker

Yes, watched this using mobile and it looked like 'close' message without related PR. My bad. Thanks

goldbergyoni avatar Mar 22 '22 11:03 goldbergyoni