Skip to content
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

feat: Pixels to Points #1364

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/prawn/measurement_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ def ft
def pt
pt2pt(self)
end

# Convert from pixels to points.
#
# @return [Number]
def px
px2pt(self)
end
end
8 changes: 8 additions & 0 deletions lib/prawn/measurements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def m2pt(m)
def pt2mm(pt)
pt * 1 / mm2pt(1) # (25.4 / 72)
end

# Convert pixels to points.
#
# @param px [Number]
# @return [Number]
def px2pt(px)
px * 72 / 96.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number "72" comes from the PDF specification. Where does the number "96" come from?

As far as I'm aware the standard DPI seems to be 96 but DPI can be changed by the user.

end
end
end
# rubocop: enable Naming/MethodParameterName
1 change: 1 addition & 0 deletions spec/prawn/measurements_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
expect(1.ft).to eq(72 * 12)
expect(1.yd).to eq(72 * 12 * 3)
expect(1.pt).to eq(1)
expect(1.px).to eq(0.75)
end
end
end