Skip to content

Commit

Permalink
Quick/dirty fix: apply rotation after text-offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Jun 2, 2017
1 parent 943dc39 commit 8c55a7e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 24 deletions.
57 changes: 35 additions & 22 deletions src/symbol/quads.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ function getIconQuads(anchor, shapedIcon, boxScale, line, layer, alongLine, shap
*/
function getGlyphQuads(anchor, shaping, boxScale, line, layer, alongLine, globalProperties, featureProperties) {

const oneEm = 24;
const textRotate = layer.getLayoutValue('text-rotate', globalProperties, featureProperties) * Math.PI / 180;
const keepUpright = layer.layout['text-keep-upright'];
const textOffset = layer.getLayoutValue('text-offset', globalProperties, featureProperties).map((t)=> t * oneEm);

const positionedGlyphs = shaping.positionedGlyphs;
const quads = [];
Expand Down Expand Up @@ -189,32 +191,19 @@ function getGlyphQuads(anchor, shaping, boxScale, line, layer, alongLine, global
}];
}

const x1 = positionedGlyph.x + glyph.left;
const y1 = positionedGlyph.y - glyph.top;
const x2 = x1 + rect.w;
const y2 = y1 + rect.h;

const center = new Point(positionedGlyph.x, glyph.advance / 2);

const otl = new Point(x1, y1);
const otr = new Point(x2, y1);
const obl = new Point(x1, y2);
const obr = new Point(x2, y2);

if (positionedGlyph.angle !== 0) {
otl._sub(center)._rotate(positionedGlyph.angle)._add(center);
otr._sub(center)._rotate(positionedGlyph.angle)._add(center);
obl._sub(center)._rotate(positionedGlyph.angle)._add(center);
obr._sub(center)._rotate(positionedGlyph.angle)._add(center);
}
const baseQuad = {
upright: calculateBaseQuad(positionedGlyph, glyph, rect, textOffset),
upsideDown: calculateBaseQuad(positionedGlyph, glyph, rect, [textOffset[0], -textOffset[1]])
};

for (let i = 0; i < glyphInstances.length; i++) {

const instance = glyphInstances[i];
let tl = otl,
tr = otr,
bl = obl,
br = obr;
const base = baseQuad[instance.upsideDown ? 'upsideDown' : 'upright'];
let tl = base.tl,
tr = base.tr,
bl = base.bl,
br = base.br;

if (textRotate) {
const sin = Math.sin(textRotate),
Expand All @@ -240,6 +229,30 @@ function getGlyphQuads(anchor, shaping, boxScale, line, layer, alongLine, global
return quads;
}

function calculateBaseQuad(positionedGlyph, glyph, rect, offset) {
const x1 = positionedGlyph.x + glyph.left + offset[0];
const y1 = positionedGlyph.y - glyph.top + offset[1];
const x2 = x1 + rect.w;
const y2 = y1 + rect.h;

const center = new Point(positionedGlyph.x, glyph.advance / 2);

const tl = new Point(x1, y1);
const tr = new Point(x2, y1);
const bl = new Point(x1, y2);
const br = new Point(x2, y2);

if (positionedGlyph.angle !== 0) {
tl._sub(center)._rotate(positionedGlyph.angle)._add(center);
tr._sub(center)._rotate(positionedGlyph.angle)._add(center);
bl._sub(center)._rotate(positionedGlyph.angle)._add(center);
br._sub(center)._rotate(positionedGlyph.angle)._add(center);
}

return { tl, tr, bl, br };
}


/**
* We can only render glyph quads that slide along a straight line. To draw
* curved lines we need an instance of a glyph for each segment it appears on.
Expand Down
5 changes: 3 additions & 2 deletions src/symbol/shaping.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,9 @@ function justifyLine(positionedGlyphs, glyphs, start, end, justify) {
}

function align(positionedGlyphs, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, lineCount, translate) {
const shiftX = (justify - horizontalAlign) * maxLineLength + translate[0];
const shiftY = (-verticalAlign * lineCount + 0.5) * lineHeight + translate[1];
// TODO: moving translate part to quads.js
const shiftX = (justify - horizontalAlign) * maxLineLength;// + translate[0];
const shiftY = (-verticalAlign * lineCount + 0.5) * lineHeight;// + translate[1];

for (let j = 0; j < positionedGlyphs.length; j++) {
positionedGlyphs[j].x += shiftX;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"version": 8,
"metadata": {
"test": {
"height": 64,
"operations": [
[ "setBearing", 20 ],
[ "wait" ]
]
}
},
"center": [ 0, 0 ],
"zoom": 0,
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-20, -20],
[20, -20],
[20, 20],
[-20, 20]
]
}
}
}
},
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
"layers": [
{
"id": "guid",
"type": "line",
"source": "geojson"
},
{
"id": "text",
"type": "symbol",
"source": "geojson",
"layout": {
"symbol-placement": "line",
"symbol-spacing": 3,
"text-allow-overlap": true,
"text-ignore-placement": true,
"text-field": "A",
"text-font": [
"Open Sans Semibold",
"Arial Unicode MS Bold"
],
"text-keep-upright": true,
"text-offset": [0, -1]
}
}
]
}

0 comments on commit 8c55a7e

Please sign in to comment.