Commit 5feb8774 authored by 张九刚's avatar 张九刚

修复字体大小问题

parent 85064870
{
"name": "psd-parse-web",
"version": "1.0.4",
"version": "2.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......
......@@ -34,9 +34,7 @@ async function compilePsdToJson(psdFile, options) {
if (!assetsPath) {
console.log("没有指定assets目录");
}
const tree = await getTree(psdFile);
// console.log("tree:", tree);
let viewRoot = {
name: path.basename(psdFile, '.psd'),
......@@ -44,12 +42,17 @@ async function compilePsdToJson(psdFile, options) {
uuid: generateUUID(),
children: []
};
const assets = [];
const pageList = tree.children || [];
// console.log("pageList:", pageList);
const isHadErrorPage = pageList.filter(page => !page.children);
if (isHadErrorPage.length) {
console.warn(`${isHadErrorPage[0].name}不合法,请保持设计稿根目录为全部文件夹形式`);
return {
error: true,
errorName: isHadErrorPage[0].name
}
}
const pr = pageList.map(async (page, index) => {
const folderName = page.name || `未命名页面${Math.random().toFixed(2) * 100}`;
if (!page.children) {
......@@ -97,9 +100,8 @@ async function compilePsdToJson(psdFile, options) {
if (typeTool) {
let fontInfo = typeTool();
properties.attrs.text = fontInfo.textValue;
const sizes = fontInfo.sizes();
const colors = fontInfo.colors();
let fsize = sizes ? sizes[0] || 20 : 20;
let fsize = computeFontSize(fontInfo)
properties.style.fontSize = parseInt(fsize);//字体取整
let [r, g, b, a] = colors[0];
properties.style.color = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
......@@ -172,7 +174,7 @@ async function compilePsdToJson(psdFile, options) {
if (!viewParent.hasOwnProperty('children')) {
viewParent.children = [];
}
console.log("viewNode:", viewNode);
// console.log("viewNode:", viewNode);
viewParent.children.push(viewNode);
......@@ -196,7 +198,7 @@ async function compilePsdToJson(psdFile, options) {
view: { children: viewRoot.children },
};
console.log("data...", JSON.stringify(data));
// console.log("data...", JSON.stringify(data));
return data;
}
......@@ -225,6 +227,21 @@ function savePng(png, output) {
});
});
}
const computeFontSize = (fontinfo, defValue = 24) => {
const sizes = fontinfo.sizes();
let size;
if (sizes && sizes[0]) {
if (fontinfo.transform.yy !== 1) {
size = Math.round((sizes[0] * fontinfo.transform.yy) * 100) * 0.01;
} else { // transform.yy为1时,sizes[0]的值就是字体显示大小的值,不需要计算
size = sizes[0].toFixed();
}
} else {
size = defValue; // 默认
}
return size;
}
......
......@@ -10,6 +10,7 @@ async function getTree(psdFilePath) {
const psd = await PSD.open(psdFilePath);
const root = {};
console.log('psd::::',JSON.stringify(psd.tree().export()));
walk(psd.tree(), root);
return root;
......
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