Skip to content

Commit 4941412

Browse files
authoredMay 30, 2017
Adding support for customizing the location of the font download (microsoft#1883)
* Adding support for defining custom locations to the fonts (or preventing the fontfaces from being registered alltogether.) * Change files. * Updating supported node range. * Going back to node v6.

File tree

6 files changed

+88
-29
lines changed

6 files changed

+88
-29
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- 'node'
3+
- '6'
44
before_script:
55
- npm install -g gulp
66
script:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@uifabric/styling",
5+
"comment": "Adding support for specifying `window.FabricConfig.fontBaseUrl` in order to customize where the font resources are pulled from. Leaving it blank will avoid fontface definitions from being registered.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@uifabric/styling",
10+
"email": "dzearing@microsoft.com"
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"packageName": "office-ui-fabric-react",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "office-ui-fabric-react",
10+
"email": "dzearing@microsoft.com"
11+
}

‎packages/office-ui-fabric-react/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
<body>
1414
<div id="content"></div>
1515
<script type="text/javascript">
16+
window.FabricConfig = {
17+
// Uncommenting this prevents the fontfaces from being registered.
18+
// Alternatively you can specify a base url which contains the font/icon assets.
19+
// fontBaseUrl: ''
20+
};
21+
1622
var isProduction = (
1723
document.location.search.indexOf('?min=0') === -1 && (
1824
document.location.search.indexOf('?min=1') > -1 ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The interface of window.FabricConfig.
3+
*/
4+
export interface IFabricConfig {
5+
fontBaseUrl?: string;
6+
}

‎packages/styling/src/styles/DefaultFontStyles.ts

+53-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { IFontStyles, IRawStyle } from '../interfaces/index';
22
import { fontFace } from '../glamorExports';
33
import {
4-
getLanguage
4+
getLanguage,
5+
getWindow
56
} from '@uifabric/utilities/lib/index';
7+
import { IFabricConfig } from '../interfaces/IFabricConfig';
68

79
// Default urls.
8-
const DefaultFontUrl = 'https://static2.sharepointonline.com/files/fabric/assets/fonts';
9-
const DefaultIconUrl = 'https://static2.sharepointonline.com/files/fabric/assets/icons';
10+
const DefaultBaseUrl = 'https://static2.sharepointonline.com/files/fabric/assets';
1011

1112
// Fallback fonts, if specified system or web fonts are unavailable.
1213
const FontFamilyFallbacks = `-apple-system, BlinkMacSystemFont, 'Roboto', 'Helvetica Neue', sans-serif`;
@@ -128,9 +129,9 @@ function _getFontFamily(): string {
128129
function _createFont(size: string, weight: number): IRawStyle {
129130
return {
130131
fontFamily: _getFontFamily(),
131-
'-moz-osx-font-smoothing': 'grayscale',
132-
'-ms-high-contrast-adjust': 'none',
133-
'-webkit-font-smoothing': 'antialiased',
132+
MozOsxFontSmoothing: 'grayscale',
133+
MsHighContrastAdjust: 'none',
134+
WebkitFontSmoothing: 'antialiased',
134135
fontSize: size,
135136
fontWeight: weight
136137
};
@@ -154,11 +155,12 @@ function _registerFontFace(
154155
}
155156

156157
function _registerFontFaceSet(
158+
baseUrl: string,
157159
fontFamily: string,
158160
cdnFolder: string,
159161
cdnFontName: string = 'segoeui'
160162
): void {
161-
const urlBase = `${DefaultFontUrl}/${cdnFolder}/${cdnFontName}`;
163+
const urlBase = `${baseUrl}/${cdnFolder}/${cdnFontName}`;
162164

163165
_registerFontFace(fontFamily, urlBase + '-light', FontWeights.light);
164166
_registerFontFace(fontFamily, urlBase + '-semilight', FontWeights.semilight);
@@ -167,26 +169,49 @@ function _registerFontFaceSet(
167169
}
168170

169171
function _registerDefaultFontFaces(): void {
170-
// Produce @font-face definitions for all supported web fonts.
171-
_registerFontFaceSet(FontNameThai, 'leelawadeeui-thai', 'leelawadeeui');
172-
_registerFontFaceSet(FontNameArabic, 'segoeui-arabic');
173-
_registerFontFaceSet(FontNameCyrillic, 'segoeui-cyrillic');
174-
_registerFontFaceSet(FontNameEastEuropean, 'segoeui-easteuropean');
175-
_registerFontFaceSet(FontNameGreek, 'segoeui-greek');
176-
_registerFontFaceSet(FontNameHebrew, 'segoeui-hebrew');
177-
_registerFontFaceSet(FontNameVietnamese, 'segoeui-vietnamese');
178-
_registerFontFaceSet(FontNameWestEuropean, 'segoeui-westeuropean');
179-
_registerFontFaceSet(FontFamilySelawik, 'selawik', 'selawik');
180-
181-
// Leelawadee UI (Thai) does not have a 'light' weight, so we override
182-
// the font-face generated above to use the 'semilight' weight instead.
183-
_registerFontFace('Leelawadee UI Web', `${DefaultFontUrl}/leelawadeeui-thai/leelawadeeui-semilight`, FontWeights.light);
184-
185-
// Leelawadee UI (Thai) does not have a 'semibold' weight, so we override
186-
// the font-face generated above to use the 'bold' weight instead.
187-
_registerFontFace('Leelawadee UI Web', `${DefaultFontUrl}/leelawadeeui-thai/leelawadeeui-bold`, FontWeights.semibold);
188-
189-
_registerFontFace('FabricMDL2Icons', DefaultIconUrl + '/fabricmdl2icons', FontWeights.regular);
172+
const baseUrl = _getFontBaseUrl();
173+
174+
if (baseUrl) {
175+
const fontUrl = `${baseUrl}/fonts`;
176+
const iconUrl = `${baseUrl}/icons`;
177+
178+
// Produce @font-face definitions for all supported web fonts.
179+
_registerFontFaceSet(fontUrl, FontNameThai, 'leelawadeeui-thai', 'leelawadeeui');
180+
_registerFontFaceSet(fontUrl, FontNameArabic, 'segoeui-arabic');
181+
_registerFontFaceSet(fontUrl, FontNameCyrillic, 'segoeui-cyrillic');
182+
_registerFontFaceSet(fontUrl, FontNameEastEuropean, 'segoeui-easteuropean');
183+
_registerFontFaceSet(fontUrl, FontNameGreek, 'segoeui-greek');
184+
_registerFontFaceSet(fontUrl, FontNameHebrew, 'segoeui-hebrew');
185+
_registerFontFaceSet(fontUrl, FontNameVietnamese, 'segoeui-vietnamese');
186+
_registerFontFaceSet(fontUrl, FontNameWestEuropean, 'segoeui-westeuropean');
187+
_registerFontFaceSet(fontUrl, FontFamilySelawik, 'selawik', 'selawik');
188+
189+
// Leelawadee UI (Thai) does not have a 'light' weight, so we override
190+
// the font-face generated above to use the 'semilight' weight instead.
191+
_registerFontFace('Leelawadee UI Web', `${fontUrl}/leelawadeeui-thai/leelawadeeui-semilight`, FontWeights.light);
192+
193+
// Leelawadee UI (Thai) does not have a 'semibold' weight, so we override
194+
// the font-face generated above to use the 'bold' weight instead.
195+
_registerFontFace('Leelawadee UI Web', `${fontUrl}/leelawadeeui-thai/leelawadeeui-bold`, FontWeights.semibold);
196+
197+
// Register icon urls.
198+
_registerFontFace('FabricMDL2Icons', `${iconUrl}/fabricmdl2icons`, FontWeights.regular);
199+
}
200+
}
201+
202+
/**
203+
* Reads the fontBaseUrl from window.FabricConfig.fontBaseUrl or falls back to a default.
204+
*/
205+
function _getFontBaseUrl(): string {
206+
let win = getWindow();
207+
208+
// tslint:disable-next-line:no-string-literal
209+
let fabricConfig: IFabricConfig = win ? win['FabricConfig'] : undefined;
210+
211+
return (fabricConfig && fabricConfig.fontBaseUrl !== undefined) ? fabricConfig.fontBaseUrl : DefaultBaseUrl;
190212
}
191213

192-
_registerDefaultFontFaces();
214+
/**
215+
* Register the font faces.
216+
*/
217+
_registerDefaultFontFaces();

0 commit comments

Comments
 (0)
Please sign in to comment.