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

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

# -*- encoding: utf-8 -*- 

r""" 

Graphics Output Types 

 

This module defines the rich output types for 2-d images, both vector 

and raster graphics. 

""" 

 

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

# Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com> 

# 

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

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

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

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

 

 

import os 

import base64 

 

from sage.cpython.string import bytes_to_str 

from sage.repl.rich_output.output_basic import OutputBase 

from sage.repl.rich_output.buffer import OutputBuffer 

 

 

class OutputImagePng(OutputBase): 

 

def __init__(self, png): 

""" 

PNG Image 

 

.. NOTE:: 

 

Every backend that is capable of displaying any kind of 

graphics is supposed to support the PNG format at least. 

 

INPUT: 

 

- ``png`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

PNG image data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImagePng 

sage: OutputImagePng.example() # indirect doctest 

OutputImagePng container 

""" 

self.png = OutputBuffer(png) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample PNG output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImagePng`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImagePng 

sage: OutputImagePng.example() 

OutputImagePng container 

sage: OutputImagePng.example().png 

buffer containing 608 bytes 

sage: OutputImagePng.example().png.get().startswith(b'\x89PNG') 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.png') 

with open(filename, 'rb') as f: 

return cls(f.read()) 

 

 

class OutputImageGif(OutputBase): 

 

def __init__(self, gif): 

""" 

GIF Image (possibly animated) 

 

INPUT: 

 

- ``gif`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

GIF image data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageGif 

sage: OutputImageGif.example() # indirect doctest 

OutputImageGif container 

""" 

self.gif = OutputBuffer(gif) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample GIF output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImageGif`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageGif 

sage: OutputImageGif.example() 

OutputImageGif container 

sage: OutputImageGif.example().gif 

buffer containing 408 bytes 

sage: OutputImageGif.example().gif.get().startswith(b'GIF89a') 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.gif') 

with open(filename, 'rb') as f: 

return cls(f.read()) 

 

def html_fragment(self): 

""" 

Return a self-contained HTML fragment displaying the image 

 

This is a workaround for the Jupyter notebook which doesn't support GIF directly. 

 

OUTPUT: 

 

String. HTML fragment for displaying the GIF image. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageGif 

sage: OutputImageGif.example().html_fragment() 

'<img src="data:image/gif;base64,R0lGODl...zd3t/g4eLj5OVDQQA7"/>' 

""" 

b64 = bytes_to_str(base64.b64encode(self.gif.get()), 'ascii') 

return '<img src="data:image/gif;base64,{0}"/>'.format(b64) 

 

 

class OutputImageJpg(OutputBase): 

 

def __init__(self, jpg): 

""" 

JPEG Image 

 

INPUT: 

 

- ``jpg`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

JPEG image data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageJpg 

sage: OutputImageJpg.example() # indirect doctest 

OutputImageJpg container 

""" 

self.jpg = OutputBuffer(jpg) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample JPEG output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImageJpg`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageJpg 

sage: OutputImageJpg.example() 

OutputImageJpg container 

sage: OutputImageJpg.example().jpg 

buffer containing 978 bytes 

sage: OutputImageJpg.example().jpg.get().startswith(b'\xff\xd8\xff\xe0\x00\x10JFIF') 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.jpg') 

with open(filename, 'rb') as f: 

return cls(f.read()) 

 

 

class OutputImageSvg(OutputBase): 

 

def __init__(self, svg): 

""" 

SVG Image 

 

INPUT: 

 

- ``svg`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

SVG image data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageSvg 

sage: OutputImageSvg.example() # indirect doctest 

OutputImageSvg container 

""" 

self.svg = OutputBuffer(svg) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample SVG output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImageSvg`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageSvg 

sage: OutputImageSvg.example() 

OutputImageSvg container 

sage: OutputImageSvg.example().svg 

buffer containing 1422 bytes 

sage: b'</svg>' in OutputImageSvg.example().svg.get() 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.svg') 

with open(filename, 'rb') as f: 

return cls(f.read()) 

 

 

class OutputImagePdf(OutputBase): 

 

def __init__(self, pdf): 

""" 

PDF Image 

 

INPUT: 

 

- ``pdf`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

PDF data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImagePdf 

sage: OutputImagePdf.example() # indirect doctest 

OutputImagePdf container 

""" 

self.pdf = OutputBuffer(pdf) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample PDF output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImagePdf`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImagePdf 

sage: OutputImagePdf.example() 

OutputImagePdf container 

sage: OutputImagePdf.example().pdf 

buffer containing 4285 bytes 

sage: OutputImagePdf.example().pdf.get().startswith(b'%PDF-1.4') 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.pdf') 

with open(filename, 'rb') as f: 

return cls(f.read()) 

 

 

class OutputImageDvi(OutputBase): 

 

def __init__(self, dvi): 

""" 

DVI Image 

 

INPUT: 

 

- ``dvi`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

DVI data. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageDvi 

sage: OutputImageDvi.example() # indirect doctest 

OutputImageDvi container 

""" 

self.dvi = OutputBuffer(dvi) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample DVI output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputImageDvi`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputImageDvi 

sage: OutputImageDvi.example() 

OutputImageDvi container 

sage: OutputImageDvi.example().dvi 

buffer containing 212 bytes 

sage: b'TeX output' in OutputImageDvi.example().dvi.get() 

True 

""" 

from sage.env import SAGE_EXTCODE 

filename = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.dvi') 

with open(filename, 'rb') as f: 

return cls(f.read())