| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Util::Unicode | # | ||
| 3 | # | Unicode Utilities for LaTeXML | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Util::Unicode; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use base qw(Exporter); | ||
| 16 | our @EXPORT = qw( &UTF &unicode_mathvariant &unicode_convert); | ||
| 17 | |||
| 18 | #====================================================================== | ||
| 19 | # Unicode manipulation utilities useful for LaTeXML | ||
| 20 | # Mostly, but not exclusively, about Mathematics | ||
| 21 | #====================================================================== | ||
| 22 | |||
| 23 | sub UTF { | ||
| 24 | my ($code) = @_; | ||
| 25 | return pack('U', $code); } | ||
| 26 | |||
| 27 | #====================================================================== | ||
| 28 | # Unicode Math Codepoints | ||
| 29 | # The basic latin and greek alphabets, as well as numbers appear in several | ||
| 30 | # (virtual) blocks. | ||
| 31 | # normal | ||
| 32 | # bold, italic, bold-italic | ||
| 33 | # sans-serif, bold-sans-serif, sans-serif-italic | ||
| 34 | # monospace | ||
| 35 | # script, bold-script | ||
| 36 | # fraktur, bold-fraktur | ||
| 37 | # double-struck | ||
| 38 | |||
| 39 | # Create mapping sequences of | ||
| 40 | # latin (uppercase & lowercase), greek (uppercase & lowercase) and digits | ||
| 41 | # to blocks at the given positions within | ||
| 42 | sub makePlane1Map { | ||
| 43 | my ($latin, $GREEK, $greek, $digits) = @_; | ||
| 44 | return ( | ||
| 45 | (map { (UTF(ord('A') + $_) => UTF($latin + $_)) } 0 .. 25), | ||
| 46 | (map { (UTF(ord('a') + $_) => UTF($latin + 26 + $_)) } 0 .. 25), | ||
| 47 | ($GREEK ? (map { (UTF(0x0391 + $_) => UTF($GREEK + $_)) } 0 .. 24) : ()), | ||
| 48 | ($greek ? (map { (UTF(0x03B1 + $_) => UTF($greek + $_)) } 0 .. 24) : ()), | ||
| 49 | ($digits ? (map { (UTF(ord('0') + $_) => UTF($digits + $_)) } 0 .. 9) : ())); } | ||
| 50 | |||
| 51 | my %unicode_map = ( # CONSTANT | ||
| 52 | 'bold' => { makePlane1Map(0x1D400, 0x1D6A8, 0x1D6C2, 0x1D7CE) }, | ||
| 53 | 'italic' => { makePlane1Map(0x1D434, 0x1D6E2, 0x1D6FC, undef), | ||
| 54 | h => "\x{210E}" }, | ||
| 55 | 'bold-italic' => { makePlane1Map(0x1D468, 0x1D71C, 0x1D736, undef) }, | ||
| 56 | 'sans-serif' => { makePlane1Map(0x1D5A0, undef, undef, 0x1D7E2) }, | ||
| 57 | 'bold-sans-serif' => { makePlane1Map(0x1D5D4, 0x1D756, 0x1D770, 0x1D7EC) }, | ||
| 58 | 'sans-serif-italic' => { makePlane1Map(0x1D608, undef, undef, undef) }, | ||
| 59 | 'sans-serif-bold-italic' => { makePlane1Map(0x1D63C, 0x1D790, 0x1D7AA, undef) }, | ||
| 60 | 'monospace' => { makePlane1Map(0x1D670, undef, undef, 0x1D7F6) }, | ||
| 61 | 'script' => { makePlane1Map(0x1D49C, undef, undef, undef), | ||
| 62 | B => "\x{212C}", E => "\x{2130}", F => "\x{2131}", H => "\x{210B}", I => "\x{2110}", | ||
| 63 | L => "\x{2112}", M => "\x{2133}", R => "\x{211B}", | ||
| 64 | e => "\x{212F}", g => "\x{210A}", o => "\x{2134}" }, | ||
| 65 | 'bold-script' => { makePlane1Map(0x1D4D0, undef, undef, undef) }, | ||
| 66 | 'fraktur' => { makePlane1Map(0x1D504, undef, undef, undef), | ||
| 67 | C => "\x{212D}", H => "\x{210C}", I => "\x{2111}", R => "\x{211C}", Z => "\x{2128}" }, | ||
| 68 | 'bold-fraktur' => { makePlane1Map(0x1D56C, undef, undef, undef) }, | ||
| 69 | 'double-struck' => { makePlane1Map(0x1D538, undef, undef, 0x1D7D8), | ||
| 70 | C => "\x{2102}", H => "\x{210D}", N => "\x{2115}", P => "\x{2119}", Q => "\x{211A}", | ||
| 71 | R => "\x{211D}", Z => "\x{2124}" }, | ||
| 72 | superscript => { | ||
| 73 | "\x{2032}" => "'", # \prime | ||
| 74 | 0 => "\x{2070}", | ||
| 75 | 1 => UTF(0xB9), | ||
| 76 | 2 => UTF(0xB2), | ||
| 77 | 3 => UTF(0xB3), | ||
| 78 | 4 => "\x{2074}", | ||
| 79 | 5 => "\x{2075}", | ||
| 80 | 6 => "\x{2076}", | ||
| 81 | 7 => "\x{2077}", | ||
| 82 | 8 => "\x{2078}", | ||
| 83 | 9 => "\x{2079}", | ||
| 84 | '+' => "\x{207A}", | ||
| 85 | '-' => "\x{207B}", | ||
| 86 | '=' => "\x{207C}", | ||
| 87 | '(' => "\x{207D}", | ||
| 88 | ')' => "\x{207E}", | ||
| 89 | 'n' => "\x{207F}", | ||
| 90 | 'i' => "\x{2071}", | ||
| 91 | 'V' => "\x{2C7D}", | ||
| 92 | 'h' => "\x{02B0}", # aspirated!? | ||
| 93 | 'j' => "\x{02B2}", | ||
| 94 | 'r' => "\x{02B3}", | ||
| 95 | 'W' => "\x{02B7}", | ||
| 96 | 'y' => "\x{02B8}", | ||
| 97 | 's' => "\x{02E2}", | ||
| 98 | 'x' => "\x{02E3}", | ||
| 99 | 'A' => "\x{1D2C}", | ||
| 100 | UTF(0xC6) => "\x{1D2D}", | ||
| 101 | 'B' => "\x{1D2E}", | ||
| 102 | 'D' => "\x{1D30}", | ||
| 103 | 'E' => "\x{1D31}", | ||
| 104 | 'G' => "\x{1D33}", | ||
| 105 | 'H' => "\x{1D34}", | ||
| 106 | 'I' => "\x{1D35}", | ||
| 107 | 'J' => "\x{1D36}", | ||
| 108 | 'K' => "\x{1D37}", | ||
| 109 | 'L' => "\x{1D38}", | ||
| 110 | 'M' => "\x{1D39}", | ||
| 111 | 'N' => "\x{1D3A}", | ||
| 112 | 'O' => "\x{1D3C}", | ||
| 113 | 'P' => "\x{1D3E}", | ||
| 114 | 'R' => "\x{1D3F}", | ||
| 115 | 'T' => "\x{1D40}", | ||
| 116 | 'U' => "\x{1D41}", | ||
| 117 | 'W' => "\x{1D42}", | ||
| 118 | 'a' => "\x{1D43}", | ||
| 119 | "\x{03B1}" => "\x{1D45}", # \alpha | ||
| 120 | UTF(0xE6) => "\x{1D46}", | ||
| 121 | 'b' => "\x{1D47}", | ||
| 122 | 'd' => "\x{1D48}", | ||
| 123 | 'e' => "\x{1D49}", | ||
| 124 | "\x{03B5}" => "\x{1D4B}", # \varepsilon | ||
| 125 | "\x{03F5}" => "\x{1D4B}", # \epsilon ??? Close enough? | ||
| 126 | 'g' => "\x{1D4D}", | ||
| 127 | '!' => "\x{1D4E}", | ||
| 128 | 'k' => "\x{1D4F}", | ||
| 129 | 'm' => "\x{1D50}", | ||
| 130 | 'o' => "\x{1D52}", | ||
| 131 | 'p' => "\x{1D56}", | ||
| 132 | 't' => "\x{1D57}", | ||
| 133 | 'u' => "\x{1D58}", | ||
| 134 | 'v' => "\x{1D5B}", | ||
| 135 | "\x{03B2}" => "\x{1D5D}", # \beta | ||
| 136 | "\x{03B3}" => "\x{1D5E}", # \gamma | ||
| 137 | "\x{03B4}" => "\x{1D5F}", # \delta | ||
| 138 | "\x{03C6}" => "\x{1D60}", # \varphi | ||
| 139 | "\x{03D5}" => "\x{1D60}", # \phi; close enough? | ||
| 140 | "\x{03BE}" => "\x{1D61}", # \xi | ||
| 141 | 'H' => "\x{1D78}", | ||
| 142 | 'c' => "\x{1D9C}", | ||
| 143 | 'f' => "\x{1DA0}", | ||
| 144 | 'g' => "\x{1DA2}", | ||
| 145 | "\x{03A6}" => "\x{1DB2}", # \Phi? | ||
| 146 | "\x{03C5}" => "\x{1DB7}", # \upsilon | ||
| 147 | 'z' => "\x{1DBB}", | ||
| 148 | "\x{03B8}" => "\x{1DBF}", # \theta | ||
| 149 | }, | ||
| 150 | subscript => { | ||
| 151 | 0 => "\x{2080}", | ||
| 152 | 1 => "\x{2081}", | ||
| 153 | 2 => "\x{2082}", | ||
| 154 | 3 => "\x{2083}", | ||
| 155 | 4 => "\x{2084}", | ||
| 156 | 5 => "\x{2085}", | ||
| 157 | 6 => "\x{2086}", | ||
| 158 | 7 => "\x{2087}", | ||
| 159 | 8 => "\x{2088}", | ||
| 160 | 9 => "\x{2089}", | ||
| 161 | '+' => "\x{208A}", | ||
| 162 | '-' => "\x{208B}", | ||
| 163 | '=' => "\x{208C}", | ||
| 164 | '(' => "\x{208D}", | ||
| 165 | ')' => "\x{208E}", | ||
| 166 | 'a' => "\x{2090}", | ||
| 167 | 'e' => "\x{2091}", | ||
| 168 | 'o' => "\x{2092}", | ||
| 169 | 'x' => "\x{2093}", | ||
| 170 | 'upsidedowne' => "\x{2094}", | ||
| 171 | 'h' => "\x{2095}", | ||
| 172 | 'k' => "\x{2096}", | ||
| 173 | 'l' => "\x{2097}", | ||
| 174 | 'm' => "\x{2098}", | ||
| 175 | 'n' => "\x{2099}", | ||
| 176 | 'p' => "\x{209A}", | ||
| 177 | 's' => "\x{209B}", | ||
| 178 | 't' => "\x{209C}", | ||
| 179 | 'j' => "\x{2C7C}", | ||
| 180 | 'i' => "\x{1D62}", | ||
| 181 | 'r' => "\x{1D63}", | ||
| 182 | 'u' => "\x{1D64}", | ||
| 183 | 'v' => "\x{1D65}", | ||
| 184 | "\x{03B2}" => "\x{1D66}", # \beta | ||
| 185 | "\x{03B3}" => "\x{1D67}", # \gamma | ||
| 186 | "\x{03C1}" => "\x{1D68}", # \rho | ||
| 187 | "\x{03C6}" => "\x{1D69}", # \varphi | ||
| 188 | "\x{03D5}" => "\x{1D69}", # \phi; close enough? | ||
| 189 | "\x{03BE}" => "\x{1D6A}", # \xi | ||
| 190 | }, | ||
| 191 | ); | ||
| 192 | |||
| 193 | # Return the re-mapping of the codepoints in $string to the codepoints corresponding | ||
| 194 | # to the given $style. | ||
| 195 | # Returns undef if not all chars have a known mapping. | ||
| 196 | sub unicode_convert { | ||
| 197 | my ($string, $style) = @_; | ||
| 198 | if (my $mapping = $unicode_map{$style}) { | ||
| 199 | my @c = map { $$mapping{$_} } split(//, (defined $string ? $string : '')); | ||
| 200 | if (!grep { !defined $_ } @c) { # Only if ALL chars in the token could be mapped... ????? | ||
| 201 | return join('', @c); } } | ||
| 202 | return; } | ||
| 203 | |||
| 204 | #====================================================================== | ||
| 205 | # Normalizing a Math Font for lookup in Unicode. | ||
| 206 | # Basically, this results in MathML's mathvariant | ||
| 207 | # which corresponds to the major blocks of "styled" characters in Unicode: | ||
| 208 | # Mappings between (normalized) internal fonts & sizes. | ||
| 209 | # Default math font is roman|medium|upright. | ||
| 210 | my %mathvariants = ( # CONSTANT | ||
| 211 | 'upright' => 'normal', | ||
| 212 | 'serif' => 'normal', | ||
| 213 | 'medium' => 'normal', | ||
| 214 | 'bold' => 'bold', | ||
| 215 | 'italic' => 'italic', | ||
| 216 | 'medium italic' => 'italic', | ||
| 217 | 'bold italic' => 'bold-italic', | ||
| 218 | 'doublestruck' => 'double-struck', | ||
| 219 | 'blackboard' => 'double-struck', | ||
| 220 | 'blackboard bold' => 'double-struck', # all collapse | ||
| 221 | 'blackboard upright' => 'double-struck', # all collapse | ||
| 222 | 'blackboard bold upright' => 'double-struck', # all collapse | ||
| 223 | 'fraktur' => 'fraktur', | ||
| 224 | 'fraktur italic' => 'fraktur', # all collapse | ||
| 225 | 'fraktur bold' => 'bold-fraktur', | ||
| 226 | 'script' => 'script', | ||
| 227 | 'script italic' => 'script', # all collapse | ||
| 228 | 'script bold' => 'bold-script', | ||
| 229 | 'caligraphic' => 'script', # NOTE: TeX caligraphic is NOT script! | ||
| 230 | 'caligraphic bold' => 'bold-script', # collapse | ||
| 231 | 'sansserif' => 'sans-serif', | ||
| 232 | 'sansserif bold' => 'bold-sans-serif', | ||
| 233 | 'sansserif italic' => 'sans-serif-italic', | ||
| 234 | 'sansserif bold italic' => 'sans-serif-bold-italic', | ||
| 235 | 'typewriter' => 'monospace', | ||
| 236 | 'typewriter bold' => 'monospace', | ||
| 237 | 'typewriter italic' => 'monospace', | ||
| 238 | 'typewriter bold italic' => 'monospace', | ||
| 239 | ); | ||
| 240 | |||
| 241 | # The font differences (from the containing context) have been deciphered | ||
| 242 | # into font, size and color attributes. The font should match | ||
| 243 | # one of the above... (?) | ||
| 244 | |||
| 245 | # Given a font string (joining the components) | ||
| 246 | # reduce it to a "sane" font. Note that MathML uses a single mathvariant | ||
| 247 | # to name the font, and doesn't inherit font components like italic or bold. | ||
| 248 | # Thus the font should be "complete", but also we can ignore components with | ||
| 249 | # default values like medium or upright (unless that is the only component). | ||
| 250 | sub unicode_mathvariant { | ||
| 251 | my ($font) = @_; | ||
| 252 | $font =~ s/slanted/italic/; # equivalent in math | ||
| 253 | $font =~ s/(?<!\w)serif// unless $font eq 'serif'; # Not needed (unless alone) | ||
| 254 | $font =~ s/(?<!^)upright//; # Not needed (unless 1st element) | ||
| 255 | $font =~ s/(?<!^)medium//; # Not needed (unless 1st element) | ||
| 256 | $font =~ s/^\s+//; $font =~ s/\s+$//; | ||
| 257 | my $variant; | ||
| 258 | return $variant if $variant = $mathvariants{$font}; | ||
| 259 | # $font =~ s/\sitalic//; # try w/o italic ? | ||
| 260 | # return $variant if $variant = $mathvariants{$font}; | ||
| 261 | # $font =~ s/\sbold//; # try w/o bold ? | ||
| 262 | # return $variant if $variant = $mathvariants{$font}; | ||
| 263 | return 'normal'; } | ||
| 264 | |||
| 265 | #====================================================================== | ||
| 266 | 1; | ||
| 267 | |||
| 268 | __END__ |