summaryrefslogtreecommitdiff
path: root/test/test_tex_output_module.py
blob: b3a2180f6511486d1f796e05167aa684a7f70d31 (plain)
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
#!/usr/bin/env python
# coding=utf-8

'''
gMathLib, mathematic expressions handling library

 # pyGMathLib bindings test

Copyright (C) 2007 Guillaume Seguin <guillaume@segu.in>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
'''

import gmathlib
import struct

TEST_PNG_FILE = "test_texrenderer.tex"

if __name__ == "__main__":
    try:
        context = gmathlib.Context ()
        formula = context.Formula ()

        symbol0 = context.Atom ("0")
        symbol1 = context.Atom ("1")
        symbol2 = context.Atom ("2")
        symbol3 = context.Atom ("3")
        symbol10 = context.Atom ("10")
        symbolx = context.Atom ("x")
        symboly = context.Atom ("y")
        symboli = context.Atom ("i")
        symbolinfty = context.Symbol (symtype = "infty")

        sin = context.Symbol (symtype = "fun")
        sin.value = "sin"
        sin.set_child (symbolx, 0)
        frac = context.Symbol (symtype = "frac")
        frac.set_child (sin, 0)
        frac.set_child (symbolx.copy (), 1)
        app = context.Symbol (symtype = "approach")
        app.set_child (symbolx.copy (), 0)
        app.set_child (symbol0, 1)
        lim = context.Symbol (symtype = "lim")
        lim.set_child (app, 0)
        lim.set_child (frac, 1)
        equal = context.Symbol ("2op")
        equal.value = "="
        equal.set_child (lim, 0)
        equal.set_child (symbol1, 1)

        super = context.Symbol (symtype = "super")
        super.set_child (symbol2, 0)
        super.set_child (symbolx.copy (), 1)
        sub = context.Symbol (symtype = "sub")
        sub.set_child (symbol3, 0)
        sub.set_child (symboly, 1)
        sqrt = context.Symbol (symtype = "sqrt")
        sqrt.set_child (sub, 0);
        add = context.Symbol (symtype = "2op")
        add.value = "+"
        add.set_child (super, 0)
        add.set_child (sqrt, 1)
        lparen = context.Symbol (symtype = "lparen")
        lparen.value = "("
        rparen = context.Symbol (symtype = "rparen")
        rparen.value = ")"
        paren = context.Symbol (symtype = "paren")
        paren.set_child (lparen, 0)
        paren.set_child (add, 1)
        paren.set_child (rparen, 2)
        fun = context.Symbol (symtype = "fun")
        fun.value = "sin"
        fun.set_child (paren, 0)
        sqrt2 = context.Symbol (symtype = "sqrt")
        sqrt2.set_child (fun, 0)
        sqrt2.set_child (symboli.copy (), 1)
        frac = context.Symbol (symtype = "frac")
        frac.set_child (sqrt2, 0)
        frac.set_child (symbol0.copy (), 1)
        eq = equal.copy ()
        eq.set_child (symboli, 0)
        eq.set_child (symbol0.copy (), 1)
        lparen = lparen.copy ()
        rparen = rparen.copy ()
        paren = context.Symbol (symtype = "paren")
        paren.set_child (lparen, 0)
        paren.set_child (frac, 1)
        paren.set_child (rparen, 2)
        product = context.Symbol (symtype = "product")
        product.set_child (eq, 0)
        product.set_child (symbol10, 1)
        product.set_child (paren, 2)
        equal2 = equal.copy ()
        equal2.set_child (product, 0)
        equal2.set_child (symbolinfty, 1)

        lcurly = context.Symbol (symtype = "lparen")
        lcurly.value = "{"

        rnone = context.Symbol (symtype = "rparen")
        rnone.value = "."

        array = context.Array (2, 1)
        array.set_child (equal, 0, 0)
        array.set_child (equal2, 1, 0)

        curly = context.Symbol (symtype = "paren")
        curly.set_child (lcurly, 0)
        curly.set_child (array, 1)
        curly.set_child (rnone, 2)

        formula.set_root (curly)
        context.output_module = "tex"
        data = formula.export ()
        if not data:
            raise RuntimeError, "Failed to render formula"
        f = open (TEST_PNG_FILE, "w")
        f.write (data)
        f.close ()
        print "Wrote %s." % TEST_PNG_FILE
    except Exception, e:
        print "Failed at testing pyGMathLib: %s." % e