《Lua in ConTeXt》插曲:框框

ConTeXt 的 xtable,是用框框实现的。框框,可能只有我这么称呼它,多数人是叫它文本框。我喜欢叫它框框。条条框框的框框。在 ConTeXt 里,它叫 \framed

下面的代码可以为时间戳加上框框,

\framed{2021 年 05 月 02 日}

排版结果为

框框可以指定宽度。我想用正文区域的高度 \textheight 作为上面这个例子里的框框的宽度。试试看,

\framed[width=\textheight]{2021 年 05 月 02 日}

结果为

若要确认这个框框的宽度是否是 \textheight,可 \showframe

然后用尺子量一下。

现在,我在框框里放入页码,然后用 \hfill 将时间戳和页码挤压到框框的两端:

\framed[width=\textheight]{2021 年 05 月 02 日\hfill\pagenumber}

结果如下:

\hfill 真是好东西……页码之所以是 3,是因为这是 card.pdf 的第 3 页啊。真快,不知不觉就第 3 页了。

接下来,用 \setuptexttexts 将这个框框塞进右边的留白区域里:

\def\timemachinebar#1{\framed[width=\textheight]{#1\hfill\pagenumber}}
\setuptexttexts[margin][][\hfill{\timemachinebar{2021 年 05 月 02 日}}\hfill]

\timemachinebar 肯定会出界的。那就旋转一下……再令框框两端向内偏移半个字宽的距离,让时间戳和页码距离框框的两端不要那么近:

\def\timemachinebar#1{%
  \framed[width=\textheight,
          loffset=.5em,
          roffset=.5em]{#1\hfill\pagenumber}}

我不希望页码躺着,所以对 \timemachinebar 里的 \pagenumber 旋转 90 度角:

\def\timemachinebar#1{%
  \framed[width=\textheight,
          loffset=.5em,
          roffset=.5em]{#1\hfill\rotate[rotation=90]{\pagenumber}}}

现在可以隐藏框框了,

\def\timemachinebar#1{%
  \framed[width=\textheight,
          frame=off,
          loffset=.5em,
          roffset=.5em]{#1\hfill\rotate[rotation=90]{\pagenumber}}}

我想让框框有些姿色,令其背景为黄色,前景为蓝色:

\def\timemachinebar#1{%
  \framed[width=\textheight,
          frame=off,
          loffset=.5em,
          roffset=.5em,
          background=color,
          backgroundcolor=yellow,
          foreground=color,
          foregroundcolor=blue]{#1\hfill\rotate[rotation=90]{\pagenumber}}}

然后重新定义 \timestamp 宏:

\def\timestamp#1{%
  \setuptexttexts
    [margin]
    []
    [\hfill{\rotate[rotation=270]{\timemachinebar{#1}}}\hfill]
}

这个新的 \timestamp 的效果如下图所示:

以后,我应该不会再修改时间戳的样式了。

最后,对一下 card-env.tex:

\definepapersize[card][width=85.6mm,height=53.98mm]
\setuppapersize[card]
\setuplayout
  [backspace=.1\paperwidth,
    width=.8\paperwidth,
    topspace=.015\paperheight,
    height=.97\paperheight,
    leftmargin=.666\backspace,
    rightmargin=.666\cutspace,
    headerdistance=.025\makeupheight,
    footerdistance=.025\makeupheight,
    textheight=.95\makeupheight]
 
\setuppagenumbering[location=]
%\setupfootertexts[margin][][\hfill\pagenumber\hfill]

\setuphead[title][align=middle]

\def\timemachinebar#1{%
  \framed[frame=off,
          width=\textheight,
          loffset=.5em,
          roffset=.5em,
          background=color,
          backgroundcolor=yellow,
          foreground=color,
          foregroundcolor=blue]
         {#1\hfill\rotate[rotation=90]{\pagenumber}}}
\def\timestamp#1{%
  \setuptexttexts
    [margin]
    []
    [\hfill{\rotate[rotation=270]{\timemachinebar{#1}}}]
}

\setupindenting[first,always,2em]
\setupinterlinespace[line=1.5em]

\def\cangjie#1{%
  \lower.2ex\hbox{\externalfigure[#1][width=\bodyfontsize]}}
\def\ok{\cangjie{ok}}
\def\no{\cangjie{no}}
\def\mask{\cangjie{mask.png}}

\startluacode
mingyi = {}

local ctx = context
local dim = number.todimen
local w = tex.dimen.textwidth
local w1, w2, w3 = 0.0655 * w, nil, 0.0655 * w; w2 = w - (w1 + w3)

function mingyi.make_row(row, n)
    ctx.startxrow()
    ctx.startxcell{width = dim(w1)}; ctx([[\m{\cdot}]]); ctx.stopxcell()
    if n == 1 then
        ctx.startxcell{width = dim(w2)}; ctx(row[1]); ctx.stopxcell()
        ctx.startxcell{width = dim(w3)}; ctx([[\strut ]] .. row[2]); ctx.stopxcell()
    else
        ctx.startxcell(); ctx(row[1]); ctx.stopxcell()
        ctx.startxcell(); ctx([[\strut ]] .. row[2]); ctx.stopxcell()        
    end
    ctx.stopxrow()
end

function mingyi.make_table(x)
    context.blank(halfline)
    context.startxtable{frame = "off"}
    for i, v in ipairs(x) do
        mingyi.make_row(v, i)
    end
    context.stopxtable()
    context.blank(halfline)
end
\stopluacode
\def\timemachine#1{\ctxlua{mingyi.make_table({#1})}}

\definefontfamily[myfont][serif][sourcehanserifcn]
\definefontfamily[myfont][math][xitsmath]
\setscript[hanzi]
\setupbodyfont[myfont,7pt]

春夏之交,杂花生树,草长莺飞,一切都生长得很快……card-env.tex 并不例外。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章