Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for copying embedded fonts settings #557

Closed
wants to merge 2 commits into from

Conversation

vorpalvorpal
Copy link

No description provided.

Look for embedded font settings if there are fonts embedded into the current file
Added settings for embedded fonts.
@davidgohel
Copy link
Owner

Thank you.

There is an issue with the code. I would use this:

  # Copy embedded font settings iff fonts are embedded in the current document
  fonts <- list(ttf = FALSE, system = FALSE, subset = FALSE)
  if (dir.exists(file.path(package_dir, "word/fonts"))) {
    node_doc <- read_xml(file.path(package_dir, "word/settings.xml"))
    if (!inherits(xml_child(node_doc, "w:embedTrueTypeFonts"), "xml_missing")) {
      fonts$ttf <- TRUE
    }
    if (!inherits(xml_child(node_doc, "w:embedSystemFonts"), "xml_missing")) {
      fonts$system <- TRUE
    }
    if (!inherits(xml_child(node_doc, "w:saveSubsetFonts"), "xml_missing")) {
      fonts$subset <- TRUE
    }
  }

so that fonts$subset or the other elements can not be NULL

However, when I tested with a docx with a template that embed font, Word complains and says the document has an issue.

read_docx(path = "coco.docx") |> print(target = "cucu.docx")
Capture d’écran 2024-03-12 à 19 36 53

Can you add a few tests to the PR?

coco.docx

Best,
David

@vorpalvorpal
Copy link
Author

Thanks for looking at my PR.

After much searching I found that the problem with coco.docx was in fontTable.xml, however I wasn't able to generate a faulty docx myself, so something weird is going on. When you made coco.docx was that using my PR or the modified code you suggested above?

I accepted word's correction of coco.docx and then ran that corrected file through read_docx(path = "coco.docx") |> print(target = "coco2.docx") and the resulting file was fine. I also tried creating files from scratch and running them through the same but it all came out fine. coco.docx doesn't include w:embedSystemFonts (ie it has the “Do not embed common system fonts” setting in word chosen), but the fonts folder contains additional fonts (presumably system fonts) and the fontTable.xml is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:fonts xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh">
  <w:font w:name="Cambria">
    <w:panose1 w:val="02040503050406030204"/>
    <w:charset w:val="00"/>
    <w:family w:val="roman"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E00002FF" w:usb1="400004FF" w:usb2="00000000" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
    <w:embedRegular r:id="rId1" w:fontKey="{3024D4E8-6DA2-E943-9225-3829064D197B}"/>
    <w:embedBold r:id="rId2" w:fontKey="{269B668A-DB16-1442-AD8E-5A571F935C73}"/>
  </w:font>
  <w:font w:name="Times New Roman">
    <w:panose1 w:val="02020603050405020304"/>
    <w:charset w:val="00"/>
    <w:family w:val="roman"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E0002EFF" w:usb1="C000785B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/>
    <w:embedRegular r:id="rId3" w:fontKey="{0D0DFF31-141B-7E47-B711-E7A60FEABC57}"/>
    <w:embedBold r:id="rId4" w:fontKey="{0D61B6C2-4A1C-0B4B-A2E7-C85E5078CD0A}"/>
    <w:embedBoldItalic r:id="rId5" w:fontKey="{AA69A62C-C5B1-514E-9A72-573BF593198B}"/>
  </w:font>
  <w:font w:name="Open Sans">
    <w:panose1 w:val="020B0604020202020204"/>
    <w:charset w:val="00"/>
    <w:family w:val="auto"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="A000007F" w:usb1="0000000B" w:usb2="00000028" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
    <w:embedRegular r:id="rId6" w:fontKey="{22ED20E3-8F03-A048-A60F-8A910C302729}"/>
    <w:embedBoldItalic r:id="rId7" w:fontKey="{4C9EFDE0-289A-6345-A3B3-EB80EDED12B5}"/>
  </w:font>
  <w:font w:name="Calibri">
    <w:panose1 w:val="020F0502020204030204"/>
    <w:charset w:val="00"/>
    <w:family w:val="swiss"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E0002AFF" w:usb1="C000247B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/>
    <w:embedRegular r:id="rId8" w:fontKey="{15282AC0-9253-AA47-9916-B8E08D79C5FB}"/>
    <w:embedBold r:id="rId9" w:fontKey="{0A0FDEEF-E1FB-BF44-98C8-36A26AA9E00F}"/>
  </w:font>
  <w:font w:name="Lucida Grande">
    <w:panose1 w:val="020B0600040502020204"/>
    <w:charset w:val="00"/>
    <w:family w:val="auto"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E1000AEF" w:usb1="5000A1FF" w:usb2="00000000" w:usb3="00000000" w:csb0="000001BF" w:csb1="00000000"/>
  </w:font>
</w:fonts>

instead of the version in the word corrected file which is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:fonts xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh">
  <w:font w:name="Cambria">
    <w:panose1 w:val="02040503050406030204"/>
    <w:charset w:val="00"/>
    <w:family w:val="roman"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E00006FF" w:usb1="420024FF" w:usb2="02000000" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
    <w:embedRegular r:id="rId1" w:fontKey="{C2E7DFB9-3B20-42EA-924F-B4D1612512BA}"/>
    <w:embedBold r:id="rId2" w:fontKey="{B7BE5861-6FA4-42FA-94BD-88B0F9E27900}"/>
  </w:font>
  <w:font w:name="Times New Roman">
    <w:panose1 w:val="02020603050405020304"/>
    <w:charset w:val="00"/>
    <w:family w:val="roman"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E0002EFF" w:usb1="C000785B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/>
  </w:font>
  <w:font w:name="Open Sans">
    <w:charset w:val="00"/>
    <w:family w:val="swiss"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E00002EF" w:usb1="4000205B" w:usb2="00000028" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
    <w:embedRegular r:id="rId3" w:fontKey="{E156D5F2-6AE7-4D3A-8443-712DA26340DD}"/>
    <w:embedBoldItalic r:id="rId4" w:fontKey="{98D9B174-20D7-4CB5-9D19-1F76585C722B}"/>
  </w:font>
  <w:font w:name="Calibri">
    <w:panose1 w:val="020F0502020204030204"/>
    <w:charset w:val="00"/>
    <w:family w:val="swiss"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E4002EFF" w:usb1="C000247B" w:usb2="00000009" w:usb3="00000000" w:csb0="000001FF" w:csb1="00000000"/>
    <w:embedRegular r:id="rId5" w:fontKey="{40BB2FA1-2400-4E18-B5C5-3302E34DB57A}"/>
    <w:embedBold r:id="rId6" w:fontKey="{A46084A5-459C-4177-B79E-E286D10BEDE9}"/>
  </w:font>
  <w:font w:name="Lucida Grande">
    <w:charset w:val="00"/>
    <w:family w:val="auto"/>
    <w:pitch w:val="variable"/>
    <w:sig w:usb0="E1000AEF" w:usb1="5000A1FF" w:usb2="00000000" w:usb3="00000000" w:csb0="000001BF" w:csb1="00000000"/>
  </w:font>
</w:fonts>

Notice the additional tags for Times New Roman (presumably a system font). I'm not clear how this happened, since I wasn't able to re-create a faulty docx myself.

I've not made tests before, but would be happy to give it a go, if you could give me a hint as to what you want. Is there a way to test if a resultant docx is valid?

@davidgohel
Copy link
Owner

I activated the 'embed font' option in the coco.docx with Word, not with officer. I ran the code with the code of your branch. So something does not work.

You can see also it break the package, the CI/CD results show it:

Capture d’écran 2024-03-14 à 08 51 21

if you could give me a hint as to what you want

Not sure, something that test the new feature you are bringing. You need to test what makes it work. For example, there are XML tags to check, the presence of fonts in a certain format in a directory, etc.

@davidgohel
Copy link
Owner

Let me know if you want to re-open the PR. I am not able to make it work for now. Also I did try to add this feature in the past without success .

@davidgohel davidgohel closed this May 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants