Commit 82258d3f authored by AI Bot's avatar AI Bot

时间线正文两端对齐:非末行把剩余空隙分到字间,年内右缘统一

连续 ASCII 字符(股票代码/英文单词)保持整体不拆,CJK 逐字参与分配;
剩余超过列宽 45% 的病态短行退回左对齐,避免字距稀烂。
Co-Authored-By: 's avatarClaude Code <noreply@anthropic.com>
parent bcbd9e25
......@@ -226,6 +226,31 @@ function wrapText(ctx, text, maxWidth) {
return lines
}
// 一行文字按两端对齐绘制:把超出列宽的剩余空隙均匀分到字间。
// 连续 ASCII 可见字符(股票代码、英文单词、数字)保持整体不拆,
// CJK/全角逐字参与分配;空隙过大或行超宽时退回普通左对齐。
function drawJustified(ctx, line, x, y, targetW) {
var tokens = line.match(/[\x21-\x7E]+|./g) || [line]
var natural = 0
var widths = []
for (var t = 0; t < tokens.length; t++) {
widths[t] = ctx.measureText(tokens[t]).width
natural += widths[t]
}
var extra = targetW - natural
// 已满行/超行直接画;剩余超过列宽 45%(断行遇上超长单词的病态短行)不对齐,避免字距稀烂
if (extra <= 0.5 || extra > targetW * 0.45) {
ctx.fillText(line, x, y)
return
}
var gap = extra / (tokens.length - 1)
var cx = x
for (var t2 = 0; t2 < tokens.length; t2++) {
ctx.fillText(tokens[t2], cx, y)
cx += widths[t2] + gap
}
}
// 把时间线绘制到第 i 个切片的 canvas 上(逐切片绘制,避免 iOS 大画布尺寸限制)
var wrapTextCache = {}
function drawTimelineStrip(ctx, layout, data, sliceIndex, spriteW) {
......@@ -319,7 +344,13 @@ function drawTimelineStrip(ctx, layout, data, sliceIndex, spriteW) {
lines = wrapTextCache[cacheKey] = wrapText(ctx, text, wrap)
}
for (var li = 0; li < lines.length; li++) {
ctx.fillText(lines[li], localX(textX), cursorY + li * layout.linePitch)
var lineY = cursorY + li * layout.linePitch
// 两端对齐:除末行外把剩余空隙分到字间,年内右缘对齐
if (li === lines.length - 1) {
ctx.fillText(lines[li], localX(textX), lineY)
} else {
drawJustified(ctx, lines[li], localX(textX), lineY, wrap)
}
}
cursorY += lines.length * layout.linePitch + layout.entryExtra
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment