Check if custom font can display character

前端 未结 6 986
感情败类
感情败类 2020-12-06 17:14

I have a custom font which is displaying the box character. The font I am using does not support all languages apparently. I want to check if the String I am about to displa

6条回答
  •  隐瞒了意图╮
    2020-12-06 17:41

    @nibarius answer works well, though with a different character: "\u2936".

    However, that solution is heavy in memory, cpu and time. I needed a solution for a very small number of characters so I implemented a "quick and dirty" solution by checking only the bounds. It works about 50x times faster, so it is more suitable for my case:

    public boolean isCharGlyphAvailable(char... c) {
          Rect missingCharBounds = new Rect();
          char missingCharacter = '\u2936';
          paint.getTextBounds(c, 0, 1, missingCharBounds); // takes array as argument, but I need only one char.
          Rect testCharsBounds = new Rect();
          paint.getTextBounds(c, 0, 1, testCharsBounds);
          return !testCharsBounds.equals(missingCharBounds);
    }
    

    I should warn that this method is less accurate than @nibarius, and there are some false negatives and false positives, but if you just need to test a few characters in order to be able to fallback to other characters - this solution is great.

提交回复
热议问题