Skip to content

Commit

Permalink
Handling of multiple baselines in vertical rhythm module
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielnau committed Jun 27, 2013
1 parent 9ee6b42 commit eb1f9e7
Showing 1 changed file with 154 additions and 52 deletions.
206 changes: 154 additions & 52 deletions frameworks/compass/stylesheets/compass/typography/_vertical_rhythm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,41 @@ $min-line-padding: 2px !default;

// $base-font-size but in your output unit of choice.
// Defaults to 1em when `$relative-font-sizing` is true.
$font-unit: if($relative-font-sizing, 1em, $base-font-size) !default;
@function font-unit($font-size-context: $base-font-size) {
@return if($relative-font-sizing, 1em, $font-size-context);
}

// The basic unit of font rhythm.
$base-rhythm-unit: $base-line-height / $base-font-size * $font-unit;
@function base-rhythm-unit(
$font-size-context: $base-font-size,
$line-height-context: $base-line-height
) {
$rhythm-unit: $line-height-context / $font-size-context * font-unit($font-size-context);
@if $relative-font-sizing {
$context-ratio: $font-size-context / $base-font-size;
@return $rhythm-unit * $context-ratio;
} @else {
@return $rhythm-unit;
}
}

// The leader is the amount of whitespace in a line.
// It might be useful in your calculations.
$base-leader: ($base-line-height - $base-font-size) * $font-unit / $base-font-size;
@function base-leader(
$font-size-context: $base-font-size,
$line-height-context: $base-line-height
) {
@return ($line-height-context - $font-size-context) * font-unit($font-size-context) / $font-size-context;
}

// The half-leader is the amount of whitespace above and below a line.
// It might be useful in your calculations.
$base-half-leader: $base-leader / 2;
@function base-half-leader(
$font-size-context: $base-font-size,
$line-height-context: $base-line-height
) {
@return base-leader($font-size-context, $line-height-context) / 2;
}

// True if a number has a relative unit.
@function relative-unit($number) {
Expand All @@ -49,8 +72,8 @@ $base-half-leader: $base-leader / 2;
@return not (relative-unit($number) or unitless($number));
}

@if $relative-font-sizing and not relative-unit($font-unit) {
@warn "$relative-font-sizing is true but $font-unit is set to #{$font-unit} which is not a relative unit.";
@if $relative-font-sizing and not relative-unit(font-unit()) {
@warn "$relative-font-sizing is true but font-unit() returns #{font-unit()} which is not a relative unit.";
}

// Establishes a font baseline for the given font-size.
Expand All @@ -73,17 +96,21 @@ $base-half-leader: $base-leader / 2;
// @deprecated This mixin will be removed in the next release.
// Please use the `adjust-leading-to` mixin instead.
@mixin reset-baseline {
@include adjust-leading-to(1, if($relative-font-sizing, $base-font-size, $base-font-size));
@include adjust-leading-to(1, if($relative-font-sizing, $base-font-size, $base-font-size), $line-height-context);
}

// Show a background image that can be used to debug your alignments.
// Include the $img argument if you would rather use your own image than the
// Compass default gradient image.
@mixin debug-vertical-alignment($img: false) {
@mixin debug-vertical-alignment(
$img: false,
$font-size-context: $base-font-size,
$line-height-context: $base-line-height
) {
@if $img {
background: image-url($img);
} @else {
@include baseline-grid-background($base-rhythm-unit);
@include baseline-grid-background(base-rhythm-unit($font-size-context, $line-height-context));
}
}

Expand All @@ -92,32 +119,46 @@ $base-half-leader: $base-leader / 2;
// of this font should use up. It does not have to be an integer, but it
// defaults to the smallest integer that is large enough to fit the font.
// Use $from-size to adjust from a font-size other than the base font-size.
@mixin adjust-font-size-to($to-size, $lines: lines-for-font-size($to-size), $from-size: $base-font-size) {
@mixin adjust-font-size-to(
$to-size,
$lines: -1,
$from-size: $base-font-size,
$font-size-context: $base-font-size,
$line-height-context: $base-line-height
) {
@if not $relative-font-sizing and $from-size != $base-font-size {
@warn "$relative-font-sizing is false but a relative font size was passed to adjust-font-size-to";
}
font-size: $font-unit * $to-size / $from-size;
@include adjust-leading-to($lines, if($relative-font-sizing, $to-size, $base-font-size));
@if $lines == -1 {
$lines: lines-for-font-size($to-size, $line-height-context)
}
font-size: font-unit() * $to-size / $from-size;
@include adjust-leading-to($lines, if($relative-font-sizing, $to-size, $to-size), $line-height-context);
}

// Adjust a block to have different line height to maintain the rhythm.
// $lines specifies how many multiples of the baseline rhythm each line of this
// font should use up. It does not have to be an integer, but it defaults to the
// smallest integer that is large enough to fit the font.
@mixin adjust-leading-to($lines, $font-size: $base-font-size) {
line-height: rhythm($lines, $font-size);
@mixin adjust-leading-to(
$lines,
$font-size: $base-font-size,
$line-height-context: $base-line-height
) {
line-height: rhythm($lines, $font-size, 0, $line-height-context);
}

// Calculate rhythm units.
@function rhythm(
$lines: 1,
$font-size: $base-font-size,
$offset: 0
$offset: 0,
$line-height-context: $base-line-height
) {
@if not $relative-font-sizing and $font-size != $base-font-size {
@warn "$relative-font-sizing is false but a relative font size was passed to the rhythm function";
}
$rhythm: $font-unit * ($lines * $base-line-height - $offset) / $font-size;
$rhythm: font-unit($font-size) * ($lines * $line-height-context - $offset) / $font-size;
// Round the pixels down to nearest integer.
@if unit($rhythm) == px {
$rhythm: floor($rhythm);
Expand All @@ -126,96 +167,157 @@ $base-half-leader: $base-leader / 2;
}

// Calculate the minimum multiple of rhythm units needed to contain the font-size.
@function lines-for-font-size($font-size) {
@function lines-for-font-size($font-size, $line-height-context: $base-line-height) {
$lines: if($round-to-nearest-half-line,
ceil(2 * $font-size / $base-line-height) / 2,
ceil($font-size / $base-line-height));
@if $lines * $base-line-height - $font-size < $min-line-padding * 2 {
ceil(2 * $font-size / $line-height-context) / 2,
ceil($font-size / $line-height-context));
@if $lines * $line-height-context - $font-size < $min-line-padding * 2 {
$lines: $lines + if($round-to-nearest-half-line, 0.5, 1);
}
@return $lines;
}

// Apply leading whitespace. The $property can be margin or padding.
@mixin leader($lines: 1, $font-size: $base-font-size, $property: margin) {
#{$property}-top: rhythm($lines, $font-size);
@mixin leader($lines: 1, $font-size: $base-font-size, $property: margin, $line-height-context: $base-line-height) {
#{$property}-top: rhythm($lines, $font-size, 0, $line-height-context);
}

// Apply leading whitespace as padding.
@mixin padding-leader($lines: 1, $font-size: $base-font-size) {
padding-top: rhythm($lines, $font-size);
@mixin padding-leader($lines: 1, $font-size: $base-font-size, $line-height-context: $base-line-height) {
padding-top: rhythm($lines, $font-size, 0, $line-height-context);
}

// Apply leading whitespace as margin.
@mixin margin-leader($lines: 1, $font-size: $base-font-size) {
margin-top: rhythm($lines, $font-size);
@mixin margin-leader(
$lines: 1,
$font-size: $base-font-size,
$line-height-context: $base-line-height
) {
margin-top: rhythm($lines, $font-size, 0, $line-height-context);
}

// Apply trailing whitespace. The $property can be margin or padding.
@mixin trailer($lines: 1, $font-size: $base-font-size, $property: margin) {
#{$property}-bottom: rhythm($lines, $font-size);
@mixin trailer(
$lines: 1,
$font-size: $base-font-size,
$property: margin,
$line-height-context: $base-line-height
) {
#{$property}-bottom: rhythm($lines, $font-size, 0, $line-height-context);
}

// Apply trailing whitespace as padding.
@mixin padding-trailer($lines: 1, $font-size: $base-font-size) {
padding-bottom: rhythm($lines, $font-size);
@mixin padding-trailer(
$lines: 1,
$font-size: $base-font-size,
$line-height-context: $base-line-height
) {
padding-bottom: rhythm($lines, $font-size, 0, $line-height-context);
}

// Apply trailing whitespace as margin.
@mixin margin-trailer($lines: 1, $font-size: $base-font-size) {
margin-bottom: rhythm($lines, $font-size);
@mixin margin-trailer(
$lines: 1,
$font-size: $base-font-size,
$line-height-context: $base-line-height
) {
margin-bottom: rhythm($lines, $font-size, 0, $line-height-context);
}

// Shorthand mixin to apply whitespace for top and bottom margins and padding.
@mixin rhythm($leader: 0, $padding-leader: 0, $padding-trailer: 0, $trailer: 0, $font-size: $base-font-size) {
@include leader($leader, $font-size);
@include padding-leader($padding-leader, $font-size);
@include padding-trailer($padding-trailer, $font-size);
@include trailer($trailer, $font-size);
@mixin rhythm(
$leader: 0,
$padding-leader: 0,
$padding-trailer: 0,
$trailer: 0,
$font-size: $base-font-size,
$line-height-context: $base-line-height
) {
@include leader($leader, $font-size, $line-height-context);
@include padding-leader($padding-leader, $font-size, $line-height-context);
@include padding-trailer($padding-trailer, $font-size, $line-height-context);
@include trailer($trailer, $font-size, $line-height-context);
}

// Apply a border and whitespace to any side without destroying the vertical
// rhythm. The whitespace must be greater than the width of the border.
@mixin apply-side-rhythm-border($side, $width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@mixin apply-side-rhythm-border(
$side,
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@if not $relative-font-sizing and $font-size != $base-font-size {
@warn "$relative-font-sizing is false but a relative font size was passed to apply-side-rhythm-border";
}
border-#{$side}: {
style: $border-style;
width: $font-unit * $width / $font-size;
width: font-unit($font-size) * $width / $font-size;
};
padding-#{$side}: rhythm($lines, $font-size, $offset: $width);
padding-#{$side}: rhythm($lines, $font-size, $width, $line-height-context);
}

// Apply borders and whitespace equally to all sides.
@mixin rhythm-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@mixin rhythm-borders(
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@if not $relative-font-sizing and $font-size != $base-font-size {
@warn "$relative-font-sizing is false but a relative font size was passed to rhythm-borders";
}
border: {
style: $border-style;
width: $font-unit * $width / $font-size;
width: font-unit($font-size) * $width / $font-size;
};
padding: rhythm($lines, $font-size, $offset: $width);
padding: rhythm($lines, $font-size, $width, $line-height-context);
}

// Apply a leading border.
@mixin leading-border($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@include apply-side-rhythm-border(top, $width, $lines, $font-size, $border-style);
@mixin leading-border(
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@include apply-side-rhythm-border(top, $width, $lines, $font-size, $border-style, $line-height-context);
}

// Apply a trailing border.
@mixin trailing-border($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@include apply-side-rhythm-border(bottom, $width, $lines, $font-size, $border-style);
@mixin trailing-border(
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@include apply-side-rhythm-border(bottom, $width, $lines, $font-size, $border-style, $line-height-context);
}

// Apply both leading and trailing borders.
@mixin horizontal-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@include leading-border($width, $lines, $font-size, $border-style);
@include trailing-border($width, $lines, $font-size, $border-style);
@mixin horizontal-borders(
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@include leading-border($width, $lines, $font-size, $border-style, $line-height-context);
@include trailing-border($width, $lines, $font-size, $border-style, $line-height-context);
}

// Alias for `horizontal-borders` mixin.
@mixin h-borders($width: 1px, $lines: 1, $font-size: $base-font-size, $border-style: $default-rhythm-border-style) {
@include horizontal-borders($width, $lines, $font-size, $border-style);
@mixin h-borders(
$width: 1px,
$lines: 1,
$font-size: $base-font-size,
$border-style: $default-rhythm-border-style,
$line-height-context: $base-line-height
) {
@include horizontal-borders($width, $lines, $font-size, $border-style, $line-height-context);
}

0 comments on commit eb1f9e7

Please sign in to comment.