Skip to content

Commit

Permalink
Merge branch 'feature/string-render' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
paulosousadias committed Nov 12, 2024
2 parents afa64ba + b8ba7c0 commit 51e472f
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/java/pt/lsts/neptus/util/RenderStringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2004-2024 Universidade do Porto - Faculdade de Engenharia
* Laboratório de Sistemas e Tecnologia Subaquática (LSTS)
* All rights reserved.
* Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
*
* This file is part of Neptus, Command and Control Framework.
*
* Commercial Licence Usage
* Licencees holding valid commercial Neptus licences may use this file
* in accordance with the commercial licence agreement provided with the
* Software or, alternatively, in accordance with the terms contained in a
* written agreement between you and Universidade do Porto. For licensing
* terms, conditions, and further information contact lsts@fe.up.pt.
*
* Modified European Union Public Licence - EUPL v.1.1 Usage
* Alternatively, this file may be used under the terms of the Modified EUPL,
* Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
* included in the packaging of this file. You may not use this work
* except in compliance with the Licence. Unless required by applicable
* law or agreed to in writing, software distributed under the Licence is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific
* language governing permissions and limitations at
* https://github.com/LSTS/neptus/blob/develop/LICENSE.md
* and http://ec.europa.eu/idabc/eupl.html.
*
* For more information please see <http://lsts.fe.up.pt/neptus>.
*
* Author: Miguel Carvalho
* 2024/11/04
*/
package pt.lsts.neptus.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.GlyphVector;

public class RenderStringUtils {

/** To avoid instantiation */
private RenderStringUtils() {
}

/**
* Renders an outline of string text.
*
* @param g
* @param font
* @param textColor
* @param outlineColor
* @param text
* @param x
* @param y
* @return
*/
public static void drawStringWOutline(Graphics2D g, Font font, Color textColor, Color outlineColor, String text, double x, double y) {
Graphics2D gtemp = (Graphics2D) g.create();
try {
gtemp.setFont(font);
gtemp.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gtemp.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// Calculate dynamic outline thickness based on font size
float outlineThickness = font.getSize() * 0.25f;

GlyphVector glyphVector = font.createGlyphVector(gtemp.getFontRenderContext(), text);
Shape textShape = glyphVector.getOutline((int) (x), (int) (y));

// Draw the outline
gtemp.setColor(outlineColor);
gtemp.setStroke(new BasicStroke(outlineThickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // Thickness of the outline
gtemp.draw(textShape);

// Draw the fill
gtemp.setColor(textColor);
gtemp.fill(textShape);
}
catch (Exception e) {
e.printStackTrace();
} finally {
gtemp.dispose();
}
}
}

0 comments on commit 51e472f

Please sign in to comment.