Add support for linear gradients (#135)

Fixes #134.
corinna000-feature/color-parameter-sliders
Atul Varma 2021-05-28 12:41:25 -04:00 zatwierdzone przez GitHub
rodzic f4e76f9adb
commit cfa414d74c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 65 dodań i 36 usunięć

Wyświetl plik

@ -27,14 +27,24 @@ export type SvgSymbolGradientStop = {
* This represents a definition that will be referenced * This represents a definition that will be referenced
* from elsewhere in an SVG, such as a radial gradient. * from elsewhere in an SVG, such as a radial gradient.
*/ */
export type SvgSymbolDef = { export type SvgSymbolDef =
type: "radialGradient"; | {
id: string; type: "radialGradient";
cx: string; id: string;
cy: string; cx: string;
r: string; cy: string;
stops: SvgSymbolGradientStop[]; r: string;
}; stops: SvgSymbolGradientStop[];
}
| {
type: "linearGradient";
id: string;
x1: string;
y1: string;
x2: string;
y2: string;
stops: SvgSymbolGradientStop[];
};
export const EMPTY_SVG_SYMBOL_DATA: SvgSymbolData = { export const EMPTY_SVG_SYMBOL_DATA: SvgSymbolData = {
name: "", name: "",
@ -167,24 +177,23 @@ function reactifySvgSymbolElement(
const SvgSymbolDef: React.FC< const SvgSymbolDef: React.FC<
{ def: SvgSymbolDef; uidMap: UniqueIdMap } & SvgSymbolContext { def: SvgSymbolDef; uidMap: UniqueIdMap } & SvgSymbolContext
> = ({ def, uidMap, ...ctx }) => { > = ({ def, uidMap, ...ctx }) => {
const id = uidMap.getStrict(def.id);
const stops = def.stops.map((stop, i) => (
<stop key={i} offset={stop.offset} stopColor={getColor(ctx, stop.color)} />
));
switch (def.type) { switch (def.type) {
case "radialGradient": case "radialGradient":
return ( return (
<radialGradient <radialGradient id={id} cx={def.cx} cy={def.cy} r={def.r}>
id={uidMap.getStrict(def.id)} {stops}
cx={def.cx}
cy={def.cy}
r={def.r}
>
{def.stops.map((stop, i) => (
<stop
key={i}
offset={stop.offset}
stopColor={getColor(ctx, stop.color)}
/>
))}
</radialGradient> </radialGradient>
); );
case "linearGradient":
return (
<linearGradient id={id} x1={def.x1} y1={def.y1} x2={def.x2} y2={def.y2}>
{stops}
</linearGradient>
);
} }
}; };

Wyświetl plik

@ -89,16 +89,18 @@ function getNonEmptyAttrib(el: cheerio.TagElement, attr: string): string {
return result; return result;
} }
function parseRadialGradient(el: cheerio.TagElement): SvgSymbolDef { function parseGradientStops(
els: cheerio.TagElement["children"]
): SvgSymbolGradientStop[] {
const stops: SvgSymbolGradientStop[] = []; const stops: SvgSymbolGradientStop[] = [];
for (let child of el.children) { for (let el of els) {
if (child.type === "tag") { if (el.type === "tag") {
if (child.tagName !== "stop") { if (el.tagName !== "stop") {
throw new Error( throw new Error(
`Expected an SVG gradient to only contain <stop> elements!` `Expected an SVG gradient to only contain <stop> elements!`
); );
} }
const style = getNonEmptyAttrib(child, "style"); const style = getNonEmptyAttrib(el, "style");
const color = style.match(/stop-color\:rgb\((\d+),(\d+),(\d+)\)/); const color = style.match(/stop-color\:rgb\((\d+),(\d+),(\d+)\)/);
if (!color) { if (!color) {
throw new Error(`Expected "${style}" to contain a stop-color!`); throw new Error(`Expected "${style}" to contain a stop-color!`);
@ -107,18 +109,34 @@ function parseRadialGradient(el: cheerio.TagElement): SvgSymbolDef {
.slice(1) .slice(1)
.map((value) => parseInt(value)); .map((value) => parseInt(value));
stops.push({ stops.push({
offset: getNonEmptyAttrib(child, "offset"), offset: getNonEmptyAttrib(el, "offset"),
color: clampedBytesToRGBColor(rgb), color: clampedBytesToRGBColor(rgb),
}); });
} }
} }
return stops;
}
function parseLinearGradient(el: cheerio.TagElement): SvgSymbolDef {
return {
type: "linearGradient",
id: getNonEmptyAttrib(el, "id"),
x1: getNonEmptyAttrib(el, "x1"),
y1: getNonEmptyAttrib(el, "y1"),
x2: getNonEmptyAttrib(el, "x2"),
y2: getNonEmptyAttrib(el, "y2"),
stops: parseGradientStops(el.children),
};
}
function parseRadialGradient(el: cheerio.TagElement): SvgSymbolDef {
return { return {
type: "radialGradient", type: "radialGradient",
id: getNonEmptyAttrib(el, "id"), id: getNonEmptyAttrib(el, "id"),
cx: getNonEmptyAttrib(el, "cx"), cx: getNonEmptyAttrib(el, "cx"),
cy: getNonEmptyAttrib(el, "cy"), cy: getNonEmptyAttrib(el, "cy"),
r: getNonEmptyAttrib(el, "r"), r: getNonEmptyAttrib(el, "r"),
stops, stops: parseGradientStops(el.children),
}; };
} }
@ -138,20 +156,22 @@ function serializeSvgSymbolElement(
if (tagName === "radialGradient") { if (tagName === "radialGradient") {
defsOutput.push(parseRadialGradient(el)); defsOutput.push(parseRadialGradient(el));
return null; return null;
} else if (tagName === "linearGradient") {
defsOutput.push(parseLinearGradient(el));
return null;
} else if (!isSupportedSvgTag(tagName)) {
throw new Error(`Unsupported SVG element: <${tagName}>`);
} }
let children = withoutNulls( let children = withoutNulls(
onlyTags(el.children).map((child) => onlyTags(el.children).map((child) =>
serializeSvgSymbolElement($, child, defsOutput) serializeSvgSymbolElement($, child, defsOutput)
) )
); );
if (isSupportedSvgTag(tagName)) { return {
return { tagName,
tagName, props: attribsToProps(el) as any,
props: attribsToProps(el) as any, children,
children, };
};
}
throw new Error(`Unsupported SVG element: <${tagName}>`);
} }
function removeEmptyGroups(s: SvgSymbolElement[]): SvgSymbolElement[] { function removeEmptyGroups(s: SvgSymbolElement[]): SvgSymbolElement[] {