Advertisement
Naheulf

ComputerCraft/modules/main/cc/image/nft.lua

Oct 20th, 2020
2,593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. --- This file come from cc-tweaked-1.15.2-1.90.0
  2. --- Provides utilities for working with "nft" images.
  3. --
  4. -- nft ("Nitrogen Fingers Text") is a file format for drawing basic images.
  5. -- Unlike the images that @{paintutils.parseImage} uses, nft supports coloured
  6. -- text.
  7. --
  8. -- @module cc.image.nft
  9. -- @usage Load an image from `example.nft` and draw it.
  10. --
  11. --     local nft = require "cc.image.nft"
  12. --     local image = assert(nft.load("example.nft"))
  13. --     nft.draw(image)
  14.  
  15. local expect = require "cc.expect".expect
  16.  
  17. --- Parse an nft image from a string.
  18. --
  19. -- @tparam string image The image contents.
  20. -- @return table The parsed image.
  21. local function parse(image)
  22.     expect(1, image, "string")
  23.  
  24.     local result = {}
  25.     local line = 1
  26.     local foreground = "0"
  27.     local background = "f"
  28.  
  29.     local i, len = 1, #image
  30.     while i <= len do
  31.         local c = image:sub(i, i)
  32.         if c == "\31" and i < len then
  33.             i = i + 1
  34.             foreground = image:sub(i, i)
  35.         elseif c == "\30" and i < len then
  36.             i = i + 1
  37.             background = image:sub(i, i)
  38.         elseif c == "\n" then
  39.             if result[line] == nil then
  40.                 result[line] = { text = "", foreground = "", background = "" }
  41.             end
  42.  
  43.             line = line + 1
  44.         else
  45.             local next = image:find("[\n\30\31]", i) or #image + 1
  46.             local seg_len = next - i
  47.  
  48.             local this_line = result[line]
  49.             if this_line == nil then
  50.                 this_line = { foreground = "", background = "", text = "" }
  51.                 result[line] = this_line
  52.             end
  53.  
  54.             this_line.text = this_line.text .. image:sub(i, next - 1)
  55.             this_line.foreground = this_line.foreground .. foreground:rep(seg_len)
  56.             this_line.background = this_line.background .. background:rep(seg_len)
  57.  
  58.             i = next - 1
  59.         end
  60.  
  61.         i = i + 1
  62.     end
  63.     return result
  64. end
  65.  
  66. --- Load an nft image from a file.
  67. --
  68. -- @tparam string path The file to load.
  69. -- @treturn[1] table The parsed image.
  70. -- @treturn[2] nil If the file does not exist or could not be loaded.
  71. -- @treturn[2] string An error message explaining why the file could not be
  72. -- loaded.
  73. local function load(path)
  74.     expect(1, path, "string")
  75.     local file, err = io.open(path, "r")
  76.     if not file then return nil, err end
  77.  
  78.     local result = file:read("*a")
  79.     file:close()
  80.     return parse(result)
  81. end
  82.  
  83. --- Draw an nft image to the screen.
  84. --
  85. -- @tparam table image An image, as returned from @{load} or @{draw}.
  86. -- @tparam number xPos The x position to start drawing at.
  87. -- @tparam number xPos The y position to start drawing at.
  88. -- @tparam[opt] term.Redirect target The terminal redirect to draw to. Defaults to the
  89. -- current terminal.
  90. local function draw(image, xPos, yPos, target)
  91.     expect(1, image, "table")
  92.     expect(2, xPos, "number")
  93.     expect(3, yPos, "number")
  94.     expect(4, target, "table", "nil")
  95.  
  96.     if not target then target = term end
  97.  
  98.     for y, line in ipairs(image) do
  99.         target.setCursorPos(xPos, yPos + y - 1)
  100.         target.blit(line.text, line.foreground, line.background)
  101.     end
  102. end
  103.  
  104. return {
  105.     parse = parse,
  106.     load = load,
  107.     draw = draw,
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement