@yft-design/psd-lib 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -0
- package/dist/abr.d.ts +143 -0
- package/dist/abr.js +300 -0
- package/dist/additionalInfo.d.ts +26 -0
- package/dist/additionalInfo.js +3891 -0
- package/dist/csh.d.ts +10 -0
- package/dist/csh.js +32 -0
- package/dist/descriptor.d.ts +566 -0
- package/dist/descriptor.js +1958 -0
- package/dist/effectsHelpers.d.ts +5 -0
- package/dist/effectsHelpers.js +280 -0
- package/dist/engineData.d.ts +2 -0
- package/dist/engineData.js +330 -0
- package/dist/engineData2.d.ts +2 -0
- package/dist/engineData2.js +405 -0
- package/dist/helpers.d.ts +89 -0
- package/dist/helpers.js +300 -0
- package/dist/imageResources.d.ts +17 -0
- package/dist/imageResources.js +1070 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +30 -0
- package/dist/initializeCanvas.d.ts +1 -0
- package/dist/initializeCanvas.js +21 -0
- package/dist/jpeg.d.ts +1 -0
- package/dist/jpeg.js +1017 -0
- package/dist/psd.d.ts +1822 -0
- package/dist/psd.js +7 -0
- package/dist/psdReader.d.ts +46 -0
- package/dist/psdReader.js +1188 -0
- package/dist/psdWriter.d.ts +33 -0
- package/dist/psdWriter.js +733 -0
- package/dist/text.d.ts +179 -0
- package/dist/text.js +556 -0
- package/dist/utf8.d.ts +4 -0
- package/dist/utf8.js +150 -0
- package/package.json +46 -0
package/dist/text.js
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
const defaultFont = {
|
|
2
|
+
name: 'MyriadPro-Regular',
|
|
3
|
+
script: 0,
|
|
4
|
+
type: 0,
|
|
5
|
+
synthetic: 0,
|
|
6
|
+
};
|
|
7
|
+
const defaultParagraphStyle = {
|
|
8
|
+
justification: 'left',
|
|
9
|
+
firstLineIndent: 0,
|
|
10
|
+
startIndent: 0,
|
|
11
|
+
endIndent: 0,
|
|
12
|
+
spaceBefore: 0,
|
|
13
|
+
spaceAfter: 0,
|
|
14
|
+
autoHyphenate: true,
|
|
15
|
+
hyphenatedWordSize: 6,
|
|
16
|
+
preHyphen: 2,
|
|
17
|
+
postHyphen: 2,
|
|
18
|
+
consecutiveHyphens: 8,
|
|
19
|
+
zone: 36,
|
|
20
|
+
wordSpacing: [0.8, 1, 1.33],
|
|
21
|
+
letterSpacing: [0, 0, 0],
|
|
22
|
+
glyphSpacing: [1, 1, 1],
|
|
23
|
+
autoLeading: 1.2,
|
|
24
|
+
leadingType: 0,
|
|
25
|
+
hanging: false,
|
|
26
|
+
burasagari: false,
|
|
27
|
+
kinsokuOrder: 0,
|
|
28
|
+
everyLineComposer: false,
|
|
29
|
+
};
|
|
30
|
+
const defaultStyle = {
|
|
31
|
+
font: defaultFont,
|
|
32
|
+
fontSize: 12,
|
|
33
|
+
fauxBold: false,
|
|
34
|
+
fauxItalic: false,
|
|
35
|
+
autoLeading: true,
|
|
36
|
+
leading: 0,
|
|
37
|
+
horizontalScale: 1,
|
|
38
|
+
verticalScale: 1,
|
|
39
|
+
tracking: 0,
|
|
40
|
+
autoKerning: true,
|
|
41
|
+
kerning: 0,
|
|
42
|
+
baselineShift: 0,
|
|
43
|
+
fontCaps: 0,
|
|
44
|
+
fontBaseline: 0,
|
|
45
|
+
underline: false,
|
|
46
|
+
strikethrough: false,
|
|
47
|
+
ligatures: true,
|
|
48
|
+
dLigatures: false,
|
|
49
|
+
baselineDirection: 2,
|
|
50
|
+
tsume: 0,
|
|
51
|
+
styleRunAlignment: 2,
|
|
52
|
+
language: 0,
|
|
53
|
+
noBreak: false,
|
|
54
|
+
fillColor: { r: 0, g: 0, b: 0 },
|
|
55
|
+
strokeColor: { r: 0, g: 0, b: 0 },
|
|
56
|
+
fillFlag: true,
|
|
57
|
+
strokeFlag: false,
|
|
58
|
+
fillFirst: true,
|
|
59
|
+
yUnderline: 1,
|
|
60
|
+
outlineWidth: 1,
|
|
61
|
+
characterDirection: 0,
|
|
62
|
+
hindiNumbers: false,
|
|
63
|
+
kashida: 1,
|
|
64
|
+
diacriticPos: 2,
|
|
65
|
+
};
|
|
66
|
+
const defaultGridInfo = {
|
|
67
|
+
isOn: false,
|
|
68
|
+
show: false,
|
|
69
|
+
size: 18,
|
|
70
|
+
leading: 22,
|
|
71
|
+
color: { r: 0, g: 0, b: 255 },
|
|
72
|
+
leadingFillColor: { r: 0, g: 0, b: 255 },
|
|
73
|
+
alignLineHeightToGridFlags: false,
|
|
74
|
+
};
|
|
75
|
+
const paragraphStyleKeys = [
|
|
76
|
+
'justification', 'firstLineIndent', 'startIndent', 'endIndent', 'spaceBefore', 'spaceAfter',
|
|
77
|
+
'autoHyphenate', 'hyphenatedWordSize', 'preHyphen', 'postHyphen', 'consecutiveHyphens',
|
|
78
|
+
'zone', 'wordSpacing', 'letterSpacing', 'glyphSpacing', 'autoLeading', 'leadingType',
|
|
79
|
+
'hanging', 'burasagari', 'kinsokuOrder', 'everyLineComposer',
|
|
80
|
+
];
|
|
81
|
+
const styleKeys = [
|
|
82
|
+
'font', 'fontSize', 'fauxBold', 'fauxItalic', 'autoLeading', 'leading', 'horizontalScale',
|
|
83
|
+
'verticalScale', 'tracking', 'autoKerning', 'kerning', 'baselineShift', 'fontCaps', 'fontBaseline',
|
|
84
|
+
'underline', 'strikethrough', 'ligatures', 'dLigatures', 'baselineDirection', 'tsume',
|
|
85
|
+
'styleRunAlignment', 'language', 'noBreak', 'fillColor', 'strokeColor', 'fillFlag',
|
|
86
|
+
'strokeFlag', 'fillFirst', 'yUnderline', 'outlineWidth', 'characterDirection', 'hindiNumbers',
|
|
87
|
+
'kashida', 'diacriticPos',
|
|
88
|
+
];
|
|
89
|
+
const antialias = ['none', 'crisp', 'strong', 'smooth', 'sharp'];
|
|
90
|
+
const justification = [
|
|
91
|
+
'left', // 0
|
|
92
|
+
'right', // 1
|
|
93
|
+
'center', // 2
|
|
94
|
+
'justify-left', // 3
|
|
95
|
+
'justify-right', // 4
|
|
96
|
+
'justify-center', // 5
|
|
97
|
+
'justify-all', // 6
|
|
98
|
+
];
|
|
99
|
+
function upperFirst(value) {
|
|
100
|
+
return value.substring(0, 1).toUpperCase() + value.substring(1);
|
|
101
|
+
}
|
|
102
|
+
function decodeColor(color) {
|
|
103
|
+
const c = color.Values;
|
|
104
|
+
switch (color.Type) {
|
|
105
|
+
case 0: return { k: c[1] * 255 }; // grayscale (alpha?)
|
|
106
|
+
case 1: return c[0] === 1 ?
|
|
107
|
+
{ r: c[1] * 255, g: c[2] * 255, b: c[3] * 255 } : // rgb
|
|
108
|
+
{ r: c[1] * 255, g: c[2] * 255, b: c[3] * 255, a: c[0] * 255 }; // rgba
|
|
109
|
+
case 2: return { c: c[1] * 255, m: c[2] * 255, y: c[3] * 255, k: c[4] * 255 }; // cmyk (alpha?)
|
|
110
|
+
default: throw new Error('Unknown color type in text layer');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function encodeColor(color) {
|
|
114
|
+
if (!color) {
|
|
115
|
+
return { Type: 1, Values: [0, 0, 0, 0] };
|
|
116
|
+
}
|
|
117
|
+
else if ('r' in color) {
|
|
118
|
+
return { Type: 1, Values: ['a' in color ? color.a / 255 : 1, color.r / 255, color.g / 255, color.b / 255] };
|
|
119
|
+
}
|
|
120
|
+
else if ('c' in color) {
|
|
121
|
+
return { Type: 2, Values: [1, color.c / 255, color.m / 255, color.y / 255, color.k / 255] };
|
|
122
|
+
}
|
|
123
|
+
else if ('k' in color) {
|
|
124
|
+
return { Type: 0, Values: [1, color.k / 255] };
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
throw new Error('Invalid color type in text layer');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function arraysEqual(a, b) {
|
|
131
|
+
if (!a || !b)
|
|
132
|
+
return false;
|
|
133
|
+
if (a.length !== b.length)
|
|
134
|
+
return false;
|
|
135
|
+
for (let i = 0; i < a.length; i++)
|
|
136
|
+
if (a[i] !== b[i])
|
|
137
|
+
return false;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
function objectsEqual(a, b) {
|
|
141
|
+
if (!a || !b)
|
|
142
|
+
return false;
|
|
143
|
+
for (const key of Object.keys(a))
|
|
144
|
+
if (a[key] !== b[key])
|
|
145
|
+
return false;
|
|
146
|
+
for (const key of Object.keys(b))
|
|
147
|
+
if (a[key] !== b[key])
|
|
148
|
+
return false;
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
function findOrAddFont(fonts, font) {
|
|
152
|
+
for (let i = 0; i < fonts.length; i++) {
|
|
153
|
+
if (fonts[i].name === font.name)
|
|
154
|
+
return i;
|
|
155
|
+
}
|
|
156
|
+
fonts.push(font);
|
|
157
|
+
return fonts.length - 1;
|
|
158
|
+
}
|
|
159
|
+
function decodeObject(obj, keys, fonts) {
|
|
160
|
+
const result = {};
|
|
161
|
+
for (const key of keys) {
|
|
162
|
+
const Key = upperFirst(key);
|
|
163
|
+
if (obj[Key] === undefined)
|
|
164
|
+
continue;
|
|
165
|
+
if (key === 'justification') {
|
|
166
|
+
result[key] = justification[obj[Key]];
|
|
167
|
+
}
|
|
168
|
+
else if (key === 'font') {
|
|
169
|
+
result[key] = fonts[obj[Key]];
|
|
170
|
+
}
|
|
171
|
+
else if (key === 'fillColor' || key === 'strokeColor') {
|
|
172
|
+
result[key] = decodeColor(obj[Key]);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
result[key] = obj[Key];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
function encodeObject(obj, keys, fonts) {
|
|
181
|
+
const result = {};
|
|
182
|
+
for (const key of keys) {
|
|
183
|
+
const Key = upperFirst(key);
|
|
184
|
+
if (obj[key] === undefined)
|
|
185
|
+
continue;
|
|
186
|
+
if (key === 'justification') {
|
|
187
|
+
result[Key] = justification.indexOf(obj[key] ?? 'left');
|
|
188
|
+
}
|
|
189
|
+
else if (key === 'font') {
|
|
190
|
+
result[Key] = findOrAddFont(fonts, obj[key]);
|
|
191
|
+
}
|
|
192
|
+
else if (key === 'fillColor' || key === 'strokeColor') {
|
|
193
|
+
result[Key] = encodeColor(obj[key]);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
result[Key] = obj[key];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
function decodeParagraphStyle(obj, fonts) {
|
|
202
|
+
return decodeObject(obj, paragraphStyleKeys, fonts);
|
|
203
|
+
}
|
|
204
|
+
function decodeStyle(obj, fonts) {
|
|
205
|
+
return decodeObject(obj, styleKeys, fonts);
|
|
206
|
+
}
|
|
207
|
+
function encodeParagraphStyle(obj, fonts) {
|
|
208
|
+
return encodeObject(obj, paragraphStyleKeys, fonts);
|
|
209
|
+
}
|
|
210
|
+
function encodeStyle(obj, fonts) {
|
|
211
|
+
return encodeObject(obj, styleKeys, fonts);
|
|
212
|
+
}
|
|
213
|
+
function deduplicateValues(base, runs, keys) {
|
|
214
|
+
if (!runs.length)
|
|
215
|
+
return;
|
|
216
|
+
for (const key of keys) {
|
|
217
|
+
const value = runs[0].style[key];
|
|
218
|
+
if (value !== undefined) {
|
|
219
|
+
let identical = false;
|
|
220
|
+
if (Array.isArray(value)) {
|
|
221
|
+
identical = runs.every(r => arraysEqual(r.style[key], value));
|
|
222
|
+
}
|
|
223
|
+
else if (typeof value === 'object') {
|
|
224
|
+
identical = runs.every(r => objectsEqual(r.style[key], value));
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
identical = runs.every(r => r.style[key] === value);
|
|
228
|
+
}
|
|
229
|
+
if (identical) {
|
|
230
|
+
base[key] = value;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const styleValue = base[key];
|
|
234
|
+
if (styleValue !== undefined) {
|
|
235
|
+
for (const r of runs) {
|
|
236
|
+
let same = false;
|
|
237
|
+
if (Array.isArray(value)) {
|
|
238
|
+
same = arraysEqual(r.style[key], value);
|
|
239
|
+
}
|
|
240
|
+
else if (typeof value === 'object') {
|
|
241
|
+
same = objectsEqual(r.style[key], value);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
same = r.style[key] === value;
|
|
245
|
+
}
|
|
246
|
+
if (same)
|
|
247
|
+
delete r.style[key];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (runs.every(x => Object.keys(x.style).length === 0)) {
|
|
252
|
+
runs.length = 0;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
export function decodeEngineData(engineData) {
|
|
256
|
+
// console.log('engineData', require('util').inspect(engineData, false, 99, true));
|
|
257
|
+
const engineDict = engineData.EngineDict;
|
|
258
|
+
const resourceDict = engineData.ResourceDict;
|
|
259
|
+
const fonts = resourceDict.FontSet.map(f => ({
|
|
260
|
+
name: f.Name,
|
|
261
|
+
script: f.Script,
|
|
262
|
+
type: f.FontType,
|
|
263
|
+
synthetic: f.Synthetic,
|
|
264
|
+
}));
|
|
265
|
+
let text = engineDict.Editor.Text.replace(/\r/g, '\n');
|
|
266
|
+
let removedCharacters = 0;
|
|
267
|
+
while (/\n$/.test(text)) {
|
|
268
|
+
text = text.substring(0, text.length - 1);
|
|
269
|
+
removedCharacters++;
|
|
270
|
+
}
|
|
271
|
+
const result = {
|
|
272
|
+
text,
|
|
273
|
+
antiAlias: antialias[engineDict.AntiAlias] ?? 'smooth',
|
|
274
|
+
useFractionalGlyphWidths: !!engineDict.UseFractionalGlyphWidths,
|
|
275
|
+
superscriptSize: resourceDict.SuperscriptSize,
|
|
276
|
+
superscriptPosition: resourceDict.SuperscriptPosition,
|
|
277
|
+
subscriptSize: resourceDict.SubscriptSize,
|
|
278
|
+
subscriptPosition: resourceDict.SubscriptPosition,
|
|
279
|
+
smallCapSize: resourceDict.SmallCapSize,
|
|
280
|
+
};
|
|
281
|
+
// shape
|
|
282
|
+
const photoshop = engineDict.Rendered?.Shapes?.Children?.[0]?.Cookie?.Photoshop;
|
|
283
|
+
if (photoshop) {
|
|
284
|
+
result.shapeType = photoshop.ShapeType === 1 ? 'box' : 'point';
|
|
285
|
+
if (photoshop.PointBase)
|
|
286
|
+
result.pointBase = photoshop.PointBase;
|
|
287
|
+
if (photoshop.BoxBounds)
|
|
288
|
+
result.boxBounds = photoshop.BoxBounds;
|
|
289
|
+
}
|
|
290
|
+
// paragraph style
|
|
291
|
+
// const theNormalParagraphSheet = resourceDict.TheNormalParagraphSheet;
|
|
292
|
+
// const paragraphSheetSet = resourceDict.ParagraphSheetSet;
|
|
293
|
+
// const paragraphProperties = paragraphSheetSet[theNormalParagraphSheet].Properties;
|
|
294
|
+
const paragraphRun = engineDict.ParagraphRun;
|
|
295
|
+
result.paragraphStyle = {}; // decodeParagraphStyle(paragraphProperties, fonts);
|
|
296
|
+
result.paragraphStyleRuns = [];
|
|
297
|
+
for (let i = 0; i < paragraphRun.RunArray.length; i++) {
|
|
298
|
+
const run = paragraphRun.RunArray[i];
|
|
299
|
+
const length = paragraphRun.RunLengthArray[i];
|
|
300
|
+
const style = decodeParagraphStyle(run.ParagraphSheet.Properties, fonts);
|
|
301
|
+
// const adjustments = {
|
|
302
|
+
// axis: run.Adjustments.Axis,
|
|
303
|
+
// xy: run.Adjustments.XY,
|
|
304
|
+
// };
|
|
305
|
+
result.paragraphStyleRuns.push({ length, style /*, adjustments*/ });
|
|
306
|
+
}
|
|
307
|
+
for (let counter = removedCharacters; result.paragraphStyleRuns.length && counter > 0; counter--) {
|
|
308
|
+
if (--result.paragraphStyleRuns[result.paragraphStyleRuns.length - 1].length === 0) {
|
|
309
|
+
result.paragraphStyleRuns.pop();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
deduplicateValues(result.paragraphStyle, result.paragraphStyleRuns, paragraphStyleKeys);
|
|
313
|
+
if (!result.paragraphStyleRuns.length)
|
|
314
|
+
delete result.paragraphStyleRuns;
|
|
315
|
+
// style
|
|
316
|
+
// const theNormalStyleSheet = resourceDict.TheNormalStyleSheet;
|
|
317
|
+
// const styleSheetSet = resourceDict.StyleSheetSet;
|
|
318
|
+
// const styleSheetData = styleSheetSet[theNormalStyleSheet].StyleSheetData;
|
|
319
|
+
const styleRun = engineDict.StyleRun;
|
|
320
|
+
result.style = {}; // decodeStyle(styleSheetData, fonts);
|
|
321
|
+
result.styleRuns = [];
|
|
322
|
+
for (let i = 0; i < styleRun.RunArray.length; i++) {
|
|
323
|
+
const length = styleRun.RunLengthArray[i];
|
|
324
|
+
const style = decodeStyle(styleRun.RunArray[i].StyleSheet.StyleSheetData, fonts);
|
|
325
|
+
if (!style.font)
|
|
326
|
+
style.font = fonts[0];
|
|
327
|
+
result.styleRuns.push({ length, style });
|
|
328
|
+
}
|
|
329
|
+
for (let counter = removedCharacters; result.styleRuns.length && counter > 0; counter--) {
|
|
330
|
+
if (--result.styleRuns[result.styleRuns.length - 1].length === 0) {
|
|
331
|
+
result.styleRuns.pop();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
deduplicateValues(result.style, result.styleRuns, styleKeys);
|
|
335
|
+
if (!result.styleRuns.length)
|
|
336
|
+
delete result.styleRuns;
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
export function encodeEngineData(data) {
|
|
340
|
+
const text = `${(data.text || '').replace(/\r?\n/g, '\r')}\r`;
|
|
341
|
+
const fonts = [
|
|
342
|
+
{ name: 'AdobeInvisFont', script: 0, type: 0, synthetic: 0 },
|
|
343
|
+
];
|
|
344
|
+
const defFont = data.style?.font || data.styleRuns?.find(s => s.style.font)?.style.font || defaultFont;
|
|
345
|
+
const paragraphRunArray = [];
|
|
346
|
+
const paragraphRunLengthArray = [];
|
|
347
|
+
const paragraphRuns = data.paragraphStyleRuns;
|
|
348
|
+
if (paragraphRuns && paragraphRuns.length) {
|
|
349
|
+
let leftLength = text.length;
|
|
350
|
+
for (const run of paragraphRuns) {
|
|
351
|
+
let runLength = Math.min(run.length, leftLength);
|
|
352
|
+
leftLength -= runLength;
|
|
353
|
+
if (!runLength)
|
|
354
|
+
continue; // ignore 0 size runs
|
|
355
|
+
// extend last run if it's only for trailing \r
|
|
356
|
+
if (leftLength === 1 && run === paragraphRuns[paragraphRuns.length - 1]) {
|
|
357
|
+
runLength++;
|
|
358
|
+
leftLength--;
|
|
359
|
+
}
|
|
360
|
+
paragraphRunLengthArray.push(runLength);
|
|
361
|
+
paragraphRunArray.push({
|
|
362
|
+
ParagraphSheet: {
|
|
363
|
+
DefaultStyleSheet: 0,
|
|
364
|
+
Properties: encodeParagraphStyle({ ...defaultParagraphStyle, ...data.paragraphStyle, ...run.style }, fonts),
|
|
365
|
+
},
|
|
366
|
+
Adjustments: { Axis: [1, 0, 1], XY: [0, 0] },
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
if (leftLength) {
|
|
370
|
+
paragraphRunLengthArray.push(leftLength);
|
|
371
|
+
paragraphRunArray.push({
|
|
372
|
+
ParagraphSheet: {
|
|
373
|
+
DefaultStyleSheet: 0,
|
|
374
|
+
Properties: encodeParagraphStyle({ ...defaultParagraphStyle, ...data.paragraphStyle }, fonts),
|
|
375
|
+
},
|
|
376
|
+
Adjustments: { Axis: [1, 0, 1], XY: [0, 0] },
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
for (let i = 0, last = 0; i < text.length; i++) {
|
|
382
|
+
if (text.charCodeAt(i) === 13) { // \r
|
|
383
|
+
paragraphRunLengthArray.push(i - last + 1);
|
|
384
|
+
paragraphRunArray.push({
|
|
385
|
+
ParagraphSheet: {
|
|
386
|
+
DefaultStyleSheet: 0,
|
|
387
|
+
Properties: encodeParagraphStyle({ ...defaultParagraphStyle, ...data.paragraphStyle }, fonts),
|
|
388
|
+
},
|
|
389
|
+
Adjustments: { Axis: [1, 0, 1], XY: [0, 0] },
|
|
390
|
+
});
|
|
391
|
+
last = i + 1;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const styleSheetData = encodeStyle({ ...defaultStyle, font: defFont }, fonts);
|
|
396
|
+
const styleRuns = data.styleRuns || [{ length: text.length, style: data.style || {} }];
|
|
397
|
+
const styleRunArray = [];
|
|
398
|
+
const styleRunLengthArray = [];
|
|
399
|
+
let leftLength = text.length;
|
|
400
|
+
for (const run of styleRuns) {
|
|
401
|
+
let runLength = Math.min(run.length, leftLength);
|
|
402
|
+
leftLength -= runLength;
|
|
403
|
+
if (!runLength)
|
|
404
|
+
continue; // ignore 0 size runs
|
|
405
|
+
// extend last run if it's only for trailing \r
|
|
406
|
+
if (leftLength === 1 && run === styleRuns[styleRuns.length - 1]) {
|
|
407
|
+
runLength++;
|
|
408
|
+
leftLength--;
|
|
409
|
+
}
|
|
410
|
+
styleRunLengthArray.push(runLength);
|
|
411
|
+
styleRunArray.push({
|
|
412
|
+
StyleSheet: {
|
|
413
|
+
StyleSheetData: encodeStyle({
|
|
414
|
+
kerning: 0,
|
|
415
|
+
autoKerning: true,
|
|
416
|
+
fillColor: { r: 0, g: 0, b: 0 },
|
|
417
|
+
...data.style,
|
|
418
|
+
...run.style,
|
|
419
|
+
}, fonts),
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
// add extra run to the end if existing ones didn't fill it up
|
|
424
|
+
if (leftLength && styleRuns.length) {
|
|
425
|
+
styleRunLengthArray.push(leftLength);
|
|
426
|
+
styleRunArray.push({
|
|
427
|
+
StyleSheet: {
|
|
428
|
+
StyleSheetData: encodeStyle({
|
|
429
|
+
kerning: 0,
|
|
430
|
+
autoKerning: true,
|
|
431
|
+
fillColor: { r: 0, g: 0, b: 0 },
|
|
432
|
+
...data.style,
|
|
433
|
+
}, fonts),
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
const gridInfo = { ...defaultGridInfo, ...data.gridInfo };
|
|
438
|
+
const WritingDirection = data.orientation === 'vertical' ? 2 : 0;
|
|
439
|
+
const Procession = data.orientation === 'vertical' ? 1 : 0;
|
|
440
|
+
const ShapeType = data.shapeType === 'box' ? 1 : 0;
|
|
441
|
+
const Photoshop = {
|
|
442
|
+
ShapeType,
|
|
443
|
+
};
|
|
444
|
+
if (ShapeType === 0) {
|
|
445
|
+
Photoshop.PointBase = data.pointBase || [0, 0];
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
Photoshop.BoxBounds = data.boxBounds || [0, 0, 0, 0];
|
|
449
|
+
}
|
|
450
|
+
// needed for correct order of properties
|
|
451
|
+
Photoshop.Base = {
|
|
452
|
+
ShapeType,
|
|
453
|
+
TransformPoint0: [1, 0],
|
|
454
|
+
TransformPoint1: [0, 1],
|
|
455
|
+
TransformPoint2: [0, 0],
|
|
456
|
+
};
|
|
457
|
+
const defaultResources = {
|
|
458
|
+
KinsokuSet: [
|
|
459
|
+
{
|
|
460
|
+
Name: 'PhotoshopKinsokuHard',
|
|
461
|
+
NoStart: '、。,.・:;?!ー―’”)〕]}〉》」』】ヽヾゝゞ々ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ゛゜?!)]},.:;℃℉¢%‰',
|
|
462
|
+
NoEnd: '‘“(〔[{〈《「『【([{¥$£@§〒#',
|
|
463
|
+
Keep: '―‥',
|
|
464
|
+
Hanging: '、。.,',
|
|
465
|
+
},
|
|
466
|
+
{
|
|
467
|
+
Name: 'PhotoshopKinsokuSoft',
|
|
468
|
+
NoStart: '、。,.・:;?!’”)〕]}〉》」』】ヽヾゝゞ々',
|
|
469
|
+
NoEnd: '‘“(〔[{〈《「『【',
|
|
470
|
+
Keep: '―‥',
|
|
471
|
+
Hanging: '、。.,',
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
MojiKumiSet: [
|
|
475
|
+
{ InternalName: 'Photoshop6MojiKumiSet1' },
|
|
476
|
+
{ InternalName: 'Photoshop6MojiKumiSet2' },
|
|
477
|
+
{ InternalName: 'Photoshop6MojiKumiSet3' },
|
|
478
|
+
{ InternalName: 'Photoshop6MojiKumiSet4' },
|
|
479
|
+
],
|
|
480
|
+
TheNormalStyleSheet: 0,
|
|
481
|
+
TheNormalParagraphSheet: 0,
|
|
482
|
+
ParagraphSheetSet: [
|
|
483
|
+
{
|
|
484
|
+
Name: 'Normal RGB',
|
|
485
|
+
DefaultStyleSheet: 0,
|
|
486
|
+
Properties: encodeParagraphStyle({ ...defaultParagraphStyle, ...data.paragraphStyle }, fonts),
|
|
487
|
+
},
|
|
488
|
+
],
|
|
489
|
+
StyleSheetSet: [
|
|
490
|
+
{
|
|
491
|
+
Name: 'Normal RGB',
|
|
492
|
+
StyleSheetData: styleSheetData,
|
|
493
|
+
},
|
|
494
|
+
],
|
|
495
|
+
FontSet: fonts.map(f => ({
|
|
496
|
+
Name: f.name,
|
|
497
|
+
Script: f.script || 0,
|
|
498
|
+
FontType: f.type || 0,
|
|
499
|
+
Synthetic: f.synthetic || 0,
|
|
500
|
+
})),
|
|
501
|
+
SuperscriptSize: data.superscriptSize ?? 0.583,
|
|
502
|
+
SuperscriptPosition: data.superscriptPosition ?? 0.333,
|
|
503
|
+
SubscriptSize: data.subscriptSize ?? 0.583,
|
|
504
|
+
SubscriptPosition: data.subscriptPosition ?? 0.333,
|
|
505
|
+
SmallCapSize: data.smallCapSize ?? 0.7,
|
|
506
|
+
};
|
|
507
|
+
const engineData = {
|
|
508
|
+
EngineDict: {
|
|
509
|
+
Editor: { Text: text },
|
|
510
|
+
ParagraphRun: {
|
|
511
|
+
DefaultRunData: {
|
|
512
|
+
ParagraphSheet: { DefaultStyleSheet: 0, Properties: {} },
|
|
513
|
+
Adjustments: { Axis: [1, 0, 1], XY: [0, 0] },
|
|
514
|
+
},
|
|
515
|
+
RunArray: paragraphRunArray,
|
|
516
|
+
RunLengthArray: paragraphRunLengthArray,
|
|
517
|
+
IsJoinable: 1,
|
|
518
|
+
},
|
|
519
|
+
StyleRun: {
|
|
520
|
+
DefaultRunData: { StyleSheet: { StyleSheetData: {} } },
|
|
521
|
+
RunArray: styleRunArray,
|
|
522
|
+
RunLengthArray: styleRunLengthArray,
|
|
523
|
+
IsJoinable: 2,
|
|
524
|
+
},
|
|
525
|
+
GridInfo: {
|
|
526
|
+
GridIsOn: !!gridInfo.isOn,
|
|
527
|
+
ShowGrid: !!gridInfo.show,
|
|
528
|
+
GridSize: gridInfo.size ?? 18,
|
|
529
|
+
GridLeading: gridInfo.leading ?? 22,
|
|
530
|
+
GridColor: encodeColor(gridInfo.color),
|
|
531
|
+
GridLeadingFillColor: encodeColor(gridInfo.color),
|
|
532
|
+
AlignLineHeightToGridFlags: !!gridInfo.alignLineHeightToGridFlags,
|
|
533
|
+
},
|
|
534
|
+
AntiAlias: antialias.indexOf(data.antiAlias ?? 'sharp'),
|
|
535
|
+
UseFractionalGlyphWidths: data.useFractionalGlyphWidths ?? true,
|
|
536
|
+
Rendered: {
|
|
537
|
+
Version: 1,
|
|
538
|
+
Shapes: {
|
|
539
|
+
WritingDirection,
|
|
540
|
+
Children: [
|
|
541
|
+
{
|
|
542
|
+
ShapeType,
|
|
543
|
+
Procession,
|
|
544
|
+
Lines: { WritingDirection, Children: [] },
|
|
545
|
+
Cookie: { Photoshop },
|
|
546
|
+
},
|
|
547
|
+
],
|
|
548
|
+
},
|
|
549
|
+
},
|
|
550
|
+
},
|
|
551
|
+
ResourceDict: { ...defaultResources },
|
|
552
|
+
DocumentResources: { ...defaultResources },
|
|
553
|
+
};
|
|
554
|
+
// console.log('encodeEngineData', require('util').inspect(engineData, false, 99, true));
|
|
555
|
+
return engineData;
|
|
556
|
+
}
|
package/dist/utf8.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function stringLengthInBytes(value: string): number;
|
|
2
|
+
export declare function encodeStringTo(buffer: Uint8Array | Buffer, offset: number, value: string): number;
|
|
3
|
+
export declare function encodeString(value: string): Uint8Array;
|
|
4
|
+
export declare function decodeString(value: Uint8Array): string;
|