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

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

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

""" 

Symbols for Character Art 

 

This module defines single- and multi-line symbols. 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import * 

 

sage: symbols = ascii_art(u'') 

sage: for i in range(1, 5): 

....: symbols += ascii_left_parenthesis.character_art(i) 

....: symbols += ascii_art(u' ') 

....: symbols += ascii_right_parenthesis.character_art(i) 

....: symbols += ascii_art(u' ') 

sage: for i in range(1, 5): 

....: symbols += ascii_left_square_bracket.character_art(i) 

....: symbols += ascii_art(u' ') 

....: symbols += ascii_right_square_bracket.character_art(i) 

....: symbols += ascii_art(u' ') 

sage: for i in range(1, 5): 

....: symbols += ascii_left_curly_brace.character_art(i) 

....: symbols += ascii_art(u' ') 

....: symbols += ascii_right_curly_brace.character_art(i) 

....: symbols += ascii_art(u' ') 

sage: symbols 

( ) [ ] { } 

( ) ( ) [ ] [ ] { } { } 

( ) ( ) ( ) [ ] [ ] [ ] { } { } { } 

( ) ( ) ( ) ( ) [ ] [ ] [ ] [ ] { } { } { } { } 

 

sage: symbols = unicode_art(u'') 

sage: for i in range(1, 5): 

....: symbols += unicode_left_parenthesis.character_art(i) 

....: symbols += unicode_art(u' ') 

....: symbols += unicode_right_parenthesis.character_art(i) 

....: symbols += unicode_art(u' ') 

sage: for i in range(1, 5): 

....: symbols += unicode_left_square_bracket.character_art(i) 

....: symbols += unicode_art(u' ') 

....: symbols += unicode_right_square_bracket.character_art(i) 

....: symbols += unicode_art(u' ') 

sage: for i in range(1, 5): 

....: symbols += unicode_left_curly_brace.character_art(i) 

....: symbols += unicode_art(u' ') 

....: symbols += unicode_right_curly_brace.character_art(i) 

....: symbols += unicode_art(u' ') 

sage: symbols 

⎛ ⎞ ⎡ ⎤ ⎧ ⎫ 

⎛ ⎞ ⎜ ⎟ ⎡ ⎤ ⎢ ⎥ ⎧ ⎫ ⎭ ⎩ 

⎛ ⎞ ⎜ ⎟ ⎜ ⎟ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎰ ⎱ ⎨ ⎬ ⎫ ⎧ 

( ) ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ [ ] ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ { } ⎱ ⎰ ⎩ ⎭ ⎩ ⎭ 

""" 

 

import unicodedata 

from sage.structure.sage_object import SageObject 

 

 

class CompoundSymbol(SageObject): 

 

def __init__(self, character, top, extension, bottom, 

middle=None, 

middle_top=None, middle_bottom=None, 

top_2=None, bottom_2=None): 

""" 

A multi-character (ascii/unicode art) symbol 

 

INPUT: 

 

Instead of string, each of these can be unicode in Python 2: 

 

- ``character`` -- string. The single-line version of the symbol. 

 

- ``top`` -- string. The top line of a multi-line symbol. 

 

- ``extension`` -- string. The extension line of a multi-line symbol (will 

be repeated). 

 

- ``bottom`` -- string. The bottom line of a multi-line symbol. 

 

- ``middle`` -- optional string. The middle part, for example 

in curly braces. Will be used only once for the symbol, and 

only if its height is odd. 

 

- ``middle_top`` -- optional string. The upper half of the 

2-line middle part if the height of the symbol is even. 

Will be used only once for the symbol. 

 

- ``middle_bottom`` -- optional string. The lower half of the 

2-line middle part if the height of the symbol is even. 

Will be used only once for the symbol. 

 

- ``top_2`` -- optional string. The upper half of a 2-line symbol. 

 

- ``bottom_2`` -- optional string. The lower half of a 2-line symbol. 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import CompoundSymbol 

sage: i = CompoundSymbol('I', '+', '|', '+', '|') 

sage: i.print_to_stdout(1) 

I 

sage: i.print_to_stdout(3) 

+ 

| 

+ 

""" 

self.character = character 

self.top = top 

self.extension = extension 

self.bottom = bottom 

self.middle = middle or extension 

self.middle_top = middle_top or extension 

self.middle_bottom = middle_bottom or extension 

self.top_2 = top_2 or top 

self.bottom_2 = bottom_2 or bottom 

 

def _repr_(self): 

""" 

Return string representation 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import unicode_left_parenthesis 

sage: unicode_left_parenthesis 

multi_line version of "(" 

""" 

return u'multi_line version of "{0}"'.format(self.character).encode('utf-8') 

 

def __call__(self, num_lines): 

r""" 

Return the lines for a multi-line symbol 

 

INPUT: 

 

- ``num_lines`` -- integer. The total number of lines. 

 

OUTPUT: 

 

List of strings / unicode strings. 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import unicode_left_parenthesis 

sage: unicode_left_parenthesis(4) 

[u'\u239b', u'\u239c', u'\u239c', u'\u239d'] 

""" 

if num_lines <= 0: 

raise ValueError('number of lines must be positive') 

elif num_lines == 1: 

return [self.character] 

elif num_lines == 2: 

return [self.top_2, self.bottom_2] 

elif num_lines == 3: 

return [self.top, self.middle, self.bottom] 

elif num_lines %2 == 0: 

ext = [self.extension] * ((num_lines-4) // 2) 

return [self.top] + ext + [self.middle_top, self.middle_bottom] + ext + [self.bottom] 

else: # num_lines %2 == 1 

ext = [self.extension] * ((num_lines-3) // 2) 

return [self.top] + ext + [self.middle] + ext + [self.bottom] 

 

def print_to_stdout(self, num_lines): 

""" 

Print the multi-line symbol 

 

This method is for testing purposes. 

 

INPUT: 

 

- ``num_lines`` -- integer. The total number of lines. 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import * 

sage: unicode_integral.print_to_stdout(1) 

 

sage: unicode_integral.print_to_stdout(2) 

 

 

sage: unicode_integral.print_to_stdout(3) 

 

 

 

sage: unicode_integral.print_to_stdout(4) 

 

 

 

 

""" 

print(u'\n'.join(self(num_lines))) 

 

 

class CompoundAsciiSymbol(CompoundSymbol): 

 

def character_art(self, num_lines): 

""" 

Return the ASCII art of the symbol 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import * 

sage: ascii_left_curly_brace.character_art(3) 

{ 

{ 

{ 

""" 

from sage.typeset.ascii_art import AsciiArt 

return AsciiArt(self(num_lines)) 

 

 

class CompoundUnicodeSymbol(CompoundSymbol): 

 

def character_art(self, num_lines): 

""" 

Return the unicode art of the symbol 

 

EXAMPLES:: 

 

sage: from sage.typeset.symbols import * 

sage: unicode_left_curly_brace.character_art(3) 

 

 

 

""" 

from sage.typeset.unicode_art import UnicodeArt 

return UnicodeArt(self(num_lines)) 

 

 

ascii_integral = CompoundAsciiSymbol( 

'int', 

r' /\\', 

r' | ', 

r'\\/ ', 

) 

 

unicode_integral = CompoundUnicodeSymbol( 

unicodedata.lookup('INTEGRAL'), 

unicodedata.lookup('TOP HALF INTEGRAL'), 

unicodedata.lookup('INTEGRAL EXTENSION'), 

unicodedata.lookup('BOTTOM HALF INTEGRAL'), 

) 

 

ascii_left_parenthesis = CompoundAsciiSymbol( 

'(', 

'(', 

'(', 

'(', 

) 

 

ascii_right_parenthesis = CompoundAsciiSymbol( 

')', 

')', 

')', 

')', 

) 

 

unicode_left_parenthesis = CompoundUnicodeSymbol( 

unicodedata.lookup('LEFT PARENTHESIS'), 

unicodedata.lookup('LEFT PARENTHESIS UPPER HOOK'), 

unicodedata.lookup('LEFT PARENTHESIS EXTENSION'), 

unicodedata.lookup('LEFT PARENTHESIS LOWER HOOK'), 

) 

 

unicode_right_parenthesis = CompoundUnicodeSymbol( 

unicodedata.lookup('RIGHT PARENTHESIS'), 

unicodedata.lookup('RIGHT PARENTHESIS UPPER HOOK'), 

unicodedata.lookup('RIGHT PARENTHESIS EXTENSION'), 

unicodedata.lookup('RIGHT PARENTHESIS LOWER HOOK'), 

) 

 

ascii_left_square_bracket = CompoundAsciiSymbol( 

'[', 

'[', 

'[', 

'[', 

) 

 

ascii_right_square_bracket = CompoundAsciiSymbol( 

']', 

']', 

']', 

']', 

) 

 

unicode_left_square_bracket = CompoundUnicodeSymbol( 

unicodedata.lookup('LEFT SQUARE BRACKET'), 

unicodedata.lookup('LEFT SQUARE BRACKET UPPER CORNER'), 

unicodedata.lookup('LEFT SQUARE BRACKET EXTENSION'), 

unicodedata.lookup('LEFT SQUARE BRACKET LOWER CORNER'), 

) 

 

unicode_right_square_bracket = CompoundUnicodeSymbol( 

unicodedata.lookup('RIGHT SQUARE BRACKET'), 

unicodedata.lookup('RIGHT SQUARE BRACKET UPPER CORNER'), 

unicodedata.lookup('RIGHT SQUARE BRACKET EXTENSION'), 

unicodedata.lookup('RIGHT SQUARE BRACKET LOWER CORNER'), 

) 

 

ascii_left_curly_brace = CompoundAsciiSymbol( 

'{', 

'{', 

'{', 

'{', 

) 

 

ascii_right_curly_brace = CompoundAsciiSymbol( 

'}', 

'}', 

'}', 

'}', 

) 

 

unicode_left_curly_brace = CompoundUnicodeSymbol( 

unicodedata.lookup('LEFT CURLY BRACKET'), 

unicodedata.lookup('LEFT CURLY BRACKET UPPER HOOK'), 

unicodedata.lookup('CURLY BRACKET EXTENSION'), 

unicodedata.lookup('LEFT CURLY BRACKET LOWER HOOK'), 

unicodedata.lookup('LEFT CURLY BRACKET MIDDLE PIECE'), 

unicodedata.lookup('RIGHT CURLY BRACKET LOWER HOOK'), 

unicodedata.lookup('RIGHT CURLY BRACKET UPPER HOOK'), 

unicodedata.lookup('UPPER LEFT OR LOWER RIGHT CURLY BRACKET SECTION'), 

unicodedata.lookup('UPPER RIGHT OR LOWER LEFT CURLY BRACKET SECTION'), 

) 

 

unicode_right_curly_brace = CompoundUnicodeSymbol( 

unicodedata.lookup('RIGHT CURLY BRACKET'), 

unicodedata.lookup('RIGHT CURLY BRACKET UPPER HOOK'), 

unicodedata.lookup('CURLY BRACKET EXTENSION'), 

unicodedata.lookup('RIGHT CURLY BRACKET LOWER HOOK'), 

unicodedata.lookup('RIGHT CURLY BRACKET MIDDLE PIECE'), 

unicodedata.lookup('LEFT CURLY BRACKET LOWER HOOK'), 

unicodedata.lookup('LEFT CURLY BRACKET UPPER HOOK'), 

unicodedata.lookup('UPPER RIGHT OR LOWER LEFT CURLY BRACKET SECTION'), 

unicodedata.lookup('UPPER LEFT OR LOWER RIGHT CURLY BRACKET SECTION'), 

)