Class: Inkmake::InkFile

Inherits:
Object
  • Object
show all
Defined in:
lib/inkmake.rb

Defined Under Namespace

Classes: ProcessError, SyntaxError

Constant Summary collapse

DefaultVariants =
{
  "@2x" => {:scale => 2.0}
}
Rotations =
{
  "right" => 90,
  "left" => -90,
  "upsidedown" => 180
}
RES_RE =

123x123, 12.3cm*12.3cm

/^(\d+(?:\.\d+)?(?:px|pt|pc|mm|cm|dm|m|in|ft|uu)?)[x*](\d+(?:\.\d+)?(?:px|pt|pc|mm|cm|dm|m|in|ft|uu)?)$/
SCALE_RE =

*123, *1.23

/^\*(\d+(?:\.\d+)?)$/
DPI_RE =

180dpi

/^(\d+(?:\.\d+)?)dpi$/i
DEST_RE =

(prefix)(suffix)

/^([^\[]*)(?:\[(.*)\])?(.*)$/
SVG_RE =

test.svg, test.SVG

/\.svg$/i
EXT_RE =

ext to format, supported inkscape output formats

/\.(png|pdf|ps|eps)$/i
FORMAT_RE =

supported inkscape output formats

/^(png|pdf|ps|eps)$/i
AREA_NAME_RE =
/^@(.*)$/
AREA_SPEC_RE =

@x:y:w:h

/^@(\d+(?:\.\d+)?):(\d+(?:\.\d+)?):(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/
ROTATE_RE =

right, left, upsidedown

/^(right|left|upsidedown)$/
SHOWHIDE_RE =

show/hide layer or id, “+Layer 1”, +#id, -*

/^([+-])(.+)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, opts) ⇒ InkFile

Returns a new instance of InkFile.



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
# File 'lib/inkmake.rb', line 333

def initialize(file, opts)
  @file = file
  @images = []
  @force = opts[:force]

  svg_path = nil
  out_path = nil
  File.read(file).lines.each_with_index do |line, index|
    line.strip!
    next if line.empty? or line.start_with? "#"
    begin
      case line
      when /^svg:(.*)/i then svg_path = File.expand_path($1.strip, File.dirname(file))
      when /^out:(.*)/i then out_path = File.expand_path($1.strip, File.dirname(file))
      else
        @images << InkImage.new(self, parse_line(line))
      end
    rescue SyntaxError => e
      puts "#{file}:#{index+1}: #{e.message}"
      exit
    end
  end

  # order is: argument, config in inkfile, inkfile directory
  @svg_path = opts[:svg_path] || svg_path || File.dirname(file)
  @out_path = opts[:out_path] || out_path || File.dirname(file)
end

Instance Attribute Details

#out_pathObject (readonly)

Returns the value of attribute out_path.



295
296
297
# File 'lib/inkmake.rb', line 295

def out_path
  @out_path
end

#svg_pathObject (readonly)

Returns the value of attribute svg_path.



295
296
297
# File 'lib/inkmake.rb', line 295

def svg_path
  @svg_path
end

Instance Method Details

#parse_line(line) ⇒ Object

Raises:



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
# File 'lib/inkmake.rb', line 370

def parse_line(line)
  cols = nil
  begin
    cols = parse_split_line(line)
  rescue CSV::MalformedCSVError => e
    raise SyntaxError, e.message
  end
  raise SyntaxError, "Invalid number of columns" if cols.count < 1

  if not DEST_RE.match(cols[0])
    raise SyntaxError, "Invalid destination format \"#{cols[0]}\""
  end

  opts = {}
  opts[:prefix] = $1
  variants = $2
  opts[:suffix] = $3
  opts[:format] = $1.downcase if EXT_RE.match(opts[:prefix] + opts[:suffix])

  cols[1..-1].each do |col|
    case col
    when RES_RE then opts[:res] = InkscapeResolution.new($1, $2, "px")
    when SVG_RE then opts[:svg] = col
    when AREA_SPEC_RE then opts[:area] = [$1.to_f, $2.to_f, $3.to_f, $4.to_f]
    when AREA_NAME_RE then opts[:area] = $1
    when /^drawing$/ then opts[:area] = :drawing
    when FORMAT_RE then opts[:format] = $1.downcase
    when ROTATE_RE then opts[:rotate] = Rotations[$1]
    when SCALE_RE then opts[:scale] = $1.to_f
    when DPI_RE then opts[:dpi] = $1.to_f
    when SHOWHIDE_RE
      op = $1 == "+" ? :show : :hide
      if $2.start_with? "#"
        type = :id
        name= $2[1..-1]
      else
        type = :layer
        name = $2 == "*" ? :all : $2
      end
      (opts[:showhide] ||= []).push({:op => op, :type => type, :name => name})
    else
      raise SyntaxError, "Unknown column \"#{col}\""
    end
  end

  if not opts[:format]
    raise SyntaxError, "Unknown or no output format could be determined"
  end

  variants = (variants.split("|") if variants) || []
  opts[:variants] = variants.collect do |variant|
    name, options = variant.split("=", 2)
    if options
      options = Hash[
        options.split(",").map do |option|
        case option
        when ROTATE_RE then [:rotate, Rotations[$1]]
        when RES_RE then [:res, InkscapeResolution.new($1, $2, "px")]
        when SCALE_RE then [:scale, $1.to_f]
        when DPI_RE then [:dpi, $1.to_f]
        else
          raise SyntaxError, "Invalid variant option \"#{option}\""
        end
        end
      ]
    else
      options = DefaultVariants[name]
      raise SyntaxError, "Invalid default variant \"#{name}\"" if not options
    end

    [name, options]
  end

  opts
end

#parse_split_line(line) ⇒ Object



361
362
363
364
365
366
367
368
# File 'lib/inkmake.rb', line 361

def parse_split_line(line)
  # changed CSV API in ruby 1.9
  if RUBY_VERSION.start_with? "1.8"
    CSV::parse_line(line, fs = " ")
  else
    CSV::parse_line(line, {:col_sep => " "})
  end
end

#processObject



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/inkmake.rb', line 487

def process
  variants = variants_to_generate
  if variants.empty?
    return false
  end

  idfilemap = {}
  InkscapeRemote.new do |inkscape|
    variants.each do |variant|
      if not File.exists? variant.image.svg_path
        raise ProcessError, "Source SVG file #{variant.image.svg_path} does not exist"
      end

      out_res = nil
      # order: 200x200, @id/area, svg res
      if variant.image.res
        out_res = variant.image.res
      elsif variant.image.area == :drawing
        res = inkscape.drawing_area(variant.image.svg_path)
        out_res = InkscapeResolution.new(res[2], res[3], "uu")
      elsif variant.image.area
        if variant.image.area.kind_of? String
          if not idfilemap.has_key? variant.image.svg_path
            idfilemap[variant.image.svg_path] = inkscape.ids(variant.image.svg_path)
          end

          if not idfilemap[variant.image.svg_path].has_key? variant.image.area
            raise ProcessError, "Unknown id \"#{variant.image.area}\" in file #{variant.image.svg_path} when exporting #{variant.out_path}"
          end

          res = idfilemap[variant.image.svg_path][variant.image.area]
          out_res = InkscapeResolution.new(res[2], res[3], "uu")
        else
          a = variant.image.area
          # x0:y0:x1:y1
          out_res = InkscapeResolution.new(a[2]-a[0], a[3]-a[1], "uu")
        end
      else
        out_res = variant.image.svg_res
      end

      scale = variant.options[:scale]
      if scale
        out_res = out_res.scale(scale)
      end

      out_res = variant.options[:res] if variant.options[:res]

      rotate = (variant.image.format == "png" and variant.options[:rotate])

      FileUtils.mkdir_p File.dirname(variant.out_path)

      svg_path = variant.image.svg_path
      if variant.image.showhide
        svg_path = variant.image.svg_showhide_file.path
      end

      res = inkscape.export({
        :svg_path => svg_path,
        :out_path => variant.out_path,
        :res => out_res,
        :dpi => variant.options[:dpi],
        :format => variant.image.format,
        :area => variant.image.area,
        :rotate_scale_hack => rotate
      })

      if rotate
        tmp, width, height = temp_rotate_svg(variant.out_path, rotate, res[0].to_i, res[1].to_i)
        res = inkscape.export({
          :svg_path => tmp.path,
          :out_path => variant.out_path,
          :res => InkscapeResolution.new(width / 2, height / 2, "px"),
          :format => variant.image.format
        })
        tmp.close!
      end

      rel_path = Pathname.new(variant.out_path).relative_path_from(Pathname.new(Dir.pwd))
      if variant.image.format == "png"
        puts "#{rel_path} #{res[0]}x#{res[1]}"
      else
        puts rel_path
      end
    end
  end

  return true
end

#temp_rotate_svg(path, degrees, width, height) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/inkmake.rb', line 465

def temp_rotate_svg(path, degrees, width, height)
  if degrees != 180
    out_width, out_height = height, width
  else
    out_width, out_height = width, height
  end
  svg =
    "<?xml version=\"1.0\"?>" +
    "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"#{out_width}\" height=\"#{out_height}\">" +
    "<g>" +
    "<image transform=\"translate(#{out_width/2} #{out_height/2}) rotate(#{degrees})\"" +
    "  width=\"#{width}\" height=\"#{height}\" x=\"#{-width/2}\" y=\"#{-height/2}\"" +
      "  xlink:href=\"file:///#{URI.escape(path)}\" />" +
    "</g>" +
      "</svg>"
    f = Tempfile.new("inkmake")
    f.write(svg)
    f.flush
    f.seek(0)
    [f, out_width, out_height]
end

#variants_to_generateObject



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/inkmake.rb', line 446

def variants_to_generate
  l = []
  @images.each do |image|
    image.variants.each do |variant|
      next if not @force and
      File.exists? variant.out_path and
      File.mtime(variant.out_path) > File.mtime(image.svg_path) and
      File.mtime(variant.out_path) > File.mtime(@file)
      if variant.out_path == image.svg_path
        raise ProcessError, "Avoiding overwriting source SVG file #{image.svg_path}"
      end

      l << variant
    end
  end

  l
end