在Objective-C中解析SVG路径 [英] Parsing SVG path in objective-c

查看:132
本文介绍了在Objective-C中解析SVG路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析SVG文件的某些路径,它们是简单的行.当检索数据时,我最终得到以下字符串:

m 0,666.6479 254.28571,0

根据SVG规范,m表示新的当前点,然后以下两个数字是位置,后面的两个数字是相对于第一个的相对位置.

因此这将创建一条从点(0,666.6479)(254.28,666.64)

的线

我该如何在Objective-C中对此进行解析,以便最终得到2个或更多的c0s?

我知道,如果找到m,则以下2个逗号分隔的数字应该是我的第一个点,在每个空格之后,我应该将2个逗号分隔的数字加到当前点以得到下一个. /p>

我不知道如何正确解析它.

谢谢!

解决方案

我将直接使用C,例如:

const char *str = [myString UTF8String];

while(*str)
{
    switch(*str)
    {
        case 'm':
            state = START;
            break;
        case ' ':
            if(state == START) /* we are just after the m */
            {
                state = POINT;
                point = 0;
                break;
            }
            else if(state == POINT)
            {
                if(sscanf(str, "%f,%f", &x, &y) != 2)
                    /* handle error; */
                /* save point somewhere */
                point++;
            }
            break;
    }
    str++;
}

请注意,我只是将其写在编辑框中,仅以它为例.

另一种方法是使用NSScanner,但是我觉得上述方法根据您的要求更简单.

i need to parse some paths of an SVG file, they are simple lines. When retrieving the data i end up with this string:

m 0,666.6479 254.28571,0

According to SVG specifications m denotes a new current point then the following 2 numbers are the position and the laters are relative positions to the first one.

So that would create a line from point (0, 666.6479) to (254.28, 666.64)

How can i parse that in Objective-C so i can end up with those 2 CGPoints or more if there were?

i know that if it finds an m, the following 2 comma separated numbers should be my first point and after each space there are 2 comma separated numbers that i should sum to the current point to get the next one.

What i don't know if how to parse this correctly.

Thanks!

解决方案

I would use C directly, as:

const char *str = [myString UTF8String];

while(*str)
{
    switch(*str)
    {
        case 'm':
            state = START;
            break;
        case ' ':
            if(state == START) /* we are just after the m */
            {
                state = POINT;
                point = 0;
                break;
            }
            else if(state == POINT)
            {
                if(sscanf(str, "%f,%f", &x, &y) != 2)
                    /* handle error; */
                /* save point somewhere */
                point++;
            }
            break;
    }
    str++;
}

Please note that I just written that in the edit box, just take it as an example.

The other approach is to use NSScanner, but I feel like the above approach is simpler as per your requirements.

这篇关于在Objective-C中解析SVG路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆