Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

r""" 

Unicode Art 

 

This module implements ascii art using unicode characters. It is a 

strict superset of :mod:`~sage.typeset.ascii_art`. 

""" 

 

#******************************************************************************* 

# Copyright (C) 2013 Jean-Baptiste Priez <jbp@kerios.fr>, 

# 2015 Volker Braun <vbraun.name@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#******************************************************************************* 

 

from sage.typeset.character_art import CharacterArt 

from sage.typeset.character_art_factory import CharacterArtFactory 

import sage.typeset.symbols as symbol 

from six import text_type 

 

 

class UnicodeArt(CharacterArt): 

r""" 

An Ascii art object is an object with some specific representation for 

*printing*. 

 

INPUT: 

 

- ``lines`` -- the list of lines of the representation of the ascii art 

object 

 

- ``breakpoints`` -- the list of points where the representation can be 

split 

 

- ``baseline`` -- the reference line (from the bottom) 

 

EXAMPLES:: 

 

sage: i = var('i') 

sage: unicode_art(sum(pi^i/factorial(i)*x^i, i, 0, oo)) 

π⋅x 

 

""" 

_string_type = text_type 

 

def __unicode__(self): 

r""" 

Return a unicode representation of ``self``. 

 

EXAMPLES:: 

 

sage: i = var('i') 

sage: ua = unicode_art(sum(pi^i/factorial(i)*x^i, i, 0, oo)) 

sage: unicode(ua) # py2 

u' \u03c0\u22c5x\n\u212f ' 

sage: str(ua) # py3 

' \u03c0\u22c5x\n\u212f ' 

""" 

return repr(self).decode("utf-8") 

 

_unicode_art_factory = CharacterArtFactory( 

UnicodeArt, text_type, '_unicode_art_', 

(symbol.unicode_left_parenthesis, symbol.unicode_right_parenthesis), 

(symbol.unicode_left_square_bracket, symbol.unicode_right_square_bracket), 

(symbol.unicode_left_curly_brace, symbol.unicode_right_curly_brace), 

) 

 

 

empty_unicode_art = _unicode_art_factory.build_empty() 

 

 

def unicode_art(*obj, **kwds): 

r""" 

Return an unicode art representation 

 

INPUT: 

 

 

- ``*obj`` -- any number of positional arguments, of arbitrary 

type. The objects whose ascii art representation we want. 

 

- ``sep`` -- optional ``'sep=...'`` keyword argument (or ``'separator'``). 

Anything that can be converted to unicode art (default: empty unicode 

art). The separator in-between a list of objects. Only used if 

more than one object given. 

 

- ``baseline`` -- (default: 0) the baseline for the object 

 

- ``sep_baseline`` -- (default: 0) the baseline for the separator 

 

OUTPUT: 

 

:class:`UnicodeArt` instance. 

 

EXAMPLES:: 

 

sage: unicode_art(integral(exp(sqrt(x))/(x+pi), x)) 

 

⎮ √x 

⎮ ℯ 

⎮ ───── dx 

⎮ x + π 

 

sage: ident = lambda n: identity_matrix(ZZ, n) 

sage: unicode_art(ident(1), ident(2), ident(3), sep=' : ') 

⎛1 0 0⎞ 

⎛1 0⎞ ⎜0 1 0⎟ 

(1) : ⎝0 1⎠ : ⎝0 0 1⎠ 

 

If specified, the ``sep_baseline`` overrides the baseline of 

an unicode art separator:: 

 

sage: sep_line = unicode_art('\n'.join(u' ⎟ ' for _ in range(5)), baseline=5) 

sage: unicode_art(*AlternatingSignMatrices(3), 

....: separator=sep_line, sep_baseline=1) 

⎟ ⎟ ⎟ ⎟ ⎟ ⎟  

⎛1 0 0⎞ ⎟ ⎛0 1 0⎞ ⎟ ⎛1 0 0⎞ ⎟ ⎛ 0 1 0⎞ ⎟ ⎛0 0 1⎞ ⎟ ⎛0 1 0⎞ ⎟ ⎛0 0 1⎞ 

⎜0 1 0⎟ ⎟ ⎜1 0 0⎟ ⎟ ⎜0 0 1⎟ ⎟ ⎜ 1 -1 1⎟ ⎟ ⎜1 0 0⎟ ⎟ ⎜0 0 1⎟ ⎟ ⎜0 1 0⎟ 

⎝0 0 1⎠ ⎟ ⎝0 0 1⎠ ⎟ ⎝0 1 0⎠ ⎟ ⎝ 0 1 0⎠ ⎟ ⎝0 1 0⎠ ⎟ ⎝1 0 0⎠ ⎟ ⎝1 0 0⎠ 

⎟ ⎟ ⎟ ⎟ ⎟ ⎟  

 

TESTS:: 

 

sage: n = var('n') 

sage: unicode_art(sum(binomial(2 * n, n + 1) * x^n, n, 0, oo)) 

⎛ __________ ⎞ 

-⎝2⋅x + ╲╱ -4⋅x + 1 - 1⎠ 

────────────────────────── 

__________ 

2⋅x⋅╲╱ -4⋅x + 1 

sage: unicode_art(list(DyckWords(3))) 

⎡ ╱╲ ⎤ 

⎢ ╱╲ ╱╲ ╱╲╱╲ ╱ ╲ ⎥ 

⎣ ╱╲╱╲╱╲, ╱╲╱ ╲, ╱ ╲╱╲, ╱ ╲, ╱ ╲ ⎦ 

sage: unicode_art(1) 

1 

""" 

separator, baseline, sep_baseline = _unicode_art_factory.parse_keywords(kwds) 

if kwds: 

raise ValueError('unknown keyword arguments: {0}'.format(list(kwds))) 

if len(obj) == 1: 

return _unicode_art_factory.build(obj[0], baseline=baseline) 

if not isinstance(separator, UnicodeArt): 

separator = _unicode_art_factory.build(separator, baseline=sep_baseline) 

elif sep_baseline is not None: 

from copy import copy 

separator = copy(separator) 

separator._baseline = sep_baseline 

obj = map(_unicode_art_factory.build, obj) 

return _unicode_art_factory.concatenate(obj, separator, empty_unicode_art, 

baseline=baseline)