stationery 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +94 -1
- data/README.md +358 -25
- data/exe/stationery +6 -0
- data/lib/generators/stationery/fonts_generator.rb +24 -0
- data/lib/stationery/builder.rb +25 -3
- data/lib/stationery/canvas/debug.rb +26 -0
- data/lib/stationery/canvas/path.rb +22 -12
- data/lib/stationery/canvas/text.rb +8 -5
- data/lib/stationery/canvas.rb +25 -8
- data/lib/stationery/cli/fonts.rb +85 -0
- data/lib/stationery/cli/render.rb +137 -0
- data/lib/stationery/cli.rb +74 -0
- data/lib/stationery/document.rb +53 -9
- data/lib/stationery/elements/lists.rb +58 -0
- data/lib/stationery/elements/rich.rb +27 -0
- data/lib/stationery/elements.rb +88 -12
- data/lib/stationery/errors.rb +13 -1
- data/lib/stationery/fonts/bundled.rb +25 -0
- data/lib/stationery/fonts/catalog.rb +101 -0
- data/lib/stationery/fonts/cff.rb +140 -0
- data/lib/stationery/fonts/cff_subset.rb +145 -0
- data/lib/stationery/fonts/data/Inter-Bold.ttf +0 -0
- data/lib/stationery/fonts/data/Inter-BoldItalic.ttf +0 -0
- data/lib/stationery/fonts/data/Inter-Italic.ttf +0 -0
- data/lib/stationery/fonts/data/Inter-Regular.ttf +0 -0
- data/lib/stationery/fonts/data/OFL.txt +92 -0
- data/lib/stationery/fonts/data/README.md +14 -0
- data/lib/stationery/fonts/embedding/base.rb +56 -0
- data/lib/stationery/fonts/embedding/cff.rb +35 -0
- data/lib/stationery/fonts/embedding/true_type.rb +35 -0
- data/lib/stationery/fonts/fallback.rb +58 -0
- data/lib/stationery/fonts/family.rb +11 -0
- data/lib/stationery/fonts/font.rb +52 -88
- data/lib/stationery/fonts/font_book.rb +46 -8
- data/lib/stationery/fonts/glyph_run.rb +38 -0
- data/lib/stationery/fonts/gpos/class_def.rb +25 -0
- data/lib/stationery/fonts/gpos/coverage.rb +20 -0
- data/lib/stationery/fonts/gpos/pair_pos.rb +83 -0
- data/lib/stationery/fonts/gpos.rb +62 -0
- data/lib/stationery/fonts/installer.rb +121 -0
- data/lib/stationery/fonts/kern_table.rb +52 -0
- data/lib/stationery/fonts/kerning.rb +17 -0
- data/lib/stationery/fonts/packs.rb +45 -0
- data/lib/stationery/fonts/sfnt_metrics.rb +51 -0
- data/lib/stationery/fonts/to_unicode.rb +36 -0
- data/lib/stationery/fonts/true_type.rb +32 -49
- data/lib/stationery/html/document.rb +11 -0
- data/lib/stationery/html/tokenizer.rb +74 -0
- data/lib/stationery/html/tree_builder/blocks.rb +137 -0
- data/lib/stationery/html/tree_builder/collector.rb +50 -0
- data/lib/stationery/html/tree_builder.rb +72 -0
- data/lib/stationery/html/whitespace.rb +43 -0
- data/lib/stationery/layout/box.rb +85 -16
- data/lib/stationery/layout/flow.rb +48 -15
- data/lib/stationery/layout/image.rb +1 -0
- data/lib/stationery/layout/list_item.rb +89 -0
- data/lib/stationery/layout/mark.rb +58 -0
- data/lib/stationery/layout/node.rb +26 -2
- data/lib/stationery/layout/paginator.rb +35 -6
- data/lib/stationery/layout/positioned.rb +3 -1
- data/lib/stationery/layout/row.rb +34 -4
- data/lib/stationery/layout/svg.rb +45 -0
- data/lib/stationery/layout/table/cell.rb +33 -6
- data/lib/stationery/layout/table/grid.rb +93 -0
- data/lib/stationery/layout/table/row_splitter.rb +36 -0
- data/lib/stationery/layout/table/selection.rb +3 -2
- data/lib/stationery/layout/table.rb +69 -23
- data/lib/stationery/layout/table_of_contents/entry.rb +56 -0
- data/lib/stationery/layout/table_of_contents.rb +43 -0
- data/lib/stationery/layout/text.rb +5 -4
- data/lib/stationery/layout/wrap.rb +78 -0
- data/lib/stationery/list_markers.rb +47 -0
- data/lib/stationery/markdown/block_parser/containers.rb +94 -0
- data/lib/stationery/markdown/block_parser/tables.rb +54 -0
- data/lib/stationery/markdown/block_parser.rb +141 -0
- data/lib/stationery/markdown/document.rb +50 -0
- data/lib/stationery/markdown/inline_parser/emphasis.rb +80 -0
- data/lib/stationery/markdown/inline_parser/links.rb +70 -0
- data/lib/stationery/markdown/inline_parser/nodes.rb +39 -0
- data/lib/stationery/markdown/inline_parser.rb +89 -0
- data/lib/stationery/minitest.rb +33 -0
- data/lib/stationery/outline.rb +37 -0
- data/lib/stationery/page.rb +18 -4
- data/lib/stationery/page_templates.rb +33 -10
- data/lib/stationery/pdf/assembler.rb +18 -7
- data/lib/stationery/pdf/outline_writer.rb +72 -0
- data/lib/stationery/pdf/serializer.rb +5 -1
- data/lib/stationery/pdf/writer.rb +4 -0
- data/lib/stationery/preview.rb +64 -0
- data/lib/stationery/rails/previews_controller.rb +61 -0
- data/lib/stationery/rails.rb +2 -0
- data/lib/stationery/railtie.rb +60 -0
- data/lib/stationery/regions.rb +67 -0
- data/lib/stationery/rich/nodes.rb +55 -0
- data/lib/stationery/rich/renderer/inlines.rb +53 -0
- data/lib/stationery/rich/renderer.rb +121 -0
- data/lib/stationery/rich/styles.rb +34 -0
- data/lib/stationery/rspec.rb +6 -0
- data/lib/stationery/structure.rb +78 -0
- data/lib/stationery/svg/arc.rb +91 -0
- data/lib/stationery/svg/document.rb +73 -0
- data/lib/stationery/svg/parser.rb +38 -0
- data/lib/stationery/svg/path_data.rb +137 -0
- data/lib/stationery/svg/shapes.rb +54 -0
- data/lib/stationery/svg/style.rb +68 -0
- data/lib/stationery/svg/transform.rb +43 -0
- data/lib/stationery/testing/inspector.rb +98 -0
- data/lib/stationery/testing/matchers.rb +123 -0
- data/lib/stationery/text/entities/html4.rb +49 -0
- data/lib/stationery/text/entities.rb +6 -1
- data/lib/stationery/text/line.rb +8 -2
- data/lib/stationery/text/paragraph.rb +32 -11
- data/lib/stationery/text/runs_builder.rb +9 -1
- data/lib/stationery/text/style.rb +2 -2
- data/lib/stationery/text/wrapper.rb +3 -2
- data/lib/stationery/version.rb +1 -1
- data/lib/stationery/warnings.rb +65 -0
- data/lib/stationery.rb +51 -0
- metadata +85 -4
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -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?
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "digest/md5"
|
|
4
|
-
|
|
5
3
|
module Stationery
|
|
6
4
|
module Fonts
|
|
7
|
-
# One
|
|
8
|
-
#
|
|
5
|
+
# One font as used by one document: measures text, encodes it as
|
|
6
|
+
# character codes and remembers which glyphs were drawn so only those are
|
|
7
|
+
# embedded (TrueType) or so the widths and ToUnicode cover them (CFF).
|
|
9
8
|
#
|
|
10
|
-
# Embedded as a Type0 font
|
|
11
|
-
#
|
|
9
|
+
# Embedded as a Type0 font with Identity-H encoding and a ToUnicode map, so
|
|
10
|
+
# text extracts and searches correctly; the descendant CIDFont comes from
|
|
11
|
+
# Embedding::TrueType or Embedding::CFF.
|
|
12
12
|
class Font
|
|
13
13
|
OBLIQUE_SKEW = Math.tan(12 * Math::PI / 180)
|
|
14
14
|
|
|
@@ -18,131 +18,95 @@ module Stationery
|
|
|
18
18
|
@ttf = ttf
|
|
19
19
|
@used = {}
|
|
20
20
|
@widths = {}
|
|
21
|
+
@pairs = {}
|
|
22
|
+
@glyphs = {}
|
|
23
|
+
@advances = {}
|
|
24
|
+
@kerns = {}
|
|
25
|
+
@cid_keyed = ttf.cff? && ttf.cff.cid_keyed?
|
|
21
26
|
end
|
|
22
27
|
|
|
23
28
|
def inspect = "#<#{self.class} #{@ttf.postscript_name} used=#{@used.size}>"
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
# Advances and kerning are remembered per string, so measuring the same
|
|
31
|
+
# word again (wrapping, then laying out the line) allocates nothing.
|
|
32
|
+
def width_of(text, size, letter_spacing: 0, kerning: false)
|
|
33
|
+
width = scale(advance_units(text), size) + (letter_spacing * text.length)
|
|
34
|
+
return width unless kerning
|
|
35
|
+
|
|
36
|
+
width + (kerning_units(text) * size / 1000.0)
|
|
28
37
|
end
|
|
29
38
|
|
|
30
39
|
def ascender(size) = scale(@ttf.ascender, size)
|
|
31
40
|
def descender(size) = -scale(@ttf.descender, size)
|
|
32
41
|
def line_gap(size) = scale(@ttf.line_gap, size)
|
|
33
42
|
def line_height(size) = ascender(size) + descender(size) + line_gap(size)
|
|
43
|
+
def x_height(size) = scale(@ttf.x_height, size)
|
|
34
44
|
def underline_position(size) = scale(@ttf.underline_position, size)
|
|
35
45
|
def underline_thickness(size) = scale(@ttf.underline_thickness, size)
|
|
36
46
|
def strikeout_position(size) = scale(@ttf.strikeout_position, size)
|
|
37
47
|
def strikeout_size(size) = scale(@ttf.strikeout_size, size)
|
|
38
|
-
def glyph?(char) = @ttf.glyph?(char)
|
|
48
|
+
def glyph?(char) = @glyphs.fetch(char) { @glyphs[char] = @ttf.glyph?(char) }
|
|
39
49
|
|
|
40
50
|
def bold?
|
|
41
51
|
@ttf.weight >= 600
|
|
42
52
|
end
|
|
43
53
|
|
|
44
|
-
def encode(text)
|
|
45
|
-
|
|
54
|
+
def encode(text) = glyph_run(text).gids.map { |gid| code(gid) }.pack("n*")
|
|
55
|
+
|
|
56
|
+
# `kerning:` fills the adjustments with the font's pair kerning.
|
|
57
|
+
def glyph_run(text, kerning: false)
|
|
58
|
+
gids = text.each_char.map do |char|
|
|
46
59
|
gid = @ttf.glyph_id(char.ord)
|
|
47
60
|
@used[gid] ||= char
|
|
48
61
|
gid
|
|
49
|
-
end
|
|
62
|
+
end
|
|
63
|
+
adjust = gids.each_with_index.map { |gid, i| kerning && i + 1 < gids.size ? pair(gid, gids[i + 1]) : 0 }
|
|
64
|
+
GlyphRun.new(font: self, gids:, adjust:)
|
|
50
65
|
end
|
|
51
66
|
|
|
52
67
|
def used?
|
|
53
68
|
@used.any?
|
|
54
69
|
end
|
|
55
70
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
writer.add(
|
|
62
|
-
Type: :Font, Subtype: :Type0, BaseFont: name, Encoding: :"Identity-H",
|
|
63
|
-
DescendantFonts: [cid_font(writer, name, gids, subset, mapping)],
|
|
64
|
-
ToUnicode: writer.add(PDF::Stream.new(to_unicode_cmap))
|
|
65
|
-
)
|
|
71
|
+
# The two-byte character code a glyph is written as: its CID in a
|
|
72
|
+
# CID-keyed CFF font, otherwise the glyph id itself.
|
|
73
|
+
def code(gid)
|
|
74
|
+
@cid_keyed ? @ttf.cff.cid_for(gid) : gid
|
|
66
75
|
end
|
|
67
76
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
writer.add(
|
|
72
|
-
Type: :Font, Subtype: :CIDFontType2, BaseFont: name,
|
|
73
|
-
CIDSystemInfo: { Registry: "Adobe", Ordering: "Identity", Supplement: 0 },
|
|
74
|
-
FontDescriptor: descriptor(writer, name, subset),
|
|
75
|
-
DW: glyph_space(@ttf.advance(0)), W: glyph_widths(gids),
|
|
76
|
-
CIDToGIDMap: writer.add(PDF::Stream.new(cid_to_gid_map(gids, mapping)))
|
|
77
|
-
)
|
|
77
|
+
# { code => character } for every glyph drawn so far.
|
|
78
|
+
def used_codes
|
|
79
|
+
@used.to_h { |gid, char| [code(gid), char] }
|
|
78
80
|
end
|
|
79
81
|
|
|
80
|
-
def
|
|
82
|
+
def build(writer)
|
|
83
|
+
embedding = (@ttf.cff? ? Embedding::CFF : Embedding::TrueType).new(self)
|
|
84
|
+
name, cid_font = embedding.build(writer, @used.keys.sort)
|
|
85
|
+
|
|
81
86
|
writer.add(
|
|
82
|
-
Type: :
|
|
83
|
-
|
|
84
|
-
Ascent: glyph_space(@ttf.ascender), Descent: glyph_space(@ttf.descender),
|
|
85
|
-
CapHeight: glyph_space(@ttf.cap_height), XHeight: glyph_space(@ttf.x_height),
|
|
86
|
-
StemV: bold? ? 120 : 80,
|
|
87
|
-
FontFile2: writer.add(PDF::Stream.new(subset, { Length1: subset.bytesize }))
|
|
87
|
+
Type: :Font, Subtype: :Type0, BaseFont: name, Encoding: :"Identity-H",
|
|
88
|
+
DescendantFonts: [cid_font], ToUnicode: writer.add(PDF::Stream.new(ToUnicode.cmap(used_codes)))
|
|
88
89
|
)
|
|
89
90
|
end
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
units * size / @ttf.units_per_em.to_f
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def glyph_space(units)
|
|
96
|
-
(units * 1000.0 / @ttf.units_per_em).round
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def flags
|
|
100
|
-
flags = 32 # Nonsymbolic
|
|
101
|
-
flags |= 1 if @ttf.fixed_pitch?
|
|
102
|
-
flags |= 64 unless @ttf.italic_angle.zero?
|
|
103
|
-
flags
|
|
104
|
-
end
|
|
92
|
+
private
|
|
105
93
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
Digest::MD5.digest(gids.pack("n*") + @ttf.postscript_name).bytes.first(6).map { |b| (65 + (b % 26)).chr }.join
|
|
94
|
+
def advance_units(text)
|
|
95
|
+
@advances[text] ||= text.each_char.sum { |char| @widths[char] ||= @ttf.advance(@ttf.glyph_id(char.ord)) }
|
|
109
96
|
end
|
|
110
97
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
gids.each { |gid| map[gid] = mapping.fetch(gid) }
|
|
115
|
-
map.pack("n*")
|
|
98
|
+
def kerning_units(text)
|
|
99
|
+
@kerns[text] ||= text.each_char.map { |char| @ttf.glyph_id(char.ord) }
|
|
100
|
+
.each_cons(2).sum { |left, right| pair(left, right) }
|
|
116
101
|
end
|
|
117
102
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
end
|
|
103
|
+
# Kerning between two glyphs in thousandths of an em.
|
|
104
|
+
def pair(left, right)
|
|
105
|
+
@pairs[(left << 16) | right] ||= @ttf.kerning.adjust(left, right) * 1000.0 / @ttf.units_per_em
|
|
122
106
|
end
|
|
123
107
|
|
|
124
|
-
def
|
|
125
|
-
|
|
126
|
-
format("<%<gid>04X> <%<utf16>s>", gid:, utf16: char.encode(Encoding::UTF_16BE).unpack1("H*").upcase)
|
|
127
|
-
end
|
|
128
|
-
blocks = mappings.each_slice(100).map { |slice| "#{slice.size} beginbfchar\n#{slice.join("\n")}\nendbfchar" }
|
|
129
|
-
|
|
130
|
-
<<~CMAP
|
|
131
|
-
/CIDInit /ProcSet findresource begin
|
|
132
|
-
12 dict begin
|
|
133
|
-
begincmap
|
|
134
|
-
/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
|
|
135
|
-
/CMapName /Adobe-Identity-UCS def
|
|
136
|
-
/CMapType 2 def
|
|
137
|
-
1 begincodespacerange
|
|
138
|
-
<0000> <FFFF>
|
|
139
|
-
endcodespacerange
|
|
140
|
-
#{blocks.join("\n")}
|
|
141
|
-
endcmap
|
|
142
|
-
CMapName currentdict /CMap defineresource pop
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
CMAP
|
|
108
|
+
def scale(units, size)
|
|
109
|
+
units * size / @ttf.units_per_em.to_f
|
|
146
110
|
end
|
|
147
111
|
end
|
|
148
112
|
end
|
|
@@ -6,30 +6,68 @@ module Stationery
|
|
|
6
6
|
# document gets its own book so glyph usage (and so subsetting) is per
|
|
7
7
|
# document, while the parsed TrueType data is shared through the Registry.
|
|
8
8
|
class FontBook
|
|
9
|
-
attr_reader :families
|
|
9
|
+
attr_reader :families, :fallbacks, :warnings
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
# `fallbacks:` names the families tried, in order, for a character the
|
|
12
|
+
# run's own family has no glyph for.
|
|
13
|
+
def initialize(families = {}, fallbacks: [], warnings: Warnings.new)
|
|
12
14
|
@families = families.dup
|
|
15
|
+
@fallbacks = fallbacks.map(&:to_s).freeze
|
|
16
|
+
@warnings = warnings
|
|
13
17
|
@fonts = {}
|
|
18
|
+
@resolved = {}
|
|
19
|
+
@split = {}.compare_by_identity
|
|
14
20
|
end
|
|
15
21
|
|
|
16
22
|
def inspect = "#<#{self.class} families=#{@families.keys.inspect}>"
|
|
17
23
|
|
|
18
24
|
def register(name, **paths)
|
|
19
|
-
@
|
|
25
|
+
@resolved.clear
|
|
26
|
+
@families[name.to_s] = Family.build(name, **paths)
|
|
20
27
|
end
|
|
21
28
|
|
|
22
|
-
# Returns [Font, Family::Face] for a Text::Style.
|
|
29
|
+
# Returns [Font, Family::Face] for a Text::Style, memoised per style.
|
|
23
30
|
def resolve(style)
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
@resolved[style] ||= begin
|
|
32
|
+
face = family(style.family).face(weight: style.weight, style: style.style)
|
|
33
|
+
[@fonts[face.path] ||= Font.new(Registry.load(face.path)), face].freeze
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The runs split so every character is drawn by a font that has it. Runs
|
|
38
|
+
# this book already split come back as they are.
|
|
39
|
+
def fallback(runs)
|
|
40
|
+
return runs if @split.key?(runs)
|
|
41
|
+
|
|
42
|
+
(@fallback ||= Fallback.new(self)).apply(runs).tap { |split| @split[split] = true }
|
|
26
43
|
end
|
|
27
44
|
|
|
28
45
|
private
|
|
29
46
|
|
|
47
|
+
# Registered by name, bundled by name, an installed pack by name, then
|
|
48
|
+
# the first registered family, then the bundled default.
|
|
30
49
|
def family(name)
|
|
31
|
-
@families[name.to_s] ||
|
|
32
|
-
|
|
50
|
+
@families[name.to_s] || bundled(name) || pack(name) || substitute(name)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# An unknown name draws with the first registered family, else bundled
|
|
54
|
+
# Inter, and says so once.
|
|
55
|
+
def substitute(name)
|
|
56
|
+
used = @families.values.first || bundled(Bundled::DEFAULT)
|
|
57
|
+
@warnings << Warnings::UnknownFamily.new(requested: name.to_s, used: used.name)
|
|
58
|
+
used
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def bundled(name)
|
|
62
|
+
@bundled ||= {}
|
|
63
|
+
@bundled[name.to_s] ||= Bundled.family(name)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def pack(name)
|
|
67
|
+
@packs ||= {}
|
|
68
|
+
return @packs[name.to_s] if @packs.key?(name.to_s)
|
|
69
|
+
|
|
70
|
+
@packs[name.to_s] = Packs.family(name)
|
|
33
71
|
end
|
|
34
72
|
end
|
|
35
73
|
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Stationery
|
|
4
|
+
module Fonts
|
|
5
|
+
# Glyph ids for one run of text in one font, with an extra advance after
|
|
6
|
+
# each glyph in thousandths of the font size (positive widens). All-zero
|
|
7
|
+
# adjustments draw as a plain Tj string; otherwise as a TJ array. Glyphs
|
|
8
|
+
# are written as the font's character codes (see Font#code).
|
|
9
|
+
GlyphRun = Data.define(:font, :gids, :adjust) do
|
|
10
|
+
def width(size, letter_spacing: 0)
|
|
11
|
+
units = gids.sum { |gid| font.ttf.advance(gid) }
|
|
12
|
+
(units * size / font.ttf.units_per_em.to_f) + (adjust.sum * size / 1000.0) + (letter_spacing * gids.size)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def with_word_spacing(chars, extra_points, size)
|
|
16
|
+
extra = extra_points * 1000.0 / size
|
|
17
|
+
with(adjust: adjust.each_with_index.map { |a, i| chars[i] == " " ? a + extra : a })
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def to_operator
|
|
21
|
+
return "<#{hex(gids)}> Tj" unless adjusted?
|
|
22
|
+
|
|
23
|
+
parts = gids.each_index.slice_after { |i| !adjust[i].zero? }.flat_map do |indices|
|
|
24
|
+
last = indices.last
|
|
25
|
+
chunk = ["<#{hex(indices.map { |i| gids[i] })}>"]
|
|
26
|
+
last == gids.size - 1 || adjust[last].zero? ? chunk : chunk << PDF::Serializer.number(-adjust[last])
|
|
27
|
+
end
|
|
28
|
+
"[#{parts.join(" ")}] TJ"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def adjusted? = adjust[0...-1].any? { |a| !a.zero? }
|
|
34
|
+
|
|
35
|
+
def hex(ids) = ids.map { |gid| font.code(gid) }.pack("n*").unpack1("H*").upcase
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Stationery
|
|
4
|
+
module Fonts
|
|
5
|
+
class Gpos
|
|
6
|
+
# OpenType ClassDef table as a Hash of glyph id => class. Glyphs it
|
|
7
|
+
# does not list are class 0. Format 1 is a run of classes from a start
|
|
8
|
+
# glyph, format 2 ranges of glyphs sharing a class.
|
|
9
|
+
module ClassDef
|
|
10
|
+
def self.read(ttf, offset)
|
|
11
|
+
if ttf.u16(offset) == 1
|
|
12
|
+
start = ttf.u16(offset + 2)
|
|
13
|
+
classes = ttf.data.byteslice(offset + 6, ttf.u16(offset + 4) * 2).unpack("n*")
|
|
14
|
+
return classes.each_with_index.to_h { |klass, i| [start + i, klass] }.freeze
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
ranges = ttf.data.byteslice(offset + 4, ttf.u16(offset + 2) * 6).unpack("n*").each_slice(3)
|
|
18
|
+
ranges.each_with_object({}) do |(first, last, klass), map|
|
|
19
|
+
(first..last).each { |gid| map[gid] = klass }
|
|
20
|
+
end.freeze
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|