genFontSizes.uts 860 B

123456789101112131415161718192021222324252627282930
  1. import { FontSize } from './interface';
  2. export function getLineHeight(fontSize : number) : number {
  3. return (fontSize + 8) / fontSize;
  4. }
  5. // https://zhuanlan.zhihu.com/p/32746810
  6. export function getFontSizes(base : number) : FontSize[] {
  7. const length = 11 // 10
  8. const offset = 2 // 1
  9. // #ifdef APP-ANDROID
  10. const arr = Array.fromNative(new IntArray(length.toInt()));
  11. // #endif
  12. // #ifndef APP-ANDROID
  13. const arr = Array.from({ length });
  14. // #endif
  15. const fontSizes = arr.map((_, index) : number => {
  16. const i = index - offset;
  17. const baseSize = base * Math.pow(Math.E, i / 5);
  18. const intSize = index > 1 ? Math.floor(baseSize) : Math.ceil(baseSize);
  19. // Convert to even
  20. return Math.floor(intSize / 2) * 2;
  21. });
  22. fontSizes[offset] = base;
  23. return fontSizes.map((size) : FontSize => ({
  24. size,
  25. lineHeight: getLineHeight(size),
  26. } as FontSize));
  27. }