-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
signed distance field font rendering
fixes #19 this requires https://github.com/mapbox/fontserver as a backend as it relies on glyph images embedded into the tiles
- Loading branch information
Showing
23 changed files
with
1,544 additions
and
39,843 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function BinPack(width, height) { | ||
this.width = width; | ||
this.height = height; | ||
this.free = [{ x: 0, y: 0, w: width, h: height }]; | ||
this.index = {}; | ||
} | ||
|
||
BinPack.prototype.release = function(rect) { | ||
this.free.push({ x: rect.x, y: rect.y, w: width, h: height }); | ||
// TODO: Try to merge with neighboring segments? | ||
}; | ||
|
||
BinPack.prototype.allocate = function(width, height) { | ||
// Find the smallest free rect angle | ||
var rect = { x: Infinity, y: Infinity, w: Infinity, h: Infinity }; | ||
var smallest = -1; | ||
for (var i = 0; i < this.free.length; i++) { | ||
var ref = this.free[i]; | ||
if (width <= ref.w && height <= ref.h && ref.y <= rect.y && ref.x <= rect.x) { | ||
rect = ref; | ||
smallest = i; | ||
} | ||
} | ||
|
||
if (smallest < 0) { | ||
// There's no space left for this char. | ||
return { x: -1, y: -1 }; | ||
} else { | ||
this.free.splice(smallest, 1); | ||
|
||
// Shorter/Longer Axis Split Rule (SAS) | ||
// http://clb.demon.fi/files/RectangleBinPack.pdf p. 15 | ||
// Ignore the dimension of R and just split long the shorter dimension | ||
// See Also: http://www.cs.princeton.edu/~chazelle/pubs/blbinpacking.pdf | ||
if (rect.w < rect.h) { | ||
// split horizontally | ||
// +--+---+ | ||
// |__|___| <-- b1 | ||
// +------+ <-- b2 | ||
if (rect.w > width) this.free.push({ x: rect.x + width, y: rect.y, w: rect.w - width, h: height }); | ||
if (rect.h > height) this.free.push({ x: rect.x, y: rect.y + height, w: rect.w, h: rect.h - height }); | ||
} else { | ||
// split vertically | ||
// +--+---+ | ||
// |__| | <-- b1 | ||
// +--|---+ <-- b2 | ||
if (rect.w > width) this.free.push({ x: rect.x + width, y: rect.y, w: rect.w - width, h: rect.h }); | ||
if (rect.h > height) this.free.push({ x: rect.x, y: rect.y + height, w: width, h: rect.h - height }); | ||
} | ||
|
||
return { x: rect.x, y: rect.y, w: width, h: height }; | ||
} | ||
}; |
Oops, something went wrong.