Class: WirisPlugin::RenderImpl

Inherits:
Object
  • Object
show all
Extended by:
RenderInterface
Includes:
Wiris
Defined in:
lib/com/wiris/plugin/impl/RenderImpl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderInterface

Render

Constructor Details

#initialize(plugin) ⇒ RenderImpl

Returns a new instance of RenderImpl.



16
17
18
19
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 16

def initialize(plugin)
    super()
    self.plugin = plugin
end

Instance Attribute Details

#pluginObject

Returns the value of attribute plugin.



15
16
17
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 15

def plugin
  @plugin
end

Class Method Details

.concatPath(s1, s2) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 317

def self.concatPath(s1, s2)
    if s1::lastIndexOf("/") == (s1::length() - 1)
        return s1 + s2
    else 
        return (s1 + "/") + s2
    end
end

Instance Method Details

#computeDigest(mml, param) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 20

def computeDigest(mml, param)
    ss = getEditorParametersList()
    renderParams = Hash.new()
    for i in 0..ss::length - 1
        key = ss[i]
        value = PropertiesTools::getProperty(param,key)
        if value != nil
            renderParams::set(key,value)
        end
        i+=1
    end
    if mml != nil
        renderParams::set("mml",mml)
    end
    s = IniFile::propertiesToString(renderParams)
    return @plugin::getStorageAndCache()::codeDigest(s)
end

#createImage(mml, provider, ref_output) ⇒ Object



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
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 37

def createImage(mml, provider, ref_output)
    output = ref_output
    if mml == nil
        raise Exception,"Missing parameter \'mml\'."
    end
    digest = computeDigest(mml,provider::getRenderParameters(@plugin::getConfiguration()))
    contextPath = @plugin::getConfiguration()::getProperty(ConfigurationKeys::CONTEXT_PATH,"/")
    showImagePath = @plugin::getConfiguration()::getProperty(ConfigurationKeys::SHOWIMAGE_PATH,nil)
    saveMode = @plugin::getConfiguration()::getProperty(ConfigurationKeys::SAVE_MODE,"xml")
    s = ""
    if (provider::getParameter("metrics","false") == "true")
        s = getMetrics(digest,output)
    end
    a = ""
    if (provider::getParameter("accessible","false") == "true")
        lang = provider::getParameter("lang","en")
        text = safeMath2Accessible(mml,lang,provider::getParameters())
        if output == nil
            a = "&text=" + UrlUtils::urlComponentEncode(text)
        else 
            PropertiesTools::setProperty(output,"alt",text)
        end
    end
    rparam = ""
    if provider::getParameter("refererquery",nil) != nil
        refererquery = provider::getParameter("refererquery","")
        rparam = "&refererquery=" + refererquery
    end
    if output != nil
        PropertiesTools::setProperty(output,"role","math")
    end
    if (provider::getParameter("base64",nil) != nil) || (saveMode == "base64")
        bs = showImage(digest,nil,provider)
        by = Bytes::ofData(bs)
        b64 = Base64.new()::encodeBytes(by)
        imageContentType = @plugin::getImageFormatController()::getContentType()
        return (("data:" + imageContentType) + ";base64,") + b64::toString().to_s
    else 
        return (((RenderImpl.concatPath(contextPath,showImagePath) + UrlUtils::urlComponentEncode(digest)) + s) + a) + rparam
    end
end

#getEditorParametersListObject



241
242
243
244
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 241

def getEditorParametersList()
    pl = @plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_PARAMETERS_LIST,ConfigurationKeys::EDITOR_PARAMETERS_DEFAULT_LIST)
    return pl::split(",")
end

#getMathml(digest) ⇒ Object



238
239
240
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 238

def getMathml(digest)
    return nil
end

#getMetrics(digest, ref_output) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 245

def getMetrics(digest, ref_output)
    output = ref_output
    begin
    bs = showImage(digest,nil,nil)
    end
    b = Bytes::ofData(bs)
    return @plugin::getImageFormatController()::getMetrics(b,output)
end

#getMetricsFromBytes(bs, ref_output) ⇒ Object



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
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 253

def getMetricsFromBytes(bs, ref_output)
    output = ref_output
    width = 0
    height = 0
    dpi = 0
    baseline = 0
    bys = Bytes::ofData(bs)
    bi = BytesInput.new(bys)
    n = bys::length()
    alloc = 10
    b = Bytes::alloc(alloc)
    bi::readBytes(b,0,8)
    n -= 8
    while n > 0
        len = bi::readInt32()
        typ = bi::readInt32()
        if typ == 1229472850
            width = bi::readInt32()
            height = bi::readInt32()
            bi::readInt32()
            bi::readByte()
        else 
            if typ == 1650545477
                baseline = bi::readInt32()
            else 
                if typ == 1883789683
                    dpi = bi::readInt32()
                    dpi = (Math::round(dpi/39.37))
                    bi::readInt32()
                    bi::readByte()
                else 
                    if len > alloc
                        alloc = len
                        b = Bytes::alloc(alloc)
                    end
                    bi::readBytes(b,0,len)
                end
            end
        end
        bi::readInt32()
        n -= len + 12
    end
    if output != nil
        PropertiesTools::setProperty(output,"width","" + width.to_s)
        PropertiesTools::setProperty(output,"height","" + height.to_s)
        PropertiesTools::setProperty(output,"baseline","" + baseline.to_s)
        if dpi != 96
            PropertiesTools::setProperty(output,"dpi","" + dpi.to_s)
        end
        r = ""
    else 
        r = (((("&cw=" + width.to_s) + "&ch=") + height.to_s) + "&cb=") + baseline.to_s
        if dpi != 96
            r = (r + "&dpi=") + dpi.to_s
        end
    end
    return r
end

#safeMath2Accessible(mml, lang, param) ⇒ Object



311
312
313
314
315
316
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 311

def safeMath2Accessible(mml, lang, param)
    begin
    text = @plugin::newTextService()::mathml2accessible(mml,lang,param)
    return text
    end
end

#showImage(digest, mml, provider) ⇒ Object



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
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 78

def showImage(digest, mml, provider)
    if (digest == nil) && (mml == nil)
        raise Exception,"Missing parameters \'formula\' or \'mml\'."
    end
    if (digest != nil) && (mml != nil)
        raise Exception,"Only one parameter \'formula\' or \'mml\' is valid."
    end
    atts = false
    if ((digest == nil) && (mml != nil)) && (provider != nil)
        digest = computeDigest(mml,provider::getRenderParameters(@plugin::getConfiguration()))
    end
    formula = @plugin::getStorageAndCache()::decodeDigest(digest)
    if formula == nil
        raise Exception,"Formula associated to digest not found."
    end
    if formula::startsWith("<")
        raise Exception,"Not implemented."
    end
    iniFile = IniFile::newIniFileFromString(formula)
    renderParams = iniFile::getProperties()
    ss = getEditorParametersList()
    if provider != nil
        renderParameters = provider::getRenderParameters(@plugin::getConfiguration())
        for i in 0..ss::length - 1
            key = ss[i]
            value = PropertiesTools::getProperty(renderParameters,key)
            if value != nil
                atts = true
                renderParams::set(key,value)
            end
            i+=1
        end
    end
    if atts
        if mml != nil
            digest = computeDigest(mml,PropertiesTools::toProperties(renderParams))
        else 
            digest = computeDigest(renderParams::get("mml"),PropertiesTools::toProperties(renderParams))
        end
    end
    store = @plugin::getStorageAndCache()
    bs = nil
    bs = store::retreiveData(digest,@plugin::getConfiguration()::getProperty("wirisimageformat","png"))
    if bs == nil
        if @plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_PARAMS,nil) != nil
            json = JSon::decode(@plugin::getConfiguration()::getProperty(ConfigurationKeys::EDITOR_PARAMS,nil))
            decodedHash = (json)
            keys = decodedHash::keys()
            notAllowedParams = Std::split(ConfigurationKeys::EDITOR_PARAMETERS_NOTRENDER_LIST,",")
            while keys::hasNext()
                key = keys::next()
                if !notAllowedParams::contains_(key)
                    renderParams::set(key,(decodedHash::get(key)))
                end
            end
        else 
            for i in 0..ss::length - 1
                key = ss[i]
                if !renderParams::exists(key)
                    confKey = ConfigurationKeys::imageConfigProperties::get(key)
                    if confKey != nil
                        value = @plugin::getConfiguration()::getProperty(confKey,nil)
                        if value != nil
                            renderParams::set(key,value)
                        end
                    end
                end
                i+=1
            end
        end
        renderParams::set("format",@plugin::getConfiguration()::getProperty("wirisimageformat","png"))
        h = HttpImpl.new(@plugin::getImageServiceURL(nil,true),nil)
        @plugin::addReferer(h)
        @plugin::addProxy(h)
        iter = renderParams::keys()
        while iter::hasNext()
            key = iter::next()
            h::setParameter(key,renderParams::get(key))
        end
        h::request(true)
        b = Bytes::ofString(h::getData())
        store::storeData(digest,@plugin::getConfiguration()::getProperty("wirisimageformat","png"),b::getData())
        bs = b::getData()
    end
    return bs
end

#showImageHash(digest, lang) ⇒ Object



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
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 189

def showImageHash(digest, lang)
    imageFormat = @plugin::getConfiguration()::getProperty("wirisimageformat","png")
    store = @plugin::getStorageAndCache()
    bs = nil
    bs = store::retreiveData(digest,imageFormat)
    if bs != nil
        jsonResult = Hash.new()
        content = Bytes::ofData(bs)
        if (imageFormat == "png")
            content = Base64.new()::encodeBytes(content)
        end
        metrics = PropertiesTools::newProperties()
        self.getMetrics(digest,metrics)
        if lang == nil
            lang = "en"
        end
        s = store::retreiveData(digest,lang)
        hashMetrics = PropertiesTools::fromProperties(metrics)
        keys = hashMetrics::keys()
        while keys::hasNext()
            currentKey = keys::next()
            jsonResult::set(currentKey,hashMetrics::get(currentKey))
        end
        if s != nil
            cachedServiceText = Utf8::fromBytes(s)
            b = StringTools::endsWith(cachedServiceText,"}") && StringTools::startsWith(cachedServiceText,"{")
            if b
                decodedJson = (JSon::decode(cachedServiceText))
                jsonErrors = (decodedJson::get("errors"))
                if jsonErrors == nil
                    result = (decodedJson::get("result"))
                    if result != nil
                        jsonResult::set("alt",(result::get("text")))
                    end
                else 
                    jsonResult::set("alt","Error converting from MathML to accessible text.")
                end
            else 
                jsonResult::set("alt",cachedServiceText)
            end
        end
        jsonResult::set("content",content::toString())
        jsonResult::set("format",imageFormat)
        jsonResult::set("role","math")
        return jsonResult
    else 
        return nil
    end
end

#showImageJson(digest, lang) ⇒ Object



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
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 164

def showImageJson(digest, lang)
    jsonResult = self.showImageHash(digest,lang)
    if jsonResult != nil
        jsonSb = StringBuf.new()
        iter = jsonResult::keys()
        jsonSb::add("{\"status\":\"ok\",\"result\":{")
        while iter::hasNext()
            key = (iter::next())
            value = (jsonResult::get(key))
            value = StringTools::replace(value,"\\","\\\\")
            value = StringTools::replace(value,"\"","\\\"")
            value = StringTools::replace(value,"\r","\\r")
            value = StringTools::replace(value,"\n","\\n")
            value = StringTools::replace(value,"\t","\\t")
            jsonSb::add((((("\"" + key) + "\":") + "\"") + value) + "\"")
            if iter::hasNext()
                jsonSb::add(",")
            end
        end
        jsonSb::add("}}")
        return jsonSb::toString()
    else 
        return "{\"status\":\"warning\"}"
    end
end