Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- picture.lua: classes to create LaTeX pictures (and ascii representations).
-- This file:
-- http://angg.twu.net/dednat6/picture.lua
-- http://angg.twu.net/dednat6/picture.lua.html
--  (find-angg        "dednat6/picture.lua")
--
-- This file defines a class LPicture that generates LaTeX
-- picture-mode diagrams, and a class AsciiPicture for ascii
-- representations of the same diagrams. The classes for mixed
-- LaTeX/ascii pictures are elsewhere.
--
-- Much older version: (find-angg "LUA/picture.lua" "Picture")
--
-- «.V»				(to "V")
-- «.V-tests»			(to "V-tests")
-- «.BoundingBox»		(to "BoundingBox")
-- «.BoundingBox-tests»		(to "BoundingBox-tests")
-- «.AsciiPicture»		(to "AsciiPicture")
-- «.AsciiPicture-tests»	(to "AsciiPicture-tests")
-- «.metaopts»			(to "metaopts")
-- «.copyopts»			(to "copyopts")
-- «.copyopts-tests»		(to "copyopts-tests")
-- «.makepicture»		(to "makepicture")
-- «.texarrow»			(to "texarrow")
-- «.LPicture»			(to "LPicture")
-- «.LPicture-tests»		(to "LPicture-tests")
--
-- Obsolete (to be deleted):
-- «.Picture»			(to "Picture")
-- «.Picture-tests»		(to "Picture-tests")




--  ____  ____                   _                 
-- |___ \|  _ \  __   _____  ___| |_ ___  _ __ ___ 
--   __) | | | | \ \ / / _ \/ __| __/ _ \| '__/ __|
--  / __/| |_| |  \ V /  __/ (__| || (_) | |  \__ \
-- |_____|____/    \_/ \___|\___|\__\___/|_|  |___/
--                                                 
-- «V» (to ".V")
V = Class {
  type    = "V",
  __tostring = function (v) return "("..v[1]..","..v[2]..")" end,
  __add      = function (v, w) return V{v[1]+w[1], v[2]+w[2]} end,
  __sub      = function (v, w) return V{v[1]-w[1], v[2]-w[2]} end,
  __unm      = function (v) return v*-1 end,
  __mul      = function (v, w)
      local ktimesv   = function (k, v) return V{k*v[1], k*v[2]} end
      local innerprod = function (v, w) return v[1]*w[1] + v[2]*w[2] end
      if     type(v) == "number" and type(w) == "table" then return ktimesv(v, w)
      elseif type(v) == "table" and type(w) == "number" then return ktimesv(w, v)
      elseif type(v) == "table" and type(w) == "table"  then return innerprod(v, w)
      else error("Can't multiply "..tostring(v).."*"..tostring(w))
      end
    end,
  --
  -- isdd = function (s)
  --     return type(s) == "string" and s:match"^%d%d$"
  --   end,
  -- ispxcyp = function (s)
  --     return type(s) == "string" and s:match"^%((.-),(.-)%)$"
  --   end,
  -- fromdd = function (s) return V{s:sub(1,1)+0, s:sub(2,2)+0} end,
  -- frompxcyp = function (s)
  --     local x, y = a:match("^%((.-),(.-)%)$")
  --     return V{x+0, y+0}
  --   end,
  --
  fromab = function (a, b)
      if     type(a) == "table"  then return a
      elseif type(a) == "number" then return V{a,b}
      elseif type(a) == "string" then
        local l, r = a:match("^(%d)(%d)$")
        if l then return V{l+0, r+0}:lrtoxy() end
        local x, y = a:match("^%((.-),(.-)%)$")
        if x then return V{x+0, y+0} end
        error("V() got bad string: "..a)
      end
    end,
  __call = function () print "hi"
    end,
  __index = {
    xytolr = function (v)
        local x, y = v[1], v[2]
        local l = (y - x) / 2
        local r = (y + x) / 2
        return V{l, r}
      end,
    lrtoxy = function (v)
        local l, r = v[1], v[2]
        local x = r - l
        local y = r + l
        return V{x, y}
      end,
    todd = function (v) return v[1]..v[2] end,
    to12 = function (v) return v[1], v[2] end,
    to_x_y = function (v) return v:to12() end,
    to_l_r = function (v) return v:xytolr():to12() end,
    xy = function (v) return "("..v[1]..","..v[2]..")" end,
    lr = function (v) local l, r = v:to_l_r(); return l..r end,
    torowcol = function (v, nlines, w, rectw)
        local x, y = v[1], v[2]
        if checkrange(0, y, nlines-1) and
           checkrange(0, x, rectw/w - 1)
        then return nlines-y, x*w+1
        end
      end,
    naiveprod = function (v, w)
        return V{v[1]*w[1], v[2]*w[2]}
      end,
    naivemin = function (v, w)
        return V{min(v[1], w[1]), min(v[2], w[2])}
      end,
    naivemax = function (v, w)
        return V{max(v[1], w[1]), max(v[2], w[2])}
      end,
    s  = function (v) return v+V{ 0, -1} end,
    n  = function (v) return v+V{ 0,  1} end,
    w  = function (v) return v+V{-1,  0} end,
    e  = function (v) return v+V{ 1,  0} end,
    se = function (v) return v+V{ 1, -1} end,
    sw = function (v) return v+V{-1, -1} end,
    ne = function (v) return v+V{ 1,  1} end,
    nw = function (v) return v+V{-1,  1} end,
    --
    -- to_l_r_l_r = function (P, Q)
    --     local Pl,Pr = P:to_l_r()
    --     local Ql,Qr = Q:to_l_r()
    --     return Pl, Pr, Ql, Qr
    --   end,
    --
    And = function (P, Q)
        -- local Pl, Pr, Ql, Qr = P:to_l_r_l_r(Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return V{min(Pl, Ql), min(Pr, Qr)}:lrtoxy()
      end,
    Or = function (P, Q)
        -- local Pl, Pr, Ql, Qr = P:to_l_r_l_r(Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return V{max(Pl, Ql), max(Pr, Qr)}:lrtoxy()
      end,
    --
    above = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl >= Ql and Pr >= Qr
      end,
    below = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl <= Ql and Pr <= Qr
      end,
    leftof = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl >= Ql and Pr <= Qr
      end,
    rightof = function (P, Q)
        local Pl, Pr = P:to_l_r()
        local Ql, Qr = Q:to_l_r()
        return Pl <= Ql and Pr >= Qr
      end,
  },
}

v = V.fromab
lr = function (l, r) return V{l, r}:lrtoxy() end
-- lr = function (l, r)
--     if V.isdd(l) then return V.fromdd(l):lrtoxy() end
--     return V{l, r}:lrtoxy()
--   end

-- «V-tests» (to ".V-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= V{3,4}             --> (3,4)    
= V{3,4} - V{2,1}    --> (1,3)    
= V{3,4} + V{2,1}    --> (5,5)    
= V{3,4} * V{2,1}    --> 10	     
= V{3,4} * -10	     --> (-30,-40)
= -10 * V{3,4}	     --> (-30,-40)
= V{-3,3}:xytolr()   --> (3,0)    
= V{3,3}:xytolr()    --> (0,3)    
= V{2,4}:xytolr()    --> (1,3)    
= V{0,0}:torowcol(4, 2, 6)   --> 4 1
= V{1,0}:torowcol(4, 2, 6)   --> 4 3
= V{2,0}:torowcol(4, 2, 6)   --> 4 5
= V{3,0}:torowcol(4, 2, 6)   --> (nothing)
= V{0,1}:torowcol(4, 2, 6)   --> 3 1
= V{0,2}:torowcol(4, 2, 6)   --> 3 5
= V{0,3}:torowcol(4, 2, 6)   --> 3 5
= V{0,4}:torowcol(4, 2, 6)   --> (nothing)

* (ex "v-1")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= v{2, 3}
= v(2, 3)
= v(2, 3):to12()

* (ex "v-2")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
= lr"00"      -- err?
= lr"10"      -- err?
= lr"03"      -- err?
= lr"03":s()  -- err?

* (ex "v-3")

--]]



--  ____                        _ _             ____            
-- | __ )  ___  _   _ _ __   __| (_)_ __   __ _| __ )  _____  __
-- |  _ \ / _ \| | | | '_ \ / _` | | '_ \ / _` |  _ \ / _ \ \/ /
-- | |_) | (_) | |_| | | | | (_| | | | | | (_| | |_) | (_) >  < 
-- |____/ \___/ \__,_|_| |_|\__,_|_|_| |_|\__, |____/ \___/_/\_\
--                                        |___/                 
-- «BoundingBox» (to ".BoundingBox")

BoundingBox = Class {
  type    = "BoundingBox",
  new     = function () return BoundingBox {} end,
  __tostring = function (b)
      return bb.x0y0 and tostring(bb.x0y0).." to "..tostring(bb.x1y1) or "empty"
    end,
  __index = {
    addpoint = function (bb, v)
        if bb.x0y0 then bb.x0y0 = bb.x0y0:naivemin(v) else bb.x0y0 = v end
        if bb.x1y1 then bb.x1y1 = bb.x1y1:naivemax(v) else bb.x1y1 = v end
        return bb
      end,
    addbox = function (bb, v, delta0, delta1)
        bb:addpoint(v+delta0)
        return bb:addpoint(v+(delta1 or -delta0))
      end,
    x0y0x1y1 = function (bb)
        local x0, y0 = bb.x0y0:to_x_y()
        local x1, y1 = bb.x1y1:to_x_y()
        return x0, y0, x1, y1
      end,
    x0x1y0y1 = function (bb)
        local x0, y0 = bb.x0y0:to_x_y()
        local x1, y1 = bb.x1y1:to_x_y()
        return x0, x1, y0, y1
      end,
    --
    merge = function (bb1, bb2)
        if bb2.x0y0 then bb1:addpoint(bb2.x0y0) end
        if bb2.x1y1 then bb1:addpoint(bb2.x1y1) end
        return bb1
      end,
  },
}

-- «BoundingBox-tests» (to ".BoundingBox-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
bb = BoundingBox.new()
= bb
= bb:addpoint(v(2, 4))
= bb:addbox(v(6, 7), v(.5, .5))
= bb:addbox(v(1, 2), v(.5, .5))
= bb:x0x1y0y1()

* (ex "boundingbox")

--]]



--     _             _ _ ____  _      _                  
--    / \   ___  ___(_|_)  _ \(_) ___| |_ _   _ _ __ ___ 
--   / _ \ / __|/ __| | | |_) | |/ __| __| | | | '__/ _ \
--  / ___ \\__ \ (__| | |  __/| | (__| |_| |_| | | |  __/
-- /_/   \_\___/\___|_|_|_|   |_|\___|\__|\__,_|_|  \___|
--                                                       
-- «AsciiPicture» (to ".AsciiPicture")
-- This is a minimalistic, and V-based, reimplementation of the
-- ascii side of the "Picture" class from:
-- (find-dn6 "picture.lua" "Picture")

pad = function (spaces, str)
    return ((str or "")..spaces):sub(1, #spaces)
  end

AsciiPicture = Class {
  type = "AsciiPicture",
  new  = function (s)
      return AsciiPicture {s=s or "  ", bb=BoundingBox.new()}
    end,
  __tostring = function (ap) return ap:tostring() end,
  __index = {
    get  = function (ap, v) return pad(ap.s, ap:get0(v)) end,
    get0 = function (ap, v)
        local x, y = v:to_x_y()
        return ap[y] and ap[y][x]
      end,
    put = function (ap, v, str)
        local x, y = v:to_x_y()
        ap[y] = ap[y] or {}
        ap[y][x] = str
        ap.bb:addpoint(v)
        return ap
      end,
    --
    tolines = function (ap)
        local x0, x1, y0, y1 = ap.bb:x0x1y0y1()
        local lines = {}
        for y=y1,y0,-1 do
          local line = ""
          for x=x0,x1 do line = line..ap:get(v(x, y)) end
          table.insert(lines, line)
        end
        return lines
      end,
    tostring = function (ap) return table.concat(ap:tolines(), "\n") end,
  },
}

-- «AsciiPicture-tests» (to ".AsciiPicture-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
ap = AsciiPicture.new("  ")
ap = AsciiPicture.new("     ")
for l=0,2 do
  for r=0,3 do
    local pos=lr(l, r)
    ap:put(pos, "..")
    ap:put(pos, pos:lr())
    ap:put(pos, pos:xy())
  end
end
= ap

* (ex "asciipicture")

--]]




--                                    _       
--   ___ ___  _ __  _   _  ___  _ __ | |_ ___ 
--  / __/ _ \| '_ \| | | |/ _ \| '_ \| __/ __|
-- | (_| (_) | |_) | |_| | (_) | |_) | |_\__ \
--  \___\___/| .__/ \__, |\___/| .__/ \__|___/
--           |_|    |___/      |_|            
--
-- «metaopts» (to ".metaopts")
-- «copyopts» (to ".copyopts")
copyopts = function (A, B)
    if type(A) == "string" then
      if A:match(" ")
      then for _,a in ipairs(split(A)) do copyopts(a, B) end; return B
      else return copyopts(metaopts[A] or error("No metaopt[\""..A.."\"]"), B)
      end
    end
    for k,v in pairs(A) do
      if k == "meta" then copyopts(v, B) else B[k] = v end
    end
    return B
  end

metaopts = {}
metaopts["b"]   = {bhbox = 1}
metaopts["p"]   = {paren = 1}
metaopts["()"]  = {paren = 1}
metaopts["{}"]  = {curly = 1}
metaopts["s"]    = {cellfont="\\scriptsize",       celllower="2pt"}
metaopts["ss"]   = {cellfont="\\scriptscriptsize", celllower="1.5pt"}  -- ?
metaopts["t"]    = {cellfont="\\tiny",             celllower="1.5pt"}  -- ?
metaopts["t"]    = {cellfont="\\tiny",             celllower="1.25pt"}  -- ?
metaopts["10pt"] = {scale="10pt"} 
metaopts["8pt"]  = {scale="8pt", meta="s"} 

-- «copyopts-tests» (to ".copyopts-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "newrect.lua"
PP(copyopts("8pt", {}))
PP(copyopts({hello=1, meta="8pt"}, {}))

* (ex "copyopts")

--]]






--                  _              _      _                  
--  _ __ ___   __ _| | _____ _ __ (_) ___| |_ _   _ _ __ ___ 
-- | '_ ` _ \ / _` | |/ / _ \ '_ \| |/ __| __| | | | '__/ _ \
-- | | | | | | (_| |   <  __/ |_) | | (__| |_| |_| | | |  __/
-- |_| |_| |_|\__,_|_|\_\___| .__/|_|\___|\__|\__,_|_|  \___|
--                          |_|                              
-- «makepicture» (to ".makepicture")
-- (find-LATEX "edrx15.sty" "picture-cells")
--
makepicture = function (options, bb, body)
    local x0, y0, x1, y1 = bb:x0y0x1y1()
    local a = {}
    a.xdimen = x1 - x0
    a.ydimen = y1 - y0
    -- a.xoffset = (x0 + x1)/2
    -- a.yoffset = (y0 + y1)/2
    a.xoffset = x0
    a.yoffset = y0
    a.setscale = "\\unitlength="..(options.scale or "10pt").."%\n"
    a.setlower = "\\celllower="..(options.celllower or "2.5pt").."%\n"
    a.setfont  = "\\def\\cellfont{"..(options.cellfont or "").."}%\n"
    a.body = body
    local fmt = "!setscale!setlower!setfont"..
                "\\begin{picture}(!xdimen,!ydimen)(!xoffset,!yoffset)\n"..
                "!body"..
                "\\end{picture}"
    local latex = (fmt:gsub("!([a-z]+)", a))
    latex = "\\vcenter{\\hbox{"..latex.."}}"
    if options.bhbox then latex = "\\bhbox{$"..latex.."$}" end
    if options.paren then latex = "\\left("..latex.."\\right)" end
    if options.curly then latex = "\\left\\{"..latex.."\\right\\}" end
    if options.def   then latex = "\\def\\"..options.def.."{"..latex.."}" end
    return latex
  end



--  _                                        
-- | |_ _____  ____ _ _ __ _ __ _____      __
-- | __/ _ \ \/ / _` | '__| '__/ _ \ \ /\ / /
-- | ||  __/>  < (_| | |  | | | (_) \ V  V / 
--  \__\___/_/\_\__,_|_|  |_|  \___/ \_/\_/  
--                                           
-- «texarrow» (to ".texarrow")
-- Usage: local tar = texarrow_smart(usewhitemoves)
--        pic:putarrow(v"34", 0, -1, tar.s)
--
texarrow = {
  nw="\\nwarrow",  n="\\uparrow",   ne="\\nearrow",
   w="\\leftarrow",                  e="\\rightarrow",
  sw="\\swarrow",  s="\\downarrow", se="\\searrow",
}
texarrow_inv = {
  nw="\\searrow",   n="\\downarrow", ne="\\swarrow",
   w="\\rightarrow",                  e="\\leftarrow",
  sw="\\nearrow",   s="\\uparrow",   se="\\nwarrow", 
}
texarrow_smart = function (usewhitemoves)
    if usewhitemoves then return texarrow_inv end
    return texarrow
  end




--  _     ____  _      _                  
-- | |   |  _ \(_) ___| |_ _   _ _ __ ___ 
-- | |   | |_) | |/ __| __| | | | '__/ _ \
-- | |___|  __/| | (__| |_| |_| | | |  __/
-- |_____|_|   |_|\___|\__|\__,_|_|  \___|
--                                        
-- «LPicture» (to ".LPicture")
-- This is a minimalistic, and V-based, reimplementation of the
-- LaTeX side of the "Picture" class from:
-- (find-dn6 "picture.lua" "Picture")

LPicture = Class {
  type = "LPicture",
  new  = function (opts)
      local lp = {latex="", bb=BoundingBox.new()}    -- start empty
      -- for k,v in pairs(opts or {}) do lp[k] = v end  -- copy options
      copyopts(opts, lp)
      return LPicture(lp)                            -- set metatable
    end,
  __tostring = function (lp) return lp:tolatex() end,
  __index = {
    addpoint = function (lp, v) lp.bb:addpoint(v); return lp end,
    put = function (lp, v, tex)
        local x, y = v:to_x_y()
        lp.latex = lp.latex .. "  \\put("..x..","..y.."){\\cell{"..tex.."}}\n"
        lp:addpoint(v-V{.5,.5})
        lp:addpoint(v+V{.5,.5})
        return lp
      end,
    putarrow = function (lp, v, dx, dy, tex)
        lp:put(v+V{dx,dy}*0.5, tex)
      end,
    addlineorvector = function (lp, src, tgt, cmd)
        lp:addpoint(src)
        lp:addpoint(tgt)
        local x0, y0 = src:to_x_y()
        local x1, y1 = tgt:to_x_y()
        local dx, dy = x1-x0, y1-y0
        local adx, ady = math.abs(dx), math.abs(dy)
        local len = max(adx, ady)
        local udx, udy = dx/len, dy/len
        local put = "  \\put("..x0..","..y0..")"..
                    "{\\"..(cmd or "line").."("..udx..","..udy.."){"..len.."}}"
        lp.latex = lp.latex..put.."\n"
      end,
    tolatex = function (lp)
        return makepicture(lp, lp.bb, lp.latex)
      end,
    --
    -- 2016dec08:
    addtex = function (lp, tex) lp.latex = lp.latex.."  "..tex.."\n"; return lp; end,
    addt   = function (lp, ...) return lp:addtex(formatt(...)) end,
    rawput = function (lp, v, tex) return lp:addt("\\put%s{%s}", v, tex) end,
    print  = function (lp) print(lp); return lp end,
    lprint = function (lp) print(lp:tolatex()); return lp end,
    output = function (lp) output(lp:tolatex()); return lp end,
  },
}

-- «LPicture-tests» (to ".LPicture-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "picture.lua"
lp = LPicture.new {cellfont="\\scriptsize"}
for l=0,2 do
  for r=0,3 do
    local pos=lr(l, r)
    lp:put(pos, pos:xy())
  end
end
= lp

* (ex "lpicture")

--]]








--   ___  _               _      _         
--  / _ \| |__  ___  ___ | | ___| |_ ___ _ 
-- | | | | '_ \/ __|/ _ \| |/ _ \ __/ _ (_)
-- | |_| | |_) \__ \ (_) | |  __/ ||  __/_ 
--  \___/|_.__/|___/\___/|_|\___|\__\___(_)
--                                         

--  ____  _      _                         _
-- |  _ \(_) ___| |_ _   _ _ __ ___    ___| | __ _ ___ ___
-- | |_) | |/ __| __| | | | '__/ _ \  / __| |/ _` / __/ __|
-- |  __/| | (__| |_| |_| | | |  __/ | (__| | (_| \__ \__ \
-- |_|   |_|\___|\__|\__,_|_|  \___|  \___|_|\__,_|___/___/
--
-- «Picture» (to ".Picture")
-- We can ":put" things one by one into a Picture object, and ":totex"
-- will generate a "\begin{picture}...\end{picture}" LaTeX block with
-- the right size and offset. Also, ":toascii" generates an ascii
-- rendering of that picture object, great for debugging and stuff.

Picture = Class {
  type    = "Picture",
  new     = function (opts)
      local p = {whats={}, other={}}                -- start empty
      for k,v in pairs(opts or {}) do p[k] = v end  -- copy options
      return Picture(p)                             -- set metatable
    end,
  __index = {
    adjustbounds = function (p, x, y)
        p.minx, p.maxx = Min(p.minx, x), Max(x, p.maxx)
        p.miny, p.maxy = Min(p.miny, y), Max(y, p.maxy)
      end,
    put = function (p, x, y, what, what0)
        p:adjustbounds(x, y)
        -- p.minx = p.minx and min(x, p.minx) or x
        -- p.miny = p.miny and min(y, p.miny) or y
        -- p.maxx = p.maxx and max(p.maxx, x) or x
        -- p.maxy = p.maxy and max(p.maxy, y) or y
        table.insert(p.whats, {x=x, y=y, what=what, what0=what0})
        return p
      end,
    lrput = function (p, l, r, what, what0)
        local x = r - l
        local y = r + l
        p:put(x, y, what, what0)
        return p
      end,
    lrputline = function (p, l, r, dxdy, len)
        return p:lrput(l, r, nil, format("\\line(%s){%s}", dxdy, len))
      end,
    togrid = function (p)
        local lines = {}
        for y=p.miny,p.maxy do lines[y] = {} end
        for _,what in ipairs(p.whats) do
          lines[what.y][what.x] = what.what
        end
        return lines
      end,
    toasciilines = function (p, whitespace)
        local asciilines = {}
        local grid = p:togrid()
        for y=p.miny,p.maxy do
          for x=p.minx,p.maxx do
            local ascii = grid[y][x] or whitespace or "  "
            asciilines[y] = (asciilines[y] or "")..ascii
          end
        end
        return asciilines
      end,
    toascii = function (p, whitespace)
        local asciilines = p:toasciilines(whitespace)
        local lines = {}
        for y=p.maxy,p.miny,-1 do
          table.insert(lines, asciilines[y])
        end
        return table.concat(lines, "\n")
      end,
    --
    -- (find-es "tex" "dags")
    -- (find-kopkadaly4page (+ 12 289) "13.1.3 The positioning commands")
    -- (find-kopkadaly4text (+ 12 289) "13.1.3 The positioning commands")
    -- (find-kopkadaly4page (+ 12 298) "\\put")
    -- (find-kopkadaly4text (+ 12 298) "\\put")
    totex1 = function (p, what)
        -- local fmt = "  \\put(%s,%s){%s}\n"
        if what.what0 then
          return format("  \\put(%s,%s){%s}\n", what.x, what.y, what.what0)
        end
        local fmt = "  \\put(%s,%s){\\hbox to 0pt{\\hss %s%s\\hss}}\n"
        return format(fmt, what.x, what.y, p.font or "", what.what)
      end,
    totexputs = function (p)
        local f = function (what) return p:totex1(what) end
        return mapconcat(f, p.whats)
      end,
    --
    -- (find-kopkadaly4page (+ 12 301) "13.1.6 Shifting a picture environment")
    -- (find-kopkadaly4text            "13.1.6 Shifting a picture environment")
    -- (find-texbookpage (+ 12 66) "\\raise")
    -- (find-texbooktext (+ 12 66) "\\raise")
    totex = function (p)
        local a = {}
        a.scale   = p.scale or "1ex"
        a.xdimen  = p.maxx - p.minx + 1
        a.ydimen  = p.maxy - p.miny + 1
        a.xoffset = p.minx - 0.5
        -- a.yoffset = (p.miny + p.maxy + 1) / 2
        a.yoffset = p.miny
        a.lower   = (p.maxy - p.miny) / 2
        -- a.body = p:totexputs()
        a.body    = p:totexputs()..table.concat(p.other)
        local fmt = "{\\unitlength=!scale\n"..
                    "\\lower!lower\\unitlength\\hbox{"..
                    "\\begin{picture}(!xdimen,!ydimen)(!xoffset,!yoffset)\n"..
                    "!body"..
                    "\\end{picture}\n"..
                    "}}"
        return (fmt:gsub("!([a-z]+)", a))
      end,
  },
}



-- «Picture-tests» (to ".Picture-tests")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "picture.lua"
p = Picture.new()
p:put(2, 3, "23")
p:put(4, 7, "47")
PP(p)
PP(p:togrid())
= p:toascii()
= p:totexputs()
p.scale = "10pt"
= p:totex()
-- (find-LATEX "tmp.tex")

f1 = function (p, lr)
    local l = lr:sub(1,1)+0
    local r = lr:sub(2,2)+0
    p:lrput(l, r, lr)
  end
f = function (p, str)
    for _,lr in ipairs(split(str)) do
      f1(p, lr)
    end
  end

p = Picture {whats={}}
f(p, "00 01 02 03 04")
f(p, "10 11 12 13 14")
f(p,       "22 23 24")
f(p,       "32 33 34")

= p:toascii()
p.scale = "10pt"
= p:totex()

--]]



-- Local Variables:
-- coding: raw-text-unix
-- End: