Skip to content

Commit

Permalink
Set correct print labelAlign
Browse files Browse the repository at this point in the history
Default values in OpenLayers are 'center' and 'middle'.
  • Loading branch information
fredj committed Oct 10, 2017
1 parent 2f93fde commit 4ea9898
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
20 changes: 17 additions & 3 deletions src/services/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,25 @@ ngeo.Print.prototype.encodeTextStyle_ = function(symbolizers, textStyle) {
const label = textStyle.getText();
if (label !== undefined) {
symbolizer.label = label;
let xAlign = 'c';
let yAlign = 'm';

const olTextAlign = textStyle.getTextAlign();
// 'left', 'right', 'center', 'end' or 'start'.
if (olTextAlign === 'left' || olTextAlign === 'start') {
xAlign = 'l';
} else if (olTextAlign === 'right' || olTextAlign === 'end') {
xAlign = 'r';
}

const labelAlign = textStyle.getTextAlign();
if (labelAlign !== undefined) {
symbolizer.labelAlign = labelAlign;
const olTextBaseline = textStyle.getTextBaseline();
// 'bottom', 'top', 'middle', 'alphabetic', 'hanging' or 'ideographic'
if (olTextBaseline === 'bottom') {
yAlign = 'l';
} else if (olTextBaseline === 'top') {
yAlign = 't';
}
symbolizer.labelAlign = `${xAlign}${yAlign}`;

const labelRotation = textStyle.getRotation();
if (labelRotation !== undefined) {
Expand Down
9 changes: 6 additions & 3 deletions test/spec/services/print.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ describe('ngeo.CreatePrint', () => {
})
});

// Here to check that no offset are present if textAlign is not there.
// Here to check that textAlign default value is set.
style4 = new ol.style.Style({
text: new ol.style.Text({
font: 'normal 16px "sans serif"',
Expand Down Expand Up @@ -454,7 +454,7 @@ describe('ngeo.CreatePrint', () => {
fontSize: '16px',
fontFamily: '"sans serif"',
label: 'Ngeo',
labelAlign: 'left',
labelAlign: 'lm',
labelXOffset: 42,
labelYOffset: 42
}]
Expand All @@ -466,7 +466,10 @@ describe('ngeo.CreatePrint', () => {
fontWeight: 'normal',
fontSize: '16px',
fontFamily: '"sans serif"',
label: 'Ngeo'
label: 'Ngeo',
labelAlign: 'cm',
labelXOffset: 42,
labelYOffset: 42
}]
};

Expand Down

0 comments on commit 4ea9898

Please sign in to comment.