Can I use “old-style” (non-lining) numerals in TCPDF?

心不动则不痛 提交于 2019-12-23 05:49:11

问题


Unicode does not distinguish between lining figures (which have the same proportions as capital letters, and are useful in tables, but stand out in running text) and non-lining figures, which look more like lowercase letters, with ascenders and descenders, as it considers them variants of each other. Many fonts, though, have both sets of digits, and provide a way to choose between them. Is there any way to achieve this in TCPDF?

Ideally, I would like numbered lists to use lining figures, and numbers in running text to use non-lining, and postcodes (British and Irish postcodes are alphanumeric) to use non-lining figures and small block capital numbers. This could be achieved in LaTeX with \oldstylenums{1234567890}.

The only similar question I could find was about tweaking font metrics in TCPDF to make non-lining figures look lining. I would ideally want to use both, from the same font and within the same document. Does TCPDF give access to OpenType character variations? (Fair warning: I’m a little out of my depth here.)


回答1:


Unfortunately, as far as I'm aware, TCPDF does not support OpenType variants. Looking at the font declaration file for freesans for example, the pnum variant glyphs are not even included in the character width array.

However, what you can do is copy the variant glyphs into the Unicode range with font forge and map the characters in the contexts you want to use them. For instance, if you're not using the subscript numeral glyphs, they make good candidates as search appears to still find them in several of the PDF readers I tested.

Just so I could have something to test, I ran a similar scenario: using Free Sans but wanting to use proportional figures in some places with the same font file. I opened the Free Sans font with FontForge, looked at the glyph info to see which variants were available for the zero glyph, used that to find the set of pnum glyphs, and then copied them over the subscript figures. (u2080 - u2089) The range is arbitrary, so if you're using these glyphs, there's the private use range for instance, though that destroys searchability without some extra steps.

Then in TCPDF, simply ran a string replace on the alphanumeric strings I wanted to affect.

$pdf->writeHTMLCell(0, 0, '', '', '<span style="font-family: freesansmod">H90173A8301X5</span>', 0, 1, 0, true, '', true);

//I feel like there's a shorter way to write this...
$special_numbers = str_replace(['0','1','2','3','4','5','6','7','8','9'],
    ["\u{2080}", "\u{2081}", "\u{2082}", "\u{2083}",
         "\u{2084}", "\u{2085}", "\u{2086}", "\u{2087}",
         "\u{2088}", "\u{2089}"], 'H90173A8301X5');

$pdf->writeHTMLCell(0, 0, '', '', '<span style="font-family: freesansmod">'.$special_numbers.'</span>', 0, 1, 0, true, '', true);

The result is still searchable, though copy-and-paste is a bit weird. [Note: The one is off because I wanted to see if I could make adjustments to glyph positioning.]

Back to your specific case, an alternative if you're willing to relax the "same font" requirement would be to create a separate derivative font with the variant glyphs overwriting the standard numerals and switching to it in the cases you want them. I haven't searched, but there may be pre-existing tools that can automatically do this for you. The content would fully retain its semantic meaning, and the code may be a bit cleaner, at the cost of embedding effectively two copies of the same font.



来源:https://stackoverflow.com/questions/58505628/can-i-use-old-style-non-lining-numerals-in-tcpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!