Giter VIP home page Giter VIP logo

svg_path_parser's Introduction

Svg Path Parser

This is an event-based parser for SVG path element data.
Each of the commands in a path string will generate a callback to a handler proc specified at parser creation.

Usage

require 'svg_path_parser'

on_move = lambda{ |vo| puts "Hey!  on_move received"}
on_line = lambda{|vo| puts "Hey!  on_line received"}
on_curve  = lambda{|vo| puts "Hey!  on_curve received"}
on_quadratic = lambda{|vo| puts "Hey!  on_quadratic received"}
on_arc = lambda{|vo| puts "Hey!  on_arc received"}
on_close = lambda{|vo| puts "Hey!  on_close received"}

parser = SvgPathParser::Impl.new :on_move => on_move,
                                 :on_line => on_line,
                                 :on_curve => on_curve,
                                 :on_quadratic => on_quadratic,
                                 :on_arc => on_arc,
                                 :on_close => on_close
parser.parse(nil, "M5 5L10 10C15 15 0 0 20 20Q10 10 7 7A30 50 -45 1 1 30 30Z")
Hey!  on_move received
Hey!  on_line received
Hey!  on_curve received
Hey!  on_quadratic received
Hey!  on_arc received
Hey!  on_close received
 => nil

You only need to specify callbacks for the path commands you are interested in. All callbacks have default do-nothing lambdas that are invoked in response to the various commands.

The context object passed as the first argument is used to collect results of the parsing and needs to be modified by the callbacks to produce a meaningful result of the parsing. It is roughly analogous to a memo for the Array#inject method. It is returned as the result of the parse call.

Concrete Example

In this example, we will convert curves to lines in an SVG path, but otherwise keep the path the same.

require 'svg_path_parser'

convert_to_line = lambda do |vo|
  # a really bad conversion algorithm
  vo.context << "L#{vo.dest_pt[0]} #{vo.dest_pt[1]}"
end

dupe_command = lambda do |vo|
  vo.context << vo.command_str
end

parser = SvgPathParser::Impl.new :on_move => dupe_command,
                                 :on_line => dupe_command,
                                 :on_curve => convert_to_line,
                                 :on_quadratic => convert_to_line,
                                 :on_arc => convert_to_line,
                                 :on_close => dupe_command
parser.parse("", "M5 5L10 10C15 15 0 0 20 20Q10 10 7 7A30 50 -45 1 1 30 30Z")
 => "M5 5L10 10L15.0 15.0L10.0 10.0L30.0 30.0Z"

Events

The argument lists for the callbacks can be pretty large, so I extracted them into events as follows:

DestinationEvent = Struct.new :command_str, :context, :current_pt, :dest_pt
CurveEvent = Struct.new :command_str, :context, :current_pt, :dest_pt, :control_1, :control_2
QuadraticEvent = Struct.new :command_str, :context, :current_pt, :dest_pt, :control
ArcEvent = Struct.new :command_str, :context, :current_pt, :x_radius, :y_radius, :x_rotation, :large_arc, :sweep, :dest_pt
CloseEvent = Struct.new :command_str, :context, :current_pt, :first_pt

DestinationEvent is used for both move and line commands.

Issues

  • This currently does not support the horizontal/vertical (H/V) line shortcuts.
  • This currently does not support the shortcuts for stringing together sets of bezier or quadratic curves (S/T).

Both of these should be easy to implement.

svg_path_parser's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.