-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontFace.coffee
More file actions
155 lines (124 loc) · 4.82 KB
/
Copy pathFontFace.coffee
File metadata and controls
155 lines (124 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
###################################################################################
# Created by Jordan Robert Dobson on 05 OCT 2015
# Updated by Jordan Robert Dobson on 14 SEP 2016
#
# Use to add font files and reference them in your CSS style settings.
#
# To Get Started...
#
# 1. Place the FontFace.coffee file in Framer Studio modules directory
#
# 2. In your project include:
# {FontFace} = require "FontFace"
#
# 3. To add a font face:
# gotham = new FontFace name: "Gotham", file: "Gotham.ttf", weight: 400
#
# 4. It checks that the font was loaded. Errors can be suppressed like so...
# gotham = new FontFace name: "Gotham", file: "Gotham.ttf", hideErrors: true
#
###################################################################################
class exports.FontFace
TEST =
face: "monospace"
text: "foo"
time: .01
maxLoadAttempts: 200
hideErrorMessages: true
TEST.style =
width: "auto"
fontSize: "150px"
fontFamily: TEST.face
TEST.layer = new Layer
name: ".FontFace Tester"
width: 0
height: 1
maxX: -(Screen.width)
visible: false
html: TEST.text
style: TEST.style
# SETUP FOR EVERY INSTANCE
constructor: (options) ->
@name = @file = @testLayer = @isLoaded = @loadFailed = @loadAttempts = @originalSize = @hideErrors = null
if options?
@name = options.name or null
@file = options.file or null
@weight = options.weight or 400
@style = options.style or "normal"
return missingArgumentError() unless @name? and @file?
@testLayer = TEST.layer.copy()
@testLayer.style = TEST.style
@testLayer.maxX = -(Screen.width)
@testLayer.visible = true
@isLoaded = false
@loadFailed = false
@loadAttempts = 0
@hideErrors = options.hideErrors
return addFontFace @
##############################################################################
# Private Helper Methods #####################################################
addFontFace = (self) ->
# Create our Element & Node
styleTag = document.createElement 'style'
faceCSS = document.createTextNode """
@font-face {
font-family:"#{self.name }";
src: url("#{self.file }") format("truetype");
font-weight: #{self.weight};
font-style: #{self.style };
}"""
# Add the Element & Node to the document
styleTag.appendChild faceCSS
document.head.appendChild styleTag
# Test out the Font to see if it changed
testNewFace self.name, self
##############################################################################
removeTestLayer = (self) ->
self.testLayer.destroy()
self.testLayer = null
##############################################################################
testNewFace = (name, self) ->
initialWidth = self.testLayer._element.getBoundingClientRect().width
# Check to see if it's ready yet
if initialWidth is 0
if self.hideErrors is false or TEST.hideErrorMessages is false
print "Load testing failed. Attempting again."
return Utils.delay TEST.time, -> testNewFace name, self
self.loadAttempts++
if self.originalSize is null
self.originalSize = initialWidth
self.testLayer.style = fontFamily: "#{name}, #{TEST.face}"
widthUpdate = self.testLayer._element.getBoundingClientRect().width
if self.originalSize is widthUpdate
# If we can attempt to check again... Do it
if self.loadAttempts < TEST.maxLoadAttempts
return Utils.delay TEST.time, -> testNewFace name, self
print "⚠️ Failed loading FontFace: #{name} #{self.weight}" unless self.hideErrors
console.log "⚠️ Failed loading FontFace: #{name} #{self.weight}"
self.isLoaded = false
self.loadFailed = true
loadTestingFileError self unless self.hideErrors
return
else
print "LOADED: #{name} #{self.weight}" unless self.hideErrors is false or TEST.hideErrorMessages
console.log "FONT FACE - LOADED: #{name} #{self.weight}"
self.isLoaded = true
self.loadFailed = false
removeTestLayer self
return name
##############################################################################
# Error Handler Methods ######################################################
missingArgumentError = ->
error """<b>FontFace</b> Missing Name and/or File: See Console"""
console.error """
Error: You must pass name & file properites when creating a new FontFace. \n
Example: myFace = new FontFace name:\"Gotham\", file:\"gotham.ttf\" \n"""
loadTestingFileError = (self) ->
error """<b>FontFace</b> Loading Error: See Console"""
console.error """
Error: Couldn't detect the font: \"#{self.name}\" and file: \"#{self.file}\" was loaded. \n
Either the file couldn't be found or your browser doesn't support the file type that was provided. \n
Suppress this message by adding \"hideErrors: true\" when creating a new FontFace. \n"""
error = (message) ->
Framer.Extras.ErrorDisplay.enable()
throw Error message