Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Altered structure of SVG parsed data, added tests.

  • Loading branch information...
commit 0922677cf3b25adb13dd884d52f3ab83589e2672 1 parent c3781d3
@benkasminbullock authored
Showing with 87 additions and 14 deletions.
  1. +13 −9 lib/Data/Kanji/KanjiVG.pm
  2. +31 −5 t/Data-Kanji-KanjiVG.t
  3. +43 −0 t/test.xml
View
22 lib/Data/Kanji/KanjiVG.pm
@@ -107,6 +107,14 @@ sub end
}
my $kanji = $kanjis->{kanji};
if ($element eq 'kanji') {
+ my $id = $kanji->{attr}->{id};
+ if (! defined $id) {
+ die "No id recorded for </kanji>";
+ }
+ my $k = '?';
+ if ($id =~ /([0-9a-f]{5})/i) {
+ $k = chr (hex ($1));
+ }
my $callback = $kanjis->{callback};
if ($callback) {
my $flatten = $kanjis->{flatten};
@@ -117,7 +125,10 @@ sub end
if (ref $parsed_svg ne 'ARRAY') {
die "Not an array reference";
}
- &{$callback} ($kanjis->{callback_data}, $parsed_svg);
+ &{$callback} ($kanjis->{callback_data}, {
+ strokes => $parsed_svg,
+ kanji => $k,
+ });
}
else {
&{$callback} ($kanjis->{callback_data}, $flattened);
@@ -166,14 +177,7 @@ sub parse_svg
if (! @parsed) {
croak "Path '$d' is empty";
}
- my $kanji = '?';
- if ($id =~ /([0-9a-f]{5})/i) {
- $kanji = chr $1;
- }
- push @parsed_svg, {
- strokes => \@parsed,
- kanji => $kanji,
- };
+ push @parsed_svg, \@parsed;
}
# print "Parsed SVG: @parsed_svg\n";
return \@parsed_svg;
View
36 t/Data-Kanji-KanjiVG.t
@@ -4,6 +4,7 @@ use Test::More ;# tests => 1;
BEGIN { use_ok('Data::Kanji::KanjiVG') };
use Data::Kanji::KanjiVG 'parse';
use utf8;
+use FindBin;
# Set up outputs to not print wide character warnings (this is for
# debugging this file, not for the end-user's benefit).
@@ -49,11 +50,19 @@ parse (
parse (
xml => $xml,
- callback => \& svg_callback,
+ callback => \& svg_callback_3,
callback_data => $cbd,
parse_svg => 1,
);
+# Test running with a file.
+
+parse (
+ file_name => "$FindBin::Bin/test.xml",
+ callback => \& svg_callback,
+ parse_svg => 1,
+);
+
done_testing ();
#parse (
@@ -63,13 +72,30 @@ done_testing ();
exit;
-sub svg_callback
+sub svg_callback_3
{
my ($callback_data, $parsed_svg) = @_;
ok ($callback_data eq $cbd, "Callback data OK");
- for my $path (@{$parsed_svg}) {
- ok ($path->{kanji}, "Has kanji information");
- for my $curve (@{$path->{strokes}}) {
+ ok ($parsed_svg->{kanji} eq '', "Has kanji information");
+ for my $path (@{$parsed_svg->{strokes}}) {
+ for my $curve (@$path) {
+ my $type = $curve->{type};
+ ok (defined $type, "Has type information");
+ # Check that everything is "absolute" not relative and not
+ # shortcut.
+ ok ($curve->{position} eq 'absolute', "Is absolute");
+ ok ($type eq 'cubic-bezier' || $type eq 'moveto',
+ "Is moveto or cubic bezier instruction");
+ }
+ }
+}
+
+sub svg_callback
+{
+ my ($callback_data, $parsed_svg) = @_;
+ ok ($parsed_svg->{kanji}, "Has kanji information");
+ for my $path (@{$parsed_svg->{strokes}}) {
+ for my $curve (@$path) {
my $type = $curve->{type};
ok (defined $type, "Has type information");
# Check that everything is "absolute" not relative and not
View
43 t/test.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Copyright (C) 2009/2010/2011 Ulrich Apel.
+This work is distributed under the conditions of the Creative Commons
+Attribution-Share Alike 3.0 Licence. This means you are free:
+* to Share - to copy, distribute and transmit the work
+* to Remix - to adapt the work
+
+Under the following conditions:
+* Attribution. You must attribute the work by stating your use of KanjiVG in
+ your own copyright header and linking to KanjiVG's website
+ (http://kanjivg.tagaini.net)
+* Share Alike. If you alter, transform, or build upon this work, you may
+ distribute the resulting work only under the same or similar license to this
+ one.
+
+See http://creativecommons.org/licenses/by-sa/3.0/ for more details.
+This file has been generated on 2012-04-01, using the latest KanjiVG data
+to this date.
+-->
+<kanjivg>
+<kanji id="kvg:kanji_00021">
+<g id="kvg:00021">
+ <path id="kvg:00021-s1" d="M14.73,15.79c0,6.07-0.29,55.49-0.29,60.55"/>
+</g>
+</kanji>
+<kanji id="kvg:kanji_0002c">
+<g id="kvg:0002c">
+ <path id="kvg:0002c-s1" d="M6.93,103.36c3.61-2.46,6.65-6.21,6.65-13.29c0-1.68-1.36-3.03-3.03-3.03s-3.03,1.36-3.03,3.03s1.36,3.03,3.03,3.03C15.17,93.1,10.4,100.18,6.93,103.36z"/>
+</g>
+</kanji>
+<kanji id="kvg:kanji_04e0b">
+<g id="kvg:04e0b" kvg:element="">
+ <g id="kvg:04e0b-g1" kvg:element="" kvg:radical="general">
+ <path id="kvg:04e0b-s1" kvg:type="" d="M13.25,22.5c0.94,0.23,5.18,0.96,7.74,0.75c17.87-1.5,46.54-4.75,66.38-4.75c2.92,0,6.42,0.75,7.88,1.25"/>
+ </g>
+ <g id="kvg:04e0b-g2" kvg:element="" kvg:variant="true" kvg:original="">
+ <path id="kvg:04e0b-s2" kvg:type="" d="M52.97,23.25c0.93,1.07,1.56,2.75,1.56,5.3c0,8.65-0.2,39.42-0.27,57.2c-0.02,3.86-0.02,5.89-0.02,8.25"/>
+ <path id="kvg:04e0b-s3" kvg:type="" d="M67.83,37.17C72.75,39.5,79.88,47.62,82,52.12"/>
+ </g>
+</g>
+</kanji>
+</kanjivg>
Please sign in to comment.
Something went wrong with that request. Please try again.