Created
July 19, 2021 09:36
-
-
Save Moulberry/c93060046012890732dca42dd90f7920 to your computer and use it in GitHub Desktop.
Negative Font Util 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FontOffsetUtil { | |
private static final char MINUS_START = '\uF800'; | |
private static final char PLUS_START = '\uF820'; | |
private static final char MINUS_POW_START = '\uF808'; | |
private static final char PLUS_POW_START = '\uF828'; | |
public static String getOffset(int offset) { | |
//If no offset, return empty string | |
if(offset == 0) return ""; | |
int abs = Math.abs(offset); | |
if(abs <= 8) { | |
//If magnitude <8, return the character directly | |
return ""+(char)((offset < 0 ? MINUS_START : PLUS_START) + abs); | |
} else { | |
//If magnitude >8, use closest power of two and recursively call getOffset | |
//Find power of two - 3 | |
double logBaseTwo = Math.log(abs)/Math.log(2) - 3; | |
//Find offset from MINUS_POW_START or PLUS_POW_START of character to be used | |
int index = (int)Math.round(logBaseTwo); | |
//Find the amount offset has changed by given this power of two | |
int delta = (int)Math.round(Math.copySign(Math.pow(2, index+3), offset)); | |
char c = (char)((offset < 0 ? MINUS_POW_START : PLUS_POW_START) + index); | |
//Offset should now be closer to 0, call getOffset again | |
return c + getOffset(offset - delta); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment