Skip to content

Commit f657137

Browse files
committed
just trying a merge here...
2 parents 0d117ee + d46e1b6 commit f657137

File tree

3 files changed

+81
-74
lines changed

3 files changed

+81
-74
lines changed

app/src/processing/app/syntax/JEditTextArea.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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("&nbsp;");
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())

app/src/processing/app/syntax/TextAreaPainter.java

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import java.awt.event.MouseEvent;
1414
import java.awt.*;
15-
import java.awt.print.*;
1615

1716
import javax.swing.ToolTipManager;
1817
import javax.swing.text.*;
@@ -28,9 +27,6 @@
2827
* @author Slava Pestov
2928
*/
3029
public class TextAreaPainter extends JComponent implements TabExpander {
31-
/** True if inside printing, will handle disabling the highlight */
32-
boolean printing;
33-
3430
/** A specific painter composed by the InputMethod.*/
3531
protected CompositionTextPainter compositionTextPainter;
3632

@@ -507,38 +503,6 @@ public void paint(Graphics gfx) {
507503
}
508504

509505

510-
public Printable getPrintable() {
511-
return new Printable() {
512-
513-
@Override
514-
public int print(Graphics graphics, PageFormat pageFormat,
515-
int pageIndex) throws PrinterException {
516-
int lineHeight = fm.getHeight();
517-
int linesPerPage = (int) (pageFormat.getImageableHeight() / lineHeight);
518-
int lineCount = textArea.getLineCount();
519-
int lastPage = lineCount / linesPerPage;
520-
521-
if (pageIndex > lastPage) {
522-
return NO_SUCH_PAGE;
523-
524-
} else {
525-
Graphics2D g2 = (Graphics2D) graphics;
526-
TokenMarker tokenMarker = textArea.getDocument().getTokenMarker();
527-
int firstLine = pageIndex*linesPerPage;
528-
g2.translate(Math.max(54, pageFormat.getImageableX()),
529-
pageFormat.getImageableY() - firstLine*lineHeight);
530-
printing = true;
531-
for (int line = firstLine; line < firstLine + linesPerPage; line++) {
532-
paintLine(g2, line, 0, tokenMarker);
533-
}
534-
printing = false;
535-
return PAGE_EXISTS;
536-
}
537-
}
538-
};
539-
}
540-
541-
542506
/**
543507
* Marks a line as needing a repaint.
544508
* @param line The line to invalidate
@@ -666,9 +630,7 @@ protected void paintLine(Graphics gfx, int line, int x,
666630
// protected void paintPlainLine(Graphics gfx, int line, Font defaultFont,
667631
// Color defaultColor, int x, int y) {
668632
protected void paintPlainLine(Graphics gfx, int line, int x, int y) {
669-
if (!printing) {
670-
paintHighlight(gfx,line,y);
671-
}
633+
paintHighlight(gfx,line,y);
672634
textArea.getLineText(line, currentLine);
673635

674636
// gfx.setFont(plainFont);
@@ -798,8 +760,7 @@ protected int paintSyntaxLine(Graphics gfx, Segment line, int x, int y,
798760
}
799761

800762

801-
protected void paintHighlight(Graphics gfx, int line, int y) {//, boolean printing) {
802-
// if (!printing) {
763+
protected void paintHighlight(Graphics gfx, int line, int y) {
803764
if (line >= textArea.getSelectionStartLine() &&
804765
line <= textArea.getSelectionStopLine()) {
805766
paintLineHighlight(gfx, line, y);

app/src/processing/app/ui/Editor.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import javax.swing.event.*;
6868
import javax.swing.plaf.basic.*;
6969
import javax.swing.text.*;
70+
import javax.swing.text.html.*;
7071
import javax.swing.undo.*;
7172

7273

@@ -2709,15 +2710,51 @@ public void handlePageSetup() {
27092710
*/
27102711
public void handlePrint() {
27112712
statusNotice(Language.text("editor.status.printing"));
2713+
2714+
StringBuilder html = new StringBuilder("<html><body>");
2715+
for (SketchCode tab : sketch.getCode()) {
2716+
html.append("<b>" + tab.getPrettyName() + "</b><br>");
2717+
html.append(textarea.getTextAsHtml((SyntaxDocument)tab.getDocument()));
2718+
html.append("<br>");
2719+
}
2720+
html.setLength(html.length() - 4); // Don't want last <br>.
2721+
html.append("</body></html>");
2722+
JTextPane jtp = new JTextPane();
2723+
// Needed for good line wrapping; otherwise one very long word breaks
2724+
// wrapping for the whole document.
2725+
jtp.setEditorKit(new HTMLEditorKit() {
2726+
public ViewFactory getViewFactory() {
2727+
return new HTMLFactory() {
2728+
public View create(Element e) {
2729+
View v = super.create(e);
2730+
if (!(v instanceof javax.swing.text.html.ParagraphView))
2731+
return v;
2732+
else
2733+
return new javax.swing.text.html.ParagraphView(e) {
2734+
protected SizeRequirements calculateMinorAxisRequirements(
2735+
int axis, SizeRequirements r) {
2736+
r = super.calculateMinorAxisRequirements(axis, r);
2737+
r.minimum = 1;
2738+
return r;
2739+
}
2740+
};
2741+
}
2742+
};
2743+
}
2744+
});
2745+
jtp.setFont(new Font(Preferences.get("editor.font.family"), Font.PLAIN, 10));
2746+
jtp.setText(html.toString().replace("\n", "<br>") // Not in a <pre>.
2747+
.replaceAll("(?<!&nbsp;)&nbsp;", " ")); // Allow line wrap.
2748+
27122749
//printerJob = null;
27132750
if (printerJob == null) {
27142751
printerJob = PrinterJob.getPrinterJob();
27152752
}
27162753
if (pageFormat != null) {
27172754
//System.out.println("setting page format " + pageFormat);
2718-
printerJob.setPrintable(textarea.getPrintable(), pageFormat);
2755+
printerJob.setPrintable(jtp.getPrintable(null, null), pageFormat);
27192756
} else {
2720-
printerJob.setPrintable(textarea.getPrintable());
2757+
printerJob.setPrintable(jtp.getPrintable(null, null));
27212758
}
27222759
// set the name of the job to the code name
27232760
printerJob.setJobName(sketch.getCurrentCode().getPrettyName());

0 commit comments

Comments
 (0)