-
Notifications
You must be signed in to change notification settings - Fork 0
/
_google-font.scss
265 lines (237 loc) · 11.1 KB
/
_google-font.scss
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// _____________________________________________________________________
//
// Google Font v.1.2
//
// _____________________________________________________________________
//
//
// _________________________________________________________________
//
// Gonfig
//
// _________________________________________________________________
//
// To provide flexibility google-font can be set to handle multiple
// fonts, weights and styles in one include. This does not affect
// the output compared with individual includes (recommended).
//
// You can en-/disable each feature individually.
// Be aware of output as all combinations will be added to the import.
// Also note that when multi-fonts is enabled all multi word font names
// like Open Sans must be quoted 'Open Sans'
//
// By default only multiple weights are allowed as it is what
// is most likely to be used and does not cause unforseen
// output combinations (and heavy load)
//
// @import google-font(Open Sans, 300 400 600);
// @import google-font(Open Sans, 300 400 600, italic);
//
$google-font-enable-multi-fonts : false !default;
$google-font-enable-multi-weights: true !default;
$google-font-enable-multi-styles : false !default;
//
// To ease the job of handling subsets it is recommended you use the
// $google-font-subsets map. Simply switch the value to true and the
// subset will be added to all your imports
//
$google-font-subsets: (
'arabic' :false
, 'cyrillic' :false
, 'cyrillic-ext':false
, 'devanagari' :false
, 'greek' :false
, 'greek-ext' :false
, 'hebrew' :false
, 'khmer' :false
, 'latin' :false
, 'latin-ext' :false
, 'telugu' :false
, 'vietnamese' :false
) !default;
@mixin google-font($name: null, $weight: 400, $style: normal, $text: null, $subset: null){
$URL : '//fonts.googleapis.com/css?family=';
$_name : ();
$_weight: ();
$_style : ();
$_subset: ();
// _________________________________________________________________
//
// Import or add to font list
//
// _________________________________________________________________
@if $name {
@if $google-font-enable-multi-fonts == false { $name: #{$name}; }
@if $google-font-enable-multi-weights == false and type-of($weight) == list { $weight: nth($weight, 1); }
@if $google-font-enable-multi-styles == false and type-of($style) == list { $style : nth($style, 1); }
@if type-of($name) == string { $_name : append((), $name, comma); }
@if type-of($name) == list { $_name : join((), $name, comma); }
@if type-of($weight) == number { $_weight: append((), $weight, comma); }
@if type-of($weight) == list { $_weight: join((), $weight, comma); }
@if type-of($style) == string { $_style : append((), $style, comma); }
@if type-of($style) == list { $_style : join((), $style, comma); }
@if type-of($subset) == string { $_subset: append((), $subset, comma); }
@if type-of($subset) == list { $_subset: join((), $subset, comma); }
// ------------------------------------------------
// Name
// ------------------------------------------------
// Replace spaces in font name with + and
// remove duplicates
//
$_temp: ();
@for $i from 1 through length($_name){
$_temp: append($_temp, __gf-str-replace(#{unquote(nth($_name, $i))},' ','+'), comma)
}
$_name: __gf-list-unique($_temp);
// ------------------------------------------------
// Weight and style
// ------------------------------------------------
// Add default weight and style if null
// Concatinate weight and style if needed
// Remove duplicates
//
$_weight: if(length($_weight)>0, $_weight, (400));
$_style : if(length($_style)>0, $_style, (normal));
$_temp: ();
@for $i from 1 through length($_weight){
@for $j from 1 through length($_style){
$w: nth($_weight, $i); $s: nth($_style, $j);
$_temp: append($_temp, if($s == normal, $w, unquote($w+$s)), comma);
}
}
$_weight: __gf-list-unique($_temp);
// ------------------------------------------------
// Subsets
// ------------------------------------------------
// Add enabled subsets and remove diplicates
// if we are not requesting an optimized font
//
$_temp:();
@if length($_subset) > 0 {
@for $i from 1 through length($_subset){
$_temp: append($_temp, unquote(nth($_subset, $i)), comma);
}
}
@each $key, $value in $google-font-subsets {
@if $value { $_temp: append($_temp, unquote($key), comma); }
}
$_subset: __gf-list-unique($_temp);
// ------------------------------------------------
// Text
// ------------------------------------------------
// Remove duplicate characters and encode
//
@if $text {
$encode:('!':'%21','#':'%23','$':'%24','&':'%26','\'':'%27',
'(':'%28',')':'%29','*':'%2A','+':'%2B',',':'%2C',
'/':'%2F',':':'%3A',';':'%3B','=':'%3D','?':'%3F',
'@':'%40','[':'%5B',']':'%5D',' ':'%20');
$chars : __gf-list-unique(__gf-list-explode($text));
$text: '';
@for $i from 1 through length($chars){
$replace: map-get($encode, nth($chars,$i));
$text: $text + if($replace, $replace, nth($chars,$i));
}
}
// ------------------------------------------------
// Create single import query
// ------------------------------------------------
@if $__gf-combine == false or $text {
$query:'';
@for $i from 1 through length($_name){
$query: #{$query}#{if($i > 1,'|','')};
$query: #{$query}#{nth($_name, $i)};
// We don't need weight if we only request normal (400)
@if not(length($_weight) == 1 and nth($_weight, 1) == 400) {
$query: #{$query}#{':'+$_weight};
}
}
// We don't need subset when passing text
@if not $text {
$query: #{$query}#{if(length($_subset) > 1,#{'&subset='+$_subset},'')};
} @else {
$query: #{$query}#{'&text='+$text};
}
// Remove spaces
$query: __gf-str-replace($query, ' ','');
// Create single import
@import url(#{$URL}#{$query});
}
// ------------------------------------------------
// Push to font list to combine
// ------------------------------------------------
@else {
@for $i from 1 through length($_name){
$__gf-list: append($__gf-list, (name: nth($_name, $i), weight:$_weight, subset: $_subset), comma) !global;
}
}
}
// _________________________________________________________________
//
// Combined import
//
// _________________________________________________________________
@else {
// ------------------------------------------------
// Set combine marker and include nested
// content (now in a combine syntax)
// ------------------------------------------------
$__gf-combine: true !global;
@content;
// ------------------------------------------------
// Loop throug each item in the font list and
// merge values with already mapped values
// ------------------------------------------------
@if length($__gf-list) > 0 {
// Map to hold combined fonts
$combine-map:();
@for $i from 1 through length($__gf-list){
$font : nth($__gf-list, $i);
$name : map-get($font, name);
$mapped: map-get($combine-map, to-lower-case($name));
$_weight: join(if($mapped,map-get($mapped, weight),()), map-get($font, weight), comma);
$_subset: join(if($mapped,map-get($mapped, subset),()), map-get($font, subset), comma);
$combine-map: map-merge($combine-map, (to-lower-case($name):(name: $name, weight: __gf-list-unique($_weight), subset: __gf-list-unique($_subset))));
}
// ------------------------------------------------
// Create combined import query
// ------------------------------------------------
$query:'';
$subsets:();
@each $key, $font in $combine-map {
$_name : map-get($font, name);
$_weight: map-get($font, weight);
$_subset: map-get($font, subset);
$query: #{$query}#{if($query != '', '|', '')};
$query: #{$query}#{$_name};
@if not(length($_weight) == 1 and nth($_weight, 1) == 400) {
$query: #{$query}#{':'+$_weight};
}
$subsets: join($subsets, $_subset, comma);
}
// Remove redundant subsets and spaces
$subsets: __gf-list-unique($subsets);
$query: #{$query}#{if(length($subsets)>0, '&subset='+$subsets, '')};
$query: __gf-str-replace($query, ' ','');
// Create combined import
@import url(#{$URL}#{$query});
}
// ------------------------------------------------
// Reset combine marker and enpty font list
// ------------------------------------------------
$__gf-combine: false !global;
$__gf-list: () !global;
}
}
// Variables for internal use
$__gf-list:();
$__gf-combine: false;
// Helper functions (SassyLists ripoff)
@function ___gf-list-to-list($value, $separator: list-separator($value)) { @if type-of($value) != "list" or list-separator($value) != $separator { $new-list: if($separator == "comma", sl-comma-list(), ()); @each $item in $value { $new-list: append($new-list, $item, $separator); } @return $new-list; } @return $value; }
@function ___gf-list-unique($list) { $result: (); @each $item in $list { @if not index($result, $item) { $result: append($result, $item, list-separator($list)); }} @return ___gf-list-to-list($result); }
@function ___gf-list-explode($string, $delimiter: '', $separator: "space") { $result: (); $length: str-length($string); @if not index("space" "comma", $separator) { $separator: "space"; }@if str-length($delimiter) == 0 {@for $i from 1 through $length { $result: append($result, str-slice($string, $i, $i)); } @return $result; } $running: true; $remaining: $string; @while $running { $index: str-index($remaining, $delimiter); @if not $index { $running: false; } @else { $slice: str-slice($remaining, 1, $index - 1); $result: append($result, $slice, $separator); $remaining: str-slice($remaining, $index + str-length($delimiter)); } } @return append($result, $remaining, $separator); }
// Map helper functions
// If SassyLists are imported we use the real thing
@function __gf-str-replace($string, $search, $replace: '') {$index: str-index($string, $search); @if $index { @return str-slice($string, 1, $index - 1) + $replace + __gf-str-replace(str-slice($string, $index + str-length($search)), $search, $replace); }@return $string; }
@function __gf-list-unique($arglist...) { @return if(function-exists(sl-remove-duplicates), sl-remove-duplicates($arglist...), ___gf-list-unique($arglist...)); }
@function __gf-list-explode($arglist...){ @return if(function-exists(sl-explode), sl-explode($arglist...), ___gf-list-explode($arglist...)); }