On Friday 24 June 2016 14:10:51 Pali Rohár wrote:
> On Friday 24 June 2016 14:04:13 Hans Hagen wrote:
> > the manual: 1.3.3 Example virtual font
> 
> Thank you, going to look at it!

Now I have something working. If somebody is interesting see attachment. 
PDF output from both pdftex and luatex (on pdfcsplain format) is exactly 
same.

Hans & Mojca, it is correct way? Will hyphenation works correctly?

(Sorry for long lua code, I'm new to lua, it is quick & dirty solution, 
code deduplication could be possible.)

-- 
Pali Rohár
[email protected]
% fix-csfonts.tex - Fix loading csfonts in pdfTeX and LuaTeX
% (c) 2016 Pali
%
% Usage:
% %&pdfcsplain
% \input fix-csfonts
% \uv{áäčďěíĺľňóôŕřšťúýž}
% ffi ffl fi fl $\delta$ $\Delta$
% \bye

% font hook for loading csfonts in luatex
\ifx\directlua\undefined\else
	\directlua{
		% mapping table: unicode --> csfont
		local csbase = {
			[915] = 0,
			[916] = 1,
			[920] = 2,
			[923] = 3,
			[926] = 4,
			[928] = 5,
			[931] = 6,
			[978] = 7,
			[933] = 7,
			[934] = 8,
			[936] = 9,
			[937] = 10,
			[305] = 16,
			[567] = 17,
			[96] = 18,
			[768] = 18,
			[180] = 19,
			[769] = 19,
			[711] = 20,
			[780] = 20,
			[728] = 21,
			[774] = 21,
			[175] = 22,
			[772] = 22,
			[730] = 23,
			[778] = 23,
			[184] = 24,
			[807] = 24,
			[223] = 25,
			[230] = 26,
			[339] = 27,
			[248] = 28,
			[198] = 29,
			[338] = 30,
			[216] = 31,
			[33] = 33,
			[8221] = 34,
			[35] = 35,
			[37] = 37,
			[38] = 38,
			[8217] = 39,
			[40] = 40,
			[41] = 41,
			[42] = 42,
			[43] = 43,
			[44] = 44,
			[45] = 45,
			[46] = 46,
			[47] = 47,
			[48] = 48,
			[49] = 49,
			[50] = 50,
			[51] = 51,
			[52] = 52,
			[53] = 53,
			[54] = 54,
			[55] = 55,
			[56] = 56,
			[57] = 57,
			[58] = 58,
			[59] = 59,
			[61] = 61,
			[63] = 63,
			[64] = 64,
			[65] = 65,
			[66] = 66,
			[67] = 67,
			[68] = 68,
			[69] = 69,
			[70] = 70,
			[71] = 71,
			[72] = 72,
			[73] = 73,
			[74] = 74,
			[75] = 75,
			[76] = 76,
			[77] = 77,
			[78] = 78,
			[79] = 79,
			[80] = 80,
			[81] = 81,
			[82] = 82,
			[83] = 83,
			[84] = 84,
			[85] = 85,
			[86] = 86,
			[87] = 87,
			[88] = 88,
			[89] = 89,
			[90] = 90,
			[91] = 91,
			[93] = 93,
			[94] = 94,
			[770] = 94,
			[8216] = 96,
			[97] = 97,
			[98] = 98,
			[99] = 99,
			[100] = 100,
			[101] = 101,
			[102] = 102,
			[103] = 103,
			[104] = 104,
			[105] = 105,
			[106] = 106,
			[107] = 107,
			[108] = 108,
			[109] = 109,
			[110] = 110,
			[111] = 111,
			[112] = 112,
			[113] = 113,
			[114] = 114,
			[115] = 115,
			[116] = 116,
			[117] = 117,
			[118] = 118,
			[119] = 119,
			[120] = 120,
			[121] = 121,
			[122] = 122,
			[732] = 126,
			[771] = 126,
			[126] = 126,
			[168] = 127,
			[776] = 127,
			[8240] = 141,
			[192] = 152,
			[45] = 156,
			[731] = 157,
			[808] = 157,
			[171] = 158,
			[187] = 159,
			[317] = 165,
			[352] = 169,
			[356] = 171,
			[381] = 174,
			[318] = 181,
			[224] = 184,
			[353] = 185,
			[357] = 187,
			[382] = 190,
			[340] = 192,
			[193] = 193,
			[196] = 196,
			[313] = 197,
			[268] = 200,
			[201] = 201,
			[282] = 204,
			[205] = 205,
			[270] = 207,
			[327] = 210,
			[211] = 211,
			[212] = 212,
			[214] = 214,
			[344] = 216,
			[366] = 217,
			[218] = 218,
			[220] = 220,
			[221] = 221,
			[341] = 224,
			[225] = 225,
			[228] = 228,
			[314] = 229,
			[269] = 232,
			[233] = 233,
			[283] = 236,
			[237] = 237,
			[271] = 239,
			[328] = 242,
			[243] = 243,
			[244] = 244,
			[246] = 246,
			[345] = 248,
			[367] = 249,
			[250] = 250,
			[252] = 252,
			[253] = 253,
			[8222] = 254,
			[8220] = 255,
		}

		local csdollarmod = {
			[36] = 36,
		}
		local cssterlingmod = { % csbxti csfi csitt csu csti
			[163] = 36,
		}

		local csdashmod = {
			[8211] = 123,
			[8212] = 124,
			[733] = 125,
			[779] = 125,
		}
		local csbracemod = { % csitt cssltt cstcsc cstt csvtt
			[123] = 123,
			[124] = 124,
			[125] = 125,
		}

		local csligmod = {
			[64256] = 11,
			[64257] = 12,
			[64258] = 13,
			[64259] = 14,
			[64260] = 15,
			[823] = 32,
			[161] = 60,
			[191] = 62,
			[729] = 95,
			[775] = 95,
		}
		local csarrowmod = { % cscsc csitt csr5 cssltt cstcsc cstt csvtt
			[8593] = 11,
			[8595] = 12,
			[39] = 13,
			[161] = 14,
			[191] = 15,
			[9251] = 32,
			[60] = 60,
			[62] = 62,
			[92] = 92,
			[95] = 95,
		}

		local csfonts = {
			'csb5', 'csb6', 'csb7', 'csb8', 'csb9', 'csb10', 'csb12', 'csb17',
			'csbx5', 'csbx6', 'csbx7', 'csbx8', 'csbx9', 'csbx10', 'csbx12',
			'csbxsl5', 'csbxsl6', 'csbxsl7', 'csbxsl8', 'csbxsl9', 'csbxsl10', 'csbxsl12',
			'csbxti10', 'csbxti12', 'csbxti17',
			'cscsc8', 'cscsc9', 'cscsc10', 'cscsc12', 'cscsc17',
			'csdunh5', 'csdunh6', 'csdunh7', 'csdunh8', 'csdunh9', 'csdunh10', 'csdunh12', 'csdunh17',
			'csff10',
			'csfi10',
			'csfib8', 'csfib9', 'csfib10', 'csfib12',
			'csinch',
			'csitt8', 'csitt9', 'csitt10', 'csitt12', 'csitt17',
			'csr5', 'csr6', 'csr7', 'csr8', 'csr9', 'csr10', 'csr12', 'csr17',
			'cssl5', 'cssl6', 'cssl7', 'cssl8', 'cssl9', 'cssl10', 'cssl12', 'cssl17',
			'cssltt8', 'cssltt9', 'cssltt10', 'cssltt12',
			'csssbx9', 'csssbx10', 'csssbx12', 'csssbx17',
			'csssdc10',
			'csss8', 'csss9', 'csss10', 'csss12', 'csss17',
			'csssi8', 'csssi9', 'csssi10', 'csssi12', 'csssi17',
			'csssq8',
			'csssqi8',
			'cstcsc10', 'cstcsc12', 'cstcsc17',
			'csti7', 'csti8', 'csti9', 'csti10', 'csti12', 'csti17',
			'cstt8', 'cstt9', 'cstt10', 'cstt12',
			'csu7', 'csu8', 'csu9', 'csu10', 'csu12', 'csu17',
			'csvtt8', 'csvtt9', 'csvtt10', 'csvtt12',
			'icscsc10',
			'icstt8',
			'ilcsss8',
			'ilcsssb8',
			'ilcsssi8',
			'lcsss8',
			'lcsssb8',
			'lcsssi8',
		}

		local processed = {}
		callback.register('define_font',
			function(name, size)
				local f = font.read_tfm(name, size)

				if processed[name] then return f end
				processed[name] = true

				local found = false
				for _, csfont in pairs(csfonts) do
					if csfont == name then
						found = true
						break
					end
				end
				if not found then return f end

				f.name = name .. '-uni'
				f.type = 'virtual'
				f.fonts = {{ name = name, size = size }}

				local characters = {}
				for uni, cs in pairs(csbase) do
					if f.characters[cs] then
						characters[uni] = {}
						for key, val in pairs(f.characters[cs]) do
							characters[uni][key] = val
						end
						characters[uni].commands = {{ 'char', cs }}
					end
				end

				local found = false
				local sterlingnames = { 'csbxti', 'csfi', 'csitt', 'csu', 'csti' }
				for _, sterlingname in pairs(sterlingnames) do
					if string.match(name, sterlingname .. '\%d+') then
						found = true
						break
					end
				end
				local table
				if found then table = cssterlingmod else table = csdollarmod end
				for uni, cs in pairs(table) do
					if f.characters[cs] then
						characters[uni] = {}
						for key, val in pairs(f.characters[cs]) do
							characters[uni][key] = val
						end
						characters[uni].commands = {{ 'char', cs }}
					end
				end

				local found = false
				local sterlingnames = { 'csitt', 'cssltt', 'cstcsc', 'cstt', 'csvtt' }
				for _, sterlingname in pairs(sterlingnames) do
					if string.match(name, sterlingname .. '\%d+') then
						found = true
						break
					end
				end
				local table
				if found then table = csbracemod else table = csdashmod end
				for uni, cs in pairs(table) do
					if f.characters[cs] then
						characters[uni] = {}
						for key, val in pairs(f.characters[cs]) do
							characters[uni][key] = val
						end
						characters[uni].commands = {{ 'char', cs }}
					end
				end

				local found = false
				local sterlingnames = { 'cscsc', 'csitt', 'cssltt', 'cstcsc', 'cstt', 'csvtt' }
				for _, sterlingname in pairs(sterlingnames) do
					if string.match(name, sterlingname .. '\%d+') then
						found = true
						break
					end
				end
				if not found and name == 'csr5' then found = true end
				local table
				if found then table = csarrowmod else table = csligmod end
				for uni, cs in pairs(table) do
					if f.characters[cs] then
						characters[uni] = {}
						for key, val in pairs(f.characters[cs]) do
							characters[uni][key] = val
						end
						characters[uni].commands = {{ 'char', cs }}
					end
				end

				for cs = 0, 127 do
					if f.characters[cs] and not characters[cs] then
						characters[cs] = {}
						for key, val in pairs(f.characters[cs]) do
							characters[cs][key] = val
						end
						characters[cs].commands = {{ 'char', cs }}
					end
				end

				f.characters = characters

				return f
			end
		)
	}
\fi

% warn about missing characters in luatex
\ifx\directlua\undefined\else
	\directlua{
		callback.register('ligaturing',
			function (head)
				for t in node.traverse_id(node.id('glyph'), head) do
					if not font.fonts[t.font].characters[t.char] then
						texio.write_nl('Missing character: There is no ' .. unicode.utf8.char(t.char) .. ' in font ' .. font.fonts[t.font].name .. '!')
						texio.write_nl('')
					end
				end
				node.ligaturing(head)
			end
		)
	}
\fi

% enable \pdfglyphtounicode and \pdfgentounicode in luatex
\ifx\directlua\undefined\else
	\directlua{tex.enableprimitives('', { 'pdfglyphtounicode', 'pdfgentounicode', 'pdfextension', 'pdfvariable' })}
	\ifx\pdfglyphtounicode\undefined
		\protected\def\pdfglyphtounicode{\pdfextension glyphtounicode }
	\fi
	\ifx\pdfgentounicode\undefined
		\edef\pdfgentounicode{\pdfvariable gentounicode}
	\fi
\fi

\input glyphtounicode

% fixes for glyphtounicode file
\pdfglyphtounicode{dotlessj}{0237}
\pdfglyphtounicode{Delta}{0394}

% additional glyph names for csfonts
\pdfglyphtounicode{csquotedblright}{201C}
\pdfglyphtounicode{althyphen}{002D}
\pdfglyphtounicode{polishlcross}{0337}
\pdfglyphtounicode{suppress}{0337}

\pdfgentounicode=1

% reload current font in luatex
\ifx\directlua\undefined\else
	\font\f=\fontname\font
	\f\ignorespaces
\fi

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to