-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: more support for different unit types for sizes and line-height
- Loading branch information
CatHood0
committed
Jul 9, 2024
1 parent
79a1dc2
commit 27a58db
Showing
4 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/// Converts various CSS units to pixels (px). | ||
/// | ||
/// This function supports the following units: | ||
/// - `px` (pixels) | ||
/// - `cm` (centimeters) | ||
/// - `mm` (millimeters) | ||
/// - `in` (inches) | ||
/// - `pt` (points) | ||
/// - `pc` (picas) | ||
/// - `em` (relative to the font-size of the element) | ||
/// - `rem` (relative to the font-size of the root element) | ||
/// | ||
/// For relative units (`em` and `rem`), you need to provide the font-size of the | ||
/// relevant context. The default font-size is 16 pixels. | ||
/// | ||
/// Example usage: | ||
/// ```dart | ||
/// print(convertToPx('2cm')); // 75.5905511812 | ||
/// print(convertToPx('10mm')); // 37.7952755906 | ||
/// print(convertToPx('1in')); // 96.0 | ||
/// print(convertToPx('12pt')); // 16.0 | ||
/// print(convertToPx('1pc')); // 16.0 | ||
/// print(convertToPx('2em', fontSize: 18.0)); // 36.0 | ||
/// print(convertToPx('2rem', rootFontSize: 20.0)); // 40.0 | ||
/// ``` | ||
/// | ||
/// [value] is the CSS value to be converted, e.g., '2cm', '10mm', etc. | ||
/// [fontSize] is the font-size of the current element, used for `em` units. | ||
/// [rootFontSize] is the font-size of the root element, used for `rem` units. | ||
/// | ||
/// Returns the equivalent value in pixels. | ||
const double cmSizeMultiplier = 37.7952755906; | ||
const double mmSizeMultiplier = 3.7795275591; | ||
const double inchSizeMultiplier = 96; | ||
const double pointSizeMultiplier = 1.3333333333; | ||
const double picasSizeMultiplier = 16; | ||
double parseSizeToPx( | ||
String value, { | ||
double fontSizeEmMultiplier = 16.0, | ||
double rootFontSizeRemMultiplier = 16.0, | ||
}) { | ||
// Extract the unit from the value string. | ||
final unit = value.replaceAll(RegExp(r'[0-9.]'), ''); | ||
|
||
// Extract the numeric part of the value string. | ||
final number = double.tryParse(value.replaceAll(RegExp(r'[a-z%]'), '')) ?? 0.0; | ||
|
||
// Convert the numeric value to pixels based on the unit. | ||
switch (unit) { | ||
case 'px': | ||
return number; | ||
case 'cm': | ||
return number * cmSizeMultiplier; | ||
case 'mm': | ||
return number * mmSizeMultiplier; | ||
case 'in': | ||
return number * inchSizeMultiplier; | ||
case 'pt': | ||
return number * pointSizeMultiplier; | ||
case 'pc': | ||
return number * picasSizeMultiplier; | ||
case 'em': | ||
return number * fontSizeEmMultiplier; | ||
case 'rem': | ||
return number * rootFontSizeRemMultiplier; | ||
default: | ||
throw UnsupportedError('Unit not supported: $unit'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// Parses a CSS `line-height` value to a pixel value based on the specified [fontSize] and optional [rootFontSize]. | ||
/// | ||
/// Supports unitless values, percentages, pixels (`px`), ems (`em`), rems (`rem`), and the keyword `normal`. | ||
/// Adjusts the parsed value to fit within the supported range of line heights: 1.0, 1.15, 1.5, and 2.0. | ||
/// | ||
/// Parameters: | ||
/// - [lineHeight]: The CSS `line-height` value to parse. | ||
/// - [fontSize]: The font size to use for unit conversions. Defaults to 16. | ||
/// - [rootFontSize]: The root font size, used for `rem` conversions. Defaults to [fontSize]. | ||
/// | ||
/// Returns: | ||
/// The parsed `line-height` value as a double in pixels. | ||
/// | ||
/// Examples: | ||
/// ```dart | ||
/// print(parseLineHeight('0.8')); // 1.0 | ||
/// print(parseLineHeight('1.1')); // 1.0 | ||
/// print(parseLineHeight('1.2')); // 1.15 | ||
/// print(parseLineHeight('1.3')); // 1.5 | ||
/// print(parseLineHeight('1.7')); // 1.5 | ||
/// print(parseLineHeight('2.2')); // 2.0 | ||
/// | ||
/// // Example with different units | ||
/// print(parseLineHeight('120%')); // 19.2 (16 * 1.2) | ||
/// print(parseLineHeight('1.5em')); // 24.0 (16 * 1.5) | ||
/// print(parseLineHeight('1.2rem')); // 19.2 (16 * 1.2) | ||
/// ``` | ||
const normalLineHeightMultiplier = 1.2; | ||
double parseLineHeight(String lineHeight, {double fontSize = 16, double rootFontSize = 16}) { | ||
// Convert line-height values | ||
double parsedValue; | ||
if (lineHeight.endsWith('px')) { | ||
parsedValue = double.parse(lineHeight.replaceAll('px', '')); | ||
} else if (lineHeight.endsWith('%')) { | ||
parsedValue = fontSize * (double.parse(lineHeight.replaceAll('%', '')) / 100); | ||
} else if (lineHeight.endsWith('rem')) { | ||
parsedValue = rootFontSize * double.parse(lineHeight.replaceAll('rem', '')); | ||
} else if (lineHeight.endsWith('em')) { | ||
parsedValue = fontSize * double.parse(lineHeight.replaceAll('em', '')); | ||
} else if (lineHeight == 'normal') { | ||
parsedValue = fontSize * normalLineHeightMultiplier; | ||
} else { | ||
parsedValue = fontSize * double.parse(lineHeight); | ||
} | ||
|
||
// Apply additional constraints | ||
if (parsedValue < 1.0) { | ||
parsedValue = 1.0; | ||
} else if (parsedValue > 1.0 && parsedValue < 1.15) { | ||
parsedValue = 1.0; | ||
} else if (parsedValue > 1.15 && parsedValue < 1.25) { | ||
parsedValue = 1.15; | ||
} else if (parsedValue > 1.25 && parsedValue < 1.5) { | ||
parsedValue = 1.5; | ||
} else if (parsedValue > 1.5 && parsedValue < 2.0) { | ||
parsedValue = 1.5; | ||
} else if (parsedValue > 2.0) { | ||
parsedValue = 2.0; | ||
} | ||
|
||
return parsedValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters