diff options
Diffstat (limited to 'tex/context/base/back-pdf.lua')
-rw-r--r-- | tex/context/base/back-pdf.lua | 114 |
1 files changed, 86 insertions, 28 deletions
diff --git a/tex/context/base/back-pdf.lua b/tex/context/base/back-pdf.lua index f8a5dab6f..7f0b1acc7 100644 --- a/tex/context/base/back-pdf.lua +++ b/tex/context/base/back-pdf.lua @@ -6,8 +6,24 @@ if not modules then modules = { } end modules ['back-pdf'] = { license = "see context related readme files" } +-- we could do \pdfmatrix sx <> sy <> etc + +local tonumber = tonumber +local sind, cosd = math.sind, math.cosd +local insert, remove = table.insert, table.remove + local codeinjections = backends.pdf.codeinjections +local context = context + +local scanners = tokens.scanners +local scanstring = scanners.string +local scannumber = scanners.number +local scaninteger = scanners.integer +local scankeyword = scanners.keyword + +local scanners = interfaces.scanners + local outputfilename function codeinjections.getoutputfilename() @@ -19,15 +35,11 @@ end backends.install("pdf") -local context = context - -local sind, cosd = math.sind, math.cosd -local insert, remove = table.insert, table.remove +local f_matrix = string.formatters["%F %F %F %F"] -- 0.8 is default -local f_matrix = string.formatters["%0.8f %0.8f %0.8f %0.8f"] - -function commands.pdfrotation(a) +scanners.pdfrotation = function() -- a -- todo: check for 1 and 0 and flush sparse + local a = scannumber() local s, c = sind(a), cosd(a) context(f_matrix(c,s,-s,c)) end @@ -38,27 +50,36 @@ end -- -- we could also do the save restore wrapping here + colorhack +local pdfsave = nodes.pool.pdfsave +local pdfrestore = nodes.pool.pdfrestore local pdfsetmatrix = nodes.pool.pdfsetmatrix -local stack = { } -local function popmatrix() - local top = remove(stack) - if top then - context(pdfsetmatrix(unpack(top))) - end -end +local stack = { } +local restore = true -- false -function commands.pdfstartrotation(a) +scanners.pdfstartrotation = function() -- a + local a = scannumber() if a == 0 then insert(stack,false) else local s, c = sind(a), cosd(a) + context(pdfsave()) context(pdfsetmatrix(c,s,-s,c)) - insert(stack,{ c, -s, s, c }) + insert(stack,restore and { c, -s, s, c } or true) end end -function commands.pdfstartscaling(sx,sy) +scanners.pdfstartscaling = function() -- sx sy + local sx, sy = 0, 0 + while true do + if scankeyword("sx") then + sx = scannumber() + elseif scankeyword("sy") then + sy = scannumber() + else + break + end + end if sx == 1 and sy == 1 then insert(stack,false) else @@ -68,25 +89,62 @@ function commands.pdfstartscaling(sx,sy) if sy == 0 then sy = 0.0001 end + context(pdfsave()) context(pdfsetmatrix(sx,0,0,sy)) - insert(stack,{ 1/sx, 0, 0, 1/sy }) + insert(stack,restore and { 1/sx, 0, 0, 1/sy } or true) end end -function commands.pdfstartmirroring() - context(pdfsetmatrix(-1,0,0,1)) -end - -function commands.pdfstartmatrix(sx,rx,ry,sy) -- tx, ty +scanners.pdfstartmatrix = function() -- sx rx ry sy -- tx, ty + local sx, rx, ry, sy = 0, 0, 0, 0 + while true do + if scankeyword("sx") then + sx = scannumber() + elseif scankeyword("sy") then + sy = scannumber() + elseif scankeyword("rx") then + rx = scannumber() + elseif scankeyword("ry") then + ry = scannumber() + else + break + end + end if sx == 1 and rx == 0 and ry == 0 and sy == 1 then insert(stack,false) else + context(pdfsave()) context(pdfsetmatrix(sx,rx,ry,sy)) - insert(stack,{ -sx, -rx, -ry, -sy }) + insert(stack,store and { -sx, -rx, -ry, -sy } or true) + end +end + +local function pdfstopsomething() + local top = remove(stack) + if top == false then + -- not wrapped + elseif top == true then + context(pdfrestore()) + elseif top then + context(pdfsetmatrix(unpack(top))) + context(pdfrestore()) + else + -- nesting error end end -commands.pdfstoprotation = popmatrix -commands.pdfstopscaling = popmatrix -commands.pdfstopmirroring = commands.pdfstartmirroring -commands.pdfstopmatrix = popmatrix +scanners.pdfstoprotation = pdfstopsomething +scanners.pdfstopscaling = pdfstopsomething +scanners.pdfstopmatrix = pdfstopsomething + +scanners.pdfstartmirroring = function() + context(pdfsetmatrix(-1,0,0,1)) +end + +scanners.pdfstopmirroring = scanners.pdfstartmirroring + +scanners.registerbackendsymbol = function() + backends.codeinjections.registersymbol(scanstring(),scaninteger()) +end + +-- todo : clipping |