fix: double side printing

This commit is contained in:
hypercross 2026-03-13 17:42:51 +08:00
parent 4b08a738a7
commit 984b8aa1c8
1 changed files with 51 additions and 41 deletions

View File

@ -55,13 +55,13 @@ export function usePageLayout(store: DeckStore): UsePageLayoutReturn {
const result: PageData[] = []; const result: PageData[] = [];
if (doubleSided()) { if (doubleSided()) {
// 双面打印模式:每张卡牌需要 2 页(正面 + 背面) // 双面打印模式:每页多张卡牌,正面和背面分别在相邻的两页
// 背面卡牌顺序在长边方向上逆转
const totalCards = cards.length; const totalCards = cards.length;
const totalPages = Math.ceil(totalCards / cardsPerPage);
for (let i = 0; i < totalCards; i++) { for (let pageIndex = 0; pageIndex < totalPages; pageIndex++) {
const frontPageIndex = i * 2; const frontPageIndex = pageIndex * 2;
const backPageIndex = i * 2 + 1; const backPageIndex = pageIndex * 2 + 1;
// 确保页面数组有足够长度 // 确保页面数组有足够长度
while (result.length <= backPageIndex) { while (result.length <= backPageIndex) {
@ -76,11 +76,21 @@ export function usePageLayout(store: DeckStore): UsePageLayoutReturn {
const frontPage = result[frontPageIndex]; const frontPage = result[frontPageIndex];
const backPage = result[backPageIndex]; const backPage = result[backPageIndex];
// 计算当前正面页的卡牌范围
const startCardIndex = pageIndex * cardsPerPage;
const endCardIndex = Math.min(startCardIndex + cardsPerPage, totalCards);
for (let i = startCardIndex; i < endCardIndex; i++) {
// 正面:正常顺序排列 // 正面:正常顺序排列
const frontRow = Math.floor(i / cardsPerRow); const indexInPage = i - startCardIndex;
const frontCol = i % cardsPerRow; const row = Math.floor(indexInPage / cardsPerRow);
const frontX = baseOffsetX + frontCol * cardWidth + frontOddPageOffsetX(); const col = indexInPage % cardsPerRow;
const frontY = baseOffsetY + frontRow * cardHeight + frontOddPageOffsetY(); // 双面打印时,所有正面页都在奇数物理页上,所以都应用偏移
const pageOffsetX = frontOddPageOffsetX();
const pageOffsetY = frontOddPageOffsetY();
const frontX = baseOffsetX + col * cardWidth + pageOffsetX;
const frontY = baseOffsetY + row * cardHeight + pageOffsetY;
frontPage.cards.push({ data: cards[i], x: frontX, y: frontY, side: 'front' as const }); frontPage.cards.push({ data: cards[i], x: frontX, y: frontY, side: 'front' as const });
frontPage.bounds.minX = Math.min(frontPage.bounds.minX, frontX); frontPage.bounds.minX = Math.min(frontPage.bounds.minX, frontX);
@ -91,13 +101,12 @@ export function usePageLayout(store: DeckStore): UsePageLayoutReturn {
// 背面:逆转顺序排列(长边方向) // 背面:逆转顺序排列(长边方向)
// 对于竖向打印,长边是垂直方向,所以逆转行 // 对于竖向打印,长边是垂直方向,所以逆转行
// 对于横向打印,长边是水平方向,所以逆转列 // 对于横向打印,长边是水平方向,所以逆转列
const backIndex = totalCards - 1 - i;
const backRow = orientation() === 'portrait' const backRow = orientation() === 'portrait'
? Math.floor(backIndex / cardsPerRow) ? (rowsPerPage - 1 - row)
: Math.floor(i / cardsPerRow); : row;
const backCol = orientation() === 'portrait' const backCol = orientation() === 'portrait'
? backIndex % cardsPerRow ? col
: (cardsPerRow - 1 - (i % cardsPerRow)); : (cardsPerRow - 1 - col);
const backX = baseOffsetX + backCol * cardWidth; const backX = baseOffsetX + backCol * cardWidth;
const backY = baseOffsetY + backRow * cardHeight; const backY = baseOffsetY + backRow * cardHeight;
@ -108,6 +117,7 @@ export function usePageLayout(store: DeckStore): UsePageLayoutReturn {
backPage.bounds.maxX = Math.max(backPage.bounds.maxX, backX + cardWidth); backPage.bounds.maxX = Math.max(backPage.bounds.maxX, backX + cardWidth);
backPage.bounds.maxY = Math.max(backPage.bounds.maxY, backY + cardHeight); backPage.bounds.maxY = Math.max(backPage.bounds.maxY, backY + cardHeight);
} }
}
} else { } else {
// 单面打印模式:原有逻辑 // 单面打印模式:原有逻辑
let currentPage: PageData = { let currentPage: PageData = {