Merge branch 'devel/master' into tizen
authorseungho <seungho@seungho.tn.corp.samsungelectronics.net>
Wed, 28 Oct 2020 12:25:13 +0000 (21:25 +0900)
committerseungho <seungho@seungho.tn.corp.samsungelectronics.net>
Wed, 28 Oct 2020 12:25:13 +0000 (21:25 +0900)
Change-Id: I1407f4128695558fd76e930e13d310dc3f583222

28 files changed:
automated-tests/README.md
automated-tests/patch-coverage.pl
build/tizen/profiles/android-profile.cmake
build/tizen/profiles/common-profile.cmake
build/tizen/profiles/ivi-profile.cmake
build/tizen/profiles/mobile-profile.cmake
build/tizen/profiles/tv-profile.cmake
build/tizen/profiles/ubuntu-profile.cmake
build/tizen/profiles/wearable-profile.cmake
build/tizen/profiles/windows-profile.cmake
dali/devel-api/adaptor-framework/input-method-context.h
dali/devel-api/adaptor-framework/vector-image-renderer-plugin.h
dali/devel-api/adaptor-framework/vector-image-renderer.cpp
dali/devel-api/adaptor-framework/vector-image-renderer.h
dali/internal/adaptor/common/combined-update-render-controller.cpp
dali/internal/adaptor/common/combined-update-render-controller.h
dali/internal/imaging/common/pixel-manipulation.cpp
dali/internal/vector-image/common/vector-image-renderer-impl.cpp
dali/internal/vector-image/common/vector-image-renderer-impl.h
dali/internal/vector-image/common/vector-image-renderer-plugin-proxy.cpp
dali/internal/vector-image/common/vector-image-renderer-plugin-proxy.h
dali/public-api/dali-adaptor-version.cpp
packaging/dali-adaptor.spec
third-party/file.list
third-party/nanosvg/nanosvg.cc [new file with mode: 0755]
third-party/nanosvg/nanosvg.h [new file with mode: 0644]
third-party/nanosvg/nanosvgrast.cc [new file with mode: 0644]
third-party/nanosvg/nanosvgrast.h [new file with mode: 0644]

index 25376f7..59a7ca7 100644 (file)
@@ -105,16 +105,34 @@ To execute a subset of tests, you can run individual test sets, e.g.
 
     ./execute.sh dali-adaptor
 
-To get coverage output (you need to first build dali libraries with
+To get full coverage output (you need to first build dali libraries with
 --coverage), run
 
     ./coverage.sh
 
+To check the coverage of your patch, (the build server uses its own copy
+of these scripts), you can use
+
+    ./patch-coverage.pl -q [diff-spec]
+
+to get a summary, or
+
+    ./patch-coverage.pl [diff-spec]
+
+to get textual output, or
+
+    ./patch-coverage.pl -o out.html [diff-spec]
+
+to get HTML output (used by build server).
+
+diff-spec is any refspec accepted by git-diff. If it's left out, it creates
+a refspec to the latest commit, or uses the index/working tree.
+
 
 Testing on target
 =================
 
-To build for target, first build and install dali-core, dali-adaptor and dali-toolkit, then build dali-capi without --keep-packs option.
+To build for target, first build and install dali-core, dali-adaptor and dali-toolkit.
 
 You will need to install libconfig-tiny-perl:
 
index c5d9083..3a97dac 100755 (executable)
@@ -19,33 +19,59 @@ use strict;
 use Git;
 use Getopt::Long;
 use Error qw(:try);
-use HTML::Element;
 use Pod::Usage;
 use File::Basename;
-#use Data::Dumper;
 use File::stat;
 use Scalar::Util qw /looks_like_number/;
 use Cwd qw /getcwd/;
 use Term::ANSIColor qw(:constants);
+use Data::Dumper;
 
-# Program to run gcov on files in patch (that are in source dirs - needs to be dali-aware).
+# Dali specific program to run lcov and parse output for files in patch
 
-# A) Get patch
-# B) Remove uninteresting files
-# C) Find matching gcno/gcda files
-# D) Copy and rename them to match source prefix (i.e. strip library name off front)
-# E) Generate patch output with covered/uncovered lines marked in green/red
-# F) Generate coverage data for changed lines
-# G) Exit status should be 0 for high coverage (90% line coverage for all new/changed lines)
+# A) Generate lcov output from lib & test cases
+# B) Get patch using git diff
+# C) Generate patch output with covered/uncovered lines marked in green/red
+# D) Generate coverage data for changed lines
+# E) Exit status should be 0 for high coverage (90% line coverage for all new/changed lines)
 #    or 1 for low coverage
 
 # Sources for conversion of gcno/gcda files:
-# ~/bin/lcov
+# /usr/bin/lcov & genhtml
 # Python git-coverage (From http://stef.thewalter.net/git-coverage-useful-code-coverage.html)
 
+# From genhtml:
+sub read_info_file($);
+sub get_info_entry($);
+sub set_info_entry($$$$$$$$$;$$$$$$);
+sub combine_info_entries($$$);
+sub combine_info_files($$);
+sub compress_brcount($);
+sub brcount_to_db($);
+sub db_to_brcount($;$);
+sub brcount_db_combine($$$);
+sub add_counts($$);
+sub info(@);
+
 our $repo = Git->repository();
 our $debug=0;
 our $pd_debug=0;
+our $root;
+our %info_data; # Hash containing all data from .info files
+
+# Settings from genhtml:
+our $func_coverage;     # If set, generate function coverage statistics
+our $no_func_coverage;  # Disable func_coverage
+our $br_coverage;       # If set, generate branch coverage statistics
+our $no_br_coverage;    # Disable br_coverage
+our $sort = 1;          # If set, provide directory listings with sorted entries
+our $no_sort;           # Disable sort
+
+# Branch data combination types
+our $BR_SUB = 0;
+our $BR_ADD = 1;
+
+# Command line options
 our $opt_cached;
 our $opt_help;
 our $opt_output;
@@ -64,6 +90,793 @@ GetOptions( %longOptions ) or pod2usage(2);
 pod2usage(1) if $opt_help;
 
 
+# From genhtml:
+#
+# read_info_file(info_filename)
+#
+# Read in the contents of the .info file specified by INFO_FILENAME. Data will
+# be returned as a reference to a hash containing the following mappings:
+#
+# %result: for each filename found in file -> \%data
+#
+# %data: "test"    -> \%testdata
+#        "sum"     -> \%sumcount
+#        "func"    -> \%funcdata
+#        "found"   -> $lines_found (number of instrumented lines found in file)
+#        "hit"     -> $lines_hit (number of executed lines in file)
+#        "f_found" -> $fn_found (number of instrumented functions found in file)
+#        "f_hit"   -> $fn_hit (number of executed functions in file)
+#        "b_found" -> $br_found (number of instrumented branches found in file)
+#        "b_hit"   -> $br_hit (number of executed branches in file)
+#        "check"   -> \%checkdata
+#        "testfnc" -> \%testfncdata
+#        "sumfnc"  -> \%sumfnccount
+#        "testbr"  -> \%testbrdata
+#        "sumbr"   -> \%sumbrcount
+#
+# %testdata   : name of test affecting this file -> \%testcount
+# %testfncdata: name of test affecting this file -> \%testfnccount
+# %testbrdata:  name of test affecting this file -> \%testbrcount
+#
+# %testcount   : line number   -> execution count for a single test
+# %testfnccount: function name -> execution count for a single test
+# %testbrcount : line number   -> branch coverage data for a single test
+# %sumcount    : line number   -> execution count for all tests
+# %sumfnccount : function name -> execution count for all tests
+# %sumbrcount  : line number   -> branch coverage data for all tests
+# %funcdata    : function name -> line number
+# %checkdata   : line number   -> checksum of source code line
+# $brdata      : vector of items: block, branch, taken
+#
+# Note that .info file sections referring to the same file and test name
+# will automatically be combined by adding all execution counts.
+#
+# Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
+# is compressed using GZIP. If available, GUNZIP will be used to decompress
+# this file.
+#
+# Die on error.
+#
+sub read_info_file($)
+{
+    my $tracefile = $_[0];      # Name of tracefile
+    my %result;             # Resulting hash: file -> data
+    my $data;           # Data handle for current entry
+    my $testdata;           #       "             "
+    my $testcount;          #       "             "
+    my $sumcount;           #       "             "
+    my $funcdata;           #       "             "
+    my $checkdata;          #       "             "
+    my $testfncdata;
+    my $testfnccount;
+    my $sumfnccount;
+    my $testbrdata;
+    my $testbrcount;
+    my $sumbrcount;
+    my $line;           # Current line read from .info file
+    my $testname;           # Current test name
+    my $filename;           # Current filename
+    my $hitcount;           # Count for lines hit
+    my $count;          # Execution count of current line
+    my $negative;           # If set, warn about negative counts
+    my $changed_testname;       # If set, warn about changed testname
+    my $line_checksum;      # Checksum of current line
+    my $notified_about_relative_paths;
+    local *INFO_HANDLE;         # Filehandle for .info file
+
+    info("Reading data file $tracefile\n");
+
+    # Check if file exists and is readable
+    stat($tracefile);
+    if (!(-r _))
+    {
+        die("ERROR: cannot read file $tracefile!\n");
+    }
+
+    # Check if this is really a plain file
+    if (!(-f _))
+    {
+        die("ERROR: not a plain file: $tracefile!\n");
+    }
+
+    # Check for .gz extension
+    if ($tracefile =~ /\.gz$/)
+    {
+        # Check for availability of GZIP tool
+        system_no_output(1, "gunzip" ,"-h")
+            and die("ERROR: gunzip command not available!\n");
+
+        # Check integrity of compressed file
+        system_no_output(1, "gunzip", "-t", $tracefile)
+            and die("ERROR: integrity check failed for ".
+                    "compressed file $tracefile!\n");
+
+        # Open compressed file
+        open(INFO_HANDLE, "-|", "gunzip -c '$tracefile'")
+            or die("ERROR: cannot start gunzip to decompress ".
+                   "file $tracefile!\n");
+    }
+    else
+    {
+        # Open decompressed file
+        open(INFO_HANDLE, "<", $tracefile)
+            or die("ERROR: cannot read file $tracefile!\n");
+    }
+
+    $testname = "";
+    while (<INFO_HANDLE>)
+    {
+        chomp($_);
+        $line = $_;
+
+        # Switch statement
+        foreach ($line)
+        {
+            /^TN:([^,]*)(,diff)?/ && do
+            {
+                # Test name information found
+                $testname = defined($1) ? $1 : "";
+                if ($testname =~ s/\W/_/g)
+                {
+                    $changed_testname = 1;
+                }
+                $testname .= $2 if (defined($2));
+                last;
+            };
+
+            /^[SK]F:(.*)/ && do
+            {
+                # Filename information found
+                # Retrieve data for new entry
+                $filename = File::Spec->rel2abs($1, $root);
+
+                if (!File::Spec->file_name_is_absolute($1) &&
+                    !$notified_about_relative_paths)
+                {
+                    info("Resolved relative source file ".
+                         "path \"$1\" with CWD to ".
+                         "\"$filename\".\n");
+                    $notified_about_relative_paths = 1;
+                }
+
+                $data = $result{$filename};
+                ($testdata, $sumcount, $funcdata, $checkdata,
+                 $testfncdata, $sumfnccount, $testbrdata,
+                 $sumbrcount) =
+                    get_info_entry($data);
+
+                if (defined($testname))
+                {
+                    $testcount = $testdata->{$testname};
+                    $testfnccount = $testfncdata->{$testname};
+                    $testbrcount = $testbrdata->{$testname};
+                }
+                else
+                {
+                    $testcount = {};
+                    $testfnccount = {};
+                    $testbrcount = {};
+                }
+                last;
+            };
+
+            /^DA:(\d+),(-?\d+)(,[^,\s]+)?/ && do
+            {
+                # Fix negative counts
+                $count = $2 < 0 ? 0 : $2;
+                if ($2 < 0)
+                {
+                    $negative = 1;
+                }
+                # Execution count found, add to structure
+                # Add summary counts
+                $sumcount->{$1} += $count;
+
+                # Add test-specific counts
+                if (defined($testname))
+                {
+                    $testcount->{$1} += $count;
+                }
+
+                # Store line checksum if available
+                if (defined($3))
+                {
+                    $line_checksum = substr($3, 1);
+
+                    # Does it match a previous definition
+                    if (defined($checkdata->{$1}) &&
+                        ($checkdata->{$1} ne
+                         $line_checksum))
+                    {
+                        die("ERROR: checksum mismatch ".
+                            "at $filename:$1\n");
+                    }
+
+                    $checkdata->{$1} = $line_checksum;
+                }
+                last;
+            };
+
+            /^FN:(\d+),([^,]+)/ && do
+            {
+                last if (!$func_coverage);
+
+                # Function data found, add to structure
+                $funcdata->{$2} = $1;
+
+                # Also initialize function call data
+                if (!defined($sumfnccount->{$2})) {
+                    $sumfnccount->{$2} = 0;
+                }
+                if (defined($testname))
+                {
+                    if (!defined($testfnccount->{$2})) {
+                        $testfnccount->{$2} = 0;
+                    }
+                }
+                last;
+            };
+
+            /^FNDA:(\d+),([^,]+)/ && do
+            {
+                last if (!$func_coverage);
+                # Function call count found, add to structure
+                # Add summary counts
+                $sumfnccount->{$2} += $1;
+
+                # Add test-specific counts
+                if (defined($testname))
+                {
+                    $testfnccount->{$2} += $1;
+                }
+                last;
+            };
+
+            /^BRDA:(\d+),(\d+),(\d+),(\d+|-)/ && do {
+                # Branch coverage data found
+                my ($line, $block, $branch, $taken) =
+                    ($1, $2, $3, $4);
+
+                last if (!$br_coverage);
+                $sumbrcount->{$line} .=
+                    "$block,$branch,$taken:";
+
+                # Add test-specific counts
+                if (defined($testname)) {
+                    $testbrcount->{$line} .=
+                        "$block,$branch,$taken:";
+                }
+                last;
+            };
+
+            /^end_of_record/ && do
+            {
+                # Found end of section marker
+                if ($filename)
+                {
+                    # Store current section data
+                    if (defined($testname))
+                    {
+                        $testdata->{$testname} =
+                            $testcount;
+                        $testfncdata->{$testname} =
+                            $testfnccount;
+                        $testbrdata->{$testname} =
+                            $testbrcount;
+                    }
+
+                    set_info_entry($data, $testdata,
+                                   $sumcount, $funcdata,
+                                   $checkdata, $testfncdata,
+                                   $sumfnccount,
+                                   $testbrdata,
+                                   $sumbrcount);
+                    $result{$filename} = $data;
+                    last;
+                }
+            };
+
+            # default
+            last;
+        }
+    }
+    close(INFO_HANDLE);
+
+    # Calculate lines_found and lines_hit for each file
+    foreach $filename (keys(%result))
+    {
+        $data = $result{$filename};
+
+        ($testdata, $sumcount, undef, undef, $testfncdata,
+         $sumfnccount, $testbrdata, $sumbrcount) =
+            get_info_entry($data);
+
+        # Filter out empty files
+        if (scalar(keys(%{$sumcount})) == 0)
+        {
+            delete($result{$filename});
+            next;
+        }
+        # Filter out empty test cases
+        foreach $testname (keys(%{$testdata}))
+        {
+            if (!defined($testdata->{$testname}) ||
+                scalar(keys(%{$testdata->{$testname}})) == 0)
+            {
+                delete($testdata->{$testname});
+                delete($testfncdata->{$testname});
+            }
+        }
+
+        $data->{"found"} = scalar(keys(%{$sumcount}));
+        $hitcount = 0;
+
+        foreach (keys(%{$sumcount}))
+        {
+            if ($sumcount->{$_} > 0) { $hitcount++; }
+        }
+
+        $data->{"hit"} = $hitcount;
+
+        # Get found/hit values for function call data
+        $data->{"f_found"} = scalar(keys(%{$sumfnccount}));
+        $hitcount = 0;
+
+        foreach (keys(%{$sumfnccount})) {
+            if ($sumfnccount->{$_} > 0) {
+                $hitcount++;
+            }
+        }
+        $data->{"f_hit"} = $hitcount;
+
+        # Combine branch data for the same branches
+        (undef, $data->{"b_found"}, $data->{"b_hit"}) = compress_brcount($sumbrcount);
+        foreach $testname (keys(%{$testbrdata})) {
+            compress_brcount($testbrdata->{$testname});
+        }
+    }
+
+    if (scalar(keys(%result)) == 0)
+    {
+        die("ERROR: no valid records found in tracefile $tracefile\n");
+    }
+    if ($negative)
+    {
+        warn("WARNING: negative counts found in tracefile ".
+             "$tracefile\n");
+    }
+    if ($changed_testname)
+    {
+        warn("WARNING: invalid characters removed from testname in ".
+             "tracefile $tracefile\n");
+    }
+
+    return(\%result);
+}
+
+sub print_simplified_info
+{
+    for my $key (keys(%info_data))
+    {
+        print "K $key: \n";
+        my $sumcountref = $info_data{$key}->{"sum"};
+        for my $line (sort{$a<=>$b}(keys(%$sumcountref)))
+        {
+            print "L  $line: $sumcountref->{$line}\n";
+        }
+    }
+}
+
+# From genhtml:
+#
+# get_info_entry(hash_ref)
+#
+# Retrieve data from an entry of the structure generated by read_info_file().
+# Return a list of references to hashes:
+# (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
+#  ref, testfncdata hash ref, sumfnccount hash ref, lines found, lines hit,
+#  functions found, functions hit)
+#
+
+sub get_info_entry($)
+{
+    my $testdata_ref = $_[0]->{"test"};
+    my $sumcount_ref = $_[0]->{"sum"};
+    my $funcdata_ref = $_[0]->{"func"};
+    my $checkdata_ref = $_[0]->{"check"};
+    my $testfncdata = $_[0]->{"testfnc"};
+    my $sumfnccount = $_[0]->{"sumfnc"};
+    my $testbrdata = $_[0]->{"testbr"};
+    my $sumbrcount = $_[0]->{"sumbr"};
+    my $lines_found = $_[0]->{"found"};
+    my $lines_hit = $_[0]->{"hit"};
+    my $fn_found = $_[0]->{"f_found"};
+    my $fn_hit = $_[0]->{"f_hit"};
+    my $br_found = $_[0]->{"b_found"};
+    my $br_hit = $_[0]->{"b_hit"};
+
+    return ($testdata_ref, $sumcount_ref, $funcdata_ref, $checkdata_ref,
+            $testfncdata, $sumfnccount, $testbrdata, $sumbrcount,
+            $lines_found, $lines_hit, $fn_found, $fn_hit,
+            $br_found, $br_hit);
+}
+
+
+# From genhtml:
+#
+# set_info_entry(hash_ref, testdata_ref, sumcount_ref, funcdata_ref,
+#                checkdata_ref, testfncdata_ref, sumfcncount_ref,
+#                testbrdata_ref, sumbrcount_ref[,lines_found,
+#                lines_hit, f_found, f_hit, $b_found, $b_hit])
+#
+# Update the hash referenced by HASH_REF with the provided data references.
+#
+
+sub set_info_entry($$$$$$$$$;$$$$$$)
+{
+    my $data_ref = $_[0];
+
+    $data_ref->{"test"} = $_[1];
+    $data_ref->{"sum"} = $_[2];
+    $data_ref->{"func"} = $_[3];
+    $data_ref->{"check"} = $_[4];
+    $data_ref->{"testfnc"} = $_[5];
+    $data_ref->{"sumfnc"} = $_[6];
+    $data_ref->{"testbr"} = $_[7];
+    $data_ref->{"sumbr"} = $_[8];
+
+    if (defined($_[9])) { $data_ref->{"found"} = $_[9]; }
+    if (defined($_[10])) { $data_ref->{"hit"} = $_[10]; }
+    if (defined($_[11])) { $data_ref->{"f_found"} = $_[11]; }
+    if (defined($_[12])) { $data_ref->{"f_hit"} = $_[12]; }
+    if (defined($_[13])) { $data_ref->{"b_found"} = $_[13]; }
+    if (defined($_[14])) { $data_ref->{"b_hit"} = $_[14]; }
+}
+
+# From genhtml:
+#
+# combine_info_entries(entry_ref1, entry_ref2, filename)
+#
+# Combine .info data entry hashes referenced by ENTRY_REF1 and ENTRY_REF2.
+# Return reference to resulting hash.
+#
+sub combine_info_entries($$$)
+{
+    my $entry1 = $_[0];     # Reference to hash containing first entry
+    my $testdata1;
+    my $sumcount1;
+    my $funcdata1;
+    my $checkdata1;
+    my $testfncdata1;
+    my $sumfnccount1;
+    my $testbrdata1;
+    my $sumbrcount1;
+
+    my $entry2 = $_[1];     # Reference to hash containing second entry
+    my $testdata2;
+    my $sumcount2;
+    my $funcdata2;
+    my $checkdata2;
+    my $testfncdata2;
+    my $sumfnccount2;
+    my $testbrdata2;
+    my $sumbrcount2;
+
+    my %result;         # Hash containing combined entry
+    my %result_testdata;
+    my $result_sumcount = {};
+    my $result_funcdata;
+    my $result_testfncdata;
+    my $result_sumfnccount;
+    my $result_testbrdata;
+    my $result_sumbrcount;
+    my $lines_found;
+    my $lines_hit;
+    my $fn_found;
+    my $fn_hit;
+    my $br_found;
+    my $br_hit;
+
+    my $testname;
+    my $filename = $_[2];
+
+    # Retrieve data
+    ($testdata1, $sumcount1, $funcdata1, $checkdata1, $testfncdata1,
+     $sumfnccount1, $testbrdata1, $sumbrcount1) = get_info_entry($entry1);
+    ($testdata2, $sumcount2, $funcdata2, $checkdata2, $testfncdata2,
+     $sumfnccount2, $testbrdata2, $sumbrcount2) = get_info_entry($entry2);
+
+#    # Merge checksums
+#    $checkdata1 = merge_checksums($checkdata1, $checkdata2, $filename);
+#
+#    # Combine funcdata
+#    $result_funcdata = merge_func_data($funcdata1, $funcdata2, $filename);
+#
+#    # Combine function call count data
+#    $result_testfncdata = add_testfncdata($testfncdata1, $testfncdata2);
+#    ($result_sumfnccount, $fn_found, $fn_hit) =
+#        add_fnccount($sumfnccount1, $sumfnccount2);
+#
+#    # Combine branch coverage data
+#    $result_testbrdata = add_testbrdata($testbrdata1, $testbrdata2);
+#    ($result_sumbrcount, $br_found, $br_hit) =
+#        combine_brcount($sumbrcount1, $sumbrcount2, $BR_ADD);
+#
+    # Combine testdata
+    foreach $testname (keys(%{$testdata1}))
+    {
+        if (defined($testdata2->{$testname}))
+        {
+            # testname is present in both entries, requires
+            # combination
+            ($result_testdata{$testname}) =
+                add_counts($testdata1->{$testname},
+                           $testdata2->{$testname});
+        }
+        else
+        {
+            # testname only present in entry1, add to result
+            $result_testdata{$testname} = $testdata1->{$testname};
+        }
+
+        # update sum count hash
+        ($result_sumcount, $lines_found, $lines_hit) =
+            add_counts($result_sumcount,
+                       $result_testdata{$testname});
+    }
+
+    foreach $testname (keys(%{$testdata2}))
+    {
+        # Skip testnames already covered by previous iteration
+        if (defined($testdata1->{$testname})) { next; }
+
+        # testname only present in entry2, add to result hash
+        $result_testdata{$testname} = $testdata2->{$testname};
+
+        # update sum count hash
+        ($result_sumcount, $lines_found, $lines_hit) =
+            add_counts($result_sumcount,
+                       $result_testdata{$testname});
+    }
+
+    # Calculate resulting sumcount
+
+    # Store result
+    set_info_entry(\%result, \%result_testdata, $result_sumcount,
+                   $result_funcdata, $checkdata1, $result_testfncdata,
+                   $result_sumfnccount, $result_testbrdata,
+                   $result_sumbrcount, $lines_found, $lines_hit,
+                   $fn_found, $fn_hit, $br_found, $br_hit);
+
+    return(\%result);
+}
+
+# From genhtml:
+#
+# combine_info_files(info_ref1, info_ref2)
+#
+# Combine .info data in hashes referenced by INFO_REF1 and INFO_REF2. Return
+# reference to resulting hash.
+#
+sub combine_info_files($$)
+{
+    my %hash1 = %{$_[0]};
+    my %hash2 = %{$_[1]};
+    my $filename;
+
+    foreach $filename (keys(%hash2))
+    {
+        if ($hash1{$filename})
+        {
+            # Entry already exists in hash1, combine them
+            $hash1{$filename} =
+                combine_info_entries($hash1{$filename},
+                                     $hash2{$filename},
+                                     $filename);
+        }
+        else
+        {
+            # Entry is unique in both hashes, simply add to
+            # resulting hash
+            $hash1{$filename} = $hash2{$filename};
+        }
+    }
+
+    return(\%hash1);
+}
+
+# From genhtml:
+#
+# add_counts(data1_ref, data2_ref)
+#
+# DATA1_REF and DATA2_REF are references to hashes containing a mapping
+#
+#   line number -> execution count
+#
+# Return a list (RESULT_REF, LINES_FOUND, LINES_HIT) where RESULT_REF
+# is a reference to a hash containing the combined mapping in which
+# execution counts are added.
+#
+sub add_counts($$)
+{
+    my $data1_ref = $_[0];  # Hash 1
+    my $data2_ref = $_[1];  # Hash 2
+    my %result;             # Resulting hash
+    my $line;               # Current line iteration scalar
+    my $data1_count;        # Count of line in hash1
+    my $data2_count;        # Count of line in hash2
+    my $found = 0;          # Total number of lines found
+    my $hit = 0;            # Number of lines with a count > 0
+
+    foreach $line (keys(%$data1_ref))
+    {
+        $data1_count = $data1_ref->{$line};
+        $data2_count = $data2_ref->{$line};
+
+        # Add counts if present in both hashes
+        if (defined($data2_count)) { $data1_count += $data2_count; }
+
+        # Store sum in %result
+        $result{$line} = $data1_count;
+
+        $found++;
+        if ($data1_count > 0) { $hit++; }
+    }
+
+    # Add lines unique to data2_ref
+    foreach $line (keys(%$data2_ref))
+    {
+        # Skip lines already in data1_ref
+        if (defined($data1_ref->{$line})) { next; }
+
+        # Copy count from data2_ref
+        $result{$line} = $data2_ref->{$line};
+
+        $found++;
+        if ($result{$line} > 0) { $hit++; }
+    }
+
+    return (\%result, $found, $hit);
+}
+
+
+# From genhtml:
+sub compress_brcount($)
+{
+    my ($brcount) = @_;
+    my $db;
+
+    $db = brcount_to_db($brcount);
+    return db_to_brcount($db, $brcount);
+}
+
+#
+# brcount_to_db(brcount)
+#
+# Convert brcount data to the following format:
+#
+# db:          line number    -> block hash
+# block hash:  block number   -> branch hash
+# branch hash: branch number  -> taken value
+#
+
+sub brcount_to_db($)
+{
+    my ($brcount) = @_;
+    my $line;
+    my $db;
+
+    # Add branches to database
+    foreach $line (keys(%{$brcount})) {
+        my $brdata = $brcount->{$line};
+
+        foreach my $entry (split(/:/, $brdata)) {
+            my ($block, $branch, $taken) = split(/,/, $entry);
+            my $old = $db->{$line}->{$block}->{$branch};
+
+            if (!defined($old) || $old eq "-") {
+                $old = $taken;
+            } elsif ($taken ne "-") {
+                $old += $taken;
+            }
+
+            $db->{$line}->{$block}->{$branch} = $old;
+        }
+    }
+
+    return $db;
+}
+
+
+#
+# db_to_brcount(db[, brcount])
+#
+# Convert branch coverage data back to brcount format. If brcount is specified,
+# the converted data is directly inserted in brcount.
+#
+
+sub db_to_brcount($;$)
+{
+    my ($db, $brcount) = @_;
+    my $line;
+    my $br_found = 0;
+    my $br_hit = 0;
+
+    # Convert database back to brcount format
+    foreach $line (sort({$a <=> $b} keys(%{$db}))) {
+        my $ldata = $db->{$line};
+        my $brdata;
+        my $block;
+
+        foreach $block (sort({$a <=> $b} keys(%{$ldata}))) {
+            my $bdata = $ldata->{$block};
+            my $branch;
+
+            foreach $branch (sort({$a <=> $b} keys(%{$bdata}))) {
+                my $taken = $bdata->{$branch};
+
+                $br_found++;
+                $br_hit++ if ($taken ne "-" && $taken > 0);
+                $brdata .= "$block,$branch,$taken:";
+            }
+        }
+        $brcount->{$line} = $brdata;
+    }
+
+    return ($brcount, $br_found, $br_hit);
+}
+
+
+#
+# brcount_db_combine(db1, db2, op)
+#
+# db1 := db1 op db2, where
+#   db1, db2: brcount data as returned by brcount_to_db
+#   op:       one of $BR_ADD and BR_SUB
+#
+sub brcount_db_combine($$$)
+{
+    my ($db1, $db2, $op) = @_;
+
+    foreach my $line (keys(%{$db2})) {
+        my $ldata = $db2->{$line};
+
+        foreach my $block (keys(%{$ldata})) {
+            my $bdata = $ldata->{$block};
+
+            foreach my $branch (keys(%{$bdata})) {
+                my $taken = $bdata->{$branch};
+                my $new = $db1->{$line}->{$block}->{$branch};
+
+                if (!defined($new) || $new eq "-") {
+                    $new = $taken;
+                } elsif ($taken ne "-") {
+                    if ($op == $BR_ADD) {
+                        $new += $taken;
+                    } elsif ($op == $BR_SUB) {
+                        $new -= $taken;
+                        $new = 0 if ($new < 0);
+                    }
+                }
+
+                $db1->{$line}->{$block}->{$branch} = $new;
+            }
+        }
+    }
+}
+
+# From genhtml
+sub info(@)
+{
+    if($debug)
+    {
+        # Print info string
+        printf(@_);
+    }
+}
+
+# NEW STUFF
+
 ## Format per file, repeated, no linebreak
 # <diffcmd>
 # index c1..c2 c3
@@ -78,6 +891,12 @@ pod2usage(1) if $opt_help;
 # 3 lines of context
 #
 # output:
+# <dali-source-file>: source / header files in dali/dali-toolkit
+# \%filter: <dali-source-file> -> \%filedata
+# %filedata: "patch"   -> \@checklines
+#            "b_lines" -> \%b_lines
+# @checklines: vector of \[start, length] # line numbers of new/modified lines
+# %b_lines: <line-number> -> patch line in b-file.
 sub parse_diff
 {
     my $patchref = shift;
@@ -184,7 +1003,7 @@ sub parse_diff
         }
     }
 
-    return {%filter};
+    return {%filter};#copy? - test and fixme
 }
 
 sub show_patch_lines
@@ -203,133 +1022,11 @@ sub show_patch_lines
     }
 }
 
-sub get_gcno_file
-{
-    # Assumes test cases have been run, and "make rename_cov_data" has been executed
-
-    my $file = shift;
-    my ($name, $path, $suffix) = fileparse($file, (".c", ".cpp", ".h"));
-    my $gcno_file = $repo->wc_path() . "/build/tizen/.cov/$name.gcno";
-
-    # Note, will translate headers to their source's object, which
-    # may miss execution code in the headers (e.g. inlines are usually
-    # not all used in the implementation, and require getting coverage
-    # from test cases.
-
-    if( -f $gcno_file )
-    {
-        my $gcno_st = stat($gcno_file);
-        my $fq_file = $repo->wc_path() . $file;
-        my $src_st = stat($fq_file);
-        if($gcno_st->ctime < $src_st->mtime)
-        {
-            print "WARNING: GCNO $gcno_file older than SRC $fq_file\n";
-            $gcno_file="";
-        }
-
-    }
-    else
-    {
-        print("WARNING: No equivalent gcno file for $file\n");
-    }
-    return $gcno_file;
-}
-
-our %gcovfiles=();
-sub get_coverage
-{
-    my $file = shift;
-    my $filesref = shift;
-    print("get_coverage($file)\n") if $debug;
 
-    my $gcno_file = get_gcno_file($file);
-    my @gcov_files = ();
-    my $gcovfile;
-    if( $gcno_file )
-    {
-        print "Running gcov on $gcno_file:\n" if $debug;
-        open( my $fh,  "gcov --preserve-paths $gcno_file |") || die "Can't run gcov:$!\n";
-        while( <$fh> )
-        {
-            print $_ if $debug>=3;
-            chomp;
-            if( m!'(.*\.gcov)'$! )
-            {
-                my $coverage_file = $1; # File has / replaced with # and .. replaced with ^
-                my $source_file = $coverage_file;
-                $source_file =~ s!\^!..!g;  # Change ^ to ..
-                $source_file =~ s!\#!/!g;   # change #'s to /s
-                $source_file =~ s!.gcov$!!; # Strip off .gcov suffix
-
-                print "Matching $file against $source_file\n" if $debug >= 3;
-                # Only want the coverage files matching source file:
-                if(index( $source_file, $file ) > 0 )
-                {
-                    $gcovfile = $coverage_file;
-                    # Some header files do not produce an equivalent gcov file so we shouldn't parse them
-                    if(($source_file =~ /\.h$/) && (! -e $gcovfile))
-                    {
-                        print "Omitting Header: $source_file\n" if $debug;
-                        $gcovfile = ""
-                    }
-                    last;
-                }
-            }
-        }
-        close($fh);
-
-        if($gcovfile)
-        {
-            if($gcovfiles{$gcovfile} == undef)
-            {
-                # Only parse a gcov file once
-                $gcovfiles{$gcovfile}->{"seen"}=1;
-
-                print "Getting coverage data from $gcovfile\n" if $debug;
-
-                open( FH, "< $gcovfile" ) || die "Can't open $gcovfile for reading:$!\n";
-                while(<FH>)
-                {
-                    my ($cov, $line, @code ) = split( /:/, $_ );
-                    $cov =~ s/^\s+//; # Strip leading space
-                    $line =~ s/^\s+//;
-                    my $code=join(":", @code);
-                    if($cov =~ /\#/)
-                    {
-                        # There is no coverage data for these executable lines
-                        $gcovfiles{$gcovfile}->{"uncovered"}->{$line}++;
-                        $gcovfiles{$gcovfile}->{"src"}->{$line}=$code;
-                    }
-                    elsif( $cov ne "-" && looks_like_number($cov) && $cov > 0 )
-                    {
-                        $gcovfiles{$gcovfile}->{"covered"}->{$line}=$cov;
-                        $gcovfiles{$gcovfile}->{"src"}->{$line}=$code;
-                    }
-                    else
-                    {
-                        # All other lines are not executable.
-                        $gcovfiles{$gcovfile}->{"src"}->{$line}=$code;
-                    }
-                }
-                close( FH );
-            }
-            $filesref->{$file}->{"coverage"} = $gcovfiles{$gcovfile}; # store hashref
-        }
-        else
-        {
-            # No gcov output - the gcno file produced no coverage of the source/header
-            # Probably means that there is no coverage for the file (with the given
-            # test case - there may be some somewhere, but for the sake of speed, don't
-            # check (yet).
-        }
-    }
-}
-
-# Run the git diff command to get the patch, then check the coverage
-# output for the patch.
+# Run the git diff command to get the patch
+# Output - see parse_diff
 sub run_diff
 {
-    #print "run_diff(" . join(" ", @_) . ")\n";
     my ($fh, $c) = $repo->command_output_pipe(@_);
     our @patch=();
     while(<$fh>)
@@ -343,23 +1040,8 @@ sub run_diff
 
     # @patch has slurped diff for all files...
     my $filesref = parse_diff ( \@patch );
-    show_patch_lines($filesref) if $debug;
-
-    print "Checking coverage:\n" if $debug;
-
-    my $cwd=getcwd();
-    chdir ".cov" || die "Can't find $cwd/.cov:$!\n";
+    show_patch_lines($filesref) if $debug>1;
 
-    for my $file (keys(%$filesref))
-    {
-        my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
-        next if($path !~ /^dali/);
-        if($suffix eq ".cpp" || $suffix eq ".c" || $suffix eq ".h")
-        {
-            get_coverage($file, $filesref);
-        }
-    }
-    chdir $cwd;
     return $filesref;
 }
 
@@ -378,35 +1060,45 @@ sub calc_patch_coverage_percentage
         my $uncovered_lines = 0;
 
         my $patchref = $filesref->{$file}->{"patch"};
-        my $coverage_ref = $filesref->{$file}->{"coverage"};
-        if( $coverage_ref )
+
+        my $abs_filename = File::Spec->rel2abs($file, $root);
+        my $sumcountref = $info_data{$abs_filename}->{"sum"};
+
+        if( $sumcountref )
         {
             for my $patch (@$patchref)
             {
                 for(my $i = 0; $i < $patch->[1]; $i++ )
                 {
                     my $line = $i + $patch->[0];
-                    if($coverage_ref->{"covered"}->{$line})
-                    {
-                        $covered_lines++;
-                        $total_covered_lines++;
-                    }
-                    if($coverage_ref->{"uncovered"}->{$line})
+                    if(exists($sumcountref->{$line}))
                     {
-                        $uncovered_lines++;
-                        $total_uncovered_lines++;
+                        if( $sumcountref->{$line} > 0 )
+                        {
+                            $covered_lines++;
+                            $total_covered_lines++;
+                        }
+                        else
+                        {
+                            $uncovered_lines++;
+                            $total_uncovered_lines++;
+                        }
                     }
                 }
             }
-            $coverage_ref->{"covered_lines"} = $covered_lines;
-            $coverage_ref->{"uncovered_lines"} = $uncovered_lines;
+            $filesref->{$file}->{"covered_lines"} = $covered_lines;
+            $filesref->{$file}->{"uncovered_lines"} = $uncovered_lines;
             my $total = $covered_lines + $uncovered_lines;
             my $percent = 0;
             if($total > 0)
             {
                 $percent = $covered_lines / $total;
             }
-            $coverage_ref->{"percent_covered"} = 100 * $percent;
+            $filesref->{$file}->{"percent_covered"} = 100 * $percent;
+        }
+        else
+        {
+            print "Can't find coverage data for $abs_filename\n";
         }
     }
     my $total_exec = $total_covered_lines + $total_uncovered_lines;
@@ -416,6 +1108,8 @@ sub calc_patch_coverage_percentage
     return [ $total_exec, $percent ];
 }
 
+#
+#
 sub patch_output
 {
     my $filesref = shift;
@@ -424,18 +1118,22 @@ sub patch_output
         my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
         next if($path !~ /^dali/);
 
-        my $patchref = $filesref->{$file}->{"patch"};
-        my $b_lines_ref = $filesref->{$file}->{"b_lines"};
-        my $coverage_ref = $filesref->{$file}->{"coverage"};
+        my $fileref = $filesref->{$file};
+        my $patchref = $fileref->{"patch"};
+        my $b_lines_ref = $fileref->{"b_lines"};
+
+        my $abs_filename = File::Spec->rel2abs($file, $root);
+        my $sumcountref = $info_data{$abs_filename}->{"sum"};
+
         print BOLD, "$file  ";
 
-        if($coverage_ref)
+        if($fileref)
         {
-            if( $coverage_ref->{"covered_lines"} > 0
+            if( $fileref->{"covered_lines"} > 0
                 ||
-                $coverage_ref->{"uncovered_lines"} > 0 )
+                $fileref->{"uncovered_lines"} > 0 )
             {
-                print GREEN, "Covered: " . $coverage_ref->{"covered_lines"}, RED, " Uncovered: " . $coverage_ref->{"uncovered_lines"}, RESET;
+                print GREEN, "Covered: " . $fileref->{"covered_lines"}, RED, " Uncovered: " . $fileref->{"uncovered_lines"}, RESET;
             }
         }
         else
@@ -461,28 +1159,29 @@ sub patch_output
                 my $line = $i + $patch->[0];
                 printf "%-6s  ", $line;
 
-                if($coverage_ref)
+                if($sumcountref)
                 {
                     my $color;
-                    if($coverage_ref->{"covered"}->{$line})
+                    if(exists($sumcountref->{$line}))
                     {
-                        $color=GREEN;
-                    }
-                    elsif($coverage_ref->{"uncovered"}->{$line})
-                    {
-                        $color=BOLD . RED;
+                        if($sumcountref->{$line} > 0)
+                        {
+                            $color=GREEN;
+                        }
+                        else
+                        {
+                            $color=BOLD . RED;
+                        }
                     }
                     else
                     {
-                        $color=BLACK;
+                        $color=CYAN;
                     }
-                    my $src=$coverage_ref->{"src"}->{$line};
-                    chomp($src);
+                    my $src = $b_lines_ref->{$line};
                     print $color, "$src\n", RESET;
                 }
                 else
                 {
-                    # We don't have coverage data, so print it from the patch instead.
                     my $src = $b_lines_ref->{$line};
                     print "$src\n";
                 }
@@ -491,70 +1190,65 @@ sub patch_output
     }
 }
 
-
+#
+#
 sub patch_html_output
 {
     my $filesref = shift;
 
-    my $html = HTML::Element->new('html');
-    my $head = HTML::Element->new('head');
-    my $title = HTML::Element->new('title');
-    $title->push_content("Patch Coverage");
-    $head->push_content($title, "\n");
-    $html->push_content($head, "\n");
+    open( my $filehandle, ">", $opt_output ) || die "Can't open $opt_output for writing:$!\n";
 
-    my $body = HTML::Element->new('body');
-    $body->attr('bgcolor', "white");
+    my $OUTPUT_FH = select;
+    select $filehandle;
+    print <<EOH;
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
+"http://www.w3.org/TR/REC-html40/loose.dtd">
+<html>
+<head>
+<title>Patch Coverage</title>
+</head>
+<body bgcolor="white">
+EOH
 
-    foreach my $file (sort(keys(%$filesref)))
+    foreach my $file (keys(%$filesref))
     {
         my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
         next if($path !~ /^dali/);
 
-        my $patchref = $filesref->{$file}->{"patch"};
-        my $b_lines_ref = $filesref->{$file}->{"b_lines"};
-        my $coverage_ref = $filesref->{$file}->{"coverage"};
-
-        my $header = HTML::Element->new('h2');
-        $header->push_content($file);
-        $body->push_content($header);
-        $body->push_content("\n");
-        if($coverage_ref)
+        my $fileref = $filesref->{$file};
+        my $patchref = $fileref->{"patch"};
+        my $b_lines_ref = $fileref->{"b_lines"};
+
+        my $abs_filename = File::Spec->rel2abs($file, $root);
+        my $sumcountref = $info_data{$abs_filename}->{"sum"};
+
+        print "<h2>$file</h2>\n";
+
+        if($fileref)
         {
-            if( $coverage_ref->{"covered_lines"} > 0
+            if( $fileref->{"covered_lines"} > 0
                 ||
-                $coverage_ref->{"uncovered_lines"} > 0 )
+                $fileref->{"uncovered_lines"} > 0 )
             {
-                my $para = HTML::Element->new('p');
-                my $covered = HTML::Element->new('span');
-                $covered->attr('style', "color:green;");
-                $covered->push_content("Covered: " . $coverage_ref->{"covered_lines"} );
-                $para->push_content($covered);
-
-                my $para2 = HTML::Element->new('p');
-                my $uncovered = HTML::Element->new('span');
-                $uncovered->attr('style', "color:red;");
-                $uncovered->push_content("Uncovered: " . $coverage_ref->{"uncovered_lines"} );
-                $para2->push_content($uncovered);
-                $body->push_content($para, $para2);
-            }
-            else
-            {
-                #print "coverage ref exists for $file:\n" . Data::Dumper::Dumper($coverage_ref) . "\n";
+                print "<p style=\"color:green;\">Covered: " .
+                    $fileref->{"covered_lines"} . "<p>" .
+                    "<p style=\"color:red;\">Uncovered: " .
+                    $fileref->{"uncovered_lines"} . "</span></p>";
             }
         }
         else
         {
-            my $para = HTML::Element->new('p');
-            my $span = HTML::Element->new('span');
+            print "<p>";
+            my $span=0;
             if($suffix eq ".cpp" || $suffix eq ".c" || $suffix eq ".h")
             {
-                $span->attr('style', "color:red;");
+                print "<span style=\"color:red;\">";
+                $span=1;
             }
-            $span->push_content("No coverage found");
-            $para->push_content($span);
-            $body->push_content($para);
+            print "No coverage found";
+            print "</span>" if $span;
         }
+        print "</p>";
 
         for my $patch (@$patchref)
         {
@@ -563,71 +1257,54 @@ sub patch_html_output
             {
                 $hunkstr .= " - " . ($patch->[0]+$patch->[1]-1);
             }
+            print "<p style=\"font-weight:bold;\">" . $hunkstr . "</p>";
 
-            my $para = HTML::Element->new('p');
-            my $span = HTML::Element->new('span');
-            $span->attr('style', "font-weight:bold;");
-            $span->push_content($hunkstr);
-            $para->push_content($span);
-            $body->push_content($para);
-
-            my $codeHunk = HTML::Element->new('pre');
+            print "<pre>";
             for(my $i = 0; $i < $patch->[1]; $i++ )
             {
                 my $line = $i + $patch->[0];
                 my $num_line_digits=log($line)/log(10);
                 for $i (0..(6-$num_line_digits-1))
                 {
-                    $codeHunk->push_content(" ");
+                    print " ";
                 }
+                print "$line  ";
 
-                $codeHunk->push_content("$line  ");
-
-                my $srcLine = HTML::Element->new('span');
-                if($coverage_ref)
+                if($sumcountref)
                 {
                     my $color;
-
-                    if($coverage_ref->{"covered"}->{$line})
+                    if(exists($sumcountref->{$line}))
                     {
-                        $srcLine->attr('style', "color:green;");
-                    }
-                    elsif($coverage_ref->{"uncovered"}->{$line})
-                    {
-                        $srcLine->attr('style', "color:red;font-weight:bold;");
+                        if($sumcountref->{$line} > 0)
+                        {
+                            print("<span style=\"color:green;\">");
+                        }
+                        else
+                        {
+                            print("<span style=\"color:red;font-weight:bold;\">");
+                        }
                     }
                     else
                     {
-                        $srcLine->attr('style', "color:black;font-weight:normal;");
+                        print("<span style=\"color:black;font-weight:normal;\">");
                     }
-                    my $src=$coverage_ref->{"src"}->{$line};
+                    my $src=$b_lines_ref->{$line};
                     chomp($src);
-                    $srcLine->push_content($src);
+                    print "$src</span>\n";
                 }
                 else
                 {
-                    # We don't have coverage data, so print it from the patch instead.
                     my $src = $b_lines_ref->{$line};
-                    $srcLine->attr('style', "color:black;font-weight:normal;");
-                    $srcLine->push_content($src);
+                    print "$src\n";
                 }
-                $codeHunk->push_content($srcLine, "\n");
             }
-            $body->push_content($codeHunk, "\n");
+            print "<\pre>\n";
         }
     }
-    $body->push_content(HTML::Element->new('hr'));
-    $html->push_content($body, "\n");
 
-    open( my $filehandle, ">", $opt_output ) || die "Can't open $opt_output for writing:$!\n";
-
-    print $filehandle <<EOH;
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
-"http://www.w3.org/TR/REC-html40/loose.dtd">
-EOH
-;
-    print $filehandle $html->as_HTML();
+    print $filehandle "<hr>\n</body>\n</html>\n";
     close $filehandle;
+    select $OUTPUT_FH;
 }
 
 
@@ -635,14 +1312,32 @@ EOH
 ##                                    MAIN                                    ##
 ################################################################################
 
-my $cwd = getcwd();
-chdir $repo->wc_path();
-chdir "build/tizen";
-`make rename_cov_data`;
+my $cwd = getcwd(); # expect this to be automated-tests folder
 
-my @cmd=('--no-pager','diff','--no-ext-diff','-U0','--no-color');
+# execute coverage.sh, generating build/tizen/dali.info from lib, and
+# *.dir/dali.info. Don't generate html
+print `./coverage.sh -n`;
+chdir "..";
+$root = getcwd();
+
+our %info_data; # Hash of all data from .info files
+my @info_files = split(/\n/, `find . -name dali.info`);
+my %new_info;
+
+# Read in all specified .info files
+foreach (@info_files)
+{
+    %new_info = %{read_info_file($_)};
+
+    # Combine %new_info with %info_data
+    %info_data = %{combine_info_files(\%info_data, \%new_info)};
+}
 
+
+# Generate git diff command
+my @cmd=('--no-pager','diff','--no-ext-diff','-U0','--no-color');
 my $status = $repo->command("status", "-s");
+
 if( $status eq "" && !scalar(@ARGV))
 {
     # There are no changes in the index or working tree, and
@@ -674,6 +1369,8 @@ else
 }
 
 push @cmd, @ARGV;
+
+# Execute diff & coverage from root directory
 my $filesref = run_diff(@cmd);
 
 chdir $cwd;
@@ -693,6 +1390,9 @@ if( $filecount == 0 )
     exit 0;    # Exit with no error.
 }
 
+#print_simplified_info() if $debug;
+#exit 0;
+
 my $percentref = calc_patch_coverage_percentage($filesref);
 if($percentref->[0] == 0)
 {
index 8508211..3052a59 100644 (file)
@@ -44,6 +44,7 @@ SET( SOURCES
         ${devel_api_text_abstraction_src_files}
         ${static_libraries_libunibreak_src_files}
         ${static_libraries_glyphy_src_files}
+        ${static_libraries_nanosvg_src_files}
         ${adaptor_addons_common_src_files}
         ${adaptor_addons_dummy_src_files}
 )
index 685e2e2..d69ba6b 100644 (file)
@@ -47,6 +47,7 @@ SET( SOURCES
     ${devel_api_text_abstraction_src_files}
     ${static_libraries_glyphy_src_files}
     ${static_libraries_libunibreak_src_files}
+    ${static_libraries_nanosvg_src_files}
      )
 IF( enable_ecore_wayland2 )
     SET( SOURCES ${SOURCES}
index 40fa397..efae782 100644 (file)
@@ -48,6 +48,7 @@ SET( SOURCES
      ${devel_api_text_abstraction_src_files}
      ${static_libraries_glyphy_src_files}
      ${static_libraries_libunibreak_src_files}
+     ${static_libraries_nanosvg_src_files}
      )
 IF( enable_ecore_wayland2 )
     SET( SOURCES ${SOURCES}
index 6176202..577c0e4 100644 (file)
@@ -47,6 +47,7 @@ SET( SOURCES
         ${devel_api_text_abstraction_src_files}
         ${static_libraries_glyphy_src_files}
         ${static_libraries_libunibreak_src_files}
+        ${static_libraries_nanosvg_src_files}
 )
 IF( enable_ecore_wayland2 )
     SET( SOURCES ${SOURCES}
index ddb8677..a79cae1 100644 (file)
@@ -47,6 +47,7 @@ SET( SOURCES
     ${adaptor_addons_tizen_src_files}
     ${static_libraries_glyphy_src_files}
     ${static_libraries_libunibreak_src_files}
+    ${static_libraries_nanosvg_src_files}
      )
 IF( enable_ecore_wayland2 )
     SET( SOURCES ${SOURCES}
index 47a8de2..94496ec 100644 (file)
@@ -46,6 +46,7 @@ SET( SOURCES
         ${adaptor_addons_ubuntu_src_files}
         ${static_libraries_glyphy_src_files}
         ${static_libraries_libunibreak_src_files}
+        ${static_libraries_nanosvg_src_files}
 )
 
 IF( ECORE_WAYLAND2 )
index c5498f5..51ca0a3 100644 (file)
@@ -49,6 +49,7 @@ SET( SOURCES
     ${devel_api_text_abstraction_src_files}
     ${static_libraries_glyphy_src_files}
     ${static_libraries_libunibreak_src_files}
+    ${static_libraries_nanosvg_src_files}
 )
 IF( enable_ecore_wayland2 )
     SET( SOURCES ${SOURCES}
index 068546d..3bfa9ed 100644 (file)
@@ -35,6 +35,7 @@ SET( SOURCES
         ${devel_api_text_abstraction_src_files}
         ${static_libraries_glyphy_src_files}
         ${static_libraries_libunibreak_src_files}
+        ${static_libraries_nanosvg_src_files}
         ${adaptor_windows_platform_src_files}
         ${adaptor_adaptor_windows_src_files}
         ${adaptor_window_system_windows_src_files}
index d334646..1935b21 100644 (file)
@@ -217,8 +217,6 @@ public:
 
 public:
   /**
-   * @brief Retrieve a handle to the instance of InputMethodContext.
-   * @return A handle to the InputMethodContext.
    * @brief Constructor.
    */
   InputMethodContext();
index 0aa57cd..1709bdb 100644 (file)
  *
  */
 
-// EXTERNAL INCLUDES
-#include <string>
-
 // INTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/vector-image-renderer.h>
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
 
 namespace Dali
@@ -47,41 +43,21 @@ public:
   virtual ~VectorImageRendererPlugin() {}
 
   /**
-   * @brief Second-phase constructor.
-   */
-  virtual bool Initialize() = 0;
-
-  /**
-   * @brief Sets the target buffer.
-   *
-   * @param[in] buffer The target buffer
-   */
-  virtual void SetBuffer( Dali::Devel::PixelBuffer &buffer ) = 0;
-
-  /**
-   * @brief Renders the content to the target buffer synchronously.
-   *
-   * @param[in] scale The target image scale
-   * @return True if the rendering success, false otherwise.
-   */
-  virtual bool Render(float scale) = 0;
-
-  /**
-   * @brief Load vector image data form url.
+   * @brief Load vector image data directly.
    *
-   * @param[in] url The url of the vector image file
+   * @param[in] data The memory data of vector image
    * @return True if the load success, false otherwise.
    */
-  virtual bool Load( const std::string& url ) = 0;
+  virtual bool Load(const Vector<uint8_t>& data) = 0;
 
   /**
-   * @brief Load vector image data directly.
+   * @brief Rasterizes the content to the target buffer synchronously.
    *
-   * @param[in] data The memory data of vector image
-   * @param[in] size The size of memory data
-   * @return True if the load success, false otherwise.
+   * @param[in] buffer The target buffer
+   * @param[in] scale The target image scale
+   * @return True if the rendering succeeds, false otherwise.
    */
-  virtual bool Load( const char *data, uint32_t size ) = 0;
+  virtual bool Rasterize(Dali::Devel::PixelBuffer &buffer, float scale) = 0;
 
   /**
    * @brief Gets the default size of the file.
@@ -98,4 +74,5 @@ public:
 };
 
 } // namespace Dali
+
 #endif // DALI_VECTOR_IMAGE_RENDERER_PLUGIN_H
index 8617ede..fb51e62 100644 (file)
@@ -27,11 +27,6 @@ namespace Dali
 VectorImageRenderer VectorImageRenderer::New()
 {
   Internal::Adaptor::VectorImageRendererPtr imageRenderer = Internal::Adaptor::VectorImageRenderer::New();
-  if( imageRenderer )
-  {
-    imageRenderer->Initialize();
-  }
-
   return VectorImageRenderer( imageRenderer.Get() );
 }
 
@@ -48,24 +43,14 @@ VectorImageRenderer::VectorImageRenderer( Internal::Adaptor::VectorImageRenderer
 {
 }
 
-void VectorImageRenderer::SetBuffer( Dali::Devel::PixelBuffer &buffer )
-{
-  GetImplementation( *this ).SetBuffer( buffer );
-}
-
-bool VectorImageRenderer::Render(float scale)
-{
-  return GetImplementation( *this ).Render(scale);
-}
-
-bool VectorImageRenderer::Load( const std::string& url )
+bool VectorImageRenderer::Load(const Vector<uint8_t>& data, float dpi)
 {
-  return GetImplementation( *this ).Load( url );
+  return GetImplementation(*this).Load(data, dpi);
 }
 
-bool VectorImageRenderer::Load( const char *data, uint32_t size )
+bool VectorImageRenderer::Rasterize(Dali::Devel::PixelBuffer& buffer, float scale)
 {
-  return GetImplementation( *this ).Load( data, size );
+  return GetImplementation(*this).Rasterize(buffer, scale);
 }
 
 void VectorImageRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
index 92c8004..bd82fa3 100644 (file)
@@ -54,7 +54,6 @@ public:
    */
   static VectorImageRenderer New();
 
-
   /**
    * @brief Creates an empty handle.
    * Use VectorImageRenderer::New() to create an initialized object.
@@ -82,36 +81,22 @@ public:
   VectorImageRenderer& operator=( const VectorImageRenderer& rhs ) = default;
 
   /**
-   * @brief Sets the target buffer.
-   *
-   * @param[in] buffer The target buffer
-   */
-  void SetBuffer( Dali::Devel::PixelBuffer &buffer );
-
-  /**
-   * @brief Renders the content to the target buffer synchronously.
-   *
-   * @param[in] scale The target image scale
-   * @return True if the rendering success, false otherwise.
-   */
-  bool Render( float scale );
-
-  /**
-   * @brief Load vector image data form url.
+   * @brief Load vector image data directly.
    *
-   * @param[in] url The url of the vector image file
-   * @return True if the rendering success, false otherwise.
+   * @param[in] data SVG image data to load.
+   * @param[in] dpi The DPI of the screen.
+   * @return True if the load success, false otherwise.
    */
-  bool Load( const std::string& url );
+  bool Load(const Vector<uint8_t>& data, float dpi);
 
   /**
-   * @brief Load vector image data directly.
+   * @brief Rasterizes the content to the target buffer synchronously.
    *
-   * @param[in] data The memory data of vector image
-   * @param[in] size The size of memory data
-   * @return True if the load success, false otherwise.
+   * @param[in] buffer The target buffer
+   * @param[in] scale The target image scale factor
+   * @return True if the rendering succeeds, false otherwise.
    */
-  bool Load( const char *data, uint32_t size );
+  bool Rasterize(Dali::Devel::PixelBuffer& buffer, float scale);
 
   /**
    * @brief Gets the default size of the file.
@@ -119,7 +104,7 @@ public:
    * @param[out] width The default width of the file
    * @param[out] height The default height of the file
    */
-  void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
+  void GetDefaultSize(uint32_t& width, uint32_t& height) const;
 
 public: // Not intended for application developers
 
index 205c959..eb28975 100644 (file)
@@ -94,6 +94,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
   mUpdateStatusLogger( environmentOptions ),
   mEventThreadSemaphore(),
   mGraphicsInitializeSemaphore(),
+  mSurfaceSemaphore(),
   mUpdateRenderThreadWaitCondition(),
   mAdaptorInterfaces( adaptorInterfaces ),
   mPerformanceInterface( adaptorInterfaces.GetPerformanceInterface() ),
@@ -140,6 +141,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
   // Initialize to 0 so that it just waits if sem_post has not been called
   sem_init( &mEventThreadSemaphore, 0, 0 );
   sem_init( &mGraphicsInitializeSemaphore, 0, 0 );
+  sem_init( &mSurfaceSemaphore, 0, 0 );
 }
 
 CombinedUpdateRenderController::~CombinedUpdateRenderController()
@@ -319,7 +321,7 @@ void CombinedUpdateRenderController::ReplaceSurface( Dali::RenderSurfaceInterfac
     }
 
     // Wait until the surface has been replaced
-    sem_wait( &mEventThreadSemaphore );
+    sem_wait( &mSurfaceSemaphore );
 
     LOG_EVENT( "Surface replaced, event-thread continuing" );
   }
@@ -342,7 +344,7 @@ void CombinedUpdateRenderController::DeleteSurface( Dali::RenderSurfaceInterface
     }
 
     // Wait until the surface has been deleted
-    sem_wait( &mEventThreadSemaphore );
+    sem_wait( &mSurfaceSemaphore );
 
     LOG_EVENT( "Surface deleted, event-thread continuing" );
   }
@@ -940,7 +942,7 @@ Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeRep
 void CombinedUpdateRenderController::SurfaceReplaced()
 {
   // Just increment the semaphore
-  sem_post( &mEventThreadSemaphore );
+  sem_post( &mSurfaceSemaphore );
 }
 
 Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
@@ -956,7 +958,7 @@ Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeDel
 void CombinedUpdateRenderController::SurfaceDeleted()
 {
   // Just increment the semaphore
-  sem_post( &mEventThreadSemaphore );
+  sem_post( &mSurfaceSemaphore );
 }
 
 bool CombinedUpdateRenderController::ShouldSurfaceBeResized()
index 6d0cb37..c87a1a6 100644 (file)
@@ -343,8 +343,9 @@ private:
   FpsTracker                        mFpsTracker;                       ///< Object that tracks the FPS
   UpdateStatusLogger                mUpdateStatusLogger;               ///< Object that logs the update-status as required.
 
-  sem_t                             mEventThreadSemaphore;             ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
+  sem_t                             mEventThreadSemaphore;             ///< Used by the event thread to ensure all threads have been initialised.
   sem_t                             mGraphicsInitializeSemaphore;      ///< Used by the render thread to ensure the graphics has been initialised.
+  sem_t                             mSurfaceSemaphore;                 ///< Used by the event thread to ensure the surface has been deleted or replaced.
 
   ConditionalWait                   mUpdateRenderThreadWaitCondition;  ///< The wait condition for the update-render-thread.
 
index eaaca10..fa5094a 100644 (file)
@@ -28,6 +28,269 @@ namespace Internal
 namespace Adaptor
 {
 
+namespace
+{
+
+constexpr Channel ALPHA_CHANNEL_ONLY[]       = {ALPHA};
+constexpr Channel LUMINANCE_CHANNEL_ONLY[]   = {LUMINANCE};
+constexpr Channel LUMINANCE_ALPHA_CHANNELS[] = {ALPHA, LUMINANCE};
+constexpr Channel RGB_CHANNELS[]             = {RED, GREEN, BLUE};
+constexpr Channel BGR_CHANNELS[]             = {BLUE, GREEN, RED};
+constexpr Channel RGBA_CHANNELS[]            = {RED, GREEN, BLUE, ALPHA};
+constexpr Channel BGRA_CHANNELS[]            = {BLUE, GREEN, RED, ALPHA};
+
+/**
+ * @brief Template to Read from a buffer with pixel formats that have one byte per channel.
+ *
+ * @tparam NumberOfChannels The number of channels to check
+ * @param pixelData The pixel data to retrieve the value from
+ * @param channel The channel we're after
+ * @param channels The array of channels in the pixel format
+ * @return The value of the required channel
+ */
+template<size_t NumberOfChannels>
+unsigned int ReadChannel(unsigned char* pixelData, Channel channel, const Channel (&channels)[NumberOfChannels])
+{
+  auto num = 0u;
+  auto retVal = 0u;
+  for(auto current : channels)
+  {
+    if( channel == current )
+    {
+      retVal = static_cast<unsigned int>(*(pixelData + num));
+      break;
+    }
+    ++num;
+  }
+  return retVal;
+}
+
+/**
+ * @brief Template to Write to a buffer with pixel formats that have one byte per channel.
+ *
+ * @tparam NumberOfChannels The number of channels to check
+ * @param pixelData The pixel data to write the value to
+ * @param channel The channel we're after
+ * @param channelValue The value of the channel to set
+ * @param channels The array of channels in the pixel format
+ */
+template<size_t NumberOfChannels>
+void WriteChannel(unsigned char* pixelData, Channel channel, unsigned int channelValue, const Channel (&channels)[NumberOfChannels])
+{
+  auto num = 0u;
+  for( auto current : channels )
+  {
+    if( channel == current )
+    {
+      *(pixelData + num) = static_cast<unsigned char>( channelValue & 0xFF );
+      break;
+    }
+    ++num;
+  }
+}
+
+/**
+ * @brief Reads from buffers with a pixel format of 565.
+ *
+ * @param pixelData The pixel data to read from
+ * @param channel The channel we're after
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ * @return The value of the required channel
+ */
+unsigned int ReadChannel565(unsigned char* pixelData, Channel channel, Channel one, Channel two, Channel three)
+{
+  if( channel == one )
+  {
+    return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
+  }
+  else if( channel == two )
+  {
+    return ((static_cast<unsigned int>(*pixelData) & 0x07) << 3) |
+      ((static_cast<unsigned int>(*(pixelData+1)) & 0xE0) >> 5);
+  }
+  else if( channel == three )
+  {
+    return static_cast<unsigned int>(*(pixelData+1)) & 0x1F;
+  }
+  return 0u;
+}
+
+/**
+ * @brief Writes to the buffer with a pixel format of 565.
+ *
+ * @param pixelData The pixel data to write to
+ * @param channel The channel we're after
+ * @param channelValue The value to write
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ */
+void WriteChannel565(unsigned char* pixelData, Channel channel, unsigned int channelValue, Channel one, Channel two, Channel three)
+{
+  if( channel == one )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0xF8 );
+    *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
+  }
+  else if( channel == two )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0x07 );
+    *pixelData |= static_cast<unsigned char>( (channelValue >> 3) & 0x07 );
+
+    *(pixelData+1) &= static_cast<unsigned char>( ~0xE0 );
+    *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 5) & 0xE0 );
+  }
+  else if( channel == three )
+  {
+    *(pixelData+1) &= static_cast<unsigned char>( ~0x1F );
+    *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x1F );
+  }
+}
+
+/**
+ * @brief Reads from buffers with a pixel format of 4444.
+ *
+ * @param pixelData The pixel data to read from
+ * @param channel The channel we're after
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ * @param four The fourth channel of the pixel format
+ * @return
+ */
+unsigned int ReadChannel4444(unsigned char* pixelData, Channel channel, Channel one, Channel two, Channel three, Channel four)
+{
+  if( channel == one )
+  {
+    return (static_cast<unsigned int>(*pixelData) & 0xF0) >> 4;
+  }
+  else if( channel == two )
+  {
+    return (static_cast<unsigned int>(*pixelData) & 0x0F);
+  }
+  else if( channel == three )
+  {
+    return (static_cast<unsigned int>(*(pixelData+1)) & 0xF0) >> 4;
+  }
+  else if( channel == four )
+  {
+    return (static_cast<unsigned int>(*(pixelData+1)) & 0x0F);
+  }
+  return 0u;
+}
+
+/**
+ * @brief Writes to the buffer with a pixel format of 565.
+ *
+ * @param pixelData The pixel data to write to
+ * @param channel The channel we're after
+ * @param channelValue The value to write
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ * @param four The fourth channel of the pixel format
+ */
+void WriteChannel4444(unsigned char* pixelData, Channel channel, unsigned int channelValue, Channel one, Channel two, Channel three, Channel four)
+{
+  if( channel == one )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0xF0 );
+    *pixelData |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
+  }
+  else if( channel == two )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0x0F );
+    *pixelData |= static_cast<unsigned char>( channelValue & 0x0F );
+  }
+  else if( channel == three )
+  {
+    *(pixelData+1) &= static_cast<unsigned char>( ~0xF0 );
+    *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
+  }
+  else if( channel == four )
+  {
+    *(pixelData+1) &= static_cast<unsigned char>( ~0x0F );
+    *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x0F );
+  }
+}
+
+/**
+ * @brief Reads from buffers with a pixel format of 5551.
+ *
+ * @param pixelData The pixel data to read from
+ * @param channel The channel we're after
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ * @param four The fourth channel of the pixel format
+ * @return
+ */
+unsigned int ReadChannel5551(unsigned char* pixelData, Channel channel, Channel one, Channel two, Channel three, Channel four)
+{
+  if( channel == one )
+  {
+    return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
+  }
+  else if( channel == two )
+  {
+    return ((static_cast<unsigned int>(*pixelData) & 0x07) << 2) |
+      ((static_cast<unsigned int>(*(pixelData+1)) & 0xC0) >> 6);
+  }
+  else if( channel == three )
+  {
+    return (static_cast<unsigned int>(*(pixelData+1)) & 0x3E) >> 1;
+  }
+  else if( channel == four )
+  {
+    return static_cast<unsigned int>(*(pixelData+1)) & 0x01;
+  }
+  return 0u;
+}
+
+/**
+ * @brief Writes to the buffer with a pixel format of 5551.
+ *
+ * @param pixelData The pixel data to write to
+ * @param channel The channel we're after
+ * @param channelValue The value to write
+ * @param one The first channel of the pixel format
+ * @param two The second channel of the pixel format
+ * @param three The third channel of the pixel format
+ * @param four The fourth channel of the pixel format
+ */
+void WriteChannel5551(unsigned char* pixelData, Channel channel, unsigned int channelValue, Channel one, Channel two, Channel three, Channel four)
+{
+  // 11111222 22333334
+  //    F8  7 C0  3E 1
+  if( channel == one )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0xF8 );
+    *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
+  }
+  else if( channel == two )
+  {
+    *pixelData &= static_cast<unsigned char>( ~0x07 );
+    *pixelData |= static_cast<unsigned char>( (channelValue >> 2) & 0x07 );
+
+    *(pixelData+1) &= static_cast<unsigned char>( ~0xC0 );
+    *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 6) & 0xC0 );
+  }
+  else if( channel == three )
+  {
+    *(pixelData+1) &= static_cast<unsigned char>( ~0x3E );
+    *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 1) & 0x3E );
+  }
+  else if( channel == four )
+  {
+    *(pixelData+1) &= static_cast<unsigned char>( ~0x01 );
+    *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x01 );
+  }
+}
+
+} // unnamed namespace
+
 struct Location
 {
   unsigned int bitShift;
@@ -151,231 +414,65 @@ unsigned int ReadChannel( unsigned char* pixelData,
   {
     case Dali::Pixel::A8:
     {
-      if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, ALPHA_CHANNEL_ONLY);
     }
     case Dali::Pixel::L8:
     {
-      if( channel == LUMINANCE )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, LUMINANCE_CHANNEL_ONLY);
     }
     case Dali::Pixel::LA88:
     {
-      if( channel == LUMINANCE )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*(pixelData+1));
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, LUMINANCE_ALPHA_CHANNELS);
     }
     case Dali::Pixel::RGB565:
     {
-      if( channel == RED )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
-      }
-      else if( channel == GREEN )
-      {
-        return ((static_cast<unsigned int>(*pixelData) & 0x07) << 3) |
-          ((static_cast<unsigned int>(*(pixelData+1)) & 0xE0) >> 5);
-      }
-      else if( channel == BLUE )
-      {
-        return static_cast<unsigned int>(*(pixelData+1)) & 0x1F;
-      }
-      else return 0u;
+      return ReadChannel565(pixelData, channel, RED, GREEN, BLUE);
     }
 
     case Dali::Pixel::BGR565:
     {
-      if( channel == BLUE )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
-      }
-      else if( channel == GREEN )
-      {
-        return ((static_cast<unsigned int>(*pixelData) & 0x07) << 3) |
-          ((static_cast<unsigned int>(*(pixelData+1)) & 0xE0) >> 5);
-      }
-      else if( channel == RED )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1) & 0x1F) );
-      }
-      else return 0u;
+      return ReadChannel565(pixelData, channel, BLUE, GREEN, RED);
     }
 
     case Dali::Pixel::RGB888:
     case Dali::Pixel::RGB8888:
     {
-      if( channel == RED )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else if( channel == GREEN )
-      {
-        return static_cast<unsigned int>(*(pixelData+1));
-      }
-      else if( channel == BLUE )
-      {
-        return static_cast<unsigned int>(*(pixelData+2));
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, RGB_CHANNELS);
     }
 
     case Dali::Pixel::BGR8888:
     {
-      if( channel == BLUE )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else if( channel == GREEN )
-      {
-        return static_cast<unsigned int>(*(pixelData+1));
-      }
-      else if( channel == RED )
-      {
-        return static_cast<unsigned int>(*(pixelData+2));
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, BGR_CHANNELS);
     }
 
     case Dali::Pixel::RGBA8888:
     {
-      if( channel == RED )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else if( channel == GREEN )
-      {
-        return static_cast<unsigned int>(*(pixelData+1));
-      }
-      else if( channel == BLUE )
-      {
-        return static_cast<unsigned int>(*(pixelData+2));
-      }
-      else if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*(pixelData+3));
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, RGBA_CHANNELS);
     }
 
     case Dali::Pixel::BGRA8888:
     {
-      if( channel == BLUE )
-      {
-        return static_cast<unsigned int>(*pixelData);
-      }
-      else if( channel == GREEN )
-      {
-        return static_cast<unsigned int>(*(pixelData+1));
-      }
-      else if( channel == RED )
-      {
-        return static_cast<unsigned int>(*(pixelData+2));
-      }
-      else if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*(pixelData+3));
-      }
-      else return 0u;
+      return ReadChannel(pixelData, channel, BGRA_CHANNELS);
     }
 
     case Dali::Pixel::RGBA4444:
     {
-      if( channel == RED )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF0) >> 4;
-      }
-      else if( channel == GREEN )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0x0F);
-      }
-      else if( channel == BLUE )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1)) & 0xF0) >> 4;
-      }
-      else if( channel == ALPHA )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1)) & 0x0F);
-      }
-      else return 0u;
+      return ReadChannel4444(pixelData, channel, RED, GREEN, BLUE, ALPHA);
     }
 
     case Dali::Pixel::BGRA4444:
     {
-      if( channel == BLUE )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF0) >> 4;
-      }
-      else if( channel == GREEN )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0x0F);
-      }
-      else if( channel == RED )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1)) & 0xF0) >> 4;
-      }
-      else if( channel == ALPHA )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1)) & 0x0F);
-      }
-      else return 0u;
+      return ReadChannel4444(pixelData, channel, BLUE, GREEN, RED, ALPHA);
     }
 
     case Dali::Pixel::RGBA5551:
     {
-      if( channel == RED )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
-      }
-      else if( channel == GREEN )
-      {
-        return ((static_cast<unsigned int>(*pixelData) & 0x07) << 2) |
-          ((static_cast<unsigned int>(*(pixelData+1)) & 0xC0) >> 6);
-      }
-      else if( channel == BLUE )
-      {
-        return (static_cast<unsigned int>(*(pixelData+1)) & 0x3E) >> 1;
-      }
-      else if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*(pixelData+1)) & 0x01;
-      }
-
-      else return 0u;
+      return ReadChannel5551(pixelData, channel, RED, GREEN, BLUE, ALPHA);
     }
 
     case Dali::Pixel::BGRA5551:
     {
-      if( channel == BLUE )
-      {
-        return (static_cast<unsigned int>(*pixelData) & 0xF8) >> 3;
-      }
-      else if( channel == GREEN )
-      {
-        return ((static_cast<unsigned int>(*pixelData) & 0x07) << 2) |
-          ((static_cast<unsigned int>(*(pixelData+1)) & 0xC0) >> 6);
-      }
-      else if( channel == RED )
-      {
-        return ( static_cast<unsigned int>(*(pixelData+1)) & 0x3E) >> 1;
-      }
-      else if( channel == ALPHA )
-      {
-        return static_cast<unsigned int>(*(pixelData+1)) & 0x01;
-      }
-
-      else return 0u;
+      return ReadChannel5551(pixelData, channel, BLUE, GREEN, RED, ALPHA);
     }
 
     case Dali::Pixel::DEPTH_UNSIGNED_INT:
@@ -401,260 +498,77 @@ void WriteChannel( unsigned char* pixelData,
   {
     case Dali::Pixel::A8:
     {
-      if( channel == ALPHA )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, ALPHA_CHANNEL_ONLY);
       break;
     }
     case Dali::Pixel::L8:
     {
-      if( channel == LUMINANCE )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, LUMINANCE_CHANNEL_ONLY);
       break;
     }
     case Dali::Pixel::LA88:
     {
-      if( channel == LUMINANCE )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+1) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, LUMINANCE_ALPHA_CHANNELS);
       break;
     }
     case Dali::Pixel::RGB565:
     {
-      if( channel == RED )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF8 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x07 );
-        *pixelData |= static_cast<unsigned char>( (channelValue >> 3) & 0x07 );
-
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xE0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 5) & 0xE0 );
-      }
-      else if( channel == BLUE )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x1F );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x1F );
-      }
+      WriteChannel565(pixelData, channel, channelValue, RED, GREEN, BLUE);
       break;
     }
 
     case Dali::Pixel::BGR565:
     {
-      if( channel == BLUE )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF8 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x07 );
-        *pixelData |= static_cast<unsigned char>( (channelValue >> 3) & 0x07 );
-
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xE0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 5) & 0xE0 );
-      }
-      else if( channel == RED )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x1F );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x1F );
-      }
+      WriteChannel565(pixelData, channel, channelValue, BLUE, GREEN, RED);
       break;
     }
 
     case Dali::Pixel::RGB888:
     case Dali::Pixel::RGB8888:
     {
-      if( channel == RED )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == GREEN )
-      {
-        *(pixelData+1) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == BLUE )
-      {
-        *(pixelData+2) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, RGB_CHANNELS);
       break;
     }
 
     case Dali::Pixel::BGR8888:
     {
-      if( channel == BLUE )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == GREEN )
-      {
-        *(pixelData+1) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == RED )
-      {
-        *(pixelData+2) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, BGR_CHANNELS);
       break;
     }
 
     case Dali::Pixel::RGBA8888:
     {
-      if( channel == RED )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == GREEN )
-      {
-        *(pixelData+1) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == BLUE )
-      {
-        *(pixelData+2) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+3) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, RGBA_CHANNELS);
       break;
     }
 
     case Dali::Pixel::BGRA8888:
     {
-      if( channel == BLUE )
-      {
-        *pixelData = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == GREEN )
-      {
-        *(pixelData+1) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == RED )
-      {
-        *(pixelData+2) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+3) = static_cast<unsigned char>( channelValue & 0xFF );
-      }
+      WriteChannel(pixelData, channel, channelValue, BGRA_CHANNELS);
       break;
     }
 
     case Dali::Pixel::RGBA4444:
     {
-      if( channel == RED )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF0 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x0F );
-        *pixelData |= static_cast<unsigned char>( channelValue & 0x0F );
-      }
-      else if( channel == BLUE )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xF0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x0F );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x0F );
-      }
+      WriteChannel4444(pixelData, channel, channelValue, RED, GREEN, BLUE, ALPHA);
       break;
     }
 
     case Dali::Pixel::BGRA4444:
     {
-      if( channel == BLUE )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF0 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x0F );
-        *pixelData |= static_cast<unsigned char>( channelValue & 0x0F );
-      }
-      else if( channel == RED )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xF0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 4) & 0xF0 );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x0F );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x0F );
-      }
+      WriteChannel4444(pixelData, channel, channelValue, BLUE, GREEN, RED, ALPHA);
       break;
     }
 
     case Dali::Pixel::RGBA5551:
     {
-      // rrrrrggg ggbbbbba
-      //    F8  7 C0  3E 1
-      if( channel == RED )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF8 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x07 );
-        *pixelData |= static_cast<unsigned char>( (channelValue >> 2) & 0x07 );
-
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xC0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 6) & 0xC0 );
-      }
-      else if( channel == BLUE )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x3E );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 1) & 0x3E );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x01 );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x01 );
-      }
+      WriteChannel5551(pixelData, channel, channelValue, RED, GREEN, BLUE, ALPHA);
       break;
     }
 
     case Dali::Pixel::BGRA5551:
     {
-      if( channel == BLUE )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0xF8 );
-        *pixelData |= static_cast<unsigned char>( (channelValue << 3) & 0xF8 );
-      }
-      else if( channel == GREEN )
-      {
-        *pixelData &= static_cast<unsigned char>( ~0x07 );
-        *pixelData |= static_cast<unsigned char>( (channelValue >> 2) & 0x07 );
-
-        *(pixelData+1) &= static_cast<unsigned char>( ~0xC0 );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 6) & 0xC0 );
-      }
-      else if( channel == RED )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x3E );
-        *(pixelData+1) |= static_cast<unsigned char>( (channelValue << 1 ) & 0x3E );
-      }
-      else if( channel == ALPHA )
-      {
-        *(pixelData+1) &= static_cast<unsigned char>( ~0x01 );
-        *(pixelData+1) |= static_cast<unsigned char>( channelValue & 0x01 );
-      }
+      WriteChannel5551(pixelData, channel, channelValue, BLUE, GREEN, RED, ALPHA);
       break;
     }
 
index 1a4a084..1b5974e 100644 (file)
 #include <dali/internal/vector-image/common/vector-image-renderer-impl.h>
 
 // EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
 #include <dali/public-api/object/type-registry.h>
 
 // INTERNAL INCLUDES
+#include <third-party/nanosvg/nanosvg.h>
+#include <third-party/nanosvg/nanosvgrast.h>
 
 namespace Dali
 {
@@ -34,6 +37,7 @@ namespace Adaptor
 
 namespace // unnamed namespace
 {
+const char* const UNITS("px");
 
 // Type Registration
 Dali::BaseHandle Create()
@@ -48,46 +52,95 @@ Dali::TypeRegistration type( typeid( Dali::VectorImageRenderer ), typeid( Dali::
 VectorImageRendererPtr VectorImageRenderer::New()
 {
   VectorImageRendererPtr renderer = new VectorImageRenderer();
+  if(renderer)
+  {
+    renderer->Initialize();
+  }
   return renderer;
 }
 
 VectorImageRenderer::VectorImageRenderer()
-: mPlugin( std::string() )
+: mPlugin(std::string()),
+  mParsedImage(nullptr),
+  mRasterizer(nullptr)
 {
 }
 
-void VectorImageRenderer::Initialize()
+VectorImageRenderer::~VectorImageRenderer()
 {
-  mPlugin.Initialize();
+  if(mParsedImage)
+  {
+    nsvgDelete(mParsedImage);
+  }
+
+  if(mRasterizer)
+  {
+    nsvgDeleteRasterizer(mRasterizer);
+  }
 }
 
-void VectorImageRenderer::SetBuffer( Dali::Devel::PixelBuffer &buffer )
-{
-  mPlugin.SetBuffer( buffer );
-}
-
-bool VectorImageRenderer::Render(float scale)
+void VectorImageRenderer::Initialize()
 {
-  return mPlugin.Render( scale );
+  if(!mPlugin.IsValid())
+  {
+    mRasterizer = nsvgCreateRasterizer();
+  }
 }
 
-bool VectorImageRenderer::Load( const std::string& url )
+bool VectorImageRenderer::Load(const Vector<uint8_t>& data, float dpi)
 {
-  return mPlugin.Load( url );
+  if(mPlugin.IsValid())
+  {
+    return mPlugin.Load(data);
+  }
+  else
+  {
+    mParsedImage = nsvgParse(reinterpret_cast<char*>(data.Begin()), UNITS, dpi);
+    if(!mParsedImage)
+    {
+      DALI_LOG_ERROR("VectorImageRenderer::Load: nsvgParse failed\n");
+      return false;
+    }
+    return true;
+  }
 }
 
-bool VectorImageRenderer::Load( const char *data, uint32_t size )
+bool VectorImageRenderer::Rasterize(Dali::Devel::PixelBuffer& buffer, float scale)
 {
-  return mPlugin.Load( data, size );
+  if(mPlugin.IsValid())
+  {
+    return mPlugin.Rasterize(buffer, scale);
+  }
+  else
+  {
+    if(mParsedImage != nullptr)
+    {
+      int stride = buffer.GetWidth() * Pixel::GetBytesPerPixel(buffer.GetPixelFormat());
+      nsvgRasterize(mRasterizer, mParsedImage, 0.0f, 0.0f, scale, buffer.GetBuffer(), buffer.GetWidth(), buffer.GetHeight(), stride);
+      return true;
+    }
+    return false;
+  }
 }
 
 void VectorImageRenderer::GetDefaultSize( uint32_t& width, uint32_t& height ) const
 {
-  mPlugin.GetDefaultSize( width, height );
+  if(mPlugin.IsValid())
+  {
+    mPlugin.GetDefaultSize(width, height);
+  }
+  else
+  {
+    if(mParsedImage)
+    {
+      width  = mParsedImage->width;
+      height = mParsedImage->height;
+    }
+  }
 }
 
 } // namespace Adaptor
 
-} // namespace internal
+} // namespace Internal
 
 } // namespace Dali
index 405de4a..1d85b69 100755 (executable)
@@ -27,6 +27,9 @@
 #include <dali/internal/vector-image/common/vector-image-renderer-plugin-proxy.h>
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
 
+struct NSVGrasterizer;
+struct NSVGimage;
+
 namespace Dali
 {
 
@@ -42,7 +45,7 @@ using VectorImageRendererPtr = IntrusivePtr< VectorImageRenderer >;
 /**
  * Dali internal VectorImageRenderer.
  */
-class VectorImageRenderer : public BaseObject, public ConnectionTracker
+class VectorImageRenderer : public BaseObject
 {
 public:
 
@@ -54,29 +57,14 @@ public:
   static VectorImageRendererPtr New();
 
   /**
-   * @brief Initializes member data.
-   */
-  void Initialize();
-
-  /**
-   * @copydoc Dali::VectorImageRenderer::SetBuffer()
-   */
-  void SetBuffer( Dali::Devel::PixelBuffer &buffer );
-
-  /**
-   * @copydoc Dali::VectorImageRenderer::Render()
-   */
-  bool Render(float scale);
-
-  /**
    * @copydoc Dali::VectorImageRenderer::Load()
    */
-  bool Load( const std::string& url );
+  bool Load(const Vector<uint8_t>& data, float dpi);
 
   /**
-   * @copydoc Dali::VectorImageRenderer::Load()
+   * @copydoc Dali::VectorImageRenderer::Rasterize()
    */
-  bool Load( const char *data, uint32_t size );
+  bool Rasterize(Dali::Devel::PixelBuffer& buffer, float scale);
 
   /**
    * @copydoc Dali::VectorImageRenderer::GetDefaultSize()
@@ -98,11 +86,18 @@ private:
   /**
    * @brief Destructor.
    */
-  ~VectorImageRenderer() = default;
+  virtual ~VectorImageRenderer();
+
+  /**
+   * @brief Initializes member data.
+   */
+  void Initialize();
 
 private:
 
   VectorImageRendererPluginProxy mPlugin;
+  NSVGimage*                     mParsedImage;
+  NSVGrasterizer*                mRasterizer;
 };
 
 } // namespace Adaptor
index a1e1e9a..86af7ae 100644 (file)
@@ -39,13 +39,13 @@ const char* DEFAULT_OBJECT_NAME( "libdali2-vector-image-renderer-plugin.so" );
 
 }
 
-VectorImageRendererPluginProxy::VectorImageRendererPluginProxy( std::string sharedObjectName )
+VectorImageRendererPluginProxy::VectorImageRendererPluginProxy(std::string sharedObjectName)
 : mSharedObjectName(std::move(sharedObjectName)),
-  mLibHandle( nullptr ),
-  mPlugin( nullptr ),
-  mCreateVectorImageRendererPtr( nullptr )
+  mLibHandle(nullptr),
+  mPlugin(nullptr),
+  mCreateVectorImageRendererPtr(nullptr)
 {
-  if( mSharedObjectName.empty() )
+  if(mSharedObjectName.empty())
   {
     mSharedObjectName = DEFAULT_OBJECT_NAME;
   }
@@ -55,26 +55,26 @@ VectorImageRendererPluginProxy::VectorImageRendererPluginProxy( std::string shar
 
 VectorImageRendererPluginProxy::~VectorImageRendererPluginProxy()
 {
-  if( mPlugin )
+  if(mPlugin)
   {
     delete mPlugin;
     mPlugin = nullptr;
 
-    if( mLibHandle && dlclose( mLibHandle ) )
+    if(mLibHandle && dlclose(mLibHandle))
     {
-      DALI_LOG_ERROR( "Error closing vector image renderer plugin library: %s\n", dlerror() );
+      DALI_LOG_ERROR("Error closing vector image renderer plugin library: %s\n", dlerror());
     }
   }
 }
 
 void VectorImageRendererPluginProxy::InitializePlugin()
 {
-  mLibHandle = dlopen( mSharedObjectName.c_str(), RTLD_LAZY );
+  mLibHandle = dlopen(mSharedObjectName.c_str(), RTLD_LAZY);
 
   char* error = dlerror();
-  if( mLibHandle == nullptr || error != nullptr )
+  if(mLibHandle == nullptr || error != nullptr)
   {
-    DALI_LOG_ERROR( "VectorImageRendererPluginProxy::Initialize: dlopen error [%s]\n", error );
+    DALI_LOG_WARNING("VectorImageRendererPluginProxy::Initialize: dlopen error [%s]\n", error);
     return;
   }
 
@@ -96,46 +96,25 @@ void VectorImageRendererPluginProxy::InitializePlugin()
   }
 }
 
-bool VectorImageRendererPluginProxy::Initialize()
+bool VectorImageRendererPluginProxy::IsValid() const
 {
-  if( mPlugin )
-  {
-    return mPlugin->Initialize();
-  }
-  return false;
+  return (mPlugin != nullptr);
 }
 
-void VectorImageRendererPluginProxy::SetBuffer( Dali::Devel::PixelBuffer &buffer )
+bool VectorImageRendererPluginProxy::Load(const Vector<uint8_t>& data)
 {
-  if( mPlugin )
-  {
-    mPlugin->SetBuffer( buffer );
-  }
-}
-
-bool VectorImageRendererPluginProxy::Render(float scale)
-{
-  if( mPlugin )
-  {
-    return mPlugin->Render( scale );
-  }
-  return false;
-}
-
-bool VectorImageRendererPluginProxy::Load( const std::string& url )
-{
-  if( mPlugin )
+  if(mPlugin)
   {
-    return mPlugin->Load( url );
+    return mPlugin->Load(data);
   }
   return false;
 }
 
-bool VectorImageRendererPluginProxy::Load( const char *data, uint32_t size )
+bool VectorImageRendererPluginProxy::Rasterize(Dali::Devel::PixelBuffer& buffer, float scale)
 {
   if( mPlugin )
   {
-    return mPlugin->Load( data, size );
+    return mPlugin->Rasterize(buffer, scale);
   }
   return false;
 }
index f233403..b13b6c0 100644 (file)
@@ -21,7 +21,6 @@
 // INTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/vector-image-renderer-plugin.h>
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
-#include <memory>
 
 namespace Dali
 {
@@ -50,34 +49,25 @@ public:
   ~VectorImageRendererPluginProxy();
 
   /**
-   * @copydoc Dali::VectorImageRendererPlugin::Initialize()
+   * @brief Query whether the plugin is valid or not.
+   * @return True if valid, false otherwise
    */
-  bool Initialize();
-
-  /**
-   * @copydoc Dali::VectorImageRendererPlugin::SetBuffer()
-   */
-  void SetBuffer( Dali::Devel::PixelBuffer &buffer );
-
-  /**
-   * @copydoc Dali::VectorImageRendererPlugin::Render()
-   */
-  bool Render(float scale);
+  bool IsValid() const;
 
   /**
    * @copydoc Dali::VectorImageRendererPlugin::Load()
    */
-  bool Load( const std::string& url );
+  bool Load(const Vector<uint8_t>& data);
 
   /**
-   * @copydoc Dali::VectorImageRendererPlugin::Load()
+   * @copydoc Dali::VectorImageRendererPlugin::Rasterize()
    */
-  bool Load( const char *data, uint32_t size );
+  bool Rasterize(Dali::Devel::PixelBuffer& buffer, float scale);
 
   /**
    * @copydoc Dali::VectorImageRendererPlugin::GetDefaultSize()
    */
-  void GetDefaultSize( uint32_t& width, uint32_t& height ) const;
+  void GetDefaultSize(uint32_t& width, uint32_t& height) const;
 
   // Not copyable or movable
   VectorImageRendererPluginProxy( const VectorImageRendererPluginProxy& ) = delete; ///< Deleted copy constructor
index 99d67d7..ea39b21 100644 (file)
@@ -27,7 +27,7 @@ namespace Dali
 {
 const unsigned int ADAPTOR_MAJOR_VERSION = 1;
 const unsigned int ADAPTOR_MINOR_VERSION = 9;
-const unsigned int ADAPTOR_MICRO_VERSION = 32;
+const unsigned int ADAPTOR_MICRO_VERSION = 35;
 const char* const  ADAPTOR_BUILD_DATE    = __DATE__ " " __TIME__;
 
 #ifdef DEBUG_ENABLED
index c3ca400..d435c1b 100644 (file)
@@ -17,7 +17,7 @@
 
 Name:       dali2-adaptor
 Summary:    The DALi Tizen Adaptor
-Version:    1.9.32
+Version:    1.9.35
 Release:    1
 Group:      System/Libraries
 License:    Apache-2.0 and BSD-3-Clause and MIT
index 955fc9e..e4623f2 100644 (file)
@@ -31,4 +31,10 @@ SET( adaptor_windows_platform_src_files
     ${adaptor_thirdparty_dir}/windows-platform/Win32File/CustomFile.cpp
     ${adaptor_thirdparty_dir}/windows-platform/Win32File/MemFile.cpp
     ${adaptor_thirdparty_dir}/windows-platform/Win32File/OriginalFile.cpp
-)
\ No newline at end of file
+)
+
+SET( static_libraries_nanosvg_src_files
+  ${adaptor_thirdparty_dir}/nanosvg/nanosvg.cc
+  ${adaptor_thirdparty_dir}/nanosvg/nanosvgrast.cc
+)
+
diff --git a/third-party/nanosvg/nanosvg.cc b/third-party/nanosvg/nanosvg.cc
new file mode 100755 (executable)
index 0000000..31f5f44
--- /dev/null
@@ -0,0 +1,2855 @@
+/*
+ * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ * The SVG parser is based on Anti-Grain Geometry 2.4 SVG example
+ * Copyright (C) 2002-2004 Maxim Shemanarev (McSeem) (http://www.antigrain.com/)
+ *
+ * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
+ *
+ * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
+ *
+ */
+
+#include "nanosvg.h"
+
+/**
+ * In the original software, The nanosvg implementation was included in the header file.
+ * We have separated the implementation to source file here.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+
+#define NSVG_PI (3.14159265358979323846264338327f)
+#define NSVG_KAPPA90 (0.5522847493f)   // Length proportional to radius of a cubic bezier handle for 90deg arcs.
+
+#define NSVG_ALIGN_MIN 0
+#define NSVG_ALIGN_MID 1
+#define NSVG_ALIGN_MAX 2
+#define NSVG_ALIGN_NONE 0
+#define NSVG_ALIGN_MEET 1
+#define NSVG_ALIGN_SLICE 2
+
+#define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0)
+#define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16))
+
+#ifdef _MSC_VER
+       #pragma warning (disable: 4996) // Switch off security warnings
+       #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
+       #ifdef __cplusplus
+       #define NSVG_INLINE inline
+       #else
+       #define NSVG_INLINE
+       #endif
+#else
+       #define NSVG_INLINE inline
+#endif
+
+
+static int nsvg__isspace(char c)
+{
+       return strchr(" \t\n\v\f\r", c) != 0;
+}
+
+static int nsvg__isdigit(char c)
+{
+       return c >= '0' && c <= '9';
+}
+
+static int nsvg__isnum(char c)
+{
+       return strchr("0123456789+-.eE", c) != 0;
+}
+
+static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; }
+static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; }
+
+
+// Simple XML parser
+
+#define NSVG_XML_TAG 1
+#define NSVG_XML_CONTENT 2
+#define NSVG_XML_MAX_ATTRIBS 256
+
+static void nsvg__parseContent(char* s,
+                                                          void (*contentCb)(void* ud, const char* s),
+                                                          void* ud)
+{
+       // Trim start white spaces
+       while (*s && nsvg__isspace(*s)) s++;
+       if (!*s) return;
+
+       if (contentCb)
+               (*contentCb)(ud, s);
+}
+
+static void nsvg__parseElement(char* s,
+                                                          void (*startelCb)(void* ud, const char* el, const char** attr),
+                                                          void (*endelCb)(void* ud, const char* el),
+                                                          void* ud)
+{
+       const char* attr[NSVG_XML_MAX_ATTRIBS];
+       int nattr = 0;
+       char* name;
+       int start = 0;
+       int end = 0;
+       char quote;
+
+       // Skip white space after the '<'
+       while (*s && nsvg__isspace(*s)) s++;
+
+       // Check if the tag is end tag
+       if (*s == '/') {
+               s++;
+               end = 1;
+       } else {
+               start = 1;
+       }
+
+       // Skip comments, data and preprocessor stuff.
+       if (!*s || *s == '?' || *s == '!')
+               return;
+
+       // Get tag name
+       name = s;
+       while (*s && !nsvg__isspace(*s)) s++;
+       if (*s) { *s++ = '\0'; }
+
+       // Get attribs
+       while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
+               char* name = NULL;
+               char* value = NULL;
+
+               // Skip white space before the attrib name
+               while (*s && nsvg__isspace(*s)) s++;
+               if (!*s) break;
+               if (*s == '/') {
+                       end = 1;
+                       break;
+               }
+               name = s;
+               // Find end of the attrib name.
+               while (*s && !nsvg__isspace(*s) && *s != '=') s++;
+               if (*s) { *s++ = '\0'; }
+               // Skip until the beginning of the value.
+               while (*s && *s != '\"' && *s != '\'') s++;
+               if (!*s) break;
+               quote = *s;
+               s++;
+               // Store value and find the end of it.
+               value = s;
+               while (*s && *s != quote) s++;
+               if (*s) { *s++ = '\0'; }
+
+               // Store only well formed attributes
+               if (name && value) {
+                       attr[nattr++] = name;
+                       attr[nattr++] = value;
+               }
+       }
+
+       // List terminator
+       attr[nattr++] = 0;
+       attr[nattr++] = 0;
+
+       // Call callbacks.
+       if (start && startelCb)
+               (*startelCb)(ud, name, attr);
+       if (end && endelCb)
+               (*endelCb)(ud, name);
+}
+
+int nsvg__parseXML(char* input,
+                                  void (*startelCb)(void* ud, const char* el, const char** attr),
+                                  void (*endelCb)(void* ud, const char* el),
+                                  void (*contentCb)(void* ud, const char* s),
+                                  void* ud)
+{
+       char* s = input;
+       char* mark = s;
+       int state = NSVG_XML_CONTENT;
+       while (*s) {
+               if (*s == '<' && state == NSVG_XML_CONTENT) {
+                       // Start of a tag
+                       *s++ = '\0';
+                       nsvg__parseContent(mark, contentCb, ud);
+                       mark = s;
+                       state = NSVG_XML_TAG;
+               } else if (*s == '>' && state == NSVG_XML_TAG) {
+                       // Start of a content or new tag.
+                       *s++ = '\0';
+                       nsvg__parseElement(mark, startelCb, endelCb, ud);
+                       mark = s;
+                       state = NSVG_XML_CONTENT;
+               } else {
+                       s++;
+               }
+       }
+
+       return 1;
+}
+
+
+/* Simple SVG parser. */
+
+#define NSVG_MAX_ATTR 128
+
+enum NSVGgradientUnits {
+       NSVG_USER_SPACE = 0,
+       NSVG_OBJECT_SPACE = 1
+};
+
+#define NSVG_MAX_DASHES 8
+
+enum NSVGunits {
+       NSVG_UNITS_USER,
+       NSVG_UNITS_PX,
+       NSVG_UNITS_PT,
+       NSVG_UNITS_PC,
+       NSVG_UNITS_MM,
+       NSVG_UNITS_CM,
+       NSVG_UNITS_IN,
+       NSVG_UNITS_PERCENT,
+       NSVG_UNITS_EM,
+       NSVG_UNITS_EX
+};
+
+typedef struct NSVGcoordinate {
+       float value;
+       int units;
+} NSVGcoordinate;
+
+typedef struct NSVGlinearData {
+       NSVGcoordinate x1, y1, x2, y2;
+} NSVGlinearData;
+
+typedef struct NSVGradialData {
+       NSVGcoordinate cx, cy, r, fx, fy;
+} NSVGradialData;
+
+typedef struct NSVGgradientData
+{
+       char id[64];
+       char ref[64];
+  /**
+   * In the original file, using char type (without signed or unsigned) can be interpreted
+   * as 'unsigned char' in some build environments, like ARM architecture.
+   * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
+   */
+    signed char type;
+       union {
+               NSVGlinearData linear;
+               NSVGradialData radial;
+       };
+       char spread;
+       /**
+        * In the original file, using char type (without signed or unsigned) can be interpreted
+        * as 'unsigned char' in some build environments, like ARM architecture.
+        * To prevent the unexpected behavior, we replace 'char units' with 'signed char units' here.
+        */
+    signed char units;
+       float xform[6];
+       int nstops;
+       NSVGgradientStop* stops;
+       struct NSVGgradientData* next;
+} NSVGgradientData;
+
+typedef struct NSVGattrib
+{
+       char id[64];
+       float xform[6];
+       unsigned int fillColor;
+       unsigned int strokeColor;
+       float opacity;
+       float fillOpacity;
+       float strokeOpacity;
+       char fillGradient[64];
+       char strokeGradient[64];
+       float strokeWidth;
+       float strokeDashOffset;
+       float strokeDashArray[NSVG_MAX_DASHES];
+       int strokeDashCount;
+       char strokeLineJoin;
+       char strokeLineCap;
+       float miterLimit;
+       char fillRule;
+       float fontSize;
+       unsigned int stopColor;
+       float stopOpacity;
+       float stopOffset;
+       char hasFill;
+       char hasStroke;
+       char visible;
+} NSVGattrib;
+
+typedef struct NSVGparser
+{
+       NSVGattrib attr[NSVG_MAX_ATTR];
+       int attrHead;
+       float* pts;
+       int npts;
+       int cpts;
+       NSVGpath* plist;
+       NSVGimage* image;
+       NSVGgradientData* gradients;
+       NSVGshape* shapesTail;
+       float viewMinx, viewMiny, viewWidth, viewHeight;
+       int alignX, alignY, alignType;
+       float dpi;
+       char pathFlag;
+       char defsFlag;
+} NSVGparser;
+
+static void nsvg__xformIdentity(float* t)
+{
+       t[0] = 1.0f; t[1] = 0.0f;
+       t[2] = 0.0f; t[3] = 1.0f;
+       t[4] = 0.0f; t[5] = 0.0f;
+}
+
+static void nsvg__xformSetTranslation(float* t, float tx, float ty)
+{
+       t[0] = 1.0f; t[1] = 0.0f;
+       t[2] = 0.0f; t[3] = 1.0f;
+       t[4] = tx; t[5] = ty;
+}
+
+static void nsvg__xformSetScale(float* t, float sx, float sy)
+{
+       t[0] = sx; t[1] = 0.0f;
+       t[2] = 0.0f; t[3] = sy;
+       t[4] = 0.0f; t[5] = 0.0f;
+}
+
+static void nsvg__xformSetSkewX(float* t, float a)
+{
+       t[0] = 1.0f; t[1] = 0.0f;
+       t[2] = tanf(a); t[3] = 1.0f;
+       t[4] = 0.0f; t[5] = 0.0f;
+}
+
+static void nsvg__xformSetSkewY(float* t, float a)
+{
+       t[0] = 1.0f; t[1] = tanf(a);
+       t[2] = 0.0f; t[3] = 1.0f;
+       t[4] = 0.0f; t[5] = 0.0f;
+}
+
+static void nsvg__xformSetRotation(float* t, float a)
+{
+       float cs = cosf(a), sn = sinf(a);
+       t[0] = cs; t[1] = sn;
+       t[2] = -sn; t[3] = cs;
+       t[4] = 0.0f; t[5] = 0.0f;
+}
+
+static void nsvg__xformMultiply(float* t, float* s)
+{
+       float t0 = t[0] * s[0] + t[1] * s[2];
+       float t2 = t[2] * s[0] + t[3] * s[2];
+       float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
+       t[1] = t[0] * s[1] + t[1] * s[3];
+       t[3] = t[2] * s[1] + t[3] * s[3];
+       t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
+       t[0] = t0;
+       t[2] = t2;
+       t[4] = t4;
+}
+
+static void nsvg__xformInverse(float* inv, float* t)
+{
+       double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
+       if (det > -1e-6 && det < 1e-6) {
+               nsvg__xformIdentity(t);
+               return;
+       }
+       invdet = 1.0 / det;
+       inv[0] = (float)(t[3] * invdet);
+       inv[2] = (float)(-t[2] * invdet);
+       inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
+       inv[1] = (float)(-t[1] * invdet);
+       inv[3] = (float)(t[0] * invdet);
+       inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
+}
+
+static void nsvg__xformPremultiply(float* t, float* s)
+{
+       float s2[6];
+       memcpy(s2, s, sizeof(float)*6);
+       nsvg__xformMultiply(s2, t);
+       memcpy(t, s2, sizeof(float)*6);
+}
+
+static void nsvg__xformPoint(float* dx, float* dy, float x, float y, float* t)
+{
+       *dx = x*t[0] + y*t[2] + t[4];
+       *dy = x*t[1] + y*t[3] + t[5];
+}
+
+static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t)
+{
+       *dx = x*t[0] + y*t[2];
+       *dy = x*t[1] + y*t[3];
+}
+
+#define NSVG_EPSILON (1e-12)
+
+static int nsvg__ptInBounds(float* pt, float* bounds)
+{
+       return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3];
+}
+
+
+static double nsvg__evalBezier(double t, double p0, double p1, double p2, double p3)
+{
+       double it = 1.0-t;
+       return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3;
+}
+
+static void nsvg__curveBounds(float* bounds, float* curve)
+{
+       int i, j, count;
+       double roots[2], a, b, c, b2ac, t, v;
+       float* v0 = &curve[0];
+       float* v1 = &curve[2];
+       float* v2 = &curve[4];
+       float* v3 = &curve[6];
+
+       // Start the bounding box by end points
+       bounds[0] = nsvg__minf(v0[0], v3[0]);
+       bounds[1] = nsvg__minf(v0[1], v3[1]);
+       bounds[2] = nsvg__maxf(v0[0], v3[0]);
+       bounds[3] = nsvg__maxf(v0[1], v3[1]);
+
+       // Bezier curve fits inside the convex hull of it's control points.
+       // If control points are inside the bounds, we're done.
+       if (nsvg__ptInBounds(v1, bounds) && nsvg__ptInBounds(v2, bounds))
+               return;
+
+       // Add bezier curve inflection points in X and Y.
+       for (i = 0; i < 2; i++) {
+               a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
+               b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
+               c = 3.0 * v1[i] - 3.0 * v0[i];
+               count = 0;
+               if (fabs(a) < NSVG_EPSILON) {
+                       if (fabs(b) > NSVG_EPSILON) {
+                               t = -c / b;
+                               if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
+                                       roots[count++] = t;
+                       }
+               } else {
+                       b2ac = b*b - 4.0*c*a;
+                       if (b2ac > NSVG_EPSILON) {
+                               t = (-b + sqrt(b2ac)) / (2.0 * a);
+                               if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
+                                       roots[count++] = t;
+                               t = (-b - sqrt(b2ac)) / (2.0 * a);
+                               if (t > NSVG_EPSILON && t < 1.0-NSVG_EPSILON)
+                                       roots[count++] = t;
+                       }
+               }
+               for (j = 0; j < count; j++) {
+                       v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]);
+                       bounds[0+i] = nsvg__minf(bounds[0+i], (float)v);
+                       bounds[2+i] = nsvg__maxf(bounds[2+i], (float)v);
+               }
+       }
+}
+
+static NSVGparser* nsvg__createParser()
+{
+       NSVGparser* p;
+       p = (NSVGparser*)malloc(sizeof(NSVGparser));
+       if (p == NULL) goto error;
+       memset(p, 0, sizeof(NSVGparser));
+
+       p->image = (NSVGimage*)malloc(sizeof(NSVGimage));
+       if (p->image == NULL) goto error;
+       memset(p->image, 0, sizeof(NSVGimage));
+
+       // Init style
+       nsvg__xformIdentity(p->attr[0].xform);
+       memset(p->attr[0].id, 0, sizeof p->attr[0].id);
+       p->attr[0].fillColor = NSVG_RGB(0,0,0);
+       p->attr[0].strokeColor = NSVG_RGB(0,0,0);
+       p->attr[0].opacity = 1;
+       p->attr[0].fillOpacity = 1;
+       p->attr[0].strokeOpacity = 1;
+       p->attr[0].stopOpacity = 1;
+       p->attr[0].strokeWidth = 1;
+       p->attr[0].strokeLineJoin = NSVG_JOIN_MITER;
+       p->attr[0].strokeLineCap = NSVG_CAP_BUTT;
+       p->attr[0].miterLimit = 4;
+       p->attr[0].fillRule = NSVG_FILLRULE_NONZERO;
+       p->attr[0].hasFill = 1;
+       p->attr[0].visible = 1;
+
+       return p;
+
+error:
+       if (p) {
+               if (p->image) free(p->image);
+               free(p);
+       }
+       return NULL;
+}
+
+static void nsvg__deletePaths(NSVGpath* path)
+{
+       while (path) {
+               NSVGpath *next = path->next;
+               if (path->pts != NULL)
+                       free(path->pts);
+               free(path);
+               path = next;
+       }
+}
+
+static void nsvg__deletePaint(NSVGpaint* paint)
+{
+       if (paint->type == NSVG_PAINT_LINEAR_GRADIENT || paint->type == NSVG_PAINT_RADIAL_GRADIENT)
+               free(paint->gradient);
+}
+
+static void nsvg__deleteGradientData(NSVGgradientData* grad)
+{
+       NSVGgradientData* next;
+       while (grad != NULL) {
+               next = grad->next;
+               free(grad->stops);
+               free(grad);
+               grad = next;
+       }
+}
+
+static void nsvg__deleteParser(NSVGparser* p)
+{
+       if (p != NULL) {
+               nsvg__deletePaths(p->plist);
+               nsvg__deleteGradientData(p->gradients);
+               nsvgDelete(p->image);
+               free(p->pts);
+               free(p);
+       }
+}
+
+static void nsvg__resetPath(NSVGparser* p)
+{
+       p->npts = 0;
+}
+
+static void nsvg__addPoint(NSVGparser* p, float x, float y)
+{
+       if (p->npts+1 > p->cpts) {
+               p->cpts = p->cpts ? p->cpts*2 : 8;
+               p->pts = (float*)realloc(p->pts, p->cpts*2*sizeof(float));
+               if (!p->pts) return;
+       }
+       p->pts[p->npts*2+0] = x;
+       p->pts[p->npts*2+1] = y;
+       p->npts++;
+}
+
+static void nsvg__moveTo(NSVGparser* p, float x, float y)
+{
+       if (p->npts > 0) {
+               p->pts[(p->npts-1)*2+0] = x;
+               p->pts[(p->npts-1)*2+1] = y;
+       } else {
+               nsvg__addPoint(p, x, y);
+       }
+}
+
+static void nsvg__lineTo(NSVGparser* p, float x, float y)
+{
+       float px,py, dx,dy;
+       if (p->npts > 0) {
+               px = p->pts[(p->npts-1)*2+0];
+               py = p->pts[(p->npts-1)*2+1];
+               dx = x - px;
+               dy = y - py;
+               nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f);
+               nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f);
+               nsvg__addPoint(p, x, y);
+       }
+}
+
+static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y)
+{
+       nsvg__addPoint(p, cpx1, cpy1);
+       nsvg__addPoint(p, cpx2, cpy2);
+       nsvg__addPoint(p, x, y);
+}
+
+static NSVGattrib* nsvg__getAttr(NSVGparser* p)
+{
+       return &p->attr[p->attrHead];
+}
+
+static void nsvg__pushAttr(NSVGparser* p)
+{
+       if (p->attrHead < NSVG_MAX_ATTR-1) {
+               p->attrHead++;
+               memcpy(&p->attr[p->attrHead], &p->attr[p->attrHead-1], sizeof(NSVGattrib));
+       }
+}
+
+static void nsvg__popAttr(NSVGparser* p)
+{
+       if (p->attrHead > 0)
+               p->attrHead--;
+}
+
+static float nsvg__actualOrigX(NSVGparser* p)
+{
+       return p->viewMinx;
+}
+
+static float nsvg__actualOrigY(NSVGparser* p)
+{
+       return p->viewMiny;
+}
+
+static float nsvg__actualWidth(NSVGparser* p)
+{
+       return p->viewWidth;
+}
+
+static float nsvg__actualHeight(NSVGparser* p)
+{
+       return p->viewHeight;
+}
+
+static float nsvg__actualLength(NSVGparser* p)
+{
+       float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p);
+       return sqrtf(w*w + h*h) / sqrtf(2.0f);
+}
+
+static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, float length)
+{
+       NSVGattrib* attr = nsvg__getAttr(p);
+       switch (c.units) {
+               case NSVG_UNITS_USER:           return c.value;
+               case NSVG_UNITS_PX:                     return c.value;
+               case NSVG_UNITS_PT:                     return c.value / 72.0f * p->dpi;
+               case NSVG_UNITS_PC:                     return c.value / 6.0f * p->dpi;
+               case NSVG_UNITS_MM:                     return c.value / 25.4f * p->dpi;
+               case NSVG_UNITS_CM:                     return c.value / 2.54f * p->dpi;
+               case NSVG_UNITS_IN:                     return c.value * p->dpi;
+               case NSVG_UNITS_EM:                     return c.value * attr->fontSize;
+               case NSVG_UNITS_EX:                     return c.value * attr->fontSize * 0.52f; // x-height of Helvetica.
+               case NSVG_UNITS_PERCENT:        return orig + c.value / 100.0f * length;
+               default:                                        return c.value;
+       }
+       return c.value;
+}
+
+static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id)
+{
+       NSVGgradientData* grad = p->gradients;
+       while (grad) {
+               if (strcmp(grad->id, id) == 0)
+                       return grad;
+               grad = grad->next;
+       }
+       return NULL;
+}
+
+/**
+ * In the original file, using char type (without signed or unsigned) can be interpreted
+ * as 'unsigned char' in some build environments, like ARM architecture.
+ * To prevent the unexpected behavior, we replace 'char paintType' with 'signed char paintType' here.
+ */
+static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, signed char* paintType)
+{
+       NSVGattrib* attr = nsvg__getAttr(p);
+       NSVGgradientData* data = NULL;
+       NSVGgradientData* ref = NULL;
+       NSVGgradientStop* stops = NULL;
+       NSVGgradient* grad;
+       float ox, oy, sw, sh, sl;
+       int nstops = 0;
+
+       data = nsvg__findGradientData(p, id);
+       if (data == NULL) return NULL;
+
+       // TODO: use ref to fill in all unset values too.
+       ref = data;
+       while (ref != NULL) {
+               if (stops == NULL && ref->stops != NULL) {
+                       stops = ref->stops;
+                       nstops = ref->nstops;
+                       break;
+               }
+               ref = nsvg__findGradientData(p, ref->ref);
+       }
+       if (stops == NULL) return NULL;
+
+       grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));
+       if (grad == NULL) return NULL;
+
+       // The shape width and height.
+       if (data->units == NSVG_OBJECT_SPACE) {
+               ox = localBounds[0];
+               oy = localBounds[1];
+               sw = localBounds[2] - localBounds[0];
+               sh = localBounds[3] - localBounds[1];
+       } else {
+               ox = nsvg__actualOrigX(p);
+               oy = nsvg__actualOrigY(p);
+               sw = nsvg__actualWidth(p);
+               sh = nsvg__actualHeight(p);
+       }
+       sl = sqrtf(sw*sw + sh*sh) / sqrtf(2.0f);
+
+       if (data->type == NSVG_PAINT_LINEAR_GRADIENT) {
+               float x1, y1, x2, y2, dx, dy;
+               x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw);
+               y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh);
+               x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw);
+               y2 = nsvg__convertToPixels(p, data->linear.y2, oy, sh);
+               // Calculate transform aligned to the line
+               dx = x2 - x1;
+               dy = y2 - y1;
+               grad->xform[0] = dy; grad->xform[1] = -dx;
+               grad->xform[2] = dx; grad->xform[3] = dy;
+               grad->xform[4] = x1; grad->xform[5] = y1;
+       } else {
+               float cx, cy, fx, fy, r;
+               cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw);
+               cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh);
+               fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw);
+               fy = nsvg__convertToPixels(p, data->radial.fy, oy, sh);
+               r = nsvg__convertToPixels(p, data->radial.r, 0, sl);
+               // Calculate transform aligned to the circle
+               grad->xform[0] = r; grad->xform[1] = 0;
+               grad->xform[2] = 0; grad->xform[3] = r;
+               grad->xform[4] = cx; grad->xform[5] = cy;
+               grad->fx = fx / r;
+               grad->fy = fy / r;
+       }
+
+       nsvg__xformMultiply(grad->xform, data->xform);
+       nsvg__xformMultiply(grad->xform, attr->xform);
+
+       grad->spread = data->spread;
+       memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
+       grad->nstops = nstops;
+
+       *paintType = data->type;
+
+       return grad;
+}
+
+static float nsvg__getAverageScale(float* t)
+{
+       float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
+       float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
+       return (sx + sy) * 0.5f;
+}
+
+static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform)
+{
+       NSVGpath* path;
+       float curve[4*2], curveBounds[4];
+       int i, first = 1;
+       for (path = shape->paths; path != NULL; path = path->next) {
+               nsvg__xformPoint(&curve[0], &curve[1], path->pts[0], path->pts[1], xform);
+               for (i = 0; i < path->npts-1; i += 3) {
+                       nsvg__xformPoint(&curve[2], &curve[3], path->pts[(i+1)*2], path->pts[(i+1)*2+1], xform);
+                       nsvg__xformPoint(&curve[4], &curve[5], path->pts[(i+2)*2], path->pts[(i+2)*2+1], xform);
+                       nsvg__xformPoint(&curve[6], &curve[7], path->pts[(i+3)*2], path->pts[(i+3)*2+1], xform);
+                       nsvg__curveBounds(curveBounds, curve);
+                       if (first) {
+                               bounds[0] = curveBounds[0];
+                               bounds[1] = curveBounds[1];
+                               bounds[2] = curveBounds[2];
+                               bounds[3] = curveBounds[3];
+                               first = 0;
+                       } else {
+                               bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
+                               bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
+                               bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
+                               bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);
+                       }
+                       curve[0] = curve[6];
+                       curve[1] = curve[7];
+               }
+       }
+}
+
+static void nsvg__addShape(NSVGparser* p)
+{
+       NSVGattrib* attr = nsvg__getAttr(p);
+       float scale = 1.0f;
+       NSVGshape* shape;
+       NSVGpath* path;
+       int i;
+
+       if (p->plist == NULL)
+               return;
+
+       shape = (NSVGshape*)malloc(sizeof(NSVGshape));
+       if (shape == NULL) goto error;
+       memset(shape, 0, sizeof(NSVGshape));
+
+       memcpy(shape->id, attr->id, sizeof shape->id);
+       scale = nsvg__getAverageScale(attr->xform);
+       shape->strokeWidth = attr->strokeWidth * scale;
+       shape->strokeDashOffset = attr->strokeDashOffset * scale;
+       shape->strokeDashCount = (char)attr->strokeDashCount;
+       for (i = 0; i < attr->strokeDashCount; i++)
+               shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale;
+       shape->strokeLineJoin = attr->strokeLineJoin;
+       shape->strokeLineCap = attr->strokeLineCap;
+       shape->miterLimit = attr->miterLimit;
+       shape->fillRule = attr->fillRule;
+       shape->opacity = attr->opacity;
+
+       shape->paths = p->plist;
+       p->plist = NULL;
+
+       // Calculate shape bounds
+       shape->bounds[0] = shape->paths->bounds[0];
+       shape->bounds[1] = shape->paths->bounds[1];
+       shape->bounds[2] = shape->paths->bounds[2];
+       shape->bounds[3] = shape->paths->bounds[3];
+       for (path = shape->paths->next; path != NULL; path = path->next) {
+               shape->bounds[0] = nsvg__minf(shape->bounds[0], path->bounds[0]);
+               shape->bounds[1] = nsvg__minf(shape->bounds[1], path->bounds[1]);
+               shape->bounds[2] = nsvg__maxf(shape->bounds[2], path->bounds[2]);
+               shape->bounds[3] = nsvg__maxf(shape->bounds[3], path->bounds[3]);
+       }
+
+       // Set fill
+       if (attr->hasFill == 0) {
+               shape->fill.type = NSVG_PAINT_NONE;
+       } else if (attr->hasFill == 1) {
+               shape->fill.type = NSVG_PAINT_COLOR;
+               shape->fill.color = attr->fillColor;
+               shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24;
+       } else if (attr->hasFill == 2) {
+               float inv[6], localBounds[4];
+               nsvg__xformInverse(inv, attr->xform);
+               nsvg__getLocalBounds(localBounds, shape, inv);
+               shape->fill.gradient = nsvg__createGradient(p, attr->fillGradient, localBounds, &shape->fill.type);
+               if (shape->fill.gradient == NULL) {
+                       shape->fill.type = NSVG_PAINT_NONE;
+               }
+       }
+
+       // Set stroke
+       if (attr->hasStroke == 0) {
+               shape->stroke.type = NSVG_PAINT_NONE;
+       } else if (attr->hasStroke == 1) {
+               shape->stroke.type = NSVG_PAINT_COLOR;
+               shape->stroke.color = attr->strokeColor;
+               shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24;
+       } else if (attr->hasStroke == 2) {
+               float inv[6], localBounds[4];
+               nsvg__xformInverse(inv, attr->xform);
+               nsvg__getLocalBounds(localBounds, shape, inv);
+               shape->stroke.gradient = nsvg__createGradient(p, attr->strokeGradient, localBounds, &shape->stroke.type);
+               if (shape->stroke.gradient == NULL)
+                       shape->stroke.type = NSVG_PAINT_NONE;
+       }
+
+       // Set flags
+       shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00);
+
+       // Add to tail
+       if (p->image->shapes == NULL)
+               p->image->shapes = shape;
+       else
+               p->shapesTail->next = shape;
+       p->shapesTail = shape;
+
+       return;
+
+error:
+       if (shape) free(shape);
+}
+
+static void nsvg__addPath(NSVGparser* p, char closed)
+{
+       NSVGattrib* attr = nsvg__getAttr(p);
+       NSVGpath* path = NULL;
+       float bounds[4];
+       float* curve;
+       int i;
+
+       if (p->npts < 4)
+               return;
+
+       if (closed)
+               nsvg__lineTo(p, p->pts[0], p->pts[1]);
+
+       path = (NSVGpath*)malloc(sizeof(NSVGpath));
+       if (path == NULL) goto error;
+       memset(path, 0, sizeof(NSVGpath));
+
+       path->pts = (float*)malloc(p->npts*2*sizeof(float));
+       if (path->pts == NULL) goto error;
+       path->closed = closed;
+       path->npts = p->npts;
+
+       // Transform path.
+       for (i = 0; i < p->npts; ++i)
+               nsvg__xformPoint(&path->pts[i*2], &path->pts[i*2+1], p->pts[i*2], p->pts[i*2+1], attr->xform);
+
+       // Find bounds
+       for (i = 0; i < path->npts-1; i += 3) {
+               curve = &path->pts[i*2];
+               nsvg__curveBounds(bounds, curve);
+               if (i == 0) {
+                       path->bounds[0] = bounds[0];
+                       path->bounds[1] = bounds[1];
+                       path->bounds[2] = bounds[2];
+                       path->bounds[3] = bounds[3];
+               } else {
+                       path->bounds[0] = nsvg__minf(path->bounds[0], bounds[0]);
+                       path->bounds[1] = nsvg__minf(path->bounds[1], bounds[1]);
+                       path->bounds[2] = nsvg__maxf(path->bounds[2], bounds[2]);
+                       path->bounds[3] = nsvg__maxf(path->bounds[3], bounds[3]);
+               }
+       }
+
+       path->next = p->plist;
+       p->plist = path;
+
+       return;
+
+error:
+       if (path != NULL) {
+               if (path->pts != NULL) free(path->pts);
+               free(path);
+       }
+}
+
+// We roll our own string to float because the std library one uses locale and messes things up.
+static double nsvg__atof(const char* s)
+{
+       char* cur = (char*)s;
+       char* end = NULL;
+       double res = 0.0, sign = 1.0;
+       long long intPart = 0, fracPart = 0;
+       char hasIntPart = 0, hasFracPart = 0;
+
+       // Parse optional sign
+       if (*cur == '+') {
+               cur++;
+       } else if (*cur == '-') {
+               sign = -1;
+               cur++;
+       }
+
+       // Parse integer part
+       if (nsvg__isdigit(*cur)) {
+               // Parse digit sequence
+               intPart = strtoll(cur, &end, 10);
+               if (cur != end) {
+                       res = (double)intPart;
+                       hasIntPart = 1;
+                       cur = end;
+               }
+       }
+
+       // Parse fractional part.
+       if (*cur == '.') {
+               cur++; // Skip '.'
+               if (nsvg__isdigit(*cur)) {
+                       // Parse digit sequence
+                       fracPart = strtoll(cur, &end, 10);
+                       if (cur != end) {
+                               res += (double)fracPart / pow(10.0, (double)(end - cur));
+                               hasFracPart = 1;
+                               cur = end;
+                       }
+               }
+       }
+
+       // A valid number should have integer or fractional part.
+       if (!hasIntPart && !hasFracPart)
+               return 0.0;
+
+       // Parse optional exponent
+       if (*cur == 'e' || *cur == 'E') {
+               long expPart = 0;
+               cur++; // skip 'E'
+               expPart = strtol(cur, &end, 10); // Parse digit sequence with sign
+               if (cur != end) {
+                       res *= pow(10.0, (double)expPart);
+               }
+       }
+
+       return res * sign;
+}
+
+
+static const char* nsvg__parseNumber(const char* s, char* it, const int size)
+{
+       const int last = size-1;
+       int i = 0;
+
+       // sign
+       if (*s == '-' || *s == '+') {
+               if (i < last) it[i++] = *s;
+               s++;
+       }
+       // integer part
+       while (*s && nsvg__isdigit(*s)) {
+               if (i < last) it[i++] = *s;
+               s++;
+       }
+       if (*s == '.') {
+               // decimal point
+               if (i < last) it[i++] = *s;
+               s++;
+               // fraction part
+               while (*s && nsvg__isdigit(*s)) {
+                       if (i < last) it[i++] = *s;
+                       s++;
+               }
+       }
+       // exponent
+       if ((*s == 'e' || *s == 'E') && (s[1] != 'm' && s[1] != 'x')) {
+               if (i < last) it[i++] = *s;
+               s++;
+               if (*s == '-' || *s == '+') {
+                       if (i < last) it[i++] = *s;
+                       s++;
+               }
+               while (*s && nsvg__isdigit(*s)) {
+                       if (i < last) it[i++] = *s;
+                       s++;
+               }
+       }
+       it[i] = '\0';
+
+       return s;
+}
+
+static const char* nsvg__getNextPathItem(const char* s, char* it)
+{
+       it[0] = '\0';
+       // Skip white spaces and commas
+       while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
+       if (!*s) return s;
+       if (*s == '-' || *s == '+' || *s == '.' || nsvg__isdigit(*s)) {
+               s = nsvg__parseNumber(s, it, 64);
+       } else {
+               // Parse command
+               it[0] = *s++;
+               it[1] = '\0';
+               return s;
+       }
+
+       return s;
+}
+
+static unsigned int nsvg__parseColorHex(const char* str)
+{
+       unsigned int c = 0, r = 0, g = 0, b = 0;
+       int n = 0;
+       str++; // skip #
+       // Calculate number of characters.
+       while(str[n] && !nsvg__isspace(str[n]))
+               n++;
+       if (n == 6) {
+               sscanf(str, "%x", &c);
+       } else if (n == 3) {
+               sscanf(str, "%x", &c);
+               c = (c&0xf) | ((c&0xf0) << 4) | ((c&0xf00) << 8);
+               c |= c<<4;
+       }
+       r = (c >> 16) & 0xff;
+       g = (c >> 8) & 0xff;
+       b = c & 0xff;
+       return NSVG_RGB(r,g,b);
+}
+
+static unsigned int nsvg__parseColorRGB(const char* str)
+{
+       int r = -1, g = -1, b = -1;
+       char s1[33]="", s2[33]="";
+       /**
+        * In the original file, the formatted data reading did not specify the string with width limitation.
+        * To prevent the possible overflow, we replace '%s' with '%32s' here.
+        */
+       sscanf(str + 4, "%d%32[%%, \t]%d%32[%%, \t]%d", &r, s1, &g, s2, &b);
+       if (strchr(s1, '%')) {
+               return NSVG_RGB((r*255)/100,(g*255)/100,(b*255)/100);
+       } else {
+               return NSVG_RGB(r,g,b);
+       }
+}
+
+typedef struct NSVGNamedColor {
+       const char* name;
+       unsigned int color;
+} NSVGNamedColor;
+
+NSVGNamedColor nsvg__colors[] = {
+
+       { "red", NSVG_RGB(255, 0, 0) },
+       { "green", NSVG_RGB( 0, 128, 0) },
+       { "blue", NSVG_RGB( 0, 0, 255) },
+       { "yellow", NSVG_RGB(255, 255, 0) },
+       { "cyan", NSVG_RGB( 0, 255, 255) },
+       { "magenta", NSVG_RGB(255, 0, 255) },
+       { "black", NSVG_RGB( 0, 0, 0) },
+       { "grey", NSVG_RGB(128, 128, 128) },
+       { "gray", NSVG_RGB(128, 128, 128) },
+       { "white", NSVG_RGB(255, 255, 255) },
+
+/**
+ * In the original software, it needs to define "NANOSVG_ALL_COLOR_KEYWORDS" in order to support
+ * the following colors. We have removed this because we want to support all the colors.
+ */
+       { "aliceblue", NSVG_RGB(240, 248, 255) },
+       { "antiquewhite", NSVG_RGB(250, 235, 215) },
+       { "aqua", NSVG_RGB( 0, 255, 255) },
+       { "aquamarine", NSVG_RGB(127, 255, 212) },
+       { "azure", NSVG_RGB(240, 255, 255) },
+       { "beige", NSVG_RGB(245, 245, 220) },
+       { "bisque", NSVG_RGB(255, 228, 196) },
+       { "blanchedalmond", NSVG_RGB(255, 235, 205) },
+       { "blueviolet", NSVG_RGB(138, 43, 226) },
+       { "brown", NSVG_RGB(165, 42, 42) },
+       { "burlywood", NSVG_RGB(222, 184, 135) },
+       { "cadetblue", NSVG_RGB( 95, 158, 160) },
+       { "chartreuse", NSVG_RGB(127, 255, 0) },
+       { "chocolate", NSVG_RGB(210, 105, 30) },
+       { "coral", NSVG_RGB(255, 127, 80) },
+       { "cornflowerblue", NSVG_RGB(100, 149, 237) },
+       { "cornsilk", NSVG_RGB(255, 248, 220) },
+       { "crimson", NSVG_RGB(220, 20, 60) },
+       { "darkblue", NSVG_RGB( 0, 0, 139) },
+       { "darkcyan", NSVG_RGB( 0, 139, 139) },
+       { "darkgoldenrod", NSVG_RGB(184, 134, 11) },
+       { "darkgray", NSVG_RGB(169, 169, 169) },
+       { "darkgreen", NSVG_RGB( 0, 100, 0) },
+       { "darkgrey", NSVG_RGB(169, 169, 169) },
+       { "darkkhaki", NSVG_RGB(189, 183, 107) },
+       { "darkmagenta", NSVG_RGB(139, 0, 139) },
+       { "darkolivegreen", NSVG_RGB( 85, 107, 47) },
+       { "darkorange", NSVG_RGB(255, 140, 0) },
+       { "darkorchid", NSVG_RGB(153, 50, 204) },
+       { "darkred", NSVG_RGB(139, 0, 0) },
+       { "darksalmon", NSVG_RGB(233, 150, 122) },
+       { "darkseagreen", NSVG_RGB(143, 188, 143) },
+       { "darkslateblue", NSVG_RGB( 72, 61, 139) },
+       { "darkslategray", NSVG_RGB( 47, 79, 79) },
+       { "darkslategrey", NSVG_RGB( 47, 79, 79) },
+       { "darkturquoise", NSVG_RGB( 0, 206, 209) },
+       { "darkviolet", NSVG_RGB(148, 0, 211) },
+       { "deeppink", NSVG_RGB(255, 20, 147) },
+       { "deepskyblue", NSVG_RGB( 0, 191, 255) },
+       { "dimgray", NSVG_RGB(105, 105, 105) },
+       { "dimgrey", NSVG_RGB(105, 105, 105) },
+       { "dodgerblue", NSVG_RGB( 30, 144, 255) },
+       { "firebrick", NSVG_RGB(178, 34, 34) },
+       { "floralwhite", NSVG_RGB(255, 250, 240) },
+       { "forestgreen", NSVG_RGB( 34, 139, 34) },
+       { "fuchsia", NSVG_RGB(255, 0, 255) },
+       { "gainsboro", NSVG_RGB(220, 220, 220) },
+       { "ghostwhite", NSVG_RGB(248, 248, 255) },
+       { "gold", NSVG_RGB(255, 215, 0) },
+       { "goldenrod", NSVG_RGB(218, 165, 32) },
+       { "greenyellow", NSVG_RGB(173, 255, 47) },
+       { "honeydew", NSVG_RGB(240, 255, 240) },
+       { "hotpink", NSVG_RGB(255, 105, 180) },
+       { "indianred", NSVG_RGB(205, 92, 92) },
+       { "indigo", NSVG_RGB( 75, 0, 130) },
+       { "ivory", NSVG_RGB(255, 255, 240) },
+       { "khaki", NSVG_RGB(240, 230, 140) },
+       { "lavender", NSVG_RGB(230, 230, 250) },
+       { "lavenderblush", NSVG_RGB(255, 240, 245) },
+       { "lawngreen", NSVG_RGB(124, 252, 0) },
+       { "lemonchiffon", NSVG_RGB(255, 250, 205) },
+       { "lightblue", NSVG_RGB(173, 216, 230) },
+       { "lightcoral", NSVG_RGB(240, 128, 128) },
+       { "lightcyan", NSVG_RGB(224, 255, 255) },
+       { "lightgoldenrodyellow", NSVG_RGB(250, 250, 210) },
+       { "lightgray", NSVG_RGB(211, 211, 211) },
+       { "lightgreen", NSVG_RGB(144, 238, 144) },
+       { "lightgrey", NSVG_RGB(211, 211, 211) },
+       { "lightpink", NSVG_RGB(255, 182, 193) },
+       { "lightsalmon", NSVG_RGB(255, 160, 122) },
+       { "lightseagreen", NSVG_RGB( 32, 178, 170) },
+       { "lightskyblue", NSVG_RGB(135, 206, 250) },
+       { "lightslategray", NSVG_RGB(119, 136, 153) },
+       { "lightslategrey", NSVG_RGB(119, 136, 153) },
+       { "lightsteelblue", NSVG_RGB(176, 196, 222) },
+       { "lightyellow", NSVG_RGB(255, 255, 224) },
+       { "lime", NSVG_RGB( 0, 255, 0) },
+       { "limegreen", NSVG_RGB( 50, 205, 50) },
+       { "linen", NSVG_RGB(250, 240, 230) },
+       { "maroon", NSVG_RGB(128, 0, 0) },
+       { "mediumaquamarine", NSVG_RGB(102, 205, 170) },
+       { "mediumblue", NSVG_RGB( 0, 0, 205) },
+       { "mediumorchid", NSVG_RGB(186, 85, 211) },
+       { "mediumpurple", NSVG_RGB(147, 112, 219) },
+       { "mediumseagreen", NSVG_RGB( 60, 179, 113) },
+       { "mediumslateblue", NSVG_RGB(123, 104, 238) },
+       { "mediumspringgreen", NSVG_RGB( 0, 250, 154) },
+       { "mediumturquoise", NSVG_RGB( 72, 209, 204) },
+       { "mediumvioletred", NSVG_RGB(199, 21, 133) },
+       { "midnightblue", NSVG_RGB( 25, 25, 112) },
+       { "mintcream", NSVG_RGB(245, 255, 250) },
+       { "mistyrose", NSVG_RGB(255, 228, 225) },
+       { "moccasin", NSVG_RGB(255, 228, 181) },
+       { "navajowhite", NSVG_RGB(255, 222, 173) },
+       { "navy", NSVG_RGB( 0, 0, 128) },
+       { "oldlace", NSVG_RGB(253, 245, 230) },
+       { "olive", NSVG_RGB(128, 128, 0) },
+       { "olivedrab", NSVG_RGB(107, 142, 35) },
+       { "orange", NSVG_RGB(255, 165, 0) },
+       { "orangered", NSVG_RGB(255, 69, 0) },
+       { "orchid", NSVG_RGB(218, 112, 214) },
+       { "palegoldenrod", NSVG_RGB(238, 232, 170) },
+       { "palegreen", NSVG_RGB(152, 251, 152) },
+       { "paleturquoise", NSVG_RGB(175, 238, 238) },
+       { "palevioletred", NSVG_RGB(219, 112, 147) },
+       { "papayawhip", NSVG_RGB(255, 239, 213) },
+       { "peachpuff", NSVG_RGB(255, 218, 185) },
+       { "peru", NSVG_RGB(205, 133, 63) },
+       { "pink", NSVG_RGB(255, 192, 203) },
+       { "plum", NSVG_RGB(221, 160, 221) },
+       { "powderblue", NSVG_RGB(176, 224, 230) },
+       { "purple", NSVG_RGB(128, 0, 128) },
+       { "rosybrown", NSVG_RGB(188, 143, 143) },
+       { "royalblue", NSVG_RGB( 65, 105, 225) },
+       { "saddlebrown", NSVG_RGB(139, 69, 19) },
+       { "salmon", NSVG_RGB(250, 128, 114) },
+       { "sandybrown", NSVG_RGB(244, 164, 96) },
+       { "seagreen", NSVG_RGB( 46, 139, 87) },
+       { "seashell", NSVG_RGB(255, 245, 238) },
+       { "sienna", NSVG_RGB(160, 82, 45) },
+       { "silver", NSVG_RGB(192, 192, 192) },
+       { "skyblue", NSVG_RGB(135, 206, 235) },
+       { "slateblue", NSVG_RGB(106, 90, 205) },
+       { "slategray", NSVG_RGB(112, 128, 144) },
+       { "slategrey", NSVG_RGB(112, 128, 144) },
+       { "snow", NSVG_RGB(255, 250, 250) },
+       { "springgreen", NSVG_RGB( 0, 255, 127) },
+       { "steelblue", NSVG_RGB( 70, 130, 180) },
+       { "tan", NSVG_RGB(210, 180, 140) },
+       { "teal", NSVG_RGB( 0, 128, 128) },
+       { "thistle", NSVG_RGB(216, 191, 216) },
+       { "tomato", NSVG_RGB(255, 99, 71) },
+       { "turquoise", NSVG_RGB( 64, 224, 208) },
+       { "violet", NSVG_RGB(238, 130, 238) },
+       { "wheat", NSVG_RGB(245, 222, 179) },
+       { "whitesmoke", NSVG_RGB(245, 245, 245) },
+       { "yellowgreen", NSVG_RGB(154, 205, 50) },
+};
+
+static unsigned int nsvg__parseColorName(const char* str)
+{
+       int i, ncolors = sizeof(nsvg__colors) / sizeof(NSVGNamedColor);
+
+       for (i = 0; i < ncolors; i++) {
+               if (strcmp(nsvg__colors[i].name, str) == 0) {
+                       return nsvg__colors[i].color;
+               }
+       }
+
+       return NSVG_RGB(128, 128, 128);
+}
+
+static unsigned int nsvg__parseColor(const char* str)
+{
+       size_t len = 0;
+       while(*str == ' ') ++str;
+       len = strlen(str);
+       if (len >= 1 && *str == '#')
+               return nsvg__parseColorHex(str);
+       else if (len >= 4 && str[0] == 'r' && str[1] == 'g' && str[2] == 'b' && str[3] == '(')
+               return nsvg__parseColorRGB(str);
+       return nsvg__parseColorName(str);
+}
+
+static float nsvg__parseOpacity(const char* str)
+{
+       float val = nsvg__atof(str);
+       if (val < 0.0f) val = 0.0f;
+       if (val > 1.0f) val = 1.0f;
+       return val;
+}
+
+static float nsvg__parseMiterLimit(const char* str)
+{
+       float val = nsvg__atof(str);
+       if (val < 0.0f) val = 0.0f;
+       return val;
+}
+
+static int nsvg__parseUnits(const char* units)
+{
+       if (units[0] == 'p' && units[1] == 'x')
+               return NSVG_UNITS_PX;
+       else if (units[0] == 'p' && units[1] == 't')
+               return NSVG_UNITS_PT;
+       else if (units[0] == 'p' && units[1] == 'c')
+               return NSVG_UNITS_PC;
+       else if (units[0] == 'm' && units[1] == 'm')
+               return NSVG_UNITS_MM;
+       else if (units[0] == 'c' && units[1] == 'm')
+               return NSVG_UNITS_CM;
+       else if (units[0] == 'i' && units[1] == 'n')
+               return NSVG_UNITS_IN;
+       else if (units[0] == '%')
+               return NSVG_UNITS_PERCENT;
+       else if (units[0] == 'e' && units[1] == 'm')
+               return NSVG_UNITS_EM;
+       else if (units[0] == 'e' && units[1] == 'x')
+               return NSVG_UNITS_EX;
+       return NSVG_UNITS_USER;
+}
+
+static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str)
+{
+       NSVGcoordinate coord = {0, NSVG_UNITS_USER};
+       char buf[64];
+       coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
+       coord.value = nsvg__atof(buf);
+       return coord;
+}
+
+static NSVGcoordinate nsvg__coord(float v, int units)
+{
+       NSVGcoordinate coord = {v, units};
+       return coord;
+}
+
+static float nsvg__parseCoordinate(NSVGparser* p, const char* str, float orig, float length)
+{
+       NSVGcoordinate coord = nsvg__parseCoordinateRaw(str);
+       return nsvg__convertToPixels(p, coord, orig, length);
+}
+
+static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int* na)
+{
+       const char* end;
+       const char* ptr;
+       char it[64];
+
+       *na = 0;
+       ptr = str;
+       while (*ptr && *ptr != '(') ++ptr;
+       if (*ptr == 0)
+               return 1;
+       end = ptr;
+       while (*end && *end != ')') ++end;
+       if (*end == 0)
+               return 1;
+
+       while (ptr < end) {
+               if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) {
+                       if (*na >= maxNa) return 0;
+                       ptr = nsvg__parseNumber(ptr, it, 64);
+                       args[(*na)++] = (float)nsvg__atof(it);
+               } else {
+                       ++ptr;
+               }
+       }
+       return (int)(end - str);
+}
+
+
+static int nsvg__parseMatrix(float* xform, const char* str)
+{
+       float t[6];
+       int na = 0;
+       int len = nsvg__parseTransformArgs(str, t, 6, &na);
+       if (na != 6) return len;
+       memcpy(xform, t, sizeof(float)*6);
+       return len;
+}
+
+static int nsvg__parseTranslate(float* xform, const char* str)
+{
+       float args[2];
+       float t[6];
+       int na = 0;
+       int len = nsvg__parseTransformArgs(str, args, 2, &na);
+       if (na == 1) args[1] = 0.0;
+
+       nsvg__xformSetTranslation(t, args[0], args[1]);
+       memcpy(xform, t, sizeof(float)*6);
+       return len;
+}
+
+static int nsvg__parseScale(float* xform, const char* str)
+{
+       float args[2];
+       int na = 0;
+       float t[6];
+       int len = nsvg__parseTransformArgs(str, args, 2, &na);
+       if (na == 1) args[1] = args[0];
+       nsvg__xformSetScale(t, args[0], args[1]);
+       memcpy(xform, t, sizeof(float)*6);
+       return len;
+}
+
+static int nsvg__parseSkewX(float* xform, const char* str)
+{
+       float args[1];
+       int na = 0;
+       float t[6];
+       int len = nsvg__parseTransformArgs(str, args, 1, &na);
+       nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI);
+       memcpy(xform, t, sizeof(float)*6);
+       return len;
+}
+
+static int nsvg__parseSkewY(float* xform, const char* str)
+{
+       float args[1];
+       int na = 0;
+       float t[6];
+       int len = nsvg__parseTransformArgs(str, args, 1, &na);
+       nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI);
+       memcpy(xform, t, sizeof(float)*6);
+       return len;
+}
+
+static int nsvg__parseRotate(float* xform, const char* str)
+{
+       float args[3];
+       int na = 0;
+       float m[6];
+       float t[6];
+       int len = nsvg__parseTransformArgs(str, args, 3, &na);
+       if (na == 1)
+               args[1] = args[2] = 0.0f;
+       nsvg__xformIdentity(m);
+
+       if (na > 1) {
+               nsvg__xformSetTranslation(t, -args[1], -args[2]);
+               nsvg__xformMultiply(m, t);
+       }
+
+       nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI);
+       nsvg__xformMultiply(m, t);
+
+       if (na > 1) {
+               nsvg__xformSetTranslation(t, args[1], args[2]);
+               nsvg__xformMultiply(m, t);
+       }
+
+       memcpy(xform, m, sizeof(float)*6);
+
+       return len;
+}
+
+static void nsvg__parseTransform(float* xform, const char* str)
+{
+       float t[6];
+       nsvg__xformIdentity(xform);
+       while (*str)
+       {
+               if (strncmp(str, "matrix", 6) == 0)
+                       str += nsvg__parseMatrix(t, str);
+               else if (strncmp(str, "translate", 9) == 0)
+                       str += nsvg__parseTranslate(t, str);
+               else if (strncmp(str, "scale", 5) == 0)
+                       str += nsvg__parseScale(t, str);
+               else if (strncmp(str, "rotate", 6) == 0)
+                       str += nsvg__parseRotate(t, str);
+               else if (strncmp(str, "skewX", 5) == 0)
+                       str += nsvg__parseSkewX(t, str);
+               else if (strncmp(str, "skewY", 5) == 0)
+                       str += nsvg__parseSkewY(t, str);
+               else{
+                       ++str;
+                       continue;
+               }
+
+               nsvg__xformPremultiply(xform, t);
+       }
+}
+
+static void nsvg__parseUrl(char* id, const char* str)
+{
+       int i = 0;
+       str += 4; // "url(";
+       if (*str == '#')
+               str++;
+       while (i < 63 && *str != ')') {
+               id[i] = *str++;
+               i++;
+       }
+       id[i] = '\0';
+}
+
+static char nsvg__parseLineCap(const char* str)
+{
+       if (strcmp(str, "butt") == 0)
+               return NSVG_CAP_BUTT;
+       else if (strcmp(str, "round") == 0)
+               return NSVG_CAP_ROUND;
+       else if (strcmp(str, "square") == 0)
+               return NSVG_CAP_SQUARE;
+       // TODO: handle inherit.
+       return NSVG_CAP_BUTT;
+}
+
+static char nsvg__parseLineJoin(const char* str)
+{
+       if (strcmp(str, "miter") == 0)
+               return NSVG_JOIN_MITER;
+       else if (strcmp(str, "round") == 0)
+               return NSVG_JOIN_ROUND;
+       else if (strcmp(str, "bevel") == 0)
+               return NSVG_JOIN_BEVEL;
+       // TODO: handle inherit.
+       return NSVG_JOIN_MITER;
+}
+
+static char nsvg__parseFillRule(const char* str)
+{
+       if (strcmp(str, "nonzero") == 0)
+               return NSVG_FILLRULE_NONZERO;
+       else if (strcmp(str, "evenodd") == 0)
+               return NSVG_FILLRULE_EVENODD;
+       // TODO: handle inherit.
+       return NSVG_FILLRULE_NONZERO;
+}
+
+static const char* nsvg__getNextDashItem(const char* s, char* it)
+{
+       int n = 0;
+       it[0] = '\0';
+       // Skip white spaces and commas
+       while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
+       // Advance until whitespace, comma or end.
+       while (*s && (!nsvg__isspace(*s) && *s != ',')) {
+               if (n < 63)
+                       it[n++] = *s;
+               s++;
+       }
+       it[n++] = '\0';
+       return s;
+}
+
+static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* strokeDashArray)
+{
+       char item[64];
+       int count = 0, i;
+       float sum = 0.0f;
+
+       // Handle "none"
+       if (str[0] == 'n')
+               return 0;
+
+       // Parse dashes
+       while (*str) {
+               str = nsvg__getNextDashItem(str, item);
+               if (!*item) break;
+               if (count < NSVG_MAX_DASHES)
+                       strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p)));
+       }
+
+       for (i = 0; i < count; i++)
+               sum += strokeDashArray[i];
+       if (sum <= 1e-6f)
+               count = 0;
+
+       return count;
+}
+
+static void nsvg__parseStyle(NSVGparser* p, const char* str);
+
+static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value)
+{
+       float xform[6];
+       NSVGattrib* attr = nsvg__getAttr(p);
+       if (!attr) return 0;
+
+       if (strcmp(name, "style") == 0) {
+               nsvg__parseStyle(p, value);
+       } else if (strcmp(name, "display") == 0) {
+               if (strcmp(value, "none") == 0)
+                       attr->visible = 0;
+               // Don't reset ->visible on display:inline, one display:none hides the whole subtree
+
+       } else if (strcmp(name, "fill") == 0) {
+               if (strcmp(value, "none") == 0) {
+                       attr->hasFill = 0;
+               } else if (strncmp(value, "url(", 4) == 0) {
+                       attr->hasFill = 2;
+                       nsvg__parseUrl(attr->fillGradient, value);
+               } else {
+                       attr->hasFill = 1;
+                       attr->fillColor = nsvg__parseColor(value);
+               }
+       } else if (strcmp(name, "opacity") == 0) {
+               attr->opacity = nsvg__parseOpacity(value);
+       } else if (strcmp(name, "fill-opacity") == 0) {
+               attr->fillOpacity = nsvg__parseOpacity(value);
+       } else if (strcmp(name, "stroke") == 0) {
+               if (strcmp(value, "none") == 0) {
+                       attr->hasStroke = 0;
+               } else if (strncmp(value, "url(", 4) == 0) {
+                       attr->hasStroke = 2;
+                       nsvg__parseUrl(attr->strokeGradient, value);
+               } else {
+                       attr->hasStroke = 1;
+                       attr->strokeColor = nsvg__parseColor(value);
+               }
+       } else if (strcmp(name, "stroke-width") == 0) {
+               attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
+       } else if (strcmp(name, "stroke-dasharray") == 0) {
+               attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray);
+       } else if (strcmp(name, "stroke-dashoffset") == 0) {
+               attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
+       } else if (strcmp(name, "stroke-opacity") == 0) {
+               attr->strokeOpacity = nsvg__parseOpacity(value);
+       } else if (strcmp(name, "stroke-linecap") == 0) {
+               attr->strokeLineCap = nsvg__parseLineCap(value);
+       } else if (strcmp(name, "stroke-linejoin") == 0) {
+               attr->strokeLineJoin = nsvg__parseLineJoin(value);
+       } else if (strcmp(name, "stroke-miterlimit") == 0) {
+               attr->miterLimit = nsvg__parseMiterLimit(value);
+       } else if (strcmp(name, "fill-rule") == 0) {
+               attr->fillRule = nsvg__parseFillRule(value);
+       } else if (strcmp(name, "font-size") == 0) {
+               attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p));
+       } else if (strcmp(name, "transform") == 0) {
+               nsvg__parseTransform(xform, value);
+               nsvg__xformPremultiply(attr->xform, xform);
+       } else if (strcmp(name, "stop-color") == 0) {
+               attr->stopColor = nsvg__parseColor(value);
+       } else if (strcmp(name, "stop-opacity") == 0) {
+               attr->stopOpacity = nsvg__parseOpacity(value);
+       } else if (strcmp(name, "offset") == 0) {
+               attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f);
+       } else if (strcmp(name, "id") == 0) {
+               strncpy(attr->id, value, 63);
+               attr->id[63] = '\0';
+       } else {
+               return 0;
+       }
+       return 1;
+}
+
+static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* end)
+{
+       const char* str;
+       const char* val;
+       char name[512];
+       char value[512];
+       int n;
+
+       str = start;
+       while (str < end && *str != ':') ++str;
+
+       val = str;
+
+       // Right Trim
+       while (str > start &&  (*str == ':' || nsvg__isspace(*str))) --str;
+       ++str;
+
+       n = (int)(str - start);
+       if (n > 511) n = 511;
+       if (n) memcpy(name, start, n);
+       name[n] = 0;
+
+       while (val < end && (*val == ':' || nsvg__isspace(*val))) ++val;
+
+       n = (int)(end - val);
+       if (n > 511) n = 511;
+       if (n) memcpy(value, val, n);
+       value[n] = 0;
+
+       return nsvg__parseAttr(p, name, value);
+}
+
+static void nsvg__parseStyle(NSVGparser* p, const char* str)
+{
+       const char* start;
+       const char* end;
+
+       while (*str) {
+               // Left Trim
+               while(*str && nsvg__isspace(*str)) ++str;
+               start = str;
+               while(*str && *str != ';') ++str;
+               end = str;
+
+               // Right Trim
+               while (end > start &&  (*end == ';' || nsvg__isspace(*end))) --end;
+               ++end;
+
+               nsvg__parseNameValue(p, start, end);
+               if (*str) ++str;
+       }
+}
+
+static void nsvg__parseAttribs(NSVGparser* p, const char** attr)
+{
+       int i;
+       for (i = 0; attr[i]; i += 2)
+       {
+               if (strcmp(attr[i], "style") == 0)
+                       nsvg__parseStyle(p, attr[i + 1]);
+               else
+                       nsvg__parseAttr(p, attr[i], attr[i + 1]);
+       }
+}
+
+static int nsvg__getArgsPerElement(char cmd)
+{
+       switch (cmd) {
+               case 'v':
+               case 'V':
+               case 'h':
+               case 'H':
+                       return 1;
+               case 'm':
+               case 'M':
+               case 'l':
+               case 'L':
+               case 't':
+               case 'T':
+                       return 2;
+               case 'q':
+               case 'Q':
+               case 's':
+               case 'S':
+                       return 4;
+               case 'c':
+               case 'C':
+                       return 6;
+               case 'a':
+               case 'A':
+                       return 7;
+       }
+       return 0;
+}
+
+static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
+{
+       if (rel) {
+               *cpx += args[0];
+               *cpy += args[1];
+       } else {
+               *cpx = args[0];
+               *cpy = args[1];
+       }
+       nsvg__moveTo(p, *cpx, *cpy);
+}
+
+static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
+{
+       if (rel) {
+               *cpx += args[0];
+               *cpy += args[1];
+       } else {
+               *cpx = args[0];
+               *cpy = args[1];
+       }
+       nsvg__lineTo(p, *cpx, *cpy);
+}
+
+static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
+{
+       if (rel)
+               *cpx += args[0];
+       else
+               *cpx = args[0];
+       nsvg__lineTo(p, *cpx, *cpy);
+}
+
+static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
+{
+       if (rel)
+               *cpy += args[0];
+       else
+               *cpy = args[0];
+       nsvg__lineTo(p, *cpx, *cpy);
+}
+
+static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy,
+                                                                float* cpx2, float* cpy2, float* args, int rel)
+{
+       float x2, y2, cx1, cy1, cx2, cy2;
+
+       if (rel) {
+               cx1 = *cpx + args[0];
+               cy1 = *cpy + args[1];
+               cx2 = *cpx + args[2];
+               cy2 = *cpy + args[3];
+               x2 = *cpx + args[4];
+               y2 = *cpy + args[5];
+       } else {
+               cx1 = args[0];
+               cy1 = args[1];
+               cx2 = args[2];
+               cy2 = args[3];
+               x2 = args[4];
+               y2 = args[5];
+       }
+
+       nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
+
+       *cpx2 = cx2;
+       *cpy2 = cy2;
+       *cpx = x2;
+       *cpy = y2;
+}
+
+static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy,
+                                                                         float* cpx2, float* cpy2, float* args, int rel)
+{
+       float x1, y1, x2, y2, cx1, cy1, cx2, cy2;
+
+       x1 = *cpx;
+       y1 = *cpy;
+       if (rel) {
+               cx2 = *cpx + args[0];
+               cy2 = *cpy + args[1];
+               x2 = *cpx + args[2];
+               y2 = *cpy + args[3];
+       } else {
+               cx2 = args[0];
+               cy2 = args[1];
+               x2 = args[2];
+               y2 = args[3];
+       }
+
+       cx1 = 2*x1 - *cpx2;
+       cy1 = 2*y1 - *cpy2;
+
+       nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
+
+       *cpx2 = cx2;
+       *cpy2 = cy2;
+       *cpx = x2;
+       *cpy = y2;
+}
+
+static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy,
+                                                               float* cpx2, float* cpy2, float* args, int rel)
+{
+       float x1, y1, x2, y2, cx, cy;
+       float cx1, cy1, cx2, cy2;
+
+       x1 = *cpx;
+       y1 = *cpy;
+       if (rel) {
+               cx = *cpx + args[0];
+               cy = *cpy + args[1];
+               x2 = *cpx + args[2];
+               y2 = *cpy + args[3];
+       } else {
+               cx = args[0];
+               cy = args[1];
+               x2 = args[2];
+               y2 = args[3];
+       }
+
+       // Convert to cubic bezier
+       cx1 = x1 + 2.0f/3.0f*(cx - x1);
+       cy1 = y1 + 2.0f/3.0f*(cy - y1);
+       cx2 = x2 + 2.0f/3.0f*(cx - x2);
+       cy2 = y2 + 2.0f/3.0f*(cy - y2);
+
+       nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
+
+       *cpx2 = cx;
+       *cpy2 = cy;
+       *cpx = x2;
+       *cpy = y2;
+}
+
+static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy,
+                                                                        float* cpx2, float* cpy2, float* args, int rel)
+{
+       float x1, y1, x2, y2, cx, cy;
+       float cx1, cy1, cx2, cy2;
+
+       x1 = *cpx;
+       y1 = *cpy;
+       if (rel) {
+               x2 = *cpx + args[0];
+               y2 = *cpy + args[1];
+       } else {
+               x2 = args[0];
+               y2 = args[1];
+       }
+
+       cx = 2*x1 - *cpx2;
+       cy = 2*y1 - *cpy2;
+
+       // Convert to cubix bezier
+       cx1 = x1 + 2.0f/3.0f*(cx - x1);
+       cy1 = y1 + 2.0f/3.0f*(cy - y1);
+       cx2 = x2 + 2.0f/3.0f*(cx - x2);
+       cy2 = y2 + 2.0f/3.0f*(cy - y2);
+
+       nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2);
+
+       *cpx2 = cx;
+       *cpy2 = cy;
+       *cpx = x2;
+       *cpy = y2;
+}
+
+static float nsvg__sqr(float x) { return x*x; }
+static float nsvg__vmag(float x, float y) { return sqrtf(x*x + y*y); }
+
+static float nsvg__vecrat(float ux, float uy, float vx, float vy)
+{
+       return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy));
+}
+
+static float nsvg__vecang(float ux, float uy, float vx, float vy)
+{
+       float r = nsvg__vecrat(ux,uy, vx,vy);
+       if (r < -1.0f) r = -1.0f;
+       if (r > 1.0f) r = 1.0f;
+       return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r);
+}
+
+static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel)
+{
+       // Ported from canvg (https://code.google.com/p/canvg/)
+       float rx, ry, rotx;
+       float x1, y1, x2, y2, cx, cy, dx, dy, d;
+       float x1p, y1p, cxp, cyp, s, sa, sb;
+       float ux, uy, vx, vy, a1, da;
+       float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
+       float sinrx, cosrx;
+       int fa, fs;
+       int i, ndivs;
+       float hda, kappa;
+
+       rx = fabsf(args[0]);                            // y radius
+       ry = fabsf(args[1]);                            // x radius
+       rotx = args[2] / 180.0f * NSVG_PI;              // x rotation angle
+       fa = fabsf(args[3]) > 1e-6 ? 1 : 0;     // Large arc
+       fs = fabsf(args[4]) > 1e-6 ? 1 : 0;     // Sweep direction
+       x1 = *cpx;                                                      // start point
+       y1 = *cpy;
+       if (rel) {                                                      // end point
+               x2 = *cpx + args[5];
+               y2 = *cpy + args[6];
+       } else {
+               x2 = args[5];
+               y2 = args[6];
+       }
+
+       dx = x1 - x2;
+       dy = y1 - y2;
+       d = sqrtf(dx*dx + dy*dy);
+       if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) {
+               // The arc degenerates to a line
+               nsvg__lineTo(p, x2, y2);
+               *cpx = x2;
+               *cpy = y2;
+               return;
+       }
+
+       sinrx = sinf(rotx);
+       cosrx = cosf(rotx);
+
+       // Convert to center point parameterization.
+       // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
+       // 1) Compute x1', y1'
+       x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f;
+       y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f;
+       d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry);
+       if (d > 1) {
+               d = sqrtf(d);
+               rx *= d;
+               ry *= d;
+       }
+       // 2) Compute cx', cy'
+       s = 0.0f;
+       sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p);
+       sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p);
+       if (sa < 0.0f) sa = 0.0f;
+       if (sb > 0.0f)
+               s = sqrtf(sa / sb);
+       if (fa == fs)
+               s = -s;
+       cxp = s * rx * y1p / ry;
+       cyp = s * -ry * x1p / rx;
+
+       // 3) Compute cx,cy from cx',cy'
+       cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp;
+       cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp;
+
+       // 4) Calculate theta1, and delta theta.
+       ux = (x1p - cxp) / rx;
+       uy = (y1p - cyp) / ry;
+       vx = (-x1p - cxp) / rx;
+       vy = (-y1p - cyp) / ry;
+       a1 = nsvg__vecang(1.0f,0.0f, ux,uy);    // Initial angle
+       da = nsvg__vecang(ux,uy, vx,vy);                // Delta angle
+
+//     if (vecrat(ux,uy,vx,vy) <= -1.0f) da = NSVG_PI;
+//     if (vecrat(ux,uy,vx,vy) >= 1.0f) da = 0;
+
+       if (fs == 0 && da > 0)
+               da -= 2 * NSVG_PI;
+       else if (fs == 1 && da < 0)
+               da += 2 * NSVG_PI;
+
+       // Approximate the arc using cubic spline segments.
+       t[0] = cosrx; t[1] = sinrx;
+       t[2] = -sinrx; t[3] = cosrx;
+       t[4] = cx; t[5] = cy;
+
+       // Split arc into max 90 degree segments.
+       // The loop assumes an iteration per end point (including start and end), this +1.
+       ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f);
+       hda = (da / (float)ndivs) / 2.0f;
+       kappa = fabsf(4.0f / 3.0f * (1.0f - cosf(hda)) / sinf(hda));
+       if (da < 0.0f)
+               kappa = -kappa;
+
+       for (i = 0; i <= ndivs; i++) {
+               a = a1 + da * ((float)i/(float)ndivs);
+               dx = cosf(a);
+               dy = sinf(a);
+               nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position
+               nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t); // tangent
+               if (i > 0)
+                       nsvg__cubicBezTo(p, px+ptanx,py+ptany, x-tanx, y-tany, x, y);
+               px = x;
+               py = y;
+               ptanx = tanx;
+               ptany = tany;
+       }
+
+       *cpx = x2;
+       *cpy = y2;
+}
+
+static void nsvg__parsePath(NSVGparser* p, const char** attr)
+{
+       const char* s = NULL;
+       char cmd = '\0';
+       float args[10];
+       int nargs;
+       int rargs = 0;
+       float cpx, cpy, cpx2, cpy2;
+       const char* tmp[4];
+       char closedFlag;
+       int i;
+       char item[64];
+
+       for (i = 0; attr[i]; i += 2) {
+               if (strcmp(attr[i], "d") == 0) {
+                       s = attr[i + 1];
+               } else {
+                       tmp[0] = attr[i];
+                       tmp[1] = attr[i + 1];
+                       tmp[2] = 0;
+                       tmp[3] = 0;
+                       nsvg__parseAttribs(p, tmp);
+               }
+       }
+
+       if (s) {
+               nsvg__resetPath(p);
+               cpx = 0; cpy = 0;
+               cpx2 = 0; cpy2 = 0;
+               closedFlag = 0;
+               nargs = 0;
+
+               while (*s) {
+                       s = nsvg__getNextPathItem(s, item);
+                       if (!*item) break;
+                       if (nsvg__isnum(item[0])) {
+                               if (nargs < 10)
+                                       args[nargs++] = (float)nsvg__atof(item);
+                               if (nargs >= rargs) {
+                                       switch (cmd) {
+                                               case 'm':
+                                               case 'M':
+                                                       nsvg__pathMoveTo(p, &cpx, &cpy, args, cmd == 'm' ? 1 : 0);
+                                                       // Moveto can be followed by multiple coordinate pairs,
+                                                       // which should be treated as linetos.
+                                                       cmd = (cmd == 'm') ? 'l' : 'L';
+                                                       rargs = nsvg__getArgsPerElement(cmd);
+                                                       cpx2 = cpx; cpy2 = cpy;
+                                                       break;
+                                               case 'l':
+                                               case 'L':
+                                                       nsvg__pathLineTo(p, &cpx, &cpy, args, cmd == 'l' ? 1 : 0);
+                                                       cpx2 = cpx; cpy2 = cpy;
+                                                       break;
+                                               case 'H':
+                                               case 'h':
+                                                       nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd == 'h' ? 1 : 0);
+                                                       cpx2 = cpx; cpy2 = cpy;
+                                                       break;
+                                               case 'V':
+                                               case 'v':
+                                                       nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd == 'v' ? 1 : 0);
+                                                       cpx2 = cpx; cpy2 = cpy;
+                                                       break;
+                                               case 'C':
+                                               case 'c':
+                                                       nsvg__pathCubicBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'c' ? 1 : 0);
+                                                       break;
+                                               case 'S':
+                                               case 's':
+                                                       nsvg__pathCubicBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 's' ? 1 : 0);
+                                                       break;
+                                               case 'Q':
+                                               case 'q':
+                                                       nsvg__pathQuadBezTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 'q' ? 1 : 0);
+                                                       break;
+                                               case 'T':
+                                               case 't':
+                                                       nsvg__pathQuadBezShortTo(p, &cpx, &cpy, &cpx2, &cpy2, args, cmd == 't' ? 1 : 0);
+                                                       break;
+                                               case 'A':
+                                               case 'a':
+                                                       nsvg__pathArcTo(p, &cpx, &cpy, args, cmd == 'a' ? 1 : 0);
+                                                       cpx2 = cpx; cpy2 = cpy;
+                                                       break;
+                                               default:
+                                                       if (nargs >= 2) {
+                                                               cpx = args[nargs-2];
+                                                               cpy = args[nargs-1];
+                                                               cpx2 = cpx; cpy2 = cpy;
+                                                       }
+                                                       break;
+                                       }
+                                       nargs = 0;
+                               }
+                       } else {
+                               cmd = item[0];
+                               rargs = nsvg__getArgsPerElement(cmd);
+                               if (cmd == 'M' || cmd == 'm') {
+                                       // Commit path.
+                                       if (p->npts > 0)
+                                               nsvg__addPath(p, closedFlag);
+                                       // Start new subpath.
+                                       nsvg__resetPath(p);
+                                       closedFlag = 0;
+                                       nargs = 0;
+                               } else if (cmd == 'Z' || cmd == 'z') {
+                                       closedFlag = 1;
+                                       // Commit path.
+                                       if (p->npts > 0) {
+                                               // Move current point to first point
+                                               cpx = p->pts[0];
+                                               cpy = p->pts[1];
+                                               cpx2 = cpx; cpy2 = cpy;
+                                               nsvg__addPath(p, closedFlag);
+                                       }
+                                       // Start new subpath.
+                                       nsvg__resetPath(p);
+                                       nsvg__moveTo(p, cpx, cpy);
+                                       closedFlag = 0;
+                                       nargs = 0;
+                               }
+                       }
+               }
+               // Commit path.
+               if (p->npts)
+                       nsvg__addPath(p, closedFlag);
+       }
+
+       nsvg__addShape(p);
+}
+
+static void nsvg__parseRect(NSVGparser* p, const char** attr)
+{
+       float x = 0.0f;
+       float y = 0.0f;
+       float w = 0.0f;
+       float h = 0.0f;
+       float rx = -1.0f; // marks not set
+       float ry = -1.0f;
+       int i;
+
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
+                       if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p));
+                       if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
+                       if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
+               }
+       }
+
+       if (rx < 0.0f && ry > 0.0f) rx = ry;
+       if (ry < 0.0f && rx > 0.0f) ry = rx;
+       if (rx < 0.0f) rx = 0.0f;
+       if (ry < 0.0f) ry = 0.0f;
+       if (rx > w/2.0f) rx = w/2.0f;
+       if (ry > h/2.0f) ry = h/2.0f;
+
+       if (w != 0.0f && h != 0.0f) {
+               nsvg__resetPath(p);
+
+               if (rx < 0.00001f || ry < 0.0001f) {
+                       nsvg__moveTo(p, x, y);
+                       nsvg__lineTo(p, x+w, y);
+                       nsvg__lineTo(p, x+w, y+h);
+                       nsvg__lineTo(p, x, y+h);
+               } else {
+                       // Rounded rectangle
+                       nsvg__moveTo(p, x+rx, y);
+                       nsvg__lineTo(p, x+w-rx, y);
+                       nsvg__cubicBezTo(p, x+w-rx*(1-NSVG_KAPPA90), y, x+w, y+ry*(1-NSVG_KAPPA90), x+w, y+ry);
+                       nsvg__lineTo(p, x+w, y+h-ry);
+                       nsvg__cubicBezTo(p, x+w, y+h-ry*(1-NSVG_KAPPA90), x+w-rx*(1-NSVG_KAPPA90), y+h, x+w-rx, y+h);
+                       nsvg__lineTo(p, x+rx, y+h);
+                       nsvg__cubicBezTo(p, x+rx*(1-NSVG_KAPPA90), y+h, x, y+h-ry*(1-NSVG_KAPPA90), x, y+h-ry);
+                       nsvg__lineTo(p, x, y+ry);
+                       nsvg__cubicBezTo(p, x, y+ry*(1-NSVG_KAPPA90), x+rx*(1-NSVG_KAPPA90), y, x+rx, y);
+               }
+
+               nsvg__addPath(p, 1);
+
+               nsvg__addShape(p);
+       }
+}
+
+static void nsvg__parseCircle(NSVGparser* p, const char** attr)
+{
+       float cx = 0.0f;
+       float cy = 0.0f;
+       float r = 0.0f;
+       int i;
+
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
+                       if (strcmp(attr[i], "r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p)));
+               }
+       }
+
+       if (r > 0.0f) {
+               nsvg__resetPath(p);
+
+               nsvg__moveTo(p, cx+r, cy);
+               nsvg__cubicBezTo(p, cx+r, cy+r*NSVG_KAPPA90, cx+r*NSVG_KAPPA90, cy+r, cx, cy+r);
+               nsvg__cubicBezTo(p, cx-r*NSVG_KAPPA90, cy+r, cx-r, cy+r*NSVG_KAPPA90, cx-r, cy);
+               nsvg__cubicBezTo(p, cx-r, cy-r*NSVG_KAPPA90, cx-r*NSVG_KAPPA90, cy-r, cx, cy-r);
+               nsvg__cubicBezTo(p, cx+r*NSVG_KAPPA90, cy-r, cx+r, cy-r*NSVG_KAPPA90, cx+r, cy);
+
+               nsvg__addPath(p, 1);
+
+               nsvg__addShape(p);
+       }
+}
+
+static void nsvg__parseEllipse(NSVGparser* p, const char** attr)
+{
+       float cx = 0.0f;
+       float cy = 0.0f;
+       float rx = 0.0f;
+       float ry = 0.0f;
+       int i;
+
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
+                       if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)));
+                       if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)));
+               }
+       }
+
+       if (rx > 0.0f && ry > 0.0f) {
+
+               nsvg__resetPath(p);
+
+               nsvg__moveTo(p, cx+rx, cy);
+               nsvg__cubicBezTo(p, cx+rx, cy+ry*NSVG_KAPPA90, cx+rx*NSVG_KAPPA90, cy+ry, cx, cy+ry);
+               nsvg__cubicBezTo(p, cx-rx*NSVG_KAPPA90, cy+ry, cx-rx, cy+ry*NSVG_KAPPA90, cx-rx, cy);
+               nsvg__cubicBezTo(p, cx-rx, cy-ry*NSVG_KAPPA90, cx-rx*NSVG_KAPPA90, cy-ry, cx, cy-ry);
+               nsvg__cubicBezTo(p, cx+rx*NSVG_KAPPA90, cy-ry, cx+rx, cy-ry*NSVG_KAPPA90, cx+rx, cy);
+
+               nsvg__addPath(p, 1);
+
+               nsvg__addShape(p);
+       }
+}
+
+static void nsvg__parseLine(NSVGparser* p, const char** attr)
+{
+       float x1 = 0.0;
+       float y1 = 0.0;
+       float x2 = 0.0;
+       float y2 = 0.0;
+       int i;
+
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "x1") == 0) x1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "y1") == 0) y1 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
+                       if (strcmp(attr[i], "x2") == 0) x2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigX(p), nsvg__actualWidth(p));
+                       if (strcmp(attr[i], "y2") == 0) y2 = nsvg__parseCoordinate(p, attr[i + 1], nsvg__actualOrigY(p), nsvg__actualHeight(p));
+               }
+       }
+
+       nsvg__resetPath(p);
+
+       nsvg__moveTo(p, x1, y1);
+       nsvg__lineTo(p, x2, y2);
+
+       nsvg__addPath(p, 0);
+
+       nsvg__addShape(p);
+}
+
+static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag)
+{
+       int i;
+       const char* s;
+       float args[2];
+       int nargs, npts = 0;
+       char item[64];
+
+       nsvg__resetPath(p);
+
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "points") == 0) {
+                               s = attr[i + 1];
+                               nargs = 0;
+                               while (*s) {
+                                       s = nsvg__getNextPathItem(s, item);
+                                       args[nargs++] = (float)nsvg__atof(item);
+                                       if (nargs >= 2) {
+                                               if (npts == 0)
+                                                       nsvg__moveTo(p, args[0], args[1]);
+                                               else
+                                                       nsvg__lineTo(p, args[0], args[1]);
+                                               nargs = 0;
+                                               npts++;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       nsvg__addPath(p, (char)closeFlag);
+
+       nsvg__addShape(p);
+}
+
+static void nsvg__parseSVG(NSVGparser* p, const char** attr)
+{
+       int i;
+       for (i = 0; attr[i]; i += 2) {
+               if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "width") == 0) {
+                               p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
+                       } else if (strcmp(attr[i], "height") == 0) {
+                               p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
+                       } else if (strcmp(attr[i], "viewBox") == 0) {
+                               const char *s = attr[i + 1];
+                               char buf[64];
+                               s = nsvg__parseNumber(s, buf, 64);
+                               p->viewMinx = nsvg__atof(buf);
+                               while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+                               if (!*s) return;
+                               s = nsvg__parseNumber(s, buf, 64);
+                               p->viewMiny = nsvg__atof(buf);
+                               while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+                               if (!*s) return;
+                               s = nsvg__parseNumber(s, buf, 64);
+                               p->viewWidth = nsvg__atof(buf);
+                               while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+                               if (!*s) return;
+                               s = nsvg__parseNumber(s, buf, 64);
+                               p->viewHeight = nsvg__atof(buf);
+                       } else if (strcmp(attr[i], "preserveAspectRatio") == 0) {
+                               if (strstr(attr[i + 1], "none") != 0) {
+                                       // No uniform scaling
+                                       p->alignType = NSVG_ALIGN_NONE;
+                               } else {
+                                       // Parse X align
+                                       if (strstr(attr[i + 1], "xMin") != 0)
+                                               p->alignX = NSVG_ALIGN_MIN;
+                                       else if (strstr(attr[i + 1], "xMid") != 0)
+                                               p->alignX = NSVG_ALIGN_MID;
+                                       else if (strstr(attr[i + 1], "xMax") != 0)
+                                               p->alignX = NSVG_ALIGN_MAX;
+                                       // Parse X align
+                                       if (strstr(attr[i + 1], "yMin") != 0)
+                                               p->alignY = NSVG_ALIGN_MIN;
+                                       else if (strstr(attr[i + 1], "yMid") != 0)
+                                               p->alignY = NSVG_ALIGN_MID;
+                                       else if (strstr(attr[i + 1], "yMax") != 0)
+                                               p->alignY = NSVG_ALIGN_MAX;
+                                       // Parse meet/slice
+                                       p->alignType = NSVG_ALIGN_MEET;
+                                       if (strstr(attr[i + 1], "slice") != 0)
+                                               p->alignType = NSVG_ALIGN_SLICE;
+                               }
+                       }
+               }
+       }
+}
+
+static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type)
+{
+       int i;
+       NSVGgradientData* grad = (NSVGgradientData*)malloc(sizeof(NSVGgradientData));
+       if (grad == NULL) return;
+       memset(grad, 0, sizeof(NSVGgradientData));
+       grad->units = NSVG_OBJECT_SPACE;
+       grad->type = type;
+       if (grad->type == NSVG_PAINT_LINEAR_GRADIENT) {
+               grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
+               grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
+               grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT);
+               grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT);
+       } else if (grad->type == NSVG_PAINT_RADIAL_GRADIENT) {
+               grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
+               grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
+               grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT);
+       }
+
+       nsvg__xformIdentity(grad->xform);
+
+       for (i = 0; attr[i]; i += 2) {
+               if (strcmp(attr[i], "id") == 0) {
+                       strncpy(grad->id, attr[i+1], 63);
+                       grad->id[63] = '\0';
+               } else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) {
+                       if (strcmp(attr[i], "gradientUnits") == 0) {
+                               if (strcmp(attr[i+1], "objectBoundingBox") == 0)
+                                       grad->units = NSVG_OBJECT_SPACE;
+                               else
+                                       grad->units = NSVG_USER_SPACE;
+                       } else if (strcmp(attr[i], "gradientTransform") == 0) {
+                               nsvg__parseTransform(grad->xform, attr[i + 1]);
+                       } else if (strcmp(attr[i], "cx") == 0) {
+                               grad->radial.cx = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "cy") == 0) {
+                               grad->radial.cy = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "r") == 0) {
+                               grad->radial.r = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "fx") == 0) {
+                               grad->radial.fx = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "fy") == 0) {
+                               grad->radial.fy = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "x1") == 0) {
+                               grad->linear.x1 = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "y1") == 0) {
+                               grad->linear.y1 = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "x2") == 0) {
+                               grad->linear.x2 = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "y2") == 0) {
+                               grad->linear.y2 = nsvg__parseCoordinateRaw(attr[i + 1]);
+                       } else if (strcmp(attr[i], "spreadMethod") == 0) {
+                               if (strcmp(attr[i+1], "pad") == 0)
+                                       grad->spread = NSVG_SPREAD_PAD;
+                               else if (strcmp(attr[i+1], "reflect") == 0)
+                                       grad->spread = NSVG_SPREAD_REFLECT;
+                               else if (strcmp(attr[i+1], "repeat") == 0)
+                                       grad->spread = NSVG_SPREAD_REPEAT;
+                       } else if (strcmp(attr[i], "xlink:href") == 0) {
+                               const char *href = attr[i+1];
+                               strncpy(grad->ref, href+1, 62);
+                               grad->ref[62] = '\0';
+                       }
+               }
+       }
+
+       grad->next = p->gradients;
+       p->gradients = grad;
+}
+
+static void nsvg__parseGradientStop(NSVGparser* p, const char** attr)
+{
+       NSVGattrib* curAttr = nsvg__getAttr(p);
+       NSVGgradientData* grad;
+       NSVGgradientStop* stop;
+       int i, idx;
+
+       curAttr->stopOffset = 0;
+       curAttr->stopColor = 0;
+       curAttr->stopOpacity = 1.0f;
+
+       for (i = 0; attr[i]; i += 2) {
+               nsvg__parseAttr(p, attr[i], attr[i + 1]);
+       }
+
+       // Add stop to the last gradient.
+       grad = p->gradients;
+       if (grad == NULL) return;
+
+       grad->nstops++;
+       grad->stops = (NSVGgradientStop*)realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops);
+       if (grad->stops == NULL) return;
+
+       // Insert
+       idx = grad->nstops-1;
+       for (i = 0; i < grad->nstops-1; i++) {
+               if (curAttr->stopOffset < grad->stops[i].offset) {
+                       idx = i;
+                       break;
+               }
+       }
+       if (idx != grad->nstops-1) {
+               for (i = grad->nstops-1; i > idx; i--)
+                       grad->stops[i] = grad->stops[i-1];
+       }
+
+       stop = &grad->stops[idx];
+       stop->color = curAttr->stopColor;
+       stop->color |= (unsigned int)(curAttr->stopOpacity*255) << 24;
+       stop->offset = curAttr->stopOffset;
+}
+
+static void nsvg__startElement(void* ud, const char* el, const char** attr)
+{
+       NSVGparser* p = (NSVGparser*)ud;
+
+       if (p->defsFlag) {
+               // Skip everything but gradients in defs
+               if (strcmp(el, "linearGradient") == 0) {
+                       nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
+               } else if (strcmp(el, "radialGradient") == 0) {
+                       nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
+               } else if (strcmp(el, "stop") == 0) {
+                       nsvg__parseGradientStop(p, attr);
+               }
+               return;
+       }
+
+       if (strcmp(el, "g") == 0) {
+               nsvg__pushAttr(p);
+               nsvg__parseAttribs(p, attr);
+       } else if (strcmp(el, "path") == 0) {
+               if (p->pathFlag)        // Do not allow nested paths.
+                       return;
+               nsvg__pushAttr(p);
+               nsvg__parsePath(p, attr);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "rect") == 0) {
+               nsvg__pushAttr(p);
+               nsvg__parseRect(p, attr);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "circle") == 0) {
+               nsvg__pushAttr(p);
+               nsvg__parseCircle(p, attr);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "ellipse") == 0) {
+               nsvg__pushAttr(p);
+               nsvg__parseEllipse(p, attr);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "line") == 0)  {
+               nsvg__pushAttr(p);
+               nsvg__parseLine(p, attr);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "polyline") == 0)  {
+               nsvg__pushAttr(p);
+               nsvg__parsePoly(p, attr, 0);
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "polygon") == 0)  {
+               nsvg__pushAttr(p);
+               nsvg__parsePoly(p, attr, 1);
+               nsvg__popAttr(p);
+       } else  if (strcmp(el, "linearGradient") == 0) {
+               nsvg__parseGradient(p, attr, NSVG_PAINT_LINEAR_GRADIENT);
+       } else if (strcmp(el, "radialGradient") == 0) {
+               nsvg__parseGradient(p, attr, NSVG_PAINT_RADIAL_GRADIENT);
+       } else if (strcmp(el, "stop") == 0) {
+               nsvg__parseGradientStop(p, attr);
+       } else if (strcmp(el, "defs") == 0) {
+               p->defsFlag = 1;
+       } else if (strcmp(el, "svg") == 0) {
+               nsvg__parseSVG(p, attr);
+       }
+}
+
+static void nsvg__endElement(void* ud, const char* el)
+{
+       NSVGparser* p = (NSVGparser*)ud;
+
+       if (strcmp(el, "g") == 0) {
+               nsvg__popAttr(p);
+       } else if (strcmp(el, "path") == 0) {
+               p->pathFlag = 0;
+       } else if (strcmp(el, "defs") == 0) {
+               p->defsFlag = 0;
+       }
+}
+
+static void nsvg__content(void* ud, const char* s)
+{
+       NSVG_NOTUSED(ud);
+       NSVG_NOTUSED(s);
+       // empty
+}
+
+static void nsvg__imageBounds(NSVGparser* p, float* bounds)
+{
+       NSVGshape* shape;
+       shape = p->image->shapes;
+       if (shape == NULL) {
+               bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0;
+               return;
+       }
+       bounds[0] = shape->bounds[0];
+       bounds[1] = shape->bounds[1];
+       bounds[2] = shape->bounds[2];
+       bounds[3] = shape->bounds[3];
+       for (shape = shape->next; shape != NULL; shape = shape->next) {
+               bounds[0] = nsvg__minf(bounds[0], shape->bounds[0]);
+               bounds[1] = nsvg__minf(bounds[1], shape->bounds[1]);
+               bounds[2] = nsvg__maxf(bounds[2], shape->bounds[2]);
+               bounds[3] = nsvg__maxf(bounds[3], shape->bounds[3]);
+       }
+}
+
+static float nsvg__viewAlign(float content, float container, int type)
+{
+       if (type == NSVG_ALIGN_MIN)
+               return 0;
+       else if (type == NSVG_ALIGN_MAX)
+               return container - content;
+       // mid
+       return (container - content) * 0.5f;
+}
+
+static void nsvg__scaleGradient(NSVGgradient* grad, float tx, float ty, float sx, float sy)
+{
+       float t[6];
+       nsvg__xformSetTranslation(t, tx, ty);
+       nsvg__xformMultiply (grad->xform, t);
+
+       nsvg__xformSetScale(t, sx, sy);
+       nsvg__xformMultiply (grad->xform, t);
+}
+
+static void nsvg__scaleToViewbox(NSVGparser* p, const char* units)
+{
+       NSVGshape* shape;
+       NSVGpath* path;
+       float tx, ty, sx, sy, us, bounds[4], t[6], avgs;
+       int i;
+       float* pt;
+
+       // Guess image size if not set completely.
+       nsvg__imageBounds(p, bounds);
+
+       if (p->viewWidth == 0) {
+               if (p->image->width > 0) {
+                       p->viewWidth = p->image->width;
+               } else {
+                       p->viewMinx = bounds[0];
+                       p->viewWidth = bounds[2] - bounds[0];
+               }
+       }
+       if (p->viewHeight == 0) {
+               if (p->image->height > 0) {
+                       p->viewHeight = p->image->height;
+               } else {
+                       p->viewMiny = bounds[1];
+                       p->viewHeight = bounds[3] - bounds[1];
+               }
+       }
+
+       /**
+        * We have sample images with the width and height set to 1, whereas the viewbox aspect ratio 
+        * is not square. Use the viewbox in this case.
+        */
+       if (p->image->width <= 1)
+               p->image->width = p->viewWidth;
+       if (p->image->height <= 1)
+               p->image->height = p->viewHeight;
+
+       tx = -p->viewMinx;
+       ty = -p->viewMiny;
+       sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
+       sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
+       // Unit scaling
+       us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f);
+
+       // Fix aspect ratio
+       if (p->alignType == NSVG_ALIGN_MEET) {
+               // fit whole image into viewbox
+               sx = sy = nsvg__minf(sx, sy);
+               tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
+               ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
+       } else if (p->alignType == NSVG_ALIGN_SLICE) {
+               // fill whole viewbox with image
+               sx = sy = nsvg__maxf(sx, sy);
+               tx += nsvg__viewAlign(p->viewWidth*sx, p->image->width, p->alignX) / sx;
+               ty += nsvg__viewAlign(p->viewHeight*sy, p->image->height, p->alignY) / sy;
+       }
+
+       // Transform
+       sx *= us;
+       sy *= us;
+       avgs = (sx+sy) / 2.0f;
+       for (shape = p->image->shapes; shape != NULL; shape = shape->next) {
+               shape->bounds[0] = (shape->bounds[0] + tx) * sx;
+               shape->bounds[1] = (shape->bounds[1] + ty) * sy;
+               shape->bounds[2] = (shape->bounds[2] + tx) * sx;
+               shape->bounds[3] = (shape->bounds[3] + ty) * sy;
+               for (path = shape->paths; path != NULL; path = path->next) {
+                       path->bounds[0] = (path->bounds[0] + tx) * sx;
+                       path->bounds[1] = (path->bounds[1] + ty) * sy;
+                       path->bounds[2] = (path->bounds[2] + tx) * sx;
+                       path->bounds[3] = (path->bounds[3] + ty) * sy;
+                       for (i =0; i < path->npts; i++) {
+                               pt = &path->pts[i*2];
+                               pt[0] = (pt[0] + tx) * sx;
+                               pt[1] = (pt[1] + ty) * sy;
+                       }
+               }
+
+               if (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT || shape->fill.type == NSVG_PAINT_RADIAL_GRADIENT) {
+                       nsvg__scaleGradient(shape->fill.gradient, tx,ty, sx,sy);
+                       memcpy(t, shape->fill.gradient->xform, sizeof(float)*6);
+                       nsvg__xformInverse(shape->fill.gradient->xform, t);
+               }
+               if (shape->stroke.type == NSVG_PAINT_LINEAR_GRADIENT || shape->stroke.type == NSVG_PAINT_RADIAL_GRADIENT) {
+                       nsvg__scaleGradient(shape->stroke.gradient, tx,ty, sx,sy);
+                       memcpy(t, shape->stroke.gradient->xform, sizeof(float)*6);
+                       nsvg__xformInverse(shape->stroke.gradient->xform, t);
+               }
+
+               shape->strokeWidth *= avgs;
+               shape->strokeDashOffset *= avgs;
+               for (i = 0; i < shape->strokeDashCount; i++)
+                       shape->strokeDashArray[i] *= avgs;
+       }
+}
+
+NSVGimage* nsvgParse(char* input, const char* units, float dpi)
+{
+       NSVGparser* p;
+       NSVGimage* ret = 0;
+
+       p = nsvg__createParser();
+       if (p == NULL) {
+               return NULL;
+       }
+       p->dpi = dpi;
+
+       nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);
+
+       // Scale to viewBox
+       nsvg__scaleToViewbox(p, units);
+
+       ret = p->image;
+       p->image = NULL;
+
+       nsvg__deleteParser(p);
+
+       return ret;
+}
+
+NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi)
+{
+       FILE* fp = NULL;
+       size_t size = 0;
+       long value = 0;
+       char* data = NULL;
+       NSVGimage* image = NULL;
+
+       fp = fopen(filename, "rb");
+       if (!fp) goto error;
+       fseek(fp, 0, SEEK_END);
+       value = ftell(fp);
+       /**
+        * In the original file, unsigned long type 'size' gets a return value. But, the return value of 'ftell()' is
+        * signed long type. To prevent interpreting an unexpected large value, we put the comparitive condition here.
+        */
+       if( value < 0 ) goto error;
+       size = value;
+       fseek(fp, 0, SEEK_SET);
+       data = (char*)malloc(size+1);
+       if (data == NULL) goto error;
+       if (fread(data, 1, size, fp) != size) goto error;
+       data[size] = '\0';      // Must be null terminated.
+       fclose(fp);
+       image = nsvgParse(data, units, dpi);
+       free(data);
+
+       return image;
+
+error:
+       if (fp) fclose(fp);
+       if (data) free(data);
+       if (image) nsvgDelete(image);
+       return NULL;
+}
+
+NSVGpath* nsvgDuplicatePath(NSVGpath* p)
+{
+    NSVGpath* res = NULL;
+
+    if (p == NULL)
+        return NULL;
+
+    res = (NSVGpath*)malloc(sizeof(NSVGpath));
+    if (res == NULL) goto error;
+    memset(res, 0, sizeof(NSVGpath));
+
+    res->pts = (float*)malloc(p->npts*2*sizeof(float));
+    if (res->pts == NULL) goto error;
+    memcpy(res->pts, p->pts, p->npts * sizeof(float) * 2);
+    res->npts = p->npts;
+
+    memcpy(res->bounds, p->bounds, sizeof(p->bounds));
+
+    res->closed = p->closed;
+
+    return res;
+
+error:
+    if (res != NULL) {
+        free(res->pts);
+        free(res);
+    }
+    return NULL;
+}
+
+void nsvgDelete(NSVGimage* image)
+{
+       NSVGshape *snext, *shape;
+       if (image == NULL) return;
+       shape = image->shapes;
+       while (shape != NULL) {
+               snext = shape->next;
+               nsvg__deletePaths(shape->paths);
+               nsvg__deletePaint(&shape->fill);
+               nsvg__deletePaint(&shape->stroke);
+               free(shape);
+               shape = snext;
+       }
+       free(image);
+}
diff --git a/third-party/nanosvg/nanosvg.h b/third-party/nanosvg/nanosvg.h
new file mode 100644 (file)
index 0000000..27f8ac0
--- /dev/null
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ * The SVG parser is based on Anti-Grain Geometry 2.4 SVG example
+ * Copyright (C) 2002-2004 Maxim Shemanarev (McSeem) (http://www.antigrain.com/)
+ *
+ * Arc calculation code based on canvg (https://code.google.com/p/canvg/)
+ *
+ * Bounding box calculation based on http://blog.hackers-cafe.net/2009/06/how-to-calculate-bezier-curves-bounding.html
+ *
+ */
+
+#ifndef NANOSVG_H
+#define NANOSVG_H
+
+// NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
+//
+// The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
+//
+// NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
+//
+// The shapes in the SVG images are transformed by the viewBox and converted to specified units.
+// That is, you should get the same looking data as your designed in your favorite app.
+//
+// NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose
+// to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
+//
+// The units passed to NanoSVG should be one of: 'px', 'pt', 'pc' 'mm', 'cm', or 'in'.
+// DPI (dots-per-inch) controls how the unit conversion is done.
+//
+// If you don't know or care about the units stuff, "px" and 96 should get you going.
+
+
+/* Example Usage:
+  // Load SVG
+  NSVGimage* image;
+  image = nsvgParseFromFile("test.svg", "px", 96);
+  printf("size: %f x %f\n", image->width, image->height);
+  // Use...
+  for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) {
+    for (NSVGpath *path = shape->paths; path != NULL; path = path->next) {
+      for (int i = 0; i < path->npts-1; i += 3) {
+        float* p = &path->pts[i*2];
+        drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]);
+      }
+    }
+  }
+  // Delete
+  nsvgDelete(image);
+*/
+
+enum NSVGpaintType {
+  NSVG_PAINT_NONE = 0,
+  NSVG_PAINT_COLOR = 1,
+  NSVG_PAINT_LINEAR_GRADIENT = 2,
+  NSVG_PAINT_RADIAL_GRADIENT = 3
+};
+
+enum NSVGspreadType {
+  NSVG_SPREAD_PAD = 0,
+  NSVG_SPREAD_REFLECT = 1,
+  NSVG_SPREAD_REPEAT = 2
+};
+
+enum NSVGlineJoin {
+  NSVG_JOIN_MITER = 0,
+  NSVG_JOIN_ROUND = 1,
+  NSVG_JOIN_BEVEL = 2
+};
+
+enum NSVGlineCap {
+  NSVG_CAP_BUTT = 0,
+  NSVG_CAP_ROUND = 1,
+  NSVG_CAP_SQUARE = 2
+};
+
+enum NSVGfillRule {
+  NSVG_FILLRULE_NONZERO = 0,
+  NSVG_FILLRULE_EVENODD = 1
+};
+
+enum NSVGflags {
+  NSVG_FLAGS_VISIBLE = 0x01
+};
+
+typedef struct NSVGgradientStop {
+  unsigned int color;
+  float offset;
+} NSVGgradientStop;
+
+typedef struct NSVGgradient {
+  float xform[6];
+  char spread;
+  float fx, fy;
+  int nstops;
+  NSVGgradientStop stops[1];
+} NSVGgradient;
+
+typedef struct NSVGpaint {
+  /**
+   * In the original file, using char type (without signed or unsigned) can be interpreted
+   * as 'unsigned char' in some build environments, like ARM architecture.
+   * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
+   */
+  signed char type;
+  union {
+    unsigned int  color;
+    NSVGgradient* gradient;
+  };
+} NSVGpaint;
+
+typedef struct NSVGpath
+{
+  float*           pts;         // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ...
+  int              npts;        // Total number of bezier points.
+  char             closed;      // Flag indicating if shapes should be treated as closed.
+  float            bounds[4];   // Tight bounding box of the shape [minx,miny,maxx,maxy].
+  struct NSVGpath* next;        // Pointer to next path, or NULL if last element.
+} NSVGpath;
+
+typedef struct NSVGshape
+{
+  char              id[64];     // Optional 'id' attr of the shape or its group
+  NSVGpaint         fill;       // Fill paint
+  NSVGpaint         stroke;     // Stroke paint
+  float             opacity;    // Opacity of the shape.
+  float             strokeWidth; // Stroke width (scaled).
+  float             strokeDashOffset; // Stroke dash offset (scaled).
+  float             strokeDashArray[8]; // Stroke dash array (scaled).
+  char              strokeDashCount; // Number of dash values in dash array.
+  char              strokeLineJoin; // Stroke join type.
+  char              strokeLineCap; // Stroke cap type.
+  float             miterLimit; // Miter limit
+  char              fillRule;   // Fill rule, see NSVGfillRule.
+  unsigned char     flags;      // Logical or of NSVG_FLAGS_* flags
+  float             bounds[4];  // Tight bounding box of the shape [minx,miny,maxx,maxy].
+  NSVGpath*         paths;      // Linked list of paths in the image.
+  struct NSVGshape* next;       // Pointer to next shape, or NULL if last element.
+} NSVGshape;
+
+typedef struct NSVGimage
+{
+  float      width;             // Width of the image.
+  float      height;            // Height of the image.
+  NSVGshape* shapes;            // Linked list of shapes in the image.
+} NSVGimage;
+
+// Parses SVG file from a file, returns SVG image as paths.
+NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
+
+// Parses SVG file from a null terminated string, returns SVG image as paths.
+// Important note: changes the string.
+NSVGimage* nsvgParse(char* input, const char* units, float dpi);
+
+// Duplicates a path.
+NSVGpath* nsvgDuplicatePath(NSVGpath* p);
+
+// Deletes an image.
+void nsvgDelete(NSVGimage* image);
+
+#endif // NANOSVG_H
+
+/**
+ * In the original software, The nanosvg implementation was followed here.
+ * We have moved the implementation to nanosvg.cc.
+ */
diff --git a/third-party/nanosvg/nanosvgrast.cc b/third-party/nanosvg/nanosvgrast.cc
new file mode 100644 (file)
index 0000000..259f695
--- /dev/null
@@ -0,0 +1,1324 @@
+/*
+ * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ * The polygon rasterization is heavily based on stb_truetype rasterizer
+ * by Sean Barrett - http://nothings.org/
+ *
+ */
+
+#include "nanosvgrast.h"
+
+/**
+ * In the original software, The nanosvgrast implementation was included in the header file.
+ * We have separated the implementation to source file here.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <math.h>
+
+#define NSVG__SUBSAMPLES    5
+#define NSVG__FIXSHIFT      10
+#define NSVG__FIX           (1 << NSVG__FIXSHIFT)
+#define NSVG__FIXMASK       (NSVG__FIX-1)
+#define NSVG__MEMPAGE_SIZE  1024
+
+typedef struct NSVGedge {
+       float x0,y0, x1,y1;
+       int dir;
+       struct NSVGedge* next;
+} NSVGedge;
+
+typedef struct NSVGpoint {
+       float x, y;
+       float dx, dy;
+       float len;
+       float dmx, dmy;
+       unsigned char flags;
+} NSVGpoint;
+
+typedef struct NSVGactiveEdge {
+       int x,dx;
+       float ey;
+       int dir;
+       struct NSVGactiveEdge *next;
+} NSVGactiveEdge;
+
+typedef struct NSVGmemPage {
+       unsigned char mem[NSVG__MEMPAGE_SIZE];
+       int size;
+       struct NSVGmemPage* next;
+} NSVGmemPage;
+
+typedef struct NSVGcachedPaint {
+  /**
+   * In the original file, using char type (without signed or unsigned) can be interpreted
+   * as 'unsigned char' in some build environments, like ARM architecture.
+   * To prevent the unexpected behavior, we replace 'char type' with 'signed char type' here.
+   */
+  signed char type;
+       char spread;
+       float xform[6];
+       unsigned int colors[256];
+} NSVGcachedPaint;
+
+struct NSVGrasterizer
+{
+       float px, py;
+
+       float tessTol;
+       float distTol;
+
+       NSVGedge* edges;
+       int nedges;
+       int cedges;
+
+       NSVGpoint* points;
+       int npoints;
+       int cpoints;
+
+       NSVGpoint* points2;
+       int npoints2;
+       int cpoints2;
+
+       NSVGactiveEdge* freelist;
+       NSVGmemPage* pages;
+       NSVGmemPage* curpage;
+
+       unsigned char* scanline;
+       int cscanline;
+
+       unsigned char* bitmap;
+       int width, height, stride;
+};
+
+NSVGrasterizer* nsvgCreateRasterizer()
+{
+       NSVGrasterizer* r = (NSVGrasterizer*)malloc(sizeof(NSVGrasterizer));
+       if (r == NULL) goto error;
+       memset(r, 0, sizeof(NSVGrasterizer));
+
+       r->tessTol = 0.25f;
+       r->distTol = 0.01f;
+
+       return r;
+
+error:
+       nsvgDeleteRasterizer(r);
+       return NULL;
+}
+
+void nsvgDeleteRasterizer(NSVGrasterizer* r)
+{
+       NSVGmemPage* p;
+
+       if (r == NULL) return;
+
+       p = r->pages;
+       while (p != NULL) {
+               NSVGmemPage* next = p->next;
+               free(p);
+               p = next;
+       }
+
+       if (r->edges) free(r->edges);
+       if (r->points) free(r->points);
+       if (r->points2) free(r->points2);
+       if (r->scanline) free(r->scanline);
+
+       free(r);
+}
+
+static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur)
+{
+       NSVGmemPage *newp;
+
+       // If using existing chain, return the next page in chain
+       if (cur != NULL && cur->next != NULL) {
+               return cur->next;
+       }
+
+       // Alloc new page
+       newp = (NSVGmemPage*)malloc(sizeof(NSVGmemPage));
+       if (newp == NULL) return NULL;
+       memset(newp, 0, sizeof(NSVGmemPage));
+
+       // Add to linked list
+       if (cur != NULL)
+               cur->next = newp;
+       else
+               r->pages = newp;
+
+       return newp;
+}
+
+static void nsvg__resetPool(NSVGrasterizer* r)
+{
+       NSVGmemPage* p = r->pages;
+       while (p != NULL) {
+               p->size = 0;
+               p = p->next;
+       }
+       r->curpage = r->pages;
+}
+
+static unsigned char* nsvg__alloc(NSVGrasterizer* r, int size)
+{
+       unsigned char* buf;
+       if (size > NSVG__MEMPAGE_SIZE) return NULL;
+       if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) {
+               r->curpage = nsvg__nextPage(r, r->curpage);
+       }
+       buf = &r->curpage->mem[r->curpage->size];
+       r->curpage->size += size;
+       return buf;
+}
+
+static int nsvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
+{
+       float dx = x2 - x1;
+       float dy = y2 - y1;
+       return dx*dx + dy*dy < tol*tol;
+}
+
+static void nsvg__addPathPoint(NSVGrasterizer* r, float x, float y, int flags)
+{
+       NSVGpoint* pt;
+
+       if (r->npoints > 0) {
+               pt = &r->points[r->npoints-1];
+               if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) {
+                       pt->flags = (unsigned char)(pt->flags | flags);
+                       return;
+               }
+       }
+
+       if (r->npoints+1 > r->cpoints) {
+               r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
+               r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints);
+               if (r->points == NULL) return;
+       }
+
+       pt = &r->points[r->npoints];
+       pt->x = x;
+       pt->y = y;
+       pt->flags = (unsigned char)flags;
+       r->npoints++;
+}
+
+static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint pt)
+{
+       if (r->npoints+1 > r->cpoints) {
+               r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64;
+               r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints);
+               if (r->points == NULL) return;
+       }
+       r->points[r->npoints] = pt;
+       r->npoints++;
+}
+
+static void nsvg__duplicatePoints(NSVGrasterizer* r)
+{
+       if (r->npoints > r->cpoints2) {
+               r->cpoints2 = r->npoints;
+               r->points2 = (NSVGpoint*)realloc(r->points2, sizeof(NSVGpoint) * r->cpoints2);
+               if (r->points2 == NULL) return;
+       }
+
+       memcpy(r->points2, r->points, sizeof(NSVGpoint) * r->npoints);
+       r->npoints2 = r->npoints;
+}
+
+static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float y1)
+{
+       NSVGedge* e;
+
+       // Skip horizontal edges
+       if (y0 == y1)
+               return;
+
+       if (r->nedges+1 > r->cedges) {
+               r->cedges = r->cedges > 0 ? r->cedges * 2 : 64;
+               r->edges = (NSVGedge*)realloc(r->edges, sizeof(NSVGedge) * r->cedges);
+               if (r->edges == NULL) return;
+       }
+
+       e = &r->edges[r->nedges];
+       r->nedges++;
+
+       if (y0 < y1) {
+               e->x0 = x0;
+               e->y0 = y0;
+               e->x1 = x1;
+               e->y1 = y1;
+               e->dir = 1;
+       } else {
+               e->x0 = x1;
+               e->y0 = y1;
+               e->x1 = x0;
+               e->y1 = y0;
+               e->dir = -1;
+       }
+}
+
+static float nsvg__normalize(float *x, float* y)
+{
+       float d = sqrtf((*x)*(*x) + (*y)*(*y));
+       if (d > 1e-6f) {
+               float id = 1.0f / d;
+               *x *= id;
+               *y *= id;
+       }
+       return d;
+}
+
+static float nsvg__absf(float x) { return x < 0 ? -x : x; }
+
+static void nsvg__flattenCubicBez(NSVGrasterizer* r,
+                                                                 float x1, float y1, float x2, float y2,
+                                                                 float x3, float y3, float x4, float y4,
+                                                                 int level, int type)
+{
+       float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
+       float dx,dy,d2,d3;
+
+       if (level > 10) return;
+
+       x12 = (x1+x2)*0.5f;
+       y12 = (y1+y2)*0.5f;
+       x23 = (x2+x3)*0.5f;
+       y23 = (y2+y3)*0.5f;
+       x34 = (x3+x4)*0.5f;
+       y34 = (y3+y4)*0.5f;
+       x123 = (x12+x23)*0.5f;
+       y123 = (y12+y23)*0.5f;
+
+       dx = x4 - x1;
+       dy = y4 - y1;
+       d2 = nsvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
+       d3 = nsvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
+
+       if ((d2 + d3)*(d2 + d3) < r->tessTol * (dx*dx + dy*dy)) {
+               nsvg__addPathPoint(r, x4, y4, type);
+               return;
+       }
+
+       x234 = (x23+x34)*0.5f;
+       y234 = (y23+y34)*0.5f;
+       x1234 = (x123+x234)*0.5f;
+       y1234 = (y123+y234)*0.5f;
+
+       nsvg__flattenCubicBez(r, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
+       nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
+}
+
+static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float scale)
+{
+       int i, j;
+       NSVGpath* path;
+
+       for (path = shape->paths; path != NULL; path = path->next) {
+               r->npoints = 0;
+               // Flatten path
+               nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
+               for (i = 0; i < path->npts-1; i += 3) {
+                       float* p = &path->pts[i*2];
+                       nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0);
+               }
+               // Close path
+               nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0);
+               // Build edges
+               for (i = 0, j = r->npoints-1; i < r->npoints; j = i++)
+                       nsvg__addEdge(r, r->points[j].x, r->points[j].y, r->points[i].x, r->points[i].y);
+       }
+}
+
+enum NSVGpointFlags
+{
+       NSVG_PT_CORNER = 0x01,
+       NSVG_PT_BEVEL = 0x02,
+       NSVG_PT_LEFT = 0x04
+};
+
+static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
+{
+       float w = lineWidth * 0.5f;
+       float dx = p1->x - p0->x;
+       float dy = p1->y - p0->y;
+       float len = nsvg__normalize(&dx, &dy);
+       float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f;
+       float dlx = dy, dly = -dx;
+       float lx = px - dlx*w, ly = py - dly*w;
+       float rx = px + dlx*w, ry = py + dly*w;
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
+{
+       float w = lineWidth * 0.5f;
+       float px = p->x, py = p->y;
+       float dlx = dy, dly = -dx;
+       float lx = px - dlx*w, ly = py - dly*w;
+       float rx = px + dlx*w, ry = py + dly*w;
+
+       nsvg__addEdge(r, lx, ly, rx, ry);
+
+       if (connect) {
+               nsvg__addEdge(r, left->x, left->y, lx, ly);
+               nsvg__addEdge(r, rx, ry, right->x, right->y);
+       }
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect)
+{
+       float w = lineWidth * 0.5f;
+       float px = p->x - dx*w, py = p->y - dy*w;
+       float dlx = dy, dly = -dx;
+       float lx = px - dlx*w, ly = py - dly*w;
+       float rx = px + dlx*w, ry = py + dly*w;
+
+       nsvg__addEdge(r, lx, ly, rx, ry);
+
+       if (connect) {
+               nsvg__addEdge(r, left->x, left->y, lx, ly);
+               nsvg__addEdge(r, rx, ry, right->x, right->y);
+       }
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+#ifndef NSVG_PI
+#define NSVG_PI (3.14159265358979323846264338327f)
+#endif
+
+static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int ncap, int connect)
+{
+       int i;
+       float w = lineWidth * 0.5f;
+       float px = p->x, py = p->y;
+       float dlx = dy, dly = -dx;
+       float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0;
+
+       for (i = 0; i < ncap; i++) {
+               float a = (float)i/(float)(ncap-1)*NSVG_PI;
+               float ax = cosf(a) * w, ay = sinf(a) * w;
+               float x = px - dlx*ax - dx*ay;
+               float y = py - dly*ax - dy*ay;
+
+               if (i > 0)
+                       nsvg__addEdge(r, prevx, prevy, x, y);
+
+               prevx = x;
+               prevy = y;
+
+               if (i == 0) {
+                       lx = x; ly = y;
+               } else if (i == ncap-1) {
+                       rx = x; ry = y;
+               }
+       }
+
+       if (connect) {
+               nsvg__addEdge(r, left->x, left->y, lx, ly);
+               nsvg__addEdge(r, rx, ry, right->x, right->y);
+       }
+
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
+{
+       float w = lineWidth * 0.5f;
+       float dlx0 = p0->dy, dly0 = -p0->dx;
+       float dlx1 = p1->dy, dly1 = -p1->dx;
+       float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w);
+       float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w);
+       float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w);
+       float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w);
+
+       nsvg__addEdge(r, lx0, ly0, left->x, left->y);
+       nsvg__addEdge(r, lx1, ly1, lx0, ly0);
+
+       nsvg__addEdge(r, right->x, right->y, rx0, ry0);
+       nsvg__addEdge(r, rx0, ry0, rx1, ry1);
+
+       left->x = lx1; left->y = ly1;
+       right->x = rx1; right->y = ry1;
+}
+
+static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth)
+{
+       float w = lineWidth * 0.5f;
+       float dlx0 = p0->dy, dly0 = -p0->dx;
+       float dlx1 = p1->dy, dly1 = -p1->dx;
+       float lx0, rx0, lx1, rx1;
+       float ly0, ry0, ly1, ry1;
+
+       if (p1->flags & NSVG_PT_LEFT) {
+               lx0 = lx1 = p1->x - p1->dmx * w;
+               ly0 = ly1 = p1->y - p1->dmy * w;
+               nsvg__addEdge(r, lx1, ly1, left->x, left->y);
+
+               rx0 = p1->x + (dlx0 * w);
+               ry0 = p1->y + (dly0 * w);
+               rx1 = p1->x + (dlx1 * w);
+               ry1 = p1->y + (dly1 * w);
+               nsvg__addEdge(r, right->x, right->y, rx0, ry0);
+               nsvg__addEdge(r, rx0, ry0, rx1, ry1);
+       } else {
+               lx0 = p1->x - (dlx0 * w);
+               ly0 = p1->y - (dly0 * w);
+               lx1 = p1->x - (dlx1 * w);
+               ly1 = p1->y - (dly1 * w);
+               nsvg__addEdge(r, lx0, ly0, left->x, left->y);
+               nsvg__addEdge(r, lx1, ly1, lx0, ly0);
+
+               rx0 = rx1 = p1->x + p1->dmx * w;
+               ry0 = ry1 = p1->y + p1->dmy * w;
+               nsvg__addEdge(r, right->x, right->y, rx1, ry1);
+       }
+
+       left->x = lx1; left->y = ly1;
+       right->x = rx1; right->y = ry1;
+}
+
+static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth, int ncap)
+{
+       int i, n;
+       float w = lineWidth * 0.5f;
+       float dlx0 = p0->dy, dly0 = -p0->dx;
+       float dlx1 = p1->dy, dly1 = -p1->dx;
+       float a0 = atan2f(dly0, dlx0);
+       float a1 = atan2f(dly1, dlx1);
+       float da = a1 - a0;
+       float lx, ly, rx, ry;
+
+       if (da < NSVG_PI) da += NSVG_PI*2;
+       if (da > NSVG_PI) da -= NSVG_PI*2;
+
+       n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap);
+       if (n < 2) n = 2;
+       if (n > ncap) n = ncap;
+
+       lx = left->x;
+       ly = left->y;
+       rx = right->x;
+       ry = right->y;
+
+       for (i = 0; i < n; i++) {
+               float u = (float)i/(float)(n-1);
+               float a = a0 + u*da;
+               float ax = cosf(a) * w, ay = sinf(a) * w;
+               float lx1 = p1->x - ax, ly1 = p1->y - ay;
+               float rx1 = p1->x + ax, ry1 = p1->y + ay;
+
+               nsvg__addEdge(r, lx1, ly1, lx, ly);
+               nsvg__addEdge(r, rx, ry, rx1, ry1);
+
+               lx = lx1; ly = ly1;
+               rx = rx1; ry = ry1;
+       }
+
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, float lineWidth)
+{
+       float w = lineWidth * 0.5f;
+       float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w);
+       float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w);
+
+       nsvg__addEdge(r, lx, ly, left->x, left->y);
+       nsvg__addEdge(r, right->x, right->y, rx, ry);
+
+       left->x = lx; left->y = ly;
+       right->x = rx; right->y = ry;
+}
+
+static int nsvg__curveDivs(float r, float arc, float tol)
+{
+       float da = acosf(r / (r + tol)) * 2.0f;
+       int divs = (int)ceilf(arc / da);
+       if (divs < 2) divs = 2;
+       return divs;
+}
+
+static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, float lineWidth)
+{
+       int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol);        // Calculate divisions per half circle.
+       NSVGpoint left = {0,0,0,0,0,0,0,0}, right = {0,0,0,0,0,0,0,0}, firstLeft = {0,0,0,0,0,0,0,0}, firstRight = {0,0,0,0,0,0,0,0};
+       NSVGpoint* p0, *p1;
+       int j, s, e;
+
+       // Build stroke edges
+       if (closed) {
+               // Looping
+               p0 = &points[npoints-1];
+               p1 = &points[0];
+               s = 0;
+               e = npoints;
+       } else {
+               // Add cap
+               p0 = &points[0];
+               p1 = &points[1];
+               s = 1;
+               e = npoints-1;
+       }
+
+       if (closed) {
+               nsvg__initClosed(&left, &right, p0, p1, lineWidth);
+               firstLeft = left;
+               firstRight = right;
+       } else {
+               // Add cap
+               float dx = p1->x - p0->x;
+               float dy = p1->y - p0->y;
+               nsvg__normalize(&dx, &dy);
+               if (lineCap == NSVG_CAP_BUTT)
+                       nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
+               else if (lineCap == NSVG_CAP_SQUARE)
+                       nsvg__squareCap(r, &left, &right, p0, dx, dy, lineWidth, 0);
+               else if (lineCap == NSVG_CAP_ROUND)
+                       nsvg__roundCap(r, &left, &right, p0, dx, dy, lineWidth, ncap, 0);
+       }
+
+       for (j = s; j < e; ++j) {
+               if (p1->flags & NSVG_PT_CORNER) {
+                       if (lineJoin == NSVG_JOIN_ROUND)
+                               nsvg__roundJoin(r, &left, &right, p0, p1, lineWidth, ncap);
+                       else if (lineJoin == NSVG_JOIN_BEVEL || (p1->flags & NSVG_PT_BEVEL))
+                               nsvg__bevelJoin(r, &left, &right, p0, p1, lineWidth);
+                       else
+                               nsvg__miterJoin(r, &left, &right, p0, p1, lineWidth);
+               } else {
+                       nsvg__straightJoin(r, &left, &right, p1, lineWidth);
+               }
+               p0 = p1++;
+       }
+
+       if (closed) {
+               // Loop it
+               nsvg__addEdge(r, firstLeft.x, firstLeft.y, left.x, left.y);
+               nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y);
+       } else {
+               // Add cap
+               float dx = p1->x - p0->x;
+               float dy = p1->y - p0->y;
+               nsvg__normalize(&dx, &dy);
+               if (lineCap == NSVG_CAP_BUTT)
+                       nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
+               else if (lineCap == NSVG_CAP_SQUARE)
+                       nsvg__squareCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1);
+               else if (lineCap == NSVG_CAP_ROUND)
+                       nsvg__roundCap(r, &right, &left, p1, -dx, -dy, lineWidth, ncap, 1);
+       }
+}
+
+static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoin)
+{
+       int i, j;
+       NSVGpoint* p0, *p1;
+
+       p0 = &r->points[r->npoints-1];
+       p1 = &r->points[0];
+       for (i = 0; i < r->npoints; i++) {
+               // Calculate segment direction and length
+               p0->dx = p1->x - p0->x;
+               p0->dy = p1->y - p0->y;
+               p0->len = nsvg__normalize(&p0->dx, &p0->dy);
+               // Advance
+               p0 = p1++;
+       }
+
+       // calculate joins
+       p0 = &r->points[r->npoints-1];
+       p1 = &r->points[0];
+       for (j = 0; j < r->npoints; j++) {
+               float dlx0, dly0, dlx1, dly1, dmr2, cross;
+               dlx0 = p0->dy;
+               dly0 = -p0->dx;
+               dlx1 = p1->dy;
+               dly1 = -p1->dx;
+               // Calculate extrusions
+               p1->dmx = (dlx0 + dlx1) * 0.5f;
+               p1->dmy = (dly0 + dly1) * 0.5f;
+               dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
+               if (dmr2 > 0.000001f) {
+                       float s2 = 1.0f / dmr2;
+                       if (s2 > 600.0f) {
+                               s2 = 600.0f;
+                       }
+                       p1->dmx *= s2;
+                       p1->dmy *= s2;
+               }
+
+               // Clear flags, but keep the corner.
+               p1->flags = (p1->flags & NSVG_PT_CORNER) ? NSVG_PT_CORNER : 0;
+
+               // Keep track of left turns.
+               cross = p1->dx * p0->dy - p0->dx * p1->dy;
+               if (cross > 0.0f)
+                       p1->flags |= NSVG_PT_LEFT;
+
+               // Check to see if the corner needs to be beveled.
+               if (p1->flags & NSVG_PT_CORNER) {
+                       if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) {
+                               p1->flags |= NSVG_PT_BEVEL;
+                       }
+               }
+
+               p0 = p1++;
+       }
+}
+
+static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float scale)
+{
+       int i, j, closed;
+       NSVGpath* path;
+       NSVGpoint* p0, *p1;
+       float miterLimit = shape->miterLimit;
+       int lineJoin = shape->strokeLineJoin;
+       int lineCap = shape->strokeLineCap;
+       float lineWidth = shape->strokeWidth * scale;
+
+       for (path = shape->paths; path != NULL; path = path->next) {
+               // Flatten path
+               r->npoints = 0;
+               nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, NSVG_PT_CORNER);
+               for (i = 0; i < path->npts-1; i += 3) {
+                       float* p = &path->pts[i*2];
+                       nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, NSVG_PT_CORNER);
+               }
+               if (r->npoints < 2)
+                       continue;
+
+               closed = path->closed;
+
+               // If the first and last points are the same, remove the last, mark as closed path.
+               p0 = &r->points[r->npoints-1];
+               p1 = &r->points[0];
+               if (nsvg__ptEquals(p0->x,p0->y, p1->x,p1->y, r->distTol)) {
+                       r->npoints--;
+                       p0 = &r->points[r->npoints-1];
+                       closed = 1;
+               }
+
+               if (shape->strokeDashCount > 0) {
+                       int idash = 0, dashState = 1;
+                       float totalDist = 0, dashLen, allDashLen, dashOffset;
+                       NSVGpoint cur;
+
+                       if (closed)
+                               nsvg__appendPathPoint(r, r->points[0]);
+
+                       // Duplicate points -> points2.
+                       nsvg__duplicatePoints(r);
+
+                       r->npoints = 0;
+                       cur = r->points2[0];
+                       nsvg__appendPathPoint(r, cur);
+
+                       // Figure out dash offset.
+                       allDashLen = 0;
+                       for (j = 0; j < shape->strokeDashCount; j++)
+                               allDashLen += shape->strokeDashArray[j];
+                       if (shape->strokeDashCount & 1)
+                               allDashLen *= 2.0f;
+                       // Find location inside pattern
+                       dashOffset = fmodf(shape->strokeDashOffset, allDashLen);
+                       if (dashOffset < 0.0f)
+                               dashOffset += allDashLen;
+
+                       while (dashOffset > shape->strokeDashArray[idash]) {
+                               dashOffset -= shape->strokeDashArray[idash];
+                               idash = (idash + 1) % shape->strokeDashCount;
+                       }
+                       dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale;
+
+                       for (j = 1; j < r->npoints2; ) {
+                               float dx = r->points2[j].x - cur.x;
+                               float dy = r->points2[j].y - cur.y;
+                               float dist = sqrtf(dx*dx + dy*dy);
+
+                               if ((totalDist + dist) > dashLen) {
+                                       // Calculate intermediate point
+                                       float d = (dashLen - totalDist) / dist;
+                                       float x = cur.x + dx * d;
+                                       float y = cur.y + dy * d;
+                                       nsvg__addPathPoint(r, x, y, NSVG_PT_CORNER);
+
+                                       // Stroke
+                                       if (r->npoints > 1 && dashState) {
+                                               nsvg__prepareStroke(r, miterLimit, lineJoin);
+                                               nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
+                                       }
+                                       // Advance dash pattern
+                                       dashState = !dashState;
+                                       idash = (idash+1) % shape->strokeDashCount;
+                                       dashLen = shape->strokeDashArray[idash] * scale;
+                                       // Restart
+                                       cur.x = x;
+                                       cur.y = y;
+                                       cur.flags = NSVG_PT_CORNER;
+                                       totalDist = 0.0f;
+                                       r->npoints = 0;
+                                       nsvg__appendPathPoint(r, cur);
+                               } else {
+                                       totalDist += dist;
+                                       cur = r->points2[j];
+                                       nsvg__appendPathPoint(r, cur);
+                                       j++;
+                               }
+                       }
+                       // Stroke any leftover path
+                       if (r->npoints > 1 && dashState)
+                               nsvg__expandStroke(r, r->points, r->npoints, 0, lineJoin, lineCap, lineWidth);
+               } else {
+                       nsvg__prepareStroke(r, miterLimit, lineJoin);
+                       nsvg__expandStroke(r, r->points, r->npoints, closed, lineJoin, lineCap, lineWidth);
+               }
+       }
+}
+
+static int nsvg__cmpEdge(const void *p, const void *q)
+{
+       const NSVGedge* a = (const NSVGedge*)p;
+       const NSVGedge* b = (const NSVGedge*)q;
+
+       if (a->y0 < b->y0) return -1;
+       if (a->y0 > b->y0) return  1;
+       return 0;
+}
+
+
+static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float startPoint)
+{
+        NSVGactiveEdge* z;
+
+       if (r->freelist != NULL) {
+               // Restore from freelist.
+               z = r->freelist;
+               r->freelist = z->next;
+       } else {
+               // Alloc new edge.
+               z = (NSVGactiveEdge*)nsvg__alloc(r, sizeof(NSVGactiveEdge));
+               if (z == NULL) return NULL;
+       }
+
+       float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
+//     STBTT_assert(e->y0 <= start_point);
+       // round dx down to avoid going too far
+       if (dxdy < 0)
+               z->dx = (int)(-floorf(NSVG__FIX * -dxdy));
+       else
+               z->dx = (int)floorf(NSVG__FIX * dxdy);
+       z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)));
+//     z->x -= off_x * FIX;
+       z->ey = e->y1;
+       z->next = 0;
+       z->dir = e->dir;
+
+       return z;
+}
+
+static void nsvg__freeActive(NSVGrasterizer* r, NSVGactiveEdge* z)
+{
+       z->next = r->freelist;
+       r->freelist = z;
+}
+
+static void nsvg__fillScanline(unsigned char* scanline, int len, int x0, int x1, int maxWeight, int* xmin, int* xmax)
+{
+       int i = x0 >> NSVG__FIXSHIFT;
+       int j = x1 >> NSVG__FIXSHIFT;
+       if (i < *xmin) *xmin = i;
+       if (j > *xmax) *xmax = j;
+       if (i < len && j >= 0) {
+               if (i == j) {
+                       // x0,x1 are the same pixel, so compute combined coverage
+                       scanline[i] = (unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT));
+               } else {
+                       if (i >= 0) // add antialiasing for x0
+                               scanline[i] = (unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT));
+                       else
+                               i = -1; // clip
+
+                       if (j < len) // add antialiasing for x1
+                               scanline[j] = (unsigned char)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT));
+                       else
+                               j = len; // clip
+
+                       for (++i; i < j; ++i) // fill pixels between x0 and x1
+                               scanline[i] = (unsigned char)(scanline[i] + maxWeight);
+               }
+       }
+}
+
+// note: this routine clips fills that extend off the edges... ideally this
+// wouldn't happen, but it could happen if the truetype glyph bounding boxes
+// are wrong, or if the user supplies a too-small bitmap
+
+/**
+ * In the original file, using char type (without signed or unsigned) can be interpreted
+ * as 'unsigned char' in some build environments, like ARM architecture.
+ * To prevent the unexpected behavior, we replace 'char fillRule' with 'signed char fillRule' here.
+ */
+static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEdge* e, int maxWeight, int* xmin, int* xmax, signed char fillRule)
+{
+       // non-zero winding fill
+       int x0 = 0, w = 0;
+
+       if (fillRule == NSVG_FILLRULE_NONZERO) {
+               // Non-zero
+               while (e != NULL) {
+                       if (w == 0) {
+                               // if we're currently at zero, we need to record the edge start point
+                               x0 = e->x; w += e->dir;
+                       } else {
+                               int x1 = e->x; w += e->dir;
+                               // if we went to zero, we need to draw
+                               if (w == 0)
+                                       nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
+                       }
+                       e = e->next;
+               }
+       } else if (fillRule == NSVG_FILLRULE_EVENODD) {
+               // Even-odd
+               while (e != NULL) {
+                       if (w == 0) {
+                               // if we're currently at zero, we need to record the edge start point
+                               x0 = e->x; w = 1;
+                       } else {
+                               int x1 = e->x; w = 0;
+                               nsvg__fillScanline(scanline, len, x0, x1, maxWeight, xmin, xmax);
+                       }
+                       e = e->next;
+               }
+       }
+}
+
+static float nsvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
+
+static unsigned int nsvg__RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
+{
+       return (r) | (g << 8) | (b << 16) | (a << 24);
+}
+
+static unsigned int nsvg__lerpRGBA(unsigned int c0, unsigned int c1, float u)
+{
+       int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
+       int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8;
+       int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8;
+       int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8;
+       int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8;
+       return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a);
+}
+
+static unsigned int nsvg__applyOpacity(unsigned int c, float u)
+{
+       int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f);
+       int r = (c) & 0xff;
+       int g = (c>>8) & 0xff;
+       int b = (c>>16) & 0xff;
+       int a = (((c>>24) & 0xff)*iu) >> 8;
+       return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a);
+}
+
+static inline int nsvg__div255(int x)
+{
+    return ((x+1) * 257) >> 16;
+}
+
+static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y,
+                                                               float tx, float ty, float scale, NSVGcachedPaint* cache)
+{
+
+       if (cache->type == NSVG_PAINT_COLOR) {
+               int i, cr, cg, cb, ca;
+               cr = cache->colors[0] & 0xff;
+               cg = (cache->colors[0] >> 8) & 0xff;
+               cb = (cache->colors[0] >> 16) & 0xff;
+               ca = (cache->colors[0] >> 24) & 0xff;
+
+               for (i = 0; i < count; i++) {
+                       int r,g,b;
+                       int a = nsvg__div255((int)cover[0] * ca);
+                       int ia = 255 - a;
+                       // Premultiply
+                       r = nsvg__div255(cr * a);
+                       g = nsvg__div255(cg * a);
+                       b = nsvg__div255(cb * a);
+
+                       // Blend over
+                       r += nsvg__div255(ia * (int)dst[0]);
+                       g += nsvg__div255(ia * (int)dst[1]);
+                       b += nsvg__div255(ia * (int)dst[2]);
+                       a += nsvg__div255(ia * (int)dst[3]);
+
+                       dst[0] = (unsigned char)r;
+                       dst[1] = (unsigned char)g;
+                       dst[2] = (unsigned char)b;
+                       dst[3] = (unsigned char)a;
+
+                       cover++;
+                       dst += 4;
+               }
+       } else if (cache->type == NSVG_PAINT_LINEAR_GRADIENT) {
+               // TODO: spread modes.
+               // TODO: plenty of opportunities to optimize.
+               float fx, fy, dx, gy;
+               float* t = cache->xform;
+               int i, cr, cg, cb, ca;
+               unsigned int c;
+
+               fx = ((float)x - tx) / scale;
+               fy = ((float)y - ty) / scale;
+               dx = 1.0f / scale;
+
+               for (i = 0; i < count; i++) {
+                       int r,g,b,a,ia;
+                       gy = fx*t[1] + fy*t[3] + t[5];
+                       c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)];
+                       cr = (c) & 0xff;
+                       cg = (c >> 8) & 0xff;
+                       cb = (c >> 16) & 0xff;
+                       ca = (c >> 24) & 0xff;
+
+                       a = nsvg__div255((int)cover[0] * ca);
+                       ia = 255 - a;
+
+                       // Premultiply
+                       r = nsvg__div255(cr * a);
+                       g = nsvg__div255(cg * a);
+                       b = nsvg__div255(cb * a);
+
+                       // Blend over
+                       r += nsvg__div255(ia * (int)dst[0]);
+                       g += nsvg__div255(ia * (int)dst[1]);
+                       b += nsvg__div255(ia * (int)dst[2]);
+                       a += nsvg__div255(ia * (int)dst[3]);
+
+                       dst[0] = (unsigned char)r;
+                       dst[1] = (unsigned char)g;
+                       dst[2] = (unsigned char)b;
+                       dst[3] = (unsigned char)a;
+
+                       cover++;
+                       dst += 4;
+                       fx += dx;
+               }
+       } else if (cache->type == NSVG_PAINT_RADIAL_GRADIENT) {
+               // TODO: spread modes.
+               // TODO: plenty of opportunities to optimize.
+               // TODO: focus (fx,fy)
+               float fx, fy, dx, gx, gy, gd;
+               float* t = cache->xform;
+               int i, cr, cg, cb, ca;
+               unsigned int c;
+
+               fx = ((float)x - tx) / scale;
+               fy = ((float)y - ty) / scale;
+               dx = 1.0f / scale;
+
+               for (i = 0; i < count; i++) {
+                       int r,g,b,a,ia;
+                       gx = fx*t[0] + fy*t[2] + t[4];
+                       gy = fx*t[1] + fy*t[3] + t[5];
+                       gd = sqrtf(gx*gx + gy*gy);
+                       c = cache->colors[(int)nsvg__clampf(gd*255.0f, 0, 255.0f)];
+                       cr = (c) & 0xff;
+                       cg = (c >> 8) & 0xff;
+                       cb = (c >> 16) & 0xff;
+                       ca = (c >> 24) & 0xff;
+
+                       a = nsvg__div255((int)cover[0] * ca);
+                       ia = 255 - a;
+
+                       // Premultiply
+                       r = nsvg__div255(cr * a);
+                       g = nsvg__div255(cg * a);
+                       b = nsvg__div255(cb * a);
+
+                       // Blend over
+                       r += nsvg__div255(ia * (int)dst[0]);
+                       g += nsvg__div255(ia * (int)dst[1]);
+                       b += nsvg__div255(ia * (int)dst[2]);
+                       a += nsvg__div255(ia * (int)dst[3]);
+
+                       dst[0] = (unsigned char)r;
+                       dst[1] = (unsigned char)g;
+                       dst[2] = (unsigned char)b;
+                       dst[3] = (unsigned char)a;
+
+                       cover++;
+                       dst += 4;
+                       fx += dx;
+               }
+       }
+}
+
+static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float scale, NSVGcachedPaint* cache, char fillRule)
+{
+       NSVGactiveEdge *active = NULL;
+       int y, s;
+       int e = 0;
+       int maxWeight = (255 / NSVG__SUBSAMPLES);  // weight per vertical scanline
+       int xmin, xmax;
+
+       for (y = 0; y < r->height; y++) {
+               memset(r->scanline, 0, r->width);
+               xmin = r->width;
+               xmax = 0;
+               for (s = 0; s < NSVG__SUBSAMPLES; ++s) {
+                       // find center of pixel for this scanline
+                       float scany = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f;
+                       NSVGactiveEdge **step = &active;
+
+                       // update all active edges;
+                       // remove all active edges that terminate before the center of this scanline
+                       while (*step) {
+                               NSVGactiveEdge *z = *step;
+                               if (z->ey <= scany) {
+                                       *step = z->next; // delete from list
+//                                     NSVG__assert(z->valid);
+                                       nsvg__freeActive(r, z);
+                               } else {
+                                       z->x += z->dx; // advance to position for current scanline
+                                       step = &((*step)->next); // advance through list
+                               }
+                       }
+
+                       // resort the list if needed
+                       for (;;) {
+                               int changed = 0;
+                               step = &active;
+                               while (*step && (*step)->next) {
+                                       if ((*step)->x > (*step)->next->x) {
+                                               NSVGactiveEdge* t = *step;
+                                               NSVGactiveEdge* q = t->next;
+                                               t->next = q->next;
+                                               q->next = t;
+                                               *step = q;
+                                               changed = 1;
+                                       }
+                                       step = &(*step)->next;
+                               }
+                               if (!changed) break;
+                       }
+
+                       // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline
+                       while (e < r->nedges && r->edges[e].y0 <= scany) {
+                               if (r->edges[e].y1 > scany) {
+                                       NSVGactiveEdge* z = nsvg__addActive(r, &r->edges[e], scany);
+                                       if (z == NULL) break;
+                                       // find insertion point
+                                       if (active == NULL) {
+                                               active = z;
+                                       } else if (z->x < active->x) {
+                                               // insert at front
+                                               z->next = active;
+                                               active = z;
+                                       } else {
+                                               // find thing to insert AFTER
+                                               NSVGactiveEdge* p = active;
+                                               while (p->next && p->next->x < z->x)
+                                                       p = p->next;
+                                               // at this point, p->next->x is NOT < z->x
+                                               z->next = p->next;
+                                               p->next = z;
+                                       }
+                               }
+                               e++;
+                       }
+
+                       // now process all active edges in non-zero fashion
+                       if (active != NULL)
+                               nsvg__fillActiveEdges(r->scanline, r->width, active, maxWeight, &xmin, &xmax, fillRule);
+               }
+               // Blit
+               if (xmin < 0) xmin = 0;
+               if (xmax > r->width-1) xmax = r->width-1;
+               if (xmin <= xmax) {
+                       nsvg__scanlineSolid(&r->bitmap[y * r->stride] + xmin*4, xmax-xmin+1, &r->scanline[xmin], xmin, y, tx,ty, scale, cache);
+               }
+       }
+
+}
+
+/**
+ * In the original software, an function 'static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int stride)' is implemented here.
+ * We removed this function, as our renderer renders the pre-multiplied alpha format directly, so this process is not required.
+ */
+
+
+static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opacity)
+{
+       int i, j;
+       NSVGgradient* grad;
+
+       cache->type = paint->type;
+
+       if (paint->type == NSVG_PAINT_COLOR) {
+               cache->colors[0] = nsvg__applyOpacity(paint->color, opacity);
+               return;
+       }
+
+       grad = paint->gradient;
+
+       cache->spread = grad->spread;
+       memcpy(cache->xform, grad->xform, sizeof(float)*6);
+
+       if (grad->nstops == 0) {
+               for (i = 0; i < 256; i++)
+                       cache->colors[i] = 0;
+       } if (grad->nstops == 1) {
+               for (i = 0; i < 256; i++)
+                       cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity);
+       } else {
+               unsigned int ca, cb = 0;
+               float ua, ub, du, u;
+               int ia, ib, count;
+
+               ca = nsvg__applyOpacity(grad->stops[0].color, opacity);
+               ua = nsvg__clampf(grad->stops[0].offset, 0, 1);
+               ub = nsvg__clampf(grad->stops[grad->nstops-1].offset, ua, 1);
+               ia = (int)(ua * 255.0f);
+               ib = (int)(ub * 255.0f);
+               for (i = 0; i < ia; i++) {
+                       cache->colors[i] = ca;
+               }
+
+               for (i = 0; i < grad->nstops-1; i++) {
+                       ca = nsvg__applyOpacity(grad->stops[i].color, opacity);
+                       cb = nsvg__applyOpacity(grad->stops[i+1].color, opacity);
+                       ua = nsvg__clampf(grad->stops[i].offset, 0, 1);
+                       ub = nsvg__clampf(grad->stops[i+1].offset, 0, 1);
+                       ia = (int)(ua * 255.0f);
+                       ib = (int)(ub * 255.0f);
+                       count = ib - ia;
+                       if (count <= 0) continue;
+                       u = 0;
+                       du = 1.0f / (float)count;
+                       for (j = 0; j < count; j++) {
+                               cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u);
+                               u += du;
+                       }
+               }
+
+               for (i = ib; i < 256; i++)
+                       cache->colors[i] = cb;
+       }
+
+}
+
+void nsvgRasterize(NSVGrasterizer* r,
+                                  NSVGimage* image, float tx, float ty, float scale,
+                                  unsigned char* dst, int w, int h, int stride)
+{
+       NSVGshape *shape = NULL;
+       NSVGedge *e = NULL;
+       NSVGcachedPaint cache;
+       int i;
+
+       r->bitmap = dst;
+       r->width = w;
+       r->height = h;
+       r->stride = stride;
+
+       if (w > r->cscanline) {
+               r->cscanline = w;
+               r->scanline = (unsigned char*)realloc(r->scanline, w);
+               if (r->scanline == NULL) return;
+       }
+
+       for (i = 0; i < h; i++)
+               memset(&dst[i*stride], 0, w*4);
+
+       for (shape = image->shapes; shape != NULL; shape = shape->next) {
+               if (!(shape->flags & NSVG_FLAGS_VISIBLE))
+                       continue;
+
+               if (shape->fill.type != NSVG_PAINT_NONE) {
+                       nsvg__resetPool(r);
+                       r->freelist = NULL;
+                       r->nedges = 0;
+
+                       nsvg__flattenShape(r, shape, scale);
+
+                       // Scale and translate edges
+                       for (i = 0; i < r->nedges; i++) {
+                               e = &r->edges[i];
+                               e->x0 = tx + e->x0;
+                               e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
+                               e->x1 = tx + e->x1;
+                               e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
+                       }
+
+                       // Rasterize edges
+                       qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge);
+
+                       // now, traverse the scanlines and find the intersections on each scanline, use non-zero rule
+                       nsvg__initPaint(&cache, &shape->fill, shape->opacity);
+
+                       nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, shape->fillRule);
+               }
+               if (shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01f) {
+                       nsvg__resetPool(r);
+                       r->freelist = NULL;
+                       r->nedges = 0;
+
+                       nsvg__flattenShapeStroke(r, shape, scale);
+
+//                     dumpEdges(r, "edge.svg");
+
+                       // Scale and translate edges
+                       for (i = 0; i < r->nedges; i++) {
+                               e = &r->edges[i];
+                               e->x0 = tx + e->x0;
+                               e->y0 = (ty + e->y0) * NSVG__SUBSAMPLES;
+                               e->x1 = tx + e->x1;
+                               e->y1 = (ty + e->y1) * NSVG__SUBSAMPLES;
+                       }
+
+                       // Rasterize edges
+                       qsort(r->edges, r->nedges, sizeof(NSVGedge), nsvg__cmpEdge);
+
+                       // now, traverse the scanlines and find the intersections on each scanline, use non-zero rule
+                       nsvg__initPaint(&cache, &shape->stroke, shape->opacity);
+
+                       nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, NSVG_FILLRULE_NONZERO);
+               }
+       }
+
+    /**
+     * In the original file, the pre-multiplied alpha format is transformed to the convertional non-pre format.
+     * We skip this process here, and render the pre-multiplied alpha format directly in our svg renderer.
+     */
+
+       r->bitmap = NULL;
+       r->width = 0;
+       r->height = 0;
+       r->stride = 0;
+}
diff --git a/third-party/nanosvg/nanosvgrast.h b/third-party/nanosvg/nanosvgrast.h
new file mode 100644 (file)
index 0000000..4d6ea47
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2013-14 Mikko Mononen memon@inside.org
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ *
+ * The polygon rasterization is heavily based on stb_truetype rasterizer
+ * by Sean Barrett - http://nothings.org/
+ *
+ */
+
+#ifndef NANOSVGRAST_H
+#define NANOSVGRAST_H
+
+#include "nanosvg.h"
+
+typedef struct NSVGrasterizer NSVGrasterizer;
+
+/* Example Usage:
+  // Load SVG
+  NSVGimage* image;
+  image = nsvgParseFromFile("test.svg", "px", 96);
+
+  // Create rasterizer (can be used to render multiple images).
+  struct NSVGrasterizer* rast = nsvgCreateRasterizer();
+  // Allocate memory for image
+  unsigned char* img = malloc(w*h*4);
+  // Rasterize
+  nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);
+*/
+
+// Allocated rasterizer context.
+NSVGrasterizer* nsvgCreateRasterizer();
+
+// Rasterizes SVG image, returns RGBA image (non-premultiplied alpha)
+//   r - pointer to rasterizer context
+//   image - pointer to image to rasterize
+//   tx,ty - image offset (applied after scaling)
+//   scale - image scale
+//   dst - pointer to destination image data, 4 bytes per pixel (RGBA)
+//   w - width of the image to render
+//   h - height of the image to render
+//   stride - number of bytes per scaleline in the destination buffer
+void nsvgRasterize(NSVGrasterizer* r,
+                   NSVGimage* image, float tx, float ty, float scale,
+                   unsigned char* dst, int w, int h, int stride);
+
+// Deletes rasterizer context.
+void nsvgDeleteRasterizer(NSVGrasterizer*);
+
+
+#endif // NANOSVGRAST_H
+
+/**
+ * In the original software, The nanosvgrast implementation was followed here.
+ * We have moved the implementation to nanosvgrast.cc.
+ */