fix: preserve psd text padding alignment
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-03-31 20:25:07 +08:00
parent 43083ff90c
commit d1ee239fd6
3 changed files with 36 additions and 6 deletions

View File

@ -589,6 +589,17 @@ export async function preparePsdTemplateRenderAssets(params: {
};
}
if (
currentLayer.paddingX <= 0 &&
Number.isFinite(Number(analysis.suggestedPaddingX)) &&
Number(analysis.suggestedPaddingX) > 0
) {
nextLayer = {
...nextLayer,
paddingX: Math.max(0, Number(analysis.suggestedPaddingX)),
};
}
if (nextLayer !== currentLayer) {
nextLayers[layerIndex] = nextLayer;
}

View File

@ -131,6 +131,17 @@ export async function resolveAgentPsdTemplateTextStyles(template: AgentPsdTempla
textAlign: analysis.inferredTextAlign,
};
}
if (
Number(nextLayer.paddingX || 0) <= 0 &&
Number.isFinite(Number(analysis?.suggestedPaddingX)) &&
Number(analysis?.suggestedPaddingX) > 0
) {
nextLayer = {
...nextLayer,
paddingX: Math.max(0, Number(analysis?.suggestedPaddingX)),
};
}
}
if (nextLayer !== layer) {

View File

@ -199,20 +199,28 @@ export function analyzeTextLayerPixels(params: {
);
const leftGap = minX;
const rightGap = Math.max(0, width - maxX - 1);
const tolerance = Math.max(4, width * 0.08);
const visibleWidth = maxX - minX + 1;
const centerTolerance = Math.max(4, width * 0.06);
const edgeTolerance = Math.max(6, width * 0.08);
const contentCenterX = (minX + maxX) / 2;
const stageCenterX = (width - 1) / 2;
const inferredTextAlign: AgentPsdTextAlign =
visibleWidth >= width * 0.92
? "left"
: Math.abs(leftGap - rightGap) <= tolerance
Math.abs(contentCenterX - stageCenterX) <= centerTolerance &&
Math.abs(leftGap - rightGap) <= edgeTolerance
? "center"
: leftGap > rightGap
: leftGap > rightGap + edgeTolerance
? "right"
: "left";
const suggestedPaddingX =
inferredTextAlign === "right"
? Math.max(0, Math.round(rightGap))
: inferredTextAlign === "center"
? Math.max(0, Math.round(Math.min(leftGap, rightGap)))
: Math.max(0, Math.round(leftGap));
return {
dominantColor,
inferredTextAlign,
suggestedPaddingX,
visibleBounds: {
minX,
minY,