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

Make luaotfload transparency work with its hooks #101

Merged
merged 3 commits into from
Aug 30, 2022

Conversation

vlasakm
Copy link
Contributor

@vlasakm vlasakm commented Aug 24, 2022

Since we added new PDF resource management, transparent font settings through luaotfload were broken.

It was now possible to fix this thanks to new hooks in luaotfload. See latex3/luaotfload#212.

I tested with https://github.com/vlasakm/optex-minim/blob/master/examples/optex-minim-pgf-test.tex, since that file mixes a lot of things (luaotfload transparency, optex transparency, minim metapost resources, pgf resources, optex macro resources, ...), but didn't do a lot of thorough testing.

Since we added new PDF resource management, transparent font settings
through luaotfload were broken.

It was now possible to fix this thanks to new hooks in luaotfload.
See latex3/luaotfload#212
@vlasakm
Copy link
Contributor Author

vlasakm commented Aug 25, 2022

I hope gucci-on-fleek/lua-widow-control#39 can be resolved here as well, while at it. I have "draft" in vlasakm@1b2070e.

@vlasakm
Copy link
Contributor Author

vlasakm commented Aug 26, 2022

Alright, 1b2070e is good for gucci-on-fleek/lua-widow-control#39, so I pushed it and hope this PR is ready to go. What do you think @olsak?

@olsak
Copy link
Owner

olsak commented Aug 26, 2022

cited from gucci-on-fleek/lua-widow-control#39: "OpTeX: Cannot colour the first line of the page since OpTeX resets the colour there." I don't understand this. I am unable to create a minimal example which shows the problem.

@gucci-on-fleek
Copy link

@olsak

A messy example:

\fontfam[LM]

\directlua{
    callback.add_to_callback("pre_output_filter", function (head)
        for hlist in node.traverse_id(node.id("hlist"), head) do
            local colour = string.format(
                "\%.2f \%.2f \%.2f rg",
                1,
                math.random(),
                math.random()
            )

            local start_colour = node.new("whatsit", "pdf_colorstack")
            start_colour.stack = 0
            start_colour.command = 1
            start_colour.data = colour

            local end_colour = node.new("whatsit", "pdf_colorstack")
            end_colour.stack = 0
            end_colour.command = 2

            start_colour.next = hlist.list
            hlist.list = start_colour
            node.slide(hlist.list).next = end_colour
        end

        return head
    end, "test")
}

Hello!

Hello!

Hello!

\vfill\eject

Hello!

Hello!

Hello!

\bye

If we disassemble the PDF with qdf, you can see that OpTeX resets the text colour to black after the pdf_colorstack, thus overriding it.

%% Contents for page 1
%% Original object ID: 3 0
13 0 obj
<<
  /Length 14 0 R
>>
stream
0.68 0.73 0.66 rg
0 g
BT
/F37 9.96264 Tf
1 0 0 1 90.791 761.061 Tm [<003e00320048004800510035>]TJ
0 g 0 G
0.90 0.54 0.64 rg
1 0 0 1 90.791 749.106 Tm [<003e00320048004800510035>]TJ
0 g 0 G
0.90 0.93 0.96 rg
1 0 0 1 90.791 737.151 Tm [<003e00320048004800510035>]TJ
0 g 0 G
1 0 0 1 295.147 55.46 Tm [<0052>]TJ
ET
endstream
endobj

@vlasakm
Copy link
Contributor Author

vlasakm commented Aug 26, 2022

Well, in this case the fix can maybe be as simple as this:

diff --git a/optex/base/optex.lua b/optex/base/optex.lua
index bb67d7e..9e9c83a 100644
--- a/optex/base/optex.lua
+++ b/optex/base/optex.lua
@@ -652,10 +652,10 @@ end
 -- for this. See the definition of `pre_shipout_filter` for details on
 -- limitations.
 callback.add_to_callback("pre_shipout_filter", function(list)
-    -- By setting initial color to -1 we force initial setting of color on
-    -- every page. This is useful for transparently supporting other default
-    -- colors than black (although it has a price for each normal document).
-    local list = colorize(todirect(list), -1, -1, 0)
+    -- Each page has implicitly preset black color and no transparency, we tell
+    -- this to the colorize function so that it can take advantage of it and
+    -- not set it.
+    local list = colorize(todirect(list), 0, 0, 0)
     return tonode(list)
 end, "_colors")
 --

This setting of current_color = -1 is what forces the color setting at the start of the page. This is not necessary for black, since black is the default color, so it is possible to staart with current_color = 0. This helps with lua-widow-controls colorings, because nodes inserted by it don't have the color attribute set (thus color = 0), so when they appear at the start of the page the attribute mechanism will not recolor them.

But this doesn't save the general case, where the nodes could have color attributes set. Making lua-widow-control use the attributes seems cleaner to me (the color attaches to the content, wherever it ends up).

@vlasakm
Copy link
Contributor Author

vlasakm commented Aug 26, 2022

Hm, maybe the interface should change, so that lua-widow-control can set the attribute directly.

@vlasakm
Copy link
Contributor Author

vlasakm commented Aug 27, 2022

In the last commit I wrapped the hooks into luaotfload in pcall to ignore any errors. This is to make it possible to use new OpTeX with old luaotfload without the hooks. It should work, but I didn't test any old versions.

@olsak olsak merged commit 856a504 into olsak:master Aug 30, 2022
@vlasakm vlasakm deleted the transparency branch September 1, 2022 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants