如何在 Objective-C 中从 SVG 数据创建汉字(日语字母)动画?

发布于 2024-11-04 23:45:11 字数 548 浏览 1 评论 0原文

我见过几个显示动画汉字的 iPhone/iPad 应用程序。对于那些不熟悉汉字的人来说,笔画顺序是汉字学习中非常重要的一部分,因此如果您正在制作一个应用程序,显示动画笔画顺序是一个重要的部分。

我见过的所有执行此操作的应用程序都将 KanjiVG 项目作为笔画顺序数据的来源。经过一番研究,我发现 KanjiVG 项目为您提供了以 XML 编码的 SVG 格式的数据。

以前从未编程过图形(并且对 iOS 来说相对较新),我不知道在哪里继续寻找信息。

认为我需要:

  1. 将 XML 解析为 SVG。
  2. 渲染 SVG。

...但我不确定。据我所知,这是如何在我购买的 iPhone/iPad 应用程序中完成的,动画看起来都非常相似,所以一定有一个我没有找到的这些人正在使用的通用库(可能是因为我不知道)确切地知道我在寻找什么!)

任何人都可以给我的任何指示将不胜感激。

干杯!

I've seen several iPhone/iPad apps that show animated kanji. For those of you who are unfamiliar with kanji, stroke order is a very important part of kanji studying so if you are doing an app showing the animated stroke order is an essential part.

All the apps I've seen that do this, credit the KanjiVG project as their source for the stroke order data. After some research I found that the KanjiVG project gives you the data in SVG format encoded in XML.

Having never programmed graphics before (and being relatively new to iOS) I'm at a loss to where to keep looking for info.

I think I need to:

  1. Parse the XML into SVG.
  2. Render the SVG.

...but I'm not sure. For what I could see how this is done in the iPhone/iPad apps I bought, the animations all look surprisingly similar so there must be a common library that these guys are using that I'm failing to find (probably because I don't know exactly what I'm looking for!)

Any pointers that anyone can give me will be greatly appreciated.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海少女心 2024-11-11 23:45:11

这最终比我最初想象的要容易得多。 KanjiVG 项目提供的 XML 不仅包含所有汉字“部分”,还包含 SVG 数据!

所以你会得到这样的结果:

<kanji midashi="会" id="4f1a">
<strokegr element="会">
    <strokegr element="人" position="top" radical="general">
        <stroke type="㇒" path="M52.25,14c0.25,2.28-0.52,3.59-1.8,5.62c-5.76,9.14-17.9,27-39.2,39.88"/>
        <stroke type="㇏" path="M54.5,19.25c6.73,7.3,24.09,24.81,32.95,31.91c2.73,2.18,5.61,3.8,9.05,4.59"/>
    </strokegr>
    <strokegr element="云" position="bottom">
        <strokegr element="二">
            <stroke type="㇐" path="M37.36,50.16c1.64,0.34,4.04,0.36,4.98,0.25c6.79-0.79,14.29-1.91,19.66-2.4c1.56-0.14,3.25-0.39,4.66,0"/>
            <stroke type="㇐" path="M23,65.98c2.12,0.52,4.25,0.64,7.01,0.3c13.77-1.71,30.99-3.66,46.35-3.74c3.04-0.02,4.87,0.14,6.4,0.29"/>
        </strokegr>
        <strokegr element="厶">
            <stroke type="㇜" path="M47.16,66.38c0.62,1.65-0.03,2.93-0.92,4.28c-5.17,7.8-8.02,11.38-14.99,18.84c-2.11,2.25-1.5,4.18,2,3.75c7.35-0.91,28.19-5.83,40.16-7.95"/>
            <stroke type="㇔" path="M66.62,77.39c4.52,3.23,11,12.73,13.06,18.82"/>
        </strokegr>
    </strokegr>
</strokegr>
</kanji>

如果你仅使用 lines 节点的 path 属性创建 SVG 文件,那么你会得到一个漂亮的 SVG 绘图!像这样:

<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" version="1.1"  baseProfile="full"> 
<path d="M52.25,14c0.25,2.28-0.52,3.59-1.8,5.62c-5.76,9.14-17.9,27-39.2,39.88" style="fill:none;stroke:black;stroke-width:2" />
<path d="M54.5,19.25c6.73,7.3,24.09,24.81,32.95,31.91c2.73,2.18,5.61,3.8,9.05,4.59" style="fill:none;stroke:black;stroke-width:2" />
<path d="M37.36,50.16c1.64,0.34,4.04,0.36,4.98,0.25c6.79-0.79,14.29-1.91,19.66-2.4c1.56-0.14,3.25-0.39,4.66,0" style="fill:none;stroke:black;stroke-width:2" />
<path d="M23,65.98c2.12,0.52,4.25,0.64,7.01,0.3c13.77-1.71,30.99-3.66,46.35-3.74c3.04-0.02,4.87,0.14,6.4,0.29" style="fill:none;stroke:black;stroke-width:2" />
<path d="M47.16,66.38c0.62,1.65-0.03,2.93-0.92,4.28c-5.17,7.8-8.02,11.38-14.99,18.84c-2.11,2.25-1.5,4.18,2,3.75c7.35-0.91,28.19-5.83,40.16-7.95" style="fill:none;stroke:black;stroke-width:2" />
<path d="M66.62,77.39c4.52,3.23,11,12.73,13.06,18.82" style="fill:none;stroke:black;stroke-width:2" />
</svg>

复制上面的 SVG XML 并将其粘贴到纯文本文件中。将此文件命名为以 .svg 结尾的文件并将其拖到 Firefox 中。这就是汉字的图形表示!

现在我已经拥有了所有“原始”SVG 信息,只需找到合适的 SVG 渲染器即可。

This ended up being SO MUCH easier than I originally thought. The XML provided by the KanjiVG project not only contains all the kanji "parts" but the SVG data aswell!

So you get this:

<kanji midashi="会" id="4f1a">
<strokegr element="会">
    <strokegr element="人" position="top" radical="general">
        <stroke type="㇒" path="M52.25,14c0.25,2.28-0.52,3.59-1.8,5.62c-5.76,9.14-17.9,27-39.2,39.88"/>
        <stroke type="㇏" path="M54.5,19.25c6.73,7.3,24.09,24.81,32.95,31.91c2.73,2.18,5.61,3.8,9.05,4.59"/>
    </strokegr>
    <strokegr element="云" position="bottom">
        <strokegr element="二">
            <stroke type="㇐" path="M37.36,50.16c1.64,0.34,4.04,0.36,4.98,0.25c6.79-0.79,14.29-1.91,19.66-2.4c1.56-0.14,3.25-0.39,4.66,0"/>
            <stroke type="㇐" path="M23,65.98c2.12,0.52,4.25,0.64,7.01,0.3c13.77-1.71,30.99-3.66,46.35-3.74c3.04-0.02,4.87,0.14,6.4,0.29"/>
        </strokegr>
        <strokegr element="厶">
            <stroke type="㇜" path="M47.16,66.38c0.62,1.65-0.03,2.93-0.92,4.28c-5.17,7.8-8.02,11.38-14.99,18.84c-2.11,2.25-1.5,4.18,2,3.75c7.35-0.91,28.19-5.83,40.16-7.95"/>
            <stroke type="㇔" path="M66.62,77.39c4.52,3.23,11,12.73,13.06,18.82"/>
        </strokegr>
    </strokegr>
</strokegr>
</kanji>

And if you create your SVG file out of only the path attributes of the stroke nodes then you get a nice SVG drawing! Like this:

<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" version="1.1"  baseProfile="full"> 
<path d="M52.25,14c0.25,2.28-0.52,3.59-1.8,5.62c-5.76,9.14-17.9,27-39.2,39.88" style="fill:none;stroke:black;stroke-width:2" />
<path d="M54.5,19.25c6.73,7.3,24.09,24.81,32.95,31.91c2.73,2.18,5.61,3.8,9.05,4.59" style="fill:none;stroke:black;stroke-width:2" />
<path d="M37.36,50.16c1.64,0.34,4.04,0.36,4.98,0.25c6.79-0.79,14.29-1.91,19.66-2.4c1.56-0.14,3.25-0.39,4.66,0" style="fill:none;stroke:black;stroke-width:2" />
<path d="M23,65.98c2.12,0.52,4.25,0.64,7.01,0.3c13.77-1.71,30.99-3.66,46.35-3.74c3.04-0.02,4.87,0.14,6.4,0.29" style="fill:none;stroke:black;stroke-width:2" />
<path d="M47.16,66.38c0.62,1.65-0.03,2.93-0.92,4.28c-5.17,7.8-8.02,11.38-14.99,18.84c-2.11,2.25-1.5,4.18,2,3.75c7.35-0.91,28.19-5.83,40.16-7.95" style="fill:none;stroke:black;stroke-width:2" />
<path d="M66.62,77.39c4.52,3.23,11,12.73,13.06,18.82" style="fill:none;stroke:black;stroke-width:2" />
</svg>

Copy the above SVG XML and paste it into a plain text file. Name this file something that ends in .svg and drag it into Firefox. There it is, a graphic representation of the Kanji!

So now that I have all the "raw" SVG info it's just a matter of finding the appropriate SVG renderer.

甜`诱少女 2024-11-11 23:45:11

几年前,我为 KanjiVG 数据编写了一个 javascript 渲染器,用于制作斯托克斯动画。它可以作为您的工作示例,甚至可以作为解决方案,具体取决于您想要做什么。

我采取的方法是将 KanjiVG 笔划数据分解为一组 JavaScript 数据文件,编写自己的代码来绘制三次曲线和二次曲线,然后编写一个事件队列函数,该函数采用汉字、查找它们并将每个笔划的渲染排入队列在一个数组中。

来源没有以任何方式混淆,并且包含奇怪的评论。祝你好运!

I wrote a javascript renderer for KanjiVG data a few years back that animates stokes. It might serve as a working example for you or even a solution depending on what you want to do.

The approach I took was to break the KanjiVG stroke data into a set of javascript data files, write my own code for drawing cubic and quadratic curves and then write an event queue function that takes kanji, looks them up and enqueues the rendering of each stroke in an array.

The source is not obfuscated in any way and contains the odd comment. Best of luck!

帅哥哥的热头脑 2024-11-11 23:45:11

我对此也很感兴趣。你有进一步的进展吗?
我可以通过创建 UIwebview 以这种方式显示 svg 文件中的汉字。
在此示例中,我的文件是 k1.svg,图像是在点击按钮(发送者)后绘制的。我现在正在努力将其动画化

-(void) doSVG:(id)sender {

    NSString *filePath = [[NSBundle mainBundle]
                          pathForResource:@"k1" ofType:@"svg"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    //[kanjiView setScalesPageToFit:YES];
    [yourWebView loadRequest:req];  

}

我的问题是,您是否找到了一种简单的方法来从 kanjiVG 的 xml 文件中获取所有 svg 信息?

I'm interested in this as well. Have you gotten any further?
I am able to display the kanji from an svg file this way by creating a UIwebview.
In this example my file is k1.svg, and the image was drawn after hitting a button (the sender). I'm working on getting it animated now

-(void) doSVG:(id)sender {

    NSString *filePath = [[NSBundle mainBundle]
                          pathForResource:@"k1" ofType:@"svg"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    //[kanjiView setScalesPageToFit:YES];
    [yourWebView loadRequest:req];  

}

My question to you is, have you found an easy way to get all the svg info out of the xml file from kanjiVG?

无畏 2024-11-11 23:45:11

不需要将 KanjiVG 数据转换为 SVG 数据,因为它已经是 SVG 数据。来自他们的维基

任何 KanjiVG 文件都 100% SVG 兼容,并且可以由用户最喜欢的 SVG 查看器/编辑器打开并按原样查看。

数据看起来如此不同的原因是它们使用 SVG 组来存储额外的信息。但 KanjiVG 数据本身应该仍然可以使用任何符合标准的 SVG 库呈现良好。

There is no need to translate the KanjiVG data into SVG data, because it already is SVG data. From their wiki:

Any KanjiVG file is 100% SVG-compliant and can be opened by one's favorite SVG viewer/editor to be seen as-is.

The reason the data looks so different is that they're using SVG Groups to store extra information. But the KanjiVG data itself should still render fine using any standards-compliant SVG library.

相思碎 2024-11-11 23:45:11

您还可以将“AnimCJK项目”(https://github.com/parsimonhi/animCJK)视为汉字 svg 数据源。其原理与“KanjiVG 项目”(http://kanjivg.tagaini.net/)略有不同,底层字体不同(kaisho 风格),但提取路径数据很简单。使用正则表达式,例如 #]+id="z[0-9]+d[0-9]+"[^>]+d="([^"]+)" [^>]+># 完成这项工作。

You can also consider "AnimCJK project" (https://github.com/parsimonhi/animCJK) as source for kanji svg data. The principle is slightly different from "KanjiVG project" (http://kanjivg.tagaini.net/) and the underlying font is different (kaisho style), but extracting the path data is simple. Using a regex such as #<path[^>]+id="z[0-9]+d[0-9]+"[^>]+d="([^"]+)"[^>]+># does the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文