Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Polygon #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/svg-to-swiftui-core/src/elementHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import handleEllipseElement from './ellipseElementHandler';
import handleGroupElement from './groupElementHandler';
import handlePathElement from './pathElementHandler';
import handleRectElement from './rectElementHandler';
import handlePolygonElement from './polygonElementHandler';

export function handleElement(
element: ElementNode,
Expand All @@ -29,6 +30,9 @@ export function handleElement(
case 'ellipse':
return handleEllipseElement(element, options);

case 'polygon':
return handlePolygonElement(element, options);

default:
console.error(
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {ElementNode} from 'svg-parser';
import {SVGPolygonAttributes} from '../svgTypes';
import {TranspilerOptions} from '../types';
import { generateMoveToSwift } from './pathElementHandler/moveToGenerator';
import { generateLineToSwift } from './pathElementHandler/lineToGenerator';

export default function handlePolygonElement(
element: ElementNode,
options: TranspilerOptions
): string[] {
const swiftAccumulator: string[] = [];
// TODO: style
const props = element.properties;
if (props) {
const ellipseProps = props as unknown as SVGPolygonAttributes;

const width = options.width;
const height = options.height;

const points = ellipseProps.points as string;

if (points) {
// Split the string by spaces to get each coordinate pair
const pairs = points.split(' ');

var firstPoint = true;
// Map each pair to corresponding point in path
const coordinates = pairs.map(pair => {
const [x, y] = pair.split(',').map(Number);
if (x && y) {
if (firstPoint) {
swiftAccumulator.push(...generateMoveToSwift({x, y}, options));
} else {
swiftAccumulator.push(...generateLineToSwift({x, y}, options));
}
firstPoint = false;
}
});
return swiftAccumulator;
}
return [];
} else {
throw new Error('Polygon element should have some properties');
}
}
5 changes: 5 additions & 0 deletions packages/svg-to-swiftui-core/src/svgTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface SVGEllipseAttributes extends SVGBaseAttributes {
pathLength?: string;
}

export interface SVGPolygonAttributes extends SVGBaseAttributes {
points?: string;
fill?: string;
}

export interface SVGRectAttributes extends SVGBaseAttributes {
x: string;
y: string;
Expand Down