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

修复字体大小问题

parent 85064870
{ {
"name": "psd-parse-web", "name": "psd-parse-web",
"version": "1.0.4", "version": "2.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
......
...@@ -34,9 +34,7 @@ async function compilePsdToJson(psdFile, options) { ...@@ -34,9 +34,7 @@ async function compilePsdToJson(psdFile, options) {
if (!assetsPath) { if (!assetsPath) {
console.log("没有指定assets目录"); console.log("没有指定assets目录");
} }
const tree = await getTree(psdFile); const tree = await getTree(psdFile);
// console.log("tree:", tree);
let viewRoot = { let viewRoot = {
name: path.basename(psdFile, '.psd'), name: path.basename(psdFile, '.psd'),
...@@ -44,12 +42,17 @@ async function compilePsdToJson(psdFile, options) { ...@@ -44,12 +42,17 @@ async function compilePsdToJson(psdFile, options) {
uuid: generateUUID(), uuid: generateUUID(),
children: [] children: []
}; };
const assets = []; const assets = [];
const pageList = tree.children || []; 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 pr = pageList.map(async (page, index) => {
const folderName = page.name || `未命名页面${Math.random().toFixed(2) * 100}`; const folderName = page.name || `未命名页面${Math.random().toFixed(2) * 100}`;
if (!page.children) { if (!page.children) {
...@@ -97,9 +100,8 @@ async function compilePsdToJson(psdFile, options) { ...@@ -97,9 +100,8 @@ async function compilePsdToJson(psdFile, options) {
if (typeTool) { if (typeTool) {
let fontInfo = typeTool(); let fontInfo = typeTool();
properties.attrs.text = fontInfo.textValue; properties.attrs.text = fontInfo.textValue;
const sizes = fontInfo.sizes();
const colors = fontInfo.colors(); const colors = fontInfo.colors();
let fsize = sizes ? sizes[0] || 20 : 20; let fsize = computeFontSize(fontInfo)
properties.style.fontSize = parseInt(fsize);//字体取整 properties.style.fontSize = parseInt(fsize);//字体取整
let [r, g, b, a] = colors[0]; let [r, g, b, a] = colors[0];
properties.style.color = `rgba(${r}, ${g}, ${b}, ${a / 255})`; properties.style.color = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
...@@ -172,7 +174,7 @@ async function compilePsdToJson(psdFile, options) { ...@@ -172,7 +174,7 @@ async function compilePsdToJson(psdFile, options) {
if (!viewParent.hasOwnProperty('children')) { if (!viewParent.hasOwnProperty('children')) {
viewParent.children = []; viewParent.children = [];
} }
console.log("viewNode:", viewNode); // console.log("viewNode:", viewNode);
viewParent.children.push(viewNode); viewParent.children.push(viewNode);
...@@ -196,7 +198,7 @@ async function compilePsdToJson(psdFile, options) { ...@@ -196,7 +198,7 @@ async function compilePsdToJson(psdFile, options) {
view: { children: viewRoot.children }, view: { children: viewRoot.children },
}; };
console.log("data...", JSON.stringify(data)); // console.log("data...", JSON.stringify(data));
return data; return data;
} }
...@@ -225,6 +227,21 @@ function savePng(png, output) { ...@@ -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) { ...@@ -10,6 +10,7 @@ async function getTree(psdFilePath) {
const psd = await PSD.open(psdFilePath); const psd = await PSD.open(psdFilePath);
const root = {}; const root = {};
console.log('psd::::',JSON.stringify(psd.tree().export()));
walk(psd.tree(), root); walk(psd.tree(), root);
return 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