% !TEX TS-program = ConTeXt2021

\setuppapersize[A4]

%Define the elements of Kihon-hanmen. 
%Column length should be multiples of the character size.
%Following dimensions are taken from JIS X 4051, annex 3, case Y-4
\directlua{
unit = "pt"

characterSizeNumeric = 9
characterSize = tostring(characterSizeNumeric) .. unit

characterCountPerColumn = 25

columnCount = 2

lineCount = 46

lineGapNumeric = 6

columnGapNumeric = 18
columnGap = tostring(columnGapNumeric) .. unit

topMarginNumericMm = 29
topMarginNumeric = 2.8346 * topMarginNumericMm

headerFooterHeightNumeric = characterSizeNumeric
headerFooterHeight = tostring(headerFooterHeightNumeric) .. unit

headerFooterDistanceNumeric = characterSizeNumeric
headerFooterDistance = tostring(headerFooterDistanceNumeric) .. unit

topMarginToHeaderNumeric = topMarginNumeric - headerFooterHeightNumeric - headerFooterDistanceNumeric
topMarginToHeader = tostring(topMarginToHeaderNumeric) .. unit

lineFeedNumeric = characterSizeNumeric + lineGapNumeric
lineFeed = tostring(lineFeedNumeric) .. unit

lineWidthNumeric = characterSizeNumeric * characterCountPerColumn

kihonHanmenWidthNumeric = columnCount * lineWidthNumeric + (columnCount - 1) * columnGapNumeric
kihonHanmenWidth = tostring(kihonHanmenWidthNumeric) .. unit

kihonHanmenHeightNumeric = lineCount * characterSizeNumeric + (lineCount - 1) * lineGapNumeric
kihonHanmenHeight = tostring(kihonHanmenHeightNumeric) .. unit

makeUpHeightNumeric = kihonHanmenHeightNumeric + headerFooterHeightNumeric + headerFooterDistanceNumeric
makeUpHeight = tostring(makeUpHeightNumeric) .. unit
}
  
\setuplayout[
  topspace=\directlua{tex.print(topMarginToHeader)},
  header=\directlua{tex.print(headerFooterHeight)},
  headerdistance=\directlua{tex.print(characterSize)},
  footer=\directlua{tex.print(headerFooterHeight)},
  footerdistance=\directlua{tex.print(characterSize)},
  width=\directlua{tex.print(kihonHanmenWidth)}, 
  height=\directlua{tex.print(makeUpHeight)}, 
]

\setuppagenumbering[alternative=doublesided]

\setupheadertexts
        [] [\llap{\hskip -\directlua{tex.print(characterSize)} 右の柱及びノンブル}] % negative hskip doesn't work. Later, replace the text with {\getmarking[chapter]}
        [\rlap{\hskip \directlua{tex.print(characterSize)} 左の柱及びノンブル}] [] % Later, replace the text with {\getmarking[chapter]}
\setuppagenumbering[location=]

\setupfootertexts
        []    [\llap{\hskip -\directlua{tex.print(characterSize)} {\pagenumber}}] %negative hskip doesn't work
        [\rlap{\hskip \directlua{tex.print(characterSize)} {\pagenumber}}]        []


%Visual Debugging, to be commented out later
\showframe
\showgrid
\showmakeup[penalty] 
\showmakeup[glyph] 
\showmakeup[space] 
%\showmakeup[depth] 


\definebodyfontenvironment[\directlua{tex.print(characterSize)}][interlinespace=\directlua{tex.print(lineFeed)}]

\definepagecolumns[mycolumns]
[
  n=\directlua{tex.print(columnCount)}, 
  distance=\directlua{tex.print(columnGap)}, 
  frame=on, %to be commented out later
]

\definefontfamily [JapaneseFont] [rm] [hiraginominchoprow3]
\definefontfamily [JapaneseFont] [ss] [hiraginominchoprow3]
\definefontfamily [JapaneseFont] [mm] [math] [lucidaot] [default]

\setupbodyfont[JapaneseFont,\directlua{tex.print(characterSize)}]
\setupruby[location=top,voffset=-1.7ex]
\mainlanguage[ja]
\language[ja]
\setscript[nihongo]

%Line head indent at the beginning of paragraphs is applied to all paragraphs. Nearly all books and magazines make use of this method.
\setupindenting[yes, 1em]


\starttext

\startmycolumns
%The text has no meaning and is imitating some Japanese lorum ipsum.
打構セト読役いゆお及層\ruby{僕}{ぼく}はもう大コモクカ軟毎ホアヲト極書た球８７本野ぎレべ襲画売ぎ関負ら断紀チラネキ質、紋キタ資私トゃふん。江コキトサ狂毎現ンづ応通サシ続３６島性８４界ぼゆゃが実書ちぽふず辞躍\color[red]{【}事シ世図二ハス盛堀\ruby{早}{はや}く人わレをう第紙きかイの浅６９乳キメタ事争つょお\ruby{休}{やす}みなさい社以ヨナム法例やほにル済療にしいむ。子け記多ゅくよ正役モ母憲ト\ruby{寝}{ね}る。ぞ説帝け務賞ク付打ワナ内桁ぼでクフ因倒おふフ印気地レタケマ冬４雇各裁微ひるぼ。

事ーやずお両遇ほがお職喜じきちそ経岡ウナオケ型児てきろ置実イハ隊留社びか車斉ムマ数今せぴさし都\color[red]{｛}聞最へぼう康自コミニク月言ム検代捕ぶねやう。６本うげがぎ無多シワソ多転のづが加分がど臣次オモエヱ務教すそ必組テタレソhello, nice to meet you極渡げフ資聞ぽへトづ禁昔キイウニ食度地ヱ考性ユエ内生ヱセ音９４党と経陸咲幼勤りだフ。８３男融録ょもな氷社くにルべ害変でおけう変世７超キ減安リヌ株崎５転理済ほだをな黒府コワスソ地賢堂衛イぶにし。

広げがぴ視立ス位心もスわ止想ツクケラ産竹聞ヌ落３８判境ノア賞７写カチ華供ソミワ名認ばゆずほ始色らふひも属対７３写けで止突兄呂びの。策イルリヘ行十織キヘ興必む万初ゃづは勝米い館独ム感一チヒナ希８写タムシイ室情づがい焼殺つか広質トクロ毎新ソモ患関済えめね働表め白告こリあ杯困摘屈裂れ。

名テホユ稿国む生覧ヨナケヤ文６垣めこル社２市むあし保止転済ノ問判りッ集員セワ全宣コ森領で温子う\color[red]{。}ラちぎ夏査せすむ来資ニ面三わぼ際６之減喰杜き。堂ヲ賞了かえー単惑イめ秋投クツ意拡平ヘタヱ時８３死れる互武ぽほ拉５１届ニネラヘ掲団ヨ度両議ク。状親日値ル断大示ヘニコク米界を連禁イが性正ルッ問句サルユ試片サロヤ前毎到くぜ黄雑らりえ報同ょきむ督晴岡確たごげむ。

両おぎゅら写合ル書殖だインぎ孝自氷ばぞひ事権ウノレヲ少止ヲマヨミ惑磁ぞくゅ竹月おやま手５１質サム付２仏い職断年にーばざ場現ムワホ襲印講行否えうご。５帰７報ク用講ルのよす媛表サシメマ札変ナヲ案積出ソトマセ決情注モハイ際県らぶドご食績どはあだ徳増フシ断県ぼね了件親億かゆ。申府ホ線全る訪親ケヒネ芸比歳よをづう供記モケ正同とき練１紀ゅくふい割市ざ著法べちめせ客答ヤヌキオ入朗ルヌカス況写天境月郎ろたづ。

想ネシモヤ人時ワ化新リぱイこ京希っじ索視ケア風月受マカ東初ぞ月公て無速だ文保か受４香刃尿憎潔だクばづ。果テカ川程イ相安べろ方4映チ米闘ヤタレ会民クをづい治果とルせ字社かイよ新軍こ持芸ワラウケ掲三５８治館壌ゃ。行びまむ快野分真タネス応住えじで半記キネト正６者理ぼあ面出ヱシレワ慎首レトアハ託展オ良育圏ーを申傷説ひよもぶ索構を持試奇拝括ぱふ。

写みを参念タ三辞よルさき想３込ぜトリ会図ロ法部じち之杉ぎば厳近ワオコ光傷治ワ整区童熊習本さはリん。一ナカヒハ経松隊整キメケト需湖モユエウ秘千そ含覧ふリさ挙言ねをク凍誉ょい利際ゃぼの織用ユリル育４緊ご選１面とし胃危雪異みのひき。竹品ハ著供うげル島該ょ城豊ぎすご腹独べじり皆５座わーう化著式サマ木陸ミレノ義軍８０鯨ヒキトフ読大フニノネ読覧ロヨヤイ投時ょうす都活きっみざ。

上なラ済経ロ有実マ乳装ざなぴま武１守い掲６町ぼ禁２８担惑らばんフ社策送案物んまへ。人際クょゃね験伝め念変クマヌ白馳ナツエヤ著側ッごフぞ済川さ趣載ツア挙８５適クウネレ稿並ドむわの論棋ナソスヒ教伎締潤革フ。球尊話者が心三いむり達尼スか町過ミタオ上止ワカヤ正着ざぱぎぼ失成行偽ワマヘ北奮く。

済開ぼスじと力紹オア手混チコ成意コテモケ定因ド情最どいとづ写今えりめ問景ルオフチ度自す作１２行トヱワ録消ひイ。縦右で後賞たじつド諸場後し面詐でばト題社のれふ年結すたぜじ下労リえたゅ高作セレテ給５山ワ任共ご京前ミレシ本写午吉活ぽし。播るせ部３席ヒ投揮ぴしんけ安図んき同更展将ごさぽ画９用サキ購２１十ツヘト京締乱僕ろ。

図ラ象４５韓ど考小ヌニ野初ムユイシ河止中ヤトキヒ巻女ッ除稿イ輔余た研弔カ被悪テヘ業購ヘタ詳支ルれ身日だがん出療ずー盛趣なばぐ離女放佳怖れずへ。理じかぐ討１５枠制８五袋ヒク選会ソアシカ主手付みとこ猪月メ語経マオヱミ社接フセユア幌財ー香口衆ンろ著瀬なこン身個供悪射窃どスあ。切テセ重購ニホ覧権じゆむげ影文ヒヘ相断６６哉ノホテフ黒植くね氏員更あきぱゆ文記ふわひ載更上ぜいひば。

打構セト読役いゆお及層\ruby{僕}{ぼく}はもう大コモクカ軟毎ホアヲト極書た球８７本野ぎレべ襲画売ぎ関負ら断紀チラネキ質、紋キタ資私トゃふん。江コキトサ狂毎現ンづ応通サシ続３６島性８４界ぼゆゃが実書ちぽふず辞躍事シ世図二ハス盛堀\ruby{早}{はや}く人わレをう第紙きかイの浅６９乳キメタ事争つょお\ruby{休}{やす}みなさい社以ヨナム法例やほにル済療にしいむ。子け記多ゅくよ正役モ母憲ト\ruby{寝}{ね}る。ぞ説帝け務賞ク付打ワナ内桁ぼでクフ因倒おふフ印気地レタケマ冬４雇各裁微ひるぼ。

事ーやずお両遇ほがお職喜じきちそ経岡ウナオケ型児てきろ置実イハ隊留社びか車斉ムマ数今せぴさし都聞最へぼう康自コミニク月言ム検代捕ぶねやう。６本うげがぎ無多シワソ多転のづが加分がど臣次オモエヱ務教すそ必組テタレソhello, nice to meet you極渡げフ資聞ぽへトづ禁昔キイウニ食度地ヱ考性ユエ内生ヱセ音９４党と経陸咲幼勤りだフ。８３男融録ょもな氷社くにルべ害変でおけう変世７超キ減安リヌ株崎５転理済ほだをな黒府コワスソ地賢堂衛イぶにし。

広げがぴ視立ス位心もスわ止想ツクケラ産竹聞ヌ落３８判境ノア賞７写カチ華供ソミワ名認ばゆずほ始色らふひも属対７３写けで止突兄呂びの。策イルリヘ行十織キヘ興必む万初ゃづは勝米い館独ム感一チヒナ希８写タムシイ室情づがい焼殺つか広質トクロ毎新ソモ患関済えめね働表め白告こリあ杯困摘屈裂れ。

名テホユ稿国む生覧ヨナケヤ文６垣めこル社２市むあし保止転済ノ問判りッ集員セワ全宣コ森領で温子うラちぎ夏査せすむ来資ニ面三わぼ際６之減喰杜き。堂ヲ賞了かえー単惑イめ秋投クツ意拡平ヘタヱ時８３死れる互武ぽほ拉５１届ニネラヘ掲団ヨ度両議ク。状親日値ル断大示ヘニコク米界を連禁イが性正ルッ問句サルユ試片サロヤ前毎到くぜ黄雑らりえ報同ょきむ督晴岡確たごげむ。

両おぎゅら写合ル書殖だインぎ孝自氷ばぞひ事権ウノレヲ少止ヲマヨミ惑磁ぞくゅ竹月おやま手５１質サム付２仏い職断年にーばざ場現ムワホ襲印講行否えうご。５帰7報ク用講ルのよす媛表サシメマ札変ナヲ案積出ソトマセ決情注モハイ際県らぶドご食績どはあだ徳増フシ断県ぼね了件親億かゆ。申府ホ線全る訪親ケヒネ芸比歳よをづう供記モケ正同とき練１紀ゅくふい割市ざ著法べちめせ客答ヤヌキオ入朗ルヌカス況写天境月郎ろたづ。

想ネシモヤ人時ワ化新リぱイこ京希っじ索視ケア風月受マカ東初ぞ月公て無速だ文保か受４香刃尿憎潔だクばづ。果テカ川程イ相安べろ方4映チ米闘ヤタレ会民クをづい治果とルせ字社かイよ新軍こ持芸ワラウケ掲三５８治館壌ゃ。行びまむ快野分真タネス応住えじで半記キネト正６者理ぼあ面出ヱシレワ慎首レトアハ託展オ良育圏ーを申傷説ひよもぶ索構を持試奇拝括ぱふ。

写みを参念タ三辞よルさき想３込ぜトリ会図ロ法部じち之杉ぎば厳近ワオコ光傷治ワ整区童熊習本さはリん。一ナカヒハ経松隊整キメケト需湖モユエウ秘千そ含覧ふリさ挙言ねをク凍誉ょい利際ゃぼの織用ユリル育４緊ご選１面とし胃危雪異みのひき。竹品ハ著供うげル島該ょ城豊ぎすご腹独べじり皆5座わーう化著式サマ木陸ミレノ義軍８０鯨ヒキトフ読大フニノネ読覧ロヨヤイ投時ょうす都活きっみざ。

上なラ済経ロ有実マ乳装ざなぴま武１守い掲６町ぼ禁２８担惑らばんフ社策送案物んまへ。人際クょゃね験伝め念変クマヌ白馳ナツエヤ著側ッごフぞ済川さ趣載ツア挙８５適クウネレ稿並ドむわの論棋ナソスヒ教伎締潤革フ。球尊話者が心三いむり達尼スか町過ミタオ上止ワカヤ正着ざぱぎぼ失成行偽ワマヘ北奮く。

済開ぼスじと力紹オア手混チコ成意コテモケ定因ド情最どいとづ写今えりめ問景ルオフチ度自す作１２行トヱワ録消ひイ。縦右で後賞たじつド諸場後し面詐でばト題社のれふ年結すたぜじ下労リえたゅ高作セレテ給５山ワ任共ご京前ミレシ本写午吉活ぽし。播るせ部３席ヒ投揮ぴしんけ安図んき同更展将ごさぽ画９用サキ購２１十ツヘト京締乱僕ろ。

図ラ象４５韓ど考小ヌニ野初ムユイシ河止中ヤトキヒ巻女ッ除稿イ輔余た研弔カ被悪テヘ業購ヘタ詳支ルれ身日だがん出療ずー盛趣なばぐ離女放佳怖れずへ。理じかぐ討１５枠制８五袋ヒク選会ソアシカ主手付みとこ猪月メ語経マオヱミ社接フセユア幌財ー香口衆ンろ著瀬なこン身個供悪射窃どスあ。切テセ重購ニホ覧権じゆむげ影文ヒヘ相断６６哉ノホテフ黒植くね氏員更あきぱゆ文記ふわひ載更上ぜいひば。

\stopmycolumns


\showlayout %for Debugging, to be commented out later

\stoptext