0

I have some code I'm using to detect whether a given font supports a set of characters by rendering the character and checking its width. In the process I discovered width for a space character is 0. Can someone tell me why?

val pp = Paint()
val rr = Rect()
pp.getTextBounds(" ", 0, 1, rr)
"$rr"
> Rect(0, 0 - 0, 0)

The full code for the function is here.

steven smith
  • 1,519
  • 15
  • 31
  • Isn't the default text size for a `Paint` super tiny? What happens if you set a reasonable text size? – Mike M. Feb 17 '23 at 18:31
  • Other characters work ok. As far as I can see, only space is giving me this. I discovered this behaviour in the function I mentioned for searching for non-printable characters in a font and noticed unexpected behaviour which I tracked down to this. It could be that space is considered "non-printable" but I'd like to know that. It could also be that the fonts I'm using are Japanese and many of their characters do their own spacing. But I'd like to understand... – steven smith Feb 17 '23 at 18:40
  • Hmm, yeah, seems it's not nearly as tiny as I thought it was. In my first quick tests here, it looks like `getTextBounds()` might trim whitespace, but I'll need to investigate further to be certain. Looks like `measureText()` still works as expected, though, if that'll help. – Mike M. Feb 17 '23 at 18:46
  • Unfortunately, measureText gives same value even for fonts where a character won't render. I'm using some "Hand Written" style fonts for a japanese flash card program. For an Aoyama font, a Log statement gives me: "阝: rect:Rect(0, 0 - 0, 0): width:12: but for the system default I get "阝: rect:Rect(1, -11 - 7, 2): width:12". Aoyama can't render the char but the default font can. Thank you for the idea though. – steven smith Feb 17 '23 at 19:26
  • P.S. for now I just return ok if the char is a " ". – steven smith Feb 17 '23 at 19:29
  • Yeah, a special case might be the only way to handle it, if I'm understanding you correctly. FWIW, I found [this response on the issue tracker](https://issuetracker.google.com/issues/36916097#comment2) that confirms that `getTextBounds()` basically trims whitespace. I'm not sure if they ever did update the docs, but it's not very clear there still. – Mike M. Feb 17 '23 at 19:33
  • That's what I'm seeing and I guess it makes some sense in that a space isn't really printed. Maybe "answer" to that effect and I'll accept it. Many thanks! – steven smith Feb 17 '23 at 19:41
  • Oh, I'm good. Nothin' huge. Please feel free to finish this up however you like. Thanks, though. I appreciate the offer. Cheers! – Mike M. Feb 17 '23 at 19:44

1 Answers1

0

Thanks to Mike in the comments. This is normal behaviour for getTextBounds which treats the space as a non-printed character.

steven smith
  • 1,519
  • 15
  • 31