Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import * as fs from 'fs'
  2.  
  3. import {Buf} from './buf'
  4.  
  5. class Font {
  6. header: UcsFontHeader
  7. // blocks:
  8. }
  9.  
  10. class UcsFontHeader {
  11. magic: string // char[16]
  12. size: number // uint Total font size
  13. blocks: number // uint
  14.  
  15. constructor() {
  16. this.magic = ''
  17. this.size = 0
  18. this.blocks = 0
  19. }
  20.  
  21. read_from(buf: Buf) {
  22. this.magic = buf.read_counted_str(16)
  23. this.size = buf.read_u32_le()
  24. this.blocks = buf.read_u32_le()
  25. }
  26. }
  27.  
  28. class UcsFontBlock {
  29. start: number // ushort Code start value
  30. end: number // ushort End code value
  31. width: number // ushort Lattice (grid) width
  32. height: number // ushort Lattice (grid) height
  33. roffs: number // uint Font dot matrix data offset
  34. xoffs: number // uint Font extension data offset
  35.  
  36. constructor() {
  37. this.start = 0
  38. this.end = 0
  39. this.width = 0
  40. this.height = 0
  41. this.roffs = 0
  42. this.xoffs = 0
  43. }
  44. read_from(buf: Buf) {
  45. this.start = buf.read_u16_le()
  46. this.end = buf.read_u16_le()
  47. this.width = buf.read_u16_le()
  48. this.height = buf.read_u16_le()
  49. this.roffs = buf.read_u32_le()
  50. this.xoffs = buf.read_u32_le()
  51. }
  52. }
  53.  
  54. function read_font(path: string) {
  55. let buf = new Buf()
  56. buf.data = fs.readFileSync(path)
  57.  
  58. console.log('file: ', path)
  59. console.log('file size: ', buf.data.length)
  60.  
  61. let header = new UcsFontHeader()
  62. header.read_from(buf)
  63. console.log('header: ', header)
  64.  
  65. let blocks = []
  66.  
  67. for(let block_index = 0; block_index < header.blocks; block_index++) {
  68. let block = new UcsFontBlock()
  69. block.read_from(buf)
  70. console.log('block: ', block)
  71. blocks.push(block)
  72.  
  73. const bytes_per_symbol = block.width*block.height
  74. console.log('bytes_per_symbol: ', bytes_per_symbol)
  75. const symbols_per_block = (block.xoffs - block.roffs) / bytes_per_symbol
  76. console.log('symbols_per_block: ', symbols_per_block)
  77.  
  78. let pos = block.roffs;
  79. for(let symbol_index = 0; symbol_index < symbols_per_block; symbol_index++) {
  80. let symbol_data = buf.read_from_offset(pos, bytes_per_symbol)
  81. let start_pos = pos;
  82. pos += bytes_per_symbol
  83. console.log('symbol ', symbol_index, ' start_pos: ', start_pos, ' end_pos: ', pos)
  84. for(let h = 0; h < block.height; h++) {
  85. let str = ''
  86. for(let w = 0; w < block.width; w++) {
  87. str += symbol_data[h*block.width + w] + ' '
  88. }
  89. // console.log(str);
  90. }
  91. }
  92. }
  93.  
  94.  
  95. // console.log('buf.offset: ', buf.offset)
  96. // console.log('buf len - offset: ', buf.data.length - buf.offset);
  97. // let font_bytes = (block.width + 7) / 8 * block.height;
  98. // console.log('font_bytes: ', font_bytes);
  99.  
  100.  
  101. // console.log('bytes_per_symbol: ', block.width*block.height);
  102. // console.log('bytes_128_symbol: ', 128*block.width*block.height);
  103.  
  104. // let m_pASCIIFont = new Uint8Array(font_bytes * 128 + 128);
  105. for(let block_index = 0; block_index < header.blocks; block_index++) {
  106. }
  107.  
  108. }
  109.  
  110. read_font('./Font.bin')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement