Show More
@@ -45,6 +45,7 b' def latex_to_png(s, encode=False):' | |||||
45 | bin_data = encodestring(bin_data) |
|
45 | bin_data = encodestring(bin_data) | |
46 | return bin_data |
|
46 | return bin_data | |
47 |
|
47 | |||
|
48 | ||||
48 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" |
|
49 | _data_uri_template_png = """<img src="data:image/png;base64,%s" alt=%s />""" | |
49 |
|
50 | |||
50 | def latex_to_html(s, alt='image'): |
|
51 | def latex_to_html(s, alt='image'): | |
@@ -60,3 +61,50 b" def latex_to_html(s, alt='image'):" | |||||
60 | base64_data = latex_to_png(s, encode=True) |
|
61 | base64_data = latex_to_png(s, encode=True) | |
61 | return _data_uri_template_png % (base64_data, alt) |
|
62 | return _data_uri_template_png % (base64_data, alt) | |
62 |
|
63 | |||
|
64 | ||||
|
65 | # From matplotlib, thanks to mdboom. Once this is in matplotlib releases, we | |||
|
66 | # will remove. | |||
|
67 | def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None): | |||
|
68 | """ | |||
|
69 | Given a math expression, renders it in a closely-clipped bounding | |||
|
70 | box to an image file. | |||
|
71 | ||||
|
72 | *s* | |||
|
73 | A math expression. The math portion should be enclosed in | |||
|
74 | dollar signs. | |||
|
75 | ||||
|
76 | *filename_or_obj* | |||
|
77 | A filepath or writable file-like object to write the image data | |||
|
78 | to. | |||
|
79 | ||||
|
80 | *prop* | |||
|
81 | If provided, a FontProperties() object describing the size and | |||
|
82 | style of the text. | |||
|
83 | ||||
|
84 | *dpi* | |||
|
85 | Override the output dpi, otherwise use the default associated | |||
|
86 | with the output format. | |||
|
87 | ||||
|
88 | *format* | |||
|
89 | The output format, eg. 'svg', 'pdf', 'ps' or 'png'. If not | |||
|
90 | provided, will be deduced from the filename. | |||
|
91 | """ | |||
|
92 | from matplotlib import figure | |||
|
93 | # backend_agg supports all of the core output formats | |||
|
94 | from matplotlib.backends import backend_agg | |||
|
95 | from matplotlib.font_manager import FontProperties | |||
|
96 | from matplotlib.mathtext import MathTextParser | |||
|
97 | ||||
|
98 | if prop is None: | |||
|
99 | prop = FontProperties() | |||
|
100 | ||||
|
101 | parser = MathTextParser('path') | |||
|
102 | width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop) | |||
|
103 | ||||
|
104 | fig = figure.Figure(figsize=(width / 72.0, height / 72.0)) | |||
|
105 | fig.text(0, depth/height, s, fontproperties=prop) | |||
|
106 | backend_agg.FigureCanvasAgg(fig) | |||
|
107 | fig.savefig(filename_or_obj, dpi=dpi, format=format) | |||
|
108 | ||||
|
109 | return depth | |||
|
110 |
General Comments 0
You need to be logged in to leave comments.
Login now