This file is indexed.

/usr/share/ngraph-gtk/addin/fig2gra.rb is in ngraph-gtk-addin-import-ps 6.06.13-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
class Spline
  def initialize(pos)
    @n = 0
    @x = []
    @y = []
    @t = []

    (pos.size/ 2).times { |i|
      next if (i > 0 && @x[-1] == pos[i * 2] && @y[-1] == pos[i * 2 + 1])

      @x.push(pos[i * 2])
      @y.push(pos[i * 2 + 1])

      if (i == 0)
        @t[i] = 0
      elsif (@x.size > 1)
        t1 = @x[-1] - @x[-2]
        t2 = @y[-1] - @y[-2]
        @t.push(@t.last + Math.sqrt(t1 ** 2 + t2 ** 2))
      end
    }

    @n = @x.size

    (1 .. @n - 1).each { |i|
      @t[i] /= @t[@n - 1]
    }

    @xz = []
    @yz = []

    maketable(@t, @x, @xz)
    maketable(@t, @y, @yz)
  end

  def maketable(x, y, z)
    h = []
    d = []

    z[0] = 0
    z[@n - 1] = 0
    (@n - 1).times { |i|
      h[i    ] =  x[i + 1] - x[i]
      d[i + 1] = (y[i + 1] - y[i]) / h[i].to_f
    }

    z[1] = d[2] - d[1] - h[0] * z[0]
    d[1] = 2 * (x[2] - x[0])

    (1 .. @n - 3).each { |i|
      t = h[i] / d[i].to_f
      z[i + 1] = d[i + 2] - d[i + 1] - z[i] * t
      d[i + 1] = 2 * (x[i + 2] - x[i]) - h[i] * t
    }
    z[@n - 2] -= h[@n - 2] * z[@n - 1]
    (@n - 2).downto(1) { |i|
      z[i] = (z[i] - h[i] * z[i + 1]) / d[i].to_f
    }
  end

  def spline_sub(x, y, z, t)
    i = 0
    j = @n - 1
    while (i < j)
      k = (i + j) / 2
      if (x[k] < t)
        i = k + 1
      else
        j = k
      end
    end
    i -= 1 if (i > 0)
    h = x[i + 1] - x[i]
    d = t - x[i]
    return (((z[i + 1] - z[i]) * d / h.to_f + z[i] * 3) * d + ((y[i + 1] - y[i]) / h.to_f - (z[i] * 2 + z[i + 1]) * h)) * d + y[i]
  end

  def spline(t)
    x = spline_sub(@t, @x, @xz, t)
    y = spline_sub(@t, @y, @yz, t)
    return [x, y]
  end
end

class Fig2Gra
  SPLINE_DIV = 10

  PS_FONT = [
             ["Serif", 0], #       0      Times Roman
             ["Serif", 2], #       1      Times Italic
             ["Serif", 1], #       2      Times Bold
             ["Serif", 3], #       3      Times Bold Italic
             ["Sans-serif", 0], #  4      AvantGarde Book
             ["Sans-serif", 2], #  5      AvantGarde Book Oblique
             ["Sans-serif", 1], #  6      AvantGarde Demi
             ["Sans-serif", 3], #  7      AvantGarde Demi Oblique
             ["Serif", 0], #       8      Bookman Light
             ["Serif", 2], #       9      Bookman Light Italic
             ["Serif", 1], #      10      Bookman Demi
             ["Serif", 3], #      11      Bookman Demi Italic
             ["Monospace", 0], #  12      Courier
             ["Monospace", 2], #  13      Courier Oblique
             ["Monospace", 1], #  14      Courier Bold
             ["Monospace", 3], #  15      Courier Bold Oblique
             ["Sans-serif", 0], # 16      Helvetica
             ["Sans-serif", 2], # 17      Helvetica Oblique
             ["Sans-serif", 1], # 18      Helvetica Bold
             ["Sans-serif", 3], # 19      Helvetica Bold Oblique
             ["Sans-serif", 0], # 20      Helvetica Narrow
             ["Sans-serif", 2], # 21      Helvetica Narrow Oblique
             ["Sans-serif", 1], # 22      Helvetica Narrow Bold
             ["Sans-serif", 3], # 23      Helvetica Narrow Bold Oblique
             ["Serif", 0], #      24      New Century Schoolbook Roman
             ["Serif", 2], #      25      New Century Schoolbook Italic
             ["Serif", 1], #      26      New Century Schoolbook Bold
             ["Serif", 3], #      27      New Century Schoolbook Bold Italic
             ["Serif", 0], #      28      Palatino Roman
             ["Serif", 2], #      29      Palatino Italic
             ["Serif", 1], #      30      Palatino Bold
             ["Serif", 3], #      31      Palatino Bold Italic
             ["Serif", 0], #      32      Symbol
             ["Serif", 2], #      33      Zapf Chancery Medium Italic
             ["Serif", 0], #      34      Zapf Dingbats
             nil
            ]

  LATEX_FONT = [
                nil, #               0      Default font
                ["Serif", 0], #      1      Roman
                ["Serif", 1], #      2      Bold
                ["Serif", 2], #      3      Italic
                ["Sans-serif", 0], # 4      Sans Serif
                ["Monospace", 3],  # 5      Typewriter
               ]

  PAPER_SIZE = {
    "Letter"  => [21600,  27900],
    "Legal"   => [21600,  35600],
    "Ledger"  => [43200,  27900],
    "Tabloid" => [27900,  43200],
    "A"       => [22900,  30500],
    "B"       => [30500,  45700],
    "C"       => [45700,  61000],
    "D"       => [61000,  91400],
    "E"       => [91400, 121900],
    "A4"      => [21000,  29700],
    "A3"      => [29700,  42000],
    "A2"      => [42000,  59400],
    "A1"      => [59400,  84100],
    "A0"      => [84100, 118900],
    "B5"      => [17600,  25000],
  }

  COLOR = [
           [0, 0, 0],
           [0, 0, 0xff],
           [0, 0xff, 0],
           [0, 0xff, 0xff],
           [0xff, 0, 0],
           [0xff, 0, 0xff],
           [0xff, 0xff, 0],
           [0xff, 0xff, 0xff],
           [0, 0, 0x90],
           [0, 0, 0xB0],
           [0, 0, 0xD0],
           [0x87, 0xCE, 0xFF],
           [0, 0x90, 0],
           [0, 0xB0, 0],
           [0, 0xD0, 0],
           [0, 0x90, 0x90],
           [0, 0xB0, 0xB0],
           [0, 0xD0, 0xD0],
           [0x90, 0, 0x90],
           [0xB0, 0, 0xB0],
           [0xD0, 0, 0xD0],
           [0x80, 0x30, 0],
           [0xA0, 0x40, 0],
           [0xC0, 0x60, 0],
           [0xFF, 0x80, 0x80],
           [0xFF, 0xA0, 0xA0],
           [0xFF, 0xC0, 0xC0],
           [0xFF, 0xE0, 0xE0],
           [0xFF, 0xD7, 0xE00],
          ]

  def initialize
    @coord_conv = 1
    @resolution = 1
  end

  def coord_conv(v)
    (v * @coord_conv * 100).to_i
  end

  def unit_conv(v)
    (v * @coord_conv * 100 / @resolution).to_i
  end

  def set_line_attribute(f, type, width, cap, join, len)
    len = coord_conv(len)
    style = [
             [],
             [len, len],
             [50, len],
             [len, 100, 50, 100],
             [len, 100, 50, 100, 50, 100],
             [len, 100, 50, 100, 50, 100, 50, 100],
            ]

    
    n = style[type].size
    f.print("A,#{n + 5},#{n},#{coord_conv(width) + 1},#{cap},#{join},1000")
    if (n > 0)
      f.print(",#{style[type].join(',')}")
    end
    f.print("\n")
  end

  def set_color(f, col)
    return unless (col)
    f.puts("G,4,#{col.join(',')},255")
  end

  def draw_ellipse(f, a)
    return if (a.size != 16)
  end

  def draw_poly_line(f, a, b)
    return if (a.size != 16)

    prm = a.map{|i| i.to_i}

    return if (prm[1] == 5)

    n = prm[15] * 2
    fill = (prm[8] != -1)

    close_path = false
    if (b[0] == b[-2] && b[1] == b[-1])
      n -= 2
      close_path = true
    end

    if (prm[1] != 1 || fill)
      if (fill)
        set_color(f, COLOR[prm[5]])
        f.puts("D,#{n + 2},#{n / 2},1,#{b[0, n].join(',')}")
      end

      if (prm[3] > 0)
        set_color(f, COLOR[prm[4]])
        set_line_attribute(f, prm[2], prm[3] / 80.0, prm[11], prm[10], prm[9]) if (fill)
        f.puts("D,#{n + 2},#{n / 2},0,#{b[0, n].join(',')}")
      end
    else
      set_color(f, COLOR[prm[4]])
      set_line_attribute(f, prm[2], prm[3] / 80.0, prm[11], prm[10], prm[9])
      if (close_path)
        f.puts("D,#{n + 2},#{n / 2},0,#{b[0, n].join(',')}")
      else
        f.puts("R,#{n + 1},#{n / 2},#{b[0, n].join(',')}")
      end
    end
  end

  def uniq_pos(pos)
    n = pos.size / 2
    a = []
    n.times {|i|
      x = pos[i * 2]
      y = pos[i * 2 + 1]
      if (i == 0)
        a.push(x)
        a.push(y)
      else
        if (a[-2] != x || a[-1] != y)
          a.push(x)
          a.push(y)
        end
      end
    }
    return a
  end

  def draw_spline(f, a, b)
    return if (a.size != 14)

    prm = a.map{|i| i.to_i}

    n = prm[13] * 2
    fill = (prm[8] != -1)

    pos = []
    subpath = []

    state = b[n, n / 2]

    state.each_with_index {|v, i|
      if (v == 0) 
        m = subpath.size / 2
        if (m > 2)
          s = Spline.new(subpath)
          (1 .. m * SPLINE_DIV - 1).each { |j|
            x, y = s.spline(j * 1.0 / m / SPLINE_DIV)
            pos.push(x.to_i)
            pos.push(y.to_i)
          }
        elsif (m == 2)
          pos.push(subpath[2])
          pos.push(subpath[3])
        end
        pos.push(b[i * 2])
        pos.push(b[i * 2 + 1])
        subpath.clear
        subpath.push(b[i * 2])
        subpath.push(b[i * 2  + 1])
      else
        if (subpath[-1] != b[i * 2 + 1] || subpath[-2] != b[i * 2])
          subpath.push(b[i * 2])
          subpath.push(b[i * 2  + 1])
        end
      end
    }

    pos = uniq_pos(pos)
    n = pos.size

    close_path = false
    if (pos[0] == pos[-2] && pos[1] == pos[-1])
      n -= 2
      close_path = true
    end

    return if (n < 4)

    if (prm[1] % 2 != 0 || fill)
      if (fill)
        set_color(f, COLOR[prm[5]])
        f.puts("D,#{n + 2},#{n / 2},1,#{pos.join(',')}")
      end

      if (prm[3] > 0)
        set_color(f, COLOR[prm[4]])
        set_line_attribute(f, prm[2], prm[3] / 80.0, prm[10], 2, prm[9]) if (fill)
        f.puts("D,#{n + 2},#{n / 2},0,#{pos.join(',')}")
      end
    else
      set_color(f, COLOR[prm[4]])
      set_line_attribute(f, prm[2], prm[3] / 80.0, prm[10], 2, prm[9])
      if (close_path)
        f.puts("D,#{n + 2},#{n / 2},0,#{pos.join(',')}")
      else
        f.puts("R,#{n + 1},#{n / 2},#{pos.join(',')}")
      end
    end
  end


  def draw_text(f, a)
    return if (a.size != 14)

    prm = a.map{|i| i.to_i}

    flag = prm[8]
    f = prm[5]
    if (flag & 4 == 0)
      font = LATEX_FONT[f]
    else
      font = PS_FONT[f]
    end

    set_color(f, COLOR[prm[2]])

    if (font)
      style = font[1]
      f.puts("F#{font[0]}")
    else
      style = 0
    end

    s = a[13]

    f.puts("H,4,#{prm[6] * 100},0,#{(a[7].to_f / Math::PI * 180 * 100).to_i},#{style}")
    f.puts("M,2,#{unit_conv(prm[11])},#{unit_conv(prm[12])}")
    f.puts("S#{s[0, s.size - 4]}")
  end

  def draw_arc(f, a)
  end

  def convert(fig, gra)
    f = IO.readlines(fig)
    l = f.select {|s| s[0] != ?# } 

    return false if (l.size < 9)

    head = l[0, 8].map {|s| s.chomp}

    @resolution = head[7].split[0].to_i
    @coord_conv = (head[2] == "Metric") ? 1 : 25.4

    psize = PAPER_SIZE[head[3]]
    psize = [21000, 29700] unless (psize)

    File.open(gra, "w") {|f|
      f.puts('%Ngraph GRAF')
      f.puts('%Creator: fig2gra.rb')
      f.puts("I,5,0,0,#{psize.join(',')},10000")
      f.puts("V,5,0,0,#{psize.join(',')},0")

      i = 8
      while (l[i])
        a = l[i].split
        cmd = a[0].to_i

        b = []
        i += 1
        while (l[i] && l[i][0, 1] == "\t")
          b += l[i].split.map {|v| unit_conv(v.to_i)}
          i += 1
        end

        case cmd
        when 0
          s = a[2][1, 6]
          c = [s[0, 2].hex, s[2, 2].hex, s[4, 2].hex]
          COLOR[a[1].to_i] = c
        when 1
          draw_ellipse(f, a)
        when 2
          draw_poly_line(f, a, b)
        when 3
          draw_spline(f, a, b)
        when 4
          draw_text(f, a)
        when 5
          draw_arc(f, a)
        when 6
        end
      end

      f.puts('E,0')
    }
  end
end 

if (__FILE__ == $0)
  exit(1) if (ARGV.size != 2)
  fig2gra = Fig2Gra.new
  fig2gra.convert(ARGV[0], ARGV[1])
end