Fix font rerender test when font is a system font
This commit is contained in:
BIN
test/browser/spec/ol/data/fonts/ubuntu-regular-webfont.woff2
Normal file
BIN
test/browser/spec/ol/data/fonts/ubuntu-regular-webfont.woff2
Normal file
Binary file not shown.
@@ -18,14 +18,20 @@ import {
|
|||||||
getWidth,
|
getWidth,
|
||||||
} from '../../../../../../src/ol/extent.js';
|
} from '../../../../../../src/ol/extent.js';
|
||||||
import {checkedFonts} from '../../../../../../src/ol/render/canvas.js';
|
import {checkedFonts} from '../../../../../../src/ol/render/canvas.js';
|
||||||
|
import {createFontStyle} from '../../../util.js';
|
||||||
import {fromExtent} from '../../../../../../src/ol/geom/Polygon.js';
|
import {fromExtent} from '../../../../../../src/ol/geom/Polygon.js';
|
||||||
import {get as getProjection} from '../../../../../../src/ol/proj.js';
|
import {get as getProjection} from '../../../../../../src/ol/proj.js';
|
||||||
|
|
||||||
describe('ol.renderer.canvas.VectorLayer', function () {
|
describe('ol.renderer.canvas.VectorLayer', function () {
|
||||||
describe('constructor', function () {
|
describe('constructor', function () {
|
||||||
const font = document.createElement('link');
|
const fontFamily = 'Ubuntu - VectorLayerTest';
|
||||||
font.href = 'https://fonts.googleapis.com/css?family=Droid+Sans';
|
const font = createFontStyle({
|
||||||
font.rel = 'stylesheet';
|
fontFamily: fontFamily,
|
||||||
|
src: {
|
||||||
|
url: '/spec/ol/data/fonts/ubuntu-regular-webfont.woff2',
|
||||||
|
format: 'woff2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
let target;
|
let target;
|
||||||
|
|
||||||
@@ -155,7 +161,7 @@ describe('ol.renderer.canvas.VectorLayer', function () {
|
|||||||
|
|
||||||
it('re-renders for fonts that become available', function (done) {
|
it('re-renders for fonts that become available', function (done) {
|
||||||
checkedFonts.values_ = {};
|
checkedFonts.values_ = {};
|
||||||
document.head.appendChild(font);
|
font.add();
|
||||||
const map = new Map({
|
const map = new Map({
|
||||||
view: new View({
|
view: new View({
|
||||||
center: [0, 0],
|
center: [0, 0],
|
||||||
@@ -166,7 +172,7 @@ describe('ol.renderer.canvas.VectorLayer', function () {
|
|||||||
const layerStyle = new Style({
|
const layerStyle = new Style({
|
||||||
text: new Text({
|
text: new Text({
|
||||||
text: 'layer',
|
text: 'layer',
|
||||||
font: '12px "Droid Sans",sans-serif',
|
font: `12px "${fontFamily}",sans-serif`,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -180,9 +186,13 @@ describe('ol.renderer.canvas.VectorLayer', function () {
|
|||||||
map.addLayer(layer);
|
map.addLayer(layer);
|
||||||
const revision = layer.getRevision();
|
const revision = layer.getRevision();
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
|
try {
|
||||||
|
font.remove();
|
||||||
expect(layer.getRevision()).to.be(revision + 1);
|
expect(layer.getRevision()).to.be(revision + 1);
|
||||||
document.head.removeChild(font);
|
|
||||||
done();
|
done();
|
||||||
|
} catch (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
}, 1600);
|
}, 1600);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import View from '../../../../../../src/ol/View.js';
|
|||||||
import XYZ from '../../../../../../src/ol/source/XYZ.js';
|
import XYZ from '../../../../../../src/ol/source/XYZ.js';
|
||||||
import {checkedFonts} from '../../../../../../src/ol/render/canvas.js';
|
import {checkedFonts} from '../../../../../../src/ol/render/canvas.js';
|
||||||
import {create} from '../../../../../../src/ol/transform.js';
|
import {create} from '../../../../../../src/ol/transform.js';
|
||||||
|
import {createFontStyle} from '../../../util.js';
|
||||||
import {createXYZ} from '../../../../../../src/ol/tilegrid.js';
|
import {createXYZ} from '../../../../../../src/ol/tilegrid.js';
|
||||||
import {getCenter} from '../../../../../../src/ol/extent.js';
|
import {getCenter} from '../../../../../../src/ol/extent.js';
|
||||||
import {get as getProjection} from '../../../../../../src/ol/proj.js';
|
import {get as getProjection} from '../../../../../../src/ol/proj.js';
|
||||||
@@ -24,9 +25,14 @@ import {getUid} from '../../../../../../src/ol/util.js';
|
|||||||
|
|
||||||
describe('ol/renderer/canvas/VectorTileLayer', function () {
|
describe('ol/renderer/canvas/VectorTileLayer', function () {
|
||||||
describe('constructor', function () {
|
describe('constructor', function () {
|
||||||
const font = document.createElement('link');
|
const fontFamily = 'Ubuntu - VectorTileLayerTest';
|
||||||
font.href = 'https://fonts.googleapis.com/css?family=Dancing+Script';
|
const font = createFontStyle({
|
||||||
font.rel = 'stylesheet';
|
fontFamily: fontFamily,
|
||||||
|
src: {
|
||||||
|
url: '/spec/ol/data/fonts/ubuntu-regular-webfont.woff2',
|
||||||
|
format: 'woff2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
let map,
|
let map,
|
||||||
layer,
|
layer,
|
||||||
@@ -199,14 +205,18 @@ describe('ol/renderer/canvas/VectorTileLayer', function () {
|
|||||||
it('re-renders for fonts that become available', function (done) {
|
it('re-renders for fonts that become available', function (done) {
|
||||||
map.renderSync();
|
map.renderSync();
|
||||||
checkedFonts.values_ = {};
|
checkedFonts.values_ = {};
|
||||||
document.head.appendChild(font);
|
font.add();
|
||||||
layerStyle[0].getText().setFont('12px "Dancing Script",sans-serif');
|
layerStyle[0].getText().setFont(`12px "${fontFamily}",sans-serif`);
|
||||||
layer.changed();
|
layer.changed();
|
||||||
const revision = layer.getRevision();
|
const revision = layer.getRevision();
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
document.head.removeChild(font);
|
try {
|
||||||
|
font.remove();
|
||||||
expect(layer.getRevision()).to.be(revision + 1);
|
expect(layer.getRevision()).to.be(revision + 1);
|
||||||
done();
|
done();
|
||||||
|
} catch (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
}, 1600);
|
}, 1600);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -14,3 +14,33 @@ export function overrideRAF() {
|
|||||||
window.cancelAnimationFrame = caf;
|
window.cancelAnimationFrame = caf;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createFontStyle(options) {
|
||||||
|
const styleNode = document.createElement('style');
|
||||||
|
const src = Array.isArray(options.src) ? options.src : [options.src];
|
||||||
|
function toCssSource(src) {
|
||||||
|
const url = typeof src === 'string' ? src : src.url;
|
||||||
|
const format = typeof src === 'string' ? undefined : src.format;
|
||||||
|
return `url('${url}')${format ? ` format('${format}')` : ''}`;
|
||||||
|
}
|
||||||
|
const ruleText = `
|
||||||
|
@font-face {
|
||||||
|
font-family: '${options.fontFamily}';
|
||||||
|
font-style: ${options.fontStyle || 'normal'};
|
||||||
|
font-weight: ${
|
||||||
|
options.fontWeight === undefined ? 400 : options.fontWeight
|
||||||
|
};
|
||||||
|
src: ${src.map(toCssSource).join(',\n ')};
|
||||||
|
}`;
|
||||||
|
return {
|
||||||
|
add() {
|
||||||
|
document.head.appendChild(styleNode);
|
||||||
|
if (styleNode.sheet.cssRules.length === 0) {
|
||||||
|
styleNode.sheet.insertRule(ruleText);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
remove() {
|
||||||
|
document.head.removeChild(styleNode);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user