如何隨機化 ConTeXT 中每個單字的文字大小?

如何隨機化 ConTeXT 中每個單字的文字大小?

從這樣的一段文字開始:

This is a sample passage.

如何將每個單字設定為從 3-4 個可能大小的清單中隨機顯示不同的大小?

例如,在輸出中,“This”可能很大,“is”可能很小,“a”可能中等大小,等等。

  • 每次渲染文件時,它們的大小都可以不同。

答案1

除了 Aditya 和 Henri ConTeXt 提到的兩個命令之外,ConTeXt 還提供了命令\applytosplitstringwordspaced並將\applytosplitstringcharspaced命令應用於給定參數中的每個word或。character

\startluacode

function userdata.randomwordsize(str)
    local sizes = { "10pt", "12pt", "14pt", "16pt", "18pt", "20pt" }
    local size  = sizes[math.random(1,6)]
    context.startfont{ string.formatters["Serif at %s"](size) }
        context(str)
    context.stopfont()
end

\stopluacode

\starttext

\defineexpandable[1]\RandomWordSize
  {\ctxlua{userdata.randomwordsize("#1")}}

\applytosplitstringwordspaced\RandomWordSize
  {The fraction of fossil olfactory receptor genes is significantly
   higher in all species with full color vision. This suggests that
   the evolution of trichromatic vision --- which allows these
   primates to detect food, mates, and danger with visual cues ---
   has reduced their reliance on the sense of smell.}

\blank

\applytosplitstringcharspaced\RandomWordSize
  {The fraction of fossil olfactory receptor genes is significantly
   higher in all species with full color vision. This suggests that
   the evolution of trichromatic vision --- which allows these
   primates to detect food, mates, and danger with visual cues ---
   has reduced their reliance on the sense of smell.}

\stoptext

字串中每個單字的隨機大小

答案2

困難的部分是從段落中分離出單詞,並對每個單字應用任意宏。 ConTeXt 提供了執行此操作的命令\processwords。要使用此命令,您首先必須定義一個\processword將作用於特定單字的巨集。

舉個例子,假設您想在每個單字周圍畫一個框架。然後,定義:

\def\processword{\inframed}

\starttext
Normal text \processwords{This is a simple passage.} Normal text
\stoptext

這使

在此輸入影像描述

現在,下一步是建立一個宏,該宏選擇隨機字體大小並套用它。

答案3

Aditya 的答案原則上是正確的,但隨機化字體大小的幼稚巨集不起作用。這是因為預設\processwords使用“老電話”其中每個單字都放在一個框中,並且\processword巨集僅計算一次。為了緩解這個問題,我們必須手動切換到“MetaFun 通話”這也不需要呼叫Lua。

\def\processword#1{{\switchtobodyfont[\randomnumber{5}{15}pt]#1}}

\starttext

Normal text \processwords{This is a simple passage.} Normal text

\unprotect
\def\processwords#1%
  {\syst_helpers_process_word#1 \_e_o_w_}% no \unskip
\protect
Normal text \processwords{This is a simple passage.}Normal text

\stoptext

在此輸入影像描述

或者,您可以使用\processseparatedlist它允許您使用任意分隔符號:

\processseparatedlist
  [This is a simple passage.]
  [ ]
  {\groupedcommand{\bgroup\switchtobodyfont[\randomnumber{5}{15}pt]}{\space\egroup}}

在所有情況下,請注意清單後面是否有虛假空格。

相關內容