Skip to content

Commit

Permalink
(fix): Use OL getFontParameters to parse font
Browse files Browse the repository at this point in the history
The fontFamily was incorrectly set to the full canvas font.
Ex: 10px sans-serif
See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/font
  • Loading branch information
gberaudo committed Jun 11, 2024
1 parent 8d1d2ba commit fd77483
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @geoblocks/geoblocks changes

## 0.2.15

- Use ol getFontParameters to parse font.

## 0.2.14

- Fix (invert) text Y axis offset.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@geoblocks/mapfishprint",
"version": "0.2.14",
"version": "0.2.15",
"publishConfig": {
"access": "public"
},
Expand Down
9 changes: 8 additions & 1 deletion src/VectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import type {Feature as GeoJSONFeature, FeatureCollection as GeoJSONFeatureColle
import {fromCircle} from 'ol/geom/Polygon.js';
import {Constants} from './constants';

import {getFontParameters} from 'ol/css.js';

/** Represents the different types of printing styles. */
export const PrintStyleType = {
LINE_STRING: 'LineString',
Expand Down Expand Up @@ -518,10 +520,15 @@ export default class VectorEncoder {
protected encodeVectorStyleText(symbolizers: MFPSymbolizer[], textStyle: Text) {
const label = textStyle.getText();
if (label) {
const fp = getFontParameters(textStyle.getFont() || 'sans-serif');
const symbolizer = {
type: 'text',
label: textStyle.getText(),
fontFamily: textStyle.getFont() ? textStyle.getFont() : 'sans-serif',
fontFamily: fp.family,
fontSize: fp.size,
fontStyle: fp.style,
fontWeight: fp.weight,
// FIXME: missing fontVariant, is it supported in MFP?
labelXOffset: textStyle.getOffsetX(),
// OL and MFP behaves differently on the Y offset
labelYOffset: -textStyle.getOffsetY(),
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface MFPSymbolizerText extends MFPSymbolizer, MFPFillStyle {
type: 'text';
fontColor: string;
fontFamily: string;
fontSize: number;
fontSize: string; // ex 12px
fontStyle: string;
fontWeight: string;
haloColor: string;
Expand Down
20 changes: 16 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ test('Vector features', async (t) => {
{
type: 'text',
label: 'A polygon',
fontFamily: '12px sans-serif',
fontFamily: 'sans-serif',
fontSize: '12px',
fontStyle: 'normal',
fontWeight: 'normal',
labelXOffset: 0,
labelYOffset: 12,
labelAlign: 'cm',
Expand All @@ -264,7 +267,10 @@ test('Vector features', async (t) => {
{
type: 'text',
label: 'A circle',
fontFamily: '12px sans-serif',
fontFamily: 'sans-serif',
fontSize: '12px',
fontStyle: 'normal',
fontWeight: 'normal',
labelXOffset: 0,
labelYOffset: 12,
labelAlign: 'cm',
Expand All @@ -285,7 +291,10 @@ test('Vector features', async (t) => {
{
type: 'text',
label: 'A line',
fontFamily: '12px sans-serif',
fontFamily: 'sans-serif',
fontSize: '12px',
fontStyle: 'normal',
fontWeight: 'normal',
labelXOffset: 0,
labelYOffset: 12,
labelAlign: 'cm',
Expand All @@ -300,7 +309,10 @@ test('Vector features', async (t) => {
{
type: 'text',
label: 'A point',
fontFamily: '12px sans-serif',
fontFamily: 'sans-serif',
fontSize: '12px',
fontStyle: 'normal',
fontWeight: 'normal',
labelXOffset: 0,
labelYOffset: 12,
labelAlign: 'cm',
Expand Down

0 comments on commit fd77483

Please sign in to comment.