logifos 0.1.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 +7 -0
- data/Cargo.lock +690 -0
- data/Cargo.toml +14 -0
- data/HISTORY.txt +33 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +94 -0
- data/ext/logifos_cdylib/Cargo.toml +20 -0
- data/ext/logifos_cdylib/src/lib.rs +12 -0
- data/ext/logifos_native/Cargo.toml +29 -0
- data/ext/logifos_native/build.rs +9 -0
- data/ext/logifos_native/extconf.rb +55 -0
- data/ext/logifos_native/src/lib.rs +21 -0
- data/lib/logifos/errors.rb +40 -0
- data/lib/logifos/native.rb +48 -0
- data/lib/logifos/native_library.rb +140 -0
- data/lib/logifos/native_runtime.rb +95 -0
- data/lib/logifos/response.rb +46 -0
- data/lib/logifos/version.rb +5 -0
- data/lib/logifos.rb +17 -0
- data/logifos.gemspec +67 -0
- metadata +113 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "native_library"
|
|
6
|
+
require_relative "response"
|
|
7
|
+
|
|
8
|
+
module Logifos
|
|
9
|
+
# A per-library context for the versioned JSON facade; it owns no font state.
|
|
10
|
+
class NativeRuntime
|
|
11
|
+
BUFFER_V1 = 1
|
|
12
|
+
JSON_V1 = 2
|
|
13
|
+
RENDER = 4
|
|
14
|
+
|
|
15
|
+
attr_reader :library
|
|
16
|
+
|
|
17
|
+
# The gem ships the engine two ways, as parsanol-ruby does: platform
|
|
18
|
+
# gems carry the MRI extension (lib/logifos/<ruby version>/) and the
|
|
19
|
+
# portable cdylib (lib/logifos/native/) for the ffi tier; a source-gem
|
|
20
|
+
# install compiles the extension. library_path keeps working for an
|
|
21
|
+
# external engine.
|
|
22
|
+
def self.bundled_library_path(lib: File.expand_path("..", __dir__))
|
|
23
|
+
NativeLibrary.locate_library(lib) or raise LibraryError, "no bundled engine library in this gem"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# library_path selects an external engine explicitly; otherwise the MRI
|
|
27
|
+
# extension serves when it loaded, else the vendored cdylib.
|
|
28
|
+
def initialize(library_path: nil, required_features: BUFFER_V1 | JSON_V1)
|
|
29
|
+
required = required_features | BUFFER_V1 | JSON_V1
|
|
30
|
+
@library = if library_path
|
|
31
|
+
NativeLibrary.new(library_path: library_path, required_features: required)
|
|
32
|
+
elsif Native.available?
|
|
33
|
+
NativeLibrary.new(required_features: required)
|
|
34
|
+
else
|
|
35
|
+
bundled = NativeRuntime.bundled_library_path
|
|
36
|
+
NativeLibrary.new(library_path: bundled, required_features: required, located: true)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def request(message)
|
|
41
|
+
request_bytes(JSON.generate(message))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def request_bytes(bytes)
|
|
45
|
+
Response.decode(library.exchange(bytes))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def negotiate(options: {})
|
|
49
|
+
request(envelope("negotiate", options))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def render(formula_model, formula_contract:, format: :svg, options: {})
|
|
53
|
+
request(envelope("render", options).merge(
|
|
54
|
+
formula: formula_model, formula_contract: formula_contract, output: format.to_s
|
|
55
|
+
))
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Renders a MathML source document end to end: the engine parses it into
|
|
59
|
+
# its wire model, then renders that model with the caller's fonts. The
|
|
60
|
+
# contract returned by the parse — the engine's own — is the contract the
|
|
61
|
+
# render asserts, so caller and artifact always agree.
|
|
62
|
+
def render_mathml(mathml, fonts:, format: :svg, options: {})
|
|
63
|
+
parsed = request(parse_envelope(mathml))
|
|
64
|
+
render(
|
|
65
|
+
parsed.fetch("model"),
|
|
66
|
+
formula_contract: parsed.fetch("formula_contract"),
|
|
67
|
+
format: format,
|
|
68
|
+
options: render_options(options, fonts)
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Encodes supplied bytes without loading a font or asserting its validity.
|
|
73
|
+
def self.font(id:, bytes:, face_index: 0)
|
|
74
|
+
raise ArgumentError, "font bytes must be a String" unless bytes.is_a?(String)
|
|
75
|
+
|
|
76
|
+
bytes = bytes.b
|
|
77
|
+
{ id: id, sha256: Digest::SHA256.hexdigest(bytes), bytes_base64: [bytes].pack("m0"), face_index: face_index }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def parse_envelope(mathml)
|
|
83
|
+
envelope("parse", {}).merge(format: "mathml", source: mathml.to_s)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def render_options(options, fonts)
|
|
87
|
+
order = options.fetch(:fallback_order) { fonts.map { |font| font[:id] } }
|
|
88
|
+
options.merge(fonts: fonts, fallback_order: order)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def envelope(operation, options)
|
|
92
|
+
{ abi_version: 1, schema_version: 1, op: operation, options: options }
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "errors"
|
|
5
|
+
|
|
6
|
+
module Logifos
|
|
7
|
+
# Checks the wire envelope before exposing a result or typed core refusal.
|
|
8
|
+
module Response
|
|
9
|
+
def self.decode(bytes)
|
|
10
|
+
raise ProtocolError, "native response is not UTF-8" unless bytes.valid_encoding?
|
|
11
|
+
|
|
12
|
+
envelope = JSON.parse(bytes)
|
|
13
|
+
validate_envelope(envelope)
|
|
14
|
+
return envelope.fetch("result") if envelope["ok"]
|
|
15
|
+
|
|
16
|
+
error = envelope.fetch("error")
|
|
17
|
+
raise EngineError.new(**error.transform_keys(&:to_sym))
|
|
18
|
+
rescue JSON::ParserError
|
|
19
|
+
raise ProtocolError, "native response is not valid JSON"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.validate_envelope(envelope)
|
|
23
|
+
validate_versions(envelope)
|
|
24
|
+
field = if envelope["ok"] == true && envelope["result"].is_a?(Hash)
|
|
25
|
+
"result"
|
|
26
|
+
elsif envelope["ok"] == false && valid_error?(envelope["error"])
|
|
27
|
+
"error"
|
|
28
|
+
end
|
|
29
|
+
return if field && envelope.keys.sort == ["abi_version", "schema_version", "ok", field].sort
|
|
30
|
+
|
|
31
|
+
raise ProtocolError, "native response has an invalid envelope"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.validate_versions(envelope)
|
|
35
|
+
return if envelope.is_a?(Hash) && envelope.values_at("abi_version", "schema_version").all? { |v| v.eql?(1) }
|
|
36
|
+
|
|
37
|
+
raise CompatibilityError, "native response has incompatible versions"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.valid_error?(error)
|
|
41
|
+
fields = %w[code message path stage]
|
|
42
|
+
error.is_a?(Hash) && error.keys.sort == fields && error.values.all?(String)
|
|
43
|
+
end
|
|
44
|
+
private_class_method :validate_envelope, :validate_versions, :valid_error?
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/logifos.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "logifos/version"
|
|
4
|
+
require_relative "logifos/errors"
|
|
5
|
+
require_relative "logifos/native"
|
|
6
|
+
require_relative "logifos/native_runtime"
|
|
7
|
+
|
|
8
|
+
# The bootstrap module documents its render boundary at the method.
|
|
9
|
+
module Logifos
|
|
10
|
+
# Renders through the bundled engine; library_path still selects an
|
|
11
|
+
# external engine explicitly.
|
|
12
|
+
def self.render(formula_model, formula_contract:, library_path: nil, format: :svg, options: {})
|
|
13
|
+
NativeRuntime.new(library_path: library_path).render(
|
|
14
|
+
formula_model, formula_contract: formula_contract, format: format, options: options
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
data/logifos.gemspec
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/logifos/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "logifos"
|
|
7
|
+
spec.version = Logifos::VERSION
|
|
8
|
+
spec.platform = Gem::Platform::RUBY
|
|
9
|
+
|
|
10
|
+
spec.authors = ["Ribose Inc."]
|
|
11
|
+
spec.email = ["open.source@ribose.com"]
|
|
12
|
+
|
|
13
|
+
spec.summary = "Math formula rendering through the bundled logifos engine."
|
|
14
|
+
spec.description = <<~DESC
|
|
15
|
+
Renders math formulas to deterministic SVG through the bundled
|
|
16
|
+
logifos Rust engine: platform gems ship the native extension and the
|
|
17
|
+
portable engine cdylib, syntax models transform into the central
|
|
18
|
+
Formula model, which lays out to a box tree and emits SVG. An external
|
|
19
|
+
engine can still be passed with library_path:.
|
|
20
|
+
DESC
|
|
21
|
+
spec.homepage = "https://github.com/logifos/logifos-ruby"
|
|
22
|
+
spec.license = "MIT"
|
|
23
|
+
|
|
24
|
+
spec.metadata = {
|
|
25
|
+
"bug_tracker_uri" => "https://github.com/logifos/logifos-ruby/issues",
|
|
26
|
+
"changelog_uri" => "https://github.com/logifos/logifos-ruby/blob/main/HISTORY.txt",
|
|
27
|
+
"homepage_uri" => "https://github.com/logifos/logifos-ruby",
|
|
28
|
+
"source_code_uri" => "https://github.com/logifos/logifos-ruby",
|
|
29
|
+
"rubygems_mfa_required" => "true"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# Rust extension
|
|
33
|
+
spec.extensions = ["ext/logifos_native/extconf.rb"]
|
|
34
|
+
|
|
35
|
+
spec.files = Dir.glob("{lib,ext}/**/*") + %w[
|
|
36
|
+
HISTORY.txt
|
|
37
|
+
LICENSE.txt
|
|
38
|
+
Rakefile
|
|
39
|
+
README.md
|
|
40
|
+
logifos.gemspec
|
|
41
|
+
Cargo.toml
|
|
42
|
+
Cargo.lock
|
|
43
|
+
]
|
|
44
|
+
spec.files.reject! { |f| File.directory?(f) }
|
|
45
|
+
# Cargo build trees are never gem content (a dirty local
|
|
46
|
+
# ext/logifos_native/target once inflated a build to 218 MB).
|
|
47
|
+
spec.files.reject! { |f| f.include?("/target/") || f.start_with?("target/") }
|
|
48
|
+
# Binaries are never tracked in git, and the ruby (source) gem stays
|
|
49
|
+
# slim: the pure-portable cdylib is vendored ONLY into platform gems
|
|
50
|
+
# by the cross-gem build (rake gem:vendor_cdylib), never by a plain
|
|
51
|
+
# `gem build`.
|
|
52
|
+
spec.files.reject! do |f|
|
|
53
|
+
f =~ /\.(dll|so|dylib|lib|bundle)\Z/ &&
|
|
54
|
+
!(ENV["LOGIFOS_VENDOR_CDYLIB"] == "1" &&
|
|
55
|
+
f =~ %r{\Alib/logifos/native/liblogifos\.(so|dylib|dll)\Z})
|
|
56
|
+
end
|
|
57
|
+
spec.require_paths = ["lib"]
|
|
58
|
+
|
|
59
|
+
spec.required_ruby_version = ">= 3.2.0"
|
|
60
|
+
|
|
61
|
+
# Required for Rust extension
|
|
62
|
+
spec.add_dependency "rb_sys", "~> 0.9.39"
|
|
63
|
+
# The ffi tier (MRI without the extension, TruffleRuby) binds the
|
|
64
|
+
# vendored cdylib through these.
|
|
65
|
+
spec.add_dependency "ffi", "~> 1.17"
|
|
66
|
+
spec.add_dependency "json", "~> 2.6"
|
|
67
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: logifos
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ribose Inc.
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rb_sys
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.9.39
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.9.39
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: ffi
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.17'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.17'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: json
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.6'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.6'
|
|
54
|
+
description: |
|
|
55
|
+
Renders math formulas to deterministic SVG through the bundled
|
|
56
|
+
logifos Rust engine: platform gems ship the native extension and the
|
|
57
|
+
portable engine cdylib, syntax models transform into the central
|
|
58
|
+
Formula model, which lays out to a box tree and emits SVG. An external
|
|
59
|
+
engine can still be passed with library_path:.
|
|
60
|
+
email:
|
|
61
|
+
- open.source@ribose.com
|
|
62
|
+
executables: []
|
|
63
|
+
extensions:
|
|
64
|
+
- ext/logifos_native/extconf.rb
|
|
65
|
+
extra_rdoc_files: []
|
|
66
|
+
files:
|
|
67
|
+
- Cargo.lock
|
|
68
|
+
- Cargo.toml
|
|
69
|
+
- HISTORY.txt
|
|
70
|
+
- LICENSE.txt
|
|
71
|
+
- README.md
|
|
72
|
+
- Rakefile
|
|
73
|
+
- ext/logifos_cdylib/Cargo.toml
|
|
74
|
+
- ext/logifos_cdylib/src/lib.rs
|
|
75
|
+
- ext/logifos_native/Cargo.toml
|
|
76
|
+
- ext/logifos_native/build.rs
|
|
77
|
+
- ext/logifos_native/extconf.rb
|
|
78
|
+
- ext/logifos_native/src/lib.rs
|
|
79
|
+
- lib/logifos.rb
|
|
80
|
+
- lib/logifos/errors.rb
|
|
81
|
+
- lib/logifos/native.rb
|
|
82
|
+
- lib/logifos/native_library.rb
|
|
83
|
+
- lib/logifos/native_runtime.rb
|
|
84
|
+
- lib/logifos/response.rb
|
|
85
|
+
- lib/logifos/version.rb
|
|
86
|
+
- logifos.gemspec
|
|
87
|
+
homepage: https://github.com/logifos/logifos-ruby
|
|
88
|
+
licenses:
|
|
89
|
+
- MIT
|
|
90
|
+
metadata:
|
|
91
|
+
bug_tracker_uri: https://github.com/logifos/logifos-ruby/issues
|
|
92
|
+
changelog_uri: https://github.com/logifos/logifos-ruby/blob/main/HISTORY.txt
|
|
93
|
+
homepage_uri: https://github.com/logifos/logifos-ruby
|
|
94
|
+
source_code_uri: https://github.com/logifos/logifos-ruby
|
|
95
|
+
rubygems_mfa_required: 'true'
|
|
96
|
+
rdoc_options: []
|
|
97
|
+
require_paths:
|
|
98
|
+
- lib
|
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: 3.2.0
|
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '0'
|
|
109
|
+
requirements: []
|
|
110
|
+
rubygems_version: 4.0.20
|
|
111
|
+
specification_version: 4
|
|
112
|
+
summary: Math formula rendering through the bundled logifos engine.
|
|
113
|
+
test_files: []
|