Skip to content

Commit

Permalink
Choose correct glyphs for inline font change
Browse files Browse the repository at this point in the history
  • Loading branch information
camelpunch committed Oct 27, 2024
1 parent 9012b3c commit 2803c63
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lib/mudbrick/text_block/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ defmodule Mudbrick.TextBlock.Output do

def add_part(output, part, operator) do
output
|> Output.with_font(struct!(operator, font: output.font, text: part.text), part)
|> Output.with_font(
struct!(operator, font: part.font || output.font, text: part.text),
part
)
|> Output.colour(part.colour)
end
end
Expand Down Expand Up @@ -88,7 +91,7 @@ defmodule Mudbrick.TextBlock.Output do

def add_part(output, part) do
output
|> Output.with_font(%Tj{font: output.font, text: part.text}, part)
|> Output.with_font(%Tj{font: part.font || output.font, text: part.text}, part)
|> Output.colour(part.colour)
end
end
Expand Down
41 changes: 28 additions & 13 deletions test/mudbrick/text_block_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
defmodule Mudbrick.TextBlockTest do
use ExUnit.Case, async: true

import Mudbrick.TestHelper, only: [bodoni_regular: 0, bodoni_bold: 0]
import Mudbrick.TestHelper,
only: [
bodoni_regular: 0,
bodoni_bold: 0,
franklin_regular: 0
]

alias Mudbrick.Page
alias Mudbrick.TextBlock
Expand Down Expand Up @@ -67,7 +72,7 @@ defmodule Mudbrick.TextBlockTest do
"() '",
"ET"
] =
output(fn font, _ ->
output(fn font, _, _ ->
TextBlock.new(
font: font,
font_size: 10,
Expand Down Expand Up @@ -100,7 +105,7 @@ defmodule Mudbrick.TextBlockTest do
"<00C0> Tj",
"ET"
] =
output(fn font, _ ->
output(fn font, _, _ ->
TextBlock.new(
font: font,
font_size: 10,
Expand All @@ -125,18 +130,22 @@ defmodule Mudbrick.TextBlockTest do
"/F2 10 Tf",
"<00B400FC00ED00BB01B7> Tj",
"/F1 10 Tf",
"<00B40121011D01B7011D00D500D9011601B700D9011600F4019E011D> Tj",
"<00B40121011D01B7011D00D500D9011601B700D9011600F4019E011D01B7> Tj",
"/F3 10 Tf",
"<015A01050109015201F00109015201F000FF014B00C30125011B011E01090125> Tj",
"/F1 10 Tf",
"ET"
] =
output(fn regular, bold ->
output(fn regular, bold, franklin ->
TextBlock.new(
font: regular,
font_size: 10,
position: {400, 500}
)
|> TextBlock.write("this is ")
|> TextBlock.write("bold ", font: bold)
|> TextBlock.write("but this isn't")
|> TextBlock.write("but this isn't ")
|> TextBlock.write("this is franklin", font: franklin)
end)
|> operations()
end
Expand Down Expand Up @@ -179,7 +188,7 @@ defmodule Mudbrick.TextBlockTest do
"<00D500D9> Tj",
"ET"
] =
output(fn font, _ ->
output(fn font, _, _ ->
Mudbrick.TextBlock.new(
font: font,
font_size: 10,
Expand Down Expand Up @@ -218,16 +227,19 @@ defmodule Mudbrick.TextBlockTest do
"12.0 TL",
"400 500 Td",
"BT",
"293.34000000000003 500.0 Td",
"225.89999999999998 500.0 Td",
"0 0 0 rg",
"<011D00D500D9011601B700D9011601B7> Tj",
"/F2 10 Tf",
"<00B400FC00ED00BB01B7> Tj",
"/F1 10 Tf",
"<00B40121011D01B7011D00D500D9011601B700D9011600F4019E011D> Tj",
"<00B40121011D01B7011D00D500D9011601B700D9011600F4019E011D01B7> Tj",
"/F3 10 Tf",
"<015A01050109015201F00109015201F000FF014B00C30125011B011E01090125> Tj",
"/F1 10 Tf",
"ET"
] =
output(fn regular, bold ->
output(fn regular, bold, franklin ->
TextBlock.new(
font: regular,
font_size: 10,
Expand All @@ -236,7 +248,8 @@ defmodule Mudbrick.TextBlockTest do
)
|> TextBlock.write("this is ")
|> TextBlock.write("bold ", font: bold)
|> TextBlock.write("but this isn't")
|> TextBlock.write("but this isn't ")
|> TextBlock.write("this is franklin", font: franklin)
end)
|> operations()
end
Expand All @@ -256,15 +269,17 @@ defmodule Mudbrick.TextBlockTest do
compress: false,
fonts: %{
a: [file: bodoni_regular()],
b: [file: bodoni_bold()]
b: [file: bodoni_bold()],
c: [file: franklin_regular()]
}
)
|> page(size: Page.size(:letter))

fonts = Mudbrick.Document.root_page_tree(doc).value.fonts
regular_font = Map.fetch!(fonts, :a).value
bold_font = Map.fetch!(fonts, :b).value
block = f.(regular_font, bold_font)
franklin_regular_font = Map.fetch!(fonts, :c).value
block = f.(regular_font, bold_font, franklin_regular_font)

ops = Output.from(block)

Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Mudbrick.TestHelper do
@bodoni_regular System.fetch_env!("FONT_LIBRE_BODONI_REGULAR") |> File.read!()
@bodoni_bold System.fetch_env!("FONT_LIBRE_BODONI_BOLD") |> File.read!()
@franklin_regular System.fetch_env!("FONT_LIBRE_FRANKLIN_REGULAR") |> File.read!()
@flower Path.join(__DIR__, "fixtures/JPEG_example_flower.jpg") |> File.read!()
@example_png Path.join(__DIR__, "fixtures/Example.png") |> File.read!()

Expand All @@ -22,6 +23,10 @@ defmodule Mudbrick.TestHelper do
@bodoni_bold
end

def franklin_regular do
@franklin_regular
end

def flower do
@flower
end
Expand Down

0 comments on commit 2803c63

Please sign in to comment.