File tree Expand file tree Collapse file tree 4 files changed +78
-16
lines changed Expand file tree Collapse file tree 4 files changed +78
-16
lines changed Original file line number Diff line number Diff line change @@ -74,13 +74,39 @@ defmodule Mudbrick do
74
74
_ -> false
75
75
end )
76
76
77
- if latest_font_setting . font . descendant do
77
+ context =
78
78
ContentStream . add (
79
79
context ,
80
- ContentStream.Tj ,
81
- font: latest_font_setting . font ,
82
- text: text
80
+ ContentStream.TL ,
81
+ leading: latest_font_setting . size * 1.2
83
82
)
83
+
84
+ if latest_font_setting . font . descendant do
85
+ text_parts = String . split ( text , "\n " )
86
+
87
+ { _ , context } =
88
+ for part <- text_parts , reduce: { true , context } do
89
+ { true , acc } ->
90
+ { false ,
91
+ ContentStream . add (
92
+ acc ,
93
+ ContentStream.Tj ,
94
+ font: latest_font_setting . font ,
95
+ text: part
96
+ ) }
97
+
98
+ { false , acc } ->
99
+ { false ,
100
+ ContentStream . add (
101
+ acc ,
102
+ ContentStream.Tj ,
103
+ font: latest_font_setting . font ,
104
+ operator: "'" ,
105
+ text: part
106
+ ) }
107
+ end
108
+
109
+ context
84
110
else
85
111
ContentStream . add (
86
112
context ,
Original file line number Diff line number Diff line change @@ -24,8 +24,20 @@ defmodule Mudbrick.ContentStream do
24
24
end
25
25
end
26
26
27
+ defmodule TL do
28
+ defstruct [ :leading ]
29
+
30
+ defimpl Mudbrick.Object do
31
+ def from ( tl ) do
32
+ [ to_string ( tl . leading ) , " TL" ]
33
+ end
34
+ end
35
+ end
36
+
27
37
defmodule Tj do
28
- defstruct [ :font , :text ]
38
+ defstruct font: nil ,
39
+ operator: "Tj" ,
40
+ text: nil
29
41
30
42
defimpl Mudbrick.Object do
31
43
def from ( tj ) do
@@ -42,9 +54,9 @@ defmodule Mudbrick.ContentStream do
42
54
|> String . pad_leading ( 4 , "0" )
43
55
end )
44
56
45
- [ "<" , glyph_ids_hex , "> Tj " ]
57
+ [ "<" , glyph_ids_hex , "> #{ tj . operator } " ]
46
58
else
47
- [ Mudbrick.Object . from ( tj . text ) , " Tj " ]
59
+ [ Mudbrick.Object . from ( tj . text ) , " #{ tj . operator } " ]
48
60
end
49
61
end
50
62
end
Original file line number Diff line number Diff line change @@ -3,13 +3,35 @@ defmodule Mudbrick.ContentStreamTest do
3
3
4
4
import Mudbrick
5
5
6
- alias Mudbrick.ContentStream
6
+ alias Mudbrick.ContentStream.Tj
7
7
alias Mudbrick.Font
8
8
alias Mudbrick.Indirect
9
9
alias Mudbrick.Object
10
10
11
11
@ font_data System . fetch_env! ( "FONT_LIBRE_BODONI_REGULAR" ) |> File . read! ( )
12
12
13
+ test "linebreaks are converted to the ' operator" do
14
+ { _doc , content_stream } =
15
+ new ( )
16
+ |> page (
17
+ size: :letter ,
18
+ fonts: % { bodoni: [ file: @ font_data ] }
19
+ )
20
+ |> contents ( )
21
+ |> font ( :bodoni , size: 10 )
22
+ |> text_position ( 0 , 700 )
23
+ |> text ( """
24
+ a
25
+ b\
26
+ """ )
27
+
28
+ [ apostrophe , tj , leading | _ ] = content_stream . value . operations
29
+
30
+ assert Object . from ( leading ) |> to_string ( ) == "12.0 TL"
31
+ assert Object . from ( tj ) |> to_string ( ) == "<00A5> Tj"
32
+ assert Object . from ( apostrophe ) |> to_string ( ) == "<00B4> '"
33
+ end
34
+
13
35
test "font is assigned to the operator struct when font descendant present" do
14
36
{ _doc , content_stream } =
15
37
new ( )
@@ -24,7 +46,7 @@ defmodule Mudbrick.ContentStreamTest do
24
46
25
47
[ show_text_operation | _ ] = content_stream . value . operations
26
48
27
- assert % ContentStream. Tj{
49
+ assert % Tj {
28
50
text: "CO₂" ,
29
51
font: % Font {
30
52
name: :"LibreBodoni-Regular" ,
Original file line number Diff line number Diff line change @@ -37,10 +37,10 @@ defmodule MudbrickTest do
37
37
}
38
38
)
39
39
|> contents ( )
40
- |> font ( :helvetica , size: 24 )
40
+ |> font ( :helvetica , size: 100 )
41
41
|> text_position ( 300 , 400 )
42
42
|> text ( "hello, world!" )
43
- |> font ( :courier , size: 12 )
43
+ |> font ( :courier , size: 10 )
44
44
|> text_position ( 0 , - 24 )
45
45
|> text ( "a new line!" )
46
46
|> page ( size: :a4 )
@@ -86,15 +86,17 @@ defmodule MudbrickTest do
86
86
>>
87
87
endobj
88
88
6 0 obj
89
- <</Length 81
89
+ <</Length 99
90
90
>>
91
91
stream
92
92
BT
93
- /F2 24 Tf
93
+ /F2 100 Tf
94
94
300 400 Td
95
+ 120.0 TL
95
96
(hello, world!) Tj
96
- /F1 12 Tf
97
+ /F1 10 Tf
97
98
0 -24 Td
99
+ 12.0 TL
98
100
(a new line!) Tj
99
101
ET
100
102
endstream
@@ -114,13 +116,13 @@ defmodule MudbrickTest do
114
116
0000000241 00000 n
115
117
0000000345 00000 n
116
118
0000000490 00000 n
117
- 0000000620 00000 n
119
+ 0000000638 00000 n
118
120
trailer
119
121
<</Root 2 0 R
120
122
/Size 8
121
123
>>
122
124
startxref
123
- 698
125
+ 716
124
126
%%EOF\
125
127
"""
126
128
end
You can’t perform that action at this time.
0 commit comments