|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- https://anggtwu.net/LUA/PictRect1.lua.html
-- https://anggtwu.net/LUA/PictRect1.lua
-- (find-angg "LUA/PictRect1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/PictRect1.lua"))
--
-- This file contains low-level functions that generate Pict2e code
-- for Dednat7. In this version the indentation is done by
-- manipulating Rect objects; this is much simpler than what I did in
-- the previous versions, in which the desired indentation was
-- described with a DSL that was parsed with Lpeg. Here are some links
-- to the previous versions:
--
-- (find-angg "LUA/Indent1.lua" "Indent")
-- (find-angg "LUA/Indent2.lua" "Indent")
-- (find-angg "LUA/Indent2.lua" "Pict")
-- (find-angg "LUA/Indent2.lua" "Pict" "{+1" "{<i:ind(1);o=''>")
-- (find-angg "LUA/Pict2e2.lua" "Pict" "{<+1>")
-- (find-angg "LUA/Pict3.lua")
--
-- «.Rect» (to "Rect")
-- «.Rect-tests» (to "Rect-tests")
-- «.Pict» (to "Pict")
-- «.Pict-tests» (to "Pict-tests")
-- ____ _
-- | _ \ ___ ___| |_
-- | |_) / _ \/ __| __|
-- | _ < __/ (__| |_
-- |_| \_\___|\___|\__|
--
-- «Rect» (to ".Rect")
-- Here we add an operation "+" to our class Rect, that either appends
-- a string to the last line of a rectangle or prepends a string to
-- the first line of a rectangle; it is used as "rect+str" or
-- "str+rect"
-- We also add a method "indentrest", that indents all the lines of a
-- rectangle except for the first one.
-- Here we add a few new methods to the Rect class.
-- (find-angg "LUA/lua50init.lua" "Rect")
--
Rect.__sub = function (a,b)
if type(a) == "string" and #b == 0 then return Rect {a} end
if type(b) == "string" and #a == 0 then return Rect {b} end
if type(a) == "string" then b = copy(b); b[1] = a..b[1]; return b end
if type(b) == "string" then a = copy(a); a[#a] = a[#a]..b; return a end
PP(a,b)
error("One of the arguments of Rect.__sub needs to be a string")
end
Rect.__index.rest = function (rect)
rect = copy(rect)
table.remove(rect,1)
return rect
end
Rect.__index.indentrest = function (rect,spaces)
if #rect == 0 then return Rect {} end
if #rect == 1 then return rect[1] end
if #rect > 1 then return rect[1] / (spaces .. rect:rest()) end
end
-- «Rect-tests» (to ".Rect-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "PictRect1.lua"
r0 = Rect {}
r1 = Rect {"abc"}
r3 = Rect {"abc", "de", "f"}
= r3 - "...."
= r3 - "...." - "____"
= (r3 - "....") / "____"
= "...." - r3
= r3:rest()
= r0:rest()
= r0:indentrest("::")
= r1:indentrest("::")
= r3:indentrest("::")
= "\\foo{" - r3 - "}"
= "\\foo{" - r3:indentrest(" ") - "}"
--]]
-- ____ _ _
-- | _ \(_) ___| |_
-- | |_) | |/ __| __|
-- | __/| | (__| |_
-- |_| |_|\___|\__|
--
-- «Pict» (to ".Pict")
-- Supersedes:
-- (find-angg "LUA/Indent2.lua" "Pict")
-- (find-angg "LUA/Pict2e1.lua" "Pict2e-methods")
-- (find-angg "LUA/Pict2e2.lua" "Pict")
--
Pict = Class {
type = "Pict",
r_ = function (o) return type(o)=="string" and o or Pict.r(o) end,
r = function (o)
if type(o)=="string" then return Rect{o} end
local rects = map(Pict.r, o)
local bigrect = Rect {}
for _,rect in ipairs(rects) do
for _,line in ipairs(rect) do
table.insert(bigrect, line)
end
end
return bigrect
end,
__tostring = function (p) return p:tostring() end,
__div = function (p1,p2) return Pict(Pict.r(p1) / Pict.r(p2)) end,
__concat = function (p1,p2) return Pict(Pict.r(p1) .. Pict.r(p2)) end,
__sub = function (p1,p2) return Pict(Pict.r_(p1) - Pict.r_(p2)) end,
__index = {
torect = function (p) return Pict.r(p) end,
tostring = function (p) return p:torect():tostring() end,
output = function (p) output(tostring(p)) end,
add = function (p, o) table.insert(p, o); return p end,
pre = function (p, o) return Pict {o, p} end,
print = function (p, o) return p:add(o) end,
printf = function (p, ...) return p:add(format(...)) end,
pprintf = function (p, ...) return p:add(pformat(...)) end,
--
indentrest = function (p,spaces) return Pict(Pict.r(p):indentrest(spaces)) end,
wrap_00 = function (p,o,i,c) return o - p:indentrest(i) - c end,
wrap_01 = function (p,o,i,c) return (o - p:indentrest(i)) / c end,
wrap_02 = function (p,o,i,c) return o - ((p / c):indentrest(i)) end,
wrap_10 = function (p,o,i,c) return o / ((i..p) - c) end,
wrap_11 = function (p,o,i,c) return o / (i..p) / c end,
wrap_12 = function (p,o,i,c) return o / (i..(p / c)) end,
--
-- Compatibility:
-- (find-angg "LUA/Indent2.lua" "Pict-tests")
Wrap10 = function (p,pre) return p:wrap_10((pre or "").."{{", " ", "}}") end,
wrap00 = function (p,pre) return p:wrap_00((pre or "").."{", " ", "}") end,
wrapin = function (p,pre) return p:wrap_00((pre or "").."{", " ", "}") end,
wrap1 = function (p,pre) return p:wrap_00((pre or "").."{", " ", "}") end,
wrap2 = function (p,pre) return p:wrap_11((pre or "").."{", " ", "}") end,
--
d = function (p) return p:wrap_00("$", " ", "$") end,
dd = function (p) return p:wrap_00("$$", " ", "$$") end,
bhbox = function (p) return p:wrap_00("\\bhbox{$", " ", "$}") end,
--
-- (find-angg "LUA/Pict2e1.lua" "Pict2e-methods")
def = function (p, name) return p:Wrap10("\\def\\"..name) end,
sa = function (p, name) return p:Wrap10("\\sa{"..name.."}") end,
color = function (p, color) return p:wrap00("\\color{"..color.."}") end,
Color = function (p, color) return p:wrap00("\\Color"..color) end,
precolor = function (p, color) return p:pre("\\color{"..color.."}") end,
prethickness = function (p, th) return p:pre("\\linethickness{"..th.."}") end,
preunitlength = function (p, u) return p:pre("\\unitlength="..u) end,
myvcenter = function (p) return p:wrap00("\\myvcenter") end,
--
putat = function (p, xy) return p:wrap00(pformat("\\put%s", xy)) end,
--
},
}
-- «Pict-tests» (to ".Pict-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Pict2e3.lua"
p = Pict {"ab","cd","ef"}
= p:wrap_00("\\foo{", " ", "}")
= p:wrap_01("\\foo{", " ", "}")
= p:wrap_02("\\foo{", " ", "}")
= p:wrap_10("\\foo{", " ", "}")
= p:wrap_11("\\foo{", " ", "}")
= p:wrap_12("\\foo{", " ", "}")
ab = Pict {"a", "b"}
= ab:def"foo"
= ab:sa "foo"
= ab:Color"Red"
= ab:precolor"red"
= ab:prethickness"1pt"
= ab:preunitlength"1pt" -- ok até aqui
= ab:bhbox()
= ab:myvcenter()
-- = ab:putat(v(2,3))
-- = ab:scalebox(2.3)
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: