Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9bf6e87

Browse files
committed
Add support for more accents in mathtext
1 parent 9e0747b commit 9bf6e87

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
220220
if bunch is not None:
221221
return bunch
222222

223-
font, num, slanted = self._get_glyph(
223+
font, num, slanted, substituted_glyph = self._get_glyph(
224224
fontname, font_class, sym, fontsize)
225225

226226
font.set_size(fontsize, dpi)
@@ -239,7 +239,8 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
239239
ymax = ymax+offset,
240240
# iceberg is the equivalent of TeX's "height"
241241
iceberg = glyph.horiBearingY/64.0 + offset,
242-
slanted = slanted
242+
slanted = slanted,
243+
substituted_glyph = substituted_glyph
243244
)
244245

245246
result = self.glyphd[key] = types.SimpleNamespace(
@@ -323,7 +324,7 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
323324
if font is not None:
324325
num = ord(sym)
325326
if font is not None and font.get_char_index(num) != 0:
326-
return font, num, slanted
327+
return font, num, slanted, False
327328
else:
328329
return self._stix_fallback._get_glyph(
329330
fontname, font_class, sym, fontsize)
@@ -448,6 +449,8 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
448449
found_symbol = False
449450
_log.warning("No TeX to Unicode mapping for {!a}.".format(sym))
450451

452+
substituted_glyph = False
453+
451454
fontname, uniindex = self._map_virtual_font(
452455
fontname, font_class, uniindex)
453456

@@ -499,8 +502,9 @@ def _get_glyph(self, fontname, font_class, sym, fontsize):
499502
font = self._get_font('rm')
500503
uniindex = 0xA4 # currency char, for lack of anything better
501504
slanted = False
505+
substituted_glyph = True
502506

503-
return font, uniindex, slanted
507+
return font, uniindex, slanted, substituted_glyph
504508

505509
def get_sized_alternatives_for_symbol(self, fontname, sym):
506510
if self._fallback_font:
@@ -935,6 +939,9 @@ def _update_metrics(self):
935939
def is_slanted(self):
936940
return self._metrics.slanted
937941

942+
def is_substituted(self):
943+
return self._metrics.substituted_glyph
944+
938945
def get_kerning(self, next):
939946
"""
940947
Return the amount of kerning between this and the given character.
@@ -2011,7 +2018,7 @@ def unknown_symbol(self, s, loc, toks):
20112018
raise ParseFatalException(s, loc, f"Unknown symbol: {toks['name']}")
20122019

20132020
_accent_map = {
2014-
r'hat': r'\circumflexaccent',
2021+
r'hat': r'\combiningcircumflexaccent',
20152022
r'breve': r'\combiningbreve',
20162023
r'bar': r'\combiningoverline',
20172024
r'grave': r'\combininggraveaccent',
@@ -2027,10 +2034,13 @@ def unknown_symbol(self, s, loc, toks):
20272034
r"'": r'\combiningacuteaccent',
20282035
r'~': r'\combiningtilde',
20292036
r'.': r'\combiningdotabove',
2030-
r'^': r'\circumflexaccent',
2031-
r'overrightarrow': r'\rightarrow',
2032-
r'overleftarrow': r'\leftarrow',
2033-
r'mathring': r'\circ',
2037+
r'^': r'\combiningcircumflexaccent',
2038+
r'overrightarrow': r'\combiningrightarrowabove',
2039+
r'overleftarrow': r'\combiningleftarrowabove',
2040+
r'mathring': r'\combiningringabove',
2041+
r'=': r'\combiningmacron',
2042+
r'H': r'\combiningdoubleacuteaccent',
2043+
r'check': r'\combiningcaron',
20342044
}
20352045

20362046
_wide_accents = set(r"widehat widetilde widebar".split())
@@ -2050,10 +2060,27 @@ def accent(self, s, loc, toks):
20502060
accent_box = AutoWidthChar(
20512061
'\\' + accent, sym.width, state, char_class=Accent)
20522062
else:
2063+
# Check if accent and character can be combined
2064+
a = get_unicode_index(self._accent_map[accent])
2065+
if isinstance(sym, Char):
2066+
c = sym.c
2067+
else:
2068+
c = sym.children[0].c
2069+
if c == chr(305): # Dotless i, but normal i combines
2070+
c = 'i'
2071+
comb = unicodedata.normalize('NFC', c + chr(a))
2072+
if len(comb) == 1: # Check that they did combine
2073+
newsym = Char(comb, state)
2074+
# Check that glyph exists
2075+
if not newsym.is_substituted():
2076+
return newsym
2077+
if c == 'i': # Turn i into dotless i
2078+
sym = Char(chr(305), state)
2079+
if sym.is_substituted():
2080+
# Dotless i does not exist
2081+
sym = Char('i', state)
2082+
# Cannot be combined
20532083
accent_box = Accent(self._accent_map[accent], state)
2054-
if accent == 'mathring':
2055-
accent_box.shrink()
2056-
accent_box.shrink()
20572084
centered = HCentered([Hbox(sym.width / 4.0), accent_box])
20582085
centered.hpack(sym.width, 'exactly')
20592086
return Vlist([
@@ -2137,9 +2164,6 @@ def is_slanted(self, nucleus):
21372164
return nucleus.is_slanted()
21382165
return False
21392166

2140-
def is_between_brackets(self, s, loc):
2141-
return False
2142-
21432167
def subsuper(self, s, loc, toks):
21442168
nucleus = toks.get("nucleus", Hbox(0))
21452169
subsuper = toks.get("subsuper", [])

lib/matplotlib/_mathtext_data.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@
117117
'\\Upsilon' : ('cmr10', 0xa8),
118118
'\\Xi' : ('cmr10', 0xa5),
119119
'\\circumflexaccent' : ('cmr10', 0x5e),
120+
'\\combiningcircumflexaccent' : ('cmr10', 0x5e),
120121
'\\combiningacuteaccent' : ('cmr10', 0xb6),
121122
'\\combiningbreve' : ('cmr10', 0xb8),
122123
'\\combiningdiaeresis' : ('cmr10', 0xc4),
123124
'\\combiningdotabove' : ('cmr10', 0x5f),
124125
'\\combininggraveaccent' : ('cmr10', 0xb5),
125126
'\\combiningoverline' : ('cmr10', 0xb9),
127+
'\\combiningmacron' : ('cmr10', 0x16),
128+
'\\combiningringabove' : ('cmr10', 0x15),
129+
'\\combiningcaron' : ('cmr10', 0x14),
126130
'\\combiningtilde' : ('cmr10', 0x7e),
131+
'\\combiningdoubleacuteaccent' : ('cmr10', 0x7d),
127132
'\\leftbracket' : ('cmr10', 0x5b),
128133
'\\leftparen' : ('cmr10', 0x28),
129134
'\\rightbracket' : ('cmr10', 0x5d),
@@ -994,14 +999,20 @@
994999
'circumflexaccent' : 770,
9951000
'combiningbreve' : 774,
9961001
'combiningoverline' : 772,
1002+
'combiningmacron' : 773,
9971003
'combininggraveaccent' : 768,
9981004
'combiningacuteaccent' : 769,
9991005
'combiningdiaeresis' : 776,
10001006
'combiningtilde' : 771,
10011007
'combiningrightarrowabove' : 8407,
1008+
'combiningleftarrowabove' : 8406,
10021009
'combiningdotabove' : 775,
1010+
'combiningringabove' : 778,
10031011
'combiningthreedotsabove' : 8411,
10041012
'combiningfourdotsabove' : 8412,
1013+
'combiningdoubleacuteaccent' : 779,
1014+
'combiningcaron' : 780,
1015+
'combiningcircumflexaccent' : 770,
10051016
'to' : 8594,
10061017
'succeq' : 8829,
10071018
'emptyset' : 8709,

lib/matplotlib/tests/test_mathtext.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
r'$,$ $.$ $1{,}234{, }567{ , }890$ and $1,234,567,890$', # github issue 5799
116116
r'$\left(X\right)_{a}^{b}$', # github issue 7615
117117
r'$\dfrac{\$100.00}{y}$', # github issue #1888
118+
r'$\={M}\H{a}\check{t}\hat{p}\overleftarrow{l}\overrightarrow{o}\"{t}'
119+
r'\.{l}\mathring{i}\~{b}$ $\breve{M}\bar{a}\grave{t}\acute{p}\vec{l}'
120+
r'\dddot{o}\ddddot{t}\^{l}\ddot{i}\overline{b}$'
118121
]
119122
# 'svgastext' tests switch svg output to embed text as text (rather than as
120123
# paths).

tutorials/text/mathtext.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@
306306
Command Result
307307
============================== =================================
308308
``\acute a`` or ``\'a`` :mathmpl:`\acute a`
309-
``\bar a`` :mathmpl:`\bar a`
309+
``\bar a`` or ``\= a`` :mathmpl:`\bar a`
310310
``\breve a`` :mathmpl:`\breve a`
311311
``\dot a`` or ``\.a`` :mathmpl:`\dot a`
312312
``\ddot a`` or ``\''a`` :mathmpl:`\ddot a`
@@ -316,6 +316,8 @@
316316
``\hat a`` or ``\^a`` :mathmpl:`\hat a`
317317
``\tilde a`` or ``\~a`` :mathmpl:`\tilde a`
318318
``\vec a`` :mathmpl:`\vec a`
319+
``\check a`` :mathmpl:`\check a`
320+
``\H a`` :mathmpl:`\H a`
319321
``\overline{abc}`` :mathmpl:`\overline{abc}`
320322
============================== =================================
321323

0 commit comments

Comments
 (0)