Skip to content

Instantly share code, notes, and snippets.

@zr-tex8r
Created February 5, 2015 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zr-tex8r/b0dbff990ffea068b964 to your computer and use it in GitHub Desktop.
Save zr-tex8r/b0dbff990ffea068b964 to your computer and use it in GitHub Desktop.
LaTeX: Paragraph formatting like “karakasa-renban”
% tckarakasa.sty
%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{tckarakasa}[2015/02/05 v0.1]
%% preparations
\def\tckk@pkgname{tckarakasa}
\def\tckk@error{\PackageError\tckk@pkgname}
\providecommand\bxDebug[1]{}
%% prerequisite
\RequirePackage{etoolbox}
\RequirePackage{graphicx}
%--------------------------------------- helpers
%% variables
\newbool{tckk@g@last@line}
\newbox\tckk@g@orgbox
\newbox\tckk@g@procbox
\newbox\tckk@g@linebox
\let\tckk@line@count\relax
\let\tckk@linewidth\relax
%% unique tokens
\def\tckk@end{\tckk@end@}
\def\tckk@mt{\tckk@mt@}
%--------------------------------------- public stuffs
%%<*> \begin{karakasa}{<width>}...\end{karakasa}
\newenvironment{karakasa}[1]{%
\setlength\@tempdima{#1}%
\edef\tckk@linewidth{\the\@tempdima}%
\global\setbox\tckk@g@orgbox\vbox\bgroup\tate
\tckk@set@box@param
\bgroup
}{%
\egroup\egroup
\tckk@split@lines
\tckk@draw@text
\box\tckk@g@procbox
}
%% \tckk@set@box@param
\def\tckk@set@box@param{%
\hsize\tckk@linewidth\relax
\@parboxrestore
\raggedright
\interlinepenalty\z@
\clubpenalty\z@
\widowpenalty\z@
\displaywidowpenalty\z@
\brokenpenalty\z@
\lineskip2\baselineskip
\baselineskip\z@
\lineskiplimit\maxdimen
}
%--------------------------------------- splitter
%% constants
\chardef\tckk@max@factor=5
%% variables
\let\tckk@factor\relax
\let\tckk@g@retcode\relax
\newbox\tckk@normbox
%% temporaries
\chardef\tckk@boxa=2
\chardef\tckk@boxb=4
\chardef\tckk@boxc=6
%% \tckk@dir
% \tate or \yoko, or just \relax
\let\tckk@dir\tate
%% \tckk@split@lines
% Splits \tckk@g@orgbox into "line chunks" (hbox), arranges
% them inside a vbox in reverse order, and stores the box
% in \tckk@g@procbox. In addition, the number of line chunks
% is stored to \tckk@line@count.
\def\tckk@split@lines{%
\def\tckk@line@count{0}%
\global\setbox\tckk@g@procbox\vbox{\tckk@dir}%
\tckk@split@lines@a
\bxDebug{karakasa: \tckk@line@count\ifnum\tckk@g@retcode=3 +\fi
\space lines}%
}
\def\tckk@split@lines@a{%
\def\tckk@factor{0}%
\tckk@split@once
\ifnum\tckk@g@retcode<3
\numdef\tckk@line@count{\tckk@line@count+1}%
\fi
\global\setbox\tckk@g@procbox\vbox{\tckk@dir
\box\tckk@g@linebox
\unvbox\tckk@g@procbox
}%
\ifnum\tckk@g@retcode<2
\expandafter\tckk@split@lines@a
\fi
}
%% \tckk@split@once
\def\tckk@split@once{%
\numdef\tckk@factor{\tckk@factor+1}%
\ifnum\tckk@factor>\tckk@max@factor
\global\chardef\tckk@g@retcode=4
\def\tckk@line@count{0}%
\global\setbox\tckk@g@procbox\vbox{\tckk@dir}%
\else \expandafter\tckk@split@once@a
\fi
}
\def\tckk@split@once@a{%
\begingroup
\splitmaxdepth\maxdimen
\splittopskip\z@
\vbadness\@M
\setbox\tckk@boxa\copy\tckk@g@orgbox
\setbox\tckk@boxb\vsplit\tckk@boxa to\tckk@factor\baselineskip
\setbox\tckk@boxc\vbox{\tckk@dir
\unvbox\tckk@boxb
\tckk@discard@stuffs
\global\setbox\tckk@g@linebox\lastbox
\tckk@discard@stuffs
}%
{\setbox\tckk@boxb\vbox{\tckk@dir % chenge-dir
\ht\tckk@boxc\z@ \dp\tckk@boxc\z@
}}%
\unless\ifvoid\tckk@g@linebox
\global\setbox\tckk@g@linebox\hbox{\tckk@dir
\box\tckk@boxc
\ht\tckk@g@linebox\z@ \dp\tckk@g@linebox\z@
\box\tckk@g@linebox
}%
\fi
\ifvoid\tckk@boxa
\ifvoid\tckk@g@linebox
\global\chardef\tckk@g@retcode=3
\global\setbox\tckk@g@linebox\hbox{\tckk@dir
\box\tckk@boxc
}%
\else \global\chardef\tckk@g@retcode=2
\fi
\else
\ifvoid\tckk@g@linebox
\global\chardef\tckk@g@retcode=0
\else \global\chardef\tckk@g@retcode=1
\fi
\fi
\ifnum\tckk@g@retcode>0
\global\setbox\tckk@g@orgbox\box\tckk@boxa
\fi
\endgroup
\ifnum\tckk@g@retcode=0
\expandafter\tckk@split@once
\fi
}
%% \tckk@discard@stuffs
\def\tckk@discard@stuffs{%
\let\tckk@next\tckk@discard@stuffs
\ifnum \lastnodetype=11 \unskip
\else\ifnum\lastnodetype=12 \unkern
\else\ifnum\lastnodetype=13 \unpenalty
\else \let\tckk@next\relax
\fi\fi\fi \tckk@next
}
%--------------------------------------- drawing
%% variables
\let\tckk@iradius\relax
\let\tckk@oradius\relax
\let\tckk@phi\relax
\let\tckk@omega\relax
%% \tckk@draw@text
\def\tckk@draw@text{%
\leavevmode
\ifnum\tckk@line@count>0
\@tempdima\tckk@line@count\baselineskip
\dimdef\tckk@iradius{0.159155\@tempdima}%
\dimdef\tckk@oradius{\tckk@iradius+\tckk@linewidth}%
\dimdef\tckk@phi{360pt/\tckk@line@count}%
\dimdef\tckk@omega{0pt}%
\global\setbox\tckk@g@procbox\hbox{%
\kern\tckk@oradius\relax
\tckk@draw@text@a
\kern\tckk@oradius\relax
}%
\ht\tckk@g@procbox\tckk@oradius\relax
\dp\tckk@g@procbox\tckk@oradius\relax
\else
\global\setbox\tckk@g@procbox\hbox{%
\ht\tckk@g@procbox\z@ \dp\tckk@g@procbox\z@
\wd\tckk@g@procbox\z@ \box\tckk@g@procbox
}%
\fi
%\showbox\tckk@g@procbox
}
\def\tckk@draw@text@a{%
\begingroup
\@whilenum{\tckk@line@count>\z@}\do{%
\global\setbox\tckk@g@procbox\vbox{\tate
\unvbox\tckk@g@procbox \unskip
\global\setbox\tckk@g@linebox\lastbox
}%
\edef\tckk@tmpa{\expandafter\rem@pt\tckk@omega}%
\setbox\tckk@boxa\hbox{\hb@xt@\z@{\tate
\kern\tckk@iradius\relax
\box\tckk@g@linebox
\hss
}}%
\rotatebox{\tckk@tmpa}{\box\tckk@boxa}%
\dimdef\tckk@omega{\tckk@omega-\tckk@phi}%
\numdef\tckk@line@count{\tckk@line@count-1}%
}%
\global\setbox\tckk@g@procbox\vbox{\tate
\unvbox\tckk@g@procbox \unskip
\global\setbox\tckk@g@linebox\lastbox
}%
\unless\ifvoid\tckk@g@linebox
\hbox{\hb@xt@\z@{\tate\box\tckk@g@linebox\hss}}%
\fi
\endgroup
}
%--------------------------------------- all done
\endinput
%% EOF
\documentclass[a4paper,dvipdfmx]{jsarticle}
\usepackage{tckarakasa}
\begin{document}
\begin{karakasa}{10zw}
日本国民は、正当に選挙された国会における代表者を通じて行動し、
われらとわれらの子孫のために、諸国民との協和による成果と、
わが国全土にわたつて自由のもたらす恵沢を確保し、
政府の行為によつて再び戦争の惨禍が起ることのないやうにすることを決意し、
ここに主権が国民に存することを宣言し、この憲法を確定する。
そもそも国政は、国民の厳粛な信託によるものであつて、
その権威は国民に由来し、その権力は国民の代表者がこれを行使し、
その福利は国民がこれを享受する。これは人類普遍の原理であり、
この憲法は、かかる原理に基くものである。われらは、
これに反する一切の憲法、法令及び詔勅を排除する。
\end{karakasa}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment