@@ -1618,16 +1618,38 @@ public void copy() {
16181618 * specific to any language or version of the PDE.
16191619 */
16201620 public void copyAsHTML () {
1621- StringBuilder cf = new StringBuilder ("<html><body><pre>\n " );
1621+ HtmlSelection formatted = new HtmlSelection ("<html><body><pre>\n "
1622+ + getTextAsHtml (null ) + "\n </pre></body></html>" );
1623+
1624+ Clipboard clipboard = processing .app .ui .Toolkit .getSystemClipboard ();
1625+ clipboard .setContents (formatted , new ClipboardOwner () {
1626+ public void lostOwnership (Clipboard clipboard , Transferable contents ) {
1627+ // I don't care about ownership
1628+ }
1629+ });
1630+ }
1631+
1632+
1633+ /**
1634+ * Guts of copyAsHTML, minus the pre, body, and html blocks surrounding.
1635+ * @param doc If null, read only the selection if any, and use the active
1636+ * document. Otherwise, the whole of doc is used.
1637+ */
1638+ public String getTextAsHtml (SyntaxDocument doc ) {
1639+ StringBuilder cf = new StringBuilder ();
16221640
16231641 int selStart = getSelectionStart ();
16241642 int selStop = getSelectionStop ();
16251643
16261644 int startLine = getSelectionStartLine ();
16271645 int stopLine = getSelectionStopLine ();
16281646
1647+ if (doc != null ) {
1648+ startLine = 0 ;
1649+ stopLine = doc .getDefaultRootElement ().getElementCount () - 1 ;
1650+ }
16291651 // If no selection, convert all the lines
1630- if (selStart == selStop ) {
1652+ else if (selStart == selStop ) {
16311653 startLine = 0 ;
16321654 stopLine = getLineCount () - 1 ;
16331655 } else {
@@ -1636,66 +1658,54 @@ public void copyAsHTML() {
16361658 stopLine --;
16371659 }
16381660 }
1661+ if (doc == null ) {
1662+ doc = getDocument ();
1663+ }
16391664
16401665 // Read the code line by line
16411666 for (int i = startLine ; i <= stopLine ; i ++) {
1642- emitAsHTML (cf , i );
1667+ emitAsHTML (cf , i , doc );
16431668 }
16441669
1645- cf .append ("\n </pre></body></html>" );
1646-
1647- HtmlSelection formatted = new HtmlSelection (cf .toString ());
1648-
1649- Clipboard clipboard = processing .app .ui .Toolkit .getSystemClipboard ();
1650- clipboard .setContents (formatted , new ClipboardOwner () {
1651- public void lostOwnership (Clipboard clipboard , Transferable contents ) {
1652- // i don't care about ownership
1653- }
1654- });
1670+ return cf .toString ();
16551671 }
16561672
16571673
1658- private void emitAsHTML (StringBuilder cf , int line ) {
1674+ private void emitAsHTML (StringBuilder cf , int line , SyntaxDocument doc ) {
1675+ // Almost static; only needs the painter for a color scheme.
16591676 Segment segment = new Segment ();
1660- getLineText (line , segment );
1677+ try {
1678+ Element element = doc .getDefaultRootElement ().getElement (line );
1679+ int start = element .getStartOffset ();
1680+ int stop = element .getEndOffset ();
1681+ doc .getText (start , stop - start - 1 , segment );
1682+ } catch (BadLocationException e ) { return ; }
16611683
16621684 char [] segmentArray = segment .array ;
16631685 int limit = segment .getEndIndex ();
16641686 int segmentOffset = segment .offset ;
16651687 int segmentCount = segment .count ;
16661688
1667- TokenMarker tokenMarker = getTokenMarker ();
1689+ TokenMarker tokenMarker = doc . getTokenMarker ();
16681690 // If syntax coloring is disabled, do simple translation
16691691 if (tokenMarker == null ) {
16701692 for (int j = 0 ; j < segmentCount ; j ++) {
16711693 char c = segmentArray [j + segmentOffset ];
1672- //cf = cf.append(c);
16731694 appendAsHTML (cf , c );
16741695 }
16751696 } else {
16761697 // If syntax coloring is enabled, we have to do this
16771698 // because tokens can vary in width
1678- Token tokens ;
1679- if ((painter .getCurrentLineIndex () == line ) &&
1680- (painter .getCurrentLineTokens () != null )) {
1681- tokens = painter .getCurrentLineTokens ();
1682-
1683- } else {
1684- painter .setCurrentLineIndex (line );
1685- painter .setCurrentLineTokens (tokenMarker .markTokens (segment , line ));
1686- tokens = painter .getCurrentLineTokens ();
1687- }
1699+ Token tokens = tokenMarker .markTokens (segment , line );
16881700
16891701 int offset = 0 ;
16901702 SyntaxStyle [] styles = painter .getStyles ();
16911703
16921704 for (;;) {
16931705 byte id = tokens .id ;
16941706 if (id == Token .END ) {
1695- char c = segmentArray [segmentOffset + offset ];
16961707 if (segmentOffset + offset < limit ) {
1697- //cf.append(c);
1698- appendAsHTML (cf , c );
1708+ appendAsHTML (cf , segmentArray [segmentOffset + offset ]);
16991709 } else {
17001710 cf .append ('\n' );
17011711 }
@@ -1718,7 +1728,6 @@ private void emitAsHTML(StringBuilder cf, int line) {
17181728 cf .append (" " );
17191729 } else {
17201730 appendAsHTML (cf , c );
1721- //cf.append(c);
17221731 }
17231732 // Place close tags [/]
17241733 if (j == (length - 1 ) && id != Token .NULL && styles [id ].isBold ())
0 commit comments