File: font_parser.rb

package info (click to toggle)
ruby-mojo-magick 0.6.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: ruby: 755; makefile: 4
file content (43 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# rubocop:disable Lint/AssignmentInCondition
module MojoMagick
  module Util
    class FontParser
      attr_reader :raw_fonts

      def initialize(raw_fonts)
        @raw_fonts = raw_fonts
      end

      def parse
        fonts = {}
        enumerator = raw_fonts.split("\n").each
        name = nil
        while begin; line = enumerator.next; rescue StopIteration; line = nil; end
          line.chomp!
          line = enumerator.next if line_is_empty(line)
          m = /^\s*Font:\s+(.*)$/.match(line)
          if m
            name = m[1].strip
            fonts[name] = { name: name }
          else
            k, v = extract_key_value(line)
            fonts[name][k] = v if k && name
          end
        end
        fonts.values.map { |f| MojoMagick::Font.new f }
      end

      private

      def extract_key_value(line)
        key_val = line.split(":").map(&:strip)
        [key_val[0].downcase.to_sym, key_val[1]]
      end

      def line_is_empty(line)
        line.nil? || line.empty? || (/^\s+$/ =~ line)
      end
    end
  end
end
# rubocop:enable Lint/AssignmentInCondition