Class: SvgConform::ImageQualityAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_conform/image_quality_analyzer.rb

Overview

Analyzes SVG image quality across multiple dimensions.

Main entry point for quality analysis. Analyzes files and returns immutable QualityResult value objects that can be serialized or rendered.

Examples:

Single file analysis

analyzer = ImageQualityAnalyzer.new
result = analyzer.analyze('image.svg')

puts result.quality_score.value   # => 85
puts result.quality_score.good?   # => true
puts result.clean?                # => false

puts result.to_report.render      # Terminal colored output

Batch analysis

analyzer = ImageQualityAnalyzer.new
batch = analyzer.analyze_batch('./svgs')
puts batch.render                 # Summary chart

Programmatic summary

analyzer = ImageQualityAnalyzer.new
batch = analyzer.analyze_batch('./svgs')

puts "Average quality: #{batch.avg_quality_score.round(1)}"
puts "Total errors: #{batch.total_errors}"

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ ImageQualityAnalyzer

Returns a new instance of ImageQualityAnalyzer.

Parameters:



47
48
49
50
51
52
53
54
55
# File 'lib/svg_conform/image_quality_analyzer.rb', line 47

def initialize(config: nil)
  @config = config || QualityMetrics::Configuration.default
  @validator = Validator.new
  @error_analyzer = QualityMetrics::ErrorAnalyzer.new(@config)
  @feature_detector = QualityMetrics::FeatureDetector.new(@config)
  @complexity_calculator = QualityMetrics::ComplexityCalculator.new(@config)
  @quality_calculator = QualityMetrics::QualityCalculator.new(@config)
  @formatter = QualityMetrics::QualityReportFormatter.new
end

Instance Method Details

#analyze(path, profile: :svg_1_2_rfc) ⇒ QualityMetrics::QualityResult

Analyze a single SVG file.

Examples:

result = analyzer.analyze('image.svg', profile: :svg_1_2_rfc)
puts result.quality_score.grade  # => "B"

Parameters:

  • path (String)

    Path to SVG file

  • profile (Symbol, String) (defaults to: :svg_1_2_rfc)

    Profile name for validation

Returns:

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/svg_conform/image_quality_analyzer.rb', line 67

def analyze(path, profile: :svg_1_2_rfc)
  raise ArgumentError, "File not found: #{path}" unless File.exist?(path)

  file_size_bytes = File.size(path)
  content = File.read(path)

  # Run validation
  validation_result = @validator.validate(content, profile: profile)

  # Analyze errors
  error_breakdown = @error_analyzer.analyze(validation_result)

  # Detect features from content
  feature_flags = @feature_detector.detect(content)

  # Calculate complexity
  complexity_metrics = @complexity_calculator.calculate_from_content(
    content: content,
    features: feature_flags,
  )

  # Calculate all quality metrics
  @quality_calculator.calculate_all(
    validation_result: validation_result,
    file_path: path,
    file_size_bytes: file_size_bytes,
    error_breakdown: error_breakdown,
    complexity_metrics: complexity_metrics,
    feature_flags: feature_flags,
  )
end

#analyze_batch(dir, pattern: "**/*.svg", profile: :svg_1_2_rfc, progress: false) ⇒ SvgQualityBatchReport

Analyze all SVG files in a directory.

Examples:

batch = analyzer.analyze_batch('./svgs', progress: true)
puts batch.to_yaml  # Full YAML with all reports
puts batch.render   # Summary chart with colors

Parameters:

  • dir (String)

    Directory path

  • pattern (String) (defaults to: "**/*.svg")

    Glob pattern for matching files

  • profile (Symbol, String) (defaults to: :svg_1_2_rfc)

    Profile name for validation

  • progress (Boolean) (defaults to: false)

    Show progress output

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/svg_conform/image_quality_analyzer.rb', line 121

def analyze_batch(dir, pattern: "**/*.svg", profile: :svg_1_2_rfc,
                  progress: false)
  reports = analyze_reports(dir, pattern: pattern, profile: profile,
                                 progress: progress)
  summary = summarize_reports(reports)

  SvgQualityBatchReport.new(
    total_files: summary[:total_files],
    successful: summary[:successful],
    failed: summary[:failed],
    avg_quality_score: summary[:avg_quality_score],
    quality_distribution: summary[:quality_distribution],
    avg_error_count: summary[:avg_error_count],
    total_errors: summary[:total_errors],
    remediable_errors: summary[:remediable_errors],
    non_remediable_errors: summary[:non_remediable_errors],
    reports: reports,
  )
end

#analyze_report(path, profile: :svg_1_2_rfc) ⇒ SvgQualityReport

Analyze a single SVG file and return report for terminal output.

Parameters:

  • path (String)

    Path to SVG file

  • profile (Symbol, String) (defaults to: :svg_1_2_rfc)

    Profile name for validation

Returns:



104
105
106
# File 'lib/svg_conform/image_quality_analyzer.rb', line 104

def analyze_report(path, profile: :svg_1_2_rfc)
  analyze(path, profile: profile).to_report
end

#analyze_reports(dir, pattern: "**/*.svg", profile: :svg_1_2_rfc, progress: false) ⇒ Array<SvgQualityReport>

Analyze all SVG files and return array of reports.

Parameters:

  • dir (String)

    Directory path

  • pattern (String) (defaults to: "**/*.svg")

    Glob pattern

  • profile (Symbol, String) (defaults to: :svg_1_2_rfc)

    Profile name

  • progress (Boolean) (defaults to: false)

    Show progress

Returns:



148
149
150
151
152
153
154
155
156
# File 'lib/svg_conform/image_quality_analyzer.rb', line 148

def analyze_reports(dir, pattern: "**/*.svg", profile: :svg_1_2_rfc,
                    progress: false)
  files = glob_files(dir, pattern)
  reports = files.map { |file| analyze_report(file, profile: profile) }
  puts "Processed #{files.size} files..." if progress && !files.empty?
  reports
rescue StandardError
  []
end

#generate_report(results, format: :csv) ⇒ String

Generate a formatted report from results.

Parameters:

  • results (Array<SvgQualityReport>)
  • format (Symbol) (defaults to: :csv)

    :csv or :json

Returns:



163
164
165
166
167
168
169
# File 'lib/svg_conform/image_quality_analyzer.rb', line 163

def generate_report(results, format: :csv)
  case format
  when :csv then @formatter.format_csv(results)
  when :json then @formatter.format_json(results)
  else raise ArgumentError, "Unknown format: #{format}. Use :csv or :json"
  end
end