Jules Sam. Randolph ― v2.0.0 |
The way embedded are scaled greatly impacts library consumers experience. This document is an attempt at specifying behaviors which might not have been presented to library consumers in the past, to convey consistency with regard to this library behavior.
In this section, we introduce a context to understand the challenges surrounding scaling of embedded. In the bellow figure, a React Native layout sketch is presented with the following objects:
-
A Container element, which represents the component responsible for displaying HTML content;
-
A Embedded Box element, which is the renderer for the embedded HTML element (
img
,video
,iframe
…); -
An Print Layout, which represents the surface of the screen effectively covered by the embedded to display.
ℹ️
|
The relation between the dimensions of the Print Layout and the Image
Box Element is defined by resizeMode . The conditions under which these
properties can be passed is out of scope of this document.
|
The problem can be defined as follows:
How to determine Box Element width and height which satisfies best library consumers?
Which API would enable such capabilities?
Variable | Type | Definition |
---|---|---|
|
pixel |
The pixel width of the embedded. |
|
pixel |
The pixel height of the embedded. |
|
dpi |
The width of the container in which the HTML content is rendered. |
|
dpi |
|
image box constraints |
dpi |
See section bellow. |
Embedded Box Element Constrains are constructed from a range of different sources:
-
styles defined by library consumer for the embedded tag, or for a class assigned to this element;
-
inline styles of the element;
-
element attributes such as
width
andheight
.
After having applied the rules of specificity, we end up with a set of rules which are meant to influence the dimensions of the Embedded Box Element. For the sake of simplicity, we will ignore the following constraints:
-
margins and padding;
-
constraints expressed in relative units (%, vh);
-
constraints on parents in the DOM.
Therefore we have width
, height
, maxWidth
, maxHeight
, minWidth
and minHeight
, all expressed in dpi. For the sake of clarity, we
introduce new definitions:
-
The minimum constraints is the rectangle formed by
minWidth
andminHeight
. -
The maximum constraints is the rectangle formed by
maxWidth
andmaxHeight
. -
The required constraints is the rectangle formed by
width
andheight
.
Default minimum constraints are (0, 0) and default maximum constraints are (Infinity, Infinity).
These functions pertain to ``injected behaviors'' and give library consumers great flexibility in implementing their own constraints regarding embedded scaling logic.
Variable | Type | Definition |
---|---|---|
|
f(dpi, tagName) → dpi |
A function of |
|
f(u, val) → dpi |
A function which take a CSS unit and value for conversion to dpi. The details of challenges surrounding unit conversions will be the subject of a distinct RFC. |
These behaviors have default values for consumer’s comfort sake:
-
Default
computeEmbeddedMaxWidth
is defined as identity:(contentWidth) ⇒ contentWidth
; -
Default
convertUnit
will be defined in a distinct RFC.
Let box dimensions be the width
and height
of the Embedded Box
Element with each width and height undefined
.
-
If required constraints
width
andheight
are both defined, assign box dimensions to the value of required constraints.
Else if one of both dimensions of required constraints is defined, infer the other dimension from the original embedded aspect ratio (embeddedPhysicalHeight / embeddedPhysicalWidth
).
Else, assign box dimensions to the result of applyingconvertUnit
to bothembeddedPhysicalWidth
andembeddedPhysicalHeight
with a"px"
argument. -
Apply the Cut Box algorithm.
Let box dimensions be the width
and height
of the Embedded Box
Element an argument of this algorithm.
-
Apply minimum constraints and maximum constraints to box dimensions width and height whilst preserving aspect ratio.
-
If
computeEmbeddedMaxWidth
function is defined, apply max-width constrain to box dimensions whilst preserving aspect ratio.
Else, applycontentWidth
as a max-width constrain to box dimensions whilst preserving aspect ratio.
The consumer of the library might hold some assumptions depending on how he expects the content to be rendered. In reality, there are different approaches and library authors must take into account a variety of opinions:
-
Some consumers will expect embedded to never overflow the horizontal axis.
-
Other consumers will expect embedded to be rendered whilst honoring strictly the inline styles as dictated by HTML authors, even if that means overflowing the horizontal axis.
This solution can be satisfying for both types of consumers. The
advantages of computeEmbeddedMaxWidth
are multiple:
-
Enable an architecture ready for responsiveness. Screen orientation changes can be easily and deterministically handled.
-
Unopinionated: each consumer can implement the behavior he favors.
-
Extensible to any embedded contents.