fontisan 0.2.22 → 0.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -0
  3. data/.rubocop_todo.yml +93 -17
  4. data/CHANGELOG.md +12 -2
  5. data/README.adoc +6 -210
  6. data/fontisan.gemspec +48 -0
  7. data/lib/fontisan/cldr/unicode_set_parser.rb +23 -6
  8. data/lib/fontisan/cldr/version_resolver.rb +1 -1
  9. data/lib/fontisan/cli.rb +0 -170
  10. data/lib/fontisan/commands.rb +0 -3
  11. data/lib/fontisan/formatters/text_formatter.rb +0 -6
  12. data/lib/fontisan/formatters.rb +0 -3
  13. data/lib/fontisan/hints.rb +6 -3
  14. data/lib/fontisan/models.rb +4 -4
  15. data/lib/fontisan/pipeline/strategies.rb +4 -2
  16. data/lib/fontisan/pipeline.rb +2 -1
  17. data/lib/fontisan/tables/cff.rb +2 -1
  18. data/lib/fontisan/tables.rb +2 -1
  19. data/lib/fontisan/version.rb +1 -1
  20. data/lib/fontisan.rb +0 -3
  21. metadata +7 -70
  22. data/lib/fontisan/audit/codepoint_range_coalescer.rb +0 -41
  23. data/lib/fontisan/audit/context.rb +0 -122
  24. data/lib/fontisan/audit/differ.rb +0 -124
  25. data/lib/fontisan/audit/extractors/aggregations.rb +0 -54
  26. data/lib/fontisan/audit/extractors/base.rb +0 -26
  27. data/lib/fontisan/audit/extractors/color_capabilities.rb +0 -141
  28. data/lib/fontisan/audit/extractors/coverage.rb +0 -48
  29. data/lib/fontisan/audit/extractors/hinting.rb +0 -197
  30. data/lib/fontisan/audit/extractors/identity.rb +0 -52
  31. data/lib/fontisan/audit/extractors/language_coverage.rb +0 -37
  32. data/lib/fontisan/audit/extractors/licensing.rb +0 -79
  33. data/lib/fontisan/audit/extractors/metrics.rb +0 -103
  34. data/lib/fontisan/audit/extractors/opentype_layout.rb +0 -69
  35. data/lib/fontisan/audit/extractors/provenance.rb +0 -29
  36. data/lib/fontisan/audit/extractors/style.rb +0 -32
  37. data/lib/fontisan/audit/extractors/variation_detail.rb +0 -99
  38. data/lib/fontisan/audit/extractors.rb +0 -27
  39. data/lib/fontisan/audit/library_aggregator.rb +0 -83
  40. data/lib/fontisan/audit/library_auditor.rb +0 -90
  41. data/lib/fontisan/audit/registry.rb +0 -60
  42. data/lib/fontisan/audit/style_extractor.rb +0 -80
  43. data/lib/fontisan/audit.rb +0 -20
  44. data/lib/fontisan/cli/ucd_cli.rb +0 -97
  45. data/lib/fontisan/commands/audit_command.rb +0 -123
  46. data/lib/fontisan/commands/audit_compare_command.rb +0 -66
  47. data/lib/fontisan/commands/audit_library_command.rb +0 -46
  48. data/lib/fontisan/config/ucd.yml +0 -23
  49. data/lib/fontisan/formatters/audit_diff_text_renderer.rb +0 -122
  50. data/lib/fontisan/formatters/audit_text_renderer.rb +0 -324
  51. data/lib/fontisan/formatters/library_summary_text_renderer.rb +0 -99
  52. data/lib/fontisan/models/audit/audit_axis.rb +0 -30
  53. data/lib/fontisan/models/audit/audit_block.rb +0 -32
  54. data/lib/fontisan/models/audit/audit_diff.rb +0 -77
  55. data/lib/fontisan/models/audit/audit_report.rb +0 -153
  56. data/lib/fontisan/models/audit/codepoint_range.rb +0 -40
  57. data/lib/fontisan/models/audit/codepoint_set_diff.rb +0 -34
  58. data/lib/fontisan/models/audit/color_capabilities.rb +0 -93
  59. data/lib/fontisan/models/audit/duplicate_group.rb +0 -23
  60. data/lib/fontisan/models/audit/embedding_type.rb +0 -76
  61. data/lib/fontisan/models/audit/field_change.rb +0 -28
  62. data/lib/fontisan/models/audit/fs_selection_flags.rb +0 -61
  63. data/lib/fontisan/models/audit/gasp_range.rb +0 -63
  64. data/lib/fontisan/models/audit/hinting.rb +0 -93
  65. data/lib/fontisan/models/audit/library_summary.rb +0 -40
  66. data/lib/fontisan/models/audit/licensing.rb +0 -48
  67. data/lib/fontisan/models/audit/metrics.rb +0 -111
  68. data/lib/fontisan/models/audit/named_instance.rb +0 -41
  69. data/lib/fontisan/models/audit/opentype_layout.rb +0 -40
  70. data/lib/fontisan/models/audit/script_coverage_row.rb +0 -26
  71. data/lib/fontisan/models/audit/script_features.rb +0 -28
  72. data/lib/fontisan/models/audit/variation_detail.rb +0 -44
  73. data/lib/fontisan/models/audit.rb +0 -33
  74. data/lib/fontisan/models/ucd/ucd.rb +0 -38
  75. data/lib/fontisan/models/ucd/ucd_char.rb +0 -67
  76. data/lib/fontisan/models/ucd.rb +0 -19
  77. data/lib/fontisan/ucd/aggregator.rb +0 -73
  78. data/lib/fontisan/ucd/cache_manager.rb +0 -111
  79. data/lib/fontisan/ucd/config.rb +0 -59
  80. data/lib/fontisan/ucd/download_error.rb +0 -9
  81. data/lib/fontisan/ucd/downloader.rb +0 -88
  82. data/lib/fontisan/ucd/error.rb +0 -8
  83. data/lib/fontisan/ucd/index.rb +0 -103
  84. data/lib/fontisan/ucd/index_builder.rb +0 -107
  85. data/lib/fontisan/ucd/range_entry.rb +0 -56
  86. data/lib/fontisan/ucd/unknown_version_error.rb +0 -9
  87. data/lib/fontisan/ucd/version_resolver.rb +0 -79
  88. data/lib/fontisan/ucd.rb +0 -23
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- module Extractors
6
- # Licensing + embedding permissions + vendor provenance.
7
- #
8
- # Returned fields:
9
- # licensing: Models::Audit::Licensing instance, or nil for Type 1
10
- #
11
- # Type 1 fonts have no OS/2 table; their licensing is nil. WOFF/
12
- # WOFF2 carry the same OS/2 + name tables as TTF/OTF and need no
13
- # special handling.
14
- class Licensing < Base
15
- # nameID → AuditReport field name, per OpenType name table spec.
16
- NAME_IDS = {
17
- copyright: 0,
18
- trademark: 7,
19
- manufacturer: 8,
20
- designer: 9,
21
- description: 10,
22
- vendor_url: 11,
23
- designer_url: 12,
24
- license_description: 13,
25
- license_url: 14,
26
- }.freeze
27
- private_constant :NAME_IDS
28
-
29
- def extract(context)
30
- font = context.font
31
- return { licensing: nil } unless sfnt?(font)
32
-
33
- os2 = os2_for(font)
34
- name = name_table_for(font)
35
-
36
- {
37
- licensing: Models::Audit::Licensing.new(
38
- **name_fields(name),
39
- vendor_id: sanitized_vendor_id(os2),
40
- embedding_type: Models::Audit::EmbeddingType.decode(os2&.fs_type&.to_i),
41
- fs_selection_flags: Models::Audit::FsSelectionFlags.decode(os2&.fs_selection&.to_i),
42
- ),
43
- }
44
- end
45
-
46
- private
47
-
48
- def sfnt?(font)
49
- font.is_a?(SfntFont)
50
- end
51
-
52
- def os2_for(font)
53
- return nil unless font.has_table?(Constants::OS2_TAG)
54
-
55
- font.table(Constants::OS2_TAG)
56
- end
57
-
58
- def name_table_for(font)
59
- return nil unless font.has_table?(Constants::NAME_TAG)
60
-
61
- font.table(Constants::NAME_TAG)
62
- end
63
-
64
- def name_fields(name)
65
- return {} unless name
66
-
67
- NAME_IDS.transform_values { |id| name.english_name(id) }
68
- end
69
-
70
- def sanitized_vendor_id(os2)
71
- raw = os2&.ach_vend_id
72
- return nil if raw.nil?
73
-
74
- raw.gsub(/[\x00\s]+$/, "")
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- module Extractors
6
- # Layout-critical metrics consolidated from head, hhea, OS/2, post.
7
- #
8
- # Returned fields:
9
- # metrics: Models::Audit::Metrics instance, or nil for Type 1
10
- #
11
- # All table reads are nil-safe; tables may be absent in stripped
12
- # WOFF builds or legacy formats.
13
- class Metrics < Base
14
- def extract(context)
15
- font = context.font
16
- return { metrics: nil } unless sfnt?(font)
17
-
18
- { metrics: Models::Audit::Metrics.new(**gather(font)) }
19
- end
20
-
21
- private
22
-
23
- def sfnt?(font)
24
- font.is_a?(SfntFont)
25
- end
26
-
27
- def gather(font)
28
- {}.tap do |h|
29
- h.merge!(head_fields(font))
30
- h.merge!(hhea_fields(font))
31
- h.merge!(os2_fields(font))
32
- h.merge!(post_fields(font))
33
- end
34
- end
35
-
36
- def head_fields(font)
37
- head = table(font, Constants::HEAD_TAG)
38
- return {} unless head
39
-
40
- {
41
- units_per_em: head.units_per_em&.to_i,
42
- bbox_x_min: head.x_min&.to_i,
43
- bbox_y_min: head.y_min&.to_i,
44
- bbox_x_max: head.x_max&.to_i,
45
- bbox_y_max: head.y_max&.to_i,
46
- }
47
- end
48
-
49
- def hhea_fields(font)
50
- hhea = table(font, Constants::HHEA_TAG)
51
- return {} unless hhea
52
-
53
- {
54
- hhea_ascent: hhea.ascent&.to_i,
55
- hhea_descent: hhea.descent&.to_i,
56
- hhea_line_gap: hhea.line_gap&.to_i,
57
- }
58
- end
59
-
60
- def os2_fields(font)
61
- os2 = table(font, Constants::OS2_TAG)
62
- return {} unless os2
63
-
64
- {
65
- typo_ascender: os2.s_typo_ascender&.to_i,
66
- typo_descender: os2.s_typo_descender&.to_i,
67
- typo_line_gap: os2.s_typo_line_gap&.to_i,
68
- win_ascent: os2.us_win_ascent&.to_i,
69
- win_descent: os2.us_win_descent&.to_i,
70
- x_height: os2.sx_height&.to_i,
71
- cap_height: os2.s_cap_height&.to_i,
72
- subscript_x_size: os2.y_subscript_x_size&.to_i,
73
- subscript_y_size: os2.y_subscript_y_size&.to_i,
74
- subscript_x_offset: os2.y_subscript_x_offset&.to_i,
75
- subscript_y_offset: os2.y_subscript_y_offset&.to_i,
76
- superscript_x_size: os2.y_superscript_x_size&.to_i,
77
- superscript_y_size: os2.y_superscript_y_size&.to_i,
78
- superscript_x_offset: os2.y_superscript_x_offset&.to_i,
79
- superscript_y_offset: os2.y_superscript_y_offset&.to_i,
80
- strikeout_size: os2.y_strikeout_size&.to_i,
81
- strikeout_position: os2.y_strikeout_position&.to_i,
82
- }
83
- end
84
-
85
- def post_fields(font)
86
- post = table(font, Constants::POST_TAG)
87
- return {} unless post
88
-
89
- {
90
- underline_position: post.underline_position&.to_f,
91
- underline_thickness: post.underline_thickness&.to_f,
92
- }
93
- end
94
-
95
- def table(font, tag)
96
- return nil unless font.has_table?(tag)
97
-
98
- font.table(tag)
99
- end
100
- end
101
- end
102
- end
103
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- module Extractors
6
- # OpenType layout summary: union of GSUB + GPOS scripts and features,
7
- # plus a per-script breakdown preserving which feature belongs to
8
- # which script under which table.
9
- #
10
- # Returned fields:
11
- # opentype_layout: Models::Audit::OpenTypeLayout, or nil for
12
- # Type 1
13
- #
14
- # Owned here (MECE split from Aggregations, which is UCD-only).
15
- class OpenTypeLayout < Base
16
- def extract(context)
17
- font = context.font
18
- return { opentype_layout: nil } unless sfnt?(font)
19
-
20
- gsub_scripts = scripts_in(font, Constants::GSUB_TAG)
21
- gpos_scripts = scripts_in(font, Constants::GPOS_TAG)
22
- all_scripts = (gsub_scripts + gpos_scripts).uniq.sort
23
-
24
- by_script = all_scripts.map do |tag|
25
- Models::Audit::ScriptFeatures.new(
26
- script: tag,
27
- gsub_features: features_for(font, Constants::GSUB_TAG, tag),
28
- gpos_features: features_for(font, Constants::GPOS_TAG, tag),
29
- )
30
- end
31
-
32
- { opentype_layout: Models::Audit::OpenTypeLayout.new(
33
- scripts: all_scripts,
34
- features: aggregate_features(by_script),
35
- by_script: by_script,
36
- has_gsub: font.has_table?(Constants::GSUB_TAG),
37
- has_gpos: font.has_table?(Constants::GPOS_TAG),
38
- ) }
39
- end
40
-
41
- protected
42
-
43
- def sfnt?(font)
44
- font.is_a?(SfntFont)
45
- end
46
-
47
- private
48
-
49
- def scripts_in(font, tag)
50
- return [] unless font.has_table?(tag)
51
-
52
- font.table(tag).scripts
53
- end
54
-
55
- def features_for(font, tag, script)
56
- return [] unless font.has_table?(tag)
57
-
58
- font.table(tag).features(script_tag: script).sort
59
- end
60
-
61
- def aggregate_features(by_script)
62
- gsub = by_script.flat_map(&:gsub_features)
63
- gpos = by_script.flat_map(&:gpos_features)
64
- (gsub + gpos).uniq.sort
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "digest"
4
- require "time"
5
-
6
- module Fontisan
7
- module Audit
8
- module Extractors
9
- # Provenance fields: who generated this report, when, from what.
10
- #
11
- # Returned fields:
12
- # generated_at, fontisan_version, source_file, source_sha256,
13
- # source_format, font_index, num_fonts_in_source
14
- class Provenance < Base
15
- def extract(context)
16
- {
17
- generated_at: Time.now.utc.iso8601,
18
- fontisan_version: Fontisan::VERSION,
19
- source_file: File.expand_path(context.font_path),
20
- source_sha256: Digest::SHA256.file(context.font_path).hexdigest,
21
- source_format: context.source_format,
22
- font_index: context.font_index,
23
- num_fonts_in_source: context.num_fonts_in_source,
24
- }
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- module Extractors
6
- # Style fields: weight, width, italic/bold flags, Panose family
7
- # classification.
8
- #
9
- # Returned fields:
10
- # weight_class, width_class, italic, bold, panose
11
- #
12
- # Variable-font axis inventory lives in {Extractors::VariationDetail}
13
- # (MECE: this extractor is the OS/2 + head specialist, that one owns
14
- # everything fvar-derived).
15
- #
16
- # Delegates to {Audit::StyleExtractor} — the existing specialist
17
- # class that owns the OS/2 + head interpretation rules.
18
- class Style < Base
19
- def extract(context)
20
- style = StyleExtractor.new(context.font)
21
- {
22
- weight_class: style.weight_class,
23
- width_class: style.width_class,
24
- italic: style.italic,
25
- bold: style.bold,
26
- panose: style.panose,
27
- }
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,99 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- module Extractors
6
- # Variable-font detail: fvar axes + named instances + presence flags
7
- # for every variation side-table (avar, cvar, HVAR, VVAR, MVAR, gvar).
8
- #
9
- # Returned fields:
10
- # variation: Models::Audit::VariationDetail, or nil for non-variable
11
- # faces and Type 1 fonts
12
- #
13
- # A face is considered variable iff the fvar table is present. CFF2
14
- # outlines without fvar are not "variable" by this definition (they
15
- # may carry variation data but no user-facing axes).
16
- class VariationDetail < Base
17
- def extract(context)
18
- font = context.font
19
- return { variation: nil } unless variable?(font)
20
-
21
- fvar = font.table(Constants::FVAR_TAG)
22
- return { variation: nil } unless fvar
23
-
24
- name_table = font.has_table?(Constants::NAME_TAG) ? font.table(Constants::NAME_TAG) : nil
25
- axis_tags = axis_tags_from(fvar)
26
-
27
- { variation: Models::Audit::VariationDetail.new(
28
- axes: build_axes(name_table, fvar),
29
- named_instances: build_instances(name_table, fvar, axis_tags),
30
- has_avar: font.has_table?(Constants::AVAR_TAG),
31
- has_cvar: font.has_table?(Constants::CVAR_TAG),
32
- has_hvar: font.has_table?(Constants::HVAR_TAG),
33
- has_vvar: font.has_table?(Constants::VVAR_TAG),
34
- has_mvar: font.has_table?(Constants::MVAR_TAG),
35
- has_gvar: font.has_table?(Constants::GVAR_TAG),
36
- ) }
37
- end
38
-
39
- protected
40
-
41
- def variable?(font)
42
- font.is_a?(SfntFont) && font.has_table?(Constants::FVAR_TAG)
43
- end
44
-
45
- private
46
-
47
- def build_axes(name_table, fvar)
48
- return [] unless fvar.axes
49
-
50
- fvar.axes.map do |axis|
51
- Models::Audit::AuditAxis.new(
52
- tag: axis.axis_tag,
53
- min_value: axis.min_value,
54
- default_value: axis.default_value,
55
- max_value: axis.max_value,
56
- name: english_name(name_table, axis.axis_name_id),
57
- )
58
- end
59
- end
60
-
61
- def build_instances(name_table, fvar, axis_tags)
62
- instances = fvar.instances
63
- return [] unless instances
64
-
65
- instances.map do |instance|
66
- build_instance(name_table, instance, axis_tags)
67
- end
68
- end
69
-
70
- def build_instance(name_table, instance, axis_tags)
71
- subfamily_name = english_name(name_table, instance[:name_id])
72
- ps_name_id = instance[:postscript_name_id]
73
- ps_name = ps_name_id ? english_name(name_table, ps_name_id) : nil
74
- coords = Models::Audit::NamedInstance.format_coordinates(
75
- axis_tags, instance[:coordinates]
76
- )
77
-
78
- Models::Audit::NamedInstance.new(
79
- subfamily_name: subfamily_name,
80
- postscript_name: ps_name,
81
- coordinates: coords,
82
- )
83
- end
84
-
85
- def english_name(name_table, name_id)
86
- return nil unless name_table && name_id
87
-
88
- name_table.english_name(name_id)
89
- end
90
-
91
- def axis_tags_from(fvar)
92
- return [] unless fvar.axes
93
-
94
- fvar.axes.map(&:axis_tag)
95
- end
96
- end
97
- end
98
- end
99
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Autoload hub for the Fontisan::Audit::Extractors namespace.
4
- #
5
- # Each extractor is a small MECE class with a single `#extract(context)`
6
- # method returning a hash of AuditReport fields. The Audit::Registry
7
- # declares the ordered list.
8
-
9
- module Fontisan
10
- module Audit
11
- module Extractors
12
- autoload :Base, "fontisan/audit/extractors/base"
13
- autoload :Provenance, "fontisan/audit/extractors/provenance"
14
- autoload :Identity, "fontisan/audit/extractors/identity"
15
- autoload :Style, "fontisan/audit/extractors/style"
16
- autoload :Licensing, "fontisan/audit/extractors/licensing"
17
- autoload :Metrics, "fontisan/audit/extractors/metrics"
18
- autoload :Hinting, "fontisan/audit/extractors/hinting"
19
- autoload :ColorCapabilities, "fontisan/audit/extractors/color_capabilities"
20
- autoload :VariationDetail, "fontisan/audit/extractors/variation_detail"
21
- autoload :OpenTypeLayout, "fontisan/audit/extractors/opentype_layout"
22
- autoload :Coverage, "fontisan/audit/extractors/coverage"
23
- autoload :Aggregations, "fontisan/audit/extractors/aggregations"
24
- autoload :LanguageCoverage, "fontisan/audit/extractors/language_coverage"
25
- end
26
- end
27
- end
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- # Pure cross-face aggregation over a list of AuditReports.
6
- #
7
- # No I/O, no font parsing — operates only on already-built reports.
8
- # Easy to spec with synthetic reports and trivially testable. The
9
- # orchestrator ({LibraryAuditor}) handles file discovery and per-face
10
- # auditing; this class owns the rollups that span faces.
11
- #
12
- # Aggregates:
13
- # - aggregate_metrics: sum of total_codepoints and total_glyphs.
14
- # - script_coverage: one ScriptCoverageRow per Unicode script,
15
- # listing faces that cover it.
16
- # - duplicate_groups: files bucketed by source_sha256 (size > 1).
17
- # - license_distribution: face counts keyed by license_url.
18
- class LibraryAggregator
19
- # @param reports [Array<Models::Audit::AuditReport>]
20
- # @return [Hash{Symbol => Object}] keys: :aggregate_metrics,
21
- # :script_coverage, :duplicate_groups, :license_distribution
22
- def aggregate(reports)
23
- {
24
- aggregate_metrics: aggregate_metrics(reports),
25
- script_coverage: build_script_coverage(reports),
26
- duplicate_groups: find_duplicates(reports),
27
- license_distribution: license_distribution(reports),
28
- }
29
- end
30
-
31
- private
32
-
33
- def aggregate_metrics(reports)
34
- {
35
- total_codepoints: reports.sum(&:total_codepoints),
36
- total_glyphs: reports.sum(&:total_glyphs),
37
- }
38
- end
39
-
40
- def build_script_coverage(reports)
41
- by_script = Hash.new { |h, k| h[k] = [] }
42
- reports.each do |report|
43
- face = report.postscript_name || report.source_file
44
- scripts_for(report).each { |script| by_script[script] << face }
45
- end
46
- by_script.map do |script, faces|
47
- Models::Audit::ScriptCoverageRow.new(
48
- script: script,
49
- face_count: faces.size,
50
- faces: faces.uniq.sort,
51
- )
52
- end.sort_by { |row| [-row.face_count, row.script] }
53
- end
54
-
55
- def find_duplicates(reports)
56
- reports.group_by(&:source_sha256)
57
- .select { |_sha, group| group.size > 1 }
58
- .map do |sha, group|
59
- Models::Audit::DuplicateGroup.new(
60
- source_sha256: sha,
61
- files: group.map(&:source_file).sort,
62
- )
63
- end
64
- .sort_by(&:source_sha256)
65
- end
66
-
67
- def license_distribution(reports)
68
- reports.each_with_object({}) do |report, counts|
69
- url = license_url_for(report)
70
- counts[url] = counts.fetch(url, 0) + 1
71
- end
72
- end
73
-
74
- def scripts_for(report)
75
- Array(report.unicode_scripts)
76
- end
77
-
78
- def license_url_for(report)
79
- report.licensing&.license_url || "(none)"
80
- end
81
- end
82
- end
83
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "pathname"
4
-
5
- module Fontisan
6
- module Audit
7
- # Orchestrates a library-wide audit pass.
8
- #
9
- # Owns the file-system side: discovers font files under a root path
10
- # (recursively or not), audits each via {Commands::AuditCommand},
11
- # and assembles a {Models::Audit::LibrarySummary} combining the
12
- # per-face reports with cross-face rollups from {LibraryAggregator}.
13
- #
14
- # Aggregation logic lives in the pure {LibraryAggregator}; this
15
- # class stays focused on discovery + per-face auditing + summary
16
- # assembly. Errors auditing a single file are logged and skipped so
17
- # a corrupt file doesn't abort the whole pass.
18
- class LibraryAuditor
19
- FONT_EXTENSIONS = %w[.ttf .otf .ttc .otc .dfont .woff .woff2
20
- .pfb .pfa .svg].freeze
21
-
22
- # @param root_path [String, Pathname] directory containing fonts
23
- # @param recursive [Boolean] walk into subdirectories
24
- # @param options [Hash] forwarded to AuditCommand (minus library-only keys)
25
- def initialize(root_path, recursive:, options:)
26
- @root_path = Pathname.new(root_path)
27
- @recursive = recursive
28
- @options = options
29
- @aggregator = LibraryAggregator.new
30
- @skipped = []
31
- end
32
-
33
- # @return [Models::Audit::LibrarySummary]
34
- def audit
35
- paths = discover_font_paths
36
- reports = paths.flat_map { |p| audit_one(p) }
37
- rolled_up = aggregates(reports)
38
-
39
- Models::Audit::LibrarySummary.new(
40
- root_path: @root_path.to_s,
41
- total_files: paths.size,
42
- total_faces: reports.size,
43
- scanned_extensions: scanned_extensions(paths),
44
- aggregate_metrics: rolled_up[:aggregate_metrics].merge(
45
- total_size_bytes: paths.sum { |p| File.size(p) },
46
- ),
47
- script_coverage: rolled_up[:script_coverage],
48
- duplicate_groups: rolled_up[:duplicate_groups],
49
- license_distribution: rolled_up[:license_distribution],
50
- per_face_reports: reports,
51
- )
52
- end
53
-
54
- # @return [Array<String>] source files that could not be audited
55
- attr_reader :skipped
56
-
57
- private
58
-
59
- def discover_font_paths
60
- method = @recursive ? :find : :children
61
- @root_path.public_send(method).select do |entry|
62
- next false unless entry.file?
63
- next false if entry.symlink?
64
-
65
- FONT_EXTENSIONS.include?(entry.extname.downcase)
66
- end.map(&:to_s).sort
67
- end
68
-
69
- def audit_one(path)
70
- Array(Commands::AuditCommand.new(path, audit_options).run)
71
- rescue StandardError => e
72
- @skipped << "#{path}: #{e.message}"
73
- []
74
- end
75
-
76
- # Drop library-only options before forwarding to AuditCommand.
77
- def audit_options
78
- @options.except(:recursive, :summary, :output)
79
- end
80
-
81
- def scanned_extensions(paths)
82
- paths.map { |p| File.extname(p).downcase }.uniq.sort
83
- end
84
-
85
- def aggregates(reports)
86
- @aggregator.aggregate(reports)
87
- end
88
- end
89
- end
90
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Fontisan
4
- module Audit
5
- # Ordered list of extractor classes run for every audit face.
6
- #
7
- # Order matters only for human-readable output (text formatter).
8
- # All extractors are independent; their outputs are merged into
9
- # one big hash before constructing the AuditReport.
10
- #
11
- # Add new extractors here. AuditCommand never enumerates them
12
- # directly (OCP: adding a concern = one line here + one file).
13
- module Registry
14
- # Full audit: every concern.
15
- ORDERED_EXTRACTORS = [
16
- Extractors::Provenance,
17
- Extractors::Identity,
18
- Extractors::Style,
19
- Extractors::Licensing,
20
- Extractors::Metrics,
21
- Extractors::Hinting,
22
- Extractors::ColorCapabilities,
23
- Extractors::VariationDetail,
24
- Extractors::OpenTypeLayout,
25
- Extractors::Coverage,
26
- Extractors::Aggregations,
27
- Extractors::LanguageCoverage,
28
- ].freeze
29
-
30
- # Brief audit: only the cheap, name-table-only extractors. Skips
31
- # metrics/hinting/color/variation/layout (extra table loads) and
32
- # aggregations/language coverage (need UCD/CLDR indices). Used by
33
- # `fontisan audit --brief` for a fast inventory pass.
34
- BRIEF_EXTRACTORS = [
35
- Extractors::Provenance,
36
- Extractors::Identity,
37
- Extractors::Style,
38
- Extractors::Licensing,
39
- Extractors::Coverage,
40
- ].freeze
41
-
42
- # Iterate the extractors appropriate for the given mode.
43
- #
44
- # @param mode [Symbol] :full (default) or :brief
45
- # @yieldparam extractor_class [Class]
46
- def self.each(mode: :full, &)
47
- extractors_for(mode).each(&)
48
- end
49
-
50
- # @param mode [Symbol] :full or :brief
51
- # @return [Array<Class>] the extractor list for the given mode
52
- def self.extractors_for(mode)
53
- case mode
54
- when :brief then BRIEF_EXTRACTORS
55
- else ORDERED_EXTRACTORS
56
- end
57
- end
58
- end
59
- end
60
- end