Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract label calculation code Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
- Loading branch information
1 parent
d472f95
commit 9518e5a
Showing
3 changed files
with
50 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...lipse.chronograph.swt/src/org/eclipse/chronograph/internal/swt/renderers/impl/Labels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.chronograph.internal.swt.renderers.impl; | ||
|
||
import org.eclipse.swt.graphics.GC; | ||
import org.eclipse.swt.graphics.Point; | ||
|
||
public class Labels { | ||
|
||
String fit(String label, int limit, GC gc) { | ||
StringBuilder builder = new StringBuilder(); | ||
String ellipsis = "..."; //$NON-NLS-1$ | ||
Point point = gc.stringExtent(ellipsis); | ||
int consumed = 0; | ||
for (char ch : label.toCharArray()) { | ||
consumed += gc.getCharWidth(ch); | ||
if (consumed < limit - point.x) { | ||
builder.append(ch); | ||
} else { | ||
builder.append(ellipsis); | ||
break; | ||
} | ||
} | ||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters