Commits

Philipp Gesang committed b342330

integrated titlos and dots

  • Participants
  • Parent commits 341c8b8

Comments (0)

Files changed (2)

tex/context/third/cyrillicnumbers/cyrillicnumbers.lua

 --- http://www.uni-giessen.de/partosch/eurotex99/berdnikov2.pdf
 --- <http://ru.wikipedia.org/wiki/Кириллическая_система_счисления>
 
+local iowrite     = io.write
+local mathceil    = math.ceil
 local mathfloor   = math.floor
 local tableconcat = table.concat
+local tablemaxn   = table.maxn
+local tableinsert = table.insert
 local utf8char    = unicode.utf8.char
+local utf8len     = unicode.utf8.len
 local utfupper    = unicode.utf8.upper
 
-thirddata = thirddata or { }
+local cyrnum     = {
+  placetitlo    = "font",
+  titlolocation = "final", -- above final digit
+  titlospan     = 3,       -- only with mp
+  drawdots      = true,
+  debug         = false,
+}
 
-local cyrillic_placetitlo = true
+thirddata        = thirddata or { }
+thirddata.cyrnum = cyrnum
+
+local dbgpfx = "[cyrnum]"
+local dbg = function (...)
+  if cyrnum.debug then
+    local args = {...}
+    if type(args[1]) == "table" then args = args[1] end
+    iowrite(dbgpfx)
+    for i=1, #args do
+      local this = args[i]
+      local tthis = type(this)
+      iowrite" "
+      if tthis == "number" or tthis == "string" then
+        iowrite(this)
+      else
+        iowrite(tostring(this))
+      end
+    end
+    iowrite"\n"
+  end
+end
 
 local cyrillic_numerals = {
   { "а", "в", "г", "д", "е", "ѕ", "з", "и", "ѳ", },
 local cyrillic_1m    = utf8char(0x489) -- combining million sign
 local cyrillic_titlo = utf8char(0x483) -- combining titlo
 
-local concat_cyrillic_nums = function (chars, upper)
-  local result = ""
-  for i=#chars, 1, -1 do
-    local this = chars[i]
-    if this then
-      if upper then this = utfupper(this) end
-      if i > 3 then
-        result = result .. cyrillic_1k .. this
-      else
-        result = result .. this
-      end
+
+--local concat_cyrillic_nums = function (chars, upper)
+--  dbg(chars)
+--  local nchars, result = tablemaxn(chars), ""
+--  --if cyrnum.placetitlo and cyrnum.titlomode == "fm" then -- above central char
+--  --  dbg("char cnt", nchars)
+--  --  dbg("insert pos", mathceil(nchars/2))
+--  --  tableinsert(chars, mathceil(nchars/2), cyrillic_titlo)
+--  --end
+--  for i=#chars, 1, -1 do
+--    local this = chars[i]
+--    if this then
+--      if upper then this = utfupper(this) end
+--      if i > 3 then
+--        result = result .. cyrillic_1k .. this
+--      else
+--        result = result .. this
+--      end
+--    end
+--  end
+--  if cyrnum.placetitlo and cyrnum.titlomode == "fl" then -- take titlo from font, place it after / above last char
+--    result = result .. cyrillic_titlo
+--  end
+--  return result
+--end
+
+local digits_only = function (list)
+  local result = { }
+  for i=1, tablemaxn(list) do
+    local elm = list[i]
+    if type(elm) == "string" then
+      result[#result+1] = elm
     end
   end
-  if cyrillic_placetitlo then -- not all fonts do this well
-    result = result .. cyrillic_titlo
+  return result
+end
+
+local lreverse = function(list)local r={}for i=tablemaxn(list),1,-1 do r[#r+1]=list[i]end return r end
+
+local start_titlo, stop_titlo = [[\cyrnumdrawtitlo{]], "}"
+
+local titlofuncs = {
+  font = function (list)
+    if cyrnum.titlolocation == "final" then
+      list[#list+1] = cyrillic_titlo
+    else -- “middle” or whatever
+      local pos = mathceil(tablemaxn(list)/2)
+      tableinsert(list, mathceil(tablemaxn(list)/2), cyrillic_titlo)
+    end
+    return lreverse(list)
+  end,
+  mp = function (list)
+    local result = { start_titlo }
+    for i=tablemaxn(list), 1, -1 do
+      result[#result+1] = list[i]
+    end
+    result[#result+1] = stop_titlo
+    return result
+  end,
+  no = function (x) return x end,
+}
+
+local concat_cyrillic_nums = function (list, upper)
+  local result         = ""
+  local digits         = digits_only(list) -- strip placeholders
+  local nlist, ndigits = tablemaxn(list), tablemaxn(digits)
+  dbg(list)
+  dbg(digits)
+  local titlo = titlofuncs[cyrnum.placetitlo]
+  if titlo then
+    result = tableconcat(titlo(digits))
+    if cyrnum.drawdots then
+      local sym = cyrnum.dotsymbol
+      result = sym .. result .. sym
+    end
   end
+  dbg(result)
   return result
 end
 

tex/context/third/cyrillicnumbers/t-cyrillicnumbers.mkiv

 
 \registerctxluafile{cyrillicnumbers}
 
-\enablemode[cyrnum-titlo9]
+\def\cyrnumsetvariables{%
+  \startluacode
+    local tc = thirddata.cyrnum
+    tc.placetitlo    = "\cyrnumparameter{titlo}"
+    tc.titlospan     = "\cyrnumparameter{titlospan}"
+    tc.titlolocation = "\cyrnumparameter{titlolocation}"
+    tc.drawdots      = "\cyrnumparameter{dots}" == "yes"
+    tc.dotsymbol     = "\cyrnumparameter{dotsymbol}"
+  \stopluacode%
+}
 
-\def\cyrnum_penwidth{12}
+\appendtoks \cyrnumsetvariables \to \everysetupcyrnum
+
+\setupcyrnum[
+          titlo=mp,     % font|mp|no
+      titlospan=3,      % n|all
+  titlolocation=middle, % final|middle -- only effective if span ~= all
+           dots=\v!yes, % yes|no
+      dotsymbol=·,
+       penwidth=3,
+      titlomode=5,
+]
+
+\startmoduletestsection
+  \ctxlua{thirddata.cyrnum.debug = true}
+\stopmoduletestsection
 
 \startuniqueMPgraphic{cyrnum-titlo1}
   path titlo ;
            ( 0.05,  .4*Vfactor) ..
            ( 0.95, -.4*Vfactor) ..
            ( 1.00,     Vfactor) ;
-  pickup pensquare yscaled \cyrnum_penwidth xscaled 1 rotated -30 ;
+  pickup pensquare yscaled \cyrnumparameter{penwidth} xscaled 1 rotated -30 ;
   draw titlo
        xysized (Hfactor*OverlayWidth,Vfactor*OverlayHeight)
        shifted (.5*(OverlayWidth-Hfactor*OverlayWidth),OverlayHeight-Vfactor*OverlayHeight) ;
 
 \startuniqueMPgraphic{cyrnum-titlo2}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .15 ;
   save Hfactor ; Hfactor = .80 ;
 
 \startuniqueMPgraphic{cyrnum-titlo3}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .15 ;
   save Hfactor ; Hfactor = .80 ;
 %%% Same as 2 with inner vertical stroke
 \startuniqueMPgraphic{cyrnum-titlo4}
   path titlo, stroke ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .15 ;
   save Hfactor ; Hfactor = .80 ;
 
 \startuniqueMPgraphic{cyrnum-titlo5}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .20 ;
   save Hfactor ; Hfactor = .85 ;
 
 \startuniqueMPgraphic{cyrnum-titlo6}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .20 ;
   save Hfactor ; Hfactor = .85 ;
 
 \startuniqueMPgraphic{cyrnum-titlo7}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .20 ;
   save Hfactor ; Hfactor = .85 ;
 
 \startuniqueMPgraphic{cyrnum-titlo8}
   path titlo, stroke ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .20 ;
   save Hfactor ; Hfactor = .85 ;
 
 \startuniqueMPgraphic{cyrnum-titlo9}
   path titlo ;
-  save penwidth ; penwidth = \cyrnum_penwidth ;
+  save penwidth ; penwidth = \cyrnumparameter{penwidth} ;
   save High, Low ;
   save Vfactor ; Vfactor = .10 ;
   save Hfactor ; Hfactor = .85 ;
   setbounds currentpicture to boundingbox OverlayBox ;
 \stopuniqueMPgraphic
 
-\startmode[cyrnum-titlo1]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo1}]
-\stopmode %%% titlo1
-\startmode[cyrnum-titlo2]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo2}]
-\stopmode
-\startmode[cyrnum-titlo3]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{titlo3}]
-\stopmode
-\startmode[cyrnum-titlo4]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo4}]
-\stopmode
-\startmode[cyrnum-titlo5]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo5}]
-\stopmode
-\startmode[cyrnum-titlo6]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo6}]
-\stopmode
-\startmode[cyrnum-titlo7]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo7}]
-\stopmode
-\startmode[cyrnum-titlo8]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo8}]
-\stopmode
-\startmode[cyrnum-titlo9]
-  \defineoverlay[cyrnum-titlo-overlay][\uniqueMPgraphic{cyrnum-titlo9}]
-\stopmode
+\defineoverlay[cyrnum-titlo-overlay1][\uniqueMPgraphic{cyrnum-titlo1}]
+\defineoverlay[cyrnum-titlo-overlay2][\uniqueMPgraphic{cyrnum-titlo2}]
+\defineoverlay[cyrnum-titlo-overlay3][\uniqueMPgraphic{cyrnum-titlo3}]
+\defineoverlay[cyrnum-titlo-overlay4][\uniqueMPgraphic{cyrnum-titlo4}]
+\defineoverlay[cyrnum-titlo-overlay5][\uniqueMPgraphic{cyrnum-titlo5}]
+\defineoverlay[cyrnum-titlo-overlay6][\uniqueMPgraphic{cyrnum-titlo6}]
+\defineoverlay[cyrnum-titlo-overlay7][\uniqueMPgraphic{cyrnum-titlo7}]
+\defineoverlay[cyrnum-titlo-overlay8][\uniqueMPgraphic{cyrnum-titlo8}]
+\defineoverlay[cyrnum-titlo-overlay9][\uniqueMPgraphic{cyrnum-titlo9}]
 
 \defineframed[cyrnum_titloframe][
-  location=low,
-  frame=off,
-  background=cyrnum-titlo-overlay,
+    location=low,
+       frame=off,
+  background=cyrnum-titlo-overlay\cyrnumparameter{titlomode},
 ]
 
 \def\Titlo{\dontleavehmode\cyrnum_titloframe}
 
+\let\cyrnumdrawtitlo\cyrnum_titloframe %% for protected context
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%% Interfacing with the Lua functions.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\def\cyrillicnumerals#1{\ctxcommand{cyrillicnumerals(\number#1)}}
+\def\Cyrillicnumerals#1{\ctxcommand{Cyrillicnumerals(\number#1)}}
+
+\defineconversion[cyrillicnumerals][\cyrillicnumerals]
+\defineconversion[Cyrillicnumerals][\Cyrillicnumerals]
+\defineconversion[cyr]             [\cyrillicnumerals]
+\defineconversion[Cyr]             [\Cyrillicnumerals]
+
 \unprotect \endinput