emfsvg 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8406a813f99a3967f711a2b650958d84f51b0583f16d982f4546fb6cd2460713
4
+ data.tar.gz: aec4ceefd1fcb5a45ee36e9887df3c33348d23906a2461b51a7104e87fe02088
5
+ SHA512:
6
+ metadata.gz: 04d0506ac5adaef03d45450d0b93e0b352950c9c36ba4139c708d167b3cd5f62368258d5788561c5a8112da4cd8fdded08bd2e64d7587e708985bb139493f7ba
7
+ data.tar.gz: 4c403342b7abfb81b84eea0d3919b0db0d2307a8857cd0da167c5a1e951ff852813e2acc83f8f1d905b67b9be73c7538fe25701710567611ba98e06ef76c1405
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2026, Ribose Inc.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.adoc ADDED
@@ -0,0 +1,104 @@
1
+ = emfsvg: pure-Ruby EMF to SVG transformer
2
+
3
+ `emfsvg` converts Microsoft Enhanced Metafile (EMF) documents to SVG
4
+ using the [`emf`](https://github.com/claricle/emf) gem's domain model.
5
+ It is a clean-room Ruby replacement for the FFI wrapper around the
6
+ GPLv2 `libemf2svg` C library.
7
+
8
+ == Synopsis
9
+
10
+ require "emfsvg"
11
+
12
+ svg = Emfsvg.from_file("image.emf")
13
+ svg = Emfsvg.from_bytes(emf_bytes, width: 400, height: 300)
14
+ metafile = Emf.parse(emf_bytes)
15
+ svg = Emfsvg.to_svg(metafile, namespace: "svg")
16
+
17
+ == Command-line tool
18
+
19
+ emfsvg convert INPUT.emf OUTPUT.svg [options]
20
+
21
+ Options:
22
+
23
+ -w, --width PIXELS Override viewport width
24
+ -h, --height PIXELS Override viewport height
25
+ -n, --namespace NAME XML namespace prefix (e.g. svg)
26
+ -v, --verbose Print record-by-record dispatch to STDERR
27
+ --emf-plus Render EMF+ records instead of EMF fallback
28
+
29
+ == Architecture
30
+
31
+ `emfsvg` adds a renderer layer on top of `emf`'s domain model:
32
+
33
+ Emf::Model::Metafile
34
+ v
35
+ Emfsvg::Renderer
36
+ v
37
+ Emfsvg::Visitors::EmrVisitor
38
+ v
39
+ Emfsvg::SvgBuilder (XML)
40
+ v
41
+ Emfsvg::DeviceContext (GDI state)
42
+ v
43
+ Emfsvg::ObjectTable (1-indexed pen/brush/font handles)
44
+
45
+ The renderer walks the metafile's records, dispatching each to a
46
+ `visit_<record_type>` method on the visitor. State changes (pen,
47
+ brush, transform, clip) flow through the `DeviceContext`; persistent
48
+ objects are stored in the `ObjectTable`.
49
+
50
+ === Handlers
51
+
52
+ The visitor handles a useful subset of EMF records out of the box:
53
+
54
+ * *Primitives*: `Rectangle`, `Ellipse`, `RoundRect`, `LineTo`,
55
+ `MoveToEx`, `Arc` (stub), `Polygon`, `Polyline` + 16-bit variants
56
+ * *Paths*: `BeginPath`/`EndPath`/`CloseFigure`/`FillPath`/`StrokePath`/
57
+ `StrokeAndFillPath`/`AbortPath`
58
+ * *State*: `SetTextColor`, `SetBkColor`, `SetBkMode`, `SetMapMode`,
59
+ `SetRop2`, `SetPolyFillMode`, `SetTextAlign`, `SetStretchBltMode`
60
+ * *Transform*: `SetWorldTransform`, `ModifyWorldTransform`
61
+ * *Objects*: `CreatePen`, `CreateBrushIndirect`, `SelectObject`,
62
+ `DeleteObject`, `SaveDc`, `RestoreDc`
63
+ * *Misc*: `SetMiterLimit`, `SetWindowExtEx`, `SetViewportExtEx`
64
+
65
+ Unsupported record types fall through silently (verbose mode logs
66
+ them).
67
+
68
+ === Sparx/Wine Y-repair
69
+
70
+ EMFs from Sparx Enterprise Architect under Wine have only the Y anchor
71
+ negated. The detector (`rclBounds.top * rclBounds.bottom < 0`) and the
72
+ per-coordinate repair live in `Emfsvg::Renderer` and
73
+ `Emfsvg::Visitors::EmrVisitor#maybe_flip_y`, matching the behaviour
74
+ of the C library.
75
+
76
+ == Installation
77
+
78
+ gem install emfsvg
79
+
80
+ Or in your Gemfile:
81
+
82
+ gem "emfsvg", "~> 0.1"
83
+ gem "emf", "~> 0.1"
84
+
85
+ Requires Ruby >= 3.1.
86
+
87
+ == Development
88
+
89
+ git clone https://github.com/claricle/emfsvg
90
+ cd emfsvg
91
+ bundle install
92
+ bundle exec rspec
93
+ bundle exec rubocop
94
+
95
+ == Status
96
+
97
+ EMF-to-SVG renders the common subset of records from the EMF
98
+ fixtures. Bitmap records (StretchDIBits, AlphaBlend, etc.) and
99
+ EMF+ drawing records are not yet rendered. Corrupted inputs are
100
+ resilient through the `emf` parser's hardened error handling.
101
+
102
+ == License
103
+
104
+ BSD-2-Clause. See `LICENSE.txt`.
data/exe/emfsvg ADDED
@@ -0,0 +1,69 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "optparse"
5
+
6
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
7
+ require "emfsvg"
8
+
9
+ module EmfsvgCLI
10
+ module_function
11
+
12
+ def run(argv)
13
+ command = argv.shift
14
+ case command
15
+ when "convert" then convert(argv)
16
+ when "version", "-V", "--version"
17
+ puts "emfsvg #{Emfsvg::VERSION}"
18
+ when "help", "-h", "--help", nil
19
+ print_help
20
+ else
21
+ warn "emfsvg: unknown command '#{command}'"
22
+ print_help
23
+ exit 1
24
+ end
25
+ end
26
+
27
+ def convert(argv)
28
+ options = {}
29
+ parser = OptionParser.new do |opts|
30
+ opts.banner = "Usage: emfsvg convert [OPTIONS] INPUT.emf OUTPUT.svg"
31
+ opts.on("-w", "--width PIXELS", Integer, "Override viewport width") { |v| options[:width] = v }
32
+ opts.on("-h", "--height PIXELS", Integer, "Override viewport height") { |v| options[:height] = v }
33
+ opts.on("-n", "--namespace NAME", "XML namespace prefix (e.g. svg)") { |v| options[:namespace] = v }
34
+ opts.on("-v", "--verbose", "Print record-by-record dispatch to STDERR") { options[:verbose] = true }
35
+ opts.on("--emf-plus", "Render EMF+ records instead of EMF fallback") { options[:emf_plus] = true }
36
+ end
37
+
38
+ args = parser.parse(argv)
39
+ if args.length != 2
40
+ warn parser
41
+ exit 2
42
+ end
43
+
44
+ input, output = args
45
+ svg = Emfsvg.from_file(input, **options)
46
+ File.write(output, svg)
47
+ warn "Wrote #{output} (#{svg.bytesize} bytes)"
48
+ end
49
+
50
+ def print_help
51
+ puts <<~HELP
52
+ emfsvg #{Emfsvg::VERSION} — EMF to SVG transformer
53
+
54
+ Usage:
55
+ emfsvg convert INPUT.emf OUTPUT.svg [options]
56
+ emfsvg version
57
+ emfsvg help
58
+
59
+ Options:
60
+ -w, --width PIXELS Override viewport width
61
+ -h, --height PIXELS Override viewport height
62
+ -n, --namespace NAME XML namespace prefix (e.g. svg)
63
+ -v, --verbose Print record-by-record dispatch to STDERR
64
+ --emf-plus Render EMF+ records instead of EMF fallback
65
+ HELP
66
+ end
67
+ end
68
+
69
+ EmfsvgCLI.run(ARGV)
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Emfsvg
4
+ # Replicates BSD rand() (macOS libc default) with default seed 1, which
5
+ # libemf2svg relies on for clip-path IDs via get_id() → rand().
6
+ #
7
+ # The first call returns 16807, then 282475249, 1622650073, ... We pre-load
8
+ # 5000 values from a generated table to keep the runtime trivial. If more
9
+ # are needed, extend the table via `scripts/dump_rand.c`.
10
+ class BsdRand
11
+ SEQUENCE_PATH = File.expand_path("bsd_rand_sequence.txt", __dir__)
12
+ @values = File.readlines(SEQUENCE_PATH, chomp: true).map(&:to_i)
13
+ @index = 0
14
+
15
+ class << self
16
+ def next
17
+ v = @values[@index]
18
+ @index += 1
19
+ v
20
+ end
21
+
22
+ def reset
23
+ @index = 0
24
+ end
25
+
26
+ attr_reader :values
27
+ end
28
+ end
29
+ end