-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use TinySDF to render CJK glyphs locally #4895
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91447c4
Add 'localIdeographFontFamily' option to side-step expensive glyph lo…
ChrisLoer e923805
Basic GlyphSource unit test.
ChrisLoer 9e76666
Chinese-language debugging view for TinySDF glyph generation.
ChrisLoer c419cae
Add a 'localIdeographFontFamily' example.
ChrisLoer 15fec87
Replace local Chinese Streets style with a public streets style overr…
ChrisLoer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Mapbox GL JS debug page</title> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<link rel='stylesheet' href='/dist/mapbox-gl.css' /> | ||
<style> | ||
body { margin: 0; padding: 0; } | ||
html, body, #originalMap, #newMap { height: 100%; } | ||
#originalMap, #newMap { width: 49%; } | ||
|
||
#originalLabel { | ||
position: absolute; | ||
background: #fff; | ||
top:0; | ||
left:0; | ||
padding:10px; | ||
} | ||
|
||
#newLabel { | ||
position: absolute; | ||
background: #fff; | ||
top:0; | ||
right:0; | ||
padding:10px; | ||
} | ||
|
||
#checkboxes { | ||
position: absolute; | ||
background: #fff; | ||
bottom:35px; | ||
left:0; | ||
padding:10px; | ||
} | ||
|
||
</style> | ||
</head> | ||
|
||
<body> | ||
|
||
<div id='originalMap' style="float: left"></div> | ||
<div id='newMap' style="float: right"></div> | ||
<label id='originalLabel'>Original Map</label> | ||
<label id='newLabel'>New Map</label> | ||
<div id='checkboxes'> | ||
<label><input id='show-overdraw-checkbox' type='checkbox'> overdraw debug</label><br /> | ||
</div> | ||
|
||
<script src='/dist/mapbox-gl-dev.js'></script> | ||
<script src='/debug/access_token_generated.js'></script> | ||
<script> | ||
|
||
function localizeLayers(map) { | ||
map.on('load', function() { | ||
var style = map.getStyle(); | ||
for (var layerID in style.layers) { | ||
var layer = style.layers[layerID]; | ||
if (layer.layout && layer.layout['text-field'] === "{name_en}") { | ||
layer.layout['text-field'] = "{name}"; | ||
} | ||
} | ||
map.setStyle(style, map.style.glyphSource.localIdeographFontFamily); | ||
}); | ||
} | ||
|
||
var originalMap = window.originalMap = new mapboxgl.Map({ | ||
container: 'originalMap', | ||
zoom: 8.8, | ||
center: [121.574, 31.1489], | ||
style: 'mapbox://styles/mapbox/streets-v10', | ||
hash: true | ||
}); | ||
|
||
localizeLayers(originalMap); | ||
|
||
var newMap = window.newMap = new mapboxgl.Map({ | ||
container: 'newMap', | ||
zoom: 8.8, | ||
center: [121.574, 31.1489], | ||
style: 'mapbox://styles/mapbox/streets-v10', | ||
localIdeographFontFamily: '"Noto Sans", "Noto Sans CJK SC", sans-serif', | ||
hash: true | ||
}); | ||
|
||
localizeLayers(newMap); | ||
|
||
document.getElementById('show-overdraw-checkbox').onclick = function() { | ||
originalMap.showOverdrawInspector = !!this.checked; | ||
newMap.showOverdrawInspector = !!this.checked; | ||
}; | ||
|
||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
layout: example | ||
category: example | ||
title: Use locally generated ideographs | ||
description: Rendering Chinese/Japanese/Korean (CJK) ideographs and precomposed Hangul Syllables requires downloading large amounts of font data, which can significantly slow map load times. Use the 'localIdeographFontFamily' setting to speed up map load times by using locally available fonts instead of font data fetched from the server. This setting defines a CSS 'font-family' for locally overriding generation of glyphs in the 'CJK Unified Ideographs' and 'Hangul Syllables' Unicode ranges. In these ranges, font settings from the map's style will be ignored in favor of the locally available font. Keywords in the fontstack defined in the map's style (light/regular/medium/bold) will be translated into a CSS 'font-weight'. When using this setting, keep in mind that the fonts you select may not be available on all users' devices. It is best to specify at least one broadly available fallback font class such as 'sans-serif'. | ||
tags: | ||
- internationalization | ||
--- | ||
<div id='map'></div> | ||
|
||
<script> | ||
|
||
var map = new mapboxgl.Map({ | ||
container: 'map', | ||
style: 'mapbox://styles/mapbox/streets-v9', | ||
center: [120.3049, 31.4751], | ||
zoom: 12, | ||
localIdeographFontFamily: "'Noto Sans', 'Noto Sans CJK SC', sans-serif" | ||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const test = require('mapbox-gl-js-test').test; | ||
const GlyphSource = require('../../../src/symbol/glyph_source'); | ||
const fs = require('fs'); | ||
|
||
const mockTinySDF = { | ||
// Return empty 30x30 bitmap (24 fontsize + 3 * 2 buffer) | ||
draw: function () { return new Uint8ClampedArray(900); } | ||
}; | ||
|
||
function createSource(t, localIdeographFontFamily) { | ||
const aPBF = fs.readFileSync('./test/fixtures/0-255.pbf'); | ||
const source = new GlyphSource("https://localhost/fonts/v1{fontstack}/{range}.pbf", localIdeographFontFamily); | ||
t.stub(source, 'createTinySDF').returns(mockTinySDF); | ||
// It would be better to mock with FakeXMLHttpRequest, but the binary encoding | ||
// doesn't survive the mocking | ||
source.loadPBF = function(url, callback) { | ||
callback(null, { data: aPBF }); | ||
}; | ||
|
||
return source; | ||
} | ||
|
||
|
||
test('GlyphSource', (t) => { | ||
t.test('requests 0-255 PBF', (t) => { | ||
const source = createSource(t); | ||
source.getSimpleGlyphs("Arial Unicode MS", [55], 0, (err, glyphs, fontName) => { | ||
t.notOk(err); | ||
t.equal(fontName, "Arial Unicode MS"); | ||
t.equal(glyphs['55'].advance, 12); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
t.test('requests remote CJK PBF', (t) => { | ||
const source = createSource(t); | ||
source.getSimpleGlyphs("Arial Unicode MS", [0x5e73], 0, (err, glyphs, fontName) => { | ||
t.notOk(err); | ||
t.equal(fontName, "Arial Unicode MS"); | ||
t.notOk(Object.keys(glyphs).length); // The fixture returns a PBF without the glyph we requested | ||
t.end(); | ||
}); | ||
|
||
}); | ||
|
||
t.test('locally generates CJK PBF', (t) => { | ||
const source = createSource(t, 'sans-serif'); | ||
source.getSimpleGlyphs("Arial Unicode MS", [0x5e73], 0, (err, glyphs, fontName) => { | ||
t.notOk(err); | ||
t.equal(fontName, "Arial Unicode MS"); | ||
t.equal(glyphs['24179'].advance, 24); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move the
fontWeight
checks up togetSimpleGlyphs
so that they're not repeated unnecessarily. Also, you could move out regexps into top-level constants (const boldRe = /bold/i
) so that they're not recreated on every check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks are only done once per unique fontstack, so probably only a handful of times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right. Missed that.