- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望在顶部和底部围绕圆的半径绘制轮廓文本,并且文本的两个部分都面向正确的方向。
How to draw an outline around text in AWT?和 Write text along a curve in Java帮助我创建了代码(如下),该代码将逐个字符地绘制文本,其中每个字符独立旋转(有一些小的图形故障,这将是另一个问题的主题)。
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CreateTextStackOverflow {
private static final int EDGE_GAP = 10;
public static void main(String[] args) throws IOException {
BufferedImage bi = new BufferedImage(364,364,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
Font font = new Font("Arial",Font.BOLD,48);
FontMetrics fm = g.getFontMetrics(font);
int imageWidth = bi.getWidth();
int imageHeight = bi.getHeight();
int centerX = imageWidth/2;
int centerY = imageHeight/2;
int radius = Math.min(centerX,centerY)-EDGE_GAP-((int)Math.ceil(fm.getMaxCharBounds(g).getHeight()/2.0));
drawOutlinedTextAroundCircle(g,"TOP TEXT",0,false,imageWidth,imageHeight,radius,centerX,centerY,font,Color.BLACK,Color.WHITE);
drawOutlinedTextAroundCircle(g,"BOTTOM TEXT",0,true,imageWidth,imageHeight,radius,centerX,centerY,font,Color.RED,Color.WHITE);
ImageIO.write(bi, "png", new File("test.png"));
}
private static void drawOutlinedTextAroundCircle(Graphics2D g, String text, int textCenterAngleDegrees, boolean atBottom, int imageWidth, int imageHeight, int radius, int centerX, int centerY, Font font, Color outlineColour, Color innerColour) {
FontMetrics fm = g.getFontMetrics(font);
char[] characters = text.toCharArray();
int characterCount = characters.length;
int spaceCharacterWidth = fm.charWidth('n');
boolean[] spaces = new boolean[characterCount];
int[] characterWidths = new int[characterCount+1];
characterWidths[characterCount] = 0;
for (int index=0; index<characterCount; index++) {
char character = characters[index];
spaces[index] = character == ' ' || Character.isSpaceChar(character);
characterWidths[index] = spaces[index]?spaceCharacterWidth:fm.charWidth(character);
}
double currentAngle = 0;
double[] characterAngles = new double[characterCount];
int leading = fm.getLeading();
for (int index=0; index<characterCount; index++) {
characterAngles[index] = currentAngle;
currentAngle += Math.sin(((characterWidths[index]/2.0) + leading + (characterWidths[index+1]/2.0)) / (double)radius);
}
double adjustment = (textCenterAngleDegrees * Math.PI / 180) - ((characterAngles[characterCount-1] - characterAngles[0]) / 2.0);
for (int index=0; index<characterCount; index++) {
characterAngles[index] += adjustment;
}
AffineTransform flipTransform = new AffineTransform();
if (atBottom) {
flipTransform.scale(1,-1);
flipTransform.translate(0,-imageHeight);
}
AffineTransform translateTransform = new AffineTransform(flipTransform);
translateTransform.translate(centerX, centerY);
for (int index=0; index<characterCount; index++) {
if (!spaces[index]) {
AffineTransform rotateTransform = new AffineTransform(translateTransform);
rotateTransform.rotate(characterAngles[index], 0.0, 0.0);
float x = (float)(-(characterWidths[index]/2.0));
float y = (float)(-radius);
FontRenderContext frc = g.getFontRenderContext();
String str = new String(characters,index,1);
GlyphVector glyphVector = font.createGlyphVector(frc, str);
Rectangle2D box = glyphVector.getVisualBounds();
g.setTransform(rotateTransform);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape shape = glyphVector.getOutline((float)(x - box.getX()), (float)(y - box.getY()));
g.setColor(innerColour);
g.fill(shape);
g.setColor(outlineColour);
g.setStroke(new BasicStroke(1f));
g.draw(shape);
}
}
}
}
(产生)
这存在底部文本不正确的问题。
我已经多次尝试使用不同的仿射变换来更改绘制的每个字符,但没有成功。
我的目标是找到一个解决方案,其中 0 度会导致文本以圆的顶部/底部为中心,而增加度数会将文本进一步移至右侧,如 GIMP 中模拟的图表所示:
以这种方式绘制文本的正确方法是什么?
最佳答案
从头开始,使用我编写并发布到原始问题的代码作为基础,以及无数张纸,我计算出了这个工作:
如果文本位于底部,并且增加角度会将文本向左移动(与最初要求的不同)
按顺序执行:
如果文本位于底部,并且增加角度会将文本移至右侧
或者:
下面的解决方案实现了这一点(使用左侧选项)。它还将文本样式的逻辑与文本定位分开:
package uk.co.scottdennison.java.testbed;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import uk.co.scottdennison.java.testbed.TextUtilities.CircularTextPosition;
import uk.co.scottdennison.java.testbed.TextUtilities.GlyphDrawer;
import static uk.co.scottdennison.java.testbed.TextUtilities.CircularTextPosition.BASELINE_ON_CIRCLE;
import static uk.co.scottdennison.java.testbed.TextUtilities.CircularTextPosition.INSIDE_CIRCLE;
import static uk.co.scottdennison.java.testbed.TextUtilities.CircularTextPosition.OUTSIDE_CIRCLE;
public class CreateText4 {
private static final int IMAGE_SIZE = 364;
private static final Color BACKGROUND_COLOUR = new Color(242,247,254);
private static final Font FONT = new Font("Arial",Font.BOLD,48);
private static final Stroke STROKE = new BasicStroke(1f);
private static final Color UPPER_TEXT_FILL_COLOUR = Color.WHITE;
private static final Color UPPER_TEXT_OUTLINE_COLOUR = Color.RED;
private static final Color LOWER_TEXT_FILL_COLOUR = Color.WHITE;
private static final Color LOWER_TEXT_OUTLINE_COLOUR = Color.BLUE;
public static void main(String[] args) throws IOException {
for (CircularTextPosition textPosition : CircularTextPosition.values()) {
for (int squash=0; squash<=1; squash++) {
for (int angle=0; angle<360; angle+=15) {
int edgeGap = 0;
switch (textPosition) { // For the demo, the edge gaps need to be different, but the enum in TextUtilities could be in a completely separate library so should have no knowledge of this.
case INSIDE_CIRCLE:
edgeGap = 10;
break;
case BASELINE_ON_CIRCLE:
edgeGap = 40;
break;
case OUTSIDE_CIRCLE:
edgeGap = 70;
break;
}
draw(angle,edgeGap,squash==1,textPosition);
}
}
}
}
public static void draw(int angle, int edgeGap, boolean squash, CircularTextPosition circularTextPosition) throws IOException {
System.out.println("angle=" + angle + " / squash=" + squash + " / circularTextPosition=" + circularTextPosition);
BufferedImage bi = new BufferedImage(IMAGE_SIZE,IMAGE_SIZE,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
double center = IMAGE_SIZE/2.0;
double radius = center-edgeGap;
int flooredCenter = (int)Math.floor(center);
int ceiledCenter = (int)Math.ceil(center);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(BACKGROUND_COLOUR);
g.fillRect(-1,-1,IMAGE_SIZE+2,IMAGE_SIZE+2);
g.setStroke(new BasicStroke(1f));
g.setColor(Color.GREEN);
g.drawLine(0, 0, IMAGE_SIZE, IMAGE_SIZE);
g.drawLine(flooredCenter, 0, flooredCenter, IMAGE_SIZE);
if (flooredCenter != ceiledCenter) {
g.drawLine(flooredCenter-1, 0, flooredCenter-1, IMAGE_SIZE);
}
g.drawLine(IMAGE_SIZE, 0, 0, IMAGE_SIZE);
g.drawLine(0, flooredCenter, IMAGE_SIZE, flooredCenter);
if (flooredCenter != ceiledCenter) {
g.drawLine(0, flooredCenter-1, IMAGE_SIZE, flooredCenter-1);
}
g.setColor(Color.RED);
g.drawOval((int)(center-radius),(int)(center-radius),(int)(radius+radius-1),(int)(radius+radius-1));
TextUtilities.drawTextAroundCircle(g,"Top y Text" ,angle,squash,circularTextPosition,false,radius,center,center,FONT,new OutlinedFilledGlyphDrawer(UPPER_TEXT_FILL_COLOUR,UPPER_TEXT_OUTLINE_COLOUR,STROKE));
TextUtilities.drawTextAroundCircle(g,"Bottom y Text",angle,squash,circularTextPosition,true ,radius,center,center,FONT,new OutlinedFilledGlyphDrawer(LOWER_TEXT_FILL_COLOUR,LOWER_TEXT_OUTLINE_COLOUR,STROKE));
ImageIO.write(bi, "png", new File(String.format("test_%s_%s_%03d.png", circularTextPosition.name().toLowerCase().replace("_","-"),squash?"squashed":"normal",angle)));
}
public static class OutlinedFilledGlyphDrawer implements GlyphDrawer {
private Color oldGraphicsStateColor;
private Stroke oldGraphicsStateStroke;
private final Color fillColour;
private final Color outlineColour;
private final Stroke stroke;
public OutlinedFilledGlyphDrawer(Color fillColour, Color outlineColour, Stroke stroke) {
this.fillColour = fillColour;
this.outlineColour = outlineColour;
this.stroke = stroke;
}
@Override
public void saveGraphicsStateBeforeDraw(Graphics2D g) {
this.oldGraphicsStateColor = g.getColor();
this.oldGraphicsStateStroke = g.getStroke();
}
@Override
public void drawGlyph(Graphics2D preTransformedG, Shape glyph) {
preTransformedG.setColor(this.fillColour);
preTransformedG.fill(glyph);
preTransformedG.setColor(this.outlineColour);
preTransformedG.setStroke(this.stroke);
preTransformedG.draw(glyph);
}
@Override
public void restoreGraphicsStateAfterDraw(Graphics2D g) {
g.setColor(this.oldGraphicsStateColor);
g.setStroke(this.oldGraphicsStateStroke);
}
}
}
class TextUtilities { // Ideally separate into it's own package rather and then don't use static inner classes, but for this single-file demo, it works.
public static enum CircularTextPosition {
OUTSIDE_CIRCLE {
@Override
double calculateActualRadius(double radius, double maxAscent, double maxDescent, boolean flip) {
return radius;
}
},
BASELINE_ON_CIRCLE {
@Override
double calculateActualRadius(double radius, double maxAscent, double maxDescent, boolean flip) {
return radius-(flip?maxAscent:maxDescent);
}
},
INSIDE_CIRCLE {
@Override
double calculateActualRadius(double radius, double maxAscent, double maxDescent, boolean flip) {
return radius-maxAscent-maxDescent;
}
};
abstract double calculateActualRadius(double radius, double maxAscent, double maxDescent, boolean flip);
}
public static interface GlyphDrawer {
void saveGraphicsStateBeforeDraw(Graphics2D g);
void drawGlyph(Graphics2D preTransformedG, Shape glyph);
void restoreGraphicsStateAfterDraw(Graphics2D g);
}
public static void drawTextAroundCircle(Graphics2D g, String text, int centerAngleInDegrees, boolean squash, CircularTextPosition circularTextPosition, boolean flip, double radius, double centerX, double centerY, Font font, GlyphDrawer glyphDrawer) {
AffineTransform oldTransform = g.getTransform();
glyphDrawer.saveGraphicsStateBeforeDraw(g);
FontMetrics fm = g.getFontMetrics(font);
FontRenderContext frc = g.getFontRenderContext();
char[] characters = text.toCharArray();
int characterCount = characters.length;
if (flip) {
char[] reversedCharacters = new char[characterCount];
for (int index=0; index<characterCount; index++) {
reversedCharacters[index] = characters[characterCount-index-1];
}
characters = reversedCharacters;
}
double maxAscent;
double maxDescent;
if (squash) {
maxAscent = 0;
maxDescent = 0;
} else {
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
}
double spaceCharacterWidth = fm.charWidth('n');
double leading = fm.getLeading();
boolean[] charactersAreSpaces = new boolean[characterCount];
GlyphVector[] characterGlyphVectors = new GlyphVector[characterCount];
Rectangle2D[] characterGlyphBounds = new Rectangle2D[characterCount];
double[] characterWidths = new double[characterCount+1];
double[] characterAscents = new double[characterCount];
double[] characterDescents = new double[characterCount];
for (int index=0; index<characterCount; index++) {
char character = characters[index];
boolean isSpace = character == ' ' || Character.isSpaceChar(character);
GlyphVector glyphVector = font.createGlyphVector(frc, Character.toString(character));
Rectangle2D glyphBounds = glyphVector.getVisualBounds();
double width = isSpace?spaceCharacterWidth:glyphBounds.getWidth();
double ascent = -glyphBounds.getY();
double descent = glyphBounds.getHeight()-ascent;
charactersAreSpaces[index] = isSpace;
characterGlyphVectors[index] = glyphVector;
characterGlyphBounds[index] = glyphBounds;
characterWidths[index] = width;
characterAscents[index] = ascent;
characterDescents[index] = descent;
if (squash) {
maxAscent = Math.max(maxAscent,ascent);
maxDescent = Math.max(maxDescent,descent);
}
}
double actualRadius = circularTextPosition.calculateActualRadius(radius, maxAscent, maxDescent, flip);
double currentAngleInRadians = 0;
double[] characterAnglesInRadians = new double[characterCount];
for (int index=0; index<characterCount; index++) {
characterAnglesInRadians[index] = currentAngleInRadians;
currentAngleInRadians += Math.sin(((characterWidths[index]/2.0) + leading + (characterWidths[index+1]/2.0)) / actualRadius);
}
double angleAdjustment = (centerAngleInDegrees * Math.PI / 180) - ((characterAnglesInRadians[characterCount-1] - characterAnglesInRadians[0]) / 2.0);
for (int index=0; index<characterCount; index++) {
characterAnglesInRadians[index] += angleAdjustment;
}
AffineTransform stringTransform = oldTransform;
if (stringTransform == null) {
stringTransform = new AffineTransform();
}
if (flip) {
stringTransform.rotate(Math.PI, centerX, centerY);
}
for (int index=0; index<characterCount; index++) {
if (!charactersAreSpaces[index]) {
GlyphVector glyphVector = characterGlyphVectors[index];
Rectangle2D glyphBounds = characterGlyphBounds[index];
if (flip) {
AffineTransform oldGlyphVectorTransform = glyphVector.getGlyphTransform(0);
if (oldGlyphVectorTransform == null) {
oldGlyphVectorTransform = new AffineTransform();
}
AffineTransform newGlyphVectorTransform = new AffineTransform(oldGlyphVectorTransform);
newGlyphVectorTransform.scale(-1, -1);
newGlyphVectorTransform.translate(-(glyphBounds.getWidth()+glyphBounds.getX()+glyphBounds.getX()),glyphBounds.getHeight()+(maxAscent-characterAscents[index])-characterDescents[index]-maxDescent);
glyphVector.setGlyphTransform(0, newGlyphVectorTransform);
}
AffineTransform characterTransform = new AffineTransform(stringTransform);
characterTransform.translate(centerX, centerY);
characterTransform.rotate(characterAnglesInRadians[index]);
characterTransform.translate(-((glyphBounds.getX()+(glyphBounds.getWidth()/2))),-(actualRadius+maxDescent));
g.setTransform(characterTransform);
glyphDrawer.drawGlyph(g, glyphVector.getOutline(0, 0));
}
}
glyphDrawer.restoreGraphicsStateAfterDraw(g);
g.setTransform(oldTransform);
}
}
这可以产生(请注意,由于该字母的下降幅度较大,文本中包含“y”):
BASELINE_ON_CIRCLE(未挤压):
INSIDE_CIRCLE(未压扁):
OUTSIDE_CIRCLE(未挤压):
我希望此代码对将来查看此问题的其他人有所帮助。
关于Java - 在圆的边缘、顶部和底部绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161800/
我正在尝试向图像顶部和底部的 ImageView 添加渐变。我不想在 ImageView 之上添加 TextView 。我该如何实现? 最佳答案 看来您简单而干净的解决方案是用 FrameLayout
我可以寻求帮助吗, 我有日期 - “公司名称”和“日期”,例如 $value |"Comp Name"| "Date" | |:----------|----------:| |computer
我有两个表,我试图从中运行查询以返回每个人的最大(或最高)交易。我应该注意,我无法更改表结构。相反,我只能拉数据。 人 +-----------+| id | name |+-----------+|
所以我有一个用管道打开的 n 个流的数组,但是使用 gdb,我发现当我尝试关闭流或管道的写入端时程序失败。我可以很好地写入管道,但关闭它们不起作用。我在程序上运行 valgrind,它所做的只是打印出
大家好,这是我的难题。我正在尝试创建一个标签栏,该标签栏从上到下锚定在左侧,而不是从左到右锚定在底部。我创建了一个工具栏项目,将栏准确地放置在我想要的位置,但我希望选项卡栏相同,具有相同的功能,当然除
http://jsfiddle.net/GuXQZ/3/ header slideshow lates Content
我的图片出了点问题,我无法解决这个问题。这是我的代码.. HTML HIDE CSS #ads { -webkit-border-bottom-right-r
我有一个包含 3(css 网格)列的设计。第二列有嵌套的网格内容需要垂直滚动,而其他两列保持各自的高度。我给第二个嵌套列一个溢出,但我还需要给它一个顶部和底部填充或边距。我的解决方案没有顶部/底部填充
我在 View 中有两个 UIToolbar,分别在顶部和底部。我正在尝试在 iOS 版本中一致地应用外观。从 iOS5 开始有这个 setBackgroundImage: forToolbarPos
一个 div 我使用 top:-26px; 在 css 中设置高度。我有其他 div 其他地方我想与那个 div 对齐。我注意到在 jquery 中编写 .css('top') 得到了我的 css 而
我有这个无序列表 two three 有没有一种方法可以将无序列表添加到无序列表的前面,使其像这样结束? ONE two three 请注意“ONE”已添加到列表
我想检测鼠标何时离开顶部的视口(viewport)(可以说是向北)。我在网上搜了下How can I detect when the mouse leaves the window? .是一个好的开始
运行顶级命令top -c在 Ubuntu 服务器上显示当前正在运行的所有命令。关于 PostgreSQL 命令,括号中的值是什么意思?我说的是图片中红色框旁边的值。 最佳答案 我找不到任何文档来支持这
我想知道将顶部和底部边距添加到 GtkTextView 的正确且普遍接受的方法位于 GtkScrolledWindow 内.有设置左右边距的功能,我正在使用: gtk_text_view_set_le
作为很多“初学者”,我认为使用 TOP_OF_PIPELINE 作为 dst 和 BOTTOM_OF_PIPELINE 作为 src 意味着 ALL_COMMANDS 两者。 Here Nicol B
我正在尝试使用 jQuery/Javascript 解决这个问题: 当浏览器向下滚动且窗口底部到达页脚 DIV 顶部时,执行 CSS 代码更改。 问题示例: https://elodywedding.
我想使用范围 slider 来选择一个值并将该值呈现在 Angular 中的范围选择器顶部。我的html代码是: Raio: {{raio}} metros 在我的 co
我想将手的图片放在靠近脸部的黑色 Canvas 上。这可以吗?有没有办法确定图片的位置? 这是我的代码: var canvas; var canvasContext; window
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我是一名优秀的程序员,十分优秀!