UNPKG

sketch-json-parse

Version:

sketch-json解析

56 lines (55 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const dealParagraph_1 = require("./dealParagraph"); const dealColor_1 = require("./dealColor"); const dealFont_1 = require("./dealFont"); const dealLetterSpacing_1 = require("./dealLetterSpacing"); const dealTextDecorate_1 = require("./dealTextDecorate"); // :todo 结合 parseTypeFace exports.default = (layer) => { // 直接解析text字体样式有字体大小的情况下 if (layer.attributedString && layer.attributedString.string && layer.attributedString.attributes) { let size = 1; // TODO // if (layer.frame.sizeY) { // size = layer.frame.sizeY; // } let attrList = layer.attributedString.attributes; let strStyleList = []; let minFontSize = 0; for (let attrItem of attrList) { if (attrItem._class == 'stringAttribute') { let attributes = attrItem.attributes; let fontStyle = attributes['MSAttributedStringFontAttribute']; let colorStyle = attributes['MSAttributedStringColorAttribute'] || { red: 0, green: 0, blue: 0, alpha: 1, _class: 'color' }; let paragraphStyleObj = attributes['paragraphStyle']; let kerning = attributes['kerning']; const textStyle = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (0, dealParagraph_1.default)(paragraphStyleObj, fontStyle, size)), (0, dealColor_1.default)(colorStyle, layer)), (0, dealFont_1.default)(fontStyle, size)), (0, dealLetterSpacing_1.default)(kerning)), (0, dealTextDecorate_1.default)(attributes)); if (minFontSize > textStyle.fontSize) { minFontSize = textStyle.fontSize; } } } let isTextBreak = false; if (minFontSize && minFontSize > 0.5 * layer.frame.height) { // 单行文字的情况 isTextBreak = true; // TODO // layer.istransformContain = true; } // attrList = isTextBreak ? attrList : attrList.slice(0, 1); for (let attrItem of attrList) { if (attrItem._class == 'stringAttribute') { let attributes = attrItem.attributes; let fontStyle = attributes['MSAttributedStringFontAttribute']; let colorStyle = attributes['MSAttributedStringColorAttribute'] || { red: 0, green: 0, blue: 0, alpha: 1, _class: 'color' }; let paragraphStyleObj = attributes['paragraphStyle']; let kerning = attributes['kerning']; const style = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ pos: [attrItem.location, attrItem.length] }, (0, dealParagraph_1.default)(paragraphStyleObj, fontStyle, size)), (0, dealColor_1.default)(colorStyle, layer)), (0, dealFont_1.default)(fontStyle, size)), (0, dealLetterSpacing_1.default)(kerning)), (0, dealTextDecorate_1.default)(attributes)); strStyleList.push(style); } } return strStyleList; } return []; };