Skip to content

Commit 9b453e6

Browse files
committed
可以修改 svg 的字体大小
1 parent ee4e405 commit 9b453e6

File tree

4 files changed

+77
-13
lines changed

4 files changed

+77
-13
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,23 @@
5151

5252
SVG后缀是我们推荐的最优调用方案,可以在部分论坛、任何博客、小程序内无损直接调用,并且可以一定程度上控制样式。缺点是部分老旧浏览器不支持。
5353

54-
你可以直接修改svg样式:
54+
你可以直接修改svg控制最大长度
5555
```html
56-
<img src="https://api.gushi.ci/all.svg"
57-
style="max-width:100%; font-size:40px;">
56+
<img src="https://api.gushi.ci/all.svg" style="max-width:100%;">
5857
```
5958

59+
或者使用我们的<b>svg专用</b>的自定义参数
60+
个性化参数
61+
62+
| 说明 | 参数名 | 默认值 | 合法范围 |
63+
| --- | --- | --- | --- |
64+
| 字体大小(px) | font-size | 20 | [8,50] |
65+
| 字体间隔(px) | spacing | 1.5 | [0,30] |
66+
67+
调用示例
68+
```html
69+
<img src="https://api.gushi.ci/all.svg?font-size=18&spacing=4">
70+
```
6071

6172
#### JSON 调用
6273

@@ -127,6 +138,9 @@ Jmeter: 100线程数 每线程循环 1000次,走HTTP
127138

128139
### 更新历史
129140

141+
* 2018.08.09 1.3:
142+
1. svg可以定义字体大小和间隔
143+
130144
* 2018.08.08 1.2:
131145
1. 细节优化
132146

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ma.luan</groupId>
88
<artifactId>yiyan</artifactId>
9-
<version>1.2</version>
9+
<version>1.3</version>
1010

1111
<properties>
1212
<java.version>1.8</java.version>
@@ -118,6 +118,12 @@
118118
<version>2.11.1</version>
119119
</dependency>
120120

121+
<dependency>
122+
<groupId>org.apache.commons</groupId>
123+
<artifactId>commons-text</artifactId>
124+
<version>1.4</version>
125+
</dependency>
126+
121127

122128
</dependencies>
123129

src/main/java/ma/luan/yiyan/api/ApiVerticle.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.vertx.core.Future;
55
import io.vertx.core.eventbus.ReplyException;
66
import io.vertx.core.eventbus.ReplyFailure;
7+
import io.vertx.core.http.HttpServerRequest;
78
import io.vertx.core.http.HttpServerResponse;
89
import io.vertx.core.json.JsonArray;
910
import io.vertx.core.json.JsonObject;
@@ -134,7 +135,9 @@ private void returnGushici(RoutingContext routingContext, String obj, JsonObject
134135
case "svg": {
135136
setCommonHeader(routingContext.response()
136137
.putHeader("Content-Type", "image/svg+xml; charset=utf-8"))
137-
.end(ConvertUtil.getSvg(new JsonObject(obj).getString("content")));
138+
.end(ConvertUtil.getSvg(new JsonObject(obj).getString("content"),
139+
params.getDouble("font-size"),
140+
params.getDouble("spacing")));
138141
break;
139142
}
140143
case "txt": {
@@ -195,9 +198,37 @@ private Future<JsonObject> parseURI(RoutingContext routingContext) {
195198
String format = "".equals(rawFormat) ? "json" : rawFormat;
196199

197200
JsonObject pathParams = new JsonObject();
201+
202+
// svg 额外配置
203+
if ("svg".equals(format)) {
204+
HttpServerRequest request = routingContext.request();
205+
parseAndSet(pathParams,"font-size", request.getParam("font-size")
206+
, 20, 8, 50);
207+
parseAndSet(pathParams,"spacing", request.getParam("spacing")
208+
, 1.5, 0, 30);
209+
}
210+
198211
pathParams.put("categories", categories);
199212
pathParams.put("format", format);
200213
result.complete(pathParams);
201214
return result;
202215
}
216+
217+
private void parseAndSet(JsonObject jsonObject, String paramName,
218+
String value, double defaultValue, double minValue, double maxValue) {
219+
if (value == null) {
220+
jsonObject.put(paramName, defaultValue);
221+
} else {
222+
try {
223+
double i = Double.parseDouble(value);
224+
if (Double.compare(i, minValue) >= 0 && Double.compare(i, maxValue) <= 0) {
225+
jsonObject.put(paramName, i);
226+
} else {
227+
jsonObject.put(paramName, defaultValue);
228+
}
229+
} catch (NumberFormatException ex) {
230+
jsonObject.put(paramName, defaultValue);
231+
}
232+
}
233+
}
203234
}

src/main/java/ma/luan/yiyan/util/ConvertUtil.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import io.vertx.core.buffer.Buffer;
66
import io.vertx.core.eventbus.ReplyException;
77
import io.vertx.core.eventbus.ReplyFailure;
8+
import org.apache.commons.text.StringSubstitutor;
89

910
import java.util.Base64;
11+
import java.util.HashMap;
12+
import java.util.Map;
1013

1114
public class ConvertUtil {
1215
/**
@@ -29,14 +32,24 @@ public static Future<Buffer> getImageFromBase64(String obj) {
2932
}
3033

3134
private static final String svgTemplate =
32-
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"22\" width = \"%.2f\">\n" +
33-
" <!-- Generated by https://api.gushi.ci/ -->\n" +
34-
" <g>\n" +
35-
" <text text-anchor=\"start\" letter-spacing = \"1.5px\" font-smoothing = \"antialiased\" font-family='KaiTi, \"Segoe UI\", \"Lucida Grande\", Helvetica, Arial, \"Microsoft YaHei\", FreeSans, Arimo, \"Droid Sans\",\"wenquanyi micro hei\",\"Hiragino Sans GB\", \"Hiragino Sans GB W3\", sans-serif' font-size=\"20\" id=\"svg_1\" y=\"20\" x=\"0\" stroke-width=\"0\" stroke=\"#000\" fill=\"#000000\">%s</text>\n" +
36-
" </g>\n" +
37-
"</svg>";
35+
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"${height}\" width = \"${width}\">\n" +
36+
" <!-- Generated by https://api.gushi.ci/ -->\n" +
37+
" <g>\n" +
38+
" <text text-anchor=\"start\" letter-spacing = \"${spacing}\" font-smoothing = \"antialiased\" font-family='KaiTi, \"Segoe UI\", \"Lucida Grande\", Helvetica, Arial, \"Microsoft YaHei\", FreeSans, Arimo, \"Droid Sans\",\"wenquanyi micro hei\",\"Hiragino Sans GB\", \"Hiragino Sans GB W3\", sans-serif' font-size=\"${font-size}\" id=\"svg_1\" y=\"${font-size}\" x=\"0\" stroke-width=\"0\" stroke=\"#000\" fill=\"#000000\">${content}</text>\n" +
39+
" </g>\n" +
40+
"</svg>";
3841

39-
public static String getSvg(String content) {
40-
return String.format(svgTemplate,content.length()*20+(content.length()-1)*1.5, content);
42+
public static String getSvg(String content, Double fontSize, Double spacing) {
43+
int length = content.length();
44+
double realWidth = length * fontSize + (length-1) * spacing;
45+
double realFontSize = fontSize;
46+
Map<String, Object> templateValue = new HashMap<>();
47+
templateValue.put("width", realWidth);
48+
templateValue.put("font-size", realFontSize);
49+
templateValue.put("spacing", spacing);
50+
templateValue.put("content", content);
51+
templateValue.put("height", realFontSize * 1.1);
52+
StringSubstitutor sub = new StringSubstitutor(templateValue);
53+
return sub.replace(svgTemplate);
4154
}
4255
}

0 commit comments

Comments
 (0)