diff options
Diffstat (limited to 'tex/context/base/chem-str.lua')
-rw-r--r-- | tex/context/base/chem-str.lua | 122 |
1 files changed, 92 insertions, 30 deletions
diff --git a/tex/context/base/chem-str.lua b/tex/context/base/chem-str.lua index 347363345..d724394bb 100644 --- a/tex/context/base/chem-str.lua +++ b/tex/context/base/chem-str.lua @@ -44,6 +44,8 @@ local P, R, S, C, Cs, Ct, Cc, Cmt = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Cs, lpe local variables = interfaces and interfaces.variables local commands = commands local context = context +local implement = interfaces.implement + local formatters = string.formatters local texgetcount = tex.getcount @@ -60,11 +62,14 @@ local mpnamedcolor = attributes.colors.mpnamedcolor local topoints = number.topoints local todimen = string.todimen +local trialtypesetting = context.trialtypesetting + chemistry = chemistry or { } local chemistry = chemistry chemistry.instance = "chemistry" chemistry.format = "metafun" +chemistry.method = "double" chemistry.structures = 0 local common_keys = { @@ -147,6 +152,8 @@ local one_keys = { es = "line", ed = "line", et = "line", + au = "line", + ad = "line", cz = "text", rot = "transform", dir = "transform", @@ -156,6 +163,7 @@ local one_keys = { local ring_keys = { db = "line", + hb = "line", br = "line", lr = "line", rr = "line", @@ -316,7 +324,7 @@ local pattern = -- print(lpegmatch(pattern,"RZ13=x")) -- 1 RZ false false table x local f_initialize = 'if unknown context_chem : input mp-chem.mpiv ; fi ;' -local f_start_structure = formatters['chem_start_structure(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'] +local f_start_structure = formatters['chem_start_structure(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'] local f_set_trace_bounds = formatters['chem_trace_boundingbox := %l ;'] local f_stop_structure = 'chem_stop_structure;' local f_start_component = 'chem_start_component;' @@ -419,7 +427,11 @@ local function process(level,spec,text,n,rulethickness,rulecolor,offset,default_ insert(sstack,variant) m = m + 1 ; metacode[m] = syntax.save.direct elseif operation == "restore" then - variant = remove(sstack) + if #sstack > 0 then + variant = remove(sstack) + else + report_chemistry("restore without save") + end local ss = syntax[variant] keys, max = ss.keys, ss.max m = m + 1 ; metacode[m] = syntax.restore.direct @@ -536,6 +548,8 @@ local function process(level,spec,text,n,rulethickness,rulecolor,offset,default_ if not t then txt, t = fetch(txt) end if t then t = molecule(processor_tostring(t)) +-- local p, t = processors.split(t) +-- m = m + 1 ; metacode[m] = f_text(operation,p or align,variant,si,t) m = m + 1 ; metacode[m] = f_text(operation,align,variant,si,t) end end @@ -605,7 +619,7 @@ end -- -- rulethickness in points -local function checked(d,factor,unit,scale) +local function checked(d,bondlength,unit,scale) if d == v_none then return 0 end @@ -613,9 +627,9 @@ local function checked(d,factor,unit,scale) if not n then -- assume dimen elseif n >= 10 or n <= -10 then - return factor * unit * n / 1000 + return bondlength * unit * n / 1000 else - return factor * unit * n + return bondlength * unit * n end local n = todimen(d) if n then @@ -625,7 +639,7 @@ local function checked(d,factor,unit,scale) end end -local function calculated(height,bottom,top,factor,unit,scale) +local function calculated(height,bottom,top,bondlength,unit,scale) local scaled = 0 if height == v_none then -- this always wins @@ -634,24 +648,24 @@ local function calculated(height,bottom,top,factor,unit,scale) top = "0pt" elseif height == v_fit then height = "true" - bottom = bottom == v_fit and "true" or topoints(checked(bottom,factor,unit,scale)) - top = top == v_fit and "true" or topoints(checked(top, factor,unit,scale)) + bottom = bottom == v_fit and "true" or topoints(checked(bottom,bondlength,unit,scale)) + top = top == v_fit and "true" or topoints(checked(top, bondlength,unit,scale)) else - height = checked(height,factor,unit,scale) + height = checked(height,bondlength,unit,scale) if bottom == v_fit then if top == v_fit then bottom = height / 2 top = bottom else - top = checked(top,factor,unit,scale) + top = checked(top,bondlength,unit,scale) bottom = height - top end elseif top == v_fit then - bottom = checked(bottom,factor,unit,scale) + bottom = checked(bottom,bondlength,unit,scale) top = height - bottom else - bottom = checked(bottom,factor,unit,scale) - top = checked(top, factor,unit,scale) + bottom = checked(bottom,bondlength,unit,scale) + top = checked(top, bondlength,unit,scale) local ratio = height / (bottom+top) bottom = bottom * ratio top = top * ratio @@ -669,7 +683,7 @@ function chemistry.start(settings) local width = settings.width or v_fit local height = settings.height or v_fit local unit = settings.unit or 655360 - local factor = settings.factor or 3 + local bondlength = settings.factor or 3 local rulethickness = settings.rulethickness or 65536 local rulecolor = settings.rulecolor or "" local axiscolor = settings.framecolor or "" @@ -683,7 +697,7 @@ function chemistry.start(settings) -- align = settings.symalign or "auto" if trace_structure then - report_chemistry("unit %p, factor %s, symalign %s",unit,factor,align) + report_chemistry("unit %p, bondlength %s, symalign %s",unit,bondlength,align) end if align ~= "" then align = "." .. align @@ -713,10 +727,10 @@ function chemistry.start(settings) local sp_width = 0 local sp_height = 0 -- - width, left, right, sp_width = calculated(width, left, right,factor,unit,scale) - height, bottom, top, sp_height = calculated(height,bottom,top, factor,unit,scale) + width, left, right, sp_width = calculated(width, left, right,bondlength,unit,scale) + height, bottom, top, sp_height = calculated(height,bottom,top, bondlength,unit,scale) -- - if width ~= "true" and height ~= "true" and texgetcount("@@trialtypesetting") ~= 0 then + if width ~= "true" and height ~= "true" and trialtypesetting() then if trace_structure then report_chemistry("skipping trial run") end @@ -736,7 +750,7 @@ function chemistry.start(settings) metacode[#metacode+1] = f_start_structure( chemistry.structures, left, right, top, bottom, - rotation, topoints(unit), factor, topoints(offset), + rotation, topoints(unit), bondlength, scale, topoints(offset), tostring(settings.axis == v_on), topoints(rulethickness), tostring(axiscolor) ) metacode[#metacode+1] = f_set_trace_bounds(trace_boundingbox) ; @@ -757,6 +771,7 @@ function chemistry.stop() metapost.graphic { instance = chemistry.instance, format = chemistry.format, + method = chemistry.method, data = mpcode, definitions = f_initialize, } @@ -765,14 +780,12 @@ function chemistry.stop() end end -function chemistry.component(spec,text,settings) +function chemistry.component(spec,text,rulethickness,rulecolor) if metacode then - rulethickness, rulecolor, offset = settings.rulethickness, settings.rulecolor local spec = settings_to_array_with_repeat(spec,true) -- no lower? local text = settings_to_array_with_repeat(text,true) - -- inspect(spec) metacode[#metacode+1] = f_start_component - process(1,spec,text,1,rulethickness,rulecolor) -- offset? + process(1,spec,text,1,rulethickness,rulecolor) metacode[#metacode+1] = f_stop_component end end @@ -785,11 +798,52 @@ end) -- interfaces -commands.undefinechemical = chemistry.undefine -commands.definechemical = chemistry.define -commands.startchemical = chemistry.start -commands.stopchemical = chemistry.stop -commands.chemicalcomponent = chemistry.component +implement { + name = "undefinechemical", + actions = chemistry.undefine, + arguments = "string" +} + +implement { + name = "definechemical", + actions = chemistry.define, + arguments = { "string", "string", "string" } +} + +implement { + name = "startchemical", + actions = chemistry.start, + arguments = { + { + { "width" }, + { "height" }, + { "left" }, + { "right" }, + { "top" }, + { "bottom" }, + { "scale" }, + { "rotation" }, + { "symalign" }, + { "axis" }, + { "framecolor" }, + { "rulethickness" }, + { "offset" }, + { "unit" }, + { "factor" } + } + } +} + +implement { + name = "stopchemical", + actions = chemistry.stop, +} + +implement { + name = "chemicalcomponent", + actions = chemistry.component, + arguments = { "string", "string", "string", "string" } +} -- todo: top / bottom -- maybe add "=" for double and "≡" for triple? @@ -806,7 +860,9 @@ local inline = { ["space"] = "\\chemicalspace", } -function commands.inlinechemical(spec) +local ctx_chemicalinline = context.chemicalinline + +function chemistry.inlinechemical(spec) local spec = settings_to_array_with_repeat(spec,true) for i=1,#spec do local s = spec[i] @@ -814,7 +870,13 @@ function commands.inlinechemical(spec) if inl then context(inl) -- could be a fast context.sprint else - context.chemicalinline(molecule(s)) + ctx_chemicalinline(molecule(s)) end end end + +implement { + name = "inlinechemical", + actions = chemistry.inlinechemical, + arguments = "string" +} |