Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Multi-colored text cells #160

Open
tterimaa opened this issue Jan 9, 2024 · 5 comments
Open

Multi-colored text cells #160

tterimaa opened this issue Jan 9, 2024 · 5 comments

Comments

@tterimaa
Copy link

tterimaa commented Jan 9, 2024

Hi,

I have a use-case where I would need to write text cell that has different colors. For example a cell that contains a text 'foo bar' where 'foo' would be green and 'bar' would be red.

I assume this is not currently supported. It also seems a little bit difficult task to do with a custom drawer, because instead of a string I would need to deal with a list of strings, each of which have different colors.

I think this would either require a breaking change to TextCell API, or alternatively introduction of a new cell type e.g. MultiColoredTextCell. The latter would probably result in a lot of duplication in the code.

Is this a feature that has been requested previously, and do you think this type of a change could be accepted into the library?

@vandeseer
Copy link
Owner

Hey @tterimaa, good question!

This is not planned and also hasn't been requested so far, but my gut feeling is, that it shouldn't be too hard to implement.

I might have a look the next days (if I find some time).
Could you provide me with an example of that color change using only plain PDFBox code? That would make it easier to integrate it ...

Best,
Stefan

@tterimaa
Copy link
Author

I've tried to come up with a solution, here's an example of what I've been trying to do:

This code is from DrawingUtil, to illustrate how multi-colored text would be drawn.


    public static void drawText(PDPageContentStream contentStream, PositionedStyledText styledText) throws IOException {
        contentStream.beginText();
        contentStream.setFont(styledText.getFont(), styledText.getFontSize());
        contentStream.newLineAtOffset(styledText.getX(), styledText.getY());
        for (final Text t: styledText.getText()) {
            contentStream.setNonStrokingColor(t.getColor());
            contentStream.showText(t.getText());
        }
        contentStream.endText();
        contentStream.setCharacterSpacing(0);
    }

So instead of just a String, PositionedStyledText::getText would return a list of Text objects, each of which could have their own color. In default case there would be only one Text, with some default color.

I have came up with a solution, which seems to work in some basic use cases, but I haven't done much testing yet. Would you like me to open a PR, so you could see if this approach makes sense to you?

tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 19, 2024
@tterimaa
Copy link
Author

tterimaa commented Jan 19, 2024

Hi @vandeseer !

I opened a pull request for you to check about this feature: #161

In a summary, it adds a '.coloredText' method to text cell builder, so the users could text with multiple different colors inside a cell.

Please let me know if you think this approach is suitable and if any changes are needed. The most difficult aspect was to get the line wrappings to work correctly. Also, I couldn't find out how to extend TextCellBuilder and VerticalTextCellBuilder with Lombok so I had to de-lombok these classes.

Best regards,
Tarmo

tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 22, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 22, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 22, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 23, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 24, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 24, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 24, 2024
tterimaa added a commit to tterimaa/easytable that referenced this issue Jan 24, 2024
@vandeseer
Copy link
Owner

Hi @tterimaa,

thanks a lot for your PR! Sorry, that I didn't answer earlier. I also only had a brief look – I'm simply lacking time currently. I am even not using this project myself anymore 😉

Having said all that: I'm a bit inclined towards having a new cell type, to be honest. I'm willing to accept a PR which is more or less entirely decoupled from the current code and adds on top. Since I'm trying to more or less only keep the code up to date with dependencies, I don't want maintain something I didn't introduce myself ...

Hope you understand my motivation :)

Thanks again and best regards,
Stefan

@tterimaa
Copy link
Author

tterimaa commented Feb 5, 2024

Thank you for your response,

Yes, I completely understand your point. I will see if it would be easy enough to decouple the new feature from the old code. It's probably mostly simple, but some utility methods that are currently shared among all cell types need to be separate for this new one. Maybe a factory of some sort could work in those situations. If you have any ideas let me know.

I might have some time to work with this during this month, I will get back to you then. Would be great to have the dependency updates.

Best regards,
Tarmo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants