@dcl/react-ecs
Version:
Decentraland ECS
70 lines (69 loc) • 2.16 kB
JavaScript
import { calcOnViewport, getScaleCtx } from '../utils';
const parseFont = {
'sans-serif': 0 /* Font.F_SANS_SERIF */,
serif: 1 /* Font.F_SERIF */,
monospace: 2 /* Font.F_MONOSPACE */
};
/**
* @internal
*/
export function getFont(font) {
if (!font)
return undefined;
return { font: parseFont[font] };
}
const parseTextAlign = {
'top-left': 0 /* TextAlignMode.TAM_TOP_LEFT */,
'top-center': 1 /* TextAlignMode.TAM_TOP_CENTER */,
'top-right': 2 /* TextAlignMode.TAM_TOP_RIGHT */,
'middle-left': 3 /* TextAlignMode.TAM_MIDDLE_LEFT */,
'middle-center': 4 /* TextAlignMode.TAM_MIDDLE_CENTER */,
'middle-right': 5 /* TextAlignMode.TAM_MIDDLE_RIGHT */,
'bottom-left': 6 /* TextAlignMode.TAM_BOTTOM_LEFT */,
'bottom-center': 7 /* TextAlignMode.TAM_BOTTOM_CENTER */,
'bottom-right': 8 /* TextAlignMode.TAM_BOTTOM_RIGHT */
};
/**
* @internal
*/
export function getTextAlign(textAlign) {
if (!textAlign)
return undefined;
return { textAlign: parseTextAlign[textAlign] };
}
/**
* @internal
*/
export function getFontSize(fontSize) {
if (!fontSize)
return undefined;
if (typeof fontSize === 'string')
return { fontSize: calcOnViewport(fontSize) };
return { fontSize };
}
/**
* Scales a font size depending on a context's width/height
* @param {number} fontSize size of the font to scale
* @param {ScaleUnit} [scaleUnit=0.39] the scaling unit (uses "width" as unit if a number is supplied)
* @param {ScaleContext} [ctx=viewport] the context where to apply the scaling
* @returns {number} the fontSize scaled
* @see https://matthewjamestaylor.com/responsive-font-size#fluid
* @public
*/
export function scaleFontSize(fontSize, scaleUnit = 0.39, ctx = getScaleCtx()) {
if (!ctx)
return fontSize;
return fontSize + calcOnViewport(scaleUnit, ctx);
}
const parseTextWrap = {
wrap: 0 /* TextWrap.TW_WRAP */,
nowrap: 1 /* TextWrap.TW_NO_WRAP */
};
/**
* @internal
*/
export function getTextWrap(textWrap) {
if (!textWrap)
return { textWrap: 0 /* TextWrap.TW_WRAP */ };
return { textWrap: parseTextWrap[textWrap] };
}