• R/O
  • SSH
  • HTTPS

pdf-brewer: コミット


コミットメタ情報

リビジョン13 (tree)
日時2020-02-24 15:56:37
作者hirukawa_ryo

ログメッセージ

* pdf-brewer 0.4
あふれた文字列の行末処理を指定する命令 \text-overflow を追加しました。wrap を指定すると従来通り行の折り返しをおこないます。truncate を指定すると折り返さずに残りの文字を切り捨てます。ellipsis を指定すると折り返さずに残りの文字を切り捨て行末に省略記号(…)を出力します。\text-overflow を指定しない場合の既定値は wrap です。

変更サマリ

差分

--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/BrewerData.java (revision 12)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/BrewerData.java (revision 13)
@@ -17,6 +17,7 @@
1717 import java.util.Map.Entry;
1818 import java.util.Scanner;
1919
20+import net.osdn.pdf_brewer.instruction.text.TextOverflow;
2021 import org.apache.pdfbox.pdmodel.common.PDRectangle;
2122
2223 import net.osdn.pdf_brewer.instruction.Align;
@@ -115,6 +116,8 @@
115116 }
116117 } else if(first.equals("text-align")) {
117118 instructions.add(new TextAlign(indent, tokens));
119+ } else if(first.equals("text-overflow")) {
120+ instructions.add(new TextOverflow(indent, tokens));
118121 }
119122 } catch(IllegalArgumentException e) {
120123 e.printStackTrace();
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Context.java (revision 12)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Context.java (revision 13)
@@ -22,6 +22,7 @@
2222 private float fontSize;
2323 private float lineHeight; //行の高さ倍率
2424 private Horizontal textAlign;
25+ private Overflow textOverflow;
2526
2627 private float lineWidth; //RectやLineで線を描画するときの線の太さ
2728 private int lineStyle;
@@ -44,6 +45,7 @@
4445 fontSize = 14.0f;
4546 lineHeight = 1.8f;
4647 textAlign = Horizontal.Left;
48+ textOverflow = Overflow.Wrap;
4749
4850 lineWidth = LineStyle.WIDTH_MEDIUM;
4951 lineStyle = LineStyle.LINE_SOLID;
@@ -67,6 +69,7 @@
6769 fontSize = parent.getFontSize();
6870 lineHeight = parent.getLineHeight();
6971 textAlign = parent.getTextAlignment();
72+ textOverflow = parent.getTextOverflow();
7073
7174 lineWidth = parent.getLineWidth();
7275 lineStyle = parent.getLineStyle();
@@ -175,7 +178,15 @@
175178 public void setTextAlignment(Horizontal textAlign) {
176179 this.textAlign = textAlign;
177180 }
178-
181+
182+ public Overflow getTextOverflow() {
183+ return textOverflow;
184+ }
185+
186+ public void setTextOverflow(Overflow textOverflow) {
187+ this.textOverflow = textOverflow;
188+ }
189+
179190 public float getLineWidth() {
180191 return lineWidth;
181192 }
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Overflow.java (nonexistent)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/Overflow.java (revision 13)
@@ -0,0 +1,7 @@
1+package net.osdn.pdf_brewer;
2+
3+public enum Overflow {
4+ Wrap,
5+ Truncate,
6+ Ellipsis,
7+}
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/TextBuffer.java (revision 12)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/TextBuffer.java (revision 13)
@@ -4,6 +4,7 @@
44 import java.util.ArrayList;
55 import java.util.List;
66
7+import net.osdn.pdf_brewer.instruction.text.TextOverflow;
78 import org.apache.pdfbox.pdmodel.PDPageContentStream;
89 import org.apache.pdfbox.pdmodel.font.PDFont;
910
@@ -47,10 +48,15 @@
4748 ops.add(new LineHeightOp(lineHeightInstruction.getLineHeight()));
4849 }
4950 } else if(instruction instanceof TextAlign) {
50- TextAlign textAlignInstruction = (TextAlign)instruction;
51- if(textAlignInstruction.getTextAlignment() != null) {
51+ TextAlign textAlignInstruction = (TextAlign) instruction;
52+ if (textAlignInstruction.getTextAlignment() != null) {
5253 ops.add(new TextAlignOp(textAlignInstruction.getTextAlignment()));
5354 }
55+ } else if(instruction instanceof TextOverflow) {
56+ TextOverflow textOverflowInstruction = (TextOverflow)instruction;
57+ if(textOverflowInstruction.getTextOverflow() != null) {
58+ ops.add(new TextOverflowOp(textOverflowInstruction.getTextOverflow()));
59+ }
5460 } else {
5561
5662 }
@@ -70,6 +76,10 @@
7076 if(textAlign == null) {
7177 textAlign = Horizontal.Left;
7278 }
79+ Overflow textOverflow = context.getTextOverflow();
80+ if(textOverflow == null) {
81+ textOverflow = Overflow.Wrap;
82+ }
7383 float fontSize = context.getFontSize();
7484 float lineHeight = context.getLineHeight();
7585 boolean isHeightChanged = true;
@@ -110,14 +120,47 @@
110120 leading[lineNumber] = l;
111121 }
112122 }
113- ops2.add(new TextOp(result.text1));
114- lineWidth[lineNumber] += result.width;
115- rest -= result.width;
116123 if(result.text2 == null) {
117124 // 改行の必要なくすべて出力可能。
125+ ops2.add(new TextOp(result.text1));
126+ lineWidth[lineNumber] += result.width;
127+ rest -= result.width;
128+
129+ // 続く行(text2)が存在しないので処理を終了します。
118130 text = null;
131+ } else if(textOverflow == Overflow.Truncate) {
132+ // 切り捨て(Truncate)が指定されている。
133+ ops2.add(new TextOp(result.text1));
134+ lineWidth[lineNumber] += result.width;
135+ rest -= result.width;
136+
137+ // 続く行(text2)が残っていますが処理せずに終了します。(切り捨て)
138+ text = null;
139+ } else if(textOverflow == Overflow.Ellipsis) {
140+ // 省略(Ellipsis)が指定されている。
141+ // 行末に省略記号を追加します。余白が不足している場合は、result.text1 の末尾から1文字ずつ削っていきます。
142+ String ellipsisChar = "\u2026";
143+ float ellipsisWidth = getStringWidth(font, fontSize, ellipsisChar);
144+ while(result.text1.length() >= 1 && rest - result.width - ellipsisWidth < 0.0f) {
145+ result.text1 = result.text1.substring(0, result.text1.length() - 1);
146+ result.width = getStringWidth(font, fontSize, result.text1);
147+ }
148+ if(rest - result.width - ellipsisWidth >= 0.0f) {
149+ result.text1 += ellipsisChar;
150+ result.width = getStringWidth(font, fontSize, result.text1);
151+ }
152+ ops2.add(new TextOp(result.text1));
153+ lineWidth[lineNumber] += result.width;
154+ rest -= result.width;
155+
156+ // 続く行(text2)が残っていますが処理せずに終了します。(省略)
157+ text = null;
119158 } else {
120159 // テキストの一部を出力可能。残りを出力するために改行が必要。
160+ ops2.add(new TextOp(result.text1));
161+ lineWidth[lineNumber] += result.width;
162+ rest -= result.width;
163+
121164 float w = lineWidth[lineNumber];
122165 float h = lineNumber == 0 ? fontHeight[0] : leading[lineNumber];
123166 ops2.add(new NewLineOp(w, h));
@@ -149,11 +192,11 @@
149192 ops2.add(fontOp);
150193 isHeightChanged = true;
151194 } else if(op instanceof TextAlignOp) {
152- TextAlignOp textAlignOp = (TextAlignOp)op;
153- if(textAlignOp.textAlign != null) {
154- if(textAlign != textAlignOp.textAlign) {
195+ TextAlignOp textAlignOp = (TextAlignOp) op;
196+ if (textAlignOp.textAlign != null) {
197+ if (textAlign != textAlignOp.textAlign) {
155198 textAlign = textAlignOp.textAlign;
156- if(lineWidth[lineNumber] > 0f) {
199+ if (lineWidth[lineNumber] > 0f) {
157200 float w = lineWidth[lineNumber];
158201 float h = lineNumber == 0 ? fontHeight[0] : leading[lineNumber];
159202 ops2.add(new NewLineOp(w, h));
@@ -164,6 +207,12 @@
164207 }
165208 ops2.add(textAlignOp);
166209 }
210+ } else if(op instanceof TextOverflowOp) {
211+ TextOverflowOp textOverflowOp = (TextOverflowOp)op;
212+ if(textOverflowOp.textOverflow != null) {
213+ textOverflow = textOverflowOp.textOverflow;
214+ context.setTextOverflow(textOverflow);
215+ }
167216 } else if(op instanceof LineHeightOp) {
168217 LineHeightOp lineHeightOp = (LineHeightOp)op;
169218 if(lineHeightOp.lineHeight != Float.NaN && lineHeightOp.lineHeight >= 0.0) {
@@ -385,6 +434,14 @@
385434 this.textAlign = textAlign;
386435 }
387436 }
437+
438+ private static class TextOverflowOp extends Op {
439+ public Overflow textOverflow;
440+
441+ public TextOverflowOp(Overflow textOverflow) {
442+ this.textOverflow = textOverflow;
443+ }
444+ }
388445
389446 private static class LineHeightOp extends Op {
390447 public float lineHeight;
--- pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/instruction/text/TextOverflow.java (nonexistent)
+++ pdf-brewer/trunk/src/main/java/net/osdn/pdf_brewer/instruction/text/TextOverflow.java (revision 13)
@@ -0,0 +1,32 @@
1+package net.osdn.pdf_brewer.instruction.text;
2+
3+import net.osdn.pdf_brewer.Overflow;
4+
5+import java.util.List;
6+
7+public class TextOverflow extends TextBufferingInstruction {
8+
9+ private Overflow textOverflow;
10+
11+ public TextOverflow(int indent, List<Object> params) {
12+ super(indent, params);
13+
14+ for(int i = 0; i < params.size(); i++) {
15+ Object obj = params.get(i);
16+ if(obj instanceof String) {
17+ String s = ((String)obj).toLowerCase();
18+ if(s.equals("wrap")) {
19+ textOverflow = Overflow.Wrap;
20+ } else if(s.equals("truncate")) {
21+ textOverflow = Overflow.Truncate;
22+ } else if(s.equals("ellipsis")) {
23+ textOverflow = Overflow.Ellipsis;
24+ }
25+ }
26+ }
27+ }
28+
29+ public Overflow getTextOverflow() {
30+ return textOverflow;
31+ }
32+}
旧リポジトリブラウザで表示