stationery 0.1.0 → 0.3.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 (138) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +114 -1
  3. data/README.md +395 -25
  4. data/exe/stationery +6 -0
  5. data/lib/generators/stationery/fonts_generator.rb +24 -0
  6. data/lib/stationery/builder.rb +26 -3
  7. data/lib/stationery/canvas/debug.rb +26 -0
  8. data/lib/stationery/canvas/path.rb +22 -12
  9. data/lib/stationery/canvas/text.rb +10 -6
  10. data/lib/stationery/canvas.rb +38 -8
  11. data/lib/stationery/cli/fonts.rb +85 -0
  12. data/lib/stationery/cli/render.rb +137 -0
  13. data/lib/stationery/cli.rb +74 -0
  14. data/lib/stationery/document.rb +60 -9
  15. data/lib/stationery/elements/lists.rb +58 -0
  16. data/lib/stationery/elements/rich.rb +27 -0
  17. data/lib/stationery/elements.rb +88 -12
  18. data/lib/stationery/errors.rb +13 -1
  19. data/lib/stationery/fonts/bundled.rb +25 -0
  20. data/lib/stationery/fonts/catalog.rb +101 -0
  21. data/lib/stationery/fonts/cff.rb +140 -0
  22. data/lib/stationery/fonts/cff_subset.rb +145 -0
  23. data/lib/stationery/fonts/data/Inter-Bold.ttf +0 -0
  24. data/lib/stationery/fonts/data/Inter-BoldItalic.ttf +0 -0
  25. data/lib/stationery/fonts/data/Inter-Italic.ttf +0 -0
  26. data/lib/stationery/fonts/data/Inter-Regular.ttf +0 -0
  27. data/lib/stationery/fonts/data/OFL.txt +92 -0
  28. data/lib/stationery/fonts/data/README.md +14 -0
  29. data/lib/stationery/fonts/embedding/base.rb +56 -0
  30. data/lib/stationery/fonts/embedding/cff.rb +35 -0
  31. data/lib/stationery/fonts/embedding/true_type.rb +35 -0
  32. data/lib/stationery/fonts/fallback.rb +58 -0
  33. data/lib/stationery/fonts/family.rb +11 -0
  34. data/lib/stationery/fonts/font.rb +68 -88
  35. data/lib/stationery/fonts/font_book.rb +49 -8
  36. data/lib/stationery/fonts/glyph_run.rb +39 -0
  37. data/lib/stationery/fonts/gpos/class_def.rb +25 -0
  38. data/lib/stationery/fonts/gpos/coverage.rb +20 -0
  39. data/lib/stationery/fonts/gpos/pair_pos.rb +83 -0
  40. data/lib/stationery/fonts/gpos.rb +64 -0
  41. data/lib/stationery/fonts/gsub/ligature_subst.rb +44 -0
  42. data/lib/stationery/fonts/gsub.rb +65 -0
  43. data/lib/stationery/fonts/installer.rb +121 -0
  44. data/lib/stationery/fonts/kern_table.rb +52 -0
  45. data/lib/stationery/fonts/kerning.rb +17 -0
  46. data/lib/stationery/fonts/ligatures.rb +16 -0
  47. data/lib/stationery/fonts/packs.rb +45 -0
  48. data/lib/stationery/fonts/registry.rb +18 -9
  49. data/lib/stationery/fonts/sfnt_metrics.rb +51 -0
  50. data/lib/stationery/fonts/to_unicode.rb +36 -0
  51. data/lib/stationery/fonts/true_type.rb +64 -57
  52. data/lib/stationery/fonts/woff.rb +52 -0
  53. data/lib/stationery/html/document.rb +11 -0
  54. data/lib/stationery/html/tokenizer.rb +74 -0
  55. data/lib/stationery/html/tree_builder/blocks.rb +137 -0
  56. data/lib/stationery/html/tree_builder/collector.rb +50 -0
  57. data/lib/stationery/html/tree_builder.rb +72 -0
  58. data/lib/stationery/html/whitespace.rb +43 -0
  59. data/lib/stationery/layout/box.rb +102 -16
  60. data/lib/stationery/layout/flow.rb +48 -15
  61. data/lib/stationery/layout/image.rb +1 -0
  62. data/lib/stationery/layout/list_item.rb +89 -0
  63. data/lib/stationery/layout/mark.rb +58 -0
  64. data/lib/stationery/layout/node.rb +26 -2
  65. data/lib/stationery/layout/paginator.rb +35 -6
  66. data/lib/stationery/layout/positioned.rb +3 -1
  67. data/lib/stationery/layout/row.rb +34 -4
  68. data/lib/stationery/layout/svg.rb +48 -0
  69. data/lib/stationery/layout/table/cell.rb +33 -6
  70. data/lib/stationery/layout/table/grid.rb +93 -0
  71. data/lib/stationery/layout/table/row_splitter.rb +36 -0
  72. data/lib/stationery/layout/table/selection.rb +3 -2
  73. data/lib/stationery/layout/table.rb +69 -23
  74. data/lib/stationery/layout/table_of_contents/entry.rb +56 -0
  75. data/lib/stationery/layout/table_of_contents.rb +43 -0
  76. data/lib/stationery/layout/text.rb +6 -4
  77. data/lib/stationery/layout/wrap.rb +78 -0
  78. data/lib/stationery/list_markers.rb +47 -0
  79. data/lib/stationery/markdown/block_parser/containers.rb +94 -0
  80. data/lib/stationery/markdown/block_parser/tables.rb +54 -0
  81. data/lib/stationery/markdown/block_parser.rb +141 -0
  82. data/lib/stationery/markdown/document.rb +50 -0
  83. data/lib/stationery/markdown/inline_parser/emphasis.rb +80 -0
  84. data/lib/stationery/markdown/inline_parser/links.rb +70 -0
  85. data/lib/stationery/markdown/inline_parser/nodes.rb +39 -0
  86. data/lib/stationery/markdown/inline_parser.rb +89 -0
  87. data/lib/stationery/minitest.rb +33 -0
  88. data/lib/stationery/outline.rb +37 -0
  89. data/lib/stationery/page.rb +18 -4
  90. data/lib/stationery/page_templates.rb +33 -10
  91. data/lib/stationery/pdf/assembler.rb +20 -8
  92. data/lib/stationery/pdf/encryption/aes.rb +37 -0
  93. data/lib/stationery/pdf/encryption/rc4.rb +33 -0
  94. data/lib/stationery/pdf/encryption/revision4.rb +48 -0
  95. data/lib/stationery/pdf/encryption/revision6.rb +56 -0
  96. data/lib/stationery/pdf/encryption/standard_security.rb +78 -0
  97. data/lib/stationery/pdf/outline_writer.rb +72 -0
  98. data/lib/stationery/pdf/serializer.rb +22 -10
  99. data/lib/stationery/pdf/writer.rb +28 -5
  100. data/lib/stationery/preview.rb +64 -0
  101. data/lib/stationery/rails/previews_controller.rb +61 -0
  102. data/lib/stationery/rails.rb +2 -0
  103. data/lib/stationery/railtie.rb +60 -0
  104. data/lib/stationery/regions.rb +67 -0
  105. data/lib/stationery/resources.rb +8 -2
  106. data/lib/stationery/rich/nodes.rb +55 -0
  107. data/lib/stationery/rich/renderer/inlines.rb +53 -0
  108. data/lib/stationery/rich/renderer.rb +121 -0
  109. data/lib/stationery/rich/styles.rb +34 -0
  110. data/lib/stationery/rspec.rb +6 -0
  111. data/lib/stationery/structure.rb +78 -0
  112. data/lib/stationery/svg/arc.rb +91 -0
  113. data/lib/stationery/svg/bounds.rb +76 -0
  114. data/lib/stationery/svg/document.rb +103 -0
  115. data/lib/stationery/svg/gradient.rb +115 -0
  116. data/lib/stationery/svg/painter.rb +70 -0
  117. data/lib/stationery/svg/parser.rb +58 -0
  118. data/lib/stationery/svg/path_data.rb +137 -0
  119. data/lib/stationery/svg/selector.rb +37 -0
  120. data/lib/stationery/svg/shading.rb +34 -0
  121. data/lib/stationery/svg/shapes.rb +54 -0
  122. data/lib/stationery/svg/style.rb +85 -0
  123. data/lib/stationery/svg/stylesheet.rb +47 -0
  124. data/lib/stationery/svg/text.rb +122 -0
  125. data/lib/stationery/svg/transform.rb +54 -0
  126. data/lib/stationery/testing/inspector.rb +98 -0
  127. data/lib/stationery/testing/matchers.rb +123 -0
  128. data/lib/stationery/text/entities/html4.rb +49 -0
  129. data/lib/stationery/text/entities.rb +6 -1
  130. data/lib/stationery/text/line.rb +8 -2
  131. data/lib/stationery/text/paragraph.rb +33 -11
  132. data/lib/stationery/text/runs_builder.rb +9 -1
  133. data/lib/stationery/text/style.rb +4 -2
  134. data/lib/stationery/text/wrapper.rb +4 -2
  135. data/lib/stationery/version.rb +1 -1
  136. data/lib/stationery/warnings.rb +65 -0
  137. data/lib/stationery.rb +67 -0
  138. metadata +101 -4
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stationery
4
+ module Fonts
5
+ # Rewrites a CFF font so every glyph a document did not draw is a bare
6
+ # `endchar`. Glyph ids, the charset, FDSelect, the Private DICTs and all
7
+ # Subrs are kept byte for byte, so CIDs and hinting are unchanged; only the
8
+ # CharStrings INDEX shrinks. Offsets in the Top DICT and the Font DICTs are
9
+ # written as fixed five-byte integers so their size does not depend on the
10
+ # layout they describe. Charstrings are not interpreted: a deprecated
11
+ # `seac` accent in a drawn glyph would lose its base and accent glyphs.
12
+ module CffSubset
13
+ ENDCHAR = "\x0E".b
14
+
15
+ module_function
16
+
17
+ # Header, Name INDEX, Top DICT INDEX, String and Global Subr INDEXes,
18
+ # then the blocks the Top DICT points at.
19
+ def build(cff, gids)
20
+ blocks = blocks(cff, gids)
21
+ head = cff.data.byteslice(0, cff.name_index.stop)
22
+ globals = cff.data.byteslice(cff.string_index.start, cff.global_subrs.stop - cff.string_index.start)
23
+ pos = head.bytesize + index([top_dict(cff, blocks.transform_values { 0 })]).bytesize + globals.bytesize
24
+ offsets = blocks.transform_values { |block| pos.tap { pos += block.bytesize } }
25
+ blocks[:fd_array] = fd_array(cff, offsets) if cff.cid_keyed?
26
+
27
+ [head, index([top_dict(cff, offsets)]), globals, *blocks.values].join
28
+ end
29
+
30
+ # The FDArray is sized here with placeholder offsets; build fills them in.
31
+ def blocks(cff, gids)
32
+ keep = Set.new(gids) << 0
33
+ charstrings = Array.new(cff.num_glyphs) { |gid| keep.include?(gid) ? cff.item(cff.charstrings, gid) : ENDCHAR }
34
+ blocks = tables(cff).merge(charstrings: index(charstrings))
35
+ privates(cff).each_with_index { |region, i| blocks[[:private, i]] = region }
36
+ blocks[:fd_array] = fd_array(cff, Hash.new(0)) if cff.cid_keyed?
37
+ blocks
38
+ end
39
+
40
+ # Charset, custom Encoding and FDSelect, copied as they are.
41
+ def tables(cff)
42
+ data = cff.data
43
+ top = cff.top
44
+ blocks = {}
45
+ if (offset = top.fetch(CFF::CHARSET, [0]).first) > 2
46
+ blocks[:charset] = data.byteslice(offset, charset_size(data, offset, cff.num_glyphs))
47
+ end
48
+ if (offset = top.fetch(CFF::ENCODING, [0]).first) > 1
49
+ blocks[:encoding] = data.byteslice(offset, encoding_size(data, offset))
50
+ end
51
+ if (offset = top[CFF::FD_SELECT]&.first)
52
+ blocks[:fd_select] = data.byteslice(offset, fd_select_size(data, offset, cff.num_glyphs))
53
+ end
54
+ blocks
55
+ end
56
+
57
+ # Each Private DICT with its local Subrs, as one region so the Subrs
58
+ # offset (relative to the Private DICT) still holds.
59
+ def privates(cff)
60
+ font_dicts(cff).map do |dict|
61
+ size, offset = dict.fetch(CFF::PRIVATE)
62
+ stop = offset + size
63
+ if (subrs = CFF.parse_dict(cff.data.byteslice(offset, size))[CFF::SUBRS])
64
+ stop = [stop, cff.index_at(offset + subrs.first).stop].max
65
+ end
66
+ cff.data.byteslice(offset, stop - offset)
67
+ end
68
+ end
69
+
70
+ def font_dicts(cff)
71
+ return [cff.top] unless cff.cid_keyed?
72
+
73
+ fd_array = cff.index_at(cff.top.fetch(CFF::FD_ARRAY).first)
74
+ fd_array.items.each_index.map { |i| CFF.parse_dict(cff.item(fd_array, i)) }
75
+ end
76
+
77
+ def top_dict(cff, offsets)
78
+ encode(CFF.dict_entries(cff.item(cff.top_index, 0))) do |op, operands|
79
+ case op
80
+ when CFF::CHARSET then offsets.key?(:charset) ? [offsets[:charset]] : operands
81
+ when CFF::ENCODING then offsets.key?(:encoding) ? [offsets[:encoding]] : operands
82
+ when CFF::CHARSTRINGS then [offsets[:charstrings]]
83
+ when CFF::FD_SELECT then [offsets[:fd_select]]
84
+ when CFF::FD_ARRAY then [offsets[:fd_array]]
85
+ when CFF::PRIVATE then [operands.first, offsets[[:private, 0]]]
86
+ end
87
+ end
88
+ end
89
+
90
+ def fd_array(cff, offsets)
91
+ fd_array = cff.index_at(cff.top.fetch(CFF::FD_ARRAY).first)
92
+ dicts = fd_array.items.each_index.map do |i|
93
+ encode(CFF.dict_entries(cff.item(fd_array, i))) do |op, operands|
94
+ [operands.first, offsets[[:private, i]]] if op == CFF::PRIVATE
95
+ end
96
+ end
97
+ index(dicts)
98
+ end
99
+
100
+ # Entries the block answers are rewritten with five-byte integers; the
101
+ # rest keep their original bytes.
102
+ def encode(entries)
103
+ entries.map do |op, operands, raw|
104
+ values = yield(op, operands)
105
+ next raw unless values
106
+
107
+ values.map { |value| [29, value].pack("Cl>") }.join + (op >= 1200 ? [12, op - 1200].pack("CC") : op.chr)
108
+ end.join.b
109
+ end
110
+
111
+ def index(items)
112
+ offsets = items.each_with_object([1]) { |item, acc| acc << (acc.last + item.bytesize) }
113
+ size = [1, 2, 3, 4].find { |bytes| offsets.last < 256**bytes }
114
+ [items.size, size].pack("nC") + offsets.map { |offset| [offset].pack("N").byteslice(4 - size, size) }.join +
115
+ items.join
116
+ end
117
+
118
+ def charset_size(data, offset, num_glyphs)
119
+ format = data.getbyte(offset)
120
+ return 1 + ((num_glyphs - 1) * 2) if format.zero?
121
+
122
+ range = format == 1 ? 3 : 4
123
+ pos = offset + 1
124
+ covered = 1
125
+ while covered < num_glyphs
126
+ covered += 1 + (format == 1 ? data.getbyte(pos + 2) : data.byteslice(pos + 2, 2).unpack1("n"))
127
+ pos += range
128
+ end
129
+ pos - offset
130
+ end
131
+
132
+ def encoding_size(data, offset)
133
+ format = data.getbyte(offset)
134
+ count = data.getbyte(offset + 1)
135
+ size = 2 + (format.nobits?(0x7F) ? count : count * 2)
136
+ size += 1 + (data.getbyte(offset + size) * 3) if format.allbits?(0x80)
137
+ size
138
+ end
139
+
140
+ def fd_select_size(data, offset, num_glyphs)
141
+ data.getbyte(offset).zero? ? 1 + num_glyphs : 5 + (data.byteslice(offset + 1, 2).unpack1("n") * 3)
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,92 @@
1
+ Copyright (c) 2016 The Inter Project Authors (https://github.com/rsms/inter)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ http://scripts.sil.org/OFL
6
+
7
+ -----------------------------------------------------------
8
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
9
+ -----------------------------------------------------------
10
+
11
+ PREAMBLE
12
+ The goals of the Open Font License (OFL) are to stimulate worldwide
13
+ development of collaborative font projects, to support the font creation
14
+ efforts of academic and linguistic communities, and to provide a free and
15
+ open framework in which fonts may be shared and improved in partnership
16
+ with others.
17
+
18
+ The OFL allows the licensed fonts to be used, studied, modified and
19
+ redistributed freely as long as they are not sold by themselves. The
20
+ fonts, including any derivative works, can be bundled, embedded,
21
+ redistributed and/or sold with any software provided that any reserved
22
+ names are not used by derivative works. The fonts and derivatives,
23
+ however, cannot be released under any other type of license. The
24
+ requirement for fonts to remain under this license does not apply
25
+ to any document created using the fonts or their derivatives.
26
+
27
+ DEFINITIONS
28
+ "Font Software" refers to the set of files released by the Copyright
29
+ Holder(s) under this license and clearly marked as such. This may
30
+ include source files, build scripts and documentation.
31
+
32
+ "Reserved Font Name" refers to any names specified as such after the
33
+ copyright statement(s).
34
+
35
+ "Original Version" refers to the collection of Font Software components as
36
+ distributed by the Copyright Holder(s).
37
+
38
+ "Modified Version" refers to any derivative made by adding to, deleting,
39
+ or substituting -- in part or in whole -- any of the components of the
40
+ Original Version, by changing formats or by porting the Font Software to a
41
+ new environment.
42
+
43
+ "Author" refers to any designer, engineer, programmer, technical
44
+ writer or other person who contributed to the Font Software.
45
+
46
+ PERMISSION AND CONDITIONS
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
49
+ redistribute, and sell modified and unmodified copies of the Font
50
+ Software, subject to the following conditions:
51
+
52
+ 1) Neither the Font Software nor any of its individual components,
53
+ in Original or Modified Versions, may be sold by itself.
54
+
55
+ 2) Original or Modified Versions of the Font Software may be bundled,
56
+ redistributed and/or sold with any software, provided that each copy
57
+ contains the above copyright notice and this license. These can be
58
+ included either as stand-alone text files, human-readable headers or
59
+ in the appropriate machine-readable metadata fields within text or
60
+ binary files as long as those fields can be easily viewed by the user.
61
+
62
+ 3) No Modified Version of the Font Software may use the Reserved Font
63
+ Name(s) unless explicit written permission is granted by the corresponding
64
+ Copyright Holder. This restriction only applies to the primary font name as
65
+ presented to the users.
66
+
67
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68
+ Software shall not be used to promote, endorse or advertise any
69
+ Modified Version, except to acknowledge the contribution(s) of the
70
+ Copyright Holder(s) and the Author(s) or with their explicit written
71
+ permission.
72
+
73
+ 5) The Font Software, modified or unmodified, in part or in whole,
74
+ must be distributed entirely under this license, and must not be
75
+ distributed under any other license. The requirement for fonts to
76
+ remain under this license does not apply to any document created
77
+ using the Font Software.
78
+
79
+ TERMINATION
80
+ This license becomes null and void if any of the above conditions are
81
+ not met.
82
+
83
+ DISCLAIMER
84
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # Bundled fonts
2
+
3
+ Inter 4.1 by Rasmus Andersson, SIL Open Font License 1.1 (`OFL.txt`).
4
+ Static TTF instances copied unchanged from `extras/ttf/` in
5
+ https://github.com/rsms/inter/releases/download/v4.1/Inter-4.1.zip
6
+
7
+ | File | SHA-256 |
8
+ |---|---|
9
+ | Inter-Regular.ttf | 40d692fce188e4471e2b3cba937be967878f631ad3ebbbdcd587687c7ebe0c82 |
10
+ | Inter-Bold.ttf | 288316099b1e0a47a4716d159098005eef7c0066921f34e3200393dbdb01947f |
11
+ | Inter-Italic.ttf | bbc051dd204b5019a1aa0bc0ae2aa8a05ab13e7a3f979fa357631dc7feb6833a |
12
+ | Inter-BoldItalic.ttf | 948405a16cdc62701da5f4005ed068ca5f4d27061d98f7974ccfc37831d9581d |
13
+
14
+ Used when a document declares no `font_family`, or with `font_family "Inter"`.
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/md5"
4
+
5
+ module Stationery
6
+ module Fonts
7
+ module Embedding
8
+ # What every CIDFont embedding shares: the font descriptor's metrics, the
9
+ # glyph widths keyed by character code, and the subset tag.
10
+ class Base
11
+ IDENTITY = { Registry: "Adobe", Ordering: "Identity", Supplement: 0 }.freeze
12
+
13
+ def initialize(font)
14
+ @font = font
15
+ @ttf = font.ttf
16
+ end
17
+
18
+ private
19
+
20
+ def descriptor(writer, name, **font_file)
21
+ writer.add(
22
+ Type: :FontDescriptor, FontName: name, Flags: flags,
23
+ FontBBox: @ttf.bbox.map { |v| glyph_space(v) }, ItalicAngle: @ttf.italic_angle,
24
+ Ascent: glyph_space(@ttf.ascender), Descent: glyph_space(@ttf.descender),
25
+ CapHeight: glyph_space(@ttf.cap_height), XHeight: glyph_space(@ttf.x_height),
26
+ StemV: @font.bold? ? 120 : 80, **font_file
27
+ )
28
+ end
29
+
30
+ # The W array: runs of consecutive codes, each with its glyphs' widths.
31
+ def widths(gids)
32
+ pairs = gids.map { |gid| [@font.code(gid), gid] }.sort
33
+ pairs.slice_when { |(a, _), (b, _)| b != a + 1 }.flat_map do |run|
34
+ [run.first.first, run.map { |_, gid| glyph_space(@ttf.advance(gid)) }]
35
+ end
36
+ end
37
+
38
+ def glyph_space(units)
39
+ (units * 1000.0 / @ttf.units_per_em).round
40
+ end
41
+
42
+ def flags
43
+ flags = 32 # Nonsymbolic
44
+ flags |= 1 if @ttf.fixed_pitch?
45
+ flags |= 64 unless @ttf.italic_angle.zero?
46
+ flags
47
+ end
48
+
49
+ # Subset fonts are named with six uppercase letters: ABCDEF+OpenSans-Regular.
50
+ def subset_tag(gids)
51
+ Digest::MD5.digest(gids.pack("n*") + @ttf.postscript_name).bytes.first(6).map { |b| (65 + (b % 26)).chr }.join
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stationery
4
+ module Fonts
5
+ module Embedding
6
+ # A CIDFontType0 over the font's CFF table with every undrawn glyph
7
+ # blanked (FontFile3 /CIDFontType0C). Codes are CIDs: the charset's for
8
+ # a CID-keyed font, glyph ids for a name-keyed one, so no CIDToGIDMap is
9
+ # needed.
10
+ class CFF < Base
11
+ # Returns [base_font_name, cid_font_reference].
12
+ def build(writer, gids)
13
+ name = :"#{subset_tag(gids)}+#{@ttf.postscript_name}"
14
+ font_file = writer.add(PDF::Stream.new(CffSubset.build(@ttf.cff, gids), { Subtype: :CIDFontType0C }))
15
+
16
+ cid_font = writer.add(
17
+ Type: :Font, Subtype: :CIDFontType0, BaseFont: name, CIDSystemInfo: system_info,
18
+ FontDescriptor: descriptor(writer, name, FontFile3: font_file),
19
+ DW: glyph_space(@ttf.advance(0)), W: widths(gids)
20
+ )
21
+ [name, cid_font]
22
+ end
23
+
24
+ private
25
+
26
+ def system_info
27
+ registry, ordering, supplement = @ttf.cff.ros
28
+ return IDENTITY unless registry
29
+
30
+ { Registry: registry, Ordering: ordering, Supplement: supplement }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stationery
4
+ module Fonts
5
+ module Embedding
6
+ # A CIDFontType2 over a TrueType subset holding only the drawn glyphs.
7
+ # Text uses the original glyph ids as CIDs; CIDToGIDMap maps them to the
8
+ # subset's renumbered ids.
9
+ class TrueType < Base
10
+ # Returns [base_font_name, cid_font_reference].
11
+ def build(writer, gids)
12
+ subset, mapping = Subset.build(@ttf, gids)
13
+ name = :"#{subset_tag(gids)}+#{@ttf.postscript_name}"
14
+ font_file = writer.add(PDF::Stream.new(subset, { Length1: subset.bytesize }))
15
+
16
+ cid_font = writer.add(
17
+ Type: :Font, Subtype: :CIDFontType2, BaseFont: name, CIDSystemInfo: IDENTITY,
18
+ FontDescriptor: descriptor(writer, name, FontFile2: font_file),
19
+ DW: glyph_space(@ttf.advance(0)), W: widths(gids),
20
+ CIDToGIDMap: writer.add(PDF::Stream.new(cid_to_gid_map(gids, mapping)))
21
+ )
22
+ [name, cid_font]
23
+ end
24
+
25
+ private
26
+
27
+ def cid_to_gid_map(gids, mapping)
28
+ map = Array.new((gids.max || 0) + 1, 0)
29
+ gids.each { |gid| map[gid] = mapping.fetch(gid) }
30
+ map.pack("n*")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stationery
4
+ module Fonts
5
+ # Splits runs so each character is drawn by a font that has its glyph: the
6
+ # run's family, then the book's fallbacks in order, then bundled Inter. A
7
+ # glyph no font has stays in the run's family and draws as .notdef.
8
+ #
9
+ # Whitespace, joiners, variation selectors and combining marks carry the
10
+ # family of the character before them (or after them at the start of a
11
+ # run) so words are not chopped and marks stay with their base.
12
+ class Fallback
13
+ CARRIED = /[\s‌‍︀-️\p{M}]/
14
+
15
+ def self.carried?(char) = CARRIED.match?(char)
16
+
17
+ def initialize(book)
18
+ @book = book
19
+ @found = {}
20
+ end
21
+
22
+ def apply(runs) = runs.flat_map { |run| split(run) }
23
+
24
+ private
25
+
26
+ def split(run)
27
+ style = run.style
28
+ font = @book.resolve(style).first
29
+ return [run] if run.text.each_char.all? { |char| self.class.carried?(char) || font.glyph?(char) }
30
+
31
+ chars = run.text.chars
32
+ families = fill(chars.map { |char| family_for(char, style) unless self.class.carried?(char) }, style.family)
33
+ pieces = chars.zip(families).chunk_while { |(_, left), (_, right)| left == right }.map do |group|
34
+ Text::Run.new(group.map(&:first).join, style.with(family: group.first.last))
35
+ end
36
+ Text::Run.merge(pieces)
37
+ end
38
+
39
+ def family_for(char, style)
40
+ key = [char, style.family, style.weight, style.style]
41
+ @found.fetch(key) do
42
+ @found[key] = chain(style.family).find do |name|
43
+ @book.resolve(style.with(family: name)).first.glyph?(char)
44
+ end || style.family
45
+ end
46
+ end
47
+
48
+ def chain(family) = [family, *@book.fallbacks, Bundled::DEFAULT].uniq
49
+
50
+ # Carried characters (nil) take the previous family, or the first one
51
+ # after them at the start.
52
+ def fill(families, default)
53
+ previous = families.compact.first || default
54
+ families.map { |family| previous = family || previous }
55
+ end
56
+ end
57
+ end
58
+ end
@@ -12,6 +12,17 @@ module Stationery
12
12
 
13
13
  attr_reader :name, :paths
14
14
 
15
+ # A family from explicit files, or a bundled or installed pack one when
16
+ # no files are given.
17
+ def self.build(name, **paths)
18
+ return new(name, **paths) if paths.any?
19
+
20
+ Bundled.family(name) || Packs.family(name) ||
21
+ raise(ArgumentError, "font family #{name} needs a regular face (bundled: #{Bundled.names.join(", ")}; " \
22
+ "or `stationery fonts install PACK` with PACK one of " \
23
+ "#{Catalog::PACKS.map(&:key).join(", ")})")
24
+ end
25
+
15
26
  def initialize(name, **paths)
16
27
  unknown = paths.keys - STYLES
17
28
  raise ArgumentError, "unknown font style #{unknown.join(", ")} (use #{STYLES.join(", ")})" if unknown.any?