Bug 151693 - Copying text with images on a Mac, e.g. from TextEdit, loses the image when pasting to LO Writer
Summary: Copying text with images on a Mac, e.g. from TextEdit, loses the image when p...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
7.4.2.3 release
Hardware: All macOS (All)
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Paste Format-Filters
  Show dependency treegraph
 
Reported: 2022-10-21 21:34 UTC by Thomas Tempelmann
Modified: 2023-07-26 16:23 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Tempelmann 2022-10-21 21:34:28 UTC
Description:
Basically, LO is not able to decode Apple's "rtfd" format (Apple fails to write Microsoft's RTF format if images are included and instead has come up with their own format).

Steps to Reproduce:
Open TextEdit.app on a Mac, make a new document, place some text and an image into the document, Select All, Copy. Paste into a LO Writer document.

Actual Results:
Only the text appears, while the picture is missing

Expected Results:
should paste text and picture as expected


Reproducible: Always


User Profile Reset: No

Additional Info:
The clipboard contains the rtfd data (type "com.apple.flat-rtfd"), which is a container that contains a plain rtf part and the referenced images as separate items.

LO needs to decode this container in order to extract the text and the image(s).

This can be achieved with the Mac-specific type NSAttributedString, which has the `initwithrtfd:` function. Once the string is constructed, I hope LO has some functions which allows it to insert that into the document.

https://developer.apple.com/documentation/foundation/nsattributedstring/1530987-initwithrtfd?language=objc
Comment 1 Alex Thurgood 2022-10-24 10:34:14 UTC
Confirming the behaviour with

Version: 7.4.1.2 / LibreOffice Community
Build ID: 3c58a8f3a960df8bc8fd77b461821e42c061c5f0
CPU threads: 8; OS: Mac OS X 12.6; UI render: default; VCL: osx
Locale: fr-FR (fr_FR.UTF-8); UI: fr-FR
Calc: threaded


but I'm not certain that this is a bug. 

The Text.app warns the user when inserting the image file that doing so will require to converting to RTFD format, which in turn may lead to a file that isn't supported by other applications. Additionally, it seems that the image isn't stored as a copiable object with the file, rather attached somehow (at the end ?)


Honestly, I don't know how this one should be handled : RFE, copy/paste management, filter bug ?
Comment 2 Thomas Tempelmann 2022-10-24 17:05:52 UTC
As I wrote, being an expert on Mac clipboards, I know very well how to handle this.

The Warning is for Apps not using Apple's frameworks. This is mainly directed at MS Word, which has the same issue. That's not an excuse for LO to not fix this. (BTW, I reported the same issue to the Mac Office team but they seem to ignore it.) LO could do better than Word here.

Here's how to make a Mac app with Xcode that shows that it works, i.e. that the rtfd does indeed include the image data:

```
	// fetch 'rtfd' flavor from Pasteboard
	NSPasteboard *pb = NSPasteboard.generalPasteboard;
	NSData *data = [pb dataForType:NSPasteboardTypeRTFD];
	if (!data) {
		NSBeep();
	} else {
		// put the rtfd data into a NSTextView
		NSAttributedString *richText = [NSAttributedString.alloc initWithRTFD:data documentAttributes:nil];
		[self.textView setRichText:YES];
		[self.textView.textStorage appendAttributedString:richText];
	}
```

The only question is whether LO has a way to parse or otherwise process an NSAttributedString or the data in an NSTextView in order to convert it into its own text layout format.

BTW, I'm in the middle of setting up the build system so that I can build LO myself on a Mac and then look for myself, maybe with some help from the IRC dev channel.
Comment 3 Thomas Tempelmann 2022-10-24 17:30:26 UTC
This SO answer shows how to extract the images from the NSAttributedString:

https://stackoverflow.com/a/29153172/43615

So, maybe the easiest approach would be to extract the images from the rtfd, and then use the plain RTF clipboard flavor and insert the images into that.
Though, unfortunately, the plain RTF does not contain any hint where the missing image is supposed to be, it seems.

Here's an example rich text that contains "AAA\n" + image + "\nBBB\n" from the clipboard on my Mac:

{\rtf1\ansi\ansicpg1252\cocoartf1561\cocoasubrtf610
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0

\f0\fs24 \cf0 AAA\
\
BBB\
}

I do not know enough about RTF to decypher it all. But there's no image reference in there, is it?
Comment 4 Thomas Tempelmann 2022-11-01 09:25:29 UTC
I have tried to install the dev system for LO on my Mac (running Monterey), but I eventually got a build error during `./setup check`, for which I got not help in the IRC dev channel, so I give up trying to help with this. Sigh.
Comment 5 Alex Thurgood 2022-11-01 14:13:24 UTC
(In reply to Thomas Tempelmann from comment #4)
> I have tried to install the dev system for LO on my Mac (running Monterey),
> but I eventually got a build error during `./setup check`, for which I got
> not help in the IRC dev channel, so I give up trying to help with this. Sigh.

Sorry to hear that. I don't know if anyone on irc builds on macOS. I encountered an issue with Ventura's SDK not being recognized and a change was pushed to deal with that, but the build setup fails anyway with a make version error. The build environment on macOS, like everything else LO related, is always one of the last of the main OSes for which LO is built, to be brought up to date. This is because the release and dev builds are based on a generally much older version of the the OS sdk so that the build will run on older versions of macOS.