Skip to content

Commit

Permalink
Fix parsing of css font strings
Browse files Browse the repository at this point in the history
  • Loading branch information
weskamm committed Mar 24, 2023
1 parent 53b20b1 commit 08389c7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
4 changes: 3 additions & 1 deletion data/styles/point_styledLabel_static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const pointStyledLabel: Style = {
offset: [0, 5],
haloColor: '#000000',
haloWidth: 5,
rotate: 45
rotate: 45,
fontStyle: 'normal',
fontWeight: 'normal'
}]
}
]
Expand Down
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"color-name": "^1.1.4",
"css-font-parser": "^2.0.0",
"geostyler-style": "^7.2.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/OlStyleParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ describe('OlStyleParser implements StyleParser', () => {
const fontFamily = [
['arial', 'sans-serif'],
['Georgia', 'serif'],
['"Neue Helvetica"', 'Helvetica', 'sans-serif']
['Neue Helvetica', 'Helvetica', 'sans-serif']
];
const font1 = `bold 5px ${fontFamily[0].join(', ')}`;
const font2 = `italic bold 12px/30px ${fontFamily[1].join(', ')}`;
Expand Down
33 changes: 19 additions & 14 deletions src/OlStyleParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { parseFont } from 'css-font-parser';

import {
CapType,
FillSymbolizer,
Expand Down Expand Up @@ -403,24 +405,25 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
const allowOverlap = olTextStyle.getOverflow() ? olTextStyle.getOverflow() : undefined;
const text = olTextStyle.getText();
const label = Array.isArray(text) ? text[0] : text;
let fontStyleWeightSize: string;
let fontSizePart: string[];
let fontSize: number = Infinity;
let fontFamily: string[]|undefined = undefined;
let fontWeight: 'normal' | 'bold' = 'normal';
let fontStyle: 'normal' | 'italic' | 'oblique' = 'normal';

if (font) {
const fontSplit = font.split('px');
// font-size is always the first part of font-size/line-height
fontStyleWeightSize = fontSplit[0].trim();

fontSizePart = fontStyleWeightSize.split(' ');
// The last element contains font size
fontSize = parseInt(fontSizePart[fontSizePart.length - 1], 10);
const fontFamilyPart: string = fontSplit.length === 2 ?
fontSplit[1] : fontSplit[2];
fontFamily = fontFamilyPart.split(',').map((fn: string) => {
return fn.startsWith(' ') ? fn.slice(1) : fn;
});
const fontObj = parseFont(font);
if (fontObj['font-weight']) {
fontWeight = fontObj['font-weight'] as 'normal' | 'bold';
}
if (fontObj['font-size']) {
fontSize = parseInt(fontObj['font-size'], 10);
}
if (fontObj['font-family']) {
fontFamily = fontObj['font-family'];
}
if (fontObj['font-style']) {
fontStyle = fontObj['font-style'] as 'normal' | 'italic' | 'oblique';
}
}

return {
Expand All @@ -430,6 +433,8 @@ export class OlStyleParser implements StyleParser<OlStyleLike> {
color: olFillStyle ? OlStyleUtil.getHexColor(olFillStyle.getColor() as string) : undefined,
size: isFinite(fontSize) ? fontSize : undefined,
font: fontFamily,
fontWeight,
fontStyle,
offset: (offsetX !== undefined) && (offsetY !== undefined) ? [offsetX, offsetY] : [0, 0],
haloColor: olStrokeStyle && olStrokeStyle.getColor() ?
OlStyleUtil.getHexColor(olStrokeStyle.getColor() as string) : undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/Util/OlStyleUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class OlStyleUtil {

const size = symbolizer.size;
const font = symbolizer.font;
return fontWeight + ' ' + fontStyle + ' ' + size + 'px ' + font;
return fontWeight + ' ' + fontStyle + ' ' + size + 'px ' + font?.join(', ');
}


Expand Down

0 comments on commit 08389c7

Please sign in to comment.