metanorma / metanorma-iso

Metanorma processor for ISO standards
BSD 2-Clause "Simplified" License
14 stars 5 forks source link

(URGENT) LutaML model diagram missing from PDF (exists in Word) #796

Closed ronaldtse closed 2 years ago

ronaldtse commented 2 years ago

PDF missing image:

Screen Shot 2022-09-13 at 12 05 56 PM

Word:

Screen Shot 2022-09-13 at 12 06 33 PM
ronaldtse commented 2 years ago

I wonder if the data-uri-image option is turned off for these auto-generated images?

Intelligent2013 commented 2 years ago

I've tried to generate presentation xml on Windows 10, but the process failed (https://github.com/metanorma/metanorma-iso/issues/797). Then, tried to open Figure 24 .png image on default Win image viewer (Photos): image

Tried to put into empty Word document: image

Tried to generate PDF:

Can't read DPI from image: javax.imageio.IIOException: Can't read input file!

I'll try on Ubuntu.

ronaldtse commented 2 years ago

I wonder if it's related to this https://github.com/metanorma/pngcheck-ruby/issues/17 ?

Images: (XML Line 004393): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
Images: (XML Line 004430): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
Images: (XML Line 004539): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
Images: (XML Line 004543): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
Images: (XML Line 004597): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT

Let me send you a few screenshots to see if you can see them?

Intelligent2013 commented 2 years ago

Actually, there are a few issues:

ISO XSLT adds the current document path (D:\Work\Metanorma\XML\ISO97_Badimage) before src and we get wrong path D:\Work\Metanorma\XML\ISO97_Badimage\images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20220913-7144-s5djb6.png:

<fo:external-graphic width="100%" content-height="scale-to-fit" scaling="uniform" fox:alt-text="Image " src="url(file:D:\Work\Metanorma\XML\ISO97_Badimage\images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20220913-7144-s5djb6.png)"/>

If I change src to the relative path:

<image src="images/lutaml/lutaml20220913-7144-s5djb6.png" 

then FOP informs about The file format is not supported.

Image not available. URI: file:D:\Work\Metanorma\XML\ISO97_Badimage\images/lutaml/lutaml20220913-7144-s5djb6.png. Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for file:D:\Work\Metanorma\XML\ISO97_Badimage\images/lutaml/lutaml20220913-7144-s5djb6.png (See position 396:234)
org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for file:D:\Work\Metanorma\XML\ISO97_Badimage\images/lutaml/lutaml20220913-7144-s5djb6.png
        at org.apache.xmlgraphics.image.loader.ImageManager.preloadImage(ImageManager.java:181)
        at org.apache.xmlgraphics.image.loader.cache.ImageCache.needImageInfo(ImageCache.java:128)
        at org.apache.xmlgraphics.image.loader.ImageManager.getImageInfo(ImageManager.java:123)
        at org.apache.fop.fo.flow.ExternalGraphic.bind(ExternalGraphic.java:81)
        at org.apache.fop.fo.FObj.processNode(FObj.java:131)

On Ubuntu, src contains path without prefix images, but full path again:

<image src="/home/testuser/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20220913-4327-zyjutc.png" 
Intelligent2013 commented 2 years ago

There are differences between generated png in the folder images/lutaml:

Intelligent2013 commented 2 years ago

I.e. it seems the initial issue relates to the wrong image path in src, not broken images. Broken images generated on Windows-platform.

ronaldtse commented 2 years ago

@Intelligent2013 then the Windows PNG issue of The file format is not supported. is a correct warning because the images are broken anyway.

Can I confirm that the wrong image path in src is a problem to be fixed in the XSLT, or is this from Metanorma Ruby?

Intelligent2013 commented 2 years ago

Can I confirm that the wrong image path in src is a problem to be fixed in the XSLT, or is this from Metanorma Ruby?

@ronaldtse no, the problem in the Metanorma Ruby. The image path in src should be relative to the presentation XML (https://github.com/metanorma/metanorma-cli/issues/255).

ronaldtse commented 2 years ago

I believe this issue has been fixed. @Intelligent2013 can you help try again? Thanks!

Intelligent2013 commented 2 years ago

I've tried on Windows:

git clone https://github.com/metanorma/iso-19135
cd iso-19135
bundle _2.3.22_ update
bundle exec metanorma site generate
Images: (XML Line 004956): Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-rvifka.png
Images: (XML Line 005271): Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-nwcdko.png
Images: (XML Line 005392): Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-gkufvh.png
Images: (XML Line 005570): Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-iugdtl.png
Images: (XML Line 004393): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
WARN: [memory buffer] (1052x758, 32-bit RGB+alpha, non-interlaced, 100.0%).
Images: (XML Line 004430): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
WARN: [memory buffer] (1036x726, 32-bit RGB+alpha, non-interlaced, 100.0%).
Images: (XML Line 004539): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
WARN: [memory buffer] (1372x784, 32-bit RGB+alpha, non-interlaced, 100.0%).
Images: (XML Line 004543): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
WARN: [memory buffer] (1334x760, 32-bit RGB+alpha, non-interlaced, 100.0%).
Images: (XML Line 004597): Corrupt PNG image detected: [memory buffer]  illegal (unless recently approved) unknown, public chunk iDOT
WARN: [memory buffer] (1336x778, 32-bit RGB+alpha, non-interlaced, 100.0%).
Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-rvifka.png
Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-nwcdko.png
Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-gkufvh.png
Image not found: images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-14664-iugdtl.png

There is document.err only in the folder site\documents\iso-19135-2021\

cd sources/iso-19135-2021
bundle exec metanorma -t iso -x presentation document.adoc

document.xml.abort generated only and src is wrong:

<figure id="_">
<name>Elements of the Register model</name>
<image src="images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221011-8492-cakden.png" id="_" mimetype="image/png" height="auto" width="auto"/></figure>

Gemfile:

source "https://rubygems.org"

gem "metanorma-cli"
gem "metanorma-iso", git: "https://github.com/metanorma/metanorma-iso"
gem "metanorma-utils", git: "https://github.com/metanorma/metanorma-utils"
gem "metanorma-standoc", git: "https://github.com/metanorma/metanorma-standoc"
gem "isodoc", git: "https://github.com/metanorma/isodoc"
gem "mn-requirements", git: "https://github.com/metanorma/mn-requirements", branch: "main"
gem "sassc"
Intelligent2013 commented 2 years ago

From Gemfile.lock:

    pngcheck (0.3.0-x64-mingw-ucrt)

I'll try to specify right version directly in Gemfile...

Intelligent2013 commented 2 years ago

The version pngcheck (0.3.0-x64-mingw-ucrt) is correct.

Intelligent2013 commented 2 years ago

All .png in the folder iso-19135\sources\iso-19135-2021\images\lutaml are corrupted (2 file have zero-size, another wrong PNG format): image

From http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html:

12.12. PNG file signature
The first eight bytes of a PNG file always contain the following values:

   (decimal)              137  80  78  71  13  10  26  10
   (hexadecimal)           89  50  4e  47  0d  0a  1a  0a

From corrupted PNG file: image

I.e. instead of 0d0a there is just 0a,

Intelligent2013 commented 2 years ago

On Ubuntu the issue fixed: image

Intelligent2013 commented 2 years ago

Preliminary investigation - 2 files have zero-size due missing outfile.close metanorma-plugin-lutaml-0.4.18.1\lib\metanorma\plugin\lutaml\lutaml_diagram_base.rb:

          outfile = Tempfile.new(["lutaml", ".png"])
          outfile.binmode
          outfile.puts(formatter.format(uml_document))

Should be:

          outfile = Tempfile.new(["lutaml", ".png"])
          outfile.binmode
          outfile.puts(formatter.format(uml_document))
          outfile.close
Intelligent2013 commented 2 years ago

The wrong PNG on Windows platform generates here (C:\tools\ruby31\lib\ruby\gems\3.1.0\gems\lutaml-0.7.4\lib\lutaml\layout\graph_viz_engine.rb):

        Open3.popen3("dot -T#{type}") do |stdin, stdout, _stderr, _wait|
          stdin.puts(input)
          stdin.close
          stdout.read

input is string, it contains:

digraph G {
  graph [splines="ortho" pad=0.5 ranksep="1.2.equally" nodesep="1.2.equally"]
  edge [color="gray50"]
  node [shape="box" fontname="Helvetica-bold"]

  Register [
    shape="plain"
    fontname="Helvetica"
    label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
    <TR>
    <TD><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
    <TR><TD ALIGN="CENTER"><B>Register</B></TD></TR>
  </TABLE>
  </TD>
  </TR>

  <TR>
    <TD><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
              <TR><TD ALIGN="LEFT">identifier : Identifier&#91;1..&#93;</TD></TR>
            <TR><TD ALIGN="LEFT">concept_classes : ConceptClass&#91;0..*&#93;</TD></TR>
            <TR><TD ALIGN="LEFT">concepts : Concept&#91;0..*&#93;</TD></TR>
            <TR><TD ALIGN="LEFT">concept_versions : ConceptVersions&#91;0..*&#93;</TD></TR>
            <TR><TD ALIGN="LEFT">register_item_classes : RegisterItemClass&#91;0..*&#93;</TD></TR>
            <TR><TD ALIGN="LEFT">register_items : RegisterItem&#91;0..*&#93;</TD></TR>
  </TABLE>
        </TD>
  </TR>

...

  Identifier -> Register [dir="direct" arrowtail="onormal" arrowhead="vee"]
  ConceptClass -> Register [dir="direct" arrowtail="onormal" arrowhead="vee"]
  Concept -> ConceptClass [dir="direct" arrowtail="onormal" arrowhead="vee"]
  ConceptVersion -> Concept [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItem -> RegisterItemClass [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItemClass -> ConceptClass [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItem -> ConceptVersion [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItem -> ContentSchema [dir="direct" arrowtail="onormal" arrowhead="vee"]
  ContentSchema -> RegisterItemClass [dir="direct" arrowtail="onormal" arrowhead="vee"]
  ContentSchema -> ConceptClass [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItemClass -> Register [dir="direct" arrowtail="onormal" arrowhead="vee"]
  RegisterItem -> Register [dir="direct" arrowtail="onormal" arrowhead="vee"]
}

The command line dot -T#{type} (dot -Tpng) transforms input string into PNG format. I've tried to call it via command line: dot -Tpng < input.txt > output.png and the resulted output.png renders ok in Windows: image

(dot found here: C:\ProgramData\chocolatey\bin\dot.exe)

I think the problem in the code stdout.read. Internally stdout doesn't contain 0d (13) in 5th position:

stdin.puts(input)
stdin.close
captured_stdout = stdout.read
print(captured_stdout.bytes)
[137, 80, 78, 71, 10, 26, 10, 0, 0, 0, 10,

Looks like there is an issue with newline processing. IO class has a binmode feature (found here https://stackoverflow.com/questions/7533909/how-to-write-just-one-0x0a-character-to-stdout-in-ruby-on-windows), https://ruby-doc.org/core-3.1.2/IO.html#method-i-binmode. For this code:

stdout.binmode
stdin.puts(input)
stdin.close
captured_stdout = stdout.read
print(captured_stdout.bytes)

the output string looks correct:

[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13,

but the output png generated via command line lutaml -t png -o . Register.lutaml has redundant 0d before 0a: image

Will investigate further.

Intelligent2013 commented 2 years ago

For command line tool lutaml - in lutaml-0.7.4\lib\lutaml\command_line.rb write mode should be set to binary. I.e. instead of

output_path.open("w+") { |file| file.write(result) }

should be:

output_path.open("wb+") { |file| file.write(result) }

but it doesn't any affect on Metanorma XML generation. So, images in images/lutaml are ok now (after updates, see above).

But again the issue in images prefix occurs:

<image src="images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221017-8256-rqk3vc.png"

Error in document.err:

Images: (XML Line 004956): Image not found: images/D:/Work/Metanorma/repositories/iso-19135.copy6/sources/iso-19135-2021/images/lutaml/lutaml20221017-8412-7d4ql9.png

(total 4 'Image not found' errors)

Intelligent2013 commented 2 years ago

The prefix images/ (from :imagesdir: attribute) added for lutaml images on Windows platform ...\metanorma-utils-8eb32c22a730\lib\utils\image.rb updated for debug information:

def datauri(uri, local_dir = ".")
   warn "uri= `#{uri}` ."

Windows:

uri= `images/concept-preserve-a.png` .
uri= `images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-11292-vbcmss.png`
uri= `images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-11292-hrvm4g.png`
uri= `images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-11292-gaphdd.png`
uri= `images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-11292-4ubls9.png`

Ubuntu (no prefix before lutaml images):

uri= `images/concept-preserve-a.png` .
uri= `/home/user1/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-3543-6gwlrj.png` .
uri= `/home/user1/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-3543-xs2mcu.png` .
uri= `/home/user1/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-3543-962vhy.png` .
uri= `/home/user1/iso-19135/sources/iso-19135-2021/images/lutaml/lutaml20221019-3543-327wza.png` .
Intelligent2013 commented 2 years ago

The issue in asciidoctor-2.0.18\lib\asciidoctor\path_resolver.rb.

This code is working in Windows only:

  def web_path target, start = nil
      ...
    unless start.nil_or_empty? || (web_root? target)
      target, uri_prefix = extract_uri_prefix %(#{start}#{(start.end_with? SLASH) ? '' : SLASH}#{target})
    end

For input parameters

extract_uri_prefix returns images/D:/Work/Metanorma/repositories/iso-19135/sources/iso-19135-2021/imagesABC/lutaml/lutaml20221019-6140-ps61l8.png

On Ubuntu for input parameters:

is doesn't running because target starts with / and web_root returns true.

So, I think that relative path to lutaml images should be used (lutaml/lutaml20221019-2194-836f2x.png) instead of absolute. (Example for simple image target=concept-preserve-a.png)

Intelligent2013 commented 2 years ago

In metanorma-plugin-lutaml-0.4.18.1\lib\metanorma\plugin\lutaml\lutaml_diagram_base.rb:

def generate_file(parent, _reader, uml_document)
   ...
   File.join(result_pathname, filename)
end

should be replaced by:

File.join("lutaml", filename)

PDF generates on Windows ok: image

Will check on Ubuntu.

Intelligent2013 commented 2 years ago

@ronaldtse I've created the issue https://github.com/lutaml/lutaml/issues/39, but I don't have a permissions to commit the changes.

ronaldtse commented 2 years ago

You have access now. Thanks!