summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/generic/hyph-utf8/generate-pattern-loaders.rb
blob: 3c94441229c6d2269149d02c3bb2fe95cd8fe070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ruby

# this file auto-generates loaders for hyphenation patterns - to be improved

load 'languages.rb'

$package_name="hyph-utf8"


# TODO - make this a bit less hard-coded
$path_tex_generic=File.expand_path("../../../tex/generic")
$path_loadhyph="#{$path_tex_generic}/#{$package_name}/loadhyph"

# TODO: should be singleton
languages = Languages.new.list

#text_if_native_utf = "\input pattern-loader.tex\n\\ifNativeUtfEightPatterns"

text_if_native_utf = <<EOT
% Test whether we received one or two arguments
\\def\\testengine#1#2!{\\def\\secondarg{#2}}
% That's Tau (as in Taco or ΤΕΧ, Tau-Epsilon-Chi), a 2-byte UTF-8 character
\\testengine Τ!\\relax
% Unicode-aware engine (such as XeTeX or LuaTeX) only sees a single (2-byte) argument
\\ifx\\secondarg\\empty
EOT

languages.each do |language|
	if language.use_new_loader then
		filename = "#{$path_loadhyph}/loadhyph-#{language.code}.tex"
		puts "generating '#{filename}'"
		File.open(filename, "w") do |file|
			# a message about auto-generation
			# TODO: write a more comprehensive one
			file.puts "% loadhyph-#{language.code}.tex"
			file.puts "%"
			file.puts "% Autogenerated loader for hyphenation patterns for \"#{language.name}\""
			file.puts "% by source/generic/hyph-utf8/generate-pattern-loaders.rb"
			file.puts "% See also http://tug.org/tex-hyphen"
			file.puts "%"
			file.puts "% Copyright 2008 TeX Users Group."
			file.puts "% You may freely use, modify and/or distribute this file."
			file.puts "% (But consider adapting the scripts if you need modifications.)"
			file.puts "%"
			file.puts "% Once it turns out that more than a simple definition is needed,"
			file.puts "% these lines may be moved to a separate file."
			file.puts "%"

			# message (where it exists)
			# if language.message != nil
			# 	file.puts("\\message{#{language.message}}\n%")
			# end
		
			# TODO:
			# \lefthyphenmin=2 \righthyphenmin=2
			# but probably this needs to reside outside of \begingroup/endgroup
			
			file.puts('\begingroup')
			if language.code == 'af' or language.code == 'it' or language.code == 'fr' or language.code == 'uk' or language.code == 'zh-latn' then
				file.puts("\\lccode`\\'=`\\'")
			end
			if language.code == 'pt' or language.code == 'tk' then
				file.puts("\\lccode`\\-=`\\-")
			end
			
			# some special cases firs
			#
			# some languages (sanskrit) are useless in 8-bit engines; we only want to load them for UTF engines
			# TODO - maybe consider doing something similar for ibycus
			if language.code == 'sa' or
				 language.code == 'as' or
				 language.code == 'bn' or
				 language.code == 'gu' or
				 language.code == 'hi' or
				 language.code == 'hy' or
				 language.code == 'kn' or
				 language.code == 'lo' or
				 language.code == 'ml' or
				 language.code == 'mr' or
				 language.code == 'or' or
				 language.code == 'pa' or
				 language.code == 'ta' or
				 language.code == 'te' then
				file.puts(text_if_native_utf)
				file.puts("    \\message{UTF-8 #{language.message}}")
				if language.code != 'lo' then
					file.puts('    % Set \lccode for ZWNJ and ZWJ.')
					file.puts('    \lccode"200C="200C')
					file.puts('    \lccode"200D="200D')
					if language.code == 'sa' then
						file.puts('    % Set \lccode for KANNADA SIGN JIHVAMULIYA and KANNADA SIGN UPADHMANIYA.')
						file.puts('    \lccode"0CF1="0CF1')
						file.puts('    \lccode"0CF2="0CF2')
					end
				end
				file.puts("    \\input hyph-#{language.code}.tex")
				file.puts('\else')
				file.puts("    \\message{No #{language.message} - only available with Unicode engines}")
				file.puts('    \input zerohyph.tex')
				file.puts('\fi')
			# for ASCII encoding, we don't load any special support files, but simply load everything
			elsif language.encoding == "ascii" then
				file.puts('% ASCII patterns - no additional support is needed')
				# for UK English we simply load the old file without bothering about renaming it
				if language.use_old_patterns then
					file.puts("\\input #{language.filename_old_patterns}")
				# for the rest we load the new file
				else
					file.puts("\\message{ASCII #{language.message}}")
					file.puts("\\input hyph-#{language.code}.tex")
				end
			# when lanugage uses old patterns for 8-bit engines, load two different patterns rather than using the converter
			elsif language.use_old_patterns then
				file.puts(text_if_native_utf)
				file.puts("    \\message{UTF-8 #{language.message}}")
				# some catcodes for XeTeX
				if language.code == 'grc' or language.code.slice(0,2) == 'el' then
					file.puts("    \\lccode`'=`'\\lccode`’=`’\\lccode`ʼ=`ʼ\\lccode`᾽=`᾽\\lccode`᾿=`᾿")
				end
				file.puts("    \\input hyph-#{language.code}.tex")
				# russian and ukrainian exceptions - hacks with dashes
				if language.code == 'ru' or language.code == 'uk' then
					file.puts('    % Additional patterns with hyphen/dash: a hack to prevent breaking after hyphen, but not before.')
					file.puts("    \\input exhyph-#{language.code}.tex")
				end
				file.puts('\else')
				file.puts("    \\message{#{language.message}}")
				# explain why we are still using the old patterns
				if language.use_old_patterns_comment != nil then
					file.puts("    % #{language.use_old_patterns_comment}")
				else
					puts "Missing comment for #{language.name}"
					file.puts('    % we still load old patterns for 8-bit TeX')
				end
				file.puts("    \\input #{language.filename_old_patterns}")
				file.puts('\fi')
			else
				file.puts(text_if_native_utf)
				file.puts("    \\message{UTF-8 #{language.message}}")
				file.puts('\else')
				file.puts("    \\message{#{language.encoding.upcase} #{language.message}}")
				# a hack for OT1 encoding in three languages
				if language.code == 'da' or language.code == 'fr' then
					file.puts("    % A hack to support both EC and OT1 encoding in 8-bit engines.")
					file.puts("    % Kept for backward compatibility only, though we would prefer to drop it.")
					file.puts("    % OT1 encoding is close-to-useless for proper hyphenation.")
					file.puts("    \\input spechyph-ot1-#{language.code}.tex")
				end
				file.puts("    \\input conv-utf8-#{language.encoding}.tex")
				file.puts('\fi')
				if language.code == 'sr-latn' then
					file.puts("% Load Serbo-Croatian patterns")
					file.puts("\\input hyph-sh-latn.tex")
				else
					file.puts("\\input hyph-#{language.code}.tex")
				end
#				file.puts("\\loadhyphpatterns{#{language.code}}{#{language.encoding}}%")
			end
			file.puts('\endgroup')
		end
	end
end