| 1 |
-- |
| 2 |
-- This is file `luamplib.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- luamplib.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- See source file 'luamplib.dtx' for licencing and contact information. |
| 10 |
-- |
| 11 |
|
| 12 |
luatexbase.provides_module { |
| 13 |
name = "luamplib", |
| 14 |
version = "2.41.3", |
| 15 |
date = "2026/05/26", |
| 16 |
description = "Lua package to typeset Metapost with LuaTeX's MPLib.", |
| 17 |
} |
| 18 |
|
| 19 |
luamplib = luamplib or { } |
| 20 |
local luamplib = luamplib |
| 21 |
|
| 22 |
local format, abs = string.format, math.abs |
| 23 |
|
| 24 |
local function termorlog (target, text, kind) |
| 25 |
if text then |
| 26 |
local mod, write, append = "luamplib", texio.write_nl, texio.write |
| 27 |
kind = kind |
| 28 |
or target == "term" and "Warning (more info in the log)" |
| 29 |
or target == "log" and "Info" |
| 30 |
or target == "term and log" and "Warning" |
| 31 |
or "Error" |
| 32 |
target = kind == "Error" and "term and log" or target |
| 33 |
local t = text:explode"\n+" |
| 34 |
write(target, format("Module %s %s:", mod, kind)) |
| 35 |
if #t == 1 then |
| 36 |
append(target, format(" %s", t[1])) |
| 37 |
else |
| 38 |
for _,line in ipairs(t) do |
| 39 |
write(target, line) |
| 40 |
end |
| 41 |
write(target, format("(%s) ", mod)) |
| 42 |
end |
| 43 |
append(target, format(" on input line %s", tex.inputlineno)) |
| 44 |
write(target, "") |
| 45 |
if kind == "Error" then error() end |
| 46 |
end |
| 47 |
end |
| 48 |
local function warn (...) -- beware '%' symbol |
| 49 |
termorlog("term and log", select("#",...) > 1 and format(...) or ...) |
| 50 |
end |
| 51 |
local function info (...) |
| 52 |
termorlog("log", select("#",...) > 1 and format(...) or ...) |
| 53 |
end |
| 54 |
local function err (...) |
| 55 |
termorlog("error", select("#",...) > 1 and format(...) or ...) |
| 56 |
end |
| 57 |
|
| 58 |
luamplib.showlog = luamplib.showlog or false |
| 59 |
|
| 60 |
local tableconcat = table.concat |
| 61 |
local tableinsert = table.insert |
| 62 |
local tableunpack = table.unpack |
| 63 |
local texsprint = tex.sprint |
| 64 |
local texgettoks = tex.gettoks |
| 65 |
local texgetbox = tex.getbox |
| 66 |
local texruntoks = tex.runtoks |
| 67 |
if not texruntoks then |
| 68 |
err("Your LuaTeX version is too old. Please upgrade it to the latest") |
| 69 |
end |
| 70 |
local is_defined = token.is_defined |
| 71 |
local get_macro = token.get_macro |
| 72 |
local mplib = require ('mplib') |
| 73 |
local kpse = require ('kpse') |
| 74 |
local lfs = require ('lfs') |
| 75 |
local lfsattributes = lfs.attributes |
| 76 |
local lfsisdir = lfs.isdir |
| 77 |
local lfsmkdir = lfs.mkdir |
| 78 |
local lfstouch = lfs.touch |
| 79 |
local ioopen = io.open |
| 80 |
|
| 81 |
local file = file or { } |
| 82 |
local replacesuffix = file.replacesuffix or function(filename, suffix) |
| 83 |
return (filename:gsub("%.[%a%d]+$","")) .. "." .. suffix |
| 84 |
end |
| 85 |
local is_writable = file.is_writable or function(name) |
| 86 |
if lfsisdir(name) then |
| 87 |
name = name .. "/_luam_plib_temp_file_" |
| 88 |
local fh = ioopen(name,"w") |
| 89 |
if fh then |
| 90 |
fh:close(); os.remove(name) |
| 91 |
return true |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
local mk_full_path = lfs.mkdirp or lfs.mkdirs or function(path) |
| 96 |
local full = "" |
| 97 |
for sub in path:gmatch("(/*[^\\/]+)") do |
| 98 |
full = full .. sub |
| 99 |
lfsmkdir(full) |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
local cachedir |
| 104 |
local function outputdir () |
| 105 |
if lfstouch then |
| 106 |
for i,v in ipairs{'TEXMFVAR','TEXMF_OUTPUT_DIRECTORY','.','TEXMFOUTPUT'} do |
| 107 |
local var = i == 3 and v or kpse.var_value(v) |
| 108 |
if var and var ~= "" then |
| 109 |
for _,vv in ipairs(var:explode(os.type == "unix" and ":" or ";")) do |
| 110 |
local dir = format("%s/%s",vv,"luamplib_cache") |
| 111 |
if not lfsisdir(dir) then |
| 112 |
mk_full_path(dir) |
| 113 |
end |
| 114 |
if is_writable(dir) then |
| 115 |
cachedir = dir; return cachedir |
| 116 |
end |
| 117 |
end |
| 118 |
end |
| 119 |
end |
| 120 |
end |
| 121 |
cachedir = "."; return cachedir |
| 122 |
end |
| 123 |
function luamplib.getcachedir(dir) |
| 124 |
dir = dir:gsub("##","#") |
| 125 |
dir = dir:gsub("^~", |
| 126 |
os.type == "windows" and os.getenv("UserProfile") or os.getenv("HOME")) |
| 127 |
if lfstouch and dir then |
| 128 |
if lfsisdir(dir) then |
| 129 |
if is_writable(dir) then |
| 130 |
cachedir = dir |
| 131 |
else |
| 132 |
warn("Directory '%s' is not writable!", dir) |
| 133 |
end |
| 134 |
else |
| 135 |
warn("Directory '%s' does not exist!", dir) |
| 136 |
end |
| 137 |
end |
| 138 |
end |
| 139 |
local noneedtoreplace = { |
| 140 |
["boxes.mp"] = true, -- ["format.mp"] = true, |
| 141 |
["graph.mp"] = true, ["marith.mp"] = true, ["mfplain.mp"] = true, |
| 142 |
["mpost.mp"] = true, ["plain.mp"] = true, ["rboxes.mp"] = true, |
| 143 |
["sarith.mp"] = true, ["string.mp"] = true, -- ["TEX.mp"] = true, |
| 144 |
["metafun.mp"] = true, ["metafun.mpiv"] = true, ["mp-abck.mpiv"] = true, |
| 145 |
["mp-apos.mpiv"] = true, ["mp-asnc.mpiv"] = true, ["mp-bare.mpiv"] = true, |
| 146 |
["mp-base.mpiv"] = true, ["mp-blob.mpiv"] = true, ["mp-butt.mpiv"] = true, |
| 147 |
["mp-char.mpiv"] = true, ["mp-chem.mpiv"] = true, ["mp-core.mpiv"] = true, |
| 148 |
["mp-crop.mpiv"] = true, ["mp-figs.mpiv"] = true, ["mp-form.mpiv"] = true, |
| 149 |
["mp-func.mpiv"] = true, ["mp-grap.mpiv"] = true, ["mp-grid.mpiv"] = true, |
| 150 |
["mp-grph.mpiv"] = true, ["mp-idea.mpiv"] = true, ["mp-luas.mpiv"] = true, |
| 151 |
["mp-mlib.mpiv"] = true, ["mp-node.mpiv"] = true, ["mp-page.mpiv"] = true, |
| 152 |
["mp-shap.mpiv"] = true, ["mp-step.mpiv"] = true, ["mp-text.mpiv"] = true, |
| 153 |
["mp-tool.mpiv"] = true, ["mp-cont.mpiv"] = true, |
| 154 |
} |
| 155 |
luamplib.noneedtoreplace = noneedtoreplace |
| 156 |
|
| 157 |
local name_b = "%f[%a_]" |
| 158 |
local name_e = "%f[^%a_]" |
| 159 |
local btex_etex = name_b.."btex"..name_e.."%s*(.-)%s*"..name_b.."etex"..name_e |
| 160 |
local verbatimtex_etex = name_b.."verbatimtex"..name_e.."%s*(.-)%s*"..name_b.."etex"..name_e |
| 161 |
|
| 162 |
local currenttime = os.time() |
| 163 |
do |
| 164 |
local luamplibtime = lfsattributes(kpse.find_file"luamplib.lua", "modification") |
| 165 |
local function replaceformatmp(file,newfile,ofmodify) |
| 166 |
local fh = ioopen(file,"r") |
| 167 |
if not fh then return file end |
| 168 |
local data = fh:read("*all"); fh:close() |
| 169 |
fh = ioopen(newfile,"w") |
| 170 |
if not fh then return file end |
| 171 |
fh:write( |
| 172 |
"let normalinfont = infont;\n", |
| 173 |
"primarydef str infont name = rawtextext(str) enddef;\n", |
| 174 |
data, |
| 175 |
"vardef Fmant_(expr x) = rawtextext(decimal abs x) enddef;\n", |
| 176 |
"vardef Fexp_(expr x) = rawtextext(\"$^{\"&decimal x&\"}$\") enddef;\n", |
| 177 |
"let infont = normalinfont;\n" |
| 178 |
); fh:close() |
| 179 |
lfstouch(newfile,currenttime,ofmodify) |
| 180 |
return newfile |
| 181 |
end |
| 182 |
local function replaceinputmpfile (name,file) |
| 183 |
local ofmodify = lfsattributes(file,"modification") |
| 184 |
if not ofmodify then return file end |
| 185 |
local newfile = name:gsub("%W","_") |
| 186 |
newfile = format("%s/luamplib_input_%s", cachedir or outputdir(), newfile) |
| 187 |
if newfile and luamplibtime then |
| 188 |
local nf = lfsattributes(newfile) |
| 189 |
if nf and nf.mode == "file" and |
| 190 |
ofmodify == nf.modification and luamplibtime < nf.access then |
| 191 |
return nf.size == 0 and file or newfile |
| 192 |
end |
| 193 |
end |
| 194 |
if name == "format.mp" then return replaceformatmp(file,newfile,ofmodify) end |
| 195 |
local fh = ioopen(file,"r") |
| 196 |
if not fh then return file end |
| 197 |
local data = fh:read("*all"); fh:close() |
| 198 |
local count,cnt = 0,0 |
| 199 |
data, cnt = data:gsub(btex_etex, "btex %1 etex ") -- space |
| 200 |
count = count + cnt |
| 201 |
data, cnt = data:gsub(verbatimtex_etex, "verbatimtex %1 etex;") -- semicolon |
| 202 |
count = count + cnt |
| 203 |
if count == 0 then |
| 204 |
noneedtoreplace[name] = true |
| 205 |
fh = ioopen(newfile,"w"); |
| 206 |
if fh then |
| 207 |
fh:close() |
| 208 |
lfstouch(newfile,currenttime,ofmodify) |
| 209 |
end |
| 210 |
return file |
| 211 |
end |
| 212 |
fh = ioopen(newfile,"w") |
| 213 |
if not fh then return file end |
| 214 |
fh:write(data); fh:close() |
| 215 |
lfstouch(newfile,currenttime,ofmodify) |
| 216 |
return newfile |
| 217 |
end |
| 218 |
local mpkpse |
| 219 |
do |
| 220 |
local exe = 0 |
| 221 |
while arg[exe-1] do |
| 222 |
exe = exe-1 |
| 223 |
end |
| 224 |
mpkpse = kpse.new(arg[exe], "mpost") |
| 225 |
end |
| 226 |
local special_ftype = { |
| 227 |
pfb = "type1 fonts", |
| 228 |
enc = "enc files", |
| 229 |
} |
| 230 |
function luamplib.finder (name, mode, ftype) |
| 231 |
if mode == "w" then |
| 232 |
if name and name ~= "mpout.log" then |
| 233 |
kpse.record_output_file(name) -- recorder |
| 234 |
end |
| 235 |
return name |
| 236 |
else |
| 237 |
ftype = special_ftype[ftype] or ftype |
| 238 |
local file = mpkpse:find_file(name,ftype) |
| 239 |
if file then |
| 240 |
if lfstouch and ftype == "mp" and not noneedtoreplace[name] and not noneedtoreplace["*.mp"] then |
| 241 |
file = replaceinputmpfile(name,file) |
| 242 |
end |
| 243 |
else |
| 244 |
file = mpkpse:find_file(name, name:match("%a+$")) |
| 245 |
end |
| 246 |
if file then |
| 247 |
kpse.record_input_file(file) -- recorder |
| 248 |
end |
| 249 |
return file |
| 250 |
end |
| 251 |
end |
| 252 |
end |
| 253 |
|
| 254 |
local currentformat = "plain" |
| 255 |
function luamplib.setformat (name) |
| 256 |
currentformat = name |
| 257 |
end |
| 258 |
luamplib.codeinherit = false |
| 259 |
local mplibinstances = {} |
| 260 |
luamplib.instances = mplibinstances |
| 261 |
local has_instancename = false |
| 262 |
|
| 263 |
local process |
| 264 |
do |
| 265 |
local function reporterror (result, prevlog) |
| 266 |
if not result then |
| 267 |
err("no result object returned") |
| 268 |
else |
| 269 |
local t, e, l = result.term, result.error, result.log |
| 270 |
local log = l or t or "no-term" |
| 271 |
log = log:gsub("%(Please type a command or say `end'%)",""):gsub("\n+","\n") |
| 272 |
if result.status > 0 then |
| 273 |
local first = log:match"(.-\n! .-)\n! " |
| 274 |
if first then |
| 275 |
termorlog("term", first) |
| 276 |
termorlog("log", log, "Warning") |
| 277 |
else |
| 278 |
warn(log) |
| 279 |
end |
| 280 |
if result.status > 1 then |
| 281 |
err(e or "see above messages") |
| 282 |
end |
| 283 |
elseif prevlog then |
| 284 |
log = prevlog..log |
| 285 |
local show = log:match"\n>>? .+" |
| 286 |
if show then |
| 287 |
termorlog("term", show, "Info (more info in the log)") |
| 288 |
info(log) |
| 289 |
elseif luamplib.showlog and log:find"%g" then |
| 290 |
info(log) |
| 291 |
end |
| 292 |
end |
| 293 |
return log |
| 294 |
end |
| 295 |
end |
| 296 |
if not math.initialseed then math.randomseed(currenttime) end |
| 297 |
local function luamplibload (name) |
| 298 |
local mpx = mplib.new { |
| 299 |
ini_version = true, |
| 300 |
find_file = luamplib.finder, |
| 301 |
make_text = luamplib.maketext, |
| 302 |
run_script = luamplib.runscript, |
| 303 |
math_mode = luamplib.numbersystem, |
| 304 |
job_name = tex.jobname, |
| 305 |
random_seed = math.random(4095), |
| 306 |
utf8_mode = true, |
| 307 |
extensions = 1, |
| 308 |
} |
| 309 |
local preamble = tableconcat{ |
| 310 |
format(luamplib.preambles.preamble, replacesuffix(name,"mp")), |
| 311 |
luamplib.preambles.mplibcode, |
| 312 |
luamplib.legacyverbatimtex and luamplib.preambles.legacyverbatimtex or "", |
| 313 |
luamplib.textextlabel and luamplib.preambles.textextlabel or "", |
| 314 |
} |
| 315 |
local result, log |
| 316 |
if not mpx then |
| 317 |
result = { status = 99, error = "out of memory"} |
| 318 |
else |
| 319 |
result = mpx:execute(preamble) |
| 320 |
end |
| 321 |
log = reporterror(result) |
| 322 |
return mpx, result, log |
| 323 |
end |
| 324 |
local process_stack = 0 |
| 325 |
function process (data, instancename) |
| 326 |
local currfmt |
| 327 |
process_stack = process_stack + 1 |
| 328 |
if instancename and instancename ~= "" then |
| 329 |
currfmt = instancename |
| 330 |
has_instancename = true |
| 331 |
else |
| 332 |
currfmt = tableconcat{ |
| 333 |
currentformat, |
| 334 |
luamplib.numbersystem or "scaled", |
| 335 |
tostring(luamplib.textextlabel), |
| 336 |
tostring(luamplib.legacyverbatimtex), |
| 337 |
tostring(process_stack), -- try to address #63 |
| 338 |
} |
| 339 |
has_instancename = false |
| 340 |
end |
| 341 |
local mpx = mplibinstances[currfmt] |
| 342 |
local standalone = not (has_instancename or luamplib.codeinherit) |
| 343 |
if mpx and standalone then |
| 344 |
mpx:finish() |
| 345 |
end |
| 346 |
local log = "" |
| 347 |
if standalone or not mpx then |
| 348 |
mpx, _, log = luamplibload(currentformat) |
| 349 |
mplibinstances[currfmt] = mpx |
| 350 |
end |
| 351 |
local converted, result = false, {} |
| 352 |
if mpx and data then |
| 353 |
result = mpx:execute(data) |
| 354 |
local log = reporterror(result, log) |
| 355 |
if log then |
| 356 |
if result.fig then |
| 357 |
converted = luamplib.convert(result) |
| 358 |
end |
| 359 |
end |
| 360 |
else |
| 361 |
err"Mem file unloadable. Maybe generated with a different version of mplib?" |
| 362 |
end |
| 363 |
process_stack = process_stack - 1 |
| 364 |
return converted, result |
| 365 |
end |
| 366 |
end |
| 367 |
|
| 368 |
local pdfmode = tex.outputmode > 0 |
| 369 |
|
| 370 |
local catlatex = luatexbase.registernumber("catcodetable@latex") |
| 371 |
local catat11 = luatexbase.registernumber("catcodetable@atletter") |
| 372 |
local function run_tex_code (str, cat) |
| 373 |
texruntoks(function() texsprint(cat or catlatex, str) end) |
| 374 |
end |
| 375 |
local factor = 65536*(7227/7200) |
| 376 |
|
| 377 |
local texboxes = { globalid = 0, localid = 4096 } |
| 378 |
local process_tex_text |
| 379 |
do |
| 380 |
local textext_fmt = 'image(addto currentpicture doublepath unitsquare \z |
| 381 |
xscaled %f yscaled %f shifted (0,-%f) \z |
| 382 |
withprescript "mplibtexboxid=%i:%f:%f")' |
| 383 |
function process_tex_text (str, maketext) |
| 384 |
if str then |
| 385 |
if not maketext then str = str:gsub("\r.-$","") end |
| 386 |
local global = (has_instancename or luamplib.globaltextext or luamplib.codeinherit) |
| 387 |
and "\\global" or "" |
| 388 |
local tex_box_id |
| 389 |
if global == "" then |
| 390 |
tex_box_id = texboxes.localid + 1 |
| 391 |
texboxes.localid = tex_box_id |
| 392 |
else |
| 393 |
local boxid = texboxes.globalid + 1 |
| 394 |
texboxes.globalid = boxid |
| 395 |
run_tex_code(format([[\expandafter\newbox\csname luamplib.box.%s\endcsname]], boxid)) |
| 396 |
tex_box_id = tex.getcount'allocationnumber' |
| 397 |
end |
| 398 |
if str:find"^%[taggingoff%]" then |
| 399 |
str = str:gsub("^%[taggingoff%]%s*","") |
| 400 |
run_tex_code(format("\\luamplibnotagtextboxset{%i}{%s\\setbox%i\\hbox{%s}}", |
| 401 |
tex_box_id, global, tex_box_id, str)) |
| 402 |
else |
| 403 |
run_tex_code(format("\\luamplibtagtextboxset{%i}{%s\\setbox%i\\hbox{%s}}", |
| 404 |
tex_box_id, global, tex_box_id, str)) |
| 405 |
end |
| 406 |
local box = texgetbox(tex_box_id) |
| 407 |
local wd = box.width / factor |
| 408 |
local ht = box.height / factor |
| 409 |
local dp = box.depth / factor |
| 410 |
return textext_fmt:format(wd, ht+dp, dp, tex_box_id, wd, ht+dp) |
| 411 |
end |
| 412 |
return "" |
| 413 |
end |
| 414 |
end |
| 415 |
|
| 416 |
if is_defined'color_select:n' then |
| 417 |
run_tex_code{ |
| 418 |
"\\newcatcodetable\\luamplibcctabexplat", |
| 419 |
"\\begingroup", |
| 420 |
"\\catcode`@=11 ", |
| 421 |
"\\catcode`_=11 ", |
| 422 |
"\\catcode`:=11 ", |
| 423 |
"\\savecatcodetable\\luamplibcctabexplat", |
| 424 |
"\\endgroup", |
| 425 |
} |
| 426 |
end |
| 427 |
local ccexplat = luatexbase.registernumber"luamplibcctabexplat" |
| 428 |
|
| 429 |
local process_color, process_mplibcolor |
| 430 |
local function colorsplit (res) |
| 431 |
local t, tt = { }, res:gsub("[%[%]]","",2):explode() |
| 432 |
local be = tt[1]:find"^%d" and 1 or 2 |
| 433 |
for i=be, #tt do |
| 434 |
if not tonumber(tt[i]) then break end |
| 435 |
t[#t+1] = tt[i] |
| 436 |
end |
| 437 |
if #t == 0 then -- named color in DVI mode with no DocumentMetadata |
| 438 |
run_tex_code{"\\extractcolorspecs{", tt[3], "}\\mplibtmpa\\mplibtmpb"} |
| 439 |
t = get_macro"mplibtmpb":explode"," |
| 440 |
end |
| 441 |
return t |
| 442 |
end |
| 443 |
do |
| 444 |
local colfmt = ccexplat and "l3color" or "xcolor" |
| 445 |
local mplibcolorfmt = { |
| 446 |
xcolor = tableconcat{ |
| 447 |
[[\begingroup\let\XC@mcolor\relax]], |
| 448 |
[[\def\set@color{\global\mplibtmptoks\expandafter{\current@color}}]], |
| 449 |
[[\color%s\endgroup]], |
| 450 |
}, |
| 451 |
l3color = tableconcat{ |
| 452 |
[[\begingroup\def\__color_select:N#1{\expandafter\__color_select:nn#1}]], |
| 453 |
[[\def\__color_backend_select:nn#1#2{\global\mplibtmptoks{#1 #2}}]], |
| 454 |
[[\def\__kernel_backend_literal:e#1{\global\mplibtmptoks\expandafter{\expanded{#1}}}]], |
| 455 |
[[\color_select:n%s\endgroup]], |
| 456 |
}, |
| 457 |
} |
| 458 |
function process_color (str) |
| 459 |
if str then |
| 460 |
if not str:find("%b{}") then |
| 461 |
str = format("{%s}",str) |
| 462 |
end |
| 463 |
local myfmt = mplibcolorfmt[colfmt] |
| 464 |
if colfmt == "l3color" and is_defined"color" then |
| 465 |
if str:find("%b[]") then |
| 466 |
myfmt = mplibcolorfmt.xcolor |
| 467 |
else |
| 468 |
for _,v in ipairs(str:match"{(.+)}":explode"!") do |
| 469 |
if not v:find("^%s*%d+%s*$") then |
| 470 |
local pp = get_macro(format("l__color_named_%s_prop",v)) |
| 471 |
if not pp or pp == "" then |
| 472 |
myfmt = mplibcolorfmt.xcolor |
| 473 |
break |
| 474 |
end |
| 475 |
end |
| 476 |
end |
| 477 |
end |
| 478 |
end |
| 479 |
run_tex_code(myfmt:format(str), ccexplat or catat11) |
| 480 |
local t = texgettoks"mplibtmptoks" |
| 481 |
if not pdfmode then |
| 482 |
if t:find"^hsb" or not t:find"%d" then |
| 483 |
t = "color push " .. t |
| 484 |
elseif not t:find"^pdf" then |
| 485 |
t = t:gsub("%a+ (.+)","pdf:bc [%1]") |
| 486 |
end |
| 487 |
end |
| 488 |
return format('1 withprescript "mpliboverridecolor=%s"', t) |
| 489 |
end |
| 490 |
return "" |
| 491 |
end |
| 492 |
function process_mplibcolor(str) |
| 493 |
local res = process_color(str) |
| 494 |
if res:find" cs " or res:find"@pdf.obj" or res:find"color push" then return res end |
| 495 |
res = colorsplit(res:match'"mpliboverridecolor=(.+)"') |
| 496 |
return format("(%s)", tableconcat(res, ",")) |
| 497 |
end |
| 498 |
end |
| 499 |
|
| 500 |
local function process_dimen (str) |
| 501 |
if str then |
| 502 |
str = str:gsub("{(.+)}","%1") |
| 503 |
run_tex_code(format([[\mplibtmptoks\expandafter{\the\dimexpr %s\relax}]], str)) |
| 504 |
return format("begingroup %s endgroup", texgettoks"mplibtmptoks") |
| 505 |
end |
| 506 |
return "" |
| 507 |
end |
| 508 |
|
| 509 |
local function process_verbatimtex_text (str) |
| 510 |
if str then |
| 511 |
run_tex_code(str) |
| 512 |
end |
| 513 |
return "" |
| 514 |
end |
| 515 |
|
| 516 |
local tex_code_pre_mplib = {} |
| 517 |
luamplib.figid = 1 |
| 518 |
luamplib.in_the_fig = false |
| 519 |
local function process_verbatimtex_prefig (str) |
| 520 |
if str then |
| 521 |
tex_code_pre_mplib[luamplib.figid] = str |
| 522 |
end |
| 523 |
return "" |
| 524 |
end |
| 525 |
local function process_verbatimtex_infig (str) |
| 526 |
if str then |
| 527 |
return format('special "postmplibverbtex=%s";', str) |
| 528 |
end |
| 529 |
return "" |
| 530 |
end |
| 531 |
|
| 532 |
mp = mp or {} |
| 533 |
local mp = mp |
| 534 |
mp.mf_path_reset = mp.mf_path_reset or function() end |
| 535 |
mp.mf_finish_saving_data = mp.mf_finish_saving_data or function() end |
| 536 |
mp.report = mp.report or info |
| 537 |
catcodes = catcodes or {} |
| 538 |
local catcodes = catcodes |
| 539 |
catcodes.numbers = catcodes.numbers or {} |
| 540 |
catcodes.numbers.ctxcatcodes = catcodes.numbers.ctxcatcodes or catlatex |
| 541 |
catcodes.numbers.texcatcodes = catcodes.numbers.texcatcodes or catlatex |
| 542 |
catcodes.numbers.luacatcodes = catcodes.numbers.luacatcodes or catlatex |
| 543 |
catcodes.numbers.notcatcodes = catcodes.numbers.notcatcodes or catlatex |
| 544 |
catcodes.numbers.vrbcatcodes = catcodes.numbers.vrbcatcodes or catlatex |
| 545 |
catcodes.numbers.prtcatcodes = catcodes.numbers.prtcatcodes or catlatex |
| 546 |
catcodes.numbers.txtcatcodes = catcodes.numbers.txtcatcodes or catlatex |
| 547 |
|
| 548 |
do |
| 549 |
local runscript_funcs = { |
| 550 |
luamplibtext = process_tex_text, |
| 551 |
luamplibcolor = process_mplibcolor, |
| 552 |
luamplibdimen = process_dimen, |
| 553 |
luamplibprefig = process_verbatimtex_prefig, |
| 554 |
luamplibinfig = process_verbatimtex_infig, |
| 555 |
luamplibverbtex = process_verbatimtex_text, |
| 556 |
} |
| 557 |
local function mpprint(buffer,...) |
| 558 |
for i=1,select("#",...) do |
| 559 |
local value = select(i,...) |
| 560 |
if value ~= nil then |
| 561 |
local t = type(value) |
| 562 |
if t == "number" then |
| 563 |
buffer[#buffer+1] = format("%.16f",value) |
| 564 |
elseif t == "string" then |
| 565 |
buffer[#buffer+1] = value |
| 566 |
elseif t == "table" then |
| 567 |
buffer[#buffer+1] = "(" .. tableconcat(value,",") .. ")" |
| 568 |
else -- boolean or whatever |
| 569 |
buffer[#buffer+1] = tostring(value) |
| 570 |
end |
| 571 |
end |
| 572 |
end |
| 573 |
end |
| 574 |
function luamplib.runscript (code) |
| 575 |
local id, str = code:match("(.-){(.*)}") |
| 576 |
if id and str then |
| 577 |
local f = runscript_funcs[id] |
| 578 |
if f then |
| 579 |
local t = f(str) |
| 580 |
if t then return t end |
| 581 |
end |
| 582 |
end |
| 583 |
local f = loadstring(code) |
| 584 |
if type(f) == "function" then |
| 585 |
local buffer = {} |
| 586 |
function mp.print(...) |
| 587 |
mpprint(buffer,...) |
| 588 |
end |
| 589 |
local res = {f()} |
| 590 |
buffer = tableconcat(buffer) |
| 591 |
if buffer and buffer ~= "" then |
| 592 |
return buffer |
| 593 |
end |
| 594 |
buffer = {} |
| 595 |
mpprint(buffer, tableunpack(res)) |
| 596 |
return tableconcat(buffer) |
| 597 |
end |
| 598 |
return "" |
| 599 |
end |
| 600 |
end |
| 601 |
|
| 602 |
luamplib.legacyverbatimtex = true |
| 603 |
do |
| 604 |
local function protecttexcontents (str) |
| 605 |
return str:gsub("\\%%", "\0PerCent\0") |
| 606 |
:gsub("%%.-\n", "") |
| 607 |
:gsub("%%.-$", "") |
| 608 |
:gsub("%zPerCent%z", "\\%%") |
| 609 |
:gsub("\r.-$", "") |
| 610 |
:gsub("%s+", " ") |
| 611 |
end |
| 612 |
function luamplib.maketext (str, what) |
| 613 |
if str and str ~= "" then |
| 614 |
str = protecttexcontents(str) |
| 615 |
if what == 1 then |
| 616 |
if not str:find("\\documentclass"..name_e) and |
| 617 |
not str:find("\\begin%s*{document}") and |
| 618 |
not str:find("\\documentstyle"..name_e) and |
| 619 |
not str:find("\\usepackage"..name_e) then |
| 620 |
if luamplib.legacyverbatimtex then |
| 621 |
if luamplib.in_the_fig then |
| 622 |
return process_verbatimtex_infig(str) |
| 623 |
else |
| 624 |
return process_verbatimtex_prefig(str) |
| 625 |
end |
| 626 |
else |
| 627 |
return process_verbatimtex_text(str) |
| 628 |
end |
| 629 |
end |
| 630 |
else |
| 631 |
return process_tex_text(str, true) -- bool is for 'char13' |
| 632 |
end |
| 633 |
end |
| 634 |
return "" |
| 635 |
end |
| 636 |
end |
| 637 |
|
| 638 |
luamplib.gettexcolor = function (str, rgb) |
| 639 |
local res = process_color(str):match'"mpliboverridecolor=(.+)"' |
| 640 |
if res:find" cs " or res:find"@pdf.obj" then |
| 641 |
if not rgb then |
| 642 |
warn("%s is a spot color. Forced to CMYK", str) |
| 643 |
end |
| 644 |
run_tex_code({ |
| 645 |
"\\color_export:nnN{", |
| 646 |
str, |
| 647 |
"}{", |
| 648 |
rgb and "space-sep-rgb" or "space-sep-cmyk", |
| 649 |
"}\\mplib_@tempa", |
| 650 |
},ccexplat) |
| 651 |
return get_macro"mplib_@tempa":explode() |
| 652 |
end |
| 653 |
local t = colorsplit(res) |
| 654 |
if #t == 3 or not rgb then return t end |
| 655 |
if #t == 4 then |
| 656 |
return { 1 - math.min(1,t[1]+t[4]), 1 - math.min(1,t[2]+t[4]), 1 - math.min(1,t[3]+t[4]) } |
| 657 |
end |
| 658 |
return { t[1], t[1], t[1] } |
| 659 |
end |
| 660 |
|
| 661 |
luamplib.shadecolor = function (str) |
| 662 |
local res = process_color(str):match'"mpliboverridecolor=(.+)"' |
| 663 |
if res:find" cs " or res:find"@pdf.obj" then -- spot color shade: l3 only |
| 664 |
run_tex_code({ |
| 665 |
[[\color_export:nnN{]], str, [[}{backend}\mplib_@tempa]], |
| 666 |
},ccexplat) |
| 667 |
local name, value = get_macro'mplib_@tempa':match'{(.-)}{(.-)}' |
| 668 |
local t, obj = res:explode() |
| 669 |
if pdfmode then |
| 670 |
obj = format("%s 0 R", ltx.pdf.object_id( t[1]:sub(2,-1) )) |
| 671 |
else |
| 672 |
obj = t[2] |
| 673 |
end |
| 674 |
return format('(1) withprescript"mplib_spotcolor=%s:%s:%s"', value,obj,name) |
| 675 |
end |
| 676 |
return colorsplit(res) |
| 677 |
end |
| 678 |
|
| 679 |
do |
| 680 |
local function graphictextcolor (col, filldraw) |
| 681 |
if col:find"^[%d%.:]+$" then |
| 682 |
col = col:explode":" |
| 683 |
for i=1,#col do |
| 684 |
col[i] = format("%.3f", col[i]) |
| 685 |
end |
| 686 |
if pdfmode then |
| 687 |
local op = #col == 4 and "k" or #col == 3 and "rg" or "g" |
| 688 |
col[#col+1] = filldraw == "fill" and op or op:upper() |
| 689 |
return tableconcat(col," ") |
| 690 |
end |
| 691 |
return format("[%s]", tableconcat(col," ")) |
| 692 |
end |
| 693 |
col = process_color(col):match'"mpliboverridecolor=(.+)"' |
| 694 |
if pdfmode then |
| 695 |
local t = col:explode() |
| 696 |
local b = filldraw == "fill" and 1 or #t/2+1 |
| 697 |
local e = b == 1 and #t/2 or #t |
| 698 |
return tableconcat(t," ", b, e) |
| 699 |
end |
| 700 |
if col:find"@pdf.obj" then |
| 701 |
return col:gsub("pdf:bc%s*","",1) |
| 702 |
else |
| 703 |
return format("[%s]", tableconcat(colorsplit(col)," ")) |
| 704 |
end |
| 705 |
end |
| 706 |
function luamplib.fillandstrokecolor (fill, stroke) |
| 707 |
fill = graphictextcolor(fill, "fill") |
| 708 |
stroke = graphictextcolor(stroke, "stroke") |
| 709 |
local bc = pdfmode and "" or "pdf:bc " |
| 710 |
return format('withprescript "mpliboverridecolor=%s%s %s"', bc, fill, stroke) |
| 711 |
end |
| 712 |
end |
| 713 |
|
| 714 |
local decimals = "%.%d+" |
| 715 |
local function rmzeros(str) return str:gsub("%.?0+$","") end |
| 716 |
|
| 717 |
local function getrulemetric (box, curr, bp) |
| 718 |
local running = -1073741824 |
| 719 |
local wd,ht,dp = curr.width, curr.height, curr.depth |
| 720 |
wd = wd == running and box.width or wd |
| 721 |
ht = ht == running and box.height or ht |
| 722 |
dp = dp == running and box.depth or dp |
| 723 |
if bp then |
| 724 |
return wd/factor, ht/factor, dp/factor |
| 725 |
end |
| 726 |
return wd, ht, dp |
| 727 |
end |
| 728 |
|
| 729 |
do |
| 730 |
if not math.round then |
| 731 |
function math.round(x) return x < 0 and -math.floor(-x + 0.5) or math.floor(x + 0.5) end |
| 732 |
end |
| 733 |
local emboldenfonts = { } |
| 734 |
local function roundupwidth (f, fb) |
| 735 |
local wd = math.round(f.size * fb / factor * 10) |
| 736 |
if wd == 0 and fb ~= 0 then |
| 737 |
wd = 1 |
| 738 |
end |
| 739 |
emboldenfonts.width = wd |
| 740 |
return wd |
| 741 |
end |
| 742 |
local function getemboldenwidth (curr, fakebold) |
| 743 |
local width = emboldenfonts.width |
| 744 |
if not width then |
| 745 |
local f |
| 746 |
local function getglyph(n) |
| 747 |
while n do |
| 748 |
if n.head then |
| 749 |
getglyph(n.head) |
| 750 |
elseif n.font and n.font > 0 then |
| 751 |
f = n.font; break |
| 752 |
end |
| 753 |
n = node.getnext(n) |
| 754 |
end |
| 755 |
end |
| 756 |
getglyph(curr) |
| 757 |
width = roundupwidth(font.getcopy(f or font.current()), fakebold) |
| 758 |
end |
| 759 |
return width |
| 760 |
end |
| 761 |
local function getrulewhatsit (line, wd, ht, dp) |
| 762 |
line, wd, ht, dp = line/1000, wd/factor, ht/factor, dp/factor |
| 763 |
line = line == 0 and "" or ("%f w"):format(line) |
| 764 |
local pl |
| 765 |
local fmt = "q %s %f %f %f %f re B Q" |
| 766 |
if pdfmode then |
| 767 |
pl = node.new("whatsit","pdf_literal") |
| 768 |
pl.mode = 0 |
| 769 |
else |
| 770 |
fmt = "pdf:content "..fmt |
| 771 |
pl = node.new("whatsit","special") |
| 772 |
end |
| 773 |
pl.data = fmt:format(line, 0, -dp, wd, ht+dp) :gsub(decimals,rmzeros) |
| 774 |
local ss = node.new"glue" |
| 775 |
node.setglue(ss, 0, 65536, 65536, 2, 2) |
| 776 |
pl.next = ss |
| 777 |
return pl |
| 778 |
end |
| 779 |
local tag_update_attrs |
| 780 |
if is_defined"[email protected]" then |
| 781 |
tag_update_attrs = function (n, curr) |
| 782 |
while n do |
| 783 |
n.attr = curr.attr |
| 784 |
if n.head then |
| 785 |
tag_update_attrs(n.head, curr) |
| 786 |
end |
| 787 |
n = node.getnext(n) |
| 788 |
end |
| 789 |
end |
| 790 |
else |
| 791 |
tag_update_attrs = function() end |
| 792 |
end |
| 793 |
local function embolden (box, curr, fakebold) |
| 794 |
local head = curr |
| 795 |
while curr do |
| 796 |
if curr.head then |
| 797 |
curr.head = embolden(curr, curr.head, fakebold) |
| 798 |
elseif curr.replace then |
| 799 |
curr.replace = embolden(box, curr.replace, fakebold) |
| 800 |
elseif curr.leader then |
| 801 |
if curr.leader.head then |
| 802 |
curr.leader.head = embolden(curr.leader, curr.leader.head, fakebold) |
| 803 |
elseif curr.leader.id == node.id"rule" then |
| 804 |
local glue = node.effective_glue(curr, box) |
| 805 |
local line = getemboldenwidth(curr, fakebold) |
| 806 |
local wd,ht,dp = getrulemetric(box, curr.leader) |
| 807 |
if box.id == node.id"hlist" then |
| 808 |
wd = glue |
| 809 |
else |
| 810 |
ht, dp = 0, glue |
| 811 |
end |
| 812 |
local pl = getrulewhatsit(line, wd, ht, dp) |
| 813 |
local pack = box.id == node.id"hlist" and node.hpack or node.vpack |
| 814 |
local list = pack(pl, glue, "exactly") |
| 815 |
tag_update_attrs(list,curr) |
| 816 |
head = node.insert_after(head, curr, list) |
| 817 |
head, curr = node.remove(head, curr) |
| 818 |
end |
| 819 |
elseif curr.id == node.id"rule" and curr.subtype == 0 then |
| 820 |
local line = getemboldenwidth(curr, fakebold) |
| 821 |
local wd,ht,dp = getrulemetric(box, curr) |
| 822 |
if box.id == node.id"vlist" then |
| 823 |
ht, dp = 0, ht+dp |
| 824 |
end |
| 825 |
local pl = getrulewhatsit(line, wd, ht, dp) |
| 826 |
local list |
| 827 |
if box.id == node.id"hlist" then |
| 828 |
list = node.hpack(pl, wd, "exactly") |
| 829 |
else |
| 830 |
list = node.vpack(pl, ht+dp, "exactly") |
| 831 |
end |
| 832 |
tag_update_attrs(list,curr) |
| 833 |
head = node.insert_after(head, curr, list) |
| 834 |
head, curr = node.remove(head, curr) |
| 835 |
elseif curr.id == node.id"glyph" and curr.font > 0 then |
| 836 |
local f = curr.font |
| 837 |
local key = format("%s:%s",f,fakebold) |
| 838 |
local i = emboldenfonts[key] |
| 839 |
if not i then |
| 840 |
local ft = font.getfont(f) or font.getcopy(f) |
| 841 |
local width = roundupwidth(ft, fakebold) |
| 842 |
if ft.format == "opentype" or ft.format == "truetype" then |
| 843 |
local name = ft.name:gsub('"',''):gsub(';$','') |
| 844 |
local t = name:gsub("^file:",""):gsub("^name:",""):gsub("^kpse:",""):gsub("^my:","") |
| 845 |
name = format('%s%sembolden=%s;',name, t:find":" and ";" or ":", fakebold) |
| 846 |
_, i = fonts.constructors.readanddefine(name,ft.size) |
| 847 |
elseif pdfmode then |
| 848 |
local ft = table.copy(ft) |
| 849 |
ft.mode, ft.width = 2, width |
| 850 |
i = font.define(ft) |
| 851 |
else |
| 852 |
goto skip_type1 |
| 853 |
end |
| 854 |
emboldenfonts[key] = i |
| 855 |
end |
| 856 |
curr.font = i |
| 857 |
end |
| 858 |
::skip_type1:: |
| 859 |
curr = node.getnext(curr) |
| 860 |
end |
| 861 |
return head |
| 862 |
end |
| 863 |
luamplib.graphictext = function (text, fakebold, fc, dc) |
| 864 |
local fmt = process_tex_text(text):sub(1,-2) |
| 865 |
local id = tonumber(fmt:match"mplibtexboxid=(%d+):") |
| 866 |
emboldenfonts.width = nil |
| 867 |
local box = texgetbox(id) |
| 868 |
box.head = embolden(box, box.head, fakebold) |
| 869 |
local colors = luamplib.fillandstrokecolor(fc, dc) |
| 870 |
return format('%s %s)', fmt, colors) |
| 871 |
end |
| 872 |
end |
| 873 |
|
| 874 |
do |
| 875 |
local function mperr (str) |
| 876 |
return format("hide(errmessage %q)", str) |
| 877 |
end |
| 878 |
local function getangle (a,b,c) |
| 879 |
local r = math.deg(math.atan(c.y-b.y, c.x-b.x) - math.atan(b.y-a.y, b.x-a.x)) |
| 880 |
if r > 180 then |
| 881 |
r = r - 360 |
| 882 |
elseif r < -180 then |
| 883 |
r = r + 360 |
| 884 |
end |
| 885 |
return r |
| 886 |
end |
| 887 |
local function turning (t) |
| 888 |
local r, n = 0, #t |
| 889 |
for i=1,2 do |
| 890 |
tableinsert(t, t[i]) |
| 891 |
end |
| 892 |
for i=1,n do |
| 893 |
r = r + getangle(t[i], t[i+1], t[i+2]) |
| 894 |
end |
| 895 |
return r/360 |
| 896 |
end |
| 897 |
local function glyphimage(t, fmt) |
| 898 |
local q, p, r, towarn = {{},{}} |
| 899 |
local function closepath(dots) |
| 900 |
tableinsert(p, format("%scycle", dots or "--")) |
| 901 |
tableinsert(q[ turning(r) > 0 and 1 or 2 ], tableconcat(p)) |
| 902 |
end |
| 903 |
for i,v in ipairs(t) do |
| 904 |
local cmd = v[#v] |
| 905 |
local nt = t[i+1] |
| 906 |
local final = not nt or nt[#nt] ~= "l" and nt[#nt] ~= "c" |
| 907 |
if cmd == "m" then |
| 908 |
if final then towarn = true end |
| 909 |
p = {format('(%s,%s)',v[1],v[2])} |
| 910 |
r = {{x=v[1],y=v[2]}} |
| 911 |
else |
| 912 |
if cmd == "l" then |
| 913 |
local pt = t[i-1] |
| 914 |
if (final or pt and pt[#pt] == "m") and r[1].x == v[1] and r[1].y == v[2] then |
| 915 |
else |
| 916 |
tableinsert(p, format('--(%s,%s)',v[1],v[2])) |
| 917 |
tableinsert(r, {x=v[1],y=v[2]}) |
| 918 |
end |
| 919 |
if final then closepath() end |
| 920 |
elseif cmd == "c" then |
| 921 |
tableinsert(p, format('..controls(%s,%s)and(%s,%s)',v[1],v[2],v[3],v[4])) |
| 922 |
if final and r[1].x == v[5] and r[1].y == v[6] then |
| 923 |
closepath ".." |
| 924 |
else |
| 925 |
tableinsert(p, format('..(%s,%s)',v[5],v[6])) |
| 926 |
tableinsert(r, {x=v[5],y=v[6]}) |
| 927 |
if final then closepath() end |
| 928 |
end |
| 929 |
elseif cmd == "path" or cmd == "move" then |
| 930 |
else |
| 931 |
return mperr"unknown operator" |
| 932 |
end |
| 933 |
end |
| 934 |
end |
| 935 |
r = { } |
| 936 |
if fmt == "opentype" then |
| 937 |
for _,v in ipairs(q[1]) do |
| 938 |
tableinsert(r, format('addto currentpicture contour %s;',v)) |
| 939 |
end |
| 940 |
for _,v in ipairs(q[2]) do |
| 941 |
tableinsert(r, format('addto currentpicture contour %s withcolor background;',v)) |
| 942 |
end |
| 943 |
else |
| 944 |
for _,v in ipairs(q[2]) do |
| 945 |
tableinsert(r, format('addto currentpicture contour %s;',v)) |
| 946 |
end |
| 947 |
for _,v in ipairs(q[1]) do |
| 948 |
tableinsert(r, format('addto currentpicture contour %s withcolor background;',v)) |
| 949 |
end |
| 950 |
end |
| 951 |
return format('image(%s)', tableconcat(r)), towarn |
| 952 |
end |
| 953 |
if not table.tofile then require"lualibs-lpeg"; require"lualibs-table"; end |
| 954 |
function luamplib.glyph (f, c) |
| 955 |
local filename, subfont, instance, kind, shapedata |
| 956 |
local fid = tonumber(f) or font.id(f) |
| 957 |
if fid > 0 then |
| 958 |
local fontdata = font.getfont(fid) or font.getcopy(fid) |
| 959 |
filename, subfont, kind = fontdata.filename, fontdata.subfont, fontdata.format |
| 960 |
instance = fontdata.specification and fontdata.specification.instance |
| 961 |
or fontdata.shared and fontdata.shared.features.axis |
| 962 |
filename = filename and filename:gsub("^harfloaded:","") |
| 963 |
else |
| 964 |
local name |
| 965 |
f = f:match"^%s*(.+)%s*$" |
| 966 |
name, subfont, instance = f:match"(.+)%((%d+)%)%[(.-)%]$" |
| 967 |
if not name then |
| 968 |
name, instance = f:match"(.+)%[(.-)%]$" -- SourceHanSansK-VF.otf[Heavy] |
| 969 |
end |
| 970 |
if not name then |
| 971 |
name, subfont = f:match"(.+)%((%d+)%)$" -- Times.ttc(2) |
| 972 |
end |
| 973 |
name = name or f |
| 974 |
subfont = (subfont or 0)+1 |
| 975 |
instance = instance and instance:lower() |
| 976 |
for _,ftype in ipairs{"opentype", "truetype"} do |
| 977 |
filename = kpse.find_file(name, ftype.." fonts") |
| 978 |
if filename then |
| 979 |
kind = ftype; break |
| 980 |
end |
| 981 |
end |
| 982 |
end |
| 983 |
if kind ~= "opentype" and kind ~= "truetype" then |
| 984 |
f = fid and fid > 0 and tex.fontname(fid) or f |
| 985 |
if kpse.find_file(f, "tfm") then |
| 986 |
return format("glyph %s of %q", tonumber(c) or format("%q",c), f) |
| 987 |
else |
| 988 |
filename = kpse.find_file(f, "type1 fonts") |
| 989 |
if filename then |
| 990 |
kind = "type1" -- there's bug in processing cmr family |
| 991 |
else |
| 992 |
return mperr"font not found" |
| 993 |
end |
| 994 |
end |
| 995 |
end |
| 996 |
local time = lfsattributes(filename,"modification") |
| 997 |
local k = format("shapes_%s(%s)[%s]", filename, subfont or "", instance or "") |
| 998 |
local h = format(string.rep('%02x', 256/8), string.byte(sha2.digest256(k), 1, -1)) |
| 999 |
local newname = format("%s/%s.lua", cachedir or outputdir(), h) |
| 1000 |
local newtime = lfsattributes(newname,"modification") or 0 |
| 1001 |
if time == newtime then |
| 1002 |
shapedata = require(newname) |
| 1003 |
end |
| 1004 |
if not shapedata then |
| 1005 |
if fonts then |
| 1006 |
local handler = kind == "type1" and fonts.handlers.afm or fonts.handlers.otf |
| 1007 |
shapedata = handler.readers.loadshapes(filename,subfont,instance) |
| 1008 |
end |
| 1009 |
if not shapedata then return mperr"loadshapes() failed. luaotfload not loaded?" end |
| 1010 |
table.tofile(newname, shapedata, "return") |
| 1011 |
lfstouch(newname, time, time) |
| 1012 |
end |
| 1013 |
local gid = tonumber(c) |
| 1014 |
if not gid then |
| 1015 |
local uni = utf8.codepoint(c) |
| 1016 |
for i,v in pairs(shapedata.glyphs) do |
| 1017 |
if c == v.name or uni == v.unicode then |
| 1018 |
gid = i; break |
| 1019 |
end |
| 1020 |
end |
| 1021 |
end |
| 1022 |
if not gid then return mperr"cannot get GID (glyph id)" end |
| 1023 |
local fac = 1000 / (shapedata.units or 1000) |
| 1024 |
local t = shapedata.glyphs[gid]; t = t and t.segments |
| 1025 |
if not t then return "image()" end |
| 1026 |
for i,v in ipairs(t) do |
| 1027 |
if type(v) == "table" then |
| 1028 |
for ii,vv in ipairs(v) do |
| 1029 |
if type(vv) == "number" then |
| 1030 |
t[i][ii] = format("%.0f", vv * fac) |
| 1031 |
end |
| 1032 |
end |
| 1033 |
end |
| 1034 |
end |
| 1035 |
local result, towarn = glyphimage(t, shapedata.format or kind) |
| 1036 |
if towarn then |
| 1037 |
warn("mplibglyph %s not working properly. Use glyph instead", f) |
| 1038 |
end |
| 1039 |
return result |
| 1040 |
end |
| 1041 |
end |
| 1042 |
|
| 1043 |
do |
| 1044 |
local rulefmt = "mpliboutlinepic[%i]:=image(addto currentpicture contour \z |
| 1045 |
unitsquare shifted - center unitsquare;) xscaled %f yscaled %f shifted (%f,%f);" |
| 1046 |
local outline_horz, outline_vert |
| 1047 |
function outline_vert (res, box, curr, xshift, yshift) |
| 1048 |
local b2u = box.dir == "LTL" |
| 1049 |
local dy = (b2u and -box.depth or box.height)/factor |
| 1050 |
local ody = dy |
| 1051 |
while curr do |
| 1052 |
if curr.id == node.id"rule" then |
| 1053 |
local wd, ht, dp = getrulemetric(box, curr, true) |
| 1054 |
local hd = ht + dp |
| 1055 |
if hd ~= 0 then |
| 1056 |
dy = dy + (b2u and dp or -ht) |
| 1057 |
if wd ~= 0 and curr.subtype == 0 then |
| 1058 |
res[#res+1] = rulefmt:format(#res+1, wd, hd, xshift+wd/2, yshift+dy+(ht-dp)/2) |
| 1059 |
end |
| 1060 |
dy = dy + (b2u and ht or -dp) |
| 1061 |
end |
| 1062 |
elseif curr.id == node.id"glue" then |
| 1063 |
local vwidth = node.effective_glue(curr,box)/factor |
| 1064 |
if curr.leader then |
| 1065 |
local curr, kind = curr.leader, curr.subtype |
| 1066 |
if curr.id == node.id"rule" then |
| 1067 |
local wd = getrulemetric(box, curr, true) |
| 1068 |
if wd ~= 0 then |
| 1069 |
local hd = vwidth |
| 1070 |
local dy = dy + (b2u and 0 or -hd) |
| 1071 |
if hd ~= 0 and curr.subtype == 0 then |
| 1072 |
res[#res+1] = rulefmt:format(#res+1, wd, hd, xshift+wd/2, yshift+dy+hd/2) |
| 1073 |
end |
| 1074 |
end |
| 1075 |
elseif curr.head then |
| 1076 |
local hd = (curr.height + curr.depth)/factor |
| 1077 |
if hd <= vwidth then |
| 1078 |
local dy, n, iy = dy, 0, 0 |
| 1079 |
if kind == 100 or kind == 103 then -- todo: gleaders |
| 1080 |
local ady = abs(ody - dy) |
| 1081 |
local ndy = math.ceil(ady / hd) * hd |
| 1082 |
local diff = ndy - ady |
| 1083 |
n = math.floor((vwidth-diff) / hd) |
| 1084 |
dy = dy + (b2u and diff or -diff) |
| 1085 |
else |
| 1086 |
n = math.floor(vwidth / hd) |
| 1087 |
if kind == 101 then |
| 1088 |
local side = vwidth % hd / 2 |
| 1089 |
dy = dy + (b2u and side or -side) |
| 1090 |
elseif kind == 102 then |
| 1091 |
iy = vwidth % hd / (n+1) |
| 1092 |
dy = dy + (b2u and iy or -iy) |
| 1093 |
end |
| 1094 |
end |
| 1095 |
dy = dy + (b2u and curr.depth or -curr.height)/factor |
| 1096 |
hd = b2u and hd or -hd |
| 1097 |
iy = b2u and iy or -iy |
| 1098 |
local func = curr.id == node.id"hlist" and outline_horz or outline_vert |
| 1099 |
for i=1,n do |
| 1100 |
res = func(res, curr, curr.head, xshift+curr.shift/factor, yshift+dy) |
| 1101 |
dy = dy + hd + iy |
| 1102 |
end |
| 1103 |
end |
| 1104 |
end |
| 1105 |
end |
| 1106 |
dy = dy + (b2u and vwidth or -vwidth) |
| 1107 |
elseif curr.id == node.id"kern" then |
| 1108 |
dy = dy + curr.kern/factor * (b2u and 1 or -1) |
| 1109 |
elseif curr.id == node.id"vlist" then |
| 1110 |
dy = dy + (b2u and curr.depth or -curr.height)/factor |
| 1111 |
res = outline_vert(res, curr, curr.head, xshift+curr.shift/factor, yshift+dy) |
| 1112 |
dy = dy + (b2u and curr.height or -curr.depth)/factor |
| 1113 |
elseif curr.id == node.id"hlist" then |
| 1114 |
dy = dy + (b2u and curr.depth or -curr.height)/factor |
| 1115 |
res = outline_horz(res, curr, curr.head, xshift+curr.shift/factor, yshift+dy) |
| 1116 |
dy = dy + (b2u and curr.height or -curr.depth)/factor |
| 1117 |
end |
| 1118 |
curr = node.getnext(curr) |
| 1119 |
end |
| 1120 |
return res |
| 1121 |
end |
| 1122 |
function outline_horz (res, box, curr, xshift, yshift, discwd) |
| 1123 |
local r2l = box.dir == "TRT" |
| 1124 |
local dx = r2l and (discwd or box.width/factor) or 0 |
| 1125 |
local dirs = { { dir = r2l, dx = dx } } |
| 1126 |
while curr do |
| 1127 |
if curr.id == node.id"dir" then |
| 1128 |
local sign, dir = curr.dir:match"(.)(...)" |
| 1129 |
local level, newdir = curr.level, r2l |
| 1130 |
if sign == "+" then |
| 1131 |
newdir = dir == "TRT" |
| 1132 |
if r2l ~= newdir then |
| 1133 |
local n = node.getnext(curr) |
| 1134 |
while n do |
| 1135 |
if n.id == node.id"dir" and n.level+1 == level then break end |
| 1136 |
n = node.getnext(n) |
| 1137 |
end |
| 1138 |
n = n or node.tail(curr) |
| 1139 |
dx = dx + node.rangedimensions(box, curr, n)/factor * (newdir and 1 or -1) |
| 1140 |
end |
| 1141 |
dirs[level] = { dir = r2l, dx = dx } |
| 1142 |
else |
| 1143 |
local level = level + 1 |
| 1144 |
newdir = dirs[level].dir |
| 1145 |
if r2l ~= newdir then |
| 1146 |
dx = dirs[level].dx |
| 1147 |
end |
| 1148 |
end |
| 1149 |
r2l = newdir |
| 1150 |
elseif curr.char and curr.font and curr.font > 0 then |
| 1151 |
local ft = font.getfont(curr.font) or font.getcopy(curr.font) |
| 1152 |
local gid = ft.characters[curr.char].index or curr.char |
| 1153 |
local scale = ft.size / factor / 1000 |
| 1154 |
local slant = (ft.slant or 0)/1000 |
| 1155 |
local extend = (ft.extend or 1000)/1000 |
| 1156 |
local squeeze = (ft.squeeze or 1000)/1000 |
| 1157 |
local expand = 1 + (curr.expansion_factor or 0)/1000000 |
| 1158 |
local xscale, yscale = scale * extend * expand, scale * squeeze |
| 1159 |
dx = dx - (r2l and curr.width/factor*expand or 0) |
| 1160 |
local xoff, yoff = (curr.xoffset or 0)/factor, (curr.yoffset or 0)/factor |
| 1161 |
local xpos, ypos = dx + xshift + xoff, yshift + yoff |
| 1162 |
local vertical = "" |
| 1163 |
if ft.shared and (ft.shared.features.vert or ft.shared.features.vrt2) then |
| 1164 |
if ft.shared.features.vertical then -- luatexko |
| 1165 |
vertical = "rotated 90" |
| 1166 |
local data = ft.characters[curr.char] or { } |
| 1167 |
if ft.hb then |
| 1168 |
local hoff, voff = (data.luatexko_hoff or 0)/factor, (data.luatexko_voff or 0)/factor |
| 1169 |
local charraise = (ft.luatexko_charraise or 0)/factor |
| 1170 |
xpos, ypos = xpos - voff + hoff - charraise, ypos + hoff + voff + charraise |
| 1171 |
else |
| 1172 |
local cmds = data.commands or { {0,0}, {0,0} } |
| 1173 |
local voff, hoff = -cmds[1][2]/factor, cmds[2][2]/factor |
| 1174 |
xpos, ypos = xpos + hoff, ypos + voff |
| 1175 |
end |
| 1176 |
elseif curr ~= box.head then -- luatexja |
| 1177 |
vertical = "rotated 90" |
| 1178 |
local en = ft.parameters.quad/factor/2 |
| 1179 |
xpos, ypos = xpos - xoff - yoff + en, ypos - yoff + xoff - en |
| 1180 |
end |
| 1181 |
end |
| 1182 |
local image |
| 1183 |
if ft.format == "opentype" or ft.format == "truetype" then |
| 1184 |
image = luamplib.glyph(curr.font, gid) |
| 1185 |
else |
| 1186 |
local name, scale = ft.name, 1 |
| 1187 |
local vf = font.read_vf(name, ft.size) |
| 1188 |
if vf and vf.characters[gid] then |
| 1189 |
local cmds = vf.characters[gid].commands or {} |
| 1190 |
for _,v in ipairs(cmds) do |
| 1191 |
if v[1] == "char" then |
| 1192 |
gid = v[2] |
| 1193 |
elseif v[1] == "font" and vf.fonts[v[2]] then |
| 1194 |
name = vf.fonts[v[2]].name |
| 1195 |
scale = vf.fonts[v[2]].size / ft.size |
| 1196 |
end |
| 1197 |
end |
| 1198 |
end |
| 1199 |
image = format("glyph %s of %q scaled %f", gid, name, scale) |
| 1200 |
end |
| 1201 |
res[#res+1] = format("mpliboutlinepic[%i]:=%s xscaled %f yscaled %f slanted %f %s shifted (%f,%f);", |
| 1202 |
#res+1, image, xscale, yscale, slant, vertical, xpos, ypos) |
| 1203 |
dx = dx + (r2l and 0 or curr.width/factor*expand) |
| 1204 |
elseif curr.replace then |
| 1205 |
local width = node.dimensions(curr.replace)/factor |
| 1206 |
dx = dx - (r2l and width or 0) |
| 1207 |
res = outline_horz(res, box, curr.replace, xshift+dx, yshift, width) |
| 1208 |
dx = dx + (r2l and 0 or width) |
| 1209 |
elseif curr.id == node.id"rule" then |
| 1210 |
local wd, ht, dp = getrulemetric(box, curr, true) |
| 1211 |
if wd ~= 0 then |
| 1212 |
local hd = ht + dp |
| 1213 |
dx = dx - (r2l and wd or 0) |
| 1214 |
if hd ~= 0 and curr.subtype == 0 then |
| 1215 |
res[#res+1] = rulefmt:format(#res+1, wd, hd, xshift+dx+wd/2, yshift+(ht-dp)/2) |
| 1216 |
end |
| 1217 |
dx = dx + (r2l and 0 or wd) |
| 1218 |
end |
| 1219 |
elseif curr.id == node.id"glue" then |
| 1220 |
local width = node.effective_glue(curr, box)/factor |
| 1221 |
dx = dx - (r2l and width or 0) |
| 1222 |
if curr.leader then |
| 1223 |
local curr, kind = curr.leader, curr.subtype |
| 1224 |
if curr.id == node.id"rule" then |
| 1225 |
local wd, ht, dp = getrulemetric(box, curr, true) |
| 1226 |
local hd = ht + dp |
| 1227 |
if hd ~= 0 then |
| 1228 |
wd = width |
| 1229 |
if wd ~= 0 and curr.subtype == 0 then |
| 1230 |
res[#res+1] = rulefmt:format(#res+1, wd, hd, xshift+dx+wd/2, yshift+(ht-dp)/2) |
| 1231 |
end |
| 1232 |
end |
| 1233 |
elseif curr.head then |
| 1234 |
local wd = curr.width/factor |
| 1235 |
if wd <= width then |
| 1236 |
local dx = r2l and dx+width or dx |
| 1237 |
local n, ix = 0, 0 |
| 1238 |
if kind == 100 or kind == 103 then -- todo: gleaders |
| 1239 |
local adx = abs(dx-dirs[1].dx) |
| 1240 |
local ndx = math.ceil(adx / wd) * wd |
| 1241 |
local diff = ndx - adx |
| 1242 |
n = math.floor((width-diff) / wd) |
| 1243 |
dx = dx + (r2l and -diff-wd or diff) |
| 1244 |
else |
| 1245 |
n = math.floor(width / wd) |
| 1246 |
if kind == 101 then |
| 1247 |
local side = width % wd /2 |
| 1248 |
dx = dx + (r2l and -side-wd or side) |
| 1249 |
elseif kind == 102 then |
| 1250 |
ix = width % wd / (n+1) |
| 1251 |
dx = dx + (r2l and -ix-wd or ix) |
| 1252 |
end |
| 1253 |
end |
| 1254 |
wd = r2l and -wd or wd |
| 1255 |
ix = r2l and -ix or ix |
| 1256 |
local func = curr.id == node.id"hlist" and outline_horz or outline_vert |
| 1257 |
for i=1,n do |
| 1258 |
res = func(res, curr, curr.head, xshift+dx, yshift-curr.shift/factor) |
| 1259 |
dx = dx + wd + ix |
| 1260 |
end |
| 1261 |
end |
| 1262 |
end |
| 1263 |
end |
| 1264 |
dx = dx + (r2l and 0 or width) |
| 1265 |
elseif curr.id == node.id"kern" then |
| 1266 |
dx = dx + curr.kern/factor * (r2l and -1 or 1) |
| 1267 |
elseif curr.id == node.id"math" then |
| 1268 |
dx = dx + curr.surround/factor * (r2l and -1 or 1) |
| 1269 |
elseif curr.id == node.id"vlist" then |
| 1270 |
dx = dx - (r2l and curr.width/factor or 0) |
| 1271 |
res = outline_vert(res, curr, curr.head, xshift+dx, yshift-curr.shift/factor) |
| 1272 |
dx = dx + (r2l and 0 or curr.width/factor) |
| 1273 |
elseif curr.id == node.id"hlist" then |
| 1274 |
dx = dx - (r2l and curr.width/factor or 0) |
| 1275 |
res = outline_horz(res, curr, curr.head, xshift+dx, yshift-curr.shift/factor) |
| 1276 |
dx = dx + (r2l and 0 or curr.width/factor) |
| 1277 |
end |
| 1278 |
curr = node.getnext(curr) |
| 1279 |
end |
| 1280 |
return res |
| 1281 |
end |
| 1282 |
function luamplib.outlinetext (text) |
| 1283 |
local fmt = process_tex_text(text) |
| 1284 |
local id = tonumber(fmt:match"mplibtexboxid=(%d+):") |
| 1285 |
local box = texgetbox(id) |
| 1286 |
local res = outline_horz({ }, box, box.head, 0, 0) |
| 1287 |
if #res == 0 then res = { "mpliboutlinepic[1]:=image();" } end |
| 1288 |
return tableconcat(res) .. format("mpliboutlinenum:=%i;", #res) |
| 1289 |
end |
| 1290 |
end |
| 1291 |
|
| 1292 |
function luamplib.getunicodegraphemes (s) |
| 1293 |
local t = { } |
| 1294 |
local graphemes = require'lua-uni-graphemes' |
| 1295 |
for _, _, c in graphemes.graphemes(s) do |
| 1296 |
table.insert(t, c) |
| 1297 |
end |
| 1298 |
return t |
| 1299 |
end |
| 1300 |
function luamplib.unicodesubstring (s,b,e,grph) |
| 1301 |
local tt, t, step = { } |
| 1302 |
if grph then |
| 1303 |
t = luamplib.getunicodegraphemes(s) |
| 1304 |
else |
| 1305 |
t = { } |
| 1306 |
for _, c in utf8.codes(s) do |
| 1307 |
table.insert(t, utf8.char(c)) |
| 1308 |
end |
| 1309 |
end |
| 1310 |
if b <= e then |
| 1311 |
b, step = b+1, 1 |
| 1312 |
else |
| 1313 |
e, step = e+1, -1 |
| 1314 |
end |
| 1315 |
for i = b, e, step do |
| 1316 |
table.insert(tt, t[i]) |
| 1317 |
end |
| 1318 |
s = table.concat(tt):gsub('"','"&ditto&"') |
| 1319 |
return string.format('"%s"', s) |
| 1320 |
end |
| 1321 |
|
| 1322 |
luamplib.preambles = { |
| 1323 |
preamble = [[ |
| 1324 |
boolean mplib ; mplib := true ; |
| 1325 |
let dump = endinput ; |
| 1326 |
let normalfontsize = fontsize; |
| 1327 |
input %s ; |
| 1328 |
]], |
| 1329 |
mplibcode = [[ |
| 1330 |
texscriptmode := 2; |
| 1331 |
def rawtextext primary t = runscript("luamplibtext{"&t&"}") enddef; |
| 1332 |
def mplibcolor primary t = runscript("luamplibcolor{"&t&"}") enddef; |
| 1333 |
def mplibdimen primary t = runscript("luamplibdimen{"&t&"}") enddef; |
| 1334 |
def VerbatimTeX primary t = runscript("luamplibverbtex{"&t&"}") enddef; |
| 1335 |
if known context_mlib: |
| 1336 |
defaultfont := "cmtt10"; |
| 1337 |
let infont = normalinfont; |
| 1338 |
let fontsize = normalfontsize; |
| 1339 |
vardef thelabel@#(expr p,z) = |
| 1340 |
if string p : |
| 1341 |
thelabel@#(p infont defaultfont scaled defaultscale,z) |
| 1342 |
else : |
| 1343 |
p shifted (z + labeloffset*mfun_laboff@# - |
| 1344 |
(mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + |
| 1345 |
(1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) |
| 1346 |
fi |
| 1347 |
enddef; |
| 1348 |
else: |
| 1349 |
vardef textext@# primary t = rawtextext (t) enddef; |
| 1350 |
def message expr t = |
| 1351 |
if string t: runscript("mp.report[=["&t&"]=]") else: errmessage "Not a string" fi |
| 1352 |
enddef; |
| 1353 |
def withtransparency (expr a, t) = |
| 1354 |
withprescript "tr_alternative=" & if numeric a: decimal fi a |
| 1355 |
withprescript "tr_transparency=" & decimal t |
| 1356 |
enddef; |
| 1357 |
vardef ddecimal primary p = |
| 1358 |
decimal xpart p & " " & decimal ypart p |
| 1359 |
enddef; |
| 1360 |
vardef boundingbox primary p = |
| 1361 |
if (path p) or (picture p) : |
| 1362 |
llcorner p -- lrcorner p -- urcorner p -- ulcorner p |
| 1363 |
else : |
| 1364 |
origin |
| 1365 |
fi -- cycle |
| 1366 |
enddef; |
| 1367 |
fi |
| 1368 |
def resolvedcolor(expr s) = |
| 1369 |
runscript("return luamplib.shadecolor('"& s &"')") |
| 1370 |
enddef; |
| 1371 |
def colordecimals primary c = |
| 1372 |
if cmykcolor c: |
| 1373 |
decimal cyanpart c & ":" & decimal magentapart c & ":" & |
| 1374 |
decimal yellowpart c & ":" & decimal blackpart c |
| 1375 |
elseif rgbcolor c: |
| 1376 |
decimal redpart c & ":" & decimal greenpart c & ":" & decimal bluepart c |
| 1377 |
elseif string c: |
| 1378 |
if known graphictextpic: c else: colordecimals resolvedcolor(c) fi |
| 1379 |
else: |
| 1380 |
decimal c |
| 1381 |
fi |
| 1382 |
enddef; |
| 1383 |
def externalfigure primary filename = |
| 1384 |
draw rawtextext("\includegraphics{"& filename &"}") |
| 1385 |
enddef; |
| 1386 |
def TEX = textext enddef; |
| 1387 |
def mplibtexcolor primary c = |
| 1388 |
runscript("return luamplib.gettexcolor('"& c &"')") |
| 1389 |
enddef; |
| 1390 |
def mplibrgbtexcolor primary c = |
| 1391 |
runscript("return luamplib.gettexcolor('"& c &"','rgb')") |
| 1392 |
enddef; |
| 1393 |
def mplibgraphictext primary t = |
| 1394 |
begingroup; |
| 1395 |
mplibgraphictext_ (t) |
| 1396 |
enddef; |
| 1397 |
def mplibgraphictext_ (expr t) text rest = |
| 1398 |
save fakebold, scale, fillcolor, drawcolor, withfillcolor, withdrawcolor, strokecolor, |
| 1399 |
fb, fc, dc, graphictextpic, alsoordoublepath; |
| 1400 |
picture graphictextpic; graphictextpic := nullpicture; |
| 1401 |
numeric fb; string fc, dc; fb:=2; fc:="white"; dc:="black"; |
| 1402 |
let scale = scaled; |
| 1403 |
def fakebold primary c = hide(fb:=c;) enddef; |
| 1404 |
def fillcolor primary c = hide(fc:=colordecimals c;) enddef; |
| 1405 |
def drawcolor primary c = hide(dc:=colordecimals c;) enddef; |
| 1406 |
let withfillcolor = fillcolor; let withdrawcolor = drawcolor; let strokecolor = drawcolor; |
| 1407 |
def alsoordoublepath expr p = if picture p: also else: doublepath fi p enddef; |
| 1408 |
addto graphictextpic alsoordoublepath (origin--cycle) rest; graphictextpic:=nullpicture; |
| 1409 |
def fakebold primary c = enddef; |
| 1410 |
let fillcolor = fakebold; let drawcolor = fakebold; |
| 1411 |
let withfillcolor = fillcolor; let withdrawcolor = drawcolor; let strokecolor = drawcolor; |
| 1412 |
image(draw runscript("return luamplib.graphictext([===["&t&"]===]," |
| 1413 |
& decimal fb &",'"& fc &"','"& dc &"')") rest;) |
| 1414 |
endgroup; |
| 1415 |
enddef; |
| 1416 |
def mplibglyph expr c of f = |
| 1417 |
runscript ( |
| 1418 |
"return luamplib.glyph('" |
| 1419 |
& if numeric f: decimal fi f |
| 1420 |
& "','" |
| 1421 |
& if numeric c: decimal fi c |
| 1422 |
& "')" |
| 1423 |
) |
| 1424 |
enddef; |
| 1425 |
numeric luamplib_tmp_num_; luamplib_tmp_num_ = 0; |
| 1426 |
def mplibdrawglyph expr g = |
| 1427 |
luamplib_tmp_num_ := 0; |
| 1428 |
for item within g: |
| 1429 |
fill pathpart item |
| 1430 |
if incr luamplib_tmp_num_ < length g: withpostscript "collect"; fi |
| 1431 |
endfor |
| 1432 |
enddef; |
| 1433 |
let mplibfillglyph = mplibdrawglyph; |
| 1434 |
def mplibstrokeglyph expr g = |
| 1435 |
luamplib_tmp_num_ := 0; |
| 1436 |
for item within g: |
| 1437 |
draw pathpart item |
| 1438 |
if incr luamplib_tmp_num_ < length g: withpostscript "collect"; fi |
| 1439 |
endfor |
| 1440 |
enddef; |
| 1441 |
def mplibfillandstrokeglyph expr g = |
| 1442 |
luamplib_tmp_num_ := 0; |
| 1443 |
for item within g: |
| 1444 |
draw pathpart item withpostscript |
| 1445 |
if incr luamplib_tmp_num_ < length g: "collect"; else: "both" fi |
| 1446 |
endfor |
| 1447 |
enddef; |
| 1448 |
def withmplibcolors (expr f, s) = |
| 1449 |
runscript("return luamplib.fillandstrokecolor('" & |
| 1450 |
if not string f: colordecimals fi f & "','" & |
| 1451 |
if not string s: colordecimals fi s & "')") |
| 1452 |
enddef; |
| 1453 |
def withmplibopacities (expr a, f, s) = |
| 1454 |
withprescript "tr_alternative=" & if numeric a: decimal fi a |
| 1455 |
withprescript "tr_transparency=" & decimal f & ":" & decimal s |
| 1456 |
enddef; |
| 1457 |
def mplib_do_outline_text_set_b (text f) (text d) text r = |
| 1458 |
def mplib_do_outline_options_f = f enddef; |
| 1459 |
def mplib_do_outline_options_d = d enddef; |
| 1460 |
def mplib_do_outline_options_r = r enddef; |
| 1461 |
enddef; |
| 1462 |
def mplib_do_outline_text_set_f (text f) text r = |
| 1463 |
def mplib_do_outline_options_f = f enddef; |
| 1464 |
def mplib_do_outline_options_r = r enddef; |
| 1465 |
enddef; |
| 1466 |
def mplib_do_outline_text_set_u (text f) text r = |
| 1467 |
def mplib_do_outline_options_f = f enddef; |
| 1468 |
enddef; |
| 1469 |
def mplib_do_outline_text_set_d (text d) text r = |
| 1470 |
def mplib_do_outline_options_d = d enddef; |
| 1471 |
def mplib_do_outline_options_r = r enddef; |
| 1472 |
enddef; |
| 1473 |
def mplib_do_outline_text_set_r (text d) (text f) text r = |
| 1474 |
def mplib_do_outline_options_d = d enddef; |
| 1475 |
def mplib_do_outline_options_f = f enddef; |
| 1476 |
def mplib_do_outline_options_r = r enddef; |
| 1477 |
enddef; |
| 1478 |
def mplib_do_outline_text_set_n text r = |
| 1479 |
def mplib_do_outline_options_r = r enddef; |
| 1480 |
enddef; |
| 1481 |
def mplib_do_outline_text_set_p = enddef; |
| 1482 |
def mplib_fill_outline_text = |
| 1483 |
for n=1 upto mpliboutlinenum: |
| 1484 |
i:=0; |
| 1485 |
for item within mpliboutlinepic[n]: |
| 1486 |
i:=i+1; |
| 1487 |
fill pathpart item mplib_do_outline_options_f withpen pencircle scaled 0 |
| 1488 |
if (n<mpliboutlinenum) or (i<length mpliboutlinepic[n]): withpostscript "collect"; fi |
| 1489 |
endfor |
| 1490 |
endfor |
| 1491 |
enddef; |
| 1492 |
def mplib_draw_outline_text = |
| 1493 |
for n=1 upto mpliboutlinenum: |
| 1494 |
for item within mpliboutlinepic[n]: |
| 1495 |
draw pathpart item mplib_do_outline_options_d; |
| 1496 |
endfor |
| 1497 |
endfor |
| 1498 |
enddef; |
| 1499 |
def mplib_filldraw_outline_text = |
| 1500 |
for n=1 upto mpliboutlinenum: |
| 1501 |
i:=0; |
| 1502 |
for item within mpliboutlinepic[n]: |
| 1503 |
i:=i+1; |
| 1504 |
if (n<mpliboutlinenum) or (i<length mpliboutlinepic[n]): |
| 1505 |
fill pathpart item mplib_do_outline_options_f withpostscript "collect"; |
| 1506 |
else: |
| 1507 |
draw pathpart item mplib_do_outline_options_f withpostscript "both"; |
| 1508 |
fi |
| 1509 |
endfor |
| 1510 |
endfor |
| 1511 |
enddef; |
| 1512 |
vardef mpliboutlinetext@# (expr t) text rest = |
| 1513 |
save kind; string kind; kind := str @#; |
| 1514 |
save i; numeric i; |
| 1515 |
picture mpliboutlinepic[]; numeric mpliboutlinenum; |
| 1516 |
def mplib_do_outline_options_d = enddef; |
| 1517 |
def mplib_do_outline_options_f = enddef; |
| 1518 |
def mplib_do_outline_options_r = enddef; |
| 1519 |
runscript("return luamplib.outlinetext[===["&t&"]===]"); |
| 1520 |
image ( addto currentpicture also image ( |
| 1521 |
if kind = "f": |
| 1522 |
mplib_do_outline_text_set_f rest; |
| 1523 |
mplib_fill_outline_text; |
| 1524 |
elseif kind = "d": |
| 1525 |
mplib_do_outline_text_set_d rest; |
| 1526 |
mplib_draw_outline_text; |
| 1527 |
elseif kind = "b": |
| 1528 |
mplib_do_outline_text_set_b rest; |
| 1529 |
mplib_fill_outline_text; |
| 1530 |
mplib_draw_outline_text; |
| 1531 |
elseif kind = "u": |
| 1532 |
mplib_do_outline_text_set_u rest; |
| 1533 |
mplib_filldraw_outline_text; |
| 1534 |
elseif kind = "r": |
| 1535 |
mplib_do_outline_text_set_r rest; |
| 1536 |
mplib_draw_outline_text; |
| 1537 |
mplib_fill_outline_text; |
| 1538 |
elseif kind = "p": |
| 1539 |
mplib_do_outline_text_set_p; |
| 1540 |
mplib_draw_outline_text; |
| 1541 |
else: |
| 1542 |
mplib_do_outline_text_set_n rest; |
| 1543 |
mplib_fill_outline_text; |
| 1544 |
fi; |
| 1545 |
) mplib_do_outline_options_r; ) |
| 1546 |
enddef ; |
| 1547 |
def withmppattern primary p = |
| 1548 |
withprescript "mplibpattern=" & if numeric p: decimal fi p |
| 1549 |
enddef; |
| 1550 |
primarydef t withpattern p = |
| 1551 |
image( |
| 1552 |
if cycle t: |
| 1553 |
fill |
| 1554 |
else: |
| 1555 |
draw |
| 1556 |
fi |
| 1557 |
t withprescript "mplibpattern=" & if numeric p: decimal fi p; ) |
| 1558 |
enddef; |
| 1559 |
vardef mplibtransformmatrix (text e) = |
| 1560 |
save t; transform t; |
| 1561 |
t = identity e; |
| 1562 |
runscript("luamplib.transformmatrix = {" |
| 1563 |
& decimal xxpart t & "," |
| 1564 |
& decimal yxpart t & "," |
| 1565 |
& decimal xypart t & "," |
| 1566 |
& decimal yypart t & "," |
| 1567 |
& decimal xpart t & "," |
| 1568 |
& decimal ypart t & "," |
| 1569 |
& "}"); |
| 1570 |
enddef; |
| 1571 |
primarydef p withmaskinggroup s = |
| 1572 |
if picture p: |
| 1573 |
image( |
| 1574 |
draw p; |
| 1575 |
draw center p withprescript "mplibfadestate=stop"; |
| 1576 |
) |
| 1577 |
else: |
| 1578 |
p withprescript "mplibfadestate=stop" |
| 1579 |
fi |
| 1580 |
withprescript "mplibfadetype=masking" |
| 1581 |
withprescript "mplibmaskname=" & s |
| 1582 |
enddef; |
| 1583 |
def withmaskingbgcolor expr c = |
| 1584 |
withprescript "mplibmaskingbgcolor=" & colordecimals c |
| 1585 |
enddef; |
| 1586 |
primarydef p withfademethod s = |
| 1587 |
if picture p: |
| 1588 |
image( |
| 1589 |
draw p; |
| 1590 |
draw center p withprescript "mplibfadestate=stop"; |
| 1591 |
) |
| 1592 |
else: |
| 1593 |
p withprescript "mplibfadestate=stop" |
| 1594 |
fi |
| 1595 |
withprescript "mplibfadetype=" & s |
| 1596 |
hide(mplib_shade_step := 1;) |
| 1597 |
withprescript "sh_color_a=1" |
| 1598 |
withprescript "sh_color_b=0" |
| 1599 |
withprescript "mplibfadebbox=" & |
| 1600 |
decimal (xpart llcorner p -1/4) & ":" & |
| 1601 |
decimal (ypart llcorner p -1/4) & ":" & |
| 1602 |
decimal (xpart urcorner p +1/4) & ":" & |
| 1603 |
decimal (ypart urcorner p +1/4) |
| 1604 |
enddef; |
| 1605 |
def withfadevector (expr a,b) = |
| 1606 |
withprescript "mplibfadevector=" & |
| 1607 |
decimal xpart a & ":" & |
| 1608 |
decimal ypart a & ":" & |
| 1609 |
decimal xpart b & ":" & |
| 1610 |
decimal ypart b |
| 1611 |
enddef; |
| 1612 |
let withfadecenter = withfadevector; |
| 1613 |
def withfaderadius (expr a,b) = |
| 1614 |
withprescript "mplibfaderadius=" & |
| 1615 |
decimal a & ":" & |
| 1616 |
decimal b |
| 1617 |
enddef; |
| 1618 |
def withfadebbox (expr a,b) = |
| 1619 |
withprescript "mplibfadebbox=" & |
| 1620 |
decimal xpart a & ":" & |
| 1621 |
decimal ypart a & ":" & |
| 1622 |
decimal xpart b & ":" & |
| 1623 |
decimal ypart b |
| 1624 |
enddef; |
| 1625 |
primarydef p asgroup s = |
| 1626 |
image( |
| 1627 |
draw center p |
| 1628 |
withprescript "mplibgroupbbox=" & |
| 1629 |
decimal (xpart llcorner p -1/4) & ":" & |
| 1630 |
decimal (ypart llcorner p -1/4) & ":" & |
| 1631 |
decimal (xpart urcorner p +1/4) & ":" & |
| 1632 |
decimal (ypart urcorner p +1/4) |
| 1633 |
withprescript "gr_state=start" |
| 1634 |
withprescript "gr_type=" & s; |
| 1635 |
draw p withprescript "sh_in_xobj=yes"; |
| 1636 |
draw center p withprescript "gr_state=stop"; |
| 1637 |
) |
| 1638 |
enddef; |
| 1639 |
def withgroupbbox (expr a,b) = |
| 1640 |
withprescript "mplibgroupbbox=" & |
| 1641 |
decimal xpart a & ":" & |
| 1642 |
decimal ypart a & ":" & |
| 1643 |
decimal xpart b & ":" & |
| 1644 |
decimal ypart b |
| 1645 |
enddef; |
| 1646 |
def withgroupname expr s = |
| 1647 |
withprescript "mplibgroupname=" & s |
| 1648 |
enddef; |
| 1649 |
def usemplibgroup primary s = |
| 1650 |
draw maketext("\luamplibtagasgroupput{"& s &"}{\csname luamplib.group."& s &"\endcsname}") |
| 1651 |
shifted runscript("return luamplib.trgroupshifts['" & s & "']") |
| 1652 |
enddef; |
| 1653 |
path mplib_shade_path ; |
| 1654 |
numeric mplib_shade_step ; mplib_shade_step := 0 ; |
| 1655 |
numeric mplib_shade_fx, mplib_shade_fy ; |
| 1656 |
numeric mplib_shade_lx, mplib_shade_ly ; |
| 1657 |
numeric mplib_shade_nx, mplib_shade_ny ; |
| 1658 |
numeric mplib_shade_dx, mplib_shade_dy ; |
| 1659 |
numeric mplib_shade_tx, mplib_shade_ty ; |
| 1660 |
primarydef p withshadingmethod m = |
| 1661 |
p |
| 1662 |
if picture p : |
| 1663 |
withprescript "sh_operand_type=picture" |
| 1664 |
if textual p or (length p > 1): |
| 1665 |
withprescript "sh_transform=no" |
| 1666 |
mplib_with_shade_method (boundingbox p, m) |
| 1667 |
else: |
| 1668 |
withprescript "sh_transform=yes" |
| 1669 |
mplib_with_shade_method (pathpart p, m) |
| 1670 |
fi |
| 1671 |
else : |
| 1672 |
withprescript "sh_transform=yes" |
| 1673 |
mplib_with_shade_method (p, m) |
| 1674 |
fi |
| 1675 |
enddef; |
| 1676 |
def mplib_with_shade_method (expr p, m) = |
| 1677 |
hide(mplib_with_shade_method_analyze(p)) |
| 1678 |
withprescript "sh_domain=0 1" |
| 1679 |
withprescript "sh_color=into" |
| 1680 |
withprescript "sh_color_a=" & colordecimals white |
| 1681 |
withprescript "sh_color_b=" & colordecimals black |
| 1682 |
withprescript "sh_first=" & ddecimal point 0 of p |
| 1683 |
withprescript "sh_set_x=" & ddecimal (mplib_shade_nx,mplib_shade_lx) |
| 1684 |
withprescript "sh_set_y=" & ddecimal (mplib_shade_ny,mplib_shade_ly) |
| 1685 |
if m = "linear" : |
| 1686 |
withprescript "sh_type=linear" |
| 1687 |
withprescript "sh_factor=1" |
| 1688 |
withprescript "sh_center_a=" & ddecimal llcorner p |
| 1689 |
withprescript "sh_center_b=" & ddecimal urcorner p |
| 1690 |
else : |
| 1691 |
withprescript "sh_type=circular" |
| 1692 |
withprescript "sh_factor=1.2" |
| 1693 |
withprescript "sh_center_a=" & ddecimal center p |
| 1694 |
withprescript "sh_center_b=" & ddecimal center p |
| 1695 |
withprescript "sh_radius_a=" & decimal 0 |
| 1696 |
withprescript "sh_radius_b=" & decimal mplib_max_radius(p) |
| 1697 |
fi |
| 1698 |
enddef; |
| 1699 |
def mplib_with_shade_method_analyze(expr p) = |
| 1700 |
mplib_shade_path := p ; |
| 1701 |
mplib_shade_step := 1 ; |
| 1702 |
mplib_shade_fx := xpart point 0 of p ; |
| 1703 |
mplib_shade_fy := ypart point 0 of p ; |
| 1704 |
mplib_shade_lx := mplib_shade_fx ; |
| 1705 |
mplib_shade_ly := mplib_shade_fy ; |
| 1706 |
mplib_shade_nx := 0 ; |
| 1707 |
mplib_shade_ny := 0 ; |
| 1708 |
mplib_shade_dx := abs(mplib_shade_fx - mplib_shade_lx) ; |
| 1709 |
mplib_shade_dy := abs(mplib_shade_fy - mplib_shade_ly) ; |
| 1710 |
for i=1 upto length(p) : |
| 1711 |
mplib_shade_tx := abs(mplib_shade_fx - xpart point i of p) ; |
| 1712 |
mplib_shade_ty := abs(mplib_shade_fy - ypart point i of p) ; |
| 1713 |
if mplib_shade_tx > mplib_shade_dx : |
| 1714 |
mplib_shade_nx := i + 1 ; |
| 1715 |
mplib_shade_lx := xpart point i of p ; |
| 1716 |
mplib_shade_dx := mplib_shade_tx ; |
| 1717 |
fi ; |
| 1718 |
if mplib_shade_ty > mplib_shade_dy : |
| 1719 |
mplib_shade_ny := i + 1 ; |
| 1720 |
mplib_shade_ly := ypart point i of p ; |
| 1721 |
mplib_shade_dy := mplib_shade_ty ; |
| 1722 |
fi ; |
| 1723 |
endfor ; |
| 1724 |
enddef; |
| 1725 |
vardef mplib_max_radius(expr p) = |
| 1726 |
max ( |
| 1727 |
(xpart center p - xpart llcorner p) ++ (ypart center p - ypart llcorner p), |
| 1728 |
(xpart center p - xpart ulcorner p) ++ (ypart ulcorner p - ypart center p), |
| 1729 |
(xpart lrcorner p - xpart center p) ++ (ypart center p - ypart lrcorner p), |
| 1730 |
(xpart urcorner p - xpart center p) ++ (ypart urcorner p - ypart center p) |
| 1731 |
) |
| 1732 |
enddef; |
| 1733 |
def withshadingstep (text t) = |
| 1734 |
hide(mplib_shade_step := mplib_shade_step + 1 ;) |
| 1735 |
withprescript "sh_step=" & decimal mplib_shade_step |
| 1736 |
t |
| 1737 |
enddef; |
| 1738 |
let withfadestep = withshadingstep; |
| 1739 |
def withshadingradius expr a = |
| 1740 |
withprescript "sh_radius_a=" & decimal (xpart a) |
| 1741 |
withprescript "sh_radius_b=" & decimal (ypart a) |
| 1742 |
enddef; |
| 1743 |
def withshadingorigin expr a = |
| 1744 |
withprescript "sh_center_a=" & ddecimal a |
| 1745 |
withprescript "sh_center_b=" & ddecimal a |
| 1746 |
enddef; |
| 1747 |
def withshadingvector expr a = |
| 1748 |
withprescript "sh_center_a=" & ddecimal (point xpart a of mplib_shade_path) |
| 1749 |
withprescript "sh_center_b=" & ddecimal (point ypart a of mplib_shade_path) |
| 1750 |
enddef; |
| 1751 |
def withshadingdirection expr a = |
| 1752 |
withprescript "sh_center_a=" & ddecimal (point xpart a of boundingbox(mplib_shade_path)) |
| 1753 |
withprescript "sh_center_b=" & ddecimal (point ypart a of boundingbox(mplib_shade_path)) |
| 1754 |
enddef; |
| 1755 |
def withshadingtransform expr a = |
| 1756 |
withprescript "sh_transform=" & a |
| 1757 |
enddef; |
| 1758 |
def withshadingcenter expr a = |
| 1759 |
withprescript "sh_center_a=" & ddecimal ( |
| 1760 |
center mplib_shade_path shifted ( |
| 1761 |
xpart a * xpart (lrcorner mplib_shade_path - llcorner mplib_shade_path)/2, |
| 1762 |
ypart a * ypart (urcorner mplib_shade_path - lrcorner mplib_shade_path)/2 |
| 1763 |
) |
| 1764 |
) |
| 1765 |
enddef; |
| 1766 |
def withshadingcenters (expr a, b) = |
| 1767 |
withprescript "sh_center_a=" & ddecimal a |
| 1768 |
withprescript "sh_center_b=" & ddecimal b |
| 1769 |
withshadingtransform "no" |
| 1770 |
withshadingfactor 1 |
| 1771 |
enddef; |
| 1772 |
let withshadingpoints = withshadingcenters; |
| 1773 |
def withshadingextend (expr a, b) = |
| 1774 |
withprescript "sh_extend=" & |
| 1775 |
if a: "true" else: "false" fi & " " & |
| 1776 |
if b: "true" else: "false" fi |
| 1777 |
enddef; |
| 1778 |
let withfadeextend = withshadingextend; |
| 1779 |
def withshadingdomain expr d = |
| 1780 |
withprescript "sh_domain=" & ddecimal d |
| 1781 |
enddef; |
| 1782 |
def withshadingfactor expr f = |
| 1783 |
withprescript "sh_factor=" & decimal f |
| 1784 |
enddef; |
| 1785 |
def withshadingfraction expr a = |
| 1786 |
if mplib_shade_step > 0 : |
| 1787 |
withprescript "sh_fraction_" & decimal mplib_shade_step & "=" & decimal a |
| 1788 |
fi |
| 1789 |
enddef; |
| 1790 |
let withfadefraction = withshadingfraction; |
| 1791 |
def withshadingcolors (expr a, b) = |
| 1792 |
if mplib_shade_step > 0 : |
| 1793 |
withprescript "sh_color=into" |
| 1794 |
withprescript "sh_color_a_" & decimal mplib_shade_step & "=" & colordecimals a |
| 1795 |
withprescript "sh_color_b_" & decimal mplib_shade_step & "=" & colordecimals b |
| 1796 |
else : |
| 1797 |
withprescript "sh_color=into" |
| 1798 |
withprescript "sh_color_a=" & colordecimals a |
| 1799 |
withprescript "sh_color_b=" & colordecimals b |
| 1800 |
fi |
| 1801 |
enddef; |
| 1802 |
let withfadeopacity = withshadingcolors; |
| 1803 |
def withshadingstroke expr a = |
| 1804 |
withprescript "sh_stroking=" & a |
| 1805 |
enddef; |
| 1806 |
def withshadingmatrix expr s = |
| 1807 |
withprescript "sh_matrix=" & s |
| 1808 |
enddef; |
| 1809 |
let withfadematrix = withshadingmatrix; |
| 1810 |
def mpliblength primary t = |
| 1811 |
runscript("return utf8.len[===[" & t & "]===]") |
| 1812 |
enddef; |
| 1813 |
def mplibsubstring expr p of t = |
| 1814 |
runscript("return luamplib.unicodesubstring([===[" & t & "]===]," |
| 1815 |
& decimal xpart p & "," |
| 1816 |
& decimal ypart p & ")") |
| 1817 |
enddef; |
| 1818 |
def mplibuclength primary t = |
| 1819 |
runscript("return #luamplib.getunicodegraphemes[===[" & t & "]===]") |
| 1820 |
enddef; |
| 1821 |
def mplibucsubstring expr p of t = |
| 1822 |
runscript("return luamplib.unicodesubstring([===[" & t & "]===]," |
| 1823 |
& decimal xpart p & "," |
| 1824 |
& decimal ypart p & ",true)") |
| 1825 |
enddef; |
| 1826 |
]], |
| 1827 |
legacyverbatimtex = [[ |
| 1828 |
def specialVerbatimTeX (text t) = runscript("luamplibprefig{"&t&"}") enddef; |
| 1829 |
def normalVerbatimTeX (text t) = runscript("luamplibinfig{"&t&"}") enddef; |
| 1830 |
let VerbatimTeX = specialVerbatimTeX; |
| 1831 |
extra_beginfig := extra_beginfig & " let VerbatimTeX = normalVerbatimTeX;"& |
| 1832 |
"runscript(" &ditto& "luamplib.in_the_fig=true" &ditto& ");"; |
| 1833 |
extra_endfig := extra_endfig & " let VerbatimTeX = specialVerbatimTeX;"& |
| 1834 |
"runscript(" &ditto& |
| 1835 |
"if luamplib.in_the_fig then luamplib.figid=luamplib.figid+1 end "& |
| 1836 |
"luamplib.in_the_fig=false" &ditto& ");"; |
| 1837 |
]], |
| 1838 |
textextlabel = [[ |
| 1839 |
let luampliboriginalinfont = infont; |
| 1840 |
primarydef s infont f = |
| 1841 |
if (s < char 32) |
| 1842 |
or (s = char 35) % # |
| 1843 |
or (s = char 36) % $ |
| 1844 |
or (s = char 37) % % |
| 1845 |
or (s = char 38) % & |
| 1846 |
or (s = char 92) % \ |
| 1847 |
or (s = char 94) % ^ |
| 1848 |
or (s = char 95) % _ |
| 1849 |
or (s = char 123) % { |
| 1850 |
or (s = char 125) % } |
| 1851 |
or (s = char 126) % ~ |
| 1852 |
or (s = char 127) : |
| 1853 |
s luampliboriginalinfont f |
| 1854 |
else : |
| 1855 |
rawtextext(s) |
| 1856 |
fi |
| 1857 |
enddef; |
| 1858 |
def fontsize expr f = |
| 1859 |
begingroup |
| 1860 |
save size; numeric size; |
| 1861 |
size := mplibdimen("1em"); |
| 1862 |
if size = 0: 10pt else: size fi |
| 1863 |
endgroup |
| 1864 |
enddef; |
| 1865 |
]], |
| 1866 |
} |
| 1867 |
|
| 1868 |
luamplib.verbatiminput = false |
| 1869 |
luamplib.everymplib = setmetatable({ [""] = "" },{ __index = function(t) return t[""] end }) |
| 1870 |
luamplib.everyendmplib = setmetatable({ [""] = "" },{ __index = function(t) return t[""] end }) |
| 1871 |
function luamplib.process_mplibcode (data, instancename) |
| 1872 |
texboxes.localid = 4096 |
| 1873 |
if luamplib.legacyverbatimtex then |
| 1874 |
luamplib.figid, tex_code_pre_mplib = 1, {} |
| 1875 |
end |
| 1876 |
local everymplib = luamplib.everymplib[instancename] |
| 1877 |
local everyendmplib = luamplib.everyendmplib[instancename] |
| 1878 |
data = format("\n%s\n%s\n%s\n",everymplib, data, everyendmplib) |
| 1879 |
:gsub("\r","\n") |
| 1880 |
if luamplib.verbatiminput then |
| 1881 |
data = data:gsub("\\mpcolor%s+(.-%b{})","mplibcolor(\"%1\")") |
| 1882 |
:gsub("\\mpdim%s+(%b{})", "mplibdimen(\"%1\")") |
| 1883 |
:gsub("\\mpdim%s+(\\%a+)","mplibdimen(\"%1\")") |
| 1884 |
:gsub(btex_etex, "btex %1 etex ") |
| 1885 |
:gsub(verbatimtex_etex, "verbatimtex %1 etex;") |
| 1886 |
else |
| 1887 |
local t = { } -- to store btex, verbatimtex, string |
| 1888 |
data = data:gsub(btex_etex, function(str) |
| 1889 |
t[#t+1] = str |
| 1890 |
return format("btex \\unexpanded{!l!u!a!%s!m!p!l!} etex ", #t) -- space |
| 1891 |
end) |
| 1892 |
:gsub(verbatimtex_etex, function(str) |
| 1893 |
t[#t+1] = str |
| 1894 |
return format("verbatimtex \\unexpanded{!l!u!a!%s!m!p!l!} etex;", #t) -- semicolon |
| 1895 |
end) |
| 1896 |
:gsub('"(.-)"', function(str) |
| 1897 |
t[#t+1] = str |
| 1898 |
return format('"\\unexpanded{!l!u!a!%s!m!p!l!}"', #t) |
| 1899 |
end) |
| 1900 |
:gsub("\\%%", "\0PerCent\0") |
| 1901 |
:gsub("%%.-\n","\n") |
| 1902 |
:gsub("%zPerCent%z", "\\%%") |
| 1903 |
run_tex_code(format("\\mplibtmptoks\\expandafter{\\expanded{%s}}",data)) |
| 1904 |
data = texgettoks"mplibtmptoks" |
| 1905 |
:gsub("##", "#") |
| 1906 |
:gsub("!l!u!a!(%d+)!m!p!l!", function(str) return t[tonumber(str)] or str end) |
| 1907 |
end |
| 1908 |
process(data, instancename) |
| 1909 |
end |
| 1910 |
|
| 1911 |
local figcontents = { post = { } } |
| 1912 |
local function put2output(a,...) |
| 1913 |
figcontents[#figcontents+1] = type(a) == "string" and format(a,...) or a |
| 1914 |
end |
| 1915 |
local function pdf_startfigure(n,llx,lly,urx,ury) |
| 1916 |
put2output("\\mplibstarttoPDF{%f}{%f}{%f}{%f}",llx,lly,urx,ury) |
| 1917 |
end |
| 1918 |
local function pdf_stopfigure() |
| 1919 |
put2output("\\mplibstoptoPDF") |
| 1920 |
end |
| 1921 |
local function pdf_literalcode (...) |
| 1922 |
put2output{ -2, (format(...) :gsub(decimals,rmzeros)) } |
| 1923 |
end |
| 1924 |
local start_pdf_code = pdfmode |
| 1925 |
and function() pdf_literalcode"q" end |
| 1926 |
or function() put2output"\\special{pdf:bcontent}" end |
| 1927 |
local stop_pdf_code = pdfmode |
| 1928 |
and function() pdf_literalcode"Q" end |
| 1929 |
or function() put2output"\\special{pdf:econtent}" end |
| 1930 |
|
| 1931 |
local function put_tex_boxes (object,prescript) |
| 1932 |
local box = prescript.mplibtexboxid:explode":" |
| 1933 |
local n,tw,th = box[1],tonumber(box[2]),tonumber(box[3]) |
| 1934 |
if n and tw and th then |
| 1935 |
local op = object.path |
| 1936 |
local first, second, fourth = op[1], op[2], op[4] |
| 1937 |
local tx, ty = first.x_coord, first.y_coord |
| 1938 |
local sx, rx, ry, sy = 1, 0, 0, 1 |
| 1939 |
if tw ~= 0 then |
| 1940 |
sx = (second.x_coord - tx)/tw |
| 1941 |
rx = (second.y_coord - ty)/tw |
| 1942 |
if sx == 0 then sx = 0.00001 end |
| 1943 |
end |
| 1944 |
if th ~= 0 then |
| 1945 |
sy = (fourth.y_coord - ty)/th |
| 1946 |
ry = (fourth.x_coord - tx)/th |
| 1947 |
if sy == 0 then sy = 0.00001 end |
| 1948 |
end |
| 1949 |
|
| 1950 |
local matrix = format("%f %f %f %f", sx, rx, ry, sy) :gsub(decimals,rmzeros) |
| 1951 |
put2output("\\mplibputtextbox{%i}{%f}{%f}{%s}", n, tx, ty, matrix) |
| 1952 |
end |
| 1953 |
end |
| 1954 |
|
| 1955 |
local do_preobj_CR |
| 1956 |
do |
| 1957 |
local prev_override_color |
| 1958 |
function do_preobj_CR(object,prescript) |
| 1959 |
if object.postscript == "collect" then return end |
| 1960 |
local override = prescript and prescript.mpliboverridecolor |
| 1961 |
if override then |
| 1962 |
if pdfmode then |
| 1963 |
pdf_literalcode(override) |
| 1964 |
override = nil |
| 1965 |
else |
| 1966 |
put2output("\\special{%s}",override) |
| 1967 |
prev_override_color = override |
| 1968 |
end |
| 1969 |
else |
| 1970 |
local cs = object.color |
| 1971 |
if cs and #cs > 0 then |
| 1972 |
pdf_literalcode(luamplib.colorconverter(cs)) |
| 1973 |
prev_override_color = nil |
| 1974 |
elseif not pdfmode then |
| 1975 |
override = prev_override_color |
| 1976 |
if override then |
| 1977 |
put2output("\\special{%s}",override) |
| 1978 |
end |
| 1979 |
end |
| 1980 |
end |
| 1981 |
return override |
| 1982 |
end |
| 1983 |
end |
| 1984 |
|
| 1985 |
local pdfmanagement = is_defined'pdfmanagement_add:nnn' |
| 1986 |
local pdfobjs, pdfetcs = {}, {} |
| 1987 |
pdfetcs.pgfextgs = "pgf@sys@addpdfresource@extgs@plain" |
| 1988 |
pdfetcs.pgfpattern = "pgf@sys@addpdfresource@patterns@plain" |
| 1989 |
pdfetcs.pgfcolorspace = "pgf@sys@addpdfresource@colorspaces@plain" |
| 1990 |
local function update_pdfobjs (os, stream) |
| 1991 |
local key = os |
| 1992 |
if stream then key = key..stream end |
| 1993 |
local on = key and pdfobjs[key] |
| 1994 |
if on then |
| 1995 |
return on,false |
| 1996 |
end |
| 1997 |
if pdfmode then |
| 1998 |
if stream then |
| 1999 |
on = pdf.immediateobj("stream",stream,os) |
| 2000 |
elseif os then |
| 2001 |
on = pdf.immediateobj(os) |
| 2002 |
else |
| 2003 |
on = pdf.reserveobj() |
| 2004 |
end |
| 2005 |
else |
| 2006 |
on = pdfetcs.cnt or 1 |
| 2007 |
if stream then |
| 2008 |
texsprint(format("\\special{pdf:stream @mplibpdfobj%s (%s) <<%s>>}",on,stream,os)) |
| 2009 |
elseif os then |
| 2010 |
texsprint(format("\\special{pdf:obj @mplibpdfobj%s %s}",on,os)) |
| 2011 |
else |
| 2012 |
texsprint(format("\\special{pdf:obj @mplibpdfobj%s <<>>}",on)) |
| 2013 |
end |
| 2014 |
pdfetcs.cnt = on + 1 |
| 2015 |
end |
| 2016 |
if key then |
| 2017 |
pdfobjs[key] = on |
| 2018 |
end |
| 2019 |
return on,true |
| 2020 |
end |
| 2021 |
pdfetcs.resfmt = pdfmode and "%s 0 R" or "@mplibpdfobj%s" |
| 2022 |
if pdfmode then |
| 2023 |
pdfetcs.getpageres = pdf.getpageresources or function() return pdf.pageresources end |
| 2024 |
local getpageres = pdfetcs.getpageres |
| 2025 |
local setpageres = pdf.setpageresources or function(s) pdf.pageresources = s end |
| 2026 |
local initialize_resources = function (name) |
| 2027 |
local tabname = format("%s_res",name) |
| 2028 |
pdfetcs[tabname] = { } |
| 2029 |
if luatexbase.callbacktypes.finish_pdffile then -- ltluatex |
| 2030 |
local obj = pdf.reserveobj() |
| 2031 |
setpageres(format("%s/%s %i 0 R", getpageres() or "", name, obj)) |
| 2032 |
luatexbase.add_to_callback("finish_pdffile", function() |
| 2033 |
pdf.immediateobj(obj, format("<<%s>>", tableconcat(pdfetcs[tabname]))) |
| 2034 |
end, |
| 2035 |
format("luamplib.%s.finish_pdffile",name)) |
| 2036 |
end |
| 2037 |
end |
| 2038 |
pdfetcs.fallback_update_resources = function (name, res) |
| 2039 |
local tabname = format("%s_res",name) |
| 2040 |
if not pdfetcs[tabname] then |
| 2041 |
initialize_resources(name) |
| 2042 |
end |
| 2043 |
if luatexbase.callbacktypes.finish_pdffile then |
| 2044 |
local t = pdfetcs[tabname] |
| 2045 |
t[#t+1] = res |
| 2046 |
else |
| 2047 |
local tpr, n = getpageres() or "", 0 |
| 2048 |
tpr, n = tpr:gsub(format("/%s<<",name), "%1"..res) |
| 2049 |
if n == 0 then |
| 2050 |
tpr = format("%s/%s<<%s>>", tpr, name, res) |
| 2051 |
end |
| 2052 |
setpageres(tpr) |
| 2053 |
end |
| 2054 |
end |
| 2055 |
else |
| 2056 |
texsprint { |
| 2057 |
"\\luamplibatfirstshipout{", |
| 2058 |
"\\special{pdf:obj @MPlibTr<<>>}", |
| 2059 |
"\\special{pdf:obj @MPlibSh<<>>}", |
| 2060 |
"\\special{pdf:obj @MPlibCS<<>>}", |
| 2061 |
"\\special{pdf:obj @MPlibPt<<>>}}", |
| 2062 |
} |
| 2063 |
pdfetcs.fallback_update_resources = function (name,res,obj) |
| 2064 |
texsprint{"\\special{pdf:put ", obj, " <<", res, ">>}"} |
| 2065 |
local tabname = format("%s_res",name) |
| 2066 |
if not pdfetcs[tabname] then |
| 2067 |
texsprint{"\\luamplibateveryshipout{\\special{pdf:put @resources <</", name, " ", obj, ">>}}"} |
| 2068 |
pdfetcs[tabname] = { } |
| 2069 |
end |
| 2070 |
tableinsert(pdfetcs[tabname], res) |
| 2071 |
end |
| 2072 |
end |
| 2073 |
|
| 2074 |
local function add_extgs_resources (on, new) |
| 2075 |
local key = format("MPlibTr%s", on) |
| 2076 |
if new then |
| 2077 |
local val = format(pdfetcs.resfmt, on) |
| 2078 |
if pdfmanagement then |
| 2079 |
texsprint { |
| 2080 |
"\\csname pdfmanagement_add:nnn\\endcsname{Page/Resources/ExtGState}{", key, "}{", val, "}" |
| 2081 |
} |
| 2082 |
else |
| 2083 |
local tr = format("/%s %s", key, val) |
| 2084 |
if is_defined(pdfetcs.pgfextgs) then |
| 2085 |
texsprint { "\\csname ", pdfetcs.pgfextgs, "\\endcsname{", tr, "}" } |
| 2086 |
elseif is_defined"TRP@list" then |
| 2087 |
texsprint(catat11,{ |
| 2088 |
[[\if@filesw\immediate\write\@auxout{]], |
| 2089 |
[[\string\g@addto@macro\string\TRP@list{]], |
| 2090 |
tr, |
| 2091 |
[[}}\fi]], |
| 2092 |
}) |
| 2093 |
if not get_macro"TRP@list":find(tr) then |
| 2094 |
texsprint(catat11,[[\global\TRP@reruntrue]]) |
| 2095 |
end |
| 2096 |
else |
| 2097 |
pdfetcs.fallback_update_resources("ExtGState",tr,"@MPlibTr") |
| 2098 |
end |
| 2099 |
end |
| 2100 |
end |
| 2101 |
return key |
| 2102 |
end |
| 2103 |
|
| 2104 |
local do_preobj_TR |
| 2105 |
do |
| 2106 |
local transparancy_modes = { |
| 2107 |
[0] = "Normal", |
| 2108 |
"Normal", "Multiply", "Screen", "Overlay", |
| 2109 |
"SoftLight", "HardLight", "ColorDodge", "ColorBurn", |
| 2110 |
"Darken", "Lighten", "Difference", "Exclusion", |
| 2111 |
"Hue", "Saturation", "Color", "Luminosity", |
| 2112 |
"Compatible", |
| 2113 |
normal = "Normal", multiply = "Multiply", screen = "Screen", |
| 2114 |
overlay = "Overlay", softlight = "SoftLight", hardlight = "HardLight", |
| 2115 |
colordodge = "ColorDodge", colorburn = "ColorBurn", darken = "Darken", |
| 2116 |
lighten = "Lighten", difference = "Difference", exclusion = "Exclusion", |
| 2117 |
hue = "Hue", saturation = "Saturation", color = "Color", |
| 2118 |
luminosity = "Luminosity", compatible = "Compatible", |
| 2119 |
} |
| 2120 |
function do_preobj_TR(object,prescript) |
| 2121 |
if object.postscript == "collect" then return end |
| 2122 |
local opaq = prescript and prescript.tr_transparency |
| 2123 |
if not opaq then return end |
| 2124 |
|
| 2125 |
local key, on, os, new |
| 2126 |
local mode = prescript.tr_alternative or 1 |
| 2127 |
mode = transparancy_modes[tonumber(mode) or mode:lower()] |
| 2128 |
if not mode then |
| 2129 |
mode = prescript.tr_alternative |
| 2130 |
warn("unsupported blend mode: '%s'", mode) |
| 2131 |
end |
| 2132 |
opaq = opaq:explode":" |
| 2133 |
for i,v in ipairs(opaq) do |
| 2134 |
opaq[i] = format("%.3f", v) :gsub(decimals,rmzeros) |
| 2135 |
end |
| 2136 |
for i,v in ipairs{ {mode,opaq[1],opaq[2] or opaq[1]},{"Normal",1,1} } do |
| 2137 |
os = format("<</BM/%s/ca %s/CA %s/AIS false>>",v[1],v[2],v[3]) |
| 2138 |
on, new = update_pdfobjs(os) |
| 2139 |
key = add_extgs_resources(on,new) |
| 2140 |
if i == 1 then |
| 2141 |
pdf_literalcode("/%s gs",key) |
| 2142 |
else |
| 2143 |
return format("/%s gs",key) |
| 2144 |
end |
| 2145 |
end |
| 2146 |
end |
| 2147 |
end |
| 2148 |
|
| 2149 |
local function sh_pdfpageresources(shtype,domain,colorspace,ca,cb,coordinates,steps,fractions,extend) |
| 2150 |
for _,v in ipairs{ca,cb} do |
| 2151 |
for i,vv in ipairs(v) do |
| 2152 |
for ii,vvv in ipairs(vv) do |
| 2153 |
v[i][ii] = tonumber(vvv) and format("%.3f",vvv) or vvv |
| 2154 |
end |
| 2155 |
end |
| 2156 |
end |
| 2157 |
local fun2fmt,os = "<</FunctionType 2/Domain[%s]/C0[%s]/C1[%s]/N 1>>" |
| 2158 |
if steps > 1 then |
| 2159 |
local list,bounds,encode = { },{ },{ } |
| 2160 |
for i=1,steps do |
| 2161 |
if i < steps then |
| 2162 |
bounds[i] = format("%.3f", fractions[i] or 1) |
| 2163 |
end |
| 2164 |
encode[2*i-1] = 0 |
| 2165 |
encode[2*i] = 1 |
| 2166 |
os = fun2fmt:format(domain,tableconcat(ca[i],' '),tableconcat(cb[i],' ')) |
| 2167 |
:gsub(decimals,rmzeros) |
| 2168 |
list[i] = format(pdfetcs.resfmt, update_pdfobjs(os)) |
| 2169 |
end |
| 2170 |
os = tableconcat { |
| 2171 |
"<</FunctionType 3", |
| 2172 |
format("/Bounds[%s]", tableconcat(bounds,' ')), |
| 2173 |
format("/Encode[%s]", tableconcat(encode,' ')), |
| 2174 |
format("/Functions[%s]", tableconcat(list, ' ')), |
| 2175 |
format("/Domain[%s]>>", domain), |
| 2176 |
} :gsub(decimals,rmzeros) |
| 2177 |
else |
| 2178 |
os = fun2fmt:format(domain,tableconcat(ca[1],' '),tableconcat(cb[1],' ')) |
| 2179 |
:gsub(decimals,rmzeros) |
| 2180 |
end |
| 2181 |
local objref = format(pdfetcs.resfmt, update_pdfobjs(os)) |
| 2182 |
os = tableconcat { |
| 2183 |
format("<</ShadingType %i", shtype), |
| 2184 |
format("/ColorSpace %s", colorspace), |
| 2185 |
format("/Function %s", objref), |
| 2186 |
format("/Coords[%s]", coordinates), |
| 2187 |
format("/Extend[%s]/AntiAlias true>>", extend or "true true") |
| 2188 |
} :gsub(decimals,rmzeros) |
| 2189 |
local on, new = update_pdfobjs(os) |
| 2190 |
if new then |
| 2191 |
local key, val = format("MPlibSh%s", on), format(pdfetcs.resfmt, on) |
| 2192 |
if pdfmanagement then |
| 2193 |
texsprint { |
| 2194 |
"\\csname pdfmanagement_add:nnn\\endcsname{Page/Resources/Shading}{", key, "}{", val, "}" |
| 2195 |
} |
| 2196 |
else |
| 2197 |
local res = format("/%s %s", key, val) |
| 2198 |
pdfetcs.fallback_update_resources("Shading",res,"@MPlibSh") |
| 2199 |
end |
| 2200 |
end |
| 2201 |
return on |
| 2202 |
end |
| 2203 |
|
| 2204 |
local do_preobj_SH |
| 2205 |
do |
| 2206 |
pdfetcs.clrspcs = setmetatable({ }, { __index = function(t,names) |
| 2207 |
run_tex_code({ |
| 2208 |
[[\color_model_new:nnn]], |
| 2209 |
format("{mplibcolorspace_%s}", names:gsub(",","_")), |
| 2210 |
format("{DeviceN}{names={%s}}", names), |
| 2211 |
[[\edef\mplib_@tempa{\pdf_object_ref_last:}]], |
| 2212 |
}, ccexplat) |
| 2213 |
local colorspace = get_macro'mplib_@tempa' |
| 2214 |
t[names] = colorspace |
| 2215 |
return colorspace |
| 2216 |
end }) |
| 2217 |
local function color_normalize(ca,cb) |
| 2218 |
if #cb == 1 then |
| 2219 |
if #ca == 4 then |
| 2220 |
cb[1], cb[2], cb[3], cb[4] = 0, 0, 0, 1-cb[1] |
| 2221 |
else -- #ca = 3 |
| 2222 |
cb[1], cb[2], cb[3] = cb[1], cb[1], cb[1] |
| 2223 |
end |
| 2224 |
elseif #cb == 3 then -- #ca == 4 |
| 2225 |
cb[1], cb[2], cb[3], cb[4] = 1-cb[1], 1-cb[2], 1-cb[3], 0 |
| 2226 |
end |
| 2227 |
end |
| 2228 |
function do_preobj_SH(object, prescript) |
| 2229 |
local shade_no |
| 2230 |
local sh_type = prescript and prescript.sh_type |
| 2231 |
if not sh_type then return end |
| 2232 |
|
| 2233 |
local domain = prescript.sh_domain or "0 1" |
| 2234 |
local centera = (prescript.sh_center_a or "0 0"):explode() |
| 2235 |
local centerb = (prescript.sh_center_b or "0 0"):explode() |
| 2236 |
local transform = prescript.sh_transform == "yes" |
| 2237 |
local sx,sy,sr,dx,dy = 1,1,1,0,0 |
| 2238 |
if transform then |
| 2239 |
local first = (prescript.sh_first or "0 0"):explode() |
| 2240 |
local setx = (prescript.sh_set_x or "0 0"):explode() |
| 2241 |
local sety = (prescript.sh_set_y or "0 0"):explode() |
| 2242 |
local x,y = tonumber(setx[1]) or 0, tonumber(sety[1]) or 0 |
| 2243 |
if x ~= 0 and y ~= 0 then |
| 2244 |
local path = object.path |
| 2245 |
local path1x = path[1].x_coord |
| 2246 |
local path1y = path[1].y_coord |
| 2247 |
local path2x = path[x].x_coord |
| 2248 |
local path2y = path[y].y_coord |
| 2249 |
local dxa = path2x - path1x |
| 2250 |
local dya = path2y - path1y |
| 2251 |
local dxb = setx[2] - first[1] |
| 2252 |
local dyb = sety[2] - first[2] |
| 2253 |
if dxa ~= 0 and dya ~= 0 and dxb ~= 0 and dyb ~= 0 then |
| 2254 |
sx = dxa / dxb ; if sx < 0 then sx = - sx end |
| 2255 |
sy = dya / dyb ; if sy < 0 then sy = - sy end |
| 2256 |
sr = math.sqrt(sx^2 + sy^2) |
| 2257 |
dx = path1x - sx*first[1] |
| 2258 |
dy = path1y - sy*first[2] |
| 2259 |
end |
| 2260 |
end |
| 2261 |
end |
| 2262 |
local ca, cb, colorspace, steps, fractions |
| 2263 |
ca = { (prescript.sh_color_a_1 or prescript.sh_color_a or "0"):explode":" } |
| 2264 |
cb = { (prescript.sh_color_b_1 or prescript.sh_color_b or "1"):explode":" } |
| 2265 |
steps = tonumber(prescript.sh_step) or 1 |
| 2266 |
if steps > 1 then |
| 2267 |
fractions = { prescript.sh_fraction_1 or 0 } |
| 2268 |
for i=2,steps do |
| 2269 |
fractions[i] = prescript[format("sh_fraction_%i",i)] or (i/steps) |
| 2270 |
ca[i] = (prescript[format("sh_color_a_%i",i)] or "0"):explode":" |
| 2271 |
cb[i] = (prescript[format("sh_color_b_%i",i)] or "1"):explode":" |
| 2272 |
end |
| 2273 |
end |
| 2274 |
if prescript.mplib_spotcolor then |
| 2275 |
ca, cb = { }, { } |
| 2276 |
local names, pos, objref = { }, -1, "" |
| 2277 |
local script = object.prescript:explode"\13+" |
| 2278 |
for i=#script,1,-1 do |
| 2279 |
if script[i]:find"mplib_spotcolor" then |
| 2280 |
local t, name, value = script[i]:explode"="[2]:explode":" |
| 2281 |
value, objref, name = t[1], t[2], t[3] |
| 2282 |
if not names[name] then |
| 2283 |
pos = pos+1 |
| 2284 |
names[name] = pos |
| 2285 |
names[#names+1] = name |
| 2286 |
end |
| 2287 |
t = { } |
| 2288 |
for j=1,names[name] do t[#t+1] = 0 end |
| 2289 |
t[#t+1] = value |
| 2290 |
tableinsert(#ca == #cb and ca or cb, t) |
| 2291 |
end |
| 2292 |
end |
| 2293 |
for _,t in ipairs{ca,cb} do |
| 2294 |
for _,tt in ipairs(t) do |
| 2295 |
for i=1,#names-#tt do tt[#tt+1] = 0 end |
| 2296 |
end |
| 2297 |
end |
| 2298 |
if #names == 1 then |
| 2299 |
colorspace = objref |
| 2300 |
else |
| 2301 |
colorspace = pdfetcs.clrspcs[ tableconcat(names,",") ] |
| 2302 |
end |
| 2303 |
else |
| 2304 |
local model = 0 |
| 2305 |
for _,t in ipairs{ca,cb} do |
| 2306 |
for _,tt in ipairs(t) do |
| 2307 |
model = model > #tt and model or #tt |
| 2308 |
end |
| 2309 |
end |
| 2310 |
for _,t in ipairs{ca,cb} do |
| 2311 |
for _,tt in ipairs(t) do |
| 2312 |
if #tt < model then |
| 2313 |
color_normalize(model == 4 and {1,1,1,1} or {1,1,1},tt) |
| 2314 |
end |
| 2315 |
end |
| 2316 |
end |
| 2317 |
colorspace = model == 4 and "/DeviceCMYK" |
| 2318 |
or model == 3 and "/DeviceRGB" |
| 2319 |
or model == 1 and "/DeviceGray" |
| 2320 |
or err"unknown color model" |
| 2321 |
end |
| 2322 |
local extend = prescript.sh_extend |
| 2323 |
if sh_type == "linear" then |
| 2324 |
local coordinates = format("%f %f %f %f", |
| 2325 |
dx + sx*centera[1], dy + sy*centera[2], |
| 2326 |
dx + sx*centerb[1], dy + sy*centerb[2]) |
| 2327 |
shade_no = sh_pdfpageresources(2,domain,colorspace,ca,cb,coordinates,steps,fractions,extend) |
| 2328 |
elseif sh_type == "circular" then |
| 2329 |
local factor = prescript.sh_factor or 1 |
| 2330 |
local radiusa = factor * prescript.sh_radius_a |
| 2331 |
local radiusb = factor * prescript.sh_radius_b |
| 2332 |
local coordinates = format("%f %f %f %f %f %f", |
| 2333 |
dx + sx*centera[1], dy + sy*centera[2], sr*radiusa, |
| 2334 |
dx + sx*centerb[1], dy + sy*centerb[2], sr*radiusb) |
| 2335 |
shade_no = sh_pdfpageresources(3,domain,colorspace,ca,cb,coordinates,steps,fractions,extend) |
| 2336 |
else |
| 2337 |
err"unknown shading type" |
| 2338 |
end |
| 2339 |
return shade_no, prescript.sh_stroking == "yes" |
| 2340 |
end |
| 2341 |
end |
| 2342 |
|
| 2343 |
local function add_pattern_resources (key, val) |
| 2344 |
if pdfmanagement then |
| 2345 |
texsprint { |
| 2346 |
"\\csname pdfmanagement_add:nnn\\endcsname{Page/Resources/Pattern}{", key, "}{", val, "}" |
| 2347 |
} |
| 2348 |
else |
| 2349 |
local res = format("/%s %s", key, val) |
| 2350 |
if is_defined(pdfetcs.pgfpattern) then |
| 2351 |
texsprint { "\\csname ", pdfetcs.pgfpattern, "\\endcsname{", res, "}" } |
| 2352 |
else |
| 2353 |
pdfetcs.fallback_update_resources("Pattern",res,"@MPlibPt") |
| 2354 |
end |
| 2355 |
end |
| 2356 |
end |
| 2357 |
if pdfmode then |
| 2358 |
function luamplib.dolatelua (on, os, matrix) |
| 2359 |
local h, v = pdf.getpos() |
| 2360 |
local t = matrix and matrix:explode() or {1,0,0,1,0,0} |
| 2361 |
matrix = format("%f %f %f %f %f %f", t[1], t[2], t[3], t[4], t[5]+h/factor, t[6]+v/factor) |
| 2362 |
:gsub(decimals,rmzeros) |
| 2363 |
pdf.obj(on, format("<<%s/Matrix[%s]>>", os, matrix)) |
| 2364 |
pdf.refobj(on) |
| 2365 |
end |
| 2366 |
else |
| 2367 |
pdfetcs.shadingpatterns = { } |
| 2368 |
pdfetcs.shadingpatterninit_r, pdfetcs.shadingpatterninit_w = true, true |
| 2369 |
function luamplib.dolatelua (on, kind, xobj) |
| 2370 |
local h, v |
| 2371 |
local t = pdfetcs.shadingpatterns[on] or { } |
| 2372 |
local shift = kind == "group" and pdfetcs.tr_group.shifts[xobj] |
| 2373 |
or kind == "pattern" and pdfetcs.patterns[xobj].shifts |
| 2374 |
if shift then |
| 2375 |
h, v = -shift[1], -shift[2] -- engine bug in dvi mode? |
| 2376 |
else |
| 2377 |
h, v = pdf.getpos() |
| 2378 |
h = format("%f", h/factor) :gsub(decimals,rmzeros) |
| 2379 |
v = format("%f", v/factor) :gsub(decimals,rmzeros) |
| 2380 |
end |
| 2381 |
if tonumber(h) ~= tonumber(t[1]) or tonumber(v) ~= tonumber(t[2]) then |
| 2382 |
warn"Rerun to get correct shading pattern" |
| 2383 |
end |
| 2384 |
local name = format("%s/%s_shadingpatterns.aux", cachedir or outputdir(), tex.jobname) |
| 2385 |
local init = pdfetcs.shadingpatterninit_w |
| 2386 |
if init then pdfetcs.shadingpatterninit_w = nil end |
| 2387 |
local f = ioopen(name, init and "w" or "a") |
| 2388 |
if f then |
| 2389 |
f:write(("%s %s %s\n"):format(on, h, v)) |
| 2390 |
f:close() |
| 2391 |
else |
| 2392 |
err"cannot write a file. check the cache dir path" |
| 2393 |
end |
| 2394 |
end |
| 2395 |
end |
| 2396 |
local function do_preobj_shading (object, prescript) |
| 2397 |
if not prescript or not prescript.sh_operand_type then return end |
| 2398 |
local on = do_preobj_SH(object, prescript) |
| 2399 |
local os = format("/PatternType 2/Shading %s", format(pdfetcs.resfmt, on)) |
| 2400 |
local matrix = prescript.sh_matrix or "1 0 0 1 0 0" |
| 2401 |
if matrix:find"%a" then |
| 2402 |
local data = format("mplibtransformmatrix(%s);",matrix) |
| 2403 |
process(data,"@mplibtransformmatrix") |
| 2404 |
local t = luamplib.transformmatrix |
| 2405 |
matrix = format("%f %f %f %f %f %f", t[1], t[2], t[3], t[4], t[5], t[6]):gsub(decimals,rmzeros) |
| 2406 |
end |
| 2407 |
if prescript.sh_in_xobj == "yes" then |
| 2408 |
on = update_pdfobjs(("<<%s/Matrix[%s]>>"):format(os, matrix)) |
| 2409 |
goto skip_latelua |
| 2410 |
end |
| 2411 |
on = update_pdfobjs() |
| 2412 |
if pdfmode then |
| 2413 |
put2output(tableconcat{"\\latelua{luamplib.dolatelua(",on,",[[",os,"]],[[",matrix,"]])}"}) |
| 2414 |
else |
| 2415 |
local xobj = is_defined"mplibgroupname" and {"group", get_macro"mplibgroupname"} |
| 2416 |
or is_defined"mplibpatternname" and {"pattern", get_macro"mplibpatternname"} |
| 2417 |
local init = pdfetcs.shadingpatterninit_r |
| 2418 |
if init then |
| 2419 |
pdfetcs.shadingpatterninit_r = nil |
| 2420 |
local name = format("%s/%s_shadingpatterns.aux", cachedir or outputdir(), tex.jobname) |
| 2421 |
local f = ioopen(name) |
| 2422 |
if f then |
| 2423 |
for line in f:lines() do |
| 2424 |
local t = line:explode() |
| 2425 |
pdfetcs.shadingpatterns[ tonumber(t[1]) ] = { t[2], t[3] } |
| 2426 |
end |
| 2427 |
f:close() |
| 2428 |
end |
| 2429 |
end |
| 2430 |
local t = pdfetcs.shadingpatterns[on] or { 0, 0 } |
| 2431 |
local mt = matrix:explode() |
| 2432 |
matrix = format("%s %s %s %s %s %s", mt[1], mt[2], mt[3], mt[4], mt[5]+t[1], mt[6]+t[2]) |
| 2433 |
texsprint{ "\\special{pdf:put ", format(pdfetcs.resfmt, on), |
| 2434 |
format(" <<%s/Matrix[%s]>>}", os, matrix) } |
| 2435 |
put2output("\\latelua{ luamplib.dolatelua(%s,%s) }", on, |
| 2436 |
xobj and ("'%s',[[%s]]"):format(xobj[1], xobj[2])) |
| 2437 |
end |
| 2438 |
::skip_latelua:: |
| 2439 |
local key, val = format("MPlibPt%s", on), format(pdfetcs.resfmt, on) |
| 2440 |
add_pattern_resources(key,val) |
| 2441 |
pdf_literalcode("/Pattern cs/%s scn", key) |
| 2442 |
prescript.sh_type = nil |
| 2443 |
end |
| 2444 |
|
| 2445 |
pdfetcs.patterns = { } |
| 2446 |
local function gather_resources (optres, ispattern) |
| 2447 |
local t = { } |
| 2448 |
if pdfmanagement then |
| 2449 |
for _,v in ipairs {"ExtGState","ColorSpace","Pattern","Shading"} do |
| 2450 |
local mytoks |
| 2451 |
run_tex_code ({ |
| 2452 |
"\\mplibtmptoks\\expanded{{", |
| 2453 |
"\\pdfdict_if_empty:nF{g__pdf_Core/Page/Resources/",v,"}", |
| 2454 |
"{\\pdfdict_use:n{g__pdf_Core/Page/Resources/",v,"}}", "}}", |
| 2455 |
},ccexplat) |
| 2456 |
mytoks = texgettoks"mplibtmptoks" |
| 2457 |
if not pdfmode then |
| 2458 |
mytoks = mytoks:gsub("\\str_convert_pdfname:n%s*{(.-)}","%1") -- why not expanded? |
| 2459 |
end |
| 2460 |
mytoks = mytoks and mytoks:gsub("^%s*(.-)%s*$", "%1") |
| 2461 |
if mytoks and mytoks ~= "" then |
| 2462 |
t[#t+1] = ("/%s<<%s>>"):format(v, mytoks) |
| 2463 |
end |
| 2464 |
end |
| 2465 |
elseif is_defined(pdfetcs.pgfextgs) then |
| 2466 |
run_tex_code"\\relax" -- flush tex.sprint queue |
| 2467 |
if pdfmode then |
| 2468 |
for k,v in pairs { ExtGState = "pgf@sys@pgf@resource@list@extgs", |
| 2469 |
ColorSpace = "pgf@sys@pgf@resource@list@colorspaces", |
| 2470 |
Pattern = "pgf@sys@pgf@resource@list@patterns", } do |
| 2471 |
local res = (get_macro(v) or ""):gsub("^%s*(.-)%s*$","%1") |
| 2472 |
if res ~= "" then |
| 2473 |
t[#t+1] = ("/%s<<%s>>"):format(k, res ) |
| 2474 |
end |
| 2475 |
end |
| 2476 |
else |
| 2477 |
local abc = get_macro"pgfutil@abc" or "" |
| 2478 |
for k,v in pairs { ExtGState = "@pgfextgs", |
| 2479 |
ColorSpace = "@pgfcolorspaces", |
| 2480 |
Pattern = "@pgfpatterns", } do |
| 2481 |
local tt = { } |
| 2482 |
for vv in abc:gmatch( v .. "%s*(%b<>)" ) do |
| 2483 |
tt[#tt+1] = vv:match("^<<%s*(.-)%s*>>$") |
| 2484 |
end |
| 2485 |
if #tt > 0 then |
| 2486 |
t[#t+1] = ("/%s<<%s>>"):format(k, tableconcat(tt) ) |
| 2487 |
end |
| 2488 |
end |
| 2489 |
end |
| 2490 |
if luatexbase.callbacktypes.finish_pdffile then |
| 2491 |
if pdfetcs.Shading_res then |
| 2492 |
t[#t+1] = ("/Shading<<%s>>"):format( tableconcat(pdfetcs.Shading_res) ) |
| 2493 |
end |
| 2494 |
else |
| 2495 |
local res = pdfetcs.getpageres() |
| 2496 |
res = res and res:match"/Shading%s*%b<>" |
| 2497 |
if res then |
| 2498 |
t[#t+1] = res |
| 2499 |
end |
| 2500 |
end |
| 2501 |
else |
| 2502 |
if ispattern and is_defined"TRP@list" then |
| 2503 |
warn"transparent package is not fully functional without pdfmanagement code." |
| 2504 |
end |
| 2505 |
if luatexbase.callbacktypes.finish_pdffile then |
| 2506 |
for _,v in ipairs {"ExtGState","ColorSpace","Pattern","Shading"} do |
| 2507 |
local tt = pdfetcs[v.."_res"] |
| 2508 |
if tt then |
| 2509 |
t[#t+1] = ("/%s<<%s>>"):format(v, tableconcat(tt)) |
| 2510 |
end |
| 2511 |
end |
| 2512 |
else |
| 2513 |
local res = pdfetcs.getpageres() |
| 2514 |
if res then |
| 2515 |
t[#t+1] = res |
| 2516 |
end |
| 2517 |
end |
| 2518 |
end |
| 2519 |
local result = tableconcat(t) |
| 2520 |
if optres ~= "" then |
| 2521 |
for _,v in ipairs {"ExtGState","ColorSpace","Pattern","Shading"} do |
| 2522 |
local res = optres:match("/"..v.."%s*%b<>") |
| 2523 |
if res then |
| 2524 |
if result:find("/"..v) then |
| 2525 |
res = res:match("<<(.+)>>$") |
| 2526 |
result = result:gsub("/"..v.."%s*<<", "%1"..res, 1) |
| 2527 |
else |
| 2528 |
result = result .. res |
| 2529 |
end |
| 2530 |
end |
| 2531 |
end |
| 2532 |
end |
| 2533 |
return result |
| 2534 |
end |
| 2535 |
function luamplib.registerpattern ( boxid, name, opts ) |
| 2536 |
local box = texgetbox(boxid) |
| 2537 |
local wd = format("%.3f",box.width/factor) |
| 2538 |
local hd = format("%.3f",(box.height+box.depth)/factor) |
| 2539 |
info("w/h/d of pattern '%s': %s 0", name, format("%s %s",wd, hd):gsub(decimals,rmzeros)) |
| 2540 |
if opts.xstep == 0 then opts.xstep = nil end |
| 2541 |
if opts.ystep == 0 then opts.ystep = nil end |
| 2542 |
if opts.colored == nil then |
| 2543 |
opts.colored = opts.coloured |
| 2544 |
if opts.colored == nil then |
| 2545 |
opts.colored = true |
| 2546 |
end |
| 2547 |
end |
| 2548 |
if type(opts.matrix) == "table" then opts.matrix = tableconcat(opts.matrix," ") end |
| 2549 |
if type(opts.bbox) == "table" then opts.bbox = tableconcat(opts.bbox," ") end |
| 2550 |
if opts.matrix and opts.matrix:find"%a" then |
| 2551 |
local data = format("mplibtransformmatrix(%s);",opts.matrix) |
| 2552 |
process(data,"@mplibtransformmatrix") |
| 2553 |
local t = luamplib.transformmatrix |
| 2554 |
opts.matrix = format("%f %f %f %f", t[1], t[2], t[3], t[4]) |
| 2555 |
opts.xshift = opts.xshift or format("%f",t[5]) |
| 2556 |
opts.yshift = opts.yshift or format("%f",t[6]) |
| 2557 |
end |
| 2558 |
local attr = { |
| 2559 |
"/Type/Pattern", |
| 2560 |
"/PatternType 1", |
| 2561 |
format("/PaintType %i", opts.colored and 1 or 2), |
| 2562 |
"/TilingType 2", |
| 2563 |
format("/XStep %s", opts.xstep or wd), |
| 2564 |
format("/YStep %s", opts.ystep or hd), |
| 2565 |
format("/Matrix[%s %s %s]", opts.matrix or "1 0 0 1", opts.xshift or 0, opts.yshift or 0), |
| 2566 |
} |
| 2567 |
local optres = opts.resources or "" |
| 2568 |
optres = gather_resources(optres, true) -- tiling pattern plus masking glitches with acrobat |
| 2569 |
local patterns = pdfetcs.patterns |
| 2570 |
if pdfmode then |
| 2571 |
if opts.bbox then |
| 2572 |
attr[#attr+1] = format("/BBox[%s]", opts.bbox) |
| 2573 |
end |
| 2574 |
attr = tableconcat(attr) :gsub(decimals,rmzeros) |
| 2575 |
local index = tex.saveboxresource(boxid, attr, optres, true, opts.bbox and 4 or 1) |
| 2576 |
patterns[name] = { id = index, colored = opts.colored } |
| 2577 |
else |
| 2578 |
local cnt = #patterns + 1 |
| 2579 |
local objname = "@mplibpattern" .. cnt |
| 2580 |
local metric = format("bbox %s", opts.bbox or format("0 0 %s %s",wd,hd)) |
| 2581 |
texsprint { |
| 2582 |
"\\expandafter\\newbox\\csname luamplib.patternbox.", cnt, "\\endcsname", |
| 2583 |
"\\global\\setbox\\csname luamplib.patternbox.", cnt, "\\endcsname", |
| 2584 |
"\\hbox{\\unhbox ", boxid, "}\\luamplibatnextshipout{", |
| 2585 |
"\\special{pdf:bcontent}", |
| 2586 |
"\\special{pdf:bxobj ", objname, " ", metric, "}", |
| 2587 |
"\\raise\\dp\\csname luamplib.patternbox.", cnt, "\\endcsname", |
| 2588 |
"\\box\\csname luamplib.patternbox.", cnt, "\\endcsname", |
| 2589 |
"\\special{pdf:put @resources <<", optres, ">>}", |
| 2590 |
"\\special{pdf:exobj <<", tableconcat(attr), ">>}", |
| 2591 |
"\\special{pdf:econtent}}", |
| 2592 |
} |
| 2593 |
patterns[cnt] = objname |
| 2594 |
patterns[name] = { id = cnt, colored = opts.colored } |
| 2595 |
patterns[name].shifts = { get_macro"MPllx", get_macro"MPlly" } -- for shading patterns above |
| 2596 |
end |
| 2597 |
end |
| 2598 |
|
| 2599 |
local do_preobj_PAT |
| 2600 |
do |
| 2601 |
local function pattern_colorspace (cs) |
| 2602 |
local on, new = update_pdfobjs(format("[/Pattern %s]", cs)) |
| 2603 |
if new then |
| 2604 |
local key, val = format("MPlibCS%i",on), format(pdfetcs.resfmt,on) |
| 2605 |
if pdfmanagement then |
| 2606 |
texsprint { |
| 2607 |
"\\csname pdfmanagement_add:nnn\\endcsname{Page/Resources/ColorSpace}{", key, "}{", val, "}" |
| 2608 |
} |
| 2609 |
else |
| 2610 |
local res = format("/%s %s", key, val) |
| 2611 |
if is_defined(pdfetcs.pgfcolorspace) then |
| 2612 |
texsprint { "\\csname ", pdfetcs.pgfcolorspace, "\\endcsname{", res, "}" } |
| 2613 |
else |
| 2614 |
pdfetcs.fallback_update_resources("ColorSpace",res,"@MPlibCS") |
| 2615 |
end |
| 2616 |
end |
| 2617 |
end |
| 2618 |
return on |
| 2619 |
end |
| 2620 |
function do_preobj_PAT(object, prescript) |
| 2621 |
local name = prescript and prescript.mplibpattern |
| 2622 |
if not name then return end |
| 2623 |
local patterns = pdfetcs.patterns |
| 2624 |
local patt = patterns[name] |
| 2625 |
local index = patt and patt.id or err("cannot get pattern object '%s'", name) |
| 2626 |
local key = format("MPlibPt%s",index) |
| 2627 |
if patt.colored then |
| 2628 |
pdf_literalcode("/Pattern cs /%s scn", key) |
| 2629 |
else |
| 2630 |
local color = prescript.mpliboverridecolor |
| 2631 |
if not color then |
| 2632 |
local t = object.color |
| 2633 |
color = t and #t>0 and luamplib.colorconverter(t) |
| 2634 |
end |
| 2635 |
if not color then return end |
| 2636 |
local cs |
| 2637 |
if color:find" cs " or color:find"@pdf.obj" then |
| 2638 |
local t = color:explode() |
| 2639 |
if pdfmode then |
| 2640 |
cs = format("%s 0 R", ltx.pdf.object_id( t[1]:sub(2,-1) )) |
| 2641 |
color = t[3] |
| 2642 |
else |
| 2643 |
cs = t[2] |
| 2644 |
color = t[3]:match"%[(.+)%]" |
| 2645 |
end |
| 2646 |
else |
| 2647 |
local t = colorsplit(color) |
| 2648 |
cs = #t == 4 and "/DeviceCMYK" or #t == 3 and "/DeviceRGB" or "/DeviceGray" |
| 2649 |
color = tableconcat(t," ") |
| 2650 |
end |
| 2651 |
pdf_literalcode("/MPlibCS%i cs %s /%s scn", pattern_colorspace(cs), color, key) |
| 2652 |
end |
| 2653 |
if not patt.done then |
| 2654 |
local val = pdfmode and format("%s 0 R",index) or patterns[index] |
| 2655 |
add_pattern_resources(key,val) |
| 2656 |
end |
| 2657 |
patt.done = true |
| 2658 |
end |
| 2659 |
end |
| 2660 |
|
| 2661 |
pdfetcs.fading = { } |
| 2662 |
local function do_preobj_FADE (object, prescript) |
| 2663 |
local fd_type = prescript and prescript.mplibfadetype |
| 2664 |
local fd_stop = prescript and prescript.mplibfadestate |
| 2665 |
if not fd_type then |
| 2666 |
return fd_stop -- returns "stop" (if picture) or nil |
| 2667 |
end |
| 2668 |
local on, os, new |
| 2669 |
if fd_type == "masking" then |
| 2670 |
local mac = get_macro("luamplib.group."..prescript.mplibmaskname) |
| 2671 |
on = mac:match(pdfmode and "%d+" or "{pdf:uxobj (.-)}") |
| 2672 |
local bc = prescript.mplibmaskingbgcolor |
| 2673 |
bc = bc and bc:gsub(":"," ") |
| 2674 |
bc = bc and ("/BC[%s]"):format(bc):gsub(decimals,rmzeros) or "" |
| 2675 |
os = format("<</SMask<</S/Luminosity/G %s%s>>>>", |
| 2676 |
pdfmode and format(pdfetcs.resfmt, on) or on, bc) |
| 2677 |
else |
| 2678 |
local bbox = prescript.mplibfadebbox:explode":" |
| 2679 |
local dx, dy = -bbox[1], -bbox[2] |
| 2680 |
local vec = prescript.mplibfadevector; vec = vec and vec:explode":" |
| 2681 |
if not vec then |
| 2682 |
if fd_type == "linear" then |
| 2683 |
vec = {bbox[1], bbox[2], bbox[3], bbox[2]} -- left to right |
| 2684 |
else |
| 2685 |
local centerx, centery = (bbox[1]+bbox[3])/2, (bbox[2]+bbox[4])/2 |
| 2686 |
vec = {centerx, centery, centerx, centery} -- center for both circles |
| 2687 |
end |
| 2688 |
end |
| 2689 |
local coords = { vec[1], vec[2], vec[3], vec[4] } |
| 2690 |
if fd_type == "linear" then |
| 2691 |
coords = format("%f %f %f %f", tableunpack(coords)) |
| 2692 |
elseif fd_type == "circular" then |
| 2693 |
local width, height = bbox[3]-bbox[1], bbox[4]-bbox[2] |
| 2694 |
local radius = (prescript.mplibfaderadius or "0:"..math.sqrt(width^2+height^2)/2):explode":" |
| 2695 |
tableinsert(coords, 3, radius[1]) |
| 2696 |
tableinsert(coords, radius[2]) |
| 2697 |
coords = format("%f %f %f %f %f %f", tableunpack(coords)) |
| 2698 |
else |
| 2699 |
err("unknown fading method '%s'", fd_type) |
| 2700 |
end |
| 2701 |
fd_type = fd_type == "linear" and 2 or 3 |
| 2702 |
local extend, steps, fractions = prescript.sh_extend, tonumber(prescript.sh_step) or 1 |
| 2703 |
local ca = { (prescript.sh_color_a_1 or prescript.sh_color_a or "1"):explode":" } |
| 2704 |
local cb = { (prescript.sh_color_b_1 or prescript.sh_color_b or "0"):explode":" } |
| 2705 |
if steps > 1 then |
| 2706 |
fractions = { prescript.sh_fraction_1 or 0 } |
| 2707 |
for i=2,steps do |
| 2708 |
fractions[i] = prescript[format("sh_fraction_%i",i)] or (i/steps) |
| 2709 |
ca[i] = (prescript[format("sh_color_a_%i",i)] or "1"):explode":" |
| 2710 |
cb[i] = (prescript[format("sh_color_b_%i",i)] or "0"):explode":" |
| 2711 |
end |
| 2712 |
end |
| 2713 |
local matrix = prescript.sh_matrix or "1 0 0 1 0 0" |
| 2714 |
if matrix:find"%a" then |
| 2715 |
local data = format("mplibtransformmatrix(%s);",matrix) |
| 2716 |
process(data,"@mplibtransformmatrix") |
| 2717 |
matrix = luamplib.transformmatrix |
| 2718 |
else |
| 2719 |
matrix = matrix:explode() |
| 2720 |
end |
| 2721 |
matrix[5] = matrix[5] + dx |
| 2722 |
matrix[6] = matrix[6] + dy |
| 2723 |
matrix = format("%f %f %f %f %f %f", tableunpack(matrix)) :gsub(decimals,rmzeros) |
| 2724 |
on = sh_pdfpageresources(fd_type,"0 1","/DeviceGray",ca,cb,coords,steps,fractions,extend) |
| 2725 |
os = format("<</PatternType 2/Shading %s/Matrix[%s]>>", format(pdfetcs.resfmt, on), matrix) |
| 2726 |
on = update_pdfobjs(os) |
| 2727 |
bbox = format("0 0 %f %f", bbox[3]+dx, bbox[4]+dy) |
| 2728 |
local streamtext = format("q /Pattern cs/MPlibFd%s scn %s re f Q", on, bbox) |
| 2729 |
:gsub(decimals,rmzeros) |
| 2730 |
os = format("<</Pattern<</MPlibFd%s %s>>>>", on, format(pdfetcs.resfmt, on)) |
| 2731 |
on = update_pdfobjs(os) |
| 2732 |
local resources = format(pdfetcs.resfmt, on) |
| 2733 |
on = update_pdfobjs"<</S/Transparency/CS/DeviceGray>>" |
| 2734 |
local attr = tableconcat{ |
| 2735 |
"/Subtype/Form", |
| 2736 |
"/BBox[", bbox, "]", |
| 2737 |
"/Matrix[1 0 0 1 ", format("%f %f", -dx,-dy), "]", |
| 2738 |
"/Resources ", resources, |
| 2739 |
"/Group ", format(pdfetcs.resfmt, on), |
| 2740 |
} :gsub(decimals,rmzeros) |
| 2741 |
on = update_pdfobjs(attr, streamtext) |
| 2742 |
os = format("<</SMask<</S/Luminosity/G %s>>>>", format(pdfetcs.resfmt, on)) |
| 2743 |
end |
| 2744 |
on, new = update_pdfobjs(os) |
| 2745 |
local key = add_extgs_resources(on,new) |
| 2746 |
start_pdf_code() |
| 2747 |
pdf_literalcode("/%s gs", key) |
| 2748 |
if fd_stop then return "standalone" end |
| 2749 |
return "start" |
| 2750 |
end |
| 2751 |
|
| 2752 |
pdfetcs.tr_group = { shifts = { } } |
| 2753 |
luamplib.trgroupshifts = pdfetcs.tr_group.shifts |
| 2754 |
local function do_preobj_GRP (object, prescript) |
| 2755 |
local grstate = prescript and prescript.gr_state |
| 2756 |
if not grstate then return end |
| 2757 |
local trgroup = pdfetcs.tr_group |
| 2758 |
if grstate == "start" then |
| 2759 |
trgroup.name = prescript.mplibgroupname or "lastmplibgroup" |
| 2760 |
trgroup.isolated, trgroup.knockout, trgroup.off = false, false, false |
| 2761 |
trgroup.wrapped = false |
| 2762 |
for _,v in ipairs(prescript.gr_type:gsub("%s",""):explode",+") do |
| 2763 |
trgroup[v] = true |
| 2764 |
end |
| 2765 |
trgroup.bbox = prescript.mplibgroupbbox:explode":" |
| 2766 |
put2output[[\begingroup\setbox\mplibscratchbox\hbox\bgroup\luamplibtagasgroupset]] |
| 2767 |
elseif grstate == "stop" then |
| 2768 |
local llx,lly,urx,ury = tableunpack(trgroup.bbox) |
| 2769 |
put2output(tableconcat{ |
| 2770 |
"\\egroup", |
| 2771 |
format("\\wd\\mplibscratchbox %fbp", urx-llx), |
| 2772 |
format("\\ht\\mplibscratchbox %fbp", ury-lly), |
| 2773 |
"\\dp\\mplibscratchbox 0pt", |
| 2774 |
}) |
| 2775 |
local grattr |
| 2776 |
if trgroup.off then |
| 2777 |
grattr = "" |
| 2778 |
else |
| 2779 |
local on = update_pdfobjs(format("<</S/Transparency/I %s/K %s>>", |
| 2780 |
trgroup.isolated, trgroup.knockout)) |
| 2781 |
grattr = format("/Group %s", pdfetcs.resfmt:format(on)) |
| 2782 |
end |
| 2783 |
local res = gather_resources("") |
| 2784 |
local bbox = format("%f %f %f %f", llx,lly,urx,ury) :gsub(decimals,rmzeros) |
| 2785 |
if pdfmode then |
| 2786 |
if trgroup.wrapped then |
| 2787 |
put2output(tableconcat{ |
| 2788 |
"\\saveboxresource type 2 attr{/Type/XObject/Subtype/Form/FormType 1", |
| 2789 |
"/BBox[", bbox, "]} resources{", res, "}\\mplibscratchbox", |
| 2790 |
"\\setbox\\mplibscratchbox\\hbox{\\useboxresource\\lastsavedboxresourceindex}", |
| 2791 |
}) |
| 2792 |
end |
| 2793 |
put2output(tableconcat{ |
| 2794 |
"\\saveboxresource type 2 attr{/Type/XObject/Subtype/Form/FormType 1", |
| 2795 |
"/BBox[", bbox, "]", grattr, "} resources{", res, "}\\mplibscratchbox", |
| 2796 |
"\\luamplibtagasgroupput{",trgroup.name,"}{", |
| 2797 |
[[\setbox\mplibscratchbox\hbox{\useboxresource\lastsavedboxresourceindex}]], |
| 2798 |
[[\wd\mplibscratchbox 0pt\ht\mplibscratchbox 0pt\dp\mplibscratchbox 0pt]], |
| 2799 |
[[\box\mplibscratchbox]], |
| 2800 |
"}\\endgroup", |
| 2801 |
"\\expandafter\\xdef\\csname luamplib.group.", trgroup.name, "\\endcsname{", |
| 2802 |
"\\setbox\\mplibscratchbox\\hbox{\\hskip",-llx,"bp\\raise",-lly,"bp\\hbox{", |
| 2803 |
"\\useboxresource \\the\\lastsavedboxresourceindex", |
| 2804 |
"}}\\wd\\mplibscratchbox",urx-llx,"bp\\ht\\mplibscratchbox",ury-lly,"bp", |
| 2805 |
"\\box\\mplibscratchbox}", |
| 2806 |
}) |
| 2807 |
else |
| 2808 |
if trgroup.wrapped then |
| 2809 |
trgroup.cnt = (trgroup.cnt or 0) + 1 |
| 2810 |
local objname = format("@mplibtrgr%s", trgroup.cnt) |
| 2811 |
put2output(tableconcat{ |
| 2812 |
"\\special{pdf:bxobj ", objname, " bbox ", bbox, "}", |
| 2813 |
"\\unhbox\\mplibscratchbox", |
| 2814 |
"\\special{pdf:put @resources <<", res, ">>}", |
| 2815 |
"\\special{pdf:exobj}", |
| 2816 |
"\\setbox\\mplibscratchbox\\hbox{\\special{pdf:uxobj ", objname, "}}", |
| 2817 |
}) |
| 2818 |
end |
| 2819 |
trgroup.cnt = (trgroup.cnt or 0) + 1 |
| 2820 |
local objname = format("@mplibtrgr%s", trgroup.cnt) |
| 2821 |
put2output(tableconcat{ |
| 2822 |
"\\special{pdf:bxobj ", objname, " bbox ", bbox, "}", |
| 2823 |
"\\unhbox\\mplibscratchbox", |
| 2824 |
"\\special{pdf:put @resources <<", res, ">>}", |
| 2825 |
"\\special{pdf:exobj <<", grattr, ">>}", |
| 2826 |
"\\luamplibtagasgroupput{",trgroup.name,"}{", |
| 2827 |
"\\special{pdf:uxobj ", objname, "}", |
| 2828 |
"}\\endgroup", |
| 2829 |
}) |
| 2830 |
token.set_macro("luamplib.group."..trgroup.name, tableconcat{ |
| 2831 |
"\\setbox\\mplibscratchbox\\hbox{\\hskip",-llx,"bp\\raise",-lly,"bp\\hbox{", |
| 2832 |
"\\special{pdf:uxobj ", objname, "}", |
| 2833 |
"}}\\wd\\mplibscratchbox",urx-llx,"bp\\ht\\mplibscratchbox",ury-lly,"bp", |
| 2834 |
"\\box\\mplibscratchbox", |
| 2835 |
}, "global") |
| 2836 |
end |
| 2837 |
trgroup.shifts[trgroup.name] = { llx, lly } |
| 2838 |
end |
| 2839 |
return grstate |
| 2840 |
end |
| 2841 |
function luamplib.registergroup (boxid, name, opts) |
| 2842 |
if opts.asgroup and opts.asgroup:find"wrapped" then |
| 2843 |
luamplib.registergroup(boxid, name, {bbox=opts.bbox, resources=opts.resources}) |
| 2844 |
run_tex_code{"\\setbox", boxid, "\\hbox bdir0{\\csname luamplib.group.", name, "\\endcsname}"} |
| 2845 |
opts.asgroup = opts.asgroup:gsub("wrapped","") |
| 2846 |
end |
| 2847 |
local box = texgetbox(boxid) |
| 2848 |
local wd, ht, dp = node.getwhd(box) |
| 2849 |
local is_mask = opts.asgroup and opts.asgroup:find"masking" |
| 2850 |
local res = opts.resources or "" |
| 2851 |
res = gather_resources(res) |
| 2852 |
local attr = { "/Type/XObject/Subtype/Form/FormType 1" } |
| 2853 |
if type(opts.matrix) == "table" then opts.matrix = tableconcat(opts.matrix," ") end |
| 2854 |
if type(opts.bbox) == "table" then opts.bbox = tableconcat(opts.bbox," ") end |
| 2855 |
if opts.matrix and opts.matrix:find"%a" then |
| 2856 |
local data = format("mplibtransformmatrix(%s);",opts.matrix) |
| 2857 |
process(data,"@mplibtransformmatrix") |
| 2858 |
opts.matrix = format("%f %f %f %f %f %f",tableunpack(luamplib.transformmatrix)) |
| 2859 |
end |
| 2860 |
local grtype = 3 |
| 2861 |
if opts.bbox then |
| 2862 |
attr[#attr+1] = format("/BBox[%s]", opts.bbox) |
| 2863 |
grtype = 2 |
| 2864 |
end |
| 2865 |
local mpllx, mplly = get_macro'MPllx', get_macro'MPlly' |
| 2866 |
if is_mask then |
| 2867 |
local t = opts.matrix and opts.matrix:explode() or {1, 0, 0, 1, 0, 0} |
| 2868 |
t[5], t[6] = t[5]+mpllx, t[6]+mplly |
| 2869 |
opts.matrix = format("%f %f %f %f %f %f",tableunpack(t)) |
| 2870 |
mpllx, mplly = 0, 0 |
| 2871 |
end |
| 2872 |
if opts.matrix then |
| 2873 |
attr[#attr+1] = format("/Matrix[%s]", opts.matrix) |
| 2874 |
grtype = opts.bbox and 4 or 1 |
| 2875 |
end |
| 2876 |
if opts.asgroup and not opts.asgroup:find"off" then |
| 2877 |
local t = { isolated = false, knockout = false, masking = false } |
| 2878 |
for _,v in ipairs(opts.asgroup:gsub("%s",""):explode",+") do t[v] = true end |
| 2879 |
local on |
| 2880 |
if t.masking then |
| 2881 |
on = update_pdfobjs(format("<</S/Transparency/CS%s>>", opts.colorspace or "/DeviceGray")) |
| 2882 |
else |
| 2883 |
local cs = opts.colorspace and ("/CS%s"):format(opts.colorspace) or "" |
| 2884 |
on = update_pdfobjs(format("<</S/Transparency%s/I %s/K %s>>", cs, t.isolated, t.knockout)) |
| 2885 |
end |
| 2886 |
attr[#attr+1] = format("/Group %s", pdfetcs.resfmt:format(on)) |
| 2887 |
end |
| 2888 |
local trgroup = pdfetcs.tr_group |
| 2889 |
trgroup.shifts[name] = { mpllx, mplly } |
| 2890 |
local whd |
| 2891 |
if pdfmode then |
| 2892 |
attr = tableconcat(attr) :gsub(decimals,rmzeros) |
| 2893 |
local index = tex.saveboxresource(boxid, attr, res, true, grtype) |
| 2894 |
token.set_macro("luamplib.group."..name, tableconcat{ |
| 2895 |
"\\useboxresource ", index, |
| 2896 |
}, "global") |
| 2897 |
whd = format("%.3f %.3f 0", wd/factor, (ht+dp)/factor) :gsub(decimals,rmzeros) |
| 2898 |
else |
| 2899 |
trgroup.cnt = (trgroup.cnt or 0) + 1 |
| 2900 |
local objname = format("@mplibtrgr%s", trgroup.cnt) |
| 2901 |
texsprint { |
| 2902 |
"\\expandafter\\newbox\\csname luamplib.groupbox.", trgroup.cnt, "\\endcsname", |
| 2903 |
"\\global\\setbox\\csname luamplib.groupbox.", trgroup.cnt, "\\endcsname", |
| 2904 |
"\\hbox{\\unhbox ", boxid, "}\\luamplibatnextshipout{", |
| 2905 |
"\\special{pdf:bcontent}", |
| 2906 |
"\\special{pdf:bxobj ", objname, " width ", wd, "sp height ", ht, "sp depth ", dp, "sp}", |
| 2907 |
"\\unhbox\\csname luamplib.groupbox.", trgroup.cnt, "\\endcsname", |
| 2908 |
"\\special{pdf:put @resources <<", res, ">>}", |
| 2909 |
"\\special{pdf:exobj <<", tableconcat(attr), ">>}", |
| 2910 |
"\\special{pdf:econtent}}", |
| 2911 |
} |
| 2912 |
token.set_macro("luamplib.group."..name, tableconcat{ |
| 2913 |
"\\setbox\\mplibscratchbox\\hbox{\\special{pdf:uxobj ", objname, "}}", |
| 2914 |
"\\wd\\mplibscratchbox ", wd, "sp", |
| 2915 |
"\\ht\\mplibscratchbox ", ht, "sp", |
| 2916 |
"\\dp\\mplibscratchbox ", dp, "sp", |
| 2917 |
"\\box\\mplibscratchbox", |
| 2918 |
}, "global") |
| 2919 |
whd = format("%.3f %.3f %.3f", wd/factor, ht/factor, dp/factor) :gsub(decimals,rmzeros) |
| 2920 |
end |
| 2921 |
info("w/h/d of group '%s': %s", name, whd) |
| 2922 |
end |
| 2923 |
|
| 2924 |
do |
| 2925 |
local function stop_special_effects(fade,opaq,over) |
| 2926 |
if fade then -- fading |
| 2927 |
stop_pdf_code() |
| 2928 |
end |
| 2929 |
if opaq then -- opacity |
| 2930 |
pdf_literalcode(opaq) |
| 2931 |
end |
| 2932 |
if over then -- color |
| 2933 |
if over:find"pdf:bc" then |
| 2934 |
put2output"\\special{pdf:ec}" |
| 2935 |
else |
| 2936 |
put2output"\\special{color pop}" |
| 2937 |
end |
| 2938 |
end |
| 2939 |
end |
| 2940 |
|
| 2941 |
local function script2table(s) |
| 2942 |
local t = {} |
| 2943 |
for _,i in ipairs(s:explode("\13+")) do |
| 2944 |
local k,v = i:match("(.-)=(.*)") -- v may contain = or empty. |
| 2945 |
if k and v and k ~= "" and not t[k] then |
| 2946 |
t[k] = v |
| 2947 |
end |
| 2948 |
end |
| 2949 |
return t |
| 2950 |
end |
| 2951 |
|
| 2952 |
local function pdf_textfigure(font,size,text,width,height,depth) |
| 2953 |
text = text:gsub(".",function(c) |
| 2954 |
return format("\\hbox{\\char%i}",string.byte(c)) -- kerning happens in metapost : false |
| 2955 |
end) |
| 2956 |
put2output("\\mplibtextext{%s}{%f}{%s}{%s}{%s}",font,size,text,0,0) |
| 2957 |
end |
| 2958 |
|
| 2959 |
local bend_tolerance = 131/65536 |
| 2960 |
|
| 2961 |
local rx, sx, sy, ry, tx, ty, divider = 1, 0, 0, 1, 0, 0, 1 |
| 2962 |
|
| 2963 |
local function pen_characteristics(object) |
| 2964 |
local t = mplib.pen_info(object) |
| 2965 |
rx, ry, sx, sy, tx, ty = t.rx, t.ry, t.sx, t.sy, t.tx, t.ty |
| 2966 |
divider = sx*sy - rx*ry |
| 2967 |
return not (sx==1 and rx==0 and ry==0 and sy==1 and tx==0 and ty==0), t.width |
| 2968 |
end |
| 2969 |
|
| 2970 |
local function concat(px, py) -- no tx, ty here |
| 2971 |
return (sy*px-ry*py)/divider,(sx*py-rx*px)/divider |
| 2972 |
end |
| 2973 |
|
| 2974 |
local function curved(ith,pth) |
| 2975 |
local d = pth.left_x - ith.right_x |
| 2976 |
if abs(ith.right_x - ith.x_coord - d) <= bend_tolerance and |
| 2977 |
abs(pth.x_coord - pth.left_x - d) <= bend_tolerance then |
| 2978 |
d = pth.left_y - ith.right_y |
| 2979 |
if abs(ith.right_y - ith.y_coord - d) <= bend_tolerance and |
| 2980 |
abs(pth.y_coord - pth.left_y - d) <= bend_tolerance then |
| 2981 |
return false |
| 2982 |
end |
| 2983 |
end |
| 2984 |
return true |
| 2985 |
end |
| 2986 |
|
| 2987 |
local function flushnormalpath(path,open) |
| 2988 |
local pth, ith |
| 2989 |
for i=1,#path do |
| 2990 |
pth = path[i] |
| 2991 |
if not ith then |
| 2992 |
pdf_literalcode("%f %f m",pth.x_coord,pth.y_coord) |
| 2993 |
elseif curved(ith,pth) then |
| 2994 |
pdf_literalcode("%f %f %f %f %f %f c", |
| 2995 |
ith.right_x,ith.right_y,pth.left_x,pth.left_y,pth.x_coord,pth.y_coord) |
| 2996 |
else |
| 2997 |
pdf_literalcode("%f %f l",pth.x_coord,pth.y_coord) |
| 2998 |
end |
| 2999 |
ith = pth |
| 3000 |
end |
| 3001 |
if not open then |
| 3002 |
local one = path[1] |
| 3003 |
if curved(pth,one) then |
| 3004 |
pdf_literalcode("%f %f %f %f %f %f c", |
| 3005 |
pth.right_x,pth.right_y,one.left_x,one.left_y,one.x_coord,one.y_coord ) |
| 3006 |
else |
| 3007 |
pdf_literalcode("%f %f l",one.x_coord,one.y_coord) |
| 3008 |
end |
| 3009 |
elseif #path == 1 then -- special case .. draw point |
| 3010 |
local one = path[1] |
| 3011 |
pdf_literalcode("%f %f l",one.x_coord,one.y_coord) |
| 3012 |
end |
| 3013 |
end |
| 3014 |
|
| 3015 |
local function flushconcatpath(path,open) |
| 3016 |
pdf_literalcode("%f %f %f %f %f %f cm", sx, rx, ry, sy, tx ,ty) |
| 3017 |
local pth, ith |
| 3018 |
for i=1,#path do |
| 3019 |
pth = path[i] |
| 3020 |
if not ith then |
| 3021 |
pdf_literalcode("%f %f m",concat(pth.x_coord,pth.y_coord)) |
| 3022 |
elseif curved(ith,pth) then |
| 3023 |
local a, b = concat(ith.right_x,ith.right_y) |
| 3024 |
local c, d = concat(pth.left_x,pth.left_y) |
| 3025 |
pdf_literalcode("%f %f %f %f %f %f c",a,b,c,d,concat(pth.x_coord, pth.y_coord)) |
| 3026 |
else |
| 3027 |
pdf_literalcode("%f %f l",concat(pth.x_coord, pth.y_coord)) |
| 3028 |
end |
| 3029 |
ith = pth |
| 3030 |
end |
| 3031 |
if not open then |
| 3032 |
local one = path[1] |
| 3033 |
if curved(pth,one) then |
| 3034 |
local a, b = concat(pth.right_x,pth.right_y) |
| 3035 |
local c, d = concat(one.left_x,one.left_y) |
| 3036 |
pdf_literalcode("%f %f %f %f %f %f c",a,b,c,d,concat(one.x_coord, one.y_coord)) |
| 3037 |
else |
| 3038 |
pdf_literalcode("%f %f l",concat(one.x_coord,one.y_coord)) |
| 3039 |
end |
| 3040 |
elseif #path == 1 then -- special case .. draw point |
| 3041 |
local one = path[1] |
| 3042 |
pdf_literalcode("%f %f l",concat(one.x_coord,one.y_coord)) |
| 3043 |
end |
| 3044 |
end |
| 3045 |
|
| 3046 |
local function flush (result,flusher) |
| 3047 |
if result then |
| 3048 |
local figures = result.fig |
| 3049 |
if figures then |
| 3050 |
for f=1, #figures do |
| 3051 |
info("flushing figure %s",f) |
| 3052 |
local figure = figures[f] |
| 3053 |
local objects = figure:objects() |
| 3054 |
local fignum = tonumber(figure:filename():match("([%d]+)$") or figure:charcode() or 0) |
| 3055 |
local miterlimit, linecap, linejoin, dashed = -1, -1, -1, false |
| 3056 |
local bbox = figure:boundingbox() |
| 3057 |
local llx, lly, urx, ury = bbox[1], bbox[2], bbox[3], bbox[4] -- faster than unpack |
| 3058 |
if urx < llx then |
| 3059 |
else |
| 3060 |
if tex_code_pre_mplib[f] then |
| 3061 |
put2output(tex_code_pre_mplib[f]) |
| 3062 |
end |
| 3063 |
pdf_startfigure(fignum,llx,lly,urx,ury) |
| 3064 |
start_pdf_code() |
| 3065 |
if objects then |
| 3066 |
local savedpath = nil |
| 3067 |
local savedhtap = nil |
| 3068 |
for o=1,#objects do |
| 3069 |
local object = objects[o] |
| 3070 |
local objecttype = object.type |
| 3071 |
local prescript = object.prescript |
| 3072 |
prescript = prescript and script2table(prescript) -- prescript is now a table |
| 3073 |
local cr_over = do_preobj_CR(object,prescript) -- color |
| 3074 |
local tr_opaq = do_preobj_TR(object,prescript) -- opacity |
| 3075 |
local fading_ = do_preobj_FADE(object,prescript) -- fading |
| 3076 |
local pattern_ = do_preobj_PAT(object,prescript) -- tiling pattern |
| 3077 |
local shading_ = do_preobj_shading(object,prescript) -- shading pattern |
| 3078 |
local trgroup = do_preobj_GRP(object,prescript) -- transparency group |
| 3079 |
if prescript and prescript.mplibtexboxid then |
| 3080 |
put_tex_boxes(object,prescript) |
| 3081 |
elseif objecttype == "start_bounds" or objecttype == "stop_bounds" then --skip |
| 3082 |
elseif objecttype == "start_clip" then |
| 3083 |
local evenodd = not object.istext and object.postscript == "evenodd" |
| 3084 |
start_pdf_code() |
| 3085 |
flushnormalpath(object.path,false) |
| 3086 |
pdf_literalcode(evenodd and "W* n" or "W n") |
| 3087 |
elseif objecttype == "stop_clip" then |
| 3088 |
stop_pdf_code() |
| 3089 |
miterlimit, linecap, linejoin, dashed = -1, -1, -1, false |
| 3090 |
elseif objecttype == "special" then |
| 3091 |
if prescript and prescript.postmplibverbtex then |
| 3092 |
figcontents.post[#figcontents.post+1] = prescript.postmplibverbtex |
| 3093 |
end |
| 3094 |
elseif objecttype == "text" then |
| 3095 |
local ot = object.transform -- 3,4,5,6,1,2 |
| 3096 |
start_pdf_code() |
| 3097 |
pdf_literalcode("%f %f %f %f %f %f cm",ot[3],ot[4],ot[5],ot[6],ot[1],ot[2]) |
| 3098 |
pdf_textfigure(object.font,object.dsize,object.text,object.width,object.height,object.depth) |
| 3099 |
stop_pdf_code() |
| 3100 |
elseif not trgroup and fading_ ~= "stop" then |
| 3101 |
local evenodd, collect, both = false, false, false |
| 3102 |
local postscript = object.postscript |
| 3103 |
if not object.istext then |
| 3104 |
if postscript == "evenodd" then |
| 3105 |
evenodd = true |
| 3106 |
elseif postscript == "collect" then |
| 3107 |
collect = true |
| 3108 |
elseif postscript == "both" then |
| 3109 |
both = true |
| 3110 |
elseif postscript == "eoboth" then |
| 3111 |
evenodd = true |
| 3112 |
both = true |
| 3113 |
end |
| 3114 |
end |
| 3115 |
if collect then |
| 3116 |
if not savedpath then |
| 3117 |
savedpath = { object.path or false } |
| 3118 |
savedhtap = { object.htap or false } |
| 3119 |
else |
| 3120 |
savedpath[#savedpath+1] = object.path or false |
| 3121 |
savedhtap[#savedhtap+1] = object.htap or false |
| 3122 |
end |
| 3123 |
else |
| 3124 |
local ml = object.miterlimit |
| 3125 |
if ml and ml ~= miterlimit then |
| 3126 |
miterlimit = ml |
| 3127 |
pdf_literalcode("%f M",ml) |
| 3128 |
end |
| 3129 |
local lj = object.linejoin |
| 3130 |
if lj and lj ~= linejoin then |
| 3131 |
linejoin = lj |
| 3132 |
pdf_literalcode("%i j",lj) |
| 3133 |
end |
| 3134 |
local lc = object.linecap |
| 3135 |
if lc and lc ~= linecap then |
| 3136 |
linecap = lc |
| 3137 |
pdf_literalcode("%i J",lc) |
| 3138 |
end |
| 3139 |
local dl = object.dash |
| 3140 |
if dl then |
| 3141 |
local d = format("[%s] %f d",tableconcat(dl.dashes or {}," "),dl.offset) |
| 3142 |
if d ~= dashed then |
| 3143 |
dashed = d |
| 3144 |
pdf_literalcode(dashed) |
| 3145 |
end |
| 3146 |
elseif dashed then |
| 3147 |
pdf_literalcode("[] 0 d") |
| 3148 |
dashed = false |
| 3149 |
end |
| 3150 |
local path = object.path |
| 3151 |
local transformed, penwidth = false, 1 |
| 3152 |
local open = path and path[1].left_type and path[#path].right_type |
| 3153 |
local pen = object.pen |
| 3154 |
if pen then |
| 3155 |
if pen.type == 'elliptical' then |
| 3156 |
transformed, penwidth = pen_characteristics(object) -- boolean, value |
| 3157 |
pdf_literalcode("%f w",penwidth) |
| 3158 |
if objecttype == 'fill' then |
| 3159 |
objecttype = 'both' |
| 3160 |
end |
| 3161 |
else -- calculated by mplib itself |
| 3162 |
objecttype = 'fill' |
| 3163 |
end |
| 3164 |
end |
| 3165 |
local shade_no, shade_stroking = do_preobj_SH(object,prescript) -- shading |
| 3166 |
if shade_no then |
| 3167 |
pdf_literalcode"q /Pattern cs" |
| 3168 |
objecttype = false |
| 3169 |
end |
| 3170 |
if transformed then |
| 3171 |
start_pdf_code() |
| 3172 |
end |
| 3173 |
if path then |
| 3174 |
if savedpath then |
| 3175 |
for i=1,#savedpath do |
| 3176 |
local path = savedpath[i] |
| 3177 |
if transformed then |
| 3178 |
flushconcatpath(path,open) |
| 3179 |
else |
| 3180 |
flushnormalpath(path,open) |
| 3181 |
end |
| 3182 |
end |
| 3183 |
savedpath = nil |
| 3184 |
end |
| 3185 |
if transformed then |
| 3186 |
flushconcatpath(path,open) |
| 3187 |
else |
| 3188 |
flushnormalpath(path,open) |
| 3189 |
end |
| 3190 |
if objecttype == "fill" then |
| 3191 |
pdf_literalcode(evenodd and "h f*" or "h f") |
| 3192 |
elseif objecttype == "outline" then |
| 3193 |
if both then |
| 3194 |
pdf_literalcode(evenodd and "h B*" or "h B") |
| 3195 |
else |
| 3196 |
pdf_literalcode(open and "S" or "h S") |
| 3197 |
end |
| 3198 |
elseif objecttype == "both" then |
| 3199 |
pdf_literalcode(evenodd and "h B*" or "h B") |
| 3200 |
end |
| 3201 |
end |
| 3202 |
if transformed then |
| 3203 |
stop_pdf_code() |
| 3204 |
end |
| 3205 |
local path = object.htap |
| 3206 |
if path then |
| 3207 |
if transformed then |
| 3208 |
start_pdf_code() |
| 3209 |
end |
| 3210 |
if savedhtap then |
| 3211 |
for i=1,#savedhtap do |
| 3212 |
local path = savedhtap[i] |
| 3213 |
if transformed then |
| 3214 |
flushconcatpath(path,open) |
| 3215 |
else |
| 3216 |
flushnormalpath(path,open) |
| 3217 |
end |
| 3218 |
end |
| 3219 |
savedhtap = nil |
| 3220 |
evenodd = true |
| 3221 |
end |
| 3222 |
if transformed then |
| 3223 |
flushconcatpath(path,open) |
| 3224 |
else |
| 3225 |
flushnormalpath(path,open) |
| 3226 |
end |
| 3227 |
if objecttype == "fill" then |
| 3228 |
pdf_literalcode(evenodd and "h f*" or "h f") |
| 3229 |
elseif objecttype == "outline" then |
| 3230 |
pdf_literalcode(open and "S" or "h S") |
| 3231 |
elseif objecttype == "both" then |
| 3232 |
pdf_literalcode(evenodd and "h B*" or "h B") |
| 3233 |
end |
| 3234 |
if transformed then |
| 3235 |
stop_pdf_code() |
| 3236 |
end |
| 3237 |
end |
| 3238 |
if shade_no then -- shading |
| 3239 |
pdf_literalcode("W%s %s /MPlibSh%s sh Q", |
| 3240 |
evenodd and "*" or "", shade_stroking and "s" or "n", shade_no) |
| 3241 |
end |
| 3242 |
end |
| 3243 |
end |
| 3244 |
if fading_ == "start" then |
| 3245 |
pdfetcs.fading.specialeffects = {fading_, tr_opaq, cr_over} |
| 3246 |
elseif trgroup == "start" then |
| 3247 |
pdfetcs.tr_group.specialeffects = {fading_, tr_opaq, cr_over} |
| 3248 |
elseif fading_ == "stop" then |
| 3249 |
local se = pdfetcs.fading.specialeffects |
| 3250 |
if se then stop_special_effects(se[1], se[2], se[3]) end |
| 3251 |
elseif trgroup == "stop" then |
| 3252 |
local se = pdfetcs.tr_group.specialeffects |
| 3253 |
if se then stop_special_effects(se[1], se[2], se[3]) end |
| 3254 |
else |
| 3255 |
stop_special_effects(fading_, tr_opaq, cr_over) |
| 3256 |
end |
| 3257 |
if fading_ or trgroup then -- extgs resetted |
| 3258 |
miterlimit, linecap, linejoin, dashed = -1, -1, -1, false |
| 3259 |
end |
| 3260 |
end |
| 3261 |
end |
| 3262 |
stop_pdf_code() |
| 3263 |
pdf_stopfigure() |
| 3264 |
for _,v in ipairs(figcontents) do |
| 3265 |
if type(v) == "table" then |
| 3266 |
texsprint"\\mplibtoPDF{"; texsprint(v[1], v[2]); texsprint"}" |
| 3267 |
else |
| 3268 |
texsprint(v) |
| 3269 |
end |
| 3270 |
end |
| 3271 |
if #figcontents.post > 0 then texsprint(figcontents.post) end |
| 3272 |
figcontents = { post = { } } |
| 3273 |
end |
| 3274 |
end |
| 3275 |
end |
| 3276 |
end |
| 3277 |
end |
| 3278 |
|
| 3279 |
function luamplib.convert (result, flusher) |
| 3280 |
flush(result, flusher) |
| 3281 |
return true -- done |
| 3282 |
end |
| 3283 |
end |
| 3284 |
|
| 3285 |
function luamplib.colorconverter (cr) |
| 3286 |
local n = #cr |
| 3287 |
if n == 4 then |
| 3288 |
local c, m, y, k = cr[1], cr[2], cr[3], cr[4] |
| 3289 |
return format("%.3f %.3f %.3f %.3f k %.3f %.3f %.3f %.3f K",c,m,y,k,c,m,y,k), "0 g 0 G" |
| 3290 |
elseif n == 3 then |
| 3291 |
local r, g, b = cr[1], cr[2], cr[3] |
| 3292 |
return format("%.3f %.3f %.3f rg %.3f %.3f %.3f RG",r,g,b,r,g,b), "0 g 0 G" |
| 3293 |
else |
| 3294 |
local s = cr[1] |
| 3295 |
return format("%.3f g %.3f G",s,s), "0 g 0 G" |
| 3296 |
end |
| 3297 |
end |
| 3298 |
-- |
| 3299 |
-- End of File `luamplib.lua'. |