@@ -258,11 +258,6 @@ public final TextAreaPainter getPainter() {
258258 }
259259
260260
261- public final Printable getPrintable () {
262- return painter .getPrintable ();
263- }
264-
265-
266261 /**
267262 * Returns the input handler.
268263 */
@@ -1724,16 +1719,38 @@ public void copy() {
17241719 * specific to any language or version of the PDE.
17251720 */
17261721 public void copyAsHTML () {
1727- StringBuilder cf = new StringBuilder ("<html><body><pre>\n " );
1722+ HtmlSelection formatted = new HtmlSelection ("<html><body><pre>\n "
1723+ + getTextAsHtml (null ) + "\n </pre></body></html>" );
1724+
1725+ Clipboard clipboard = processing .app .ui .Toolkit .getSystemClipboard ();
1726+ clipboard .setContents (formatted , new ClipboardOwner () {
1727+ public void lostOwnership (Clipboard clipboard , Transferable contents ) {
1728+ // I don't care about ownership
1729+ }
1730+ });
1731+ }
1732+
1733+
1734+ /**
1735+ * Guts of copyAsHTML, minus the pre, body, and html blocks surrounding.
1736+ * @param doc If null, read only the selection if any, and use the active
1737+ * document. Otherwise, the whole of doc is used.
1738+ */
1739+ public String getTextAsHtml (SyntaxDocument doc ) {
1740+ StringBuilder cf = new StringBuilder ();
17281741
17291742 int selStart = getSelectionStart ();
17301743 int selStop = getSelectionStop ();
17311744
17321745 int startLine = getSelectionStartLine ();
17331746 int stopLine = getSelectionStopLine ();
17341747
1748+ if (doc != null ) {
1749+ startLine = 0 ;
1750+ stopLine = doc .getDefaultRootElement ().getElementCount () - 1 ;
1751+ }
17351752 // If no selection, convert all the lines
1736- if (selStart == selStop ) {
1753+ else if (selStart == selStop ) {
17371754 startLine = 0 ;
17381755 stopLine = getLineCount () - 1 ;
17391756 } else {
@@ -1742,66 +1759,54 @@ public void copyAsHTML() {
17421759 stopLine --;
17431760 }
17441761 }
1762+ if (doc == null ) {
1763+ doc = getDocument ();
1764+ }
17451765
17461766 // Read the code line by line
17471767 for (int i = startLine ; i <= stopLine ; i ++) {
1748- emitAsHTML (cf , i );
1768+ emitAsHTML (cf , i , doc );
17491769 }
17501770
1751- cf .append ("\n </pre></body></html>" );
1752-
1753- HtmlSelection formatted = new HtmlSelection (cf .toString ());
1754-
1755- Clipboard clipboard = processing .app .ui .Toolkit .getSystemClipboard ();
1756- clipboard .setContents (formatted , new ClipboardOwner () {
1757- public void lostOwnership (Clipboard clipboard , Transferable contents ) {
1758- // i don't care about ownership
1759- }
1760- });
1771+ return cf .toString ();
17611772 }
17621773
17631774
1764- private void emitAsHTML (StringBuilder cf , int line ) {
1775+ private void emitAsHTML (StringBuilder cf , int line , SyntaxDocument doc ) {
1776+ // Almost static; only needs the painter for a color scheme.
17651777 Segment segment = new Segment ();
1766- getLineText (line , segment );
1778+ try {
1779+ Element element = doc .getDefaultRootElement ().getElement (line );
1780+ int start = element .getStartOffset ();
1781+ int stop = element .getEndOffset ();
1782+ doc .getText (start , stop - start - 1 , segment );
1783+ } catch (BadLocationException e ) { return ; }
17671784
17681785 char [] segmentArray = segment .array ;
17691786 int limit = segment .getEndIndex ();
17701787 int segmentOffset = segment .offset ;
17711788 int segmentCount = segment .count ;
17721789
1773- TokenMarker tokenMarker = getTokenMarker ();
1790+ TokenMarker tokenMarker = doc . getTokenMarker ();
17741791 // If syntax coloring is disabled, do simple translation
17751792 if (tokenMarker == null ) {
17761793 for (int j = 0 ; j < segmentCount ; j ++) {
17771794 char c = segmentArray [j + segmentOffset ];
1778- //cf = cf.append(c);
17791795 appendAsHTML (cf , c );
17801796 }
17811797 } else {
17821798 // If syntax coloring is enabled, we have to do this
17831799 // because tokens can vary in width
1784- Token tokens ;
1785- if ((painter .getCurrentLineIndex () == line ) &&
1786- (painter .getCurrentLineTokens () != null )) {
1787- tokens = painter .getCurrentLineTokens ();
1788-
1789- } else {
1790- painter .setCurrentLineIndex (line );
1791- painter .setCurrentLineTokens (tokenMarker .markTokens (segment , line ));
1792- tokens = painter .getCurrentLineTokens ();
1793- }
1800+ Token tokens = tokenMarker .markTokens (segment , line );
17941801
17951802 int offset = 0 ;
17961803 SyntaxStyle [] styles = painter .getStyles ();
17971804
17981805 for (;;) {
17991806 byte id = tokens .id ;
18001807 if (id == Token .END ) {
1801- char c = segmentArray [segmentOffset + offset ];
18021808 if (segmentOffset + offset < limit ) {
1803- //cf.append(c);
1804- appendAsHTML (cf , c );
1809+ appendAsHTML (cf , segmentArray [segmentOffset + offset ]);
18051810 } else {
18061811 cf .append ('\n' );
18071812 }
@@ -1824,7 +1829,6 @@ private void emitAsHTML(StringBuilder cf, int line) {
18241829 cf .append (" " );
18251830 } else {
18261831 appendAsHTML (cf , c );
1827- //cf.append(c);
18281832 }
18291833 // Place close tags [/]
18301834 if (j == (length - 1 ) && id != Token .NULL && styles [id ].isBold ())
0 commit comments