emfsvg 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aae7748e35d0b9bdc9d1586c18a8dfe2640efa8cad6f0e67ee6faf77519e92f0
4
- data.tar.gz: 8b6866599cf1e938f55d0fe028ba6b5ce5d6626907a5227f82e097c5a1521b92
3
+ metadata.gz: d9e1b515a34b06f5b3fbfc63fc9188e0322aee70d1e9d144a0bdce75d4a6e6e4
4
+ data.tar.gz: 6749b0f41943106cc85aea27befba207fa76121f35298552e4086aac9c808d61
5
5
  SHA512:
6
- metadata.gz: 428e11c19161a8d4a22950ce592e6043c5e580e803ca1869dcf1b4d3d31d231084763fd406465f4d1785abab9dcd25035e483aeac47a9d57a1f5e112c5195d5a
7
- data.tar.gz: 2fa1da1604ffc28b495e098aa39bd22b8f0be085b27d40105b67d33c904c880bc352820e4264f131c9709dfe6bed2d00706c98e916e0f3fcaa697e856ff0745e
6
+ metadata.gz: eaf82448c8abaffb5e8711c919c180e883d134268cd6a2c0861d82cb45853d89446a0b78da685c090db4bcbd2cda427b376897c0e39c65170d5d3b2c2a59c3d0
7
+ data.tar.gz: a42e1fcbd225e74cf8299696c91a2925c5d0379b98851654fc5a921692a5ac00139ec9824687ac351eb863dfe2f4808c25be85e90a732f748bb6627b470be0af
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Emfsvg
4
+ module Svg
5
+ module Elements
6
+ # <pattern id="..."> paint server. A pattern is a reusable fill
7
+ # resource referenced by id (`fill="url(#id)""); its content is
8
+ # never rendered directly.
9
+ #
10
+ # Only the raster flavor is modeled: a pattern whose content is a
11
+ # single embedded raster <image> maps 1:1 to an EMF DIB pattern
12
+ # brush (EMR_CREATEDIBPATTERNBRUSHPT). Patterns with vector
13
+ # content, gradients, or tiles get no brush and degrade to the
14
+ # fill's fallback color at translation time.
15
+ #
16
+ # Geometry attributes (width/height/viewBox/patternUnits) are
17
+ # intentionally not retained: an EMF pattern brush tiles the DIB
18
+ # at the device origin, so per-tile SVG geometry has no faithful
19
+ # EMF counterpart to carry.
20
+ class Pattern < Element
21
+ ELEMENT_NAME = "pattern"
22
+ Element.register("pattern", self)
23
+
24
+ attr_reader :id, :image
25
+
26
+ def initialize(id:, image: nil)
27
+ @id = id
28
+ @image = image
29
+ end
30
+
31
+ def self.from_node(node)
32
+ image_node = node.element_children.find { |child| child.name == "image" }
33
+ new(id: node["id"], image: image_node && Image.from_node(image_node))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -20,6 +20,7 @@ module Emfsvg
20
20
  autoload :ClipPath, "emfsvg/svg/elements/clip_path"
21
21
  autoload :Text, "emfsvg/svg/elements/text"
22
22
  autoload :Image, "emfsvg/svg/elements/image"
23
+ autoload :Pattern, "emfsvg/svg/elements/pattern"
23
24
  end
24
25
  end
25
26
  end
@@ -40,3 +41,4 @@ require "emfsvg/svg/elements/defs"
40
41
  require "emfsvg/svg/elements/clip_path"
41
42
  require "emfsvg/svg/elements/text"
42
43
  require "emfsvg/svg/elements/image"
44
+ require "emfsvg/svg/elements/pattern"
@@ -12,6 +12,18 @@ module Emfsvg
12
12
  NULL = 1
13
13
  PATTERN = 3
14
14
 
15
+ # `fill="url(#id)"` — a reference to a paint server (<pattern>,
16
+ # <linearGradient>, <radialGradient>) defined in the document.
17
+ URL_REFERENCE_RE = /\Aurl\(\s*["']?#([^)"'\s]+)["']?\s*\)\z/
18
+ private_constant :URL_REFERENCE_RE
19
+
20
+ # `fill="#id"` — bare fragment reference spelling. Kept as a
21
+ # reference only when the fragment is NOT a hex color (those are
22
+ # colors and go to Color.parse).
23
+ BARE_REFERENCE_RE = /\A#([^\s)"']+)\z/
24
+ HEX_COLOR_RE = /\A#[0-9a-fA-F]{3}\z|\A#[0-9a-fA-F]{6}\z/
25
+ private_constant :BARE_REFERENCE_RE, :HEX_COLOR_RE
26
+
15
27
  def initialize(color:, style: SOLID, reference: nil)
16
28
  @style = style
17
29
  @color = color
@@ -25,8 +37,12 @@ module Emfsvg
25
37
  end
26
38
 
27
39
  def self.from_fill(value)
28
- if (match = /\A(?:#(img-[1-9]\d*-ref)|url\(#(img-[1-9]\d*-ref)\))\z/.match(value.to_s))
29
- return new(style: PATTERN, color: Color.parse("black"), reference: match[1] || match[2])
40
+ if (match = URL_REFERENCE_RE.match(value.to_s))
41
+ return pattern_reference(match[1])
42
+ end
43
+
44
+ if (match = BARE_REFERENCE_RE.match(value.to_s)) && !HEX_COLOR_RE.match?(value.to_s)
45
+ return pattern_reference(match[1])
30
46
  end
31
47
 
32
48
  color = Color.parse(value)
@@ -37,6 +53,16 @@ module Emfsvg
37
53
  color = Color.parse(value)
38
54
  new(style: color.null? ? NULL : SOLID, color: color)
39
55
  end
56
+
57
+ # A referenced paint server carries black as its wire color. When
58
+ # the translation layer resolves the reference to a bitmap pattern
59
+ # brush, the brush's DIB decides the appearance; when it cannot
60
+ # resolve it (gradient, unknown id), black is the solid fallback
61
+ # that keeps the draw visible instead of failing the translation.
62
+ def self.pattern_reference(id)
63
+ new(style: PATTERN, color: Color.parse("black"), reference: id)
64
+ end
65
+ private_class_method :pattern_reference
40
66
  end
41
67
  end
42
68
  end
@@ -11,6 +11,11 @@ module Emfsvg
11
11
  def self.call(element, context)
12
12
  pattern = element.selected_pattern || element.unused_pattern
13
13
  emit_pattern(pattern, context) if pattern
14
+ element.children.each do |child|
15
+ next unless child.is_a?(Svg::Elements::Pattern)
16
+
17
+ register_pattern_brush(child, context)
18
+ end
14
19
  registry = context.clip_path_registry
15
20
  return unless registry
16
21
 
@@ -25,6 +30,32 @@ module Emfsvg
25
30
  end
26
31
  end
27
32
 
33
+ # General (non-canonical) <pattern> definitions from arbitrary
34
+ # SVG authors (e.g. Visio exports): a raster tile referenced by
35
+ # fill="url(#id)". The declared SVG width/height live in tile
36
+ # units, not bitmap pixels, so unlike #emit_pattern the DIB is
37
+ # built from the decoded bitmap's own dimensions.
38
+ def self.register_pattern_brush(pattern, context)
39
+ return unless pattern.id && pattern.image&.data_uri?
40
+ return unless pattern.image.mime_type == "image/png"
41
+ return if context.pattern_handles.key?(pattern.id)
42
+
43
+ decoded = PngDecoder.decode(pattern.image.decoded_bytes)
44
+ return unless decoded
45
+
46
+ dib = DibEncoder.encode(decoded.width, decoded.height, decoded.pixels)
47
+ return if dib.empty?
48
+
49
+ handle = context.allocate_handle
50
+ context.emit(Emf::Emr::Binary::Records::CreateDibPatternBrushPt,
51
+ i_type: Emf::Emr::Binary::TypeCodes::CREATEDIBPATTERNBRUSHPT,
52
+ ih_brush: handle, i_usage: 0,
53
+ off_bmi: 32, cb_bmi: 40, off_bits: 72, cb_bits: dib.bytesize - 40,
54
+ body: dib)
55
+ context.pattern_handles[pattern.id] = handle
56
+ end
57
+ private_class_method :register_pattern_brush
58
+
28
59
  def self.emit_pattern(pattern, context)
29
60
  image = pattern.image
30
61
  decoded = PngDecoder.decode(image.decoded_bytes)
@@ -128,12 +128,20 @@ module Emfsvg
128
128
 
129
129
  def emit_create_brush(context, paint)
130
130
  if paint.reference
131
- handle = context.pattern_handles.fetch(paint.reference) do
132
- raise FormatError, "undefined SVG pattern: #{paint.reference.inspect}"
131
+ handle = context.pattern_handles[paint.reference]
132
+ if handle
133
+ context.emit(Emf::Emr::Binary::Records::SelectObject, i_type: SELECTOBJECT, ih_object: handle)
134
+ # Definition brushes remain available for subsequent draws.
135
+ return nil
133
136
  end
134
- context.emit(Emf::Emr::Binary::Records::SelectObject, i_type: SELECTOBJECT, ih_object: handle)
135
- # Definition brushes remain available for subsequent draws.
136
- return nil
137
+
138
+ # No resolvable brush for the reference (gradient paint
139
+ # server, unsupported pattern content, definition after the
140
+ # draw). Degrade to the paint's solid fallback color rather
141
+ # than failing the whole translation.
142
+ Kernel.warn("emfsvg: unresolved SVG paint reference " \
143
+ "#{paint.reference.inspect}; fill degraded to solid black")
144
+ paint = Svg::Paint.new(color: paint.color, style: Svg::Paint::SOLID)
137
145
  end
138
146
 
139
147
  handle = context.allocate_handle
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Emfsvg
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emfsvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -145,6 +145,7 @@ files:
145
145
  - lib/emfsvg/svg/elements/image.rb
146
146
  - lib/emfsvg/svg/elements/line.rb
147
147
  - lib/emfsvg/svg/elements/path.rb
148
+ - lib/emfsvg/svg/elements/pattern.rb
148
149
  - lib/emfsvg/svg/elements/polygon.rb
149
150
  - lib/emfsvg/svg/elements/polyline.rb
150
151
  - lib/emfsvg/svg/elements/rect.rb