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

Draft mode colours don't work in OpTeX and ConTeXt #39

Closed
3 tasks done
gucci-on-fleek opened this issue Aug 14, 2022 · 5 comments
Closed
3 tasks done

Draft mode colours don't work in OpTeX and ConTeXt #39

gucci-on-fleek opened this issue Aug 14, 2022 · 5 comments
Assignees
Labels
ConTeXt Issues that only affect ConTeXt/LMTX Minor Bug A bug that doesn't completely break a document Plain TeX/OpTeX Issues that affect Plain TeX or OpTeX

Comments

@gucci-on-fleek
Copy link
Owner

gucci-on-fleek commented Aug 14, 2022

  • OpTeX: Cannot colour the first line of the page since OpTeX resets the colour there.

    Can probably use something similar to luaotfload.set_colorhandler as a solution.

  • ConTeXt MkIV: Same problem as OpTeX; however, this will require a different solution.

  • ConTeXt MkXL: pdf_colorstack literals are gone, so colour doesn't work at all. Not sure how to solve this one.

@gucci-on-fleek gucci-on-fleek added Minor Bug A bug that doesn't completely break a document ConTeXt Issues that only affect ConTeXt/LMTX labels Aug 14, 2022
@gucci-on-fleek gucci-on-fleek self-assigned this Aug 14, 2022
@gucci-on-fleek gucci-on-fleek added the Plain TeX/OpTeX Issues that affect Plain TeX or OpTeX label Aug 14, 2022
@vlasakm
Copy link
Contributor

vlasakm commented Aug 25, 2022

What interface do you think could be nicely abstracted / convenient?

Extracted from set_colorhandler there could be something like:

diff --git a/source/lua-widow-control.lua b/source/lua-widow-control.lua
index 1ef8617..dafd868 100644
--- a/source/lua-widow-control.lua
+++ b/source/lua-widow-control.lua
@@ -69,7 +69,7 @@ elseif format:find("latex") then -- lualatex, lualatex-dev, ...
 elseif format == "luatex" or format == "luahbtex" then -- Plain
     plain = true
 elseif format:find("optex") then -- OpTeX
-    optex = true
+    optex = _G.optex
 end
 
 --[[
@@ -407,12 +407,21 @@ local function colour_list(head, colour)
         return head
     end
 
+    local pdf_literal = string.format("%.2f %.2f %.2f rg", table.unpack(colour))
+
+    if optex and optex.set_node_color then
+        for n in node.traverse(head) do
+	    optex.set_node_color(n, pdf_literal)
+	end
+	return head
+    end
+
     -- Adapted from https://tex.stackexchange.com/a/372437
     -- \\pdfextension colorstack is ignored in LMTX
     local start_colour = new_node("whatsit", "pdf_colorstack")
     start_colour.stack = 0
     start_colour.command = 1
-    start_colour.data = string.format("%.2f %.2f %.2f rg", table.unpack(colour))
+    start_colour.data = pdf_literal
 
     local end_colour = new_node("whatsit", "pdf_colorstack")
     end_colour.stack = 0

Through vlasakm/OpTeX@1b2070e.

@gucci-on-fleek
Copy link
Owner Author

@vlasakm That looks great, thanks! I've committed your patch, although I won't do a release until the accompanying change is made in OpTeX.

gucci-on-fleek pushed a commit that referenced this issue Aug 26, 2022
@vlasakm
Copy link
Contributor

vlasakm commented Aug 26, 2022

Maybe if for all formats we can get the color attribute and a mapping from color to its number, there could be something like this loop instead:

	    for n in node.traverse(head) do
                node.setattribute(n, attribute, color)
	    end

@gucci-on-fleek I modified your example from olsak/OpTeX#101 for ConTeXt, but it doesn't work as I expected. Do you have any ideas? I am mostly clueless about debugging ConTeXt.

\setupcolors[state=start]

\starttext

\directlua{
local color = attributes.colors.register("mycolor", "rgb", 0, 1, 0)
local attribute = attributes.colors.attribute
callback.register("pre_output_filter", function (head)
        for hlist in node.traverseid(node.id("hlist"), head) do
	    for n in node.traverse(head) do
                node.setattribute(n, attribute, color)
	    end
        end

        return head
    end)
}

Hello!

Hello!

Hello!

\page

Hello!

Hello!

Hello!

\stoptext

gucci-on-fleek added a commit that referenced this issue Aug 27, 2022
Now draft mode colouring works for all lines in both MkIV/MkXL.

See #39
@gucci-on-fleek
Copy link
Owner Author

@vlasakm

Maybe if for all formats we can get the color attribute and a mapping from color to its number, there could be something like this loop instead

"All formats" is a little tricky here though. TeX82 doesn't have any native colour support, so each format handles it very differently:

  • OpTeX uses Lua attributes and a shipout hook

  • ConTeXt also uses Lua attributes, but it's best to treat them as a black box and just use the higher-level constructs instead

  • LaTeX nearly always uses color/xcolor, which uses \pdfextension colorstack with LuaTeX. There is a package that uses Lua attributes instead (luacolor), but I'd rather not load another package, especially one that may potentially break things.

    There is a plan to use Lua attributes for colours (see Use Lua for color latex3/latex3#944), but this isn't available yet.

  • Plain TeX doesn't really have any colour support. Eplain uses miniltx + color for its colour support, so it behaves identically to LaTeX here.

I think that this is just one of those places where we unfortunately need to keep completely different implementations for different formats.

I modified your example from olsak/OpTeX#101 for ConTeXt, but it doesn't work as I expected.

ConTeXt luckily has a higher-level interface for setting node colours. Like most of the ConTeXt Lua interfaces, it is completely undocumented, but I found it by searching through the code. See ddb2258 for an implementation.

Do you have any ideas?

Just a small typo in your code:

@@ -7,7 +7,7 @@
 local attribute = attributes.colors.attribute
 callback.register("pre_output_filter", function (head)
         for hlist in node.traverseid(node.id("hlist"), head) do
-	    for n in node.traverse(head) do
+	    for n in node.traverse(hlist.list) do
                 node.setattribute(n, attribute, color)
 	    end
         end

With that change, it works as expected.

I am mostly clueless about debugging ConTeXt.

From the Lua end, you can run inspect(<some node>) which gives you quite nice output.

From the TeX end, you can try something like

\tracingonline=2
\tracingnodes=2
\shownodedetails=2
\tracingoutput=1
\showboxdepth=\maxdimen
\showboxbreadth=\maxdimen

which should show you the colour of each node.

@vlasakm
Copy link
Contributor

vlasakm commented Aug 27, 2022

Maybe if for all formats we can get the color attribute and a mapping from color to its number, there could be something like this loop instead

"All formats" is a little tricky here though. TeX82 doesn't have any native colour support, so each format handles it very differently: [...]

Sorry for my poor wording, I am familiar with the color situation, what I meant was all attribute based formats, i.e. the three ones we are dealing with in this issue. Those would allow common implementation if each exposed the color attribute and a possibility to map color to its attribute value.

I modified your example from olsak/OpTeX#101 for ConTeXt, but it doesn't work as I expected.

ConTeXt luckily has a higher-level interface for setting node colours. Like most of the ConTeXt Lua interfaces, it is completely undocumented, but I found it by searching through the code. See ddb2258 for an implementation.

Yeah, it's probably better to stick to higher level interfaces. I am not satisfied with the efficiency of optex.set_node_color, but that form is needed for the luaotfload hook anyways, so we can keep it like this even for use in lua-widow-control.

Do you have any ideas?

Just a small typo in your code: [...]

Thanks! I somehow missed that.

With that change, it works as expected.

I am mostly clueless about debugging ConTeXt.

From the Lua end, you can run inspect(<some node>) which gives you quite nice output.

From the TeX end, you can try something like

\tracingonline=2
\tracingnodes=2
\shownodedetails=2
\tracingoutput=1
\showboxdepth=\maxdimen
\showboxbreadth=\maxdimen

which should show you the colour of each node.

Thanks, very handy!

I'll try to get olsak/OpTeX#101 merged as is. Give it a few days please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ConTeXt Issues that only affect ConTeXt/LMTX Minor Bug A bug that doesn't completely break a document Plain TeX/OpTeX Issues that affect Plain TeX or OpTeX
Projects
Status: Done
Development

No branches or pull requests

2 participants