From 54925f0d088b8bc672d814dbb79d686a38e3e7e8 Mon Sep 17 00:00:00 2001 From: Michael Scharf Date: Fri, 26 Oct 2007 05:13:35 +0000 Subject: [PATCH] FIXED - bug 206328: [regression] Terminal does not draw correctly with proportional font https://bugs.eclipse.org/bugs/show_bug.cgi?id=206328 patch applied --- .../terminal/textcanvas/StyleMap.java | 42 ++++++++++++++++++- .../terminal/textcanvas/TextLineRenderer.java | 19 ++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/StyleMap.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/StyleMap.java index 25f8f4a678b..399c897a0d2 100644 --- a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/StyleMap.java +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/StyleMap.java @@ -33,7 +33,7 @@ public class StyleMap { private static final String BLUE = "blue"; //$NON-NLS-1$ private static final String GREEN = "green"; //$NON-NLS-1$ private static final String RED = "red"; //$NON-NLS-1$ - + private static final String PREFIX = "org.eclipse.tm.internal."; //$NON-NLS-1$ // TODO propagate the name of the font in the FontRegistry String fFontName="terminal.views.view.font.definition"; //$NON-NLS-1$ @@ -42,6 +42,8 @@ public class StyleMap { private Point fCharSize; private Style fDefaultStyle; private boolean fInvertColors; + private boolean fProportional; + private final int[] fOffsets=new int[256]; StyleMap() { addColor(WHITE, 255,255,255); addColor(BLACK, 0,0,0); @@ -138,6 +140,44 @@ public class StyleMap { GC gc = new GC (display); gc.setFont(getFont()); fCharSize = gc.textExtent ("W"); //$NON-NLS-1$ + fProportional=false; + + for (char c = ' '; c <= '~'; c++) { + // consider only the first 128 chars for deciding if a font + // is proportional + if(measureChar(gc, c)) + fProportional=true; + } + // TODO should we also consider the upper 128 chars?? +// for (char c = ' '+128; c <= '~'+128; c++) { +// measureChar(gc, c); +// } + for (int i = 0; i < fOffsets.length; i++) { + fOffsets[i]=(fCharSize.x-fOffsets[i])/2; + } gc.dispose (); } + private boolean measureChar(GC gc, char c) { + boolean proportional=false; + Point ext=gc.textExtent(String.valueOf(c)); + if(ext.x>0 && ext.y>0 && (fCharSize.x!=ext.x || fCharSize.y!=ext.y)) { + proportional=true; + fCharSize.x=Math.max(fCharSize.x, ext.x); + fCharSize.y=Math.max(fCharSize.y, ext.y); + } + fOffsets[c]=ext.x; + return proportional; + } + public boolean isFontProportional() { + return fProportional; + } + /** + * @param c + * @return the offset in x direction to center this character + */ + public int getCharOffset(char c) { + if(c>=fOffsets.length) + return 0; + return fOffsets[c]; + } } diff --git a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextLineRenderer.java b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextLineRenderer.java index 66d7a2f9e7a..cb08db942e7 100644 --- a/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextLineRenderer.java +++ b/terminal/org.eclipse.tm.terminal/src/org/eclipse/tm/internal/terminal/textcanvas/TextLineRenderer.java @@ -116,8 +116,23 @@ public class TextLineRenderer implements ILinelRenderer { } private void drawText(GC gc, int x, int y, int colFirst, int col, String text) { int offset=(col-colFirst)*getCellWidth(); - text=text.replace('\000', ' '); - gc.drawString(text,x+offset,y,false); + if(fStyleMap.isFontProportional()) { + // draw the background + // TODO why does this not work??????? +// gc.fillRectangle(x,y,fStyleMap.getFontWidth()*text.length(),fStyleMap.getFontHeight()); + for (int i = 0; i < text.length(); i++) { + char c=text.charAt(i); + int xx=x+offset+i*fStyleMap.getFontWidth(); + // TODO why do I have to draw the background character by character?????? + gc.fillRectangle(xx,y,fStyleMap.getFontWidth(),fStyleMap.getFontHeight()); + if(c!=' ' && c!='\000') { + gc.drawString(String.valueOf(c),fStyleMap.getCharOffset(c)+xx,y,true); + } + } + } else { + text=text.replace('\000', ' '); + gc.drawString(text,x+offset,y,false); + } } private void setupGC(GC gc, Style style) { Color c=fStyleMap.getForegrondColor(style);