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



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

def initialize(plugin)
  super()
  @plugin = plugin
end

Instance Attribute Details

#pluginObject

Returns the value of attribute plugin.



13
14
15
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 13

def plugin
  @plugin
end

Class Method Details

.concatPath(s1, s2) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 272

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



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

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



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

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="+StringTools::urlEncode(text).to_s
    else 
      PropertiesTools::setProperty(output,"alt",text)
    end
  end
  rparam = ""
  if provider::getParameter("refererquery",nil)!=nil
    refererquery = provider::getParameter("refererquery","")
    rparam = "&refererquery="+refererquery
  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 (((self.class.concatPath(contextPath,showImagePath)+StringTools::urlEncode(digest).to_s)+s)+a)+rparam
  end
end

#getEditorParametersListObject



196
197
198
199
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 196

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

#getMathml(digest) ⇒ Object



193
194
195
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 193

def getMathml(digest)
  return nil
end

#getMetrics(digest, ref_output) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 200

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



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

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



266
267
268
269
270
271
# File 'lib/com/wiris/plugin/impl/RenderImpl.rb', line 266

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

#showImage(digest, mml, provider) ⇒ Object



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

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

#showImageJson(digest, lang) ⇒ Object



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

def showImageJson(digest,lang)
  imageFormat = @plugin::getConfiguration()::getProperty("wirisimageformat","png")
  store = @plugin::getStorageAndCache()
  bs = nil
  bs = store::retreiveData(digest,imageFormat)
  jsonOutput = Hash.new()
  if bs!=nil
    jsonOutput::set("status","ok")
    jsonResult = Hash.new()
    by = Bytes::ofData(bs)
    b64 = Base64.new()::encodeBytes(by)
    metrics = PropertiesTools::newProperties()
    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
      jsonResult::set("alt",Utf8::fromBytes(s))
    end
    jsonResult::set("base64",b64::toString())
    jsonResult::set("format",imageFormat)
    jsonOutput::set("result",jsonResult)
  else 
    jsonOutput::set("status","warning")
  end
  return JSon::encode(jsonOutput)
end