Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 301249
Collapse All | Expand All

(-)src/org/eclipse/swt/internal/graphics/TextSizeDeterminationFacadeImpl.java (-4 / +2 lines)
Lines 16-22 Link Here
16
import org.eclipse.rwt.internal.lifecycle.CommonPatterns;
16
import org.eclipse.rwt.internal.lifecycle.CommonPatterns;
17
import org.eclipse.rwt.lifecycle.*;
17
import org.eclipse.rwt.lifecycle.*;
18
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.graphics.Font;
20
import org.eclipse.swt.graphics.FontData;
19
import org.eclipse.swt.graphics.FontData;
21
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
20
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
22
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
21
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
Lines 76-82 Link Here
76
        itemString = CommonPatterns.escapeLeadingTrailingSpaces( itemString );
75
        itemString = CommonPatterns.escapeLeadingTrailingSpaces( itemString );
77
        param.append( itemString );
76
        param.append( itemString );
78
        param.append( "\", " );
77
        param.append( "\", " );
79
        param.append( createFontParam( item.getFont() ) );
78
        param.append( createFontParam( item.getFontData() ) );
80
        param.append( ", " );
79
        param.append( ", " );
81
        param.append( item.getWrapWidth() );
80
        param.append( item.getWrapWidth() );
82
        param.append( " ]" );
81
        param.append( " ]" );
Lines 113-121 Link Here
113
    return requests;
112
    return requests;
114
  }
113
  }
115
114
116
  public String createFontParamInternal( final Font font ) {
115
  public String createFontParamInternal( final FontData fontData ) {
117
    StringBuffer result = new StringBuffer();
116
    StringBuffer result = new StringBuffer();
118
    FontData fontData = font.getFontData()[ 0 ];
119
    String[] names = WidgetLCAUtil.parseFontName( fontData.getName() );
117
    String[] names = WidgetLCAUtil.parseFontName( fontData.getName() );
120
    result.append( "[ " );
118
    result.append( "[ " );
121
    for( int i = 0; i < names.length; i++ ) {
119
    for( int i = 0; i < names.length; i++ ) {
(-)src/org/eclipse/swt/graphics/Font.java (-3 / +1 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2002, 2009 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2002, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 157-167 Link Here
157
   *
157
   *
158
   * @return an array of font data objects describing the receiver
158
   * @return an array of font data objects describing the receiver
159
   *
159
   *
160
   * <!--
161
   * @exception SWTException <ul>
160
   * @exception SWTException <ul>
162
   *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
161
   *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
163
   * </ul>
162
   * </ul>
164
   * -->
165
   */
163
   */
166
  public FontData[] getFontData() {
164
  public FontData[] getFontData() {
167
    if( isDisposed() ) {
165
    if( isDisposed() ) {
(-)src/org/eclipse/swt/internal/graphics/TextSizeDataBase.java (-10 / +10 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
Lines 18-24 Link Here
18
19
19
final class TextSizeDataBase {
20
final class TextSizeDataBase {
20
21
21
  public static Point lookup( final Font font,
22
  public static Point lookup( final FontData font,
22
                              final String string,
23
                              final String string,
23
                              final int wrapWidth )
24
                              final int wrapWidth )
24
  {
25
  {
Lines 32-50 Link Here
32
    return result;
33
    return result;
33
  }
34
  }
34
35
35
  public static void store( final Font font,
36
  public static void store( final FontData fontData,
36
                            final String string,
37
                            final String string,
37
                            final int wrapWidth,
38
                            final int wrapWidth,
38
                            final Point calculatedTextSize )
39
                            final Point calculatedTextSize )
39
  {
40
  {
40
    if( !TextSizeProbeStore.getInstance().containsProbeResult( font ) ) {
41
    if( !TextSizeProbeStore.getInstance().containsProbeResult( fontData ) ) {
41
      String txt = "Font ''{0}'' not probed yet.";
42
      String txt = "Font ''{0}'' not probed yet.";
42
      Object[] args = new Object[] { font.toString() };
43
      Object[] args = new Object[] { fontData.toString() };
43
      String msg = MessageFormat.format( txt, args );
44
      String msg = MessageFormat.format( txt, args );
44
      throw new IllegalStateException( msg );
45
      throw new IllegalStateException( msg );
45
    }
46
    }
46
    ITextSizeStorage registry = TextSizeStorageRegistry.obtain();
47
    ITextSizeStorage registry = TextSizeStorageRegistry.obtain();
47
    Integer key = getKey( font, string, wrapWidth );
48
    Integer key = getKey( fontData, string, wrapWidth );
48
    registry.storeTextSize( key, calculatedTextSize );
49
    registry.storeTextSize( key, calculatedTextSize );
49
  }
50
  }
50
51
Lines 58-76 Link Here
58
59
59
60
60
  // for test purposes only
61
  // for test purposes only
61
  static Integer getKey( final Font font,
62
  static Integer getKey( final FontData fontData,
62
                         final String string,
63
                         final String string,
63
                         final int wrapWidth )
64
                         final int wrapWidth )
64
  {
65
  {
65
    TextSizeProbeStore instance = TextSizeProbeStore.getInstance();
66
    TextSizeProbeStore instance = TextSizeProbeStore.getInstance();
66
    IProbeResult probeResult = instance.getProbeResult( font );
67
    IProbeResult probeResult = instance.getProbeResult( fontData );
67
    String probeText = probeResult.getProbe().getString();
68
    String probeText = probeResult.getProbe().getString();
68
    Point probeSize = probeResult.getSize();
69
    Point probeSize = probeResult.getSize();
69
    FontData probeFontData = font.getFontData()[ 0 ];
70
    int hashCode = 1;
70
    int hashCode = 1;
71
    hashCode = 31 * hashCode + probeText.hashCode();
71
    hashCode = 31 * hashCode + probeText.hashCode();
72
    hashCode = 31 * hashCode + probeSize.hashCode();
72
    hashCode = 31 * hashCode + probeSize.hashCode();
73
    hashCode = 31 * hashCode + probeFontData.hashCode();
73
    hashCode = 31 * hashCode + fontData.hashCode();
74
    hashCode = 31 * hashCode + string.hashCode();
74
    hashCode = 31 * hashCode + string.hashCode();
75
    hashCode = 31 * hashCode + wrapWidth;
75
    hashCode = 31 * hashCode + wrapWidth;
76
    return new Integer( hashCode );
76
    return new Integer( hashCode );
(-)src/org/eclipse/swt/internal/graphics/TextSizeProbeStore.java (-27 / +25 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2009 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 44-50 Link Here
44
  
44
  
45
  
45
  
46
  public interface IProbe {
46
  public interface IProbe {
47
    Font getFont();
47
    FontData getFontData();
48
    String getString();
48
    String getString();
49
    String getJSProbeParam();
49
    String getJSProbeParam();
50
  }
50
  }
Lines 58-74 Link Here
58
  private static final class ProbeImpl implements IProbe {
58
  private static final class ProbeImpl implements IProbe {
59
59
60
    private final String probeText;
60
    private final String probeText;
61
    private final Font font;
61
    private final FontData fontData;
62
    private String jsProbeParam;
62
    private String jsProbeParam;
63
63
64
    private ProbeImpl( final String probeText, final Font font ) {
64
    private ProbeImpl( final String probeText, final FontData fontData ) {
65
      this.probeText = probeText;
65
      this.probeText = probeText;
66
      this.font = font;
66
      this.fontData = fontData;
67
      this.jsProbeParam = createProbeParam( this );
67
      this.jsProbeParam = createProbeParam( this );
68
    }
68
    }
69
69
70
    public Font getFont() {
70
    public FontData getFontData() {
71
      return font;
71
      return fontData;
72
    }
72
    }
73
73
74
    public String getString() {
74
    public String getString() {
Lines 91-107 Link Here
91
    return ( TextSizeProbeStore )getInstance( TextSizeProbeStore.class );
91
    return ( TextSizeProbeStore )getInstance( TextSizeProbeStore.class );
92
  }
92
  }
93
  
93
  
94
  IProbeResult getProbeResult( final Font font ) {
94
  IProbeResult getProbeResult( final FontData fontData ) {
95
    FontData fontData = font.getFontData()[ 0 ];
96
    return ( IProbeResult )probeResults.get( fontData );
95
    return ( IProbeResult )probeResults.get( fontData );
97
  }
96
  }
98
  
97
  
99
  boolean containsProbeResult( final Font font ) {
98
  boolean containsProbeResult( final FontData fontData ) {
100
    return getProbeResult( font ) != null;
99
    return getProbeResult( fontData ) != null;
101
  }
100
  }
102
  
101
  
103
  IProbeResult createProbeResult( final IProbe probe, final Point size ) {
102
  IProbeResult createProbeResult( final IProbe probe, final Point size ) {
104
    FontData fontData = probe.getFont().getFontData()[ 0 ];
103
    FontData fontData = probe.getFontData();
105
    IProbeResult result = new IProbeResult() {
104
    IProbeResult result = new IProbeResult() {
106
      private float avgCharWidth;
105
      private float avgCharWidth;
107
      public IProbe getProbe() {
106
      public IProbe getProbe() {
Lines 134-140 Link Here
134
    IProbe[] result;
133
    IProbe[] result;
135
    synchronized( probes ) {
134
    synchronized( probes ) {
136
      if( probes.isEmpty() ) {
135
      if( probes.isEmpty() ) {
137
        Font[] fontList = TextSizeStorageRegistry.obtain().getFontList();
136
        FontData[] fontList = TextSizeStorageRegistry.obtain().getFontList();
138
        for( int i = 0; i < fontList.length; i++ ) {
137
        for( int i = 0; i < fontList.length; i++ ) {
139
          createProbe( fontList[ i ], getProbeString( fontList[ i ] ) );
138
          createProbe( fontList[ i ], getProbeString( fontList[ i ] ) );
140
        }
139
        }
Lines 145-175 Link Here
145
    return result;
144
    return result;
146
  }
145
  }
147
146
148
  private static String getProbeString( final Font font ) {
147
  private static String getProbeString( final FontData fontData ) {
149
    // TODO [fappel]: probe string determination different from default
148
    // TODO [fappel]: probe string determination different from default
150
    return DEFAULT_PROBE;
149
    return DEFAULT_PROBE;
151
  }
150
  }
152
  
151
  
153
  static IProbe getProbe( final Font font ) {
152
  static IProbe getProbe( final FontData font ) {
154
    FontData fontData = font.getFontData()[ 0 ];
155
    IProbe result;
153
    IProbe result;
156
    synchronized( probes ) {
154
    synchronized( probes ) {
157
      result = ( IProbe )probes.get( fontData );
155
      result = ( IProbe )probes.get( font );
158
    }
156
    }
159
    return result;
157
    return result;
160
  }
158
  }
161
  
159
  
162
  static boolean containsProbe( final Font font ) {
160
  static boolean containsProbe( final FontData fontData ) {
163
    return getProbe( font ) != null;
161
    return getProbe( fontData ) != null;
164
  }
162
  }
165
  
163
  
166
  static IProbe createProbe( final Font font, final String probeText ) {
164
  static IProbe createProbe( final FontData fontData, final String probeText ) {
167
    IProbe result = new ProbeImpl( probeText, font );
165
    IProbe result = new ProbeImpl( probeText, fontData );
168
    FontData fontData = font.getFontData()[ 0 ];
169
    synchronized( probes ) {
166
    synchronized( probes ) {
170
      probes.put( fontData, result );
167
      probes.put( fontData, result );
171
    }
168
    }
172
    TextSizeStorageRegistry.obtain().storeFont( font );
169
    TextSizeStorageRegistry.obtain().storeFont( fontData );
173
    return result;
170
    return result;
174
  }
171
  }
175
172
Lines 183-192 Link Here
183
    }
180
    }
184
  }
181
  }
185
182
186
  static void addProbeRequest( final Font font ) {
183
  static void addProbeRequest( final FontData font ) {
187
    IProbe probe = getProbe( font );
184
    IProbe probe = getProbe( font );
188
    if( probe == null ) {
185
    if( probe == null ) {
189
      probe = createProbe( font, getProbeString( font ) );
186
      FontData fontData = font;
187
      probe = createProbe( fontData, getProbeString( fontData ) );
190
    }
188
    }
191
    getProbeRequestsInternal().add( probe );
189
    getProbeRequestsInternal().add( probe );
192
  }
190
  }
Lines 207-219 Link Here
207
    final Display display = Display.getCurrent();
205
    final Display display = Display.getCurrent();
208
    if( display != null ) {
206
    if( display != null ) {
209
      result.append( "[ " );
207
      result.append( "[ " );
210
      result.append( probe.getFont().hashCode() );
208
      result.append( probe.getFontData().hashCode() );
211
      result.append( ", " );
209
      result.append( ", " );
212
      result.append( "\"" );
210
      result.append( "\"" );
213
      result.append( probe.getString() );
211
      result.append( probe.getString() );
214
      result.append( "\", " );
212
      result.append( "\", " );
215
      result.append(
213
      result.append(
216
        TextSizeDeterminationFacade.createFontParam( probe.getFont() ) );
214
        TextSizeDeterminationFacade.createFontParam( probe.getFontData() ) );
217
      result.append( " ]" );
215
      result.append( " ]" );
218
    }
216
    }
219
    return result.toString();
217
    return result.toString();
(-)src/org/eclipse/swt/internal/graphics/TextSizeEstimation.java (-8 / +9 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2008 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2006, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-21 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
13
import org.eclipse.rwt.internal.lifecycle.CommonPatterns;
14
import org.eclipse.rwt.internal.lifecycle.CommonPatterns;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.graphics.Font;
16
import org.eclipse.swt.graphics.*;
16
import org.eclipse.swt.graphics.Point;
17
17
18
class TextSizeEstimation {
18
final class TextSizeEstimation {
19
19
20
  /**
20
  /**
21
   * Estimates the size of a given text. Line breaks are not respected.
21
   * Estimates the size of a given text. Line breaks are not respected.
Lines 99-112 Link Here
99
   */
99
   */
100
  static float getAvgCharWidth( final Font font ) {
100
  static float getAvgCharWidth( final Font font ) {
101
    float result;
101
    float result;
102
    FontData fontData = font.getFontData()[ 0 ];
102
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
103
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
103
    if( probeStore.containsProbeResult( font ) ) {
104
    if( probeStore.containsProbeResult( fontData ) ) {
104
      // we can improve char width estimations in case that we already have the
105
      // we can improve char width estimations in case that we already have the
105
      // specified font probed.
106
      // specified font probed.
106
      result = probeStore.getProbeResult( font ).getAvgCharWidth();
107
      result = probeStore.getProbeResult( fontData ).getAvgCharWidth();
107
    } else {
108
    } else {
108
      result = font.getFontData()[ 0 ].getHeight() * 0.48f;
109
      result = fontData.getHeight() * 0.48f;
109
      if( ( font.getFontData()[ 0 ].getStyle() & SWT.BOLD ) != 0 ) {
110
      if( ( fontData.getStyle() & SWT.BOLD ) != 0 ) {
110
        result *= 1.45;
111
        result *= 1.45;
111
      }
112
      }
112
    }
113
    }
(-)src/org/eclipse/swt/internal/graphics/TextSizeDeterminationHandler.java (-3 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2009 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 170-176 Link Here
170
    HttpServletRequest request = ContextProvider.getRequest();
170
    HttpServletRequest request = ContextProvider.getRequest();
171
    for( int i = 0; hasProbes && i < probes.length; i++ ) {
171
    for( int i = 0; hasProbes && i < probes.length; i++ ) {
172
      IProbe probe = probes[ i ];
172
      IProbe probe = probes[ i ];
173
      String name = String.valueOf( probe.getFont().hashCode() );
173
      String name = String.valueOf( probe.getFontData().hashCode() );
174
      String value = request.getParameter( name );
174
      String value = request.getParameter( name );
175
      if( value != null ) {
175
      if( value != null ) {
176
        Point size = getSize( value );
176
        Point size = getSize( value );
Lines 207-213 Link Here
207
      // TODO [fappel]: Workaround for background process problem
207
      // TODO [fappel]: Workaround for background process problem
208
      if( value != null ) {
208
      if( value != null ) {
209
        Point size = getSize( value );
209
        Point size = getSize( value );
210
        TextSizeDataBase.store( item.getFont(),
210
        TextSizeDataBase.store( item.getFontData(),
211
                                item.getString(),
211
                                item.getString(),
212
                                item.getWrapWidth(),
212
                                item.getWrapWidth(),
213
                                size );
213
                                size );
(-)src/org/eclipse/swt/internal/graphics/TextSizeDeterminationFacade.java (-5 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2008 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2008, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-19 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
13
import java.io.IOException;
14
import java.io.IOException;
14
15
15
import org.eclipse.rwt.internal.FacadesInitializer;
16
import org.eclipse.rwt.internal.FacadesInitializer;
16
import org.eclipse.swt.graphics.Font;
17
import org.eclipse.swt.graphics.FontData;
17
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
18
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
18
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
19
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
19
20
Lines 38-45 Link Here
38
    return FACADE_IMPL.writeFontProbingInternal();
39
    return FACADE_IMPL.writeFontProbingInternal();
39
  }
40
  }
40
41
41
  static String createFontParam( final Font font ) {
42
  static String createFontParam( final FontData fontData ) {
42
    return FACADE_IMPL.createFontParamInternal( font );
43
    return FACADE_IMPL.createFontParamInternal( fontData );
43
  }
44
  }
44
45
45
  public abstract String createMeasureStringInternal( String string,
46
  public abstract String createMeasureStringInternal( String string,
Lines 50-54 Link Here
50
51
51
  public abstract IProbe[] writeFontProbingInternal() throws IOException;
52
  public abstract IProbe[] writeFontProbingInternal() throws IOException;
52
53
53
  public abstract String createFontParamInternal( Font font );
54
  public abstract String createFontParamInternal( FontData fontData );
54
}
55
}
(-)src/org/eclipse/swt/internal/graphics/TextSizeDetermination.java (-15 / +19 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
Lines 35-41 Link Here
35
36
36
37
37
  public interface ICalculationItem {
38
  public interface ICalculationItem {
38
    Font getFont();
39
    FontData getFontData();
39
    String getString();
40
    String getString();
40
    int getWrapWidth();
41
    int getWrapWidth();
41
  }
42
  }
Lines 109-115 Link Here
109
        = TextSizeDeterminationFacade.createMeasureString( string,
110
        = TextSizeDeterminationFacade.createMeasureString( string,
110
                                                           expandNewLines );
111
                                                           expandNewLines );
111
    }
112
    }
112
    Point result = TextSizeDataBase.lookup( font, toMeasure, wrapWidth );
113
    FontData fontData = font.getFontData()[ 0 ];
114
    Point result = TextSizeDataBase.lookup( fontData, toMeasure, wrapWidth );
113
    if( result == null ) {
115
    if( result == null ) {
114
      switch( estimationMode ) {
116
      switch( estimationMode ) {
115
        case MARKUP_EXTENT: {
117
        case MARKUP_EXTENT: {
Lines 128-134 Link Here
128
          throw new IllegalStateException( "Unknown estimation mode." );
130
          throw new IllegalStateException( "Unknown estimation mode." );
129
        }
131
        }
130
      }
132
      }
131
      addCalculationItem( font, toMeasure, wrapWidth );
133
      addCalculationItem( fontData, toMeasure, wrapWidth );
132
    }
134
    }
133
    // TODO [rst] Still returns wrong result for texts that contain only
135
    // TODO [rst] Still returns wrong result for texts that contain only
134
    //            whitespace ( and possibly more that one line )
136
    //            whitespace ( and possibly more that one line )
Lines 140-151 Link Here
140
142
141
  public static int getCharHeight( final Font font ) {
143
  public static int getCharHeight( final Font font ) {
142
    int result;
144
    int result;
145
    FontData fontData = font.getFontData()[ 0 ];
143
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
146
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
144
    if( probeStore.containsProbeResult( font ) ) {
147
    if( probeStore.containsProbeResult( fontData ) ) {
145
      IProbeResult probeResult = probeStore.getProbeResult( font );
148
      IProbeResult probeResult = probeStore.getProbeResult( fontData );
146
      result = probeResult.getSize().y;
149
      result = probeResult.getSize().y;
147
    } else {
150
    } else {
148
      TextSizeProbeStore.addProbeRequest( font );
151
      TextSizeProbeStore.addProbeRequest( fontData );
149
      result = TextSizeEstimation.getCharHeight( font );
152
      result = TextSizeEstimation.getCharHeight( font );
150
    }
153
    }
151
    return result;
154
    return result;
Lines 154-164 Link Here
154
  public static float getAvgCharWidth( final Font font ) {
157
  public static float getAvgCharWidth( final Font font ) {
155
    float result;
158
    float result;
156
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
159
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
157
    if( probeStore.containsProbeResult( font ) ) {
160
    FontData fontData = font.getFontData()[ 0 ];
158
      IProbeResult probeResult = probeStore.getProbeResult( font );
161
    if( probeStore.containsProbeResult( fontData ) ) {
162
      IProbeResult probeResult = probeStore.getProbeResult( fontData );
159
      result = probeResult.getAvgCharWidth();
163
      result = probeResult.getAvgCharWidth();
160
    } else {
164
    } else {
161
      TextSizeProbeStore.addProbeRequest( font );
165
      TextSizeProbeStore.addProbeRequest( fontData );
162
      result = TextSizeEstimation.getAvgCharWidth( font );
166
      result = TextSizeEstimation.getAvgCharWidth( font );
163
    }
167
    }
164
    return result;
168
    return result;
Lines 183-206 Link Here
183
    return result;
187
    return result;
184
  }
188
  }
185
189
186
  private static void addCalculationItem( final Font font,
190
  private static void addCalculationItem( final FontData fontData,
187
                                          final String string,
191
                                          final String string,
188
                                          final int wrapWidth )
192
                                          final int wrapWidth )
189
  {
193
  {
190
    ICalculationItem[] oldItems = getCalculationItems();
194
    ICalculationItem[] oldItems = getCalculationItems();
191
    boolean mustAdd = true;
195
    boolean mustAdd = true;
192
    for( int i = 0; mustAdd && i < oldItems.length; i++ ) {
196
    for( int i = 0; mustAdd && i < oldItems.length; i++ ) {
193
      FontData oldFontData = oldItems[ i ].getFont().getFontData()[ 0 ];
197
      FontData oldFontData = oldItems[ i ].getFontData();
194
      mustAdd = !(    oldItems[ i ].getString().equals( string )
198
      mustAdd = !(    oldItems[ i ].getString().equals( string )
195
                   && oldFontData.equals( font.getFontData()[ 0 ] )
199
                   && oldFontData.equals( fontData )
196
                   && oldItems[ i ].getWrapWidth() == wrapWidth );
200
                   && oldItems[ i ].getWrapWidth() == wrapWidth );
197
    }
201
    }
198
    if( mustAdd ) {
202
    if( mustAdd ) {
199
      ICalculationItem[] newItems = new ICalculationItem[ oldItems.length + 1 ];
203
      ICalculationItem[] newItems = new ICalculationItem[ oldItems.length + 1 ];
200
      System.arraycopy( oldItems, 0, newItems, 0, oldItems.length );
204
      System.arraycopy( oldItems, 0, newItems, 0, oldItems.length );
201
      newItems[ oldItems.length ] = new ICalculationItem() {
205
      newItems[ oldItems.length ] = new ICalculationItem() {
202
        public Font getFont() {
206
        public FontData getFontData() {
203
          return font;
207
          return fontData;
204
        }
208
        }
205
        public String getString() {
209
        public String getString() {
206
          return string;
210
          return string;
(-)src/org/eclipse/swt/internal/graphics/TextSizeStorageRegistry.java (-4 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
Lines 19-31 Link Here
19
  
20
  
20
  private static ITextSizeStorage storage;
21
  private static ITextSizeStorage storage;
21
  
22
  
22
  public static void register( final ITextSizeStorage fontSizeStorage ) {
23
  public static void register( final ITextSizeStorage textSizeStorage ) {
23
    if( storage != null ) {
24
    if( storage != null ) {
24
      String msg
25
      String msg
25
        = "A fontsize storage implementation has already been registered.";
26
        = "A textsize storage implementation has already been registered.";
26
      throw new IllegalStateException( msg );
27
      throw new IllegalStateException( msg );
27
    }
28
    }
28
    storage = fontSizeStorage;
29
    storage = textSizeStorage;
29
  }
30
  }
30
  
31
  
31
  public static ITextSizeStorage obtain() {
32
  public static ITextSizeStorage obtain() {
(-)src/org/eclipse/swt/internal/graphics/ITextSizeStorage.java (-5 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-17 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
13
import org.eclipse.swt.graphics.Font;
14
import org.eclipse.swt.graphics.*;
14
import org.eclipse.swt.graphics.Point;
15
15
16
16
17
/**
17
/**
Lines 22-33 Link Here
22
  /**
22
  /**
23
   * TODO [fappel]: Documentation
23
   * TODO [fappel]: Documentation
24
   */
24
   */
25
  Font[] getFontList();
25
  FontData[] getFontList();
26
  
26
  
27
  /**
27
  /**
28
   * TODO [fappel]: Documentation
28
   * TODO [fappel]: Documentation
29
   */
29
   */
30
  void storeFont( Font font );
30
  void storeFont( FontData fontData );
31
  
31
  
32
  /**
32
  /**
33
   * TODO [fappel]: Documentation
33
   * TODO [fappel]: Documentation
(-)src/org/eclipse/swt/internal/graphics/DefaultTextSizeStorage.java (-49 / +55 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2007, 2010 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 *     EclipseSource - ongoing development
10
 ******************************************************************************/
11
 ******************************************************************************/
11
package org.eclipse.swt.internal.graphics;
12
package org.eclipse.swt.internal.graphics;
12
13
Lines 15-62 Link Here
15
import java.text.MessageFormat;
16
import java.text.MessageFormat;
16
import java.util.*;
17
import java.util.*;
17
18
18
import org.eclipse.rwt.graphics.Graphics;
19
import org.eclipse.swt.graphics.FontData;
19
import org.eclipse.swt.graphics.*;
20
import org.eclipse.swt.graphics.Point;
20
21
21
22
22
public class DefaultTextSizeStorage implements ITextSizeStorage {
23
public final class DefaultTextSizeStorage implements ITextSizeStorage {
23
24
24
  public static final int MIN_STORE_SIZE = 1000;
25
  public static final int MIN_STORE_SIZE = 1000;
25
  public static final int DEFAULT_STORE_SIZE = 10000;
26
  public static final int DEFAULT_STORE_SIZE = 10000;
26
27
27
  static final String COMMENT = "RAP DefaultFontSizeStorage";
28
  static final String COMMENT = "RAP DefaultFontSizeStorage";
28
  static final String PREFIX_FONT_KEY = "FONT_";
29
  static final String PREFIX_FONT_KEY = "FONT_";
29
  private static Set fonts = new HashSet();
30
  
30
  private static Map data = new HashMap();
31
  private static Object lock = new Object();
32
  private static int storeSize ;
33
  private static int clearRange;
34
  private static int clock;
35
  static {
36
    setStoreSize( DEFAULT_STORE_SIZE );
37
  }
38
39
  private static class Entry {
31
  private static class Entry {
40
    private Point point;
32
    private Point point;
41
    private long timeStamp;
33
    private long timeStamp;
42
  }
34
  }
43
35
  
36
  private final Object lock;
37
  // access is guarded by 'lock'
38
  private final Set fontDatas;
39
  // access is guarded by 'lock'
40
  private final Map data;
41
  private int storeSize;
42
  private int clearRange;
43
  private int clock;
44
45
  public DefaultTextSizeStorage() {
46
    lock = new Object();
47
    data = new HashMap();
48
    fontDatas = new HashSet();
49
    setStoreSize( DEFAULT_STORE_SIZE );
50
  }
44
51
45
  /////////////////////////////
52
  /////////////////////////////
46
  // interface IFontSizeStorage
53
  // interface IFontSizeStorage
47
54
48
  public Font[] getFontList() {
55
  public FontData[] getFontList() {
49
    Font[] result;
56
    FontData[] result;
50
    synchronized( lock ) {
57
    synchronized( lock ) {
51
      result = new Font[ fonts.size() ];
58
      result = new FontData[ fontDatas.size() ];
52
      fonts.toArray( result );
59
      fontDatas.toArray( result );
53
    }
60
    }
54
    return result;
61
    return result;
55
  }
62
  }
56
63
57
  public void storeFont( final Font font ) {
64
  public void storeFont( final FontData fontData ) {
58
    synchronized( lock ) {
65
    synchronized( lock ) {
59
      fonts.add( font );
66
      fontDatas.add( fontData );
60
    }
67
    }
61
  }
68
  }
62
69
Lines 73-81 Link Here
73
    return result;
80
    return result;
74
  }
81
  }
75
82
76
  public void storeTextSize( final Integer key,
83
  public void storeTextSize( final Integer key, final Point size ) {
77
                               final Point size )
78
  {
79
    Point clone = defensiveCopy( size );
84
    Point clone = defensiveCopy( size );
80
    Entry entry = new Entry();
85
    Entry entry = new Entry();
81
    entry.point = clone;
86
    entry.point = clone;
Lines 89-99 Link Here
89
  //////////////
94
  //////////////
90
  // persistance
95
  // persistance
91
96
92
  public void save( final OutputStream out ) throws IOException {
97
  public void save( final OutputStream stream ) throws IOException {
93
    Properties properties = new Properties();
98
    Properties properties = new Properties();
94
    Font[] fontList;
99
    FontData[] fontDataList;
95
    synchronized( lock ) {
100
    synchronized( lock ) {
96
      fontList = getFontList();
101
      fontDataList = getFontList();
97
      Iterator iterator = data.keySet().iterator();
102
      Iterator iterator = data.keySet().iterator();
98
      while( iterator.hasNext() ) {
103
      while( iterator.hasNext() ) {
99
        Integer key = ( Integer )iterator.next();
104
        Integer key = ( Integer )iterator.next();
Lines 105-123 Link Here
105
        properties.setProperty( key.toString(), value.toString() );
110
        properties.setProperty( key.toString(), value.toString() );
106
      }
111
      }
107
    }
112
    }
108
    for( int i = 0; i < fontList.length; i++ ) {
113
    for( int i = 0; i < fontDataList.length; i++ ) {
109
      StringBuffer key = new StringBuffer();
114
      StringBuffer key = new StringBuffer();
110
      key.append( PREFIX_FONT_KEY );
115
      key.append( PREFIX_FONT_KEY );
111
      key.append( i );
116
      key.append( i );
112
      String value = fontList[ i ].getFontData()[ 0 ].toString();
117
      String value = fontDataList[ i ].toString();
113
      properties.setProperty( key.toString(), value );
118
      properties.setProperty( key.toString(), value );
114
    }
119
    }
115
    properties.store( out, COMMENT );
120
    properties.store( stream, COMMENT );
116
  }
121
  }
117
122
118
  public void read( final InputStream in ) throws IOException {
123
  public void read( final InputStream stream ) throws IOException {
119
    Properties properties = new Properties();
124
    Properties properties = new Properties();
120
    properties.load( in );
125
    properties.load( stream );
121
    synchronized( lock ) {
126
    synchronized( lock ) {
122
      Enumeration keys = properties.keys();
127
      Enumeration keys = properties.keys();
123
      while( keys.hasMoreElements() ) {
128
      while( keys.hasMoreElements() ) {
Lines 125-131 Link Here
125
        String value = properties.getProperty( key );
130
        String value = properties.getProperty( key );
126
        if( key.startsWith( PREFIX_FONT_KEY ) ) {
131
        if( key.startsWith( PREFIX_FONT_KEY ) ) {
127
          FontData fontData = new FontData( value );
132
          FontData fontData = new FontData( value );
128
          storeFont( Graphics.getFont( fontData ) );
133
          storeFont( fontData );
129
        } else {
134
        } else {
130
          storeTextSize( new Integer( key ), parsePoint( value ) );
135
          storeTextSize( new Integer( key ), parsePoint( value ) );
131
        }
136
        }
Lines 137-143 Link Here
137
  ////////////////////
142
  ////////////////////
138
  // overflow handling
143
  // overflow handling
139
144
140
  public static void setStoreSize( final int storeSize ) {
145
  public void setStoreSize( final int storeSize ) {
141
    if( storeSize < MIN_STORE_SIZE ) {
146
    if( storeSize < MIN_STORE_SIZE ) {
142
      String txt = "Store size must be >= {0}.";
147
      String txt = "Store size must be >= {0}.";
143
      Object[] param = new Object[] { new Integer( MIN_STORE_SIZE ) };
148
      Object[] param = new Object[] { new Integer( MIN_STORE_SIZE ) };
Lines 148-162 Link Here
148
    BigDecimal bdStoreSize = new BigDecimal( storeSize );
153
    BigDecimal bdStoreSize = new BigDecimal( storeSize );
149
    int rounding = BigDecimal.ROUND_HALF_UP;
154
    int rounding = BigDecimal.ROUND_HALF_UP;
150
    clearRange = bdStoreSize.divide( ten, 0, rounding ).intValue();
155
    clearRange = bdStoreSize.divide( ten, 0, rounding ).intValue();
151
    DefaultTextSizeStorage.storeSize = storeSize;
156
    this.storeSize = storeSize;
152
  }
157
  }
153
158
154
  public static int getStoreSize() {
159
  public int getStoreSize() {
155
    return storeSize;
160
    return storeSize;
156
  }
161
  }
157
162
158
  private void handleOverFlow() {
163
  private void handleOverFlow() {
159
    if( data.size() >= DefaultTextSizeStorage.storeSize ) {
164
    if( data.size() >= storeSize ) {
160
      Entry[] entries = new Entry[ data.size() ];
165
      Entry[] entries = new Entry[ data.size() ];
161
      data.values().toArray( entries );
166
      data.values().toArray( entries );
162
      Arrays.sort( entries, new Comparator() {
167
      Arrays.sort( entries, new Comparator() {
Lines 182-213 Link Here
182
  //////////////////
187
  //////////////////
183
  // helping methods
188
  // helping methods
184
189
185
  private Point parsePoint( final String value ) {
190
  private static Point parsePoint( final String value ) {
186
    String[] values = value.split( "," );
191
    String[] values = value.split( "," );
187
    int x = Integer.valueOf( values[ 0 ] ).intValue();
192
    int x = Integer.valueOf( values[ 0 ] ).intValue();
188
    int y = Integer.valueOf( values[ 1 ] ).intValue();
193
    int y = Integer.valueOf( values[ 1 ] ).intValue();
189
    return new Point( x, y );
194
    return new Point( x, y );
190
  }
195
  }
191
196
197
  private static Point defensiveCopy( final Point point ) {
198
    Point result = null;
199
    if( point != null ) {
200
      result = new Point( point.x, point.y );
201
    }
202
    return result;
203
  }
204
192
  // for test purposes only
205
  // for test purposes only
193
  void resetFontList() {
206
  void resetFontList() {
194
    synchronized( fonts ) {
207
    synchronized( fontDatas ) {
195
      fonts.clear();
208
      fontDatas.clear();
196
    }
209
    }
197
  }
210
  }
198
211
  
199
  // for test purposes only
212
  // for test purposes only
200
  void resetStringSizes() {
213
  void resetStringSizes() {
201
    synchronized( data ) {
214
    synchronized( data ) {
202
      data.clear();
215
      data.clear();
203
    }
216
    }
204
  }
217
  }
205
218
  
206
  private static Point defensiveCopy( final Point point ) {
207
    Point result = null;
208
    if( point != null ) {
209
      result = new Point( point.x, point.y );
210
    }
211
    return result;
212
  }
213
}
219
}
(-)src/org/eclipse/swt/internal/graphics/TextSizeDetermination_Test.java (-36 / +46 lines)
Lines 20-27 Link Here
20
import org.eclipse.rwt.Fixture;
20
import org.eclipse.rwt.Fixture;
21
import org.eclipse.rwt.graphics.Graphics;
21
import org.eclipse.rwt.graphics.Graphics;
22
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.graphics.Font;
23
import org.eclipse.swt.graphics.*;
24
import org.eclipse.swt.graphics.Point;
25
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
24
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
26
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
25
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
27
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbeResult;
26
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbeResult;
Lines 53-63 Link Here
53
    Point storedSize = new Point( 100, 10 );
52
    Point storedSize = new Point( 100, 10 );
54
    IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests();
53
    IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests();
55
    assertEquals( 1, probeRequests.length );
54
    assertEquals( 1, probeRequests.length );
56
    assertSame( font, probeRequests[ 0 ].getFont() );
55
    assertSame( font.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() );
57
56
58
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
57
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
59
    probeStore.createProbeResult( probeRequests[ 0 ], new Point( 10, 10 ) );
58
    probeStore.createProbeResult( probeRequests[ 0 ], new Point( 10, 10 ) );
60
    TextSizeDataBase.store( font, TEST_STRING, SWT.DEFAULT, storedSize );
59
    TextSizeDataBase.store( font.getFontData()[ 0 ], 
60
                            TEST_STRING, 
61
                            SWT.DEFAULT, 
62
                            storedSize );
61
    calculated = TextSizeDetermination.stringExtent( font, TEST_STRING );
63
    calculated = TextSizeDetermination.stringExtent( font, TEST_STRING );
62
    assertEquals( storedSize, calculated );
64
    assertEquals( storedSize, calculated );
63
65
Lines 110-116 Link Here
110
112
111
    probeRequests = TextSizeProbeStore.getProbeRequests();
113
    probeRequests = TextSizeProbeStore.getProbeRequests();
112
    assertEquals( 1, probeRequests.length );
114
    assertEquals( 1, probeRequests.length );
113
    assertSame( font0, probeRequests[ 0 ].getFont() );
115
    assertSame( font0.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() );
114
116
115
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
117
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
116
    Point probeSize = new Point( 10, 13 );
118
    Point probeSize = new Point( 10, 13 );
Lines 130-136 Link Here
130
132
131
    probeRequests = TextSizeProbeStore.getProbeRequests();
133
    probeRequests = TextSizeProbeStore.getProbeRequests();
132
    assertEquals( 1, probeRequests.length );
134
    assertEquals( 1, probeRequests.length );
133
    assertSame( font0, probeRequests[ 0 ].getFont() );
135
    assertSame( font0.getFontData()[ 0 ], probeRequests[ 0 ].getFontData() );
134
136
135
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
137
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
136
    Point probeSize = new Point( TextSizeProbeStore.DEFAULT_PROBE.length() * 4, 10 );
138
    Point probeSize = new Point( TextSizeProbeStore.DEFAULT_PROBE.length() * 4, 10 );
Lines 141-156 Link Here
141
143
142
  public void testFontSizeDataBase() {
144
  public void testFontSizeDataBase() {
143
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
145
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
146
    FontData fontData0 = font0.getFontData()[ 0 ];
144
    Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL );
147
    Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL );
148
    FontData fontData1 = font1.getFontData()[ 0 ];
145
149
146
    Point textSize = TextSizeDataBase.lookup( font0, TEST_STRING, SWT.DEFAULT );
150
    Point textSize;
151
    textSize = TextSizeDataBase.lookup( fontData0, TEST_STRING, SWT.DEFAULT );
147
    assertNull( textSize );
152
    assertNull( textSize );
148
    textSize = TextSizeDataBase.lookup( font1, TEST_STRING, SWT.DEFAULT );
153
    textSize = TextSizeDataBase.lookup( fontData1, TEST_STRING, SWT.DEFAULT );
149
    assertNull( textSize );
154
    assertNull( textSize );
150
155
151
    try {
156
    try {
152
      Point point = new Point( 1, 1 );
157
      Point point = new Point( 1, 1 );
153
      TextSizeDataBase.store( font1, TEST_STRING, SWT.DEFAULT, point );
158
      TextSizeDataBase.store( fontData1, TEST_STRING, SWT.DEFAULT, point );
154
      fail( "No probe available." );
159
      fail( "No probe available." );
155
    } catch( final IllegalStateException ise ) {
160
    } catch( final IllegalStateException ise ) {
156
    }
161
    }
Lines 163-226 Link Here
163
    probeStore.createProbeResult( findRequestedProbe( 1 ), probeSize1 );
168
    probeStore.createProbeResult( findRequestedProbe( 1 ), probeSize1 );
164
169
165
    Point calculatedTextSize0 = new Point( 100, 10 );
170
    Point calculatedTextSize0 = new Point( 100, 10 );
166
    TextSizeDataBase.store( font0,
171
    TextSizeDataBase.store( fontData0,
167
                            TEST_STRING,
172
                            TEST_STRING,
168
                            SWT.DEFAULT,
173
                            SWT.DEFAULT,
169
                            calculatedTextSize0 );
174
                            calculatedTextSize0 );
170
    Point calculatedTextSize1 = new Point( 100, 12 );
175
    Point calculatedTextSize1 = new Point( 100, 12 );
171
    TextSizeDataBase.store( font1,
176
    TextSizeDataBase.store( fontData1,
172
                            TEST_STRING,
177
                            TEST_STRING,
173
                            SWT.DEFAULT,
178
                            SWT.DEFAULT,
174
                            calculatedTextSize1 );
179
                            calculatedTextSize1 );
175
    textSize = TextSizeDataBase.lookup( font0, TEST_STRING, SWT.DEFAULT );
180
    textSize = TextSizeDataBase.lookup( fontData0, TEST_STRING, SWT.DEFAULT );
176
    assertEquals( calculatedTextSize0, textSize );
181
    assertEquals( calculatedTextSize0, textSize );
177
    textSize = TextSizeDataBase.lookup( font1, TEST_STRING, SWT.DEFAULT );
182
    textSize = TextSizeDataBase.lookup( fontData1, TEST_STRING, SWT.DEFAULT );
178
    assertEquals( calculatedTextSize1, textSize );
183
    assertEquals( calculatedTextSize1, textSize );
179
  }
184
  }
180
185
181
  private IProbe findRequestedProbe( final int i ) {
186
  private IProbe findRequestedProbe( final int i ) {
182
    IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests();
187
    IProbe[] probeRequests = TextSizeProbeStore.getProbeRequests();
183
    return TextSizeProbeStore.getProbe( probeRequests[ i ].getFont() );
188
    return TextSizeProbeStore.getProbe( probeRequests[ i ].getFontData() );
184
  }
189
  }
185
190
186
  public void testProbeStorage() {
191
  public void testProbeStorage() {
187
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
192
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
193
    FontData fontData0 = font0.getFontData()[ 0 ];
188
    IProbe[] probeList = TextSizeProbeStore.getProbeList();
194
    IProbe[] probeList = TextSizeProbeStore.getProbeList();
189
    assertEquals( 0, probeList.length );
195
    assertEquals( 0, probeList.length );
190
    IProbe probe0 = TextSizeProbeStore.getProbe( font0 );
196
    IProbe probe0 = TextSizeProbeStore.getProbe( fontData0 );
191
    assertNull( probe0 );
197
    assertNull( probe0 );
192
198
193
    String probeText0 = "ProbeText0";
199
    String probeText0 = "ProbeText0";
194
    probe0 = TextSizeProbeStore.createProbe( font0, probeText0 );
200
    probe0 = TextSizeProbeStore.createProbe( fontData0, probeText0 );
195
    probeList = TextSizeProbeStore.getProbeList();
201
    probeList = TextSizeProbeStore.getProbeList();
196
    assertEquals( 1, probeList.length );
202
    assertEquals( 1, probeList.length );
197
    assertSame( probe0, probeList[ 0 ] );
203
    assertSame( probe0, probeList[ 0 ] );
198
    assertSame( probe0, TextSizeProbeStore.getProbe( font0 ) );
204
    assertSame( probe0, TextSizeProbeStore.getProbe( fontData0 ) );
199
    assertTrue( TextSizeProbeStore.containsProbe( font0 ) );
205
    assertTrue( TextSizeProbeStore.containsProbe( fontData0 ) );
200
    assertSame( probe0.getFont(), font0 );
206
    assertSame( probe0.getFontData(), fontData0 );
201
    assertSame( probe0.getString(), probeText0 );
207
    assertSame( probe0.getString(), probeText0 );
202
208
203
    Font font1 = Graphics.getFont( "arial", 12, SWT.NORMAL );
209
    Font font1 = Graphics.getFont( "arial", 12, SWT.NORMAL );
204
    assertFalse( TextSizeProbeStore.containsProbe( font1 ) );
210
    FontData fontData1 = font1.getFontData()[ 0 ];
211
    assertFalse( TextSizeProbeStore.containsProbe( fontData1 ) );
205
212
206
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
213
    TextSizeProbeStore probeStore = TextSizeProbeStore.getInstance();
207
    IProbeResult probeResult0 = probeStore.getProbeResult( font0 );
214
    IProbeResult probeResult0 = probeStore.getProbeResult( fontData0 );
208
    assertNull( probeResult0 );
215
    assertNull( probeResult0 );
209
216
210
    Point probeSize0 = new Point( 10, 10 );
217
    Point probeSize0 = new Point( 10, 10 );
211
    probeResult0 = probeStore.createProbeResult( probe0, probeSize0 );
218
    probeResult0 = probeStore.createProbeResult( probe0, probeSize0 );
212
    assertSame( probeResult0.getProbe(), probe0 );
219
    assertSame( probeResult0.getProbe(), probe0 );
213
    assertSame( probeResult0.getSize(), probeSize0 );
220
    assertSame( probeResult0.getSize(), probeSize0 );
214
    assertTrue( probeStore.containsProbeResult( font0 ) );
221
    assertTrue( probeStore.containsProbeResult( fontData0 ) );
215
    assertFalse( probeStore.containsProbeResult( font1 ) );
222
    assertFalse( probeStore.containsProbeResult( fontData1 ) );
216
  }
223
  }
217
224
218
  public void testDefaultFontSizeStorage() throws IOException {
225
  public void testDefaultFontSizeStorage() throws IOException {
219
    DefaultTextSizeStorage storage = new DefaultTextSizeStorage();
226
    DefaultTextSizeStorage storage = new DefaultTextSizeStorage();
220
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
227
    Font font0 = Graphics.getFont( "arial", 10, SWT.NORMAL );
228
    FontData fontData0 = font0.getFontData()[ 0 ];
221
    Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL );
229
    Font font1 = Graphics.getFont( "helvetia", 12, SWT.NORMAL );
222
    storage.storeFont( font0 );
230
    FontData fontData1 = font1.getFontData()[ 0 ];
223
    storage.storeFont( font1 );
231
    storage.storeFont( fontData0 );
232
    storage.storeFont( fontData1 );
224
233
225
    Point point0 = new Point( 9, 10 );
234
    Point point0 = new Point( 9, 10 );
226
    Integer key0 = new Integer( 0 );
235
    Integer key0 = new Integer( 0 );
Lines 252-268 Link Here
252
    assertEquals( point0, actual );
261
    assertEquals( point0, actual );
253
    actual = storage.lookupTextSize( key1 );
262
    actual = storage.lookupTextSize( key1 );
254
    assertEquals( point1, actual );
263
    assertEquals( point1, actual );
255
    Font[] fontList = storage.getFontList();
264
    FontData[] fontDatas = storage.getFontList();
256
    List fonts = Arrays.asList( fontList );
265
    assertEquals( 2, fontDatas.length );
257
    assertEquals( 2, fonts.size() );
266
    List fontDataList = Arrays.asList( fontDatas );
258
    assertTrue( fonts.contains( font0 ) );
267
    assertTrue( fontDataList.contains( fontData0 ) );
259
    assertTrue( fonts.contains( font1 ) );
268
    assertTrue( fontDataList.contains( fontData1 ) );
260
  }
269
  }
261
270
262
  public void testStorageOverflow() {
271
  public void testStorageOverflow() {
263
    DefaultTextSizeStorage storage = new DefaultTextSizeStorage();
272
    DefaultTextSizeStorage storage = new DefaultTextSizeStorage();
264
    int storeSize = DefaultTextSizeStorage.MIN_STORE_SIZE;
273
    int storeSize = DefaultTextSizeStorage.MIN_STORE_SIZE;
265
    DefaultTextSizeStorage.setStoreSize( storeSize );
274
    storage.setStoreSize( storeSize );
266
275
267
    for( int i = 0; i < storeSize - 1; i++ ) {
276
    for( int i = 0; i < storeSize - 1; i++ ) {
268
      Integer key = new Integer( i );
277
      Integer key = new Integer( i );
Lines 286-300 Link Here
286
  }
295
  }
287
296
288
  public void testTextSizeDatabaseKey() {
297
  public void testTextSizeDatabaseKey() {
289
    final Font font = Graphics.getFont( "name", 10, SWT.NORMAL );
298
    Font font = Graphics.getFont( "name", 10, SWT.NORMAL );
299
    final FontData fontData = font.getFontData()[ 0 ];
290
    Set takenKeys = new HashSet();
300
    Set takenKeys = new HashSet();
291
    StringBuffer generatedText = new StringBuffer();
301
    StringBuffer generatedText = new StringBuffer();
292
    for( int i = 0; i < 100; i++ ) {
302
    for( int i = 0; i < 100; i++ ) {
293
      generatedText.append( "a" );
303
      generatedText.append( "a" );
294
      final String text = generatedText.toString();
304
      final String text = generatedText.toString();
295
      IProbe probe = new IProbe() {
305
      IProbe probe = new IProbe() {
296
        public Font getFont() {
306
        public FontData getFontData() {
297
          return font;
307
          return fontData;
298
        }
308
        }
299
        public String getString() {
309
        public String getString() {
300
          return text;
310
          return text;
Lines 305-316 Link Here
305
      };
315
      };
306
      Point size = new Point( 1, 2 );
316
      Point size = new Point( 1, 2 );
307
      TextSizeProbeStore.getInstance().createProbeResult( probe, size );
317
      TextSizeProbeStore.getInstance().createProbeResult( probe, size );
308
      Integer key = TextSizeDataBase.getKey( font, text, -1 );
318
      Integer key = TextSizeDataBase.getKey( fontData, text, -1 );
309
      assertFalse( takenKeys.contains( key ) );
319
      assertFalse( takenKeys.contains( key ) );
310
      takenKeys.add( key );
320
      takenKeys.add( key );
311
    }
321
    }
312
  }
322
  }
313
323
  
314
  protected void setUp() throws Exception {
324
  protected void setUp() throws Exception {
315
    Fixture.setUp();
325
    Fixture.setUp();
316
    TextSizeDataBase.reset();
326
    TextSizeDataBase.reset();
(-)src/org/eclipse/swt/widgets/Widget_Test.java (-1 / +1 lines)
Lines 413-419 Link Here
413
413
414
  public void testReskin() {
414
  public void testReskin() {
415
    Fixture.fakePhase( PhaseId.PROCESS_ACTION );
415
    Fixture.fakePhase( PhaseId.PROCESS_ACTION );
416
    final ArrayList log = new ArrayList();
416
    final java.util.List log = new ArrayList();
417
    Display display = new Display();
417
    Display display = new Display();
418
    Listener listener = new Listener() {
418
    Listener listener = new Listener() {
419
      public void handleEvent( final Event event ) {
419
      public void handleEvent( final Event event ) {
(-)src/org/eclipse/swt/internal/graphics/TextSizeDeterminationFacadeImpl.java (-2 / +2 lines)
Lines 13-19 Link Here
13
13
14
import java.io.IOException;
14
import java.io.IOException;
15
15
16
import org.eclipse.swt.graphics.Font;
16
import org.eclipse.swt.graphics.FontData;
17
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
17
import org.eclipse.swt.internal.graphics.TextSizeDetermination.ICalculationItem;
18
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
18
import org.eclipse.swt.internal.graphics.TextSizeProbeStore.IProbe;
19
19
Lines 38-44 Link Here
38
    return new IProbe[ 0 ];
38
    return new IProbe[ 0 ];
39
  }
39
  }
40
40
41
  public String createFontParamInternal( final Font font ) {
41
  public String createFontParamInternal( final FontData font ) {
42
    return null;
42
    return null;
43
  }
43
  }
44
}
44
}

Return to bug 301249