Class: Skia::Document
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
- .build_pdf_metadata(metadata) ⇒ Object
- .create_pdf(path, metadata: nil, &block) ⇒ Object
- .create_pdf_document(stream, metadata) ⇒ Object
- .create_pdf_stream(metadata: nil, &block) ⇒ Object
- .make_pdf_datetime(value, refs) ⇒ Object
- .make_sk_string(value, refs) ⇒ Object
- .with_optional_block(doc, &block) ⇒ Object
Instance Method Summary collapse
- #abort ⇒ Object
- #begin_page(width, height, rect = nil, content_rect: nil) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #end_page ⇒ Object
-
#initialize(ptr, stream, stream_kind:, metadata_refs: []) ⇒ Document
constructor
A new instance of Document.
- #page(width, height, content_rect: nil, &block) ⇒ Object
- #to_data ⇒ Object
Methods inherited from Base
#null?, release_callback, #to_ptr
Constructor Details
#initialize(ptr, stream, stream_kind:, metadata_refs: []) ⇒ Document
Returns a new instance of Document.
5 6 7 8 9 10 11 12 |
# File 'lib/skia/document.rb', line 5 def initialize(ptr, stream, stream_kind:, metadata_refs: []) super(ptr, :sk_document_unref) @stream = stream @stream_kind = stream_kind = @closed = false @data = nil end |
Class Method Details
.build_pdf_metadata(metadata) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/skia/document.rb', line 124 def self.() unless .is_a?(Hash) raise ArgumentError, 'metadata must be a Hash' end md = .each_with_object({}) { |(k, v), h| h[k.to_sym] = v } refs = [] = Native::SKDocumentPdfMetadata.new { title: :fTitle, author: :fAuthor, subject: :fSubject, keywords: :fKeywords, creator: :fCreator, producer: :fProducer }.each do |key, native_key| [native_key] = make_sk_string(md[key], refs) end [:fCreation] = make_pdf_datetime(md[:creation], refs) [:fModified] = make_pdf_datetime(md[:modified], refs) [:fRasterDPI] = md.fetch(:raster_dpi, 72.0).to_f [:fPDFA] = md[:pdfa] ? 1 : 0 [:fEncodingQuality] = md.fetch(:encoding_quality, 101).to_i refs << [, refs] end |
.create_pdf(path, metadata: nil, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/skia/document.rb', line 14 def self.create_pdf(path, metadata: nil, &block) stream = Native.sk_filewstream_new(path) raise Error, "Failed to create file stream for: #{path}" if stream.nil? || stream.null? ptr, = create_pdf_document(stream, ) raise Error, 'Failed to create PDF document' if ptr.nil? || ptr.null? doc = new(ptr, stream, stream_kind: :file, metadata_refs: ) stream = nil # ownership moved to Document instance with_optional_block(doc, &block) rescue StandardError Native.sk_filewstream_destroy(stream) if stream && !stream.null? raise end |
.create_pdf_document(stream, metadata) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/skia/document.rb', line 112 def self.create_pdf_document(stream, ) return [Native.sk_document_create_pdf_from_stream(stream), []] if .nil? unless Native.function_available?(:sk_document_create_pdf_from_stream_with_metadata) raise UnsupportedOperationError, 'PDF metadata is not supported by the current libSkiaSharp build' end , refs = () ptr = Native.(stream, ) [ptr, refs.grep(FFI::Pointer)] end |
.create_pdf_stream(metadata: nil, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/skia/document.rb', line 29 def self.create_pdf_stream(metadata: nil, &block) stream = Native.sk_dynamicmemorywstream_new raise Error, 'Failed to create memory stream' if stream.nil? || stream.null? ptr, = create_pdf_document(stream, ) raise Error, 'Failed to create PDF document' if ptr.nil? || ptr.null? doc = new(ptr, stream, stream_kind: :memory, metadata_refs: ) stream = nil # ownership moved to Document instance with_optional_block(doc, &block) rescue StandardError Native.sk_dynamicmemorywstream_destroy(stream) if stream && !stream.null? raise end |
.make_pdf_datetime(value, refs) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/skia/document.rb', line 167 def self.make_pdf_datetime(value, refs) return nil if value.nil? time = case value when Time value when Hash Time.new( value.fetch(:year), value.fetch(:month), value.fetch(:day), value.fetch(:hour, 0), value.fetch(:min, value.fetch(:minute, 0)), value.fetch(:sec, value.fetch(:second, 0)), value[:utc_offset] || '+00:00' ) else value.respond_to?(:to_time) ? value.to_time : nil end raise ArgumentError, "invalid datetime metadata: #{value.inspect}" if time.nil? local = time.getlocal ctime = Native::SKTimeDateTime.new ctime[:fTimeZoneMinutes] = (local.utc_offset / 60).to_i ctime[:fYear] = local.year ctime[:fMonth] = local.month ctime[:fDayOfWeek] = local.wday ctime[:fDay] = local.day ctime[:fHour] = local.hour ctime[:fMinute] = local.min ctime[:fSecond] = local.sec refs << ctime ctime end |
.make_sk_string(value, refs) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/skia/document.rb', line 154 def self.make_sk_string(value, refs) return nil if value.nil? bytes = value.to_s.b src = FFI::MemoryPointer.new(:char, bytes.bytesize) src.put_bytes(0, bytes) ptr = Native.sk_string_new_with_copy(src, bytes.bytesize) raise Error, 'Failed to allocate metadata string' if ptr.nil? || ptr.null? refs << ptr ptr end |
.with_optional_block(doc, &block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/skia/document.rb', line 100 def self.with_optional_block(doc, &block) if block begin block.call(doc) ensure doc.close unless doc.closed? end else doc end end |
Instance Method Details
#abort ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/skia/document.rb', line 79 def abort return if @closed Native.sk_document_abort(@ptr) close_stream(finalize_memory_stream: false) @closed = true end |
#begin_page(width, height, rect = nil, content_rect: nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/skia/document.rb', line 44 def begin_page(width, height, rect = nil, content_rect: nil) page_rect = content_rect || rect content_ptr = page_rect&.to_struct canvas_ptr = Native.sk_document_begin_page(@ptr, width.to_f, height.to_f, content_ptr) raise Error, 'Failed to begin page' if canvas_ptr.nil? || canvas_ptr.null? canvas = Canvas.new(canvas_ptr) if block_given? begin yield canvas ensure end_page end else canvas end end |
#close ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/skia/document.rb', line 71 def close return if @closed Native.sk_document_close(@ptr) close_stream(finalize_memory_stream: true) @closed = true end |
#closed? ⇒ Boolean
94 95 96 |
# File 'lib/skia/document.rb', line 94 def closed? @closed end |
#end_page ⇒ Object
67 68 69 |
# File 'lib/skia/document.rb', line 67 def end_page Native.sk_document_end_page(@ptr) end |
#page(width, height, content_rect: nil, &block) ⇒ Object
63 64 65 |
# File 'lib/skia/document.rb', line 63 def page(width, height, content_rect: nil, &block) begin_page(width, height, content_rect: content_rect, &block) end |
#to_data ⇒ Object
87 88 89 90 91 92 |
# File 'lib/skia/document.rb', line 87 def to_data raise UnsupportedOperationError, 'to_data is available only for memory-backed PDF documents' unless @stream_kind == :memory close unless @closed @data end |