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

Fix warning due to Elixir 1.17 #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 4 additions & 3 deletions lib/pdf/font.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Pdf.Font do

defmodule font_module do
@moduledoc false

@doc "The name of the font"
def name, do: unquote(metrics.name)
@doc "The full name of the font"
Expand Down Expand Up @@ -161,10 +162,10 @@ defmodule Pdf.Font do
|> Dictionary.put("Type", n("Font"))
|> Dictionary.put("Subtype", n("Type1"))
|> Dictionary.put("Name", n("F#{id}"))
|> Dictionary.put("BaseFont", n(font.name))
|> Dictionary.put("BaseFont", n(font_field(font, :name)))
|> Dictionary.put("FirstChar", 32)
|> Dictionary.put("LastChar", font.last_char)
|> Dictionary.put("Widths", Array.new(Enum.drop(font.widths, 32)))
|> Dictionary.put("LastChar", font_field(font, :last_char))
|> Dictionary.put("Widths", Array.new(Enum.drop(font_field(font, :widths), 32)))
|> Dictionary.put("Encoding", n("WinAnsiEncoding"))
end

Expand Down
6 changes: 3 additions & 3 deletions lib/pdf/fonts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule Pdf.Fonts do
end

defp lookup_font(state, font, opts) do
lookup_font(state, font.family_name, opts)
lookup_font(state, font_field(font, :family_name), opts)
end

defp lookup_font(%{fonts: fonts} = state, name) when is_binary(name) do
Expand All @@ -122,7 +122,7 @@ defmodule Pdf.Fonts do
end

defp lookup_font(%{fonts: fonts} = state, font_module) do
case fonts[font_module.name] do
case fonts[font_field(font_module, :name)] do
nil -> load_font(state, font_module)
font -> {state, font}
end
Expand All @@ -138,7 +138,7 @@ defmodule Pdf.Fonts do
object: font_object
}

fonts = Map.put(fonts, font_module.name, reference)
fonts = Map.put(fonts, font_field(font_module, :name), reference)
{%{state | last_id: id, fonts: fonts}, reference}
end
end
Expand Down
15 changes: 8 additions & 7 deletions lib/pdf/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,18 @@ defmodule Pdf.Page do
if Enum.any?([:bold, :italic], &Keyword.has_key?(opts, &1)) do
Fonts.get_font(fonts, font, Keyword.take(opts, [:bold, :italic]))
else
Fonts.get_font(fonts, font.name, [])
Fonts.get_font(fonts, font_field(font, :name), [])
end

font_size = Keyword.get(opts, :font_size, page.current_font_size)
leading = Keyword.get(opts, :leading, page.leading || page.current_font_size)
color = Keyword.get(opts, :color, page.fill_color)

height = Enum.max([leading, font_size])
ascender = font.module.ascender * font_size / 1000
descender = -(font.module.descender * font_size / 1000)
cap_height = (font.module.cap_height || 0) * font_size / 1000
x_height = (font.module.x_height || 0) * font_size / 1000
ascender = font_field(font.module, :ascender) * font_size / 1000
descender = -(font_field(font.module, :descender) * font_size / 1000)
cap_height = (font_field(font.module, :cap_height) || 0) * font_size / 1000
x_height = (font_field(font.module, :x_height) || 0) * font_size / 1000
line_gap = (font_size - (ascender + descender)) / 2

width = Font.text_width(font.module, text, font_size, opts)
Expand Down Expand Up @@ -463,11 +463,12 @@ defmodule Pdf.Page do
attributed_text
|> merge_same_opts
|> Enum.reduce(page, fn {text, _width, opts}, page ->
module = opts[:font].module
page
|> set_font(opts[:font].module.name, opts[:font_size], opts)
|> set_font(font_field(module, :name), opts[:font_size], opts)
|> set_text_leading(opts[:leading])
|> set_fill_color(opts[:color])
|> push(kerned_text(opts[:font].module, text, opts))
|> push(kerned_text(module, text, opts))
end)
end

Expand Down
8 changes: 8 additions & 0 deletions lib/pdf/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ defmodule Pdf.Utils do
@doc false
def a(%Pdf.Array{} = array), do: array
def a(list), do: Pdf.Array.new(list)

def font_field(font, name) do
if is_struct(font) do
Map.get(font, name)
else
apply(font, name, [])
end
end
end