@@ -3,6 +3,48 @@ import Settings from 'sketch/settings';
3
3
import { spawnSync , execSync } from '@skpm/child_process' ;
4
4
import { SettingKeys } from './constants' ;
5
5
6
+ export function getJsonDocument ( doc ) {
7
+ var filePath = decodeURIComponent ( doc . path ) ;
8
+ var fileName = decodeURIComponent ( doc . path ) . replace ( / ^ .* [ \\ \/ ] / , '' ) . trim ( ) ;
9
+ var tmpFileDocumentName = `/var/tmp/${ fileName } .json` ;
10
+ log ( "Reading Json Document:" + filePath ) ;
11
+ const unzipCmd = `unzip -p '${ filePath } ' document.json > '${ tmpFileDocumentName } '` ;
12
+ log ( unzipCmd ) ;
13
+ var exec = execSync ( unzipCmd ) ;
14
+ if ( exec ) {
15
+ var contents = readFile ( `/var/tmp/${ fileName } .json` ) ;
16
+ if ( contents ) {
17
+ removeFile ( tmpFileDocumentName ) ;
18
+ return JSON . parse ( contents ) ;
19
+ }
20
+ }
21
+ return null ;
22
+ }
23
+
24
+ export function isFileExist ( source ) {
25
+ const manager = NSFileManager . defaultManager ( )
26
+ return manager . fileExistsAtPath ( source )
27
+ }
28
+
29
+ export function copyFile2 ( source , target ) {
30
+ const manager = NSFileManager . defaultManager ( )
31
+ if ( ! manager . fileExistsAtPath ( source ) ) throw new Error ( `file not exist ${ source } ` )
32
+ manager . copyItemAtPath_toPath_error ( source , target , null )
33
+ }
34
+
35
+ export function writeToFile ( path , content ) {
36
+ const resultStr = NSString . stringWithFormat ( '%@' , content )
37
+ resultStr . writeToFile_atomically ( path , true )
38
+ }
39
+
40
+ export function removeFile ( path ) {
41
+ const manager = NSFileManager . defaultManager ( )
42
+ manager . removeItemAtPath_error ( path , null )
43
+ }
44
+
45
+ function readFile ( path ) {
46
+ return NSString . stringWithContentsOfFile_encoding_error ( path , NSUTF8StringEncoding , null ) ;
47
+ }
6
48
7
49
export function getFileAndQueueName ( doc , queuePath ) {
8
50
var branch = "" ;
@@ -87,6 +129,7 @@ export function copyFile(fromCopyFile, toCopyFile) {
87
129
return false ;
88
130
}
89
131
else {
132
+ log ( "Copied !" )
90
133
return true ;
91
134
}
92
135
}
@@ -137,16 +180,32 @@ var exportLayer = function (layer, path) {
137
180
138
181
var traverseFonts = function ( layer , fonts ) {
139
182
if ( layer . type == "Text" && layer . style . fontFamily != "Helvetica" ) {
140
- console . log ( layer . style . fontFamily + "-" + layer . style . fontWeight ) ;
141
- var fontName = NSFontManager . sharedFontManager ( ) . fontWithFamily_traits_weight_size ( layer . style . fontFamily , 0 , layer . style . fontWeight , layer . style . fontSize ) . fontName ( )
142
- var displayName = NSFontManager . sharedFontManager ( ) . fontWithFamily_traits_weight_size ( layer . style . fontFamily , 0 , layer . style . fontWeight , layer . style . fontSize ) . displayName ( )
143
- console . log ( "FONT NAME:" + fontName ) ;
144
- console . log ( "Display NAME:" + displayName ) ;
145
- var font = String ( fontName ) ;
146
- if ( ! fonts . includes ( font ) ) {
147
- fonts . push ( String ( font ) ) ;
183
+ log ( layer . style . fontFamily + "-" + layer . style . fontWeight ) ;
184
+ var fontManager = NSFontManager . sharedFontManager ( ) ;
185
+ if ( ! fontManager ) {
186
+ log ( "NSFontManager sharedFontManager NULL !!!" ) ;
187
+ return ;
188
+ }
189
+ if ( layer . style . fontFamily && layer . style . fontWeight && layer . style . fontSize ) {
190
+ var nFont = fontManager . fontWithFamily_traits_weight_size ( layer . style . fontFamily , 0 , layer . style . fontWeight , layer . style . fontSize ) ;
191
+ if ( nFont ) {
192
+ var fontName = nFont . fontName ( ) ;
193
+ log ( "FONT NAME:" + fontName ) ;
194
+ var font = String ( fontName ) ;
195
+ if ( ! fonts . includes ( font ) ) {
196
+ fonts . push ( String ( font ) ) ;
197
+ }
198
+ } else {
199
+ // maybe its a reference to a Variable Font, just add the familyname
200
+ log ( "Font not found !! : " ) ;
201
+ log ( layer . style . fontFamily + "-" + layer . style . fontWeight + "-" + layer . style . fontSize ) ;
202
+ if ( ! fonts . includes ( layer . style . fontFamily ) )
203
+ fonts . push ( layer . style . fontFamily ) ;
204
+ }
205
+ }
206
+ else {
207
+ log ( "Something wrong with this font: " + layer . style . fontFamily + "-" + layer . style . fontWeight + "-" + layer . style . fontSize ) ;
148
208
}
149
-
150
209
}
151
210
if ( layer . layers ) {
152
211
layer . layers . forEach ( l => {
@@ -176,7 +235,7 @@ function getPostcriptNames(path) {
176
235
var retMetadata = execSync ( fontNameCmd ) ;
177
236
if ( retMetadata )
178
237
return retMetadata . toString ( ) ;
179
- } catch { }
238
+ } catch { }
180
239
return "" ;
181
240
}
182
241
@@ -214,17 +273,40 @@ export function copyFonts(doc, path) {
214
273
}
215
274
}
216
275
}
217
-
218
-
219
276
) ;
220
277
221
278
fonts . forEach ( fontName => {
222
- if ( mapping [ fontName ] != undefined ) {
223
- var cmdCpy = `cp ${ mapping [ fontName ] . replace ( / \s / g, '\\ ' ) } ${ path } `
224
- console . log ( cmdCpy ) ;
225
- execSync ( cmdCpy ) ;
279
+ if ( mapping [ fontName ] != undefined ) {
280
+ var cmdCpy = `cp ${ mapping [ fontName ] . replace ( / \s / g, '\\ ' ) } ${ path } `
281
+ console . log ( cmdCpy ) ;
282
+ execSync ( cmdCpy ) ;
283
+ }
284
+ else {
285
+ log ( "Looking in references for " + fontName )
286
+ // we couldn't find in the traditional way, just unzip the document and see the font references
287
+ var document = getJsonDocument ( doc ) ;
288
+ if ( document ) {
289
+ if ( document . fontReferences ) {
290
+ for ( var i in document . fontReferences ) {
291
+ if ( document . fontReferences [ i ] . fontData ) {
292
+ log ( "Font is embbeded" ) ;
293
+ break ;
294
+ }
295
+ if ( String ( document . fontReferences [ i ] . fontFamilyName ) . localeCompare ( fontName ) == 0 ) {
296
+ var cmdCpy = `cp ${ "~/Library/Fonts/" + String ( document . fontReferences [ i ] . fontFileName ) . replace ( / \s / g, '\\ ' ) } ${ path } `
297
+ try {
298
+ if ( execSync ( cmdCpy ) ) {
299
+ log ( "copied !" ) ;
300
+ break ;
301
+ }
302
+ }
303
+ catch { }
304
+ }
305
+ }
226
306
}
307
+ }
227
308
}
309
+ }
228
310
)
229
311
}
230
312
0 commit comments