Skip to content

Commit

Permalink
Insert color literals before certain whatsits
Browse files Browse the repository at this point in the history
Fixes olsak#73 and hopefully also other
potential issues.
  • Loading branch information
vlasakm committed Aug 27, 2021
1 parent 24cad80 commit c51f957
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions optex/base/optex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,17 @@ end, "_tracingmacros")
-- inject PDF literals according to attributes.
--
local node_id = node.id
local node_subtype = node.subtype
local glyph_id = node_id("glyph")
local rule_id = node_id("rule")
local glue_id = node_id("glue")
local hlist_id = node_id("hlist")
local vlist_id = node_id("vlist")
local disc_id = node_id("disc")
local whatsit_id = node_id("whatsit")
local pdfliteral_id = node_subtype("pdf_literal")
local pdfsave_id = node_subtype("pdf_save")
local pdfrestore_id = node_subtype("pdf_restore")
local token_getmacro = token.get_macro

local direct = node.direct
Expand Down Expand Up @@ -517,6 +522,10 @@ local function is_color_needed(head, n, id, subtype) -- returns non-stroke, stro
return true, true
end
return true, false
elseif id == whatsit_id and (subtype == pdfliteral_id
or subtype == pdfsave_id
or subtype == pdfrestore_id) then
return true, true
end
return false, false
end
Expand Down

2 comments on commit c51f957

@olsak
Copy link

@olsak olsak commented on c51f957 Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, describe more, what it does. I know that pdfliterals can be not only color settings. What does the attribute-color management with such pdfliterals?

@vlasakm
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code was optimistic: PDF literals and PDF stores and restores don't interfere with colors, and if they do, the user knows what they are doing.

This change makes the code pessimistic: every PDF literal and PDF (re)store messes with colors, so change the color before them to reflect the current settings.

Example:

\Blue\pdfliteral{0 0 1 rg}A

Before processing:

Whatsit{literal: 0 0 1 rg} (colorattr=blue)
Glyph{A} (colorattr=blue)

Old code produced:

Whatsit{literal: 0 0 1 rg} (colorattr=blue)
Whatsit{literal: 1 1 0 0 k} () % <- this is new
Glyph{A} (colorattr=blue)

New code produces:

Whatsit{literal: 1 1 0 0 k} () % <- this is new
Whatsit{literal: 0 0 1 rg} (colorattr=blue)
Glyph{A} (colorattr=blue)

So the code still expects that there noone else changes colors (which works with TikZ, since its settings are local), but now reflects the current color also before whatsit nodes.

The TikZ problem:

(implicit colorattr=black on start of page)
Whatsit{literal: q} ()
Whatsit{literal: 0 g 0 G} ()
Whatsit{literal: 1 0 0 rg} ()
Glyph{t} ()
Glyph{e} ()
Glyph{s} ()
Glyph{t} ()
Whatsit{literal: Q} ()

Old code ("whatsits don't need color, insert color only before first glyph"):

(implicit colorattr=black on start of page)
Whatsit{literal: q} ()
Whatsit{literal: 0 g 0 G} ()
Whatsit{literal: 1 0 0 rg} ()
Whatsit{literal: 0  g} () % <- this is new
Glyph{t} ()
Glyph{e} ()
Glyph{s} ()
Glyph{t} ()
Whatsit{literal: Q} ()

After this commit ("whatsits also need color, but we still think everything is the implicit black, there is no red setting"):

(implicit colorattr=black on start of page)
Whatsit{literal: 0  g} () % <- this is new
Whatsit{literal: q} ()
Whatsit{literal: 0 g 0 G} ()
Whatsit{literal: 1 0 0 rg} ()
Glyph{t} ()
Glyph{e} ()
Glyph{s} ()
Glyph{t} ()
Whatsit{literal: Q} ()

Non color changing literals are not treated specially. As other literals and (re)stores they just require an injection of a color changing whatsit if current color has not been yet injected.

Please sign in to comment.