![ConTeXt:将背景图形横跨正反面页面吗?](https://linux22.com/image/270109/ConTeXt%EF%BC%9A%E5%B0%86%E8%83%8C%E6%99%AF%E5%9B%BE%E5%BD%A2%E6%A8%AA%E8%B7%A8%E6%AD%A3%E5%8F%8D%E9%9D%A2%E9%A1%B5%E9%9D%A2%E5%90%97%EF%BC%9F.png)
我想将背景图像放置在所有部分标题的后面,图像跨越两页,例如:
___________ ___________
| | |
| Part 1 | |
| Birds of | |
| South | |
| America | |
|___________|___________|
到目前为止,我已经创建了以下代码:
\definelayer[patternleft][x=0mm, y=0mm, width=\paperwidth, height=\paperheight]
\definelayer[patternright][x=0mm, y=0mm, width=\paperwidth, height=\paperheight]
\setlayer[patternleft] {%
\framed[frame=off, align=right]{%
\clip[nx=2, ny=1, x=1, y=1]{%
\externalfigure[background.jpg][factor=500]}%
}%
}%
\setlayer[patternright] {%
\framed[frame=off, align=left]{%
\clip[nx=2, ny=1, x=2, y=1]{%
\externalfigure[background.jpg][factor=500]}%
}%
}%
然后我将其放入文档中,如下所示:
\setupbackgrounds[page][background=patternleft] % sets background for verso
\part{Birds of South America}
\page[right]
\setupbackgrounds[page][background=patternright] % sets background for rector
\page[right]
\setupbackgrounds[page][background={}] % sets a blank background
This is some text.
这存在以下问题:
- 图像的尺寸与两页相同,但是,边缘和折叠处仍留有空白。
- 这有时会将背景放在错误的页面上。
我如何改进此代码,以便背景图形跨越标题的正面和背面页面\part
?
答案1
为了扩展@Aditya 在其评论中暗示的方法,您可以使用宏将您选择的任何图片切成合适的部分\clip
。首先,使用设置分区网格\setupclipping
,例如:
\setupclipping[nx=2,ny=4]
这将定义裁剪为水平方向使用两个段、垂直方向使用四个段。然后,您可以使用它们的坐标来定位这些部分:
\clip[x=2,y=3]{...}
这将提取位于应用宏的第四行第三列的部分。双面背景是一个更简单的问题,因为它只需要一个水平分割:
\setupclipping[nx=2,ny=1]
%% left half:
\clip[x=1,y=1]{\externalfigure[...][width=2\paperwidth]}
%% right half:
\clip[x=2,y=1]{\externalfigure[...][width=2\paperwidth]}
现在你只需要把这些部分画成背景。这在 Context 中没有问题,因为所有的构成页面布局的元素派生自
\framed
,并继承了其选项。因此,为了绘制图像作为页面背景,您可以使用与 相同的策略\framed
:将内容包裹在覆盖层中并将其分配给背景。跨越整个页面的元素称为paper
,您可以通过 对其进行配置\setupbackgrounds
:
\defineoverlay [background:overlay] [\backgroundcmd]
\setupbackgrounds [paper] [background=overlay:background]
剩下的就是定义\backgroundcmd
如何根据当前页面是正面还是反面来剪切左半部分或右半部分:
\def\backgroundcmd{
\ifodd\pagenumber
\clip[x=2,y=1]{\externalfigure[...]}
\else
\clip[x=1,y=1]{\externalfigure[...]}
\fi%
}
综合所有因素,解决方案可能如下所示:
\setupexternalfigures [location={default}] %% figure search path
\setuppagenumbering [alternative=doublesided]
\unprotect
%% 1) a named figure for inclusion, scaled to twice the width of one
%% page
\useexternalfigure [half_cow] [cow.pdf] [
width=2\paperwidth,
height=\paperheight,
]
%% 2) a command that draws the left or right portion of a figure,
%% depending on whether the page is recto or verso.
%%
%% also, we install a counter to check if the background should be
%% placed at all, since it is only desired with “\part” headings.
%% this counter will be decremented with every invocation and reset
%% whenever a new part begins.
\newcount\cowcount
\def\pickhalfcow_cmd{%
\setupclipping [nx=2,ny=1,]%
\ifnum\cowcount>0
\ifodd\pagenumber %% clip right half
\clip[x=2,y=1]
\else %% clip left half
\clip[x=1,y=1]
\fi{\externalfigure[half_cow]}%
\global \advance \cowcount \minusone
\fi%
}
%% 3a) define an overlay that we can use as page background
\defineoverlay [pickhalfcow] [\pickhalfcow_cmd]
%% 3b) hook the overlay into the “paper” element of the layout
\setupbackgrounds [paper] [background=pickhalfcow]
\setuphead [part] [
page=left, %% assumption: parts always begin verso
insidesection=\cowbackgrounds,
]
%% 4) define auxiliary macro that resets the background image counter
\def\cowbackgrounds{\global\cowcount=2}
\protect
%% Ready. Here’s some code that generates a couple pages.
\starttext
\startcolor[red]
\definedfont[name:IwonaMediumRegular at 22pt]
\setupinterlinespace[25pt]
\startpart[title=South America etc.]
\dorecurse{5}{
\input knuth \page
}
\stoppart
\startpart[title=Birds And Such]
\dorecurse{5}{
\input knuth \page
}
\stoppart
\stopcolor
\stoptext
更新:将代码挂接到部分标题中。