From 677c62e6eb422ebc02bda310f3ea19918e2a42d6 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sat, 11 Jul 2026 09:39:03 +0330 Subject: [PATCH] fix extra light weight --- .../editor/components/tools/TextShape.tsx | 18 +++++---------- src/pages/editor/utils/textStyle.ts | 22 ++++++++++++------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/pages/editor/components/tools/TextShape.tsx b/src/pages/editor/components/tools/TextShape.tsx index 1c00358..577b51e 100644 --- a/src/pages/editor/components/tools/TextShape.tsx +++ b/src/pages/editor/components/tools/TextShape.tsx @@ -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( diff --git a/src/pages/editor/utils/textStyle.ts b/src/pages/editor/utils/textStyle.ts index 93462d7..a308646 100644 --- a/src/pages/editor/utils/textStyle.ts +++ b/src/pages/editor/utils/textStyle.ts @@ -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]/;