distorted 0.5.3 → 0.6.0

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +661 -0
  3. data/README.md +4 -139
  4. data/font/1252/LICENSE/MoreLessPerfectDOSVGA437/img/Less_Perfect_DOS_VGA.png +0 -0
  5. data/font/1252/LICENSE/MoreLessPerfectDOSVGA437/img/More_Perfect_DOS_VGA.png +0 -0
  6. data/font/1252/LICENSE/MoreLessPerfectDOSVGA437/img/Perfect_DOS_VGA.png +0 -0
  7. data/font/1252/LICENSE/MoreLessPerfectDOSVGA437/less_more_perfect_dos_vga_437.html +52 -0
  8. data/font/1252/LICENSE/PerfectDOSVGA437/font-comment.php@file=perfect_dos_vga_437.html +5 -0
  9. data/font/1252/LessPerfectDOSVGA.ttf +0 -0
  10. data/font/1252/MorePerfectDOSVGA.ttf +0 -0
  11. data/font/1252/Perfect DOS VGA 437 Win.ttf +0 -0
  12. data/font/437/Perfect DOS VGA 437.ttf +0 -0
  13. data/font/437/dos437.txt +72 -0
  14. data/font/65001/Anonymous Pro B.ttf +0 -0
  15. data/font/65001/Anonymous Pro BI.ttf +0 -0
  16. data/font/65001/Anonymous Pro I.ttf +0 -0
  17. data/font/65001/Anonymous Pro.ttf +0 -0
  18. data/font/65001/LICENSE/AnonymousPro/FONTLOG.txt +45 -0
  19. data/font/65001/LICENSE/AnonymousPro/OFL-FAQ.txt +235 -0
  20. data/font/65001/LICENSE/AnonymousPro/OFL.txt +94 -0
  21. data/font/65001/LICENSE/AnonymousPro/README.txt +55 -0
  22. data/font/850/ProFont-Bold-01/LICENSE +22 -0
  23. data/font/850/ProFont-Bold-01/readme.txt +28 -0
  24. data/font/850/ProFontWindows-Bold.ttf +0 -0
  25. data/font/850/ProFontWindows.ttf +0 -0
  26. data/font/850/Profont/LICENSE +22 -0
  27. data/font/850/Profont/readme.txt +31 -0
  28. data/font/932/LICENSE/README-ttf.txt +213 -0
  29. data/font/932/mona.ttf +0 -0
  30. data/lib/distorted/checking_you_out.rb +116 -0
  31. data/lib/distorted/error_code.rb +51 -0
  32. data/lib/distorted/injection_of_love.rb +247 -0
  33. data/lib/distorted/modular_technology/pango.rb +90 -0
  34. data/lib/distorted/modular_technology/triple_counter.rb +45 -0
  35. data/lib/distorted/modular_technology/ttfunk.rb +48 -0
  36. data/lib/distorted/modular_technology/vips.rb +17 -0
  37. data/lib/distorted/modular_technology/vips_load.rb +77 -0
  38. data/lib/distorted/modular_technology/vips_save.rb +172 -0
  39. data/lib/distorted/molecule/C18H27NO3.rb +10 -0
  40. data/lib/distorted/molecule/font.rb +198 -0
  41. data/lib/distorted/molecule/image.rb +36 -0
  42. data/lib/distorted/molecule/pdf.rb +119 -0
  43. data/lib/distorted/molecule/svg.rb +60 -0
  44. data/lib/distorted/molecule/text.rb +225 -0
  45. data/lib/distorted/molecule/video.rb +195 -0
  46. data/lib/distorted/monkey_business/hash.rb +33 -0
  47. data/lib/distorted/monkey_business/mnemoniq.rb +8 -0
  48. data/lib/distorted/monkey_business/set.rb +15 -0
  49. data/lib/distorted/monkey_business/string.rb +6 -0
  50. data/lib/distorted/types/README +4 -0
  51. data/lib/distorted/types/application.yaml +8 -0
  52. data/lib/distorted/types/font.yaml +29 -0
  53. data/lib/distorted/version.rb +22 -0
  54. data/test/distorted_test.rb +11 -0
  55. data/test/test_helper.rb +4 -0
  56. metadata +102 -5
@@ -0,0 +1,45 @@
1
+ TripleCounter = Struct.new(:major, :minor, :micro) do
2
+ attr_reader :major, :minor, :micro
3
+
4
+ def initialize(major = 0, minor = 0, micro = 0)
5
+ @major = major
6
+ @minor = minor
7
+ @micro = micro
8
+ end
9
+
10
+ def to_s
11
+ [major, minor, micro].join('.'.freeze)
12
+ end
13
+
14
+ def ==(otra)
15
+ major == otra.major && minor == otra.minor
16
+ end
17
+
18
+ def ===(otra)
19
+ all_operator(otra, :==)
20
+ end
21
+
22
+ def >=(otra)
23
+ all_operator(otra, :>=)
24
+ end
25
+
26
+ def <=(otra)
27
+ all_operator(otra, :<=)
28
+ end
29
+
30
+ def >(otra)
31
+ all_operator(otra, :>)
32
+ end
33
+
34
+ def <(otra)
35
+ all_operator(otra, :<)
36
+ end
37
+
38
+ def to_array
39
+ [major, minor, micro]
40
+ end
41
+
42
+ def all_operator(otra, operator)
43
+ to_array.zip(otra.to_array).all?{|us, otra| us.send(operator, otra)}
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ require 'ttfunk'
2
+
3
+ module Cooltrainer; end
4
+ module Cooltrainer::DistorteD; end
5
+ module Cooltrainer::DistorteD::Technology; end
6
+ module Cooltrainer::DistorteD::Technology::TTFunk
7
+
8
+ def to_ttfunk
9
+ # TODO: Check that src exists, because TTFunk won't and will just
10
+ # give us an unusable object instead.
11
+ @ttfunk_file ||= TTFunk::File.open(font_path)
12
+ end
13
+
14
+ # Returns a boolean for whether or not this font is monospaced.
15
+ # true == monospace
16
+ # false == proportional
17
+ def font_spacing
18
+ # Monospace fonts will (read: should) have the same width
19
+ # for every glyph, so we can tell a monospace font by
20
+ # checking if a deduplicated widths table has size == 1:
21
+ # irb(main)> font.horizontal_metrics.widths.count
22
+ # => 256
23
+ # irb(main)> font.horizontal_metrics.widths.uniq.compact.length
24
+ # => 1
25
+ to_ttfunk.horizontal_metrics.widths.uniq.compact.length == 1 ? :monospace : :proportional
26
+ end
27
+
28
+ # Returns the Family and Subfamily as one string suitable for libvips
29
+ def font_name
30
+ "#{to_ttfunk.name.font_family.first.encode('UTF-8')} #{to_ttfunk.name.font_subfamily.first.encode('UTF-8')}"
31
+ end
32
+
33
+ # Returns the Pango-Markup-encoded UTF-8 String version + revision of the font
34
+ def font_version
35
+ g_markup_escape_text(to_ttfunk.name&.version&.first&.encode('UTF-8').to_s)
36
+ end
37
+
38
+ # Returns the Pango-Markup-encoded UTF-8 String font file description
39
+ def font_description
40
+ g_markup_escape_text(to_ttfunk.name&.description&.first&.encode('UTF-8').to_s)
41
+ end
42
+
43
+ # Returns the Pango-Markup-encoded UTF-8 String copyright information of the font
44
+ def font_copyright
45
+ g_markup_escape_text(to_ttfunk.name&.copyright&.first&.encode('UTF-8').to_s)
46
+ end
47
+
48
+ end
@@ -0,0 +1,17 @@
1
+ require 'set'
2
+
3
+ require 'distorted/checking_you_out'
4
+
5
+ require 'distorted/modular_technology/vips_load'
6
+ require 'distorted/modular_technology/vips_save'
7
+
8
+
9
+ module Cooltrainer; end
10
+ module Cooltrainer::DistorteD; end
11
+ module Cooltrainer::DistorteD::Technology; end
12
+ module Cooltrainer::DistorteD::Technology::Vips
13
+
14
+ include Cooltrainer::DistorteD::Technology::VipsSave
15
+ include Cooltrainer::DistorteD::Technology::VipsLoad
16
+
17
+ end
@@ -0,0 +1,77 @@
1
+
2
+ require 'set'
3
+
4
+ require 'distorted/checking_you_out'
5
+ require 'distorted/modular_technology/vips_save'
6
+
7
+
8
+ module Cooltrainer; end
9
+ module Cooltrainer::DistorteD; end
10
+ module Cooltrainer::DistorteD::Technology; end
11
+ module Cooltrainer::DistorteD::Technology::VipsLoad
12
+
13
+ # Returns a Set of MIME::Types based on libvips LipsForeignLoad capabilities.
14
+ # NOTE: libvips only declares support (via :get_suffixes) for the "saver" types,
15
+ # but libvips can use additional external libraries for wider media-types support, e.g.:
16
+ #
17
+ # - SVG with librsvg2★ / libcairo. [*]
18
+ # - PDF with PDFium if available, otherwise with libpoppler-glib / libcairo.
19
+ # - OpenEXR/libIlmImf — ILM high dynamic range image format.
20
+ # - maybe more: https://github.com/libvips/libvips/blob/master/configure.ac
21
+ #
22
+ # [FITS]: https://heasarc.gsfc.nasa.gov/docs/heasarc/fits.html
23
+ #
24
+ # [RSVG2]: This is the normal SVG library for the GNOME/GLib world and is
25
+ # probably fine for 95% of use-cases, but I'm pissed off at it because of:
26
+ #
27
+ # - https://gitlab.gnome.org/GNOME/librsvg/-/issues/56
28
+ # - https://gitlab.gnome.org/GNOME/librsvg/-/issues/100
29
+ # - https://gitlab.gnome.org/GNOME/librsvg/-/issues/183
30
+ # - https://gitlab.gnome.org/GNOME/librsvg/-/issues/494
31
+ # - https://bugzilla.gnome.org/show_bug.cgi?id=666477
32
+ # - https://phabricator.wikimedia.org/T35245
33
+ #
34
+ # TLDR: SVG <tspan> elements' [:x, :y, :dy, :dx] attributes can be
35
+ # a space-delimited list of position values for individual
36
+ # characters in the <tspan>, but librsvg2 only supported reading
37
+ # those attributes as a single one-shot numeric value.
38
+ # Documents using this totally-common and totally-in-spec feature
39
+ # rendered incorrectly with librsvg2. Effected <tspan> elements'
40
+ # subsequent children would hug one edge of the rendered output.
41
+ #
42
+ # And wouldn't you know it but the one (1) SVG on my website
43
+ # at the time I built this feature (IIDX-Turntable-parts.svg) used
44
+ # this feature for the double-digit parts diagram labels.
45
+ # I ended up having to edit my input document to just squash the
46
+ # offending <tspan>s down to a single child each.
47
+ # I guess that's semantically more correct in my document since they are
48
+ # numbers like Eleven and not two separate characters like '1 1'
49
+ # but still ugh lol
50
+ #
51
+ # This was finally fixed in 2019 as of librsvg2 version 2.45.91 :)
52
+ # https://gitlab.gnome.org/GNOME/librsvg/-/issues/494#note_579774
53
+ #
54
+
55
+ # TODO: Figure out how to detect non-Magick non-Saver Loader support,
56
+ # by which I mean "everything not included in :get_suffixes".
57
+ # NOTE: The Magick-based '.bmp' loader is broken/missing in libvips <= 8.9.1:
58
+ # https://github.com/libvips/libvips/issues/1528
59
+ # irb> MIME::Types.type_for('.bmp')
60
+ # => [#<MIME::Type: image/bmp>, #<MIME::Type: image/x-bmp>, #<MIME::Type: image/x-ms-bmp>]
61
+ # irb> MIME::Types.type_for('.bmp').map(&:preferred_extension)
62
+ # => ["bmp", "bmp", "bmp"]
63
+ LOWER_WORLD = (VIPS_AVAILABLE_VER < TripleCounter.new(8, 9, 1)) ?
64
+ Cooltrainer::DistorteD::Technology::VipsSave::OUTER_LIMITS.keep_if { |t| t.preferred_extension != 'bmp'.freeze } :
65
+ Cooltrainer::DistorteD::Technology::VipsSave::OUTER_LIMITS
66
+
67
+ def to_vips_image
68
+ # TODO: Learn more about what VipsAccess means for our use case,
69
+ # if the default should be changed, and if it should be
70
+ # a user-controllable attr or not.
71
+ # https://libvips.github.io/libvips/API/current/VipsImage.html#VipsAccess
72
+ # https://libvips.github.io/libvips/API/current/How-it-opens-files.md.html
73
+ @vips_image ||= Vips::Image.new_from_file(path)
74
+ end
75
+
76
+
77
+ end
@@ -0,0 +1,172 @@
1
+
2
+ # Requiring libvips 8.8 for HEIC/HEIF (moo) support, `justify` support in the
3
+ # Vips::Image text operator, animated WebP support, and more:
4
+ # https://libvips.github.io/libvips/2019/04/22/What's-new-in-8.8.html
5
+
6
+ require 'distorted/modular_technology/triple_counter'
7
+ VIPS_MINIMUM_VER = TripleCounter.new(8, 8, 0)
8
+
9
+ # Tell the user to install the shared library if it's missing.
10
+ begin
11
+ require 'vips'
12
+ VIPS_AVAILABLE_VER = TripleCounter.new(Vips::version(0), Vips::version(1), Vips::version(2))
13
+
14
+ unless VIPS_AVAILABLE_VER >= VIPS_MINIMUM_VER
15
+ raise LoadError.new(
16
+ "DistorteD needs libvips #{VIPS_MINIMUM_VER}, but the available version is '#{Vips::version_string}'"
17
+ )
18
+ end
19
+
20
+ rescue LoadError => le
21
+ # Only match libvips.so load failure
22
+ raise unless le.message =~ /libvips.so/
23
+
24
+ # Multiple OS help
25
+ help = <<~INSTALL
26
+
27
+ Please install the VIPS (libvips) image processing library, version #{VIPS_MINIMUM_VER} or later.
28
+
29
+ FreeBSD:
30
+ pkg install graphics/vips
31
+
32
+ macOS:
33
+ brew install vips
34
+
35
+ Debian/Ubuntu/Mint:
36
+ apt install libvips libvips-dev
37
+ INSTALL
38
+
39
+ # Re-raise with install message
40
+ raise $!, "#{help}\n#{$!}", $!.backtrace
41
+ end
42
+
43
+
44
+ require 'set'
45
+
46
+ require 'distorted/checking_you_out'
47
+
48
+
49
+ module Cooltrainer; end
50
+ module Cooltrainer::DistorteD; end
51
+ module Cooltrainer::DistorteD::Technology; end
52
+ module Cooltrainer::DistorteD::Technology::VipsSave
53
+
54
+ ATTRIBUTES = {
55
+ :crop => nil,
56
+ :Q => Set[:quality],
57
+ }
58
+ ATTRIBUTES_DEFAULT = {
59
+ :crop => :attention,
60
+ }
61
+ ATTRIBUTES_VALUES = {
62
+ # https://www.rubydoc.info/gems/ruby-vips/Vips/Interesting
63
+ :crop => {
64
+ :none => nil,
65
+ :centre => Set[:center], # America, FUCK YEAH!
66
+ :entropy => nil,
67
+ :attention => nil,
68
+ },
69
+ }
70
+
71
+ # Returns a Set of MIME::Types based on libvips VipsForeignSave capabilities.
72
+ # https://libvips.github.io/libvips/API/current/VipsForeignSave.html
73
+ #
74
+ # There is one (only one) native libvips image format, with file extname `.vips`.
75
+ # As I write this—running libvips 8.8—the :get_suffixes function does not include
76
+ # its own '.vips' as a supported extension.
77
+ # There also (as of mid 2020) seems to be no official media-type assigned
78
+ # for VIPS format, so I am going to make one up in CHECKING::YOU::OUT's local-data.
79
+ # - Raw pixel data
80
+ #
81
+ # [RAW]: https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-rawload
82
+ # https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-csvload
83
+ #
84
+ # Most libvips installations, even very minimally-built ones,
85
+ # will almost certainly support a few very common formats:
86
+ # - JPEG with libjpeg.
87
+ # - PNG with libpng.
88
+ # - GIF with giflib.
89
+ # - WebP with libwebp.
90
+ # - TIFF with libtiff.
91
+ #
92
+ # Normal libvips installations probably also support many less-mainstream formats:
93
+ # - HEIF/HEIC with libheif.
94
+ # - ICC profiles with liblcms2.
95
+ # - Matlab with matio/libhdf5.
96
+ # - FITS★ with cfitsio.
97
+ # - Styled text with Pango/ft2.
98
+ # - Saving GIF/BMP with Magick.
99
+ # NOTE that GIFs are *loaded* using giflib,
100
+ # and that BMP loading is unsupported.
101
+ # - Various simple ASCII/binary-based formats with libgsf★
102
+ # · Comma-separated values
103
+ # · Netpbm★
104
+ # · VIPS (non-Matlab) matrices★
105
+ #
106
+ # [NETPBM]: https://en.wikipedia.org/wiki/Netpbm#File_formats
107
+ # [LIBGSF]: https://developer.gnome.org/gsf/
108
+ # [MATRIX]: https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-matrixload
109
+
110
+ # Vips allows us to query supported *SAVE* types by suffix.
111
+ # There's a simple relationship between filetype and extension since
112
+ # libvips uses the suffix to pick the Saver module.
113
+ #
114
+ # Loader modules, on the other hand, are picked by sniffing the
115
+ # first few bytes of the file, so a list of file extensions for
116
+ # supported loadable formats won't always be complete.
117
+ # For example, SVG and PDF are usually supported as loaders
118
+ # (via rsvg and PDFium/Poppler)
119
+ # https://github.com/libvips/ruby-vips/issues/186
120
+ #
121
+ # irb(main)> Vips.get_suffixes
122
+ # => [".csv", ".mat", ".v", ".vips", ".ppm", ".pgm", ".pbm", ".pfm",
123
+ # ".hdr", ".dz", ".png", ".jpg", ".jpeg", ".jpe", ".webp", ".tif",
124
+ # ".tiff", ".fits", ".fit", ".fts", ".gif", ".bmp"]
125
+ OUTER_LIMITS = Vips.get_suffixes.map{ |t|
126
+ # A single call to this will return a Set of MIME::Types for a String input
127
+ CHECKING::YOU::OUT(t)
128
+ }.reduce { |c,t|
129
+ # Flatten the Set-of-Sets-of-Types into a Set-of-Types
130
+ (c || Set[]).merge(t)
131
+ }.keep_if { |t|
132
+ # Filter out any of libvips' supported output Types that aren't
133
+ # actually images (e.g. CSV)
134
+ t.media_type == 'image'
135
+ }
136
+
137
+ # Define a to_<mediatype>_<subtype> method for each MIME::Type supported by libvips,
138
+ # e.g. a supported Type 'image/png' will define a method :to_image_png in any
139
+ # context where this module is included.
140
+ self::OUTER_LIMITS.each { |t|
141
+ define_method(t.distorted_method) { |*a, **k, &b|
142
+ vips_save(*a, **k, &b)
143
+ }
144
+ }
145
+
146
+ protected
147
+
148
+ # Generic Vips saver method, optionally handling resizing and cropping.
149
+ # NOTE: libvips chooses a saver (internally) based on the extname of the destination path.
150
+ def vips_save(dest, width: nil, **kw)
151
+ begin
152
+ if width.nil? or width == :full
153
+ return to_vips_image.write_to_file(dest)
154
+ elsif width.respond_to?(:to_i)
155
+ ver = to_vips_image.thumbnail_image(
156
+ width.to_i,
157
+ # Use `self` namespace for constants so subclasses can redefine
158
+ **{:crop => abstract(:crop)},
159
+ )
160
+ return ver.write_to_file(dest)
161
+ end
162
+ rescue Vips::Error => v
163
+ if v.message.include?('No known saver')
164
+ # TODO: Handle missing output formats. Replacements? Skip it? Die?
165
+ return nil
166
+ else
167
+ raise
168
+ end
169
+ end
170
+ end # save
171
+
172
+ end
@@ -0,0 +1,10 @@
1
+ require 'distorted/injection_of_love'
2
+
3
+ module Cooltrainer; end
4
+ module Cooltrainer::DistorteD; end
5
+ module Cooltrainer::DistorteD::Molecule; end
6
+ module Cooltrainer::DistorteD::Molecule::C18H27NO3
7
+
8
+ BOOLEAN_ATTR_VALUES = Set[0, 1, false, true, '0'.freeze, '1'.freeze, 'false'.freeze, 'true'.freeze]
9
+
10
+ end
@@ -0,0 +1,198 @@
1
+ require 'set'
2
+
3
+ # Font metadata extraction
4
+ require 'ttfunk'
5
+
6
+ require 'distorted/modular_technology/pango'
7
+ require 'distorted/modular_technology/ttfunk'
8
+ require 'distorted/modular_technology/vips_save'
9
+ require 'distorted/checking_you_out'
10
+ require 'distorted/injection_of_love'
11
+
12
+
13
+ module Cooltrainer
14
+ module DistorteD
15
+ module Font
16
+
17
+
18
+ # TODO: Test OTF, OTB, and others.
19
+ # NOTE: Traditional bitmap fonts won't be supported due to Pango 1.44
20
+ # and later switching to Harfbuzz from Freetype:
21
+ # https://gitlab.gnome.org/GNOME/pango/-/issues/386
22
+ # https://blogs.gnome.org/mclasen/2019/05/25/pango-future-directions/
23
+ LOWER_WORLD = CHECKING::YOU::IN(/^font\/ttf/)
24
+ OUTER_LIMITS = CHECKING::YOU::IN(/^font\/ttf/)
25
+
26
+ ATTRIBUTES = Set[
27
+ :alt,
28
+ ]
29
+ ATTRIBUTES_VALUES = {
30
+ }
31
+ ATTRIBUTES_DEFAULT = {
32
+ }
33
+
34
+
35
+ # Maybe T0DO: Process output with TTFunk instead of only using it
36
+ # to generate images and metadata.
37
+ self::OUTER_LIMITS.each { |t|
38
+ define_method(t.distorted_method) { |*a, **k, &b|
39
+ copy_file(*a, **k, &b)
40
+ }
41
+ }
42
+
43
+ include Cooltrainer::DistorteD::Technology::TTFunk
44
+ include Cooltrainer::DistorteD::Technology::Pango
45
+ include Cooltrainer::DistorteD::Technology::VipsSave
46
+ include Cooltrainer::DistorteD::InjectionOfLove
47
+
48
+
49
+ # irb(main):089:0> chars.take(5)
50
+ # => [[1, 255], [2, 1], [3, 2], [4, 3], [5, 4]]
51
+ # irb(main):090:0> chars.values.take(5)
52
+ # => [255, 1, 2, 3, 4]
53
+ # irb(main):091:0> chars.values.map(&:chr).take(5)
54
+ # => ["\xFF", "\x01", "\x02", "\x03", "\x04"]
55
+ def to_pango
56
+ output = '' << cr << '<span>' << cr
57
+
58
+ output << "<span size='35387'> #{font_name}</span>" << cr << cr
59
+
60
+ output << "<span size='24576'> #{font_description}</span>" << cr
61
+ output << "<span size='24576'> #{font_copyright}</span>" << cr
62
+ output << "<span size='24576'> #{font_version}</span>" << cr << cr
63
+
64
+ # Print a preview String in using the loaded font. Or don't.
65
+ if abstract(:title)
66
+ output << cr << cr << "<span size='24576' foreground='grey'> #{g_markup_escape_text(abstract(:title))}</span>" << cr << cr << cr
67
+ end
68
+
69
+ # /!\ MANDATORY READING /!\
70
+ # https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html
71
+ #
72
+ # "The 'cmap' table maps character codes to glyph indices.
73
+ # The choice of encoding for a particular font is dependent upon the conventions
74
+ # used by the intended platform. A font intended to run on multiple platforms
75
+ # with different encoding conventions will require multiple encoding tables.
76
+ # As a result, the 'cmap' table may contain multiple subtables,
77
+ # one for each supported encoding scheme."
78
+ #
79
+ # Cmap#unicode is a convenient shortcut to sorting the subtables
80
+ # and removing any unusable ones:
81
+ # https://github.com/prawnpdf/ttfunk/blob/master/lib/ttfunk/table/cmap.rb
82
+ #
83
+ # irb(main):174:0> font_meta.cmap.tables.count
84
+ # => 3
85
+ # irb(main):175:0> font_meta.cmap.unicode.count
86
+ # => 2
87
+ to_ttfunk.cmap.tables.each do |table|
88
+ next if !table.unicode?
89
+ # Each subtable's `code_map` is a Hash map of character codes (the Hash keys)
90
+ # to the glyph IDs from the original font (the Hash's values).
91
+ #
92
+ # Subtable::encode takes:
93
+ # - a Hash mapping character codes to original font glyph IDs.
94
+ # - the desired output encoding — Set[:mac_roman, :unicode, :unicode_ucs4]
95
+ # https://github.com/prawnpdf/ttfunk/blob/master/lib/ttfunk/table/cmap/subtable.rb
96
+ # …and returns a Hash with keys:
97
+ # - :charmap — Hash mapping the characters in the input charmap
98
+ # to a another hash containing both the `:old`
99
+ # and `:new` glyph ids for each character code.
100
+ # - :subtable — String encoded subtable for the given encoding.
101
+ encoded = TTFunk::Table::Cmap::Subtable::encode(table&.code_map, :unicode).dig(:charmap)
102
+
103
+ output << "<span size='49152'>"
104
+
105
+ i = 0
106
+ encoded.each_pair { |c, (old, new)|
107
+
108
+ begin
109
+ if glyph = to_ttfunk.glyph_outlines.for(c)
110
+ # Add a space on either side of the character so they aren't
111
+ # all smooshed up against each other and unreadable.
112
+ output << ' ' << g_markup_escape_char(c) << ' '
113
+ if i >= 15
114
+ output << cr
115
+ i = 0
116
+ else
117
+ i = i + 1
118
+ end
119
+ else
120
+ end
121
+ rescue NoMethodError => nme
122
+ # TTFunk's `glyph_outlines.for()` will raise this if we call it
123
+ # for a codepoint that does not exist in the font, which we will
124
+ # not do because we are enumerating the codepoints in the font,
125
+ # but we should still handle the possibility.
126
+ # irb(main):060:0> font.glyph_outlines.for(555555)
127
+ #
128
+ # Traceback (most recent call last):
129
+ # 6: from /usr/bin/irb:23:in `<main>'
130
+ # 5: from /usr/bin/irb:23:in `load'
131
+ # 4: from /home/okeeblow/.gems/gems/irb-1.2.4/exe/irb:11:in `<top (required)>'
132
+ # 3: from (irb):60
133
+ # 2: from /home/okeeblow/.gems/gems/ttfunk-1.6.2.1/lib/ttfunk/table/glyf.rb:35:in `for'
134
+ # 1: from /home/okeeblow/.gems/gems/ttfunk-1.6.2.1/lib/ttfunk/table/loca.rb:35:in `size_of'
135
+ # NoMethodError (undefined method `-' for nil:NilClass)
136
+ end
137
+ }
138
+
139
+ output << '</span>' << cr
140
+ end
141
+
142
+ output << '</span>'
143
+ output
144
+ end
145
+
146
+ # Return the `src` as the font_path since we aren't using
147
+ # any of the built-in fonts.
148
+ def font_path
149
+ path
150
+ end
151
+
152
+ def to_vips_image
153
+ # https://libvips.github.io/libvips/API/current/libvips-create.html#vips-text
154
+ Vips::Image.text(
155
+ # This string must be well-escaped Pango Markup:
156
+ # https://developer.gnome.org/pango/stable/pango-Markup.html
157
+ # However the official function for escaping text is
158
+ # not implemented in Ruby GLib, so we have to do it ourselves.
159
+ to_pango,
160
+ **{
161
+ # String absolute path to TTF
162
+ :fontfile => font_path,
163
+ # It's not enough to just specify the TTF path;
164
+ # we must also specify a font family, subfamily, and size.
165
+ :font => "#{font_name}",
166
+ # Space between lines (in Points).
167
+ :spacing => to_ttfunk.line_gap,
168
+ # Requires libvips 8.8
169
+ :justify => false,
170
+ :dpi => 144,
171
+ },
172
+ )
173
+ end
174
+
175
+ end # Font
176
+ end # DistorteD
177
+ end # Cooltrainer
178
+
179
+
180
+ # Notes on file-format specifics and software-library-specifics
181
+ #
182
+ # # TTF (via TTFunk)
183
+ #
184
+ # ## Cmap
185
+ #
186
+ # Each TTFunk::Table::Cmap::Format<whatever> class responds to `:supported?`
187
+ # with its own internal boolean telling us if that Format is usable in TTFunk.
188
+ # This has nothing to do with any font file itself, just the library code.
189
+ # irb(main)> font.cmap.tables.map{|t| t.supported?}
190
+ # => [true, true, true]
191
+ #
192
+ # Any subclass of TTFunk::Table::Cmap::Subtable responds to `:unicode?`
193
+ # with a boolean calculated from the instance `@platform_id` and `@encoding_id`,
194
+ # and those numeric IDs are assigned to the symbolic (e.g. `:macroman`) names in:
195
+ # https://github.com/prawnpdf/ttfunk/blob/master/lib/ttfunk/table/cmap/subtable.rb
196
+ # irb(main)> font.cmap.tables.map{|t| t.unicode?}
197
+ # => [true, false, true]
198
+ #