Skip to content

Commit

Permalink
Refactor repeated code into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Sep 7, 2022
1 parent 1429c0f commit 5335d93
Showing 1 changed file with 28 additions and 49 deletions.
77 changes: 28 additions & 49 deletions lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,19 @@ function warnAboutUnusedVariationAxes(
const seenAxisValuesByFontUrlAndAxisName = new Map();
const outOfBoundsAxesByFontUrl = new Map();

function noteUsedValue(fontUrl, axisName, axisValue) {
let seenAxes = seenAxisValuesByFontUrlAndAxisName.get(fontUrl);
if (!seenAxes) {
seenAxes = new Map();
seenAxisValuesByFontUrlAndAxisName.set(fontUrl, seenAxes);
}
if (seenAxes.has(axisName)) {
seenAxes.get(axisName).push(axisValue);
} else {
seenAxes.set(axisName, [axisValue]);
}
}

for (const { fontUsages } of htmlOrSvgAssetTextsWithProps) {
for (const {
fontUrl,
Expand All @@ -805,83 +818,49 @@ function warnAboutUnusedVariationAxes(
hasOutOfBoundsAnimationTimingFunction,
props,
} of fontUsages) {
let seenAxes = seenAxisValuesByFontUrlAndAxisName.get(fontUrl);
if (!seenAxes) {
seenAxes = new Map();
seenAxisValuesByFontUrlAndAxisName.set(fontUrl, seenAxes);
}
const seenItalValues = [];
if (fontStyles.has('italic')) {
seenItalValues.push(1);
noteUsedValue(fontUrl, 'ital', 1);
}
// If any font-style value except italic is seen (including normal or oblique)
// we're also utilizing value 0:
if (fontStyles.size > fontStyles.has('italic') ? 1 : 0) {
seenItalValues.push(0);
noteUsedValue(fontUrl, 'ital', 0);
}
if (seenItalValues.length > 0) {
if (seenAxes.has('ital')) {
seenAxes.get('ital').push(...seenItalValues);
} else {
seenAxes.set('ital', seenItalValues);
}
}
const seenSlntValues = [];
if (fontStyles.has('oblique')) {
// https://www.w3.org/TR/css-fonts-4/#font-style-prop
// oblique <angle>?
// [...] The lack of an <angle> represents 14deg.
seenSlntValues.push(14);
noteUsedValue(fontUrl, 'slnt', 14);
}
// If any font-style value except oblique is seen (including normal or italic)
// we're also utilizing value 0:
if (fontStyles.size > fontStyles.has('oblique') ? 1 : 0) {
seenSlntValues.push(0);
}
if (seenSlntValues.length > 0) {
if (seenAxes.has('slnt')) {
seenAxes.get('slnt').push(...seenSlntValues);
} else {
seenAxes.set('slnt', seenSlntValues);
}
noteUsedValue(fontUrl, 'slnt', 0);
}

const minMaxFontWeight = parseFontWeightRange(props['font-weight']);
const seenFontWeightValues = [];
for (const fontWeight of fontWeights) {
seenFontWeightValues.push(_.clamp(fontWeight, ...minMaxFontWeight));
}
if (seenFontWeightValues.length > 0) {
if (seenAxes.has('wght')) {
seenAxes.get('wght').push(...seenFontWeightValues);
} else {
seenAxes.set('wght', seenFontWeightValues);
}
noteUsedValue(
fontUrl,
'wght',
_.clamp(fontWeight, ...minMaxFontWeight)
);
}

const minMaxFontStretch = parseFontStretchRange(props['font-stretch']);
const seenFontStretchValues = [];
for (const fontStrech of fontStretches) {
seenFontStretchValues.push(_.clamp(fontStrech, ...minMaxFontStretch));
}
if (seenFontStretchValues.length > 0) {
if (seenAxes.has('wdth')) {
seenAxes.get('wdth').push(...seenFontStretchValues);
} else {
seenAxes.set('wdth', seenFontStretchValues);
}
noteUsedValue(
fontUrl,
'wdth',
_.clamp(fontStrech, ...minMaxFontStretch)
);
}

for (const fontVariationSettingsValue of fontVariationSettings) {
for (const [axisName, axisValue] of parseFontVariationSettings(
fontVariationSettingsValue
)) {
const seenAxisValues = seenAxes.get(axisName);
if (seenAxisValues) {
seenAxisValues.push(axisValue);
} else {
seenAxes.set(axisName, [axisValue]);
}
noteUsedValue(fontUrl, axisName, axisValue);
if (hasOutOfBoundsAnimationTimingFunction) {
let outOfBoundsAxes = outOfBoundsAxesByFontUrl.get(fontUrl);
if (!outOfBoundsAxes) {
Expand Down

0 comments on commit 5335d93

Please sign in to comment.