Repository navigation
Description
Images inside text-field
This RFC explores possibility of embedding images within a text defined by symbol layer's text-field property.
Style interface
Images within a text could be added via new image expression that takes one mandatory argument that must evaluate to string, for example ["image", "car-15"].
Within 'format' expression, optional image options object may be provided after 'image' expression. Image options properties could be:
height- sets height of an imagewidth- sets width of an imagescale- proportional scalingcolor- applicable for SDF iconssources- array of sources from where image would be tried to be fetched, like<img src=[url1, url2...]>(spritesheet, base url, etc). Example:
["spritesheet://icons", "https://www.mapbox.com/images/"]
Support for aforementioned image option properties may be added in the future if found useful.
Example syntax
The new image expression can be used as a section within text-field’s format expression, for instance:
"icon-image": "philadelphia-septa",
"text-field": ["format",
"London ",
["image", "gb-national-rail.london-tfl-rail.london-underground"],
" and Taipei ",
["image", "taipei-metro"], { "useful": "image option" }
" metro\n",
"SF ", {"text-color": "blue"},
["image", "san-francisco-bart"],
" bart"
]The new image expression could be represented by Image object ('coercable' to string if needed). The Image object would contain image id and image options. If needed, "icon-image" property could be converted to accept "image" expression, so that the text-field and icon-image properties would benefit from new functionality.
"icon-image": ["image", "philadelphia-septa"]

Activity
ryanhamley commentedon Aug 7, 2019
Just wanted to note that this is what we're proposing for default images in #8052 (comment)
asheemmamoowala commentedon Aug 7, 2019
@alexshalamov this is a really neat way to expand on the formatted text property to provide richer text composing and layout!
The timing of this couldn't be better, because there is alignment on adding an "image" expression operator in #8052 as well.
This has been discussed in the past and is a big change that affects not just the expression and style-spec, but also studio and offline functionality in gl-native.
Is the options argument needed for the images to be usable inside text fields? In the absence of options, would the images in text-fields always be scaled to fit within the line height?
When the text field is laid out vertically (either using
text-writing-mode: "vertical"orsymbol-placement: "line", will the image be laid out horizontally or vertically as well? It's not clear to me what the sensible default would be for this.cc @vakila @ansis @mapbox/studio @mapbox/map-design-team
alexshalamov commentedon Aug 7, 2019
@asheemmamoowala thanks for feedback! Few comments.
All properties for an options object could be added later if we find any of them useful for map designers. Initially, we could provide 'placeholder' in parser code. Parser would expect optional 'image options' object that in the future could contain new properties, therefore, we could guarantee style parsing backward / forward compatibility.
I would say yes, I would expect image with same options to produce similar rendering result.
Most rich-text editors would change line metrics if image with height that is larger than the line height is embedded. Another option is to proportionally scale image to the line height and if designers want image to be larger, scale option may be used in that case.
I would say that image should be in it's natural orientation, upright, in any writing mode. I will add note to myself to research if there are use-cases for rotated images in vertical writing modes.
asheemmamoowala commentedon Aug 7, 2019
The
imageoperator options will really only be useful in the usage inside aformattedtext-field. If used withicon-imageor other image properties, it would be confusing in conjunction with the other layout properties, for ex:icon-size.Would it work to put the image formatter options into the formatter object, similar to how
text-colorandtext-scaleare implemented for text sections?alexshalamov commentedon Aug 8, 2019
@asheemmamoowala Good point. Let's move options out of expression, so that it can be reused in 'icon-image'. I will update proposal accordingly.
Still, I would keep one optional 'object' (3rd argument) for an 'image' expression. Would be easy to add common options in the future, for example 'sources'.
asheemmamoowala commentedon Aug 26, 2019
@mapbox/studio @mapbox/map-design-team - any thoughts on this proposal?
[-][RFC] Images inside text-field content[/-][+][RFC] New feature: Images in labels[/+]chloekraw commentedon Oct 1, 2019
cc: mobile engineers in case you have any feedback -- we're planning to start implementation soon! @mapbox/maps-ios @mapbox/maps-android @1ec5
tobrun commentedon Oct 2, 2019
This is going to be great @alexshalamov, really like the api design.
ansis commentedon Oct 3, 2019
This looks great!
I agree that sources/width/height/color could be left out in the first version and considered separately so that they also work with icon-image.
In order to avoid reshaping the text on every frame the icon would need to scale together with the text. This means that text-size would affect the icon. How does this work? Let's say we have
"text-size": ["step", ["zoom"], 16, 5, 24]and a 10px image. How big would the image be rendered at zoom 4? at zoom 5?Sounds good to me
samanpwbb commentedon Oct 8, 2019
I reviewed this on behalf of the Studio team.
First of all, can you verify that the example has some errors? Here's what I would expect a valid example to look like (always require an options object, no arbitrary option keys):
Feedback:
sourcesfrom the main proposal here – Studio will likely never be able to support such an option due to it's CSP, for example.Additional questions:
formatto register as an image, does the outer expression need to be animageexpression, or can an expression that resolves toimagebe used? What happens in a case like this, where a decision expression either resolves to an image operator or not an image operator?:1ec5 commentedon Oct 31, 2019
This is a powerful feature and I’m excited to see it coming together. 🎉
So far all the examples I’ve seen demonstrate manually hard-coding images inside
text-field. But as I alluded to in mapbox/mapbox-gl-native#15666 (comment), it’s only a matter of time before developers would want the ability to systematically insert images into labels based on the contents of a feature property. The find-and-replace expression operator proposed in #4100 would enable the developer to substitute a placeholder token in a string with an image – probably a more palatable approach than making thenamefeature property more structured in the vector tiles.Whereas a standalone icon may be decorative when paired with a text label, an icon embedded within a label would tend to carry semantic meaning. Currently, the iOS map SDK and gl-accessibility plugin integrate with screen readers to read the
namefeature property aloud. Ideally they would instead read the evaluated value oftext-field, but iftext-fieldcontains non-decorative images, those images could need an alternative text representation.I wonder if this feature could be a solution for proper display of concurrent highway route shields. The existing icon symbol options are inadequate for concurrent shields, because conventionally they’re line-placed, offset from each other along the line, and always oriented upright with respect to the map:
Unfortunately, it isn’t currently possible to achieve the desired effect using
symbol-placement: line,icon/text-rotation-alignment: viewport, andicon/text-offset. The Mapbox Streets source has long worked around symbol layer limitations by combining multiple shields into one, baking two numbers into a single shield, but this approach has a number of aesthetic and practical downsides.The formatted-images-line rendering test in #8904 accomplishes the desired line placement and offset. The two missing pieces would be a way to:
Even if it isn’t the right time to support concurrent shields via this feature, I’m glad this feature will give us an easier way to put emoji-oids inside labels that doesn’t require overhauling the text rendering system. 😅
chloekraw commentedon Oct 31, 2019
@alexshalamov asked on the PR:
One suggestion:
I'm of the opinion that the image size has to scale with
text-size, or else the label would look weird, considering the zoom-dependent expression @ansis asked about. In this case:text-fieldto provide the appropriate size image?Pretty sure that, if you wanted to use a zoom-dependent expression for
text-sizeand a missing image intext-field, you would need the image size to scale withtext-sizein order for the image to be able to be the appropriate size for the text.The other use cases that come to mind are when a user needs the size of text on the map to change based on 1) accessibility settings or 2) differing aspect ratios and screen sizes. A user might deploy a single map simultaneously on small and large screens of varying sizes, e.g., in cars.
I'm not sure to what extent we support these use cases today or what users do in these situations, but I think that we shouldn't break those implementations, i.e., we shouldn't make a decision that would force those users to start managing multiple styles if they used this feature and wanted the inlined images to scale with the text.
So, I'm not so sure about lack of vector icon support forcing fixed image sizes in labels, even though I do think crisply-rendered images are important.
tristen commentedon Oct 31, 2019
I think images should not scale in relation to text size. Resolution should be maintained and a designer would upload a scaled version to match larger text if needed.
@1ec5 Thanks for bringing up the accessibility plugin. Should an image field support some sort of
aria-labelattribute for non-decorative images?alexshalamov commentedon Oct 31, 2019
@samanpwbb @tristen Thanks for feedback. At the moment I'm porting
imageoperator to native and integrating it withformatexpression. After that I can try rewriting bits that scale image size bytext-sizeproperty value.For data-driven and constant sizes it should be rather simple to implement. For zoom-dependent, I'm not sure what to do at the moment.
kkaefer commentedon Dec 5, 2019
This merged 🎉