Comparing version 1.0.3 to 1.0.4
@@ -21,3 +21,3 @@ import huozi from '../index'; | ||
const layoutSequence = huozi(textSequence, { | ||
fontSize: +options.fontSize || 18, | ||
gridSize: +options.fontSize || 18, | ||
column: +options.column || 35, | ||
@@ -24,0 +24,0 @@ inlineCompression: options.compress === undefined ? options.compress : true, |
76
index.js
@@ -33,3 +33,3 @@ /*! | ||
fontFamily: 'sans-serif', | ||
fontSize: 26, | ||
gridSize: 26, | ||
column: 25, | ||
@@ -44,6 +44,6 @@ row: Infinity, | ||
forceSpaceBetweenCJKAndWestern: false, | ||
fixLeftQuote: undefined | ||
fixLeftQuote: true | ||
}; | ||
export default function huozi(textSequence, layoutOptions) { | ||
export default function huozi(textSequence, layoutOptions, onSequence) { | ||
@@ -54,3 +54,3 @@ layoutOptions = Object.assign({}, defaultOptions, layoutOptions); | ||
fontFamily, | ||
fontSize, | ||
gridSize, | ||
column, | ||
@@ -78,3 +78,3 @@ row, | ||
// 存储一行字中的最大字号,用以确定真实行高 | ||
let maxFontSize = fontSize; | ||
let maxFontSize = gridSize; | ||
@@ -92,3 +92,3 @@ // 缓存英文字符直到遇到下一个中文字符为止 | ||
currentY += maxFontSize + yInterval; | ||
maxFontSize = fontSize; | ||
maxFontSize = gridSize; | ||
lastIsPunctuation = false; | ||
@@ -124,3 +124,3 @@ lastCharFontSize = 0; | ||
} else if (westernTextCache.length) { | ||
const forceSpace = forceSpaceBetweenCJKAndWestern ? 0.25 * fontSize : 0; | ||
const forceSpace = forceSpaceBetweenCJKAndWestern ? 0.25 * gridSize : 0; | ||
@@ -132,6 +132,6 @@ if (currentX) { | ||
let westernLayoutSequence, isMultiLine, currentX_tmp; | ||
[westernLayoutSequence, currentX_tmp, currentY, currentRow, isMultiLine] = processWesternText(westernTextCache, layoutOptions, currentX, currentY, currentRow, column * fontSize, row); | ||
[westernLayoutSequence, currentX_tmp, currentY, currentRow, isMultiLine] = processWesternText(westernTextCache, layoutOptions, currentX, currentY, currentRow, column * gridSize, row); | ||
currentColumn = Math.ceil(currentX_tmp / (fontSize + xInterval)); | ||
currentX = currentColumn * (fontSize + xInterval); | ||
currentColumn = Math.ceil(currentX_tmp / (gridSize + xInterval)); | ||
currentX = currentColumn * (gridSize + xInterval); | ||
@@ -143,3 +143,3 @@ // 单行的情况下自动对齐两端空格 | ||
currentColumn += 1; | ||
currentX = currentColumn * fontSize; | ||
currentX = currentColumn * gridSize; | ||
} | ||
@@ -151,3 +151,3 @@ | ||
value.x += offsetX; | ||
return value; | ||
return onSequence ? (onSequence(value) || value) : value; | ||
}); | ||
@@ -179,10 +179,10 @@ } | ||
let offsetX = 0 | ||
let offsetY = (charFontSize - fontSize) / 2; | ||
let offsetY = (charFontSize - gridSize) / 2; | ||
let doubleX = false; | ||
if (forceGridAlignment && charFontSize !== fontSize) { | ||
offsetX = +forceGridAlignment * (charFontSize - fontSize) / 2; | ||
currentColumn += offsetX > 0 ? Math.ceil(offsetX * 2 / (fontSize + xInterval)) : 0; | ||
if (forceGridAlignment && charFontSize !== gridSize) { | ||
offsetX = +forceGridAlignment * (charFontSize - gridSize) / 2; | ||
currentColumn += offsetX > 0 ? Math.ceil(offsetX * 2 / (gridSize + xInterval)) : 0; | ||
offsetX = ((1 + Math.ceil(offsetX * 2 / (fontSize + xInterval))) * (fontSize + xInterval) - charFontSize) / 2; | ||
offsetX = ((1 + Math.ceil(offsetX * 2 / (gridSize + xInterval))) * (gridSize + xInterval) - charFontSize) / 2; | ||
currentX += offsetX; | ||
@@ -198,16 +198,14 @@ | ||
let quoteFix = 0; | ||
if (fixLeftQuote === undefined || fixLeftQuote) { | ||
if (fixLeftQuote) { | ||
quoteFix += ((!lastIsPunctuation && character === '“') ? charFontSize / 2 : 0); | ||
// 一些平台上引号量取结果是<0.5em宽,但绘制时却是1em宽,导致错位。下面的代码修正这一问题 | ||
// OS X 无需此修复(FLAG_STDWIDTH === true) | ||
if (character === '“' && !FLAG_STDWIDTH) { | ||
quoteFix += -charFontSize / 2; | ||
} else if (character === '“' && width === charFontSize) { | ||
quoteFix += -charFontSize / 2; | ||
} | ||
} | ||
// 一些平台上引号量取结果是<0.5em宽,但绘制时却是1em宽,导致错位。下面的代码修正这一问题 | ||
// OS X 无需此修复(FLAG_STDWIDTH === true) | ||
if (character === '“' && !FLAG_STDWIDTH) { | ||
quoteFix += -charFontSize / 2; | ||
} else if (character === '“' && width === charFontSize) { | ||
quoteFix += -charFontSize / 2; | ||
} | ||
// 确定文字位置并添加到返回数组中 | ||
layoutSequence.push({ | ||
const item = { | ||
...char, | ||
@@ -218,8 +216,11 @@ x: currentX + quoteFix, | ||
height: charFontSize | ||
}); | ||
}; | ||
// 确定文字位置并添加到返回数组中 | ||
layoutSequence.push(onSequence ? (onSequence(item) || item) : item); | ||
// 当正好在行首时,因两边空格全部加到一边,可能使得总宽度大于1em,此时应该删去多出的1em宽度 | ||
let offsetX2 = offsetX * (doubleX ? 2 : 1); | ||
if (offsetX2 > fontSize) { | ||
offsetX2 -= fontSize; | ||
if (offsetX2 > gridSize) { | ||
offsetX2 -= gridSize; | ||
currentColumn -= 1; | ||
@@ -259,2 +260,5 @@ } | ||
// 移除表意文字空格 | ||
layoutSequence.pop(); | ||
return layoutSequence; | ||
@@ -264,5 +268,5 @@ } | ||
// 独立处理西文文本的排版,返回的数据中文本两侧无空格 | ||
function processWesternText(textSequence, { fontSize, yInterval, letterSpacing }, currentX, currentY, currentRow, maxWidth, row) { | ||
function processWesternText(textSequence, { fontFamily, gridSize, yInterval, letterSpacing }, currentX, currentY, currentRow, maxWidth, row) { | ||
const layoutSequence = []; | ||
let maxFontSize = fontSize; | ||
let maxFontSize = gridSize; | ||
let word = ''; | ||
@@ -306,3 +310,3 @@ let wordChar = []; | ||
// 确定文字位置并添加到返回数组中 | ||
const offsetY = (charFontSize - fontSize) / 2; | ||
const offsetY = (charFontSize - gridSize) / 2; | ||
layoutSequence.push({ | ||
@@ -317,3 +321,3 @@ ...char, | ||
currentX += 0.35 * fontSize; | ||
currentX += 0.35 * gridSize; | ||
@@ -335,3 +339,3 @@ word = ''; | ||
return [layoutSequence, currentX - 0.35 * fontSize, currentY, currentRow, isMultiLine]; | ||
return [layoutSequence, currentX - 0.35 * gridSize, currentY, currentRow, isMultiLine]; | ||
} |
{ | ||
"name": "huozi", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A simple typography engine for CJK languages, especially designed for game rich-text.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -98,4 +98,4 @@ <h1 align="center" style=" font-family: STSong, SimSun, serif; border: none; font-size: 48px; margin-bottom: 0;"> | ||
fontFamily: 'sans-serif', | ||
// 排版用字号(即一个em多宽,与 textSequence 中的 fontSize 不同) | ||
fontSize: 26, | ||
// 排版网格宽度(即一个em多宽,与 textSequence 中的 fontSize 不同) | ||
gridSize: 26, | ||
// 每行字数 | ||
@@ -119,4 +119,4 @@ column: 25, | ||
forceSpaceBetweenCJKAndWestern: false, | ||
// 强制指定是否进行左全角引号的位置修正,默认为自动探测(依系统、浏览器和字体,存在失误可能) | ||
fixLeftQuote: undefined | ||
// 是否进行左全角引号的位置修正 | ||
fixLeftQuote: true | ||
}) | ||
@@ -123,0 +123,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
190833
450