diff options
Diffstat (limited to 'Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx')
-rw-r--r-- | Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx | 426 |
1 files changed, 369 insertions, 57 deletions
diff --git a/Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx b/Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx index 7afac4e5e5c..b9a14299694 100644 --- a/Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx +++ b/Master/texmf-dist/source/latex/xpinyin/xpinyin.dtx @@ -28,10 +28,12 @@ This work has the LPPL maintenance status "maintained". The Current Maintainer of this work is Qing Lee. This package consists of the file xpinyin.dtx, - xpinyin-map.cfg, and the derived files xpinyin.sty, xpinyin.pdf, - xpinyin.ins and + xpinyin.ins, + xpinyin.lua, + xpinyin.db, + xpinyin-database.def and README (this file). Author @@ -63,10 +65,8 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). %<*internal> \fi \begingroup - \edef\tempa{\fmtname} - \edef\tempb{plain} -\expandafter\endgroup -\ifx\tempa\tempb + \def\temp{LaTeX2e} +\expandafter\endgroup\ifx\temp\fmtname\else \csname fi\endcsname %</internal> %<*install> @@ -74,6 +74,9 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \input l3docstrip.tex \keepsilent \askforoverwritefalse + +\let\MetaPrefix\relax + \preamble Copyright (C) 2012-2014 by Qing Lee <sobenlee@gmail.com> @@ -90,25 +93,49 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). The Current Maintainer of this work is Qing Lee. \endpreamble + \postamble This package consists of the file xpinyin.dtx, - xpinyin-map.cfg, and the derived files xpinyin.sty, xpinyin.pdf, - xpinyin.ins and + xpinyin.ins, + xpinyin.lua, + xpinyin.db, + xpinyin-database.def and README. \endpostamble -\generate{ - \usedir{source/latex/xpinyin} - \file{\jobname.ins}{\from{\jobname.dtx}{install}} - \usedir{tex/latex/xpinyin} - \file{\jobname.sty}{\from{\jobname.dtx}{package}} - \nopreamble\nopostamble - \usedir{doc/latex/xpinyin} - \file{README.txt}{\from{\jobname.dtx}{readme}} -} +\declarepostamble\emptypostamble +\endpostamble + +\def\MetaPrefix{-- } + +\generate + { + \usedir{source/latex/xpinyin} + \usepostamble\emptypostamble + \file{xpinyin.lua} {\from{\jobname.dtx}{lua}} + } + +\let\MetaPrefix\DoubleperCent + +\generate + { + \usedir{source/latex/xpinyin} + \file{xpinyin.ins} {\from{\jobname.dtx}{install}} + \usedir{tex/latex/xpinyin} + \file{xpinyin.sty} {\from{\jobname.dtx}{package}} + \usepostamble\emptypostamble + \file{xpinyin-database.def} + { + \from{\jobname.dtx} {database} + \from{xpinyin.db} {} + } + \nopreamble\nopostamble + \usedir{doc/latex/xpinyin} + \file{README.txt} {\from{\jobname.dtx}{readme}} + } \endbatchfile %</install> @@ -119,17 +146,19 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). %<*driver|package> \NeedsTeXFormat{LaTeX2e} \RequirePackage{expl3} -\GetIdInfo$Id: xpinyin.dtx 654 2014-05-12 15:23:16Z sobenlee $ +\GetIdInfo$Id: xpinyin.dtx 700 2014-06-25 10:54:28Z sobenlee $ {Automatically add pinyin to Chinese characters} %<*driver> \ProvidesExplFile{\ExplFileName.\ExplFileExtension} %</driver> %<package>\ProvidesExplPackage{\ExplFileName} - {\ExplFileDate}{1.8}{\ExplFileDescription} + {\ExplFileDate}{1.9}{\ExplFileDescription} %<*driver> \ExplSyntaxOff %</driver> %</driver|package> +%<database>\ProvidesFile{xpinyin-database.def}% +%<database> [2014/06/24 v1.9 xpinyin database] % %<*driver> \documentclass[full,a4paper]{l3doc} @@ -143,26 +172,79 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \BeforeBeginEnvironment{SideBySideExample}{\vskip1ex\relax} \fvset{formatcom=\xeCJKVerbAddon} \linespread{1.1} -\setmainfont[Ligatures=TeX]{TeX Gyre Pagella} -\setmonofont{CMU Typewriter Text} +\setmainfont{TeX Gyre Pagella} +\setsansfont{CMU Sans Serif} +\setmonofont[ + UprightFont=* Light, BoldFont=* Bold, + SlantedFont=* Light Oblique]{CMU Typewriter Text} \setCJKmainfont[BoldFont=Adobe Heiti Std,ItalicFont=Adobe Kaiti Std]{Adobe Song Std} \setCJKmonofont{Adobe Kaiti Std} \xeCJKsetup{PunctStyle=kaiming} \newfontfamily\PinYinFont{TeX Gyre Adventor} \xpinyinsetup{font=\PinYinFont,multiple=\color{red}} \def\MacroFont{\linespread{1}\small\normalfont\ttfamily} +\makeatletter \ExplSyntaxOn \DeclareDocumentCommand \package { o m } { - \href + \exp_args:Nx \tex_lowercase:D + { + \exp_not:N \href + { http \token_to_str:N : //www.ctan.org/pkg/ \IfNoValueTF {#1} {#2} {#1} } + } { \pkg{#2} } + } +\cs_set_protected_nopar:Npn \__codedoc_special_main_index:n #1 + { + \tl_if_empty:NTF \l__codedoc_function_name_prefix_tl + { \SpecialOptionIndex } + { \SpecialUsageIndex } + {#1} + } +\cs_new_eq:NN \@@_special_main_index:n \SpecialMainIndex +\group_begin: + \char_set_lccode:nn { `\/ } { `\\ } + \char_set_lccode:nn { `\L } { `\L } + \char_set_lccode:nn { `\T } { `\T } + \char_set_lccode:nn { `\X } { `\X } +\tex_lowercase:D + { + \group_end: + \DeclareDocumentCommand \SpecialMainIndex { m } { - http://mirrors.ctan.org/help/Catalogue/entries/ - \IfNoValueTF {#1} { \tl_expandable_lowercase:n {#2} } {#1} .html + \tl_if_head_eq_meaning:nNTF {#1} / + { \@@_special_main_index:n } + { \@@_special_main_option_index:n } + {#1} + } + \cs_new_protected:Npn \@@_special_main_option_index:n #1 + { + \@bsphack + \group_begin: + \special@index + { + #1 \actualchar { /ttfamily ~ #1 } ~ (option) + \encapchar main + } + \special@index + { + options: \levelchar #1 \actualchar { /ttfamily ~ #1 } + \encapchar main + } + \group_end: + \@esphack + } + \DeclareDocumentCommand \tn { m } + { + { \ttfamily /#1 } + \SortIndex + { TeX~and~LaTeX2e~commands } + { + /TeX{} ~ and ~ /LaTeXe{} ~ commands: \levelchar + #1 \actualchar { /ttfamily /bslash ~ #1 } + } } - { \pkg {#2} } } \ExplSyntaxOff -\makeatletter \def\LaTeX{\hologo{LaTeX}} \def\pdfLaTeX{\hologo{pdfLaTeX}} \def\XeLaTeX{\hologo{XeLaTeX}} @@ -187,7 +269,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). %</driver> % \fi % -% \CheckSum{670} +% \CheckSum{692} % \GetFileInfo{\jobname.dtx} % % \title{\bfseries\pkg{xpinyin} 宏包} @@ -302,7 +384,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). % \begin{syntax} % \cs{setpinyin} \Arg{汉字} \Arg{拼音} % \end{syntax} -% \pkg{xpinyin} 宏包的拼音数据(\file{xpinyin-map.cfg})来源于 \texttt{Unicode 6.1.0} +% \pkg{xpinyin} 宏包的拼音数据(\file{xpinyin-database.def})来源于 \texttt{Unicode 7.0.0} % 的 \texttt{Unihan} 数据库\footnotemark 中的 \file{Unihan_Readings.txt} 文件。对于多 % 音字,一般来说这个文件选用的是常用读音。可以使用 \cs{setpinyin} 来设置多音字的首选读音。 % \end{function} @@ -423,9 +505,10 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). { ā }{ \= a } , { á }{ \' a } , { ǎ }{ \v a } , { à }{ \` a } , { ō }{ \= o } , { ó }{ \' o } , { ǒ }{ \v o } , { ò }{ \` o } , { ē }{ \= e } , { é }{ \' e } , { ě }{ \v e } , { è }{ \` e } , - { ī }{ \=\i } , { í }{ \'\i } , { ǐ }{ \v\i } , { ì }{ \`\i } , { ū }{ \= u } , { ú }{ \' u } , { ǔ }{ \v u } , { ù }{ \` u } , { ḿ }{ \' m } , { ń }{ \' n } , { ň }{ \v n } , { ǹ }{ \` n } , + { ī }{ \= { \i } } , { í }{ \' { \i } } , + { ǐ }{ \v { \i } } , { ì }{ \` { \i } } , { ü }{ \" u } , { ǖ }{ \= { \" u } } , { ǘ }{ \' { \" u } } , { ǚ }{ \v { \" u } } , { ǜ }{ \` { \" u } } @@ -440,7 +523,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \cs_new_protected_nopar:Npn \@@_UTF_char:nn #1#2 { \cs_if_exist:cF { u8:#1 } - { \cs_new_protected_nopar:cpn { u8:#1 } {#2} } + { \tl_const:cn { u8:#1 } {#2} } } % \end{macrocode} % \end{macro} @@ -487,15 +570,15 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). % \begin{macro}[internal]{\@@_save_UTF_cs:Nn} % \begin{macrocode} \group_begin: -\char_set_catcode_active:n { \c_zero } +\char_set_catcode_active:n { 126 } \cs_new_protected_nopar:Npn \@@_save_UTF_cs:Nn #1#2 { \group_begin: - \char_set_lccode:nn { \c_zero } {#2} - \tl_to_lowercase:n + \char_set_lccode:nn { 126 } {#2} + \tex_lowercase:D { \group_end: - \tl_gput_right:Nn \c_@@_reset_UTF_cs_tl { \cs_set_eq:NN ^^00 #1 } + \tl_gput_right:Nn \c_@@_reset_UTF_cs_tl { \cs_set_eq:NN ~ #1 } } } \group_end: @@ -550,12 +633,12 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). % \end{macrocode} % \end{macro} % -% \begin{macro}[internal]{\@@_make_box:nn} +% \begin{macro}[internal]{\@@_width:Nn} % \begin{macrocode} -\cs_new_protected_nopar:Npn \@@_make_box:nn #1#2 +\cs_new_protected:Npn \@@_width:Nn #1#2 { - \@@_make_pinyin_box:xnn { \@@_to_unicode:n {#1} } {#1} {#2} - \@@_save_CJKsymbol:n {#1} + \hbox_set:Nn \l_@@_tmpa_box {#2} + #1 = \box_wd:N \l_@@_tmpa_box } % \end{macrocode} % \end{macro} @@ -571,8 +654,8 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). { \@@_CJKsymbol_hook: \@@_save_CJKsymbol:n {#2} } \hbox_set:Nn \l_@@_tmpb_box { - \l_@@_pinyin_box_hook_tl \color_group_begin: \color_ensure_current: + \l_@@_pinyin_box_hook_tl \@@_select_font: \clist_if_exist:cTF { c_@@_multiple_ #1 _clist } { \l_@@_multiple_tl \l_@@_format_tl } @@ -596,6 +679,11 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). } } \tl_new:N \l_@@_pinyin_box_hook_tl +\pdftex_if_engine:T + { + \tl_put_right:Nn \l_@@_pinyin_box_hook_tl + { \cs_set_eq:NN \CJK@plane \tex_undefined:D } + } \cs_generate_variant:Nn \@@_make_pinyin_box:nnn { x } % \end{macrocode} % \end{macro} @@ -640,7 +728,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \group_begin: \keys_set:nn { xpinyin } {#2} \bool_if:NF \l_@@_enable_bool - { \settowidth \l_@@_CJKglue_dim { \CJKglue } } + { \@@_width:Nn \l_@@_CJKglue_dim { \CJKglue } } \leavevmode \@@_single_aux:nn {#3} } @@ -672,7 +760,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \cs_set_eq:NN \@@_save_CJKglue: \CJKglue \cs_set_eq:NN \CJKglue \@@_CJKglue: } - \settowidth \l_@@_CJKglue_dim { \CJKglue } + \@@_width:Nn \l_@@_CJKglue_dim { \CJKglue } \@@_replace_CJKsymbol: \@@_restore_footnote: \bool_set_true:N \l_@@_enable_bool @@ -739,7 +827,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). } \cs_new_protected_nopar:Npn \@@_single_CJKsymbol:nn #1#2 { - \@@_make_pinyin_box:nnn + \@@_make_pinyin_box:xnn { \@@_to_unicode:n {#1} } {#1} { \@@_pinyin:n {#2} } \@@_save_CJKsymbol:n {#1} } @@ -788,6 +876,7 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \fontsize { \l_@@_ratio_tl \etex_dimexpr:D \f@size pt \scan_stop: } { \f@baselineskip } + \normalfont \l_@@_font_tl \selectfont } @@ -927,6 +1016,8 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). \int_compare:nNnTF { \CJKpunct@punctstyle } = { \CJKpunct@ps@plain } { \@@_replace_CJKsymbol_single_aux:n { ##1 } } { + \bool_if:NF \l_@@_enable_bool + { \cs_set_eq:NN \@@_save_CJKsymbol:n \CJKosymbol } \cs_set_protected_nopar:Npn \CJKosymbol ####1 { \@@_single_CJKsymbol:nn { ####1 } { ##1 } } } @@ -1093,10 +1184,10 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). % \end{macrocode} % \end{macro} % -% \begin{macro}{ratio,vsep,hsep,pysep,font,format,multiple} +% \begin{macro}{ratio,vsep,hsep,pysep,font,format,multiple,footnote} % \begin{macrocode} \clist_map_inline:nn - { ratio , vsep , hsep , pysep , font , format , multiple , footnote } + { ratio , vsep , hsep , pysep , font , format , multiple } { \keys_define:nn { xpinyin } { #1 .tl_set:c = { l_@@_ #1 _tl } } } \keys_define:nn { xpinyin } { footnote .bool_set:N = \l_@@_footnote_bool } @@ -1110,23 +1201,22 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). % \end{macrocode} % \end{macro} % -% \begin{macro}[internal]{xpinyin-map.cfg} +% \begin{macro}[internal]{\xpinyin_customary:nnn,\xpinyin_multiple:nnn} +% \begin{macrocode} +\cs_new_protected_nopar:Npn \xpinyin_customary:nnn #1#2#3 + { \cs_gset_nopar:cpn { c_@@_ #2 _tl } {#3} } +\cs_new_protected_nopar:Npn \xpinyin_multiple:nnn #1#2#3 + { \cs_gset_nopar:cpn { c_@@_multiple_ #2 _clist } {#3} } +% \end{macrocode} +% \end{macro} +% % \begin{macrocode} \group_begin: -\char_set_catcode_active:N \U -\char_set_catcode_active:N \V -\cs_set_nopar:Npn U+ #1 ~ #2 ~ - { \tl_gset:cn { c_@@_ #1 _tl } {#2} } -\cs_set_nopar:Npn V+ #1 ~ #2 ~ - { \clist_gset:cn { c_@@_multiple_ #1 _clist } {#2} } -\use:n - { - \char_set_catcode_space:N \ - \file_input:n { xpinyin-map.cfg } - } + \cs_set_eq:NN \XPYU \xpinyin_customary:nnn + \cs_set_eq:NN \XPYUM \xpinyin_multiple:nnn + \file_input:n { xpinyin-database.def } \group_end: % \end{macrocode} -% \end{macro} % % \begin{macro}{\setpinyin} % \begin{macrocode} @@ -1147,6 +1237,228 @@ and some specific Chinese Simplified fonts (TrueType or OpenType). %</package> % \end{macrocode} % +% \section{\pkg{xpinyin.lua}} +% +% \begin{macrocode} +%<*lua> +% \end{macrocode} +% +% \begin{macrocode} +xpinyin = xpinyin or { } +local xpinyin = xpinyin +% \end{macrocode} +% +% 计算时区\footnote{\url{http://lua-users.org/wiki/TimeZone}}。 +% \begin{macrocode} +xpinyin.tzoffset = "+0000" +do + -- Compute the difference in seconds between local time and UTC. + local function get_timezone() + local now = os.time() + return os.difftime(now, os.time(os.date("!*t", now))) + end + -- Return a timezone string in ISO 8601:2000 standard form (+hhmm or -hhmm) + local function get_tzoffset(timezone) + local h, m = math.modf(timezone / 3600) + return string.format("%+.4d", 100 * h + 60 * m) + end + xpinyin.tzoffset = get_tzoffset(get_timezone()) +end +% \end{macrocode} +% +% \begin{macrocode} +xpinyin = { + svnid = "$Id: xpinyin.dtx 700 2014-06-25 10:54:28Z sobenlee $", + uchar = unicode.utf8.char, + readings = { }, + fixreadings = { +% \end{macrocode} +% 为汉字“〇”增加拼音。 +% \begin{macrocode} + {"U+3007", "Mandarin", "líng"}, +% \end{macrocode} +% 修改汉字“女”的错误拼音。 +% \begin{macrocode} + {"U+5973", "HanyuPinlu", "nǚ(1129) rǔ(37)"} + }, + database = { + file = "Unihan_Readings.txt", + date = "Date: 2014-05-09 18:17:02 GMT [JHJ]", + version = "Unicode version: 7.0.0", + dbfile = "xpinyin.db" + }, +% \end{macrocode} +% \pkg{DocStrip} 会将一行开头的 |%%| 替换成 \cs{MetaPrefix},因此我们在行首加了 +% 空格,需要把它去掉。 +% \begin{macrocode} + preamble = string.gsub([[ + %% + %% Do not edit this file! + %% Created from Unihan database: + %% + %% $file + %% $date + %% $version + %% + %% by "]] .. arg[-1] .. " " .. arg[0] .. [[" on ]] + .. os.date("%Y-%m-%d %X ") .. xpinyin.tzoffset + .. "\n%%", "[ ]+(%%%%)", "%1") +} +% \end{macrocode} +% +% 将 \file{Unihan_Readings.txt}^^A +% \footnote{\url{http://http://www.unicode.org/reports/tr38/}。} 保存到一张表里面。 +% \begin{macrocode} +function xpinyin.maketable (txt) + local f = assert(io.open(txt or xpinyin.database.file, "r")) + local s, prop + for line in f:lines() do + s = line:explode("\t") + if #s == 3 then + prop = s[2]:sub(2) + if prop == "Mandarin" or + prop == "HanyuPinyin" or + prop == "XHC1983" or + prop == "HanyuPinlu" then + xpinyin.insert(s[1], prop, s[3]) + end + elseif line:find("Date") then + xpinyin.database.date = line:match("^[#%s]*(.*)") + elseif line:find("Unicode version:") then + xpinyin.database.version = line:match("^[#%s]*(.*)") + end + end + f:close() + if xpinyin.fixreadings then + for _, s in pairs(xpinyin.fixreadings) do + xpinyin.insert(s[1], s[2], s[3]) + end + end +end +% \end{macrocode} +% +% 往拼音表中加入项目。 +% \begin{macrocode} +function xpinyin.insert (unicode, prop, value) + local index = tonumber(unicode:sub(3), 16) + if not xpinyin.readings[index] then + xpinyin.readings[index] = { } + end + xpinyin.readings[index][prop] = value +end +% \end{macrocode} +% +% 输出需要的格式文件。 +% \begin{macrocode} +function xpinyin.output (db) + local f = assert(io.open(db or xpinyin.database.dbfile, "w")) + local preamble = xpinyin.preamble:gsub("%$(%w+)", xpinyin.database) + f:write(preamble, "\n") + local pinyin, code_point, char + local mt = { } + for index, pyt in xpinyin.pairsByKeys(xpinyin.readings) do + pinyin = assert(xpinyin.grep(pyt)) + code_point = string.format("%X", index) + char = xpinyin.uchar(index) + f:write("\\XPYU{", char, "}{", code_point, "}{", pinyin, "}\n") + pinyin = xpinyin.multiple(pyt) + if pinyin then + mt[#mt + 1] = "\\XPYUM{" .. char .. "}{" .. code_point .. "}{" .. pinyin .."}" + end + end + f:write(table.concat(mt, "\n"), "\n") + f:close() +end +% \end{macrocode} +% +% 将表按照索引排序,代码来源于 +% \href{http://www.lua.org/pil/19.3.html}{\textit{Programming in Lua}}。 +% \begin{macrocode} +function xpinyin.pairsByKeys (t, f) + local a = { } + for n in pairs(t) do a[#a + 1] = n end + table.sort(a, f) + local i = 0 -- iterator variable + return function () -- iterator function + i = i + 1 + return a[i], t[a[i]] + end +end +% \end{macrocode} +% +% 按照 \texttt{HanyuPinlu}、\texttt{Mandarin}、\texttt{XHC1983}、\texttt{HanyuPinyin} +% 的顺序选择最常用的拼音。 +% \begin{macrocode} +function xpinyin.grep (pyt) + if pyt.HanyuPinlu then + return pyt.HanyuPinlu:match("[^%(]+"), "HanyuPinlu" + elseif pyt.Mandarin then + return pyt.Mandarin:match("%S+"), "Mandarin" + elseif pyt.XHC1983 then + return pyt.XHC1983:match(":(%S+)"), "XHC1983" + elseif pyt.HanyuPinyin then + return pyt.HanyuPinyin:match(":([^,%s]+)"), "HanyuPinyin" + end +end +% \end{macrocode} +% +% 根据 \texttt{XHC1983} 和 \texttt{HanyuPinyin} 选出多音字。 +% \begin{macrocode} +function xpinyin.multiple (pyt) + if pyt.XHC1983 then + local s = pyt.XHC1983:explode() + if s[2] then + local t = { } + for i, v in ipairs(s) do + t[#t + 1] = v:explode(":")[2] + end + return xpinyin.unique(t), "XHC1983" + end + elseif pyt.HanyuPinyin and pyt.HanyuPinyin:find("%D,") then + local t = { } + for _, v in ipairs(pyt.HanyuPinyin:explode()) do + for _, py in ipairs(v:explode(":")[2]:explode(",")) do + t[#t + 1] = py + end + end + return xpinyin.unique(t), "HanyuPinyin" + end +end +% \end{macrocode} +% +% 删除掉数组中的重复元素。 +% \begin{macrocode} +function xpinyin.unique (t) + local rt = xpinyin.remove_duplicate(t) + if #rt > 1 then + return table.concat(rt, ",") + end +end +function xpinyin.remove_duplicate (t) + local ht = { } + local nt = { } + for i, v in ipairs(t) do + if not ht[v] then + nt[#nt + 1] = v + ht[v] = true + end + end + return nt +end +% \end{macrocode} +% +% \begin{macrocode} +xpinyin.maketable() +% \end{macrocode} +% +% \begin{macrocode} +xpinyin.output() +% \end{macrocode} +% +% \begin{macrocode} +%</lua> +% \end{macrocode} +% % \end{implementation} % % \Finale |