SquidDev

FontAPI

Jul 12th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. function LoadFont(Path)
  2.     local File = fs.open(Path, "rb")
  3.  
  4.     local Version = File.read()
  5.     if Version ~= 1 then
  6.         error("Unsupported version" + tostring(Version))
  7.     end
  8.  
  9.     local CharacterWidth = File.read()
  10.     local CharacterHeight = File.read()
  11.  
  12.     local Index = 3
  13.  
  14.     local Characters = {}
  15.  
  16.     while true do
  17.         local Character = File.read()
  18.         Index = Index + 1
  19.  
  20.         if Character == nil then
  21.             break
  22.         end
  23.  
  24.         local Pixels = {}
  25.         local Row = {}
  26.  
  27.         local X = 1
  28.         local Y = 1
  29.  
  30.         while true do
  31.             local Byte = File.read()
  32.             Index = Index + 1
  33.  
  34.             for _=7, 0, -1 do
  35.                 --Parse one bit
  36.                 local MaxBit = math.pow(2, _)
  37.                 local Last = Byte / MaxBit
  38.                 local Bit = 0
  39.                 if Last >= 1 then
  40.                     Bit = 1
  41.                 end
  42.                 Byte = Byte % MaxBit
  43.  
  44.                 table.insert(Row, Bit)
  45.                 X = X + 1
  46.  
  47.                 if X > CharacterWidth then
  48.                     table.insert(Pixels, Row)
  49.                     Row = {}
  50.                    
  51.                     X = 1
  52.                     Y = Y + 1
  53.  
  54.                     if Y> CharacterHeight then
  55.                         break
  56.                     end
  57.                 end
  58.  
  59.             end
  60.  
  61.             if Y > CharacterHeight then
  62.                 break
  63.             end
  64.         end
  65.  
  66.         Characters[string.char(Character)] = Pixels
  67.     end
  68.  
  69.     File.close()
  70.  
  71.     return Characters, CharacterWidth, CharacterHeight
  72. end
  73.  
  74. function CreateObject(Font, Width, Height, Parent)
  75.     if Parent == nil then
  76.         Parent = term.current()
  77.     end
  78.  
  79.     local NewTerm = {}
  80.    
  81.     local Back = colours.black
  82.     local Front = colours.white
  83.  
  84.     local CursorX = 1
  85.     local CursorY = 1
  86.  
  87.     local W, H = Parent.getSize()
  88.  
  89.     local function GetPosition()
  90.         return (CursorX - 1) * Width, (CursorY - 1) * Height
  91.     end
  92.  
  93.     function NewTerm.write(Text)
  94.         for _=1, #Text, 1 do
  95.             local Character = Text:sub(_, _)
  96.  
  97.             local Pixels = Font[Character]
  98.             if Pixels == nil then
  99.                 Pixels = Font['?']
  100.             end
  101.  
  102.             local cX, cY = GetPosition()
  103.  
  104.             for Y = 1, Height, 1 do
  105.                 local Vals = Pixels[Y]
  106.                 for X = 1, Width, 1 do
  107.                     local Colour = Vals[X]
  108.                     Parent.setCursorPos(cX + X, cY + Y)
  109.  
  110.                     if Colour == 1 then
  111.                         Parent.setBackgroundColour(Front)
  112.                     else
  113.                         Parent.setBackgroundColour(Back)
  114.                     end
  115.  
  116.                     Parent.write(" ")
  117.                 end
  118.             end
  119.  
  120.             CursorX = CursorX + 1
  121.  
  122.         end
  123.     end
  124.  
  125.     function NewTerm.clearLine()
  126.         local X, Y = GetPosition()
  127.  
  128.         for _=Y, Y+Height, 1 do
  129.             Parent.setCursorPos(1, _)
  130.             Parent.clearLine()
  131.         end
  132.     end
  133.  
  134.     function NewTerm.scroll(Ammount)
  135.         Parent.scroll(Ammount * Height)
  136.     end
  137.  
  138.     function NewTerm.getCursorPos()
  139.         return CursorX, CursorY
  140.     end
  141.  
  142.     function NewTerm.setCursorPos(X, Y)
  143.         CursorX = X
  144.         CursorY = Y
  145.     end
  146.  
  147.     function NewTerm.setTextColour(Colour)
  148.         Front = Colour
  149.     end
  150.     NewTerm.setTextColor = NewTerm.setTextColour
  151.  
  152.     function NewTerm.setBackgroundColour(Colour)
  153.         Back = Colour
  154.     end
  155.     NewTerm.setBackgroundColor = NewTerm.setBackgroundColour
  156.  
  157.     function NewTerm.getSize()
  158.         return math.floor(W / Width), math.floor(H / Height)
  159.     end
  160.  
  161.  
  162.     --Copy across functions
  163.     NewTerm.clear = Parent.clear
  164.     NewTerm.isColor = Parent.isColor
  165.     NewTerm.isColour = Parent.isColour
  166.  
  167.     --NYI
  168.     function NewTerm.getCursorBlink(CursorBlink) end
  169.     return NewTerm
  170. end
Advertisement
Add Comment
Please, Sign In to add comment