Skip to content

Commit f49173e

Browse files
committed
Add RSyntaxTextArea for stream content
This is one of the first steps of integrating RSyntaxTextArea for PDF content stream editing. Tokenization is based on the parsing logic, which was added in the previous commit. Since PDF content streams are, in a general case, not text, but binary data, some workarounds had to be made, as RSyntaxTextArea was designed to work with text. A custom token type and painter was created, so that we could render arbitrary characters not as text, but as hexadecimal representations of the binary data. Additionally, Latin1Filter was made so that we could somewhat trick RSyntaxTextArea to work with binary data. It replaces any characters, which are not representable in Latin-1 (i.e. U+0100 and beyond) with their UTF-8 representation, but with bytes stored in chars. As a result all chars in the document fit in one byte and the backing character array for the document acts as an inflated byte array. This way we can just decode the text with Latin-1 to get the expected output, which can be put directly into PDF. With how RSyntaxTextArea is structured, quite a lot of code from there has to be copied, as inheritance is not "granular" enough to do what we cant. Since the input stream is no longer processed by iText, what you see in the editor pane is the raw data in the stream itself unmodified. This was one of the goals, as before opening a stream for editing would make it pretty much impossible to save it without altering at least whitespace in some way. As of now, there are some regressions. For example: 1. Images are no longer render in the stream pane. For now, they are displayed as text. 2. Since stream is presented as-is, there is no indentation at the moment. This will be added later as an explicit prettifier option. Code folding, static syntax analysis and RSTAUI dialog integrations will be added later.
1 parent 19e92f8 commit f49173e

19 files changed

+2165
-391
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
<itext.version>9.1.0</itext.version>
8282
<jackson.version>2.18.2</jackson.version>
8383
<logback.version>1.5.17</logback.version>
84+
<rsyntaxtextarea.version>3.6.0</rsyntaxtextarea.version>
85+
<rstaui.version>3.3.1</rstaui.version>
8486

8587
<!-- Test dependencies -->
8688
<junit-jupiter-api.version>5.12.0</junit-jupiter-api.version>
@@ -134,6 +136,16 @@
134136
<artifactId>flatlaf</artifactId>
135137
<version>${flatlaf.version}</version>
136138
</dependency>
139+
<dependency>
140+
<groupId>com.fifesoft</groupId>
141+
<artifactId>rsyntaxtextarea</artifactId>
142+
<version>${rsyntaxtextarea.version}</version>
143+
</dependency>
144+
<dependency>
145+
<groupId>com.fifesoft</groupId>
146+
<artifactId>rstaui</artifactId>
147+
<version>${rstaui.version}</version>
148+
</dependency>
137149
<dependency>
138150
<groupId>com.itextpdf</groupId>
139151
<artifactId>pdftest</artifactId>

src/main/java/com/itextpdf/rups/controller/PdfReaderController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This file is part of the iText (R) project.
7373
import com.itextpdf.rups.view.itext.PdfTree;
7474
import com.itextpdf.rups.view.itext.PlainText;
7575
import com.itextpdf.rups.view.itext.StructureTree;
76-
import com.itextpdf.rups.view.itext.SyntaxHighlightedStreamPane;
76+
import com.itextpdf.rups.view.itext.StreamTextEditorPane;
7777
import com.itextpdf.rups.view.itext.XRefTable;
7878
import com.itextpdf.rups.view.itext.treenodes.PdfObjectTreeNode;
7979

@@ -135,7 +135,7 @@ public class PdfReaderController implements IPdfObjectPanelEventListener, IRupsE
135135
/**
136136
* A panel that will show a stream.
137137
*/
138-
protected SyntaxHighlightedStreamPane streamPane;
138+
protected StreamTextEditorPane streamPane;
139139

140140
/**
141141
* The factory producing tree nodes.
@@ -205,7 +205,7 @@ public PdfReaderController(TreeSelectionListener treeSelectionListener,
205205

206206
objectPanel = new PdfObjectPanel();
207207
objectPanel.addEventListener(this);
208-
streamPane = new SyntaxHighlightedStreamPane(this);
208+
streamPane = new StreamTextEditorPane(this);
209209
JScrollPane debug = new JScrollPane(DebugView.getInstance().getTextArea());
210210
editorTabs = new JTabbedPane();
211211
editorTabs.addTab(Language.STREAM.getString(), null, streamPane, Language.STREAM.getString());
@@ -261,7 +261,7 @@ public JTabbedPane getEditorTabs() {
261261
*
262262
* @return a SyntaxHighlightedStreamPane
263263
*/
264-
public SyntaxHighlightedStreamPane getStreamPane() {
264+
public StreamTextEditorPane getStreamPane() {
265265
return streamPane;
266266
}
267267

src/main/java/com/itextpdf/rups/view/Language.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public enum Language {
9595
ERROR_BUILDING_CONTENT_STREAM,
9696
ERROR_CANNOT_CHECK_NULL_FOR_INPUT_STREAM,
9797
ERROR_CANNOT_FIND_FILE,
98+
ERROR_CHARACTER_ENCODING,
9899
ERROR_CLOSING_STREAM,
99100
ERROR_COMPARE_DOCUMENT_CREATION,
100101
ERROR_COMPARED_DOCUMENT_CLOSED,

src/main/java/com/itextpdf/rups/view/contextmenu/InspectObjectAction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.rups.view.Language;
4646
import com.itextpdf.rups.view.icons.FrameIconUtil;
4747
import com.itextpdf.rups.view.itext.PdfTree;
48-
import com.itextpdf.rups.view.itext.SyntaxHighlightedStreamPane;
48+
import com.itextpdf.rups.view.itext.StreamTextEditorPane;
4949
import com.itextpdf.rups.view.itext.treenodes.PdfObjectTreeNode;
5050

5151
import javax.swing.AbstractAction;
@@ -83,10 +83,10 @@ public void actionPerformed(ActionEvent e) {
8383

8484
final PdfObjectTreeNode node =
8585
(PdfObjectTreeNode) ((PdfTree) invoker).getSelectionPath().getLastPathComponent();
86-
final SyntaxHighlightedStreamPane syntaxHighlightedStreamPane = new SyntaxHighlightedStreamPane(null);
86+
final StreamTextEditorPane streamPane = new StreamTextEditorPane(null);
8787

88-
frame.add(syntaxHighlightedStreamPane);
89-
syntaxHighlightedStreamPane.render(node);
88+
frame.add(streamPane);
89+
streamPane.render(node);
9090

9191
final Language dialogCancel = Language.DIALOG_CANCEL;
9292
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)

src/main/java/com/itextpdf/rups/view/contextmenu/SaveToPdfStreamJTextPaneAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.rups.view.contextmenu;
4444

45-
import com.itextpdf.rups.view.itext.SyntaxHighlightedStreamPane;
45+
import com.itextpdf.rups.view.itext.StreamTextEditorPane;
4646

4747
import java.awt.event.ActionEvent;
4848

4949
public class SaveToPdfStreamJTextPaneAction extends AbstractRupsAction {
5050

51-
public SaveToPdfStreamJTextPaneAction(String name, SyntaxHighlightedStreamPane invoker) {
51+
public SaveToPdfStreamJTextPaneAction(String name, StreamTextEditorPane invoker) {
5252
super(name, invoker);
5353
}
5454

5555
public void actionPerformed(ActionEvent event) {
56-
final SyntaxHighlightedStreamPane pane = (SyntaxHighlightedStreamPane) invoker;
56+
final StreamTextEditorPane pane = (StreamTextEditorPane) invoker;
5757
pane.saveToTarget();
5858
}
5959
}

src/main/java/com/itextpdf/rups/view/contextmenu/StreamPanelContextMenu.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.rups.view.contextmenu;
4444

4545
import com.itextpdf.rups.view.Language;
46-
import com.itextpdf.rups.view.itext.SyntaxHighlightedStreamPane;
46+
import com.itextpdf.rups.view.itext.StreamTextEditorPane;
4747

4848
import javax.swing.Action;
49+
import javax.swing.JComponent;
4950
import javax.swing.JMenuItem;
5051
import javax.swing.JPopupMenu;
5152
import javax.swing.JSeparator;
52-
import javax.swing.JTextPane;
5353
import javax.swing.text.DefaultEditorKit;
5454

5555
/**
@@ -73,7 +73,7 @@ public final class StreamPanelContextMenu extends JPopupMenu {
7373
* @param textPane the text pane
7474
* @param controller the controller
7575
*/
76-
public StreamPanelContextMenu(final JTextPane textPane, final SyntaxHighlightedStreamPane controller) {
76+
public StreamPanelContextMenu(final JComponent textPane, final StreamTextEditorPane controller) {
7777
super();
7878

7979
final JMenuItem copyItem = getJMenuItem(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2025 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
APRYSE GROUP. APRYSE GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.rups.view.itext;
44+
45+
import java.awt.Toolkit;
46+
import javax.swing.undo.CannotRedoException;
47+
import javax.swing.undo.CannotUndoException;
48+
import javax.swing.undo.UndoManager;
49+
50+
/**
51+
* A variation of an {@link UndoManager}, which will issue a beep instead of
52+
* throwing {@link CannotUndoException} and {@link CannotRedoException}
53+
* exceptions.
54+
*/
55+
public final class BeepingUndoManager extends UndoManager {
56+
@Override
57+
public void redo() {
58+
try {
59+
super.redo();
60+
} catch (CannotRedoException ignored) {
61+
Toolkit.getDefaultToolkit().beep();
62+
}
63+
}
64+
65+
@Override
66+
public void undo() {
67+
try {
68+
super.undo();
69+
} catch (CannotUndoException ignored) {
70+
Toolkit.getDefaultToolkit().beep();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)