Merge version_1 into main #2
@@ -1,51 +1,74 @@
|
||||
"use client";
|
||||
|
||||
import { memo } from "react";
|
||||
import useSvgTextLogo from "./useSvgTextLogo";
|
||||
import { cls } from "@/lib/utils";
|
||||
import React from 'react';
|
||||
|
||||
interface SvgTextLogoProps {
|
||||
logoText: string;
|
||||
adjustHeightFactor?: number;
|
||||
verticalAlign?: "top" | "center";
|
||||
text: string;
|
||||
className?: string;
|
||||
fontSize?: number;
|
||||
fontWeight?: number | string;
|
||||
letterSpacing?: number;
|
||||
dominantBaseline?: 'auto' | 'middle' | 'hanging' | 'mathematical' | 'central';
|
||||
}
|
||||
|
||||
const SvgTextLogo = memo<SvgTextLogoProps>(function SvgTextLogo({
|
||||
logoText,
|
||||
adjustHeightFactor,
|
||||
verticalAlign = "top",
|
||||
className = "",
|
||||
}) {
|
||||
const { svgRef, textRef, viewBox, aspectRatio } = useSvgTextLogo(logoText, false, adjustHeightFactor);
|
||||
const SvgTextLogo: React.FC<SvgTextLogoProps> = ({
|
||||
text,
|
||||
className = '',
|
||||
fontSize = 64,
|
||||
fontWeight = 700,
|
||||
letterSpacing = 0,
|
||||
dominantBaseline = 'middle',
|
||||
}) => {
|
||||
const canvas = React.useRef<HTMLCanvasElement>(null);
|
||||
const [dimensions, setDimensions] = React.useState({ width: 0, height: 0 });
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!canvas.current) return;
|
||||
|
||||
const ctx = canvas.current.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
const fontString = `${fontWeight} ${fontSize}px -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif`;
|
||||
ctx.font = fontString;
|
||||
|
||||
const metrics = ctx.measureText(text);
|
||||
const width = Math.ceil(metrics.width + letterSpacing * text.length);
|
||||
const height = Math.ceil(fontSize * 1.2);
|
||||
|
||||
canvas.current.width = width;
|
||||
canvas.current.height = height;
|
||||
|
||||
ctx.font = fontString;
|
||||
ctx.fillStyle = 'black';
|
||||
ctx.textAlign = 'left';
|
||||
ctx.textBaseline = dominantBaseline === 'middle' ? 'middle' : 'top';
|
||||
|
||||
let x = 0;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
ctx.fillText(text[i], x, height / 2);
|
||||
x += ctx.measureText(text[i]).width + letterSpacing;
|
||||
}
|
||||
|
||||
setDimensions({ width, height });
|
||||
}, [text, fontSize, fontWeight, letterSpacing, dominantBaseline]);
|
||||
|
||||
return (
|
||||
<svg
|
||||
ref={svgRef}
|
||||
viewBox={viewBox}
|
||||
className={cls("w-full", className)}
|
||||
style={{ aspectRatio: aspectRatio }}
|
||||
preserveAspectRatio="none"
|
||||
role="img"
|
||||
aria-label={`${logoText} logo`}
|
||||
viewBox={`0 0 ${dimensions.width} ${dimensions.height}`}
|
||||
className={className}
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
preserveAspectRatio="xMidYMid meet"
|
||||
>
|
||||
<text
|
||||
ref={textRef}
|
||||
x="0"
|
||||
y={verticalAlign === "center" ? "50%" : "0"}
|
||||
className="font-bold fill-current"
|
||||
style={{
|
||||
fontSize: "20px",
|
||||
letterSpacing: "-0.02em",
|
||||
dominantBaseline: verticalAlign === "center" ? "middle" : "text-before-edge"
|
||||
}}
|
||||
>
|
||||
{logoText}
|
||||
</text>
|
||||
<foreignObject width={dimensions.width} height={dimensions.height}>
|
||||
<canvas
|
||||
ref={canvas}
|
||||
style={{
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
display: 'block',
|
||||
}}
|
||||
/>
|
||||
</foreignObject>
|
||||
</svg>
|
||||
);
|
||||
});
|
||||
|
||||
SvgTextLogo.displayName = "SvgTextLogo";
|
||||
};
|
||||
|
||||
export default SvgTextLogo;
|
||||
|
||||
Reference in New Issue
Block a user