mupdf.js icon indicating copy to clipboard operation
mupdf.js copied to clipboard

Is it possible to extract color of text segment?

Open luckymore opened this issue 1 year ago • 8 comments

just like:

{
  bbox,
  text,
  font,
  // add color here
  color,
}

luckymore avatar Dec 09 '24 07:12 luckymore

Not right now, I think we would need to add a color parameter to the StructuredText walk method (https://mupdfjs.readthedocs.io/en/latest/classes/StructuredText.html#walk). I think this may be possible to do, but I will refer to @ccxvii for feasibility here.

jamie-lemon avatar Dec 09 '24 12:12 jamie-lemon

awe!

  1. Actually I have completed a color Structured version based on pdf.js. But there are some issues with missing colors, invisible characters, textItem not in the same order as the command, not sure if you will occur the same problem about it, it's really a difficult job 🤣

  2. then I have another question about text bolding that I hope you can answer. The whole line is bolded

const document = Document.openDocument(arrayBuffer, './AlphaZero-like tree-search canuide LLM decoding and training 5.pdf')
const page = document.loadPage(0)

console.log('doc', JSON.parse(page.toStructuredText().asJSON()))

image

luckymore avatar Dec 10 '24 02:12 luckymore

Hmmmm, this is wrong, it would be better obviously if the text object had inline styling markup - we should look further into this. What happens if you do page.toStructuredText().asHTML() do you get the kind of markup around the text that you might expect?

jamie-lemon avatar Dec 10 '24 12:12 jamie-lemon

Yes, asHTML() get the correct style, but I need the data in JSON format more than HTML

<p style="top:538.2pt;left:307.1pt;line-height:10.1pt">
  <b><span style="font-family:NimbusRomNo9L,serif;font-size:10.1pt">Task Setups</span></b>
  <span style="font-family:NimbusRomNo9L,serif;font-size:10.1pt"> For a given MDP, the nature of the search</span>
</p>

luckymore avatar Dec 11 '24 02:12 luckymore

I found another options to solve this:

page.toStructuredText('preserve-spans').asJSON()

luckymore avatar Dec 20 '24 10:12 luckymore

Sure - but I still think that we need to add something to the API to return the font color info.

jamie-lemon avatar Dec 20 '24 13:12 jamie-lemon

Yes, that'll be great! lft 👀

luckymore avatar Dec 25 '24 08:12 luckymore

@jamie-lemon You should handle color in onChar query for text walker because inline can be different color for each char. I collecting different spans follow to font + color props.

https://github.com/ArtifexSoftware/mupdf.js/blob/master/src/mupdf.ts#L1402

eXponenta avatar May 15 '25 09:05 eXponenta