答案1
看起来你对每个字母都使用了相同的图像。这可以通过 Ti钾Zfadings
库与和一起tikzfadingfrompicture
。scope fading
对于图像文件 cherries.jpg,我只是从“樱桃树”图像搜索中选择了一些内容。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\renewcommand*\sfdefault{ugq}
\usepackage[T1]{fontenc}
\newcommand{\letterimage}[1]{
\foreach \S[count=\n] in {#1}{%
\begin{tikzfadingfrompicture}[name=cherries\n]
\node [text=transparent!0, font=\sffamily, scale=6]{\S};
\end{tikzfadingfrompicture}%
\begin{tikzpicture}
\node[scope fading=cherries\n]{\includegraphics[width=2cm]{cherries.jpg}};
\end{tikzpicture}%
}
}
\begin{document}
\letterimage{C,H,A,R,M}
\end{document}
答案2
无需加载即可执行此操作任何包。
以下适用于 Plain pdfTeX、Plain LuaTeX、pdfLaTeX 和 LuaLaTeX:
\catcode`\@=11
\ifdefined\directlua
\def\pdfliteral{\pdfextension literal}
\def\pdfrestore{\pdfextension restore\relax}
\def\pdfsave{\pdfextension save\relax}
\let\pdflastxform=\lastsavedboxresourceindex
\let\pdflastximage=\lastsavedimageresourceindex
\let\pdfpageheight=\pageheight
\let\pdfpagewidth=\pagewidth
\let\pdfrefxform=\useboxresource
\let\pdfrefximage=\useimageresource
\let\pdfxform=\saveboxresource
\let\pdfximage=\saveimageresource
\fi
\newbox\bg@backgroundbox
\newbox\bg@outbox
\newbox\bg@textbox
\newdimen\bg@textwidth
\newdimen\bg@textheight
\protected\def\background#1#2{%
\setbox\bg@textbox=\hbox{#1}%
\bg@textwidth=\wd\bg@textbox%
\bg@textheight=\ht\bg@textbox%
\wd\bg@textbox=\z@%
\ht\bg@textbox=\z@%
%
\setbox\bg@backgroundbox=\hbox{#2}%
\wd\bg@backgroundbox=\pdfpagewidth%
\ht\bg@backgroundbox=\pdfpageheight%
\pdfxform\bg@backgroundbox%
\setbox\bg@backgroundbox=\hbox{\pdfrefxform\pdflastxform}%
\wd\bg@backgroundbox=\z@%
\ht\bg@backgroundbox=\z@%
%
\setbox\bg@outbox=\hbox{%
\pdfsave%
\pdfliteral direct {7 Tr}%
\box\bg@textbox%
\pdfsave%
\pdfliteral direct {0 Tr}%
\box\bg@backgroundbox%
\pdfrestore%
\pdfrestore%
}%
\wd\bg@outbox=\bg@textwidth%
\ht\bg@outbox=\bg@textheight%
\box\bg@outbox%
}
\protected\def\backgroundimage#1#2{%
\setbox\bg@textbox=\hbox{#1}%
\pdfximage width \wd\bg@textbox {#2}%
\background{#1}{\pdfrefximage\pdflastximage}%
}
\catcode`\@=12
\ifdefined\documentclass
\documentclass{article}
\begin{document}
\Huge\bfseries
\backgroundimage{CHARM}{NPBT_SC_background.jpg}
\end{document}
\else
\nopagenumbers
\font\tgab=ec-qagb at 72bp\tgab
\backgroundimage{CHARM}{NPBT_SC_background.jpg}
\background{CHARM}{%
\fiverm
\vbox to 1in{%
\leaders%
\vbox{%
\hbox to \hsize{%
\leaders%
\hbox{charm}%
\hfill%
}%
}%
\vfill%
}%
}
\background{CHARM}{\pdfliteral{
1 0 0 rg 0 00 360 20 re f
0 1 0 rg 0 20 360 20 re f
0 0 1 rg 0 40 360 20 re f
}}
\bye
\fi
首先,我们将背景(本例中为图像)的绘制命令保存到 XObject 中。然后,我们将文本渲染模式设置为“clip”(7),这会将文本轮廓添加到当前剪切路径。最后,我们绘制被文本剪切的 XObject。
编译结果如下luatex
:
答案3
从此曾是标记为元帖子,我想你可能会喜欢lualatex
+luamplib
版本。
为了编译它,您需要提供一个合适的cherries.png
文件,并用作lualatex
TeX 引擎。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{graphicx}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
vardef cherry_letter(expr A, fontname) =
save g, i; path g[]; numeric i; i = 0;
for item within glyph ASCII A of fontname:
g[incr i] = pathpart item scaled 1/8;
endfor
image(
draw TEX("\includegraphics[width=1.6in]{cherries.png}") shifted 10 down;
clip currentpicture to g1;
for k=2 upto i:
fill g[k] withcolor background;
endfor
)
enddef;
beginfig(1);
background := 1/256(180,230,182);
string s; s = "METAPOST";
numeric x; x = 0;
for i=1 upto length s:
string c; c = substring (i-1, i) of s;
picture t; t = cherry_letter(c, "phvb8r");
draw t shifted (x, 0);
x := x + xpart lrcorner t + if c = "T": 0 else: 12 fi;
endfor
picture P; P = currentpicture; currentpicture := nullpicture;
bboxmargin := 20;
fill bbox P withcolor background; draw P;
endfig;
\end{mplibcode}
\end{document}
笔记
- 不能
\includegraphics
与传统的 Metapost 一起使用 - 所以你需要使用
lualatex
这个。 - 参见Metapost 手册了解详情
glyph
。 - 如果您有很多这样的字距调整程序,您可能需要设计一个更智能的字距调整程序。