diff --git a/lib/xairo/text/extents.ex b/lib/xairo/text/extents.ex index cd9930c..e954146 100644 --- a/lib/xairo/text/extents.ex +++ b/lib/xairo/text/extents.ex @@ -42,6 +42,16 @@ defmodule Xairo.Text.Extents do y_advance: number() } + @doc """ + Returns the text exetnts of the given text in the context of the + image's configuration. + """ + def for(text, %Xairo.Image{resource: resource}) do + with {:ok, extents} <- Xairo.Native.text_extents(resource, text) do + extents + end + end + defimpl Inspect do import Inspect.Algebra diff --git a/test/xairo/text/extents_test.exs b/test/xairo/text/extents_test.exs new file mode 100644 index 0000000..c36b52b --- /dev/null +++ b/test/xairo/text/extents_test.exs @@ -0,0 +1,15 @@ +defmodule Xairo.Text.ExtentsTest do + use ExUnit.Case, async: true + + alias Xairo.Text.Extents + + describe "for/2" do + test "returns a Text.Extents struct for the given string in the context of the image" do + image = Xairo.new_image(1000, 1000, 20) + + %Extents{} = extents = Extents.for("hello", image) + + assert extents.text == "hello" + end + end +end