Debian Codegraph @

src/com/github/weisj/jsvg/parser/impl/CharacterDataParser.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.parser.impl;
23
24 import org.jetbrains.annotations.NotNull;
25
26 import com.github.weisj.jsvg.logging.Logger;
27 import com.github.weisj.jsvg.logging.Logger.Level;
28 import com.github.weisj.jsvg.logging.impl.LogFactory;
29
30 final class CharacterDataParser {
31 private static final Logger LOGGER = LogFactory.createLogger(CharacterDataParser.class);
32
33 private enum State {
34 SEGMENT_START(false),
35 SEGMENT_BREAK(true),
36 WHITESPACE_AFTER_CHAR(true),
37 WHITESPACE_AFTER_SEGMENT_BREAK(true),
38 CHARACTER(false);
39
40 private final boolean isVisualSpace;
41
42 State(boolean isVisualSpace) {
43 this.isVisualSpace = isVisualSpace;
44 }
45 }
46
47 private State state = State.SEGMENT_START;
48 private StringBuilder buffer = new StringBuilder();
49 // True if any content (including whitespace-only) has been appended since the last flush.
50 // This lets us emit a space segment for inter-element whitespace like <tspan>a</tspan>
51 // <tspan>b</tspan>.
52 private boolean hadAnyContent = false;
53
54 private char[] data;
55 private int begin;
56 private int end;
57
58 public void append(char[] ch, int offset, int length) {
59 if (length == 0) return;
60 hadAnyContent = true;
61 LOGGER.log(Level.DEBUG,
62 () -> String.format("Append: [%s]", new String(ch, offset, length).replace("\n", "\\n")));
63 data = ch;
64 begin = offset;
65 end = offset + length;
66
67 if (isSegmentBreak(data[begin])) {
68 int segmentBreaks = trimLeadingWhiteSpace();
69 if (state == State.SEGMENT_BREAK) segmentBreaks++;
70 if (begin > offset && segmentBreaks > 1) {
71 begin--;
72 data[begin] = ' ';
73 if (state == State.CHARACTER || state == State.SEGMENT_BREAK) {
74 state = State.WHITESPACE_AFTER_CHAR;
75 }
76 }
77 }
78
79 int segmentBreaks = trimTrailingWhiteSpace();
80 if (end < offset + length) {
81 data[end] = segmentBreaks > 0 ? '\n' : ' ';
82 end++;
83 }
84 if (begin >= end) return;
85 LOGGER.log(Level.DEBUG,
86 () -> String.format("Portion: [%s]", new String(ch, begin, end - begin).replace("\n", "\\n")));
87
88 buffer.ensureCapacity(buffer.length() + end - begin);
89 appendData();
90 }
91
92 private void appendData() {
93 int initialOffset = begin;
94 while (begin < end) {
95 char c = data[begin];
96 boolean segmentBreak = isSegmentBreak(c);
97 boolean whiteSpace = isWhitespace(c);
98 if (!segmentBreak && !whiteSpace) {
99 if (state == State.WHITESPACE_AFTER_CHAR
100 || (state.isVisualSpace && begin > initialOffset)) {
101 buffer.append(' ');
102 }
103 state = State.CHARACTER;
104 buffer.append(c);
105 } else if (whiteSpace) {
106 switch (state) {
107 case CHARACTER:
108 case WHITESPACE_AFTER_CHAR:
109 state = State.WHITESPACE_AFTER_CHAR;
110 break;
111 case SEGMENT_BREAK:
112 case WHITESPACE_AFTER_SEGMENT_BREAK:
113 state = State.WHITESPACE_AFTER_SEGMENT_BREAK;
114 break;
115 default:
116 break;
117 }
118 } else {
119 // c is a segment break
120 // This implies we have to **append a whitespace character**
121 // But we only do this if we aren't immediately followed by a closing tag.
122 state = State.SEGMENT_BREAK;
123 }
124 begin++;
125 }
126 }
127
128 public boolean canFlush(boolean dueToSegmentBreak) {
129 if (state == State.SEGMENT_START && !hadAnyContent) return false;
130 return dueToSegmentBreak || buffer.length() > 0;
131 }
132
133 public @NotNull String flush(boolean dueToSegmentBreak) {
134 hadAnyContent = false;
135 if (dueToSegmentBreak && state != State.CHARACTER) {
136 // We ended on a non-character and hence have to insert a whitespace
137 buffer.append(' ');
138 }
139 if (dueToSegmentBreak) state = State.SEGMENT_BREAK;
140 String result = buffer.toString();
141 LOGGER.log(Level.DEBUG, () -> String.format("Flush segBreak=%s[%s]", dueToSegmentBreak, buffer));
142 buffer = new StringBuilder();
143 return result;
144 }
145
146 private int trimLeadingWhiteSpace() {
147 int segmentBreakCount = 0;
148 while (begin < end) {
149 if (isSegmentBreak(data[begin])) {
150 segmentBreakCount++;
151 begin++;
152 } else if (isWhitespace(data[begin])) {
153 begin++;
154 } else {
155 break;
156 }
157 }
158 return segmentBreakCount;
159 }
160
161 private int trimTrailingWhiteSpace() {
162 int segmentBreakCount = 0;
163 while (begin < end) {
164 if (isSegmentBreak(data[end - 1])) {
165 segmentBreakCount++;
166 end--;
167 } else if (isWhitespace(data[end - 1])) {
168 end--;
169 } else {
170 break;
171 }
172 }
173 return segmentBreakCount;
174 }
175
176 private static boolean isSegmentBreak(char c) {
177 return c == '\n' || c == '\r';
178 }
179
180 private static boolean isWhitespace(char c) {
181 return c == ' ' || c == '\t';
182 }
183 }