-
Notifications
You must be signed in to change notification settings - Fork 39
Core support for embedded files. Name dictionary entry #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dvdalilue
wants to merge
7
commits into
prawnpdf:master
Choose a base branch
from
dvdalilue:feature/embedded-file-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd0fbed
Core support for embedded files. Name dictionary entry
dvdalilue 503c388
Core support for embedded files. Name dictionary entry
dvdalilue ea9dd93
Alias without symbols
dvdalilue 9d6edf5
Bumping PDF version to 1.4
dvdalilue 3202680
Testing EmbeddedFiles module with mock class
dvdalilue 9737f81
Bumping version on each call. Cheap operation
dvdalilue a1aa484
Testing name tree size
dvdalilue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PDF | ||
| module Core | ||
| module EmbeddedFiles #:nodoc: | ||
| # The maximum number of children to fit into a single node in the | ||
| # EmbeddedFiles tree. | ||
| NAME_TREE_CHILDREN_LIMIT = 20 #:nodoc: | ||
|
|
||
| # The EmbeddedFiles name tree in the Name dictionary (see | ||
| # Prawn::Document::Internal#names). This name tree is used to store named | ||
| # embedded files (PDF spec 3.10.3). (For more on name trees, see section | ||
| # 3.8.4 in the PDF spec.) | ||
| # | ||
| def embedded_files | ||
| bump_min_version | ||
|
|
||
| names.data[:EmbeddedFiles] ||= ref!( | ||
pointlessone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PDF::Core::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT) | ||
| ) | ||
| end | ||
|
|
||
| # Adds a new embedded file to the EmbeddedFiles name tree | ||
| # (see #embedded_files). The +reference+ parameter will be converted into | ||
| # a PDF::Core::Reference if it is not already one. | ||
| # | ||
| def add_embedded_file(name, reference) | ||
| reference = ref!(reference) unless reference.is_a?(PDF::Core::Reference) | ||
pointlessone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| embedded_files.data.add(name, reference) | ||
| end | ||
|
|
||
| # Friendly method alias to attach file specifications in the catalog | ||
| alias attach_file add_embedded_file | ||
|
|
||
| private | ||
|
|
||
| def bump_min_version | ||
| renderer.min_version(1.4) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| class EmbeddedFiles | ||
| include PDF::Core::EmbeddedFiles | ||
|
|
||
| class InnerRenderer; def min_version(value); end; end | ||
|
|
||
| attr_reader :renderer | ||
|
|
||
| def initialize | ||
| @size = 0 | ||
| @root = ref!(Type: :Catalog) | ||
| @renderer = InnerRenderer.new | ||
| end | ||
|
|
||
| def names | ||
| @root.data[:Names] ||= ref!(Type: :Names) | ||
| end | ||
|
|
||
| def ref!(data) | ||
| @size += 1 | ||
|
|
||
| PDF::Core::Reference.new(@size, data) | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe EmbeddedFiles do | ||
| it 'has an empty catalog object' do | ||
| t = described_class.new | ||
| pdf_object = "2 0 obj\n<< /Type /Names\n>>\nendobj\n" | ||
| expect(t.names.object).to eq pdf_object | ||
| end | ||
|
|
||
| it 'has an embedded files object with no names' do | ||
| t = described_class.new | ||
| pdf_object = "3 0 obj\n<< /Names []\n>>\nendobj\n" | ||
| expect(t.embedded_files.object).to eq pdf_object | ||
| end | ||
|
|
||
| it 'has a catalog object with an embedded files reference' do | ||
| t = described_class.new | ||
| t.embedded_files | ||
|
|
||
| pdf_object = "2 0 obj\n<< /Type /Names\n/EmbeddedFiles 3 0 R\n>>\nendobj\n" | ||
| expect(t.names.object).to eq pdf_object | ||
| end | ||
|
|
||
| it 'has an embedded files object with one name reference' do | ||
| t = described_class.new | ||
| file_name = PDF::Core::LiteralString.new('my_file') | ||
|
|
||
| t.add_embedded_file(file_name, t.ref!(Type: :Filespec)) | ||
|
|
||
| pdf_object = "4 0 obj\n<< /Names [(my_file) 2 0 R]\n>>\nendobj\n" | ||
| expect(t.embedded_files.data.size).to eq 1 | ||
| expect(t.embedded_files.object).to eq pdf_object | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.