《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 並不例外。

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