forked from xou/elixlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The previous work in xou#124 by x and x was using a twoCellAnchor but that's quite tricky to get right. This is now using a oneCellAnchor to make the calculations much simpler. I have added a few extra tests. I also fixed the formula because I couldn't run the example file without doing that first.
- Loading branch information
1 parent
a70d878
commit c2c2fd5
Showing
17 changed files
with
1,033 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -31,3 +31,6 @@ elixlsx-*.tar | |
|
||
# Misc. | ||
*.swp | ||
|
||
# Elixir LS cache | ||
/.elixir_ls/ |
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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,27 @@ | ||
defmodule Elixlsx.Compiler.DrawingCompInfo do | ||
alias __MODULE__ | ||
|
||
@moduledoc ~S""" | ||
Compilation info for a Drawing, to be filled | ||
during the actual write process. | ||
""" | ||
|
||
defstruct rId: "", | ||
filename: "drawing1.xml", | ||
drawingId: 0 | ||
|
||
@type t :: %DrawingCompInfo{ | ||
rId: String.t(), | ||
filename: String.t(), | ||
drawingId: non_neg_integer | ||
} | ||
|
||
@spec make(non_neg_integer, non_neg_integer) :: DrawingCompInfo.t() | ||
def make(drawingidx, rId) do | ||
%DrawingCompInfo{ | ||
rId: "rId" <> to_string(rId), | ||
filename: "drawing" <> to_string(drawingidx) <> ".xml", | ||
drawingId: drawingidx | ||
} | ||
end | ||
end |
This file contains 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,49 @@ | ||
defmodule Elixlsx.Compiler.DrawingDB do | ||
alias __MODULE__ | ||
alias Elixlsx.Image | ||
|
||
@moduledoc ~S""" | ||
Database of drawing elements in the whole document. | ||
Drawing id values must be unique across the document | ||
regardless of what kind of drawing they are. | ||
""" | ||
|
||
defstruct images: %{}, element_count: 0 | ||
|
||
@type t :: %DrawingDB{ | ||
images: %{Image.t() => pos_integer}, | ||
element_count: non_neg_integer | ||
} | ||
|
||
def register_image(drawingdb, image) do | ||
case Map.fetch(drawingdb.images, image) do | ||
:error -> | ||
%DrawingDB{ | ||
images: Map.put(drawingdb.images, image, drawingdb.element_count + 1), | ||
element_count: drawingdb.element_count + 1 | ||
} | ||
|
||
{:ok, _} -> | ||
drawingdb | ||
end | ||
end | ||
|
||
def get_id(drawingdb, image) do | ||
case Map.fetch(drawingdb.images, image) do | ||
:error -> | ||
raise %ArgumentError{ | ||
message: "Invalid key provided for DrawingDB.get_id: " <> inspect(image) | ||
} | ||
|
||
{:ok, id} -> | ||
id | ||
end | ||
end | ||
|
||
def image_types(db) do | ||
db.images | ||
|> Enum.map(fn {i, _} -> {i.extension, i.type} end) | ||
|> Enum.uniq() | ||
end | ||
end |
This file contains 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 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,77 @@ | ||
defmodule Elixlsx.Image do | ||
alias Elixlsx.Image | ||
|
||
@moduledoc ~S""" | ||
An Image can either by a path to an image, or | ||
a binary {"path_or_unique_id", <<binary>>} | ||
char is the max character width of a font, this is | ||
used when calculating how many pixels are in a column. | ||
You might need to experiment with this value | ||
depending on what font and size you are using. | ||
""" | ||
|
||
defstruct file_path: "", | ||
type: "image/png", | ||
extension: "png", | ||
x: 0, | ||
y: 0, | ||
x_offset: 0, | ||
y_offset: 0, | ||
width: 1, | ||
height: 1, | ||
binary: nil, | ||
align_x: :left, | ||
char: 7 | ||
|
||
@type t :: %Image{ | ||
file_path: String.t() | {String.t(), binary}, | ||
type: String.t(), | ||
extension: String.t(), | ||
x: integer, | ||
y: integer, | ||
x_offset: integer, | ||
y_offset: integer, | ||
width: integer, | ||
height: integer, | ||
binary: binary | nil, | ||
align_x: :left | :right, | ||
char: integer | ||
} | ||
|
||
@doc """ | ||
Create an image struct based on opts | ||
""" | ||
def new(_, _, _, opts \\ []) | ||
|
||
def new(file_path, x, y, opts) when is_binary(file_path) do | ||
new({file_path, nil}, x, y, opts) | ||
end | ||
|
||
def new({file_path, binary}, x, y, opts) do | ||
{ext, type} = image_type(file_path) | ||
|
||
%Image{ | ||
file_path: file_path, | ||
binary: binary, | ||
type: type, | ||
extension: ext, | ||
x: x, | ||
y: y, | ||
x_offset: Keyword.get(opts, :x_offset, 0), | ||
y_offset: Keyword.get(opts, :y_offset, 0), | ||
width: Keyword.get(opts, :width, 1), | ||
height: Keyword.get(opts, :height, 1), | ||
align_x: Keyword.get(opts, :align_x, :left), | ||
char: Keyword.get(opts, :char, 7) | ||
} | ||
end | ||
|
||
defp image_type(file_path) do | ||
case Path.extname(file_path) do | ||
".jpg" -> {"jpg", "image/jpeg"} | ||
".jpeg" -> {"jpeg", "image/jpeg"} | ||
".png" -> {"png", "image/png"} | ||
end | ||
end | ||
end |
Oops, something went wrong.