maplibre / maplibre-style-spec

MapLibre Style Specification & Utilities
https://maplibre.org/maplibre-style-spec/
Other
74 stars 62 forks source link

Vertical alignment in format expression #832

Open stanislawpuda-tomtom opened 4 days ago

stanislawpuda-tomtom commented 4 days ago

Design Proposal: Add vertical alignment to format expression

Motivation

Format expression can facilitate multiple text and image elements of different sizes. At the moment all of them are aligned to the font baseline. It is not possible to change the vertical alignment. Having a possibility to change the alignment, eg. center, would allow us to create rich labels.

If following design would be accepted I would like to make contributions to this repo and MapLibre GL JS with the implementation.

Proposed Change

We would like to introduce vertical-align property to style override in format expression. vertical-align property can take three options:

Examples

Center

"text-field": [
  "format",
  ["image", "image"], 
  { "vertical-align": "center" },
  " ",
  {},
  "BIG yg",
  { "font-scale": 1.2, "vertical-align": "center" },
  " ",
  {},
  "SMALL",
  { "font-scale": 0.8, "vertical-align": "center" }
],

big_image_center small_image_center

Top

"text-field": [
  "format",
  ["image", "image"], 
  { "vertical-align": "top" },
  " ",
  {},
  "BIG yg",
  { "font-scale": 1.2, "vertical-align": "top" },
  " ",
  {},
  "SMALL",
  { "font-scale": 0.8, "vertical-align": "top" }
],

big_image_top small_image_top

Baseline (default, current behaviour)

"text-field": [
  "format",
  ["image", "image"], 
  {},
  " ",
  {},
  "BIG yg",
  { "font-scale": 1.2 },
  " ",
  {},
  "SMALL",
  { "font-scale": 0.8 }
],

big_image_baseline small_image_baseline

Mixed

"text-field": [
  "format",
  ["image", "image"], 
  {},
  " ",
  {},
  "BIG yg",
  { "font-scale": 1.2 },
  " ",
  {},
  "SMALL",
  { "font-scale": 0.8, "vertical-align": "center" }
],

big_image_mixed

API Modifications

Add new property to style override object of format expression:

vertical-align:

Behaviour

It is possible to set vertical-align for each element in format expression separately.

Migration Plan and Compatibility

There is no breaking change. Rendering of existing styles is unchanged. Only if style has new property vertical-align with non-default value, there is a difference in label rendering.

HarelM commented 4 days ago

Thanks for taking the time to open the proposal! What are the current alternatives available? My concern is the this is a bit two simplistic. What about if I want to move a single word two pixels upwards? What about padding between words? This might get very complex...

stanislawpuda-tomtom commented 3 days ago

I understand your concern. I believe what you've mention can be achieved by combining together additional property.

If we want to move single word a few pixels, we could add vertical-offset. Examples:

For the padding between words the easiest solution is to use spaces. I'm not sure if this requires another solution.

For the alternatives, we did think of numeric based approach, where value moves element up and down. There are two possibilities:

zbigniewmatysek-tomtom commented 3 days ago

I propose to rename this property, because it suggests it works like https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align, but in reality is similar to the https://developer.mozilla.org/en-US/docs/Web/CSS/align-self.

I belive showing the analogy to CSS properties can be helpful. It is easier to explain and adopt trough using existing terminology.

Regarding the @HarelM concern on the extendibility of this proposal. I think it's pretty extensible. One solution is having relative offset property that @stanislawpuda-tomtom explained or a different set of properties altogether that may or may not be combained with the vertical-align.

HarelM commented 10 hours ago

I would advise to bring this up in the monthly meeting to discuss this.