This commit is contained in:
@@ -4,8 +4,11 @@ import Konva from "konva";
|
||||
import type { TextShapeProps } from "./types";
|
||||
import { getFontFamily } from "@/pages/editor/utils/fontFamily";
|
||||
import {
|
||||
buildCanvasFont,
|
||||
buildKonvaCanvasFont,
|
||||
DEFAULT_TEXT_MAX_WIDTH_PX,
|
||||
getEffectiveTextWidth,
|
||||
getKonvaFontStyle,
|
||||
measureTextBlock,
|
||||
resolveTextMaxWidth,
|
||||
usesWrappedLayout,
|
||||
@@ -30,15 +33,6 @@ const getFontWeight = (fontWeight?: string): string => {
|
||||
return weightMap[fontWeight] || "normal";
|
||||
};
|
||||
|
||||
/** Konva.Text فقط `fontStyle` دارد (نه fontWeight جداگانه)؛ مقدار باید normal|bold|italic باشد. */
|
||||
const getKonvaFontStyle = (fontWeight?: string): string => {
|
||||
const w = getFontWeight(fontWeight);
|
||||
if (w === "bold" || w === "bolder") return "bold";
|
||||
const n = parseInt(w, 10);
|
||||
if (!Number.isNaN(n) && n >= 600) return "bold";
|
||||
return "normal";
|
||||
};
|
||||
|
||||
const TextShape = ({
|
||||
obj,
|
||||
onSelect,
|
||||
@@ -172,11 +166,9 @@ const TextShape = ({
|
||||
}
|
||||
|
||||
const specs = [
|
||||
`${fontSize}px ${resolvedFamily}`,
|
||||
buildCanvasFont(fontSize, obj.fontFamily, obj.fontWeight),
|
||||
buildKonvaCanvasFont(fontSize, obj.fontFamily, obj.fontWeight),
|
||||
`${konvaStyle} normal ${fontSize}px ${resolvedFamily}`,
|
||||
`bold ${fontSize}px ${resolvedFamily}`,
|
||||
`300 ${fontSize}px ${resolvedFamily}`,
|
||||
`200 ${fontSize}px ${resolvedFamily}`,
|
||||
];
|
||||
|
||||
void Promise.all(
|
||||
|
||||
@@ -11,12 +11,18 @@ export const getCssFontWeight = (fontWeight?: string): number => {
|
||||
return 400;
|
||||
};
|
||||
|
||||
/** Konva.Text uses `fontStyle: bold` (canvas keyword), not numeric weight. */
|
||||
export const usesKonvaBoldStyle = (fontWeight?: string): boolean => {
|
||||
if (!fontWeight || fontWeight === "normal") return false;
|
||||
if (fontWeight === "bold" || fontWeight === "bolder") return true;
|
||||
/**
|
||||
* Konva.Text `fontStyle` — supports `normal`, `bold`, or numeric strings like `200`.
|
||||
* @see https://konvajs.org/api/Konva.Text.html#fontStyle
|
||||
*/
|
||||
export const getKonvaFontStyle = (fontWeight?: string): string => {
|
||||
if (!fontWeight || fontWeight === "normal") return "normal";
|
||||
if (fontWeight === "bold" || fontWeight === "bolder") return "bold";
|
||||
|
||||
const n = parseInt(fontWeight, 10);
|
||||
return !Number.isNaN(n) && n >= 600;
|
||||
if (!Number.isNaN(n)) return n.toString();
|
||||
|
||||
return "normal";
|
||||
};
|
||||
|
||||
/** Canvas/CSS `font` string shared by editor measurement and viewer layout. */
|
||||
@@ -30,15 +36,15 @@ export const buildCanvasFont = (
|
||||
return `${weight} ${fontSize}px ${family}`;
|
||||
};
|
||||
|
||||
/** Canvas font string that matches Konva.Text (`fontStyle: bold`). */
|
||||
/** Canvas font string that matches Konva.Text `_getContextFont()`. */
|
||||
export const buildKonvaCanvasFont = (
|
||||
fontSize: number,
|
||||
fontFamily?: string,
|
||||
fontWeight?: string,
|
||||
): string => {
|
||||
const family = getFontFamily(fontFamily);
|
||||
const style = usesKonvaBoldStyle(fontWeight) ? "bold" : "normal";
|
||||
return `${style} ${fontSize}px ${family}`;
|
||||
const style = getKonvaFontStyle(fontWeight);
|
||||
return `${style} normal ${fontSize}px ${family}`;
|
||||
};
|
||||
|
||||
const LINE_BREAK_RE = /[\r\n\u2028\u2029]/;
|
||||
|
||||
Reference in New Issue
Block a user