Class: CSSensible::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/cssensible/finder.rb

Constant Summary collapse

MIN_LINE_HEIGHT_EM_VALUE =

A line-height should be at least this em-value to improve readability.

EmValue.new(1.3)
MIN_FONT_SIZE_PX =

Size in pixel a font should at least be to be readable

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font_sizes, min_line_height_em = MIN_LINE_HEIGHT_EM_VALUE) ⇒ Finder

Returns a new Vertical Grid Finder based on the font size passed to new.



16
17
18
19
# File 'lib/cssensible/finder.rb', line 16

def initialize(font_sizes, min_line_height_em = MIN_LINE_HEIGHT_EM_VALUE)
  @font_sizes = font_sizes.sort
  @min_line_height_em = min_line_height_em
end

Instance Attribute Details

#font_sizeObject (readonly)

Returns the value of attribute font_size.



13
14
15
# File 'lib/cssensible/finder.rb', line 13

def font_size
  @font_size
end

Instance Method Details

#findObject

Finds sensible vertical grids based on the parameters given in new.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cssensible/finder.rb', line 22

def find
  vertical_grids = []

  # for the first iteration, all line-heights from the smallest font-size
  # to two times the largest font-size are potentially possible
  line_heights = (@font_sizes.first .. @font_sizes.last * 2).to_a

  # go through required font-sizes and identify all line-heights that work
  # with all of them
  @font_sizes.each do |font_size|
    possible_line_heights = []

    # keep line-height if it can be expressed in valid 'em' for this
    # font-size
    line_heights.each do |line_height|
      if EmValue.new(line_height / font_size.to_f).usable?
        possible_line_heights << line_height
      end
    end

    # for next iteration, only check line-heights that work with this
    # font-size
    line_heights = possible_line_heights
  end

  line_heights.each do |line_height|
    base_line_height = line_height
    font_sizes_to_line_height = {}

    # we will increment the line-height as soon as a resulting em-value
    # falls underneath @min_line_height_em; to prevent large differences, we
    # add half of our base line-height if possible
    if line_height.even?
      line_height_increment = line_height / 2
    else
      line_height_increment = line_height
    end

    # to offer a larger range of available font-sizes, we start from the
    # smallest possible font-size and end at don't just end at the biggest
    # supplied font-size
    (MIN_FONT_SIZE_PX .. @font_sizes.last * 2).each do |font_size|
      line_height_em = EmValue.new(line_height / font_size.to_f)

      # increment line-height if we fall below the minimum value
      if line_height_em < @min_line_height_em
        line_height += line_height_increment
        redo
      end

      # map font-size and line-height for this grid
      if line_height_em.usable? && line_height_em >= @min_line_height_em
        font_sizes_to_line_height[font_size] = line_height_em
      end
    end

    # add grid to results
    # TODO can font_sizes_to_line_height ever really be empty?
    unless font_sizes_to_line_height.empty?
      vertical_grid = VerticalGrid.new(@font_sizes, base_line_height,
          font_sizes_to_line_height)
      vertical_grids << vertical_grid
    end
  end

  vertical_grids
end