Skip to content

Commit

Permalink
fix: Avoid using rbga format for SVG attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanokomiya committed Feb 1, 2024
1 parent a790eb3 commit 5011441
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/utils/color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
rednerHSVA,
rednerRGBA,
rgbaToHsva,
toHexAndAlpha,
} from "./color";

describe("parseHSLA", () => {
Expand Down Expand Up @@ -192,3 +193,12 @@ describe("hsvaToHsla", () => {
});
});
});

describe("toHexAndAlpha", () => {
test("should parse RGBA format", () => {
expect(toHexAndAlpha("rgba(1,2,3,0.5)")).toEqual(["#010203", 0.5]);
});
test("should parse HEX format", () => {
expect(toHexAndAlpha("#010203")).toEqual(["#010203", 1]);
});
});
8 changes: 8 additions & 0 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,11 @@ export function hexToColor(hex: string): Color {
}
: { r: 0, g: 0, b: 0, a: 1 };
}

/**
* Suppose "str" is either RGBA or HEX format.
*/
export function toHexAndAlpha(str?: string | null): [string, number] | undefined {
const rgba = str ? parseRGBA(str) ?? hexToColor(str) : undefined;
return rgba ? [colorToHex(rgba), rgba.a] : undefined;
}
7 changes: 7 additions & 0 deletions src/utils/svgElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ function appendChildren($el: SVGElement, $children: (SVGElement | Text)[]) {
export function isPlainText(elm: unknown): elm is string {
return typeof elm === "string";
}

export function getColorAttributes(
type: "fill" | "stroke",
val?: [hex: string, alpha: number],
): SVGAttributes | undefined {
return val ? { [type]: val[0], [`${type}-opacity`]: val[1] === 1 ? undefined : val[1] } : undefined;
}
11 changes: 6 additions & 5 deletions src/utils/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { DocAttrInfo, DocAttributes, DocDelta, DocDeltaInsert, DocOutput } from
import { Size } from "../models";
import { applyDefaultStrokeStyle } from "./strokeStyle";
import { newChronoCache } from "../composables/cache";
import { SVGElementInfo } from "./svgElements";
import { SVGElementInfo, getColorAttributes } from "./svgElements";
import { toHexAndAlpha } from "./color";

export const DEFAULT_FONT_SIZE = 18;
export const DEFAULT_LINEHEIGHT = 1.2;
Expand Down Expand Up @@ -232,7 +233,7 @@ export function renderSVGDocByComposition(
y: group.bounds.y,
width: group.bounds.width,
height: lineHeight,
fill: group.attributes.background,
...getColorAttributes("fill", toHexAndAlpha(group.attributes.background)),
},
});
});
Expand All @@ -248,7 +249,7 @@ export function renderSVGDocByComposition(
y1: y,
x2: group.bounds.x + group.bounds.width,
y2: y,
stroke: group.attributes?.color ?? "#000",
...getColorAttributes("stroke", toHexAndAlpha(group.attributes.color)),
"stroke-width": fontHeight * 0.07,
},
});
Expand All @@ -265,7 +266,7 @@ export function renderSVGDocByComposition(
y1: y,
x2: group.bounds.x + group.bounds.width,
y2: y,
stroke: group.attributes?.color ?? "#000",
...getColorAttributes("stroke", toHexAndAlpha(group.attributes.color)),
"stroke-width": fontHeight * 0.07,
},
});
Expand All @@ -278,7 +279,7 @@ export function renderSVGDocByComposition(
tag: "tspan",
attributes: {
x: lineComposition.bounds.x,
fill: op.attributes?.color ?? undefined,
...getColorAttributes("fill", toHexAndAlpha(op.attributes?.color)),
"font-size": op.attributes?.size ?? undefined,
"font-weight": op.attributes?.bold ? "bold" : undefined,
"font-style": op.attributes?.italic ? "italic" : undefined,
Expand Down

0 comments on commit 5011441

Please sign in to comment.