Skip to content

Commit 0b4e02e

Browse files
Fix glyph width calculation and Remove extra spaces (#722)
1 parent 54ac7c6 commit 0b4e02e

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/ChunkParser.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,13 @@ private List<Double> parseTextShowArgument(COSBase argument, StringBuilder unico
832832
double shift = - obj.getReal() / 1000 *
833833
graphicsState.getTextState().getTextFontSize() *
834834
graphicsState.getTextState().getHorizontalScaling();
835-
if (-obj.getReal() >= TextChunkUtils.TEXT_CHUNK_SPACE_RATIO && StaticStorages.getIsAddSpacesBetweenTextPieces()) {
836-
textPieces.add(new TextPieces.TextPiece(" ", textPieces.getCurrentX(),
837-
textPieces.getCurrentX() + shift));
838-
} else {
839-
textPieces.shiftCurrentX(shift);
840-
}
835+
textPieces.shiftCurrentX(shift);
841836
}
842837
}
843838
}
839+
double threshold = graphicsState.getTextState().getTextFontSize() * TextChunkUtils.TEXT_LINE_SPACE_RATIO;
840+
textPieces.addSpaces(threshold);
841+
844842
unicodeValue.append(textPieces.getValue());
845843
if (!textPieces.isEmpty()) {
846844
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getStartX(), 0));
@@ -867,17 +865,18 @@ private void parseString(COSString string, StringBuilder unicodeValue, TextPiece
867865
" in font" + graphicsState.getTextState().getTextFont().getName());
868866
width = 0.0;
869867
}
870-
double shift = (width *
871-
graphicsState.getTextState().getTextFontSize() / 1000 +
872-
graphicsState.getTextState().getCharacterSpacing() + (code == 32 ?
868+
double shift = (graphicsState.getTextState().getCharacterSpacing() + (code == 32 ?
873869
graphicsState.getTextState().getWordSpacing() : 0)) *
874870
graphicsState.getTextState().getHorizontalScaling();
875-
String value = graphicsState.getTextState().getTextFont().toUnicode(code);
871+
width = width *
872+
graphicsState.getTextState().getTextFontSize() / 1000 *
873+
graphicsState.getTextState().getHorizontalScaling();
874+
String value = graphicsState.getTextState().getTextFont().toUnicode(code);
876875
if (symbolEnds != null) {
877876
if (symbolEnds.isEmpty()) {
878-
TextChunksHelper.updateSymbolEnds(symbolEnds, shift, 0, value != null ? value.length() : 0);
877+
TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, 0, value != null ? value.length() : 0);
879878
} else {
880-
TextChunksHelper.updateSymbolEnds(symbolEnds, shift, symbolEnds.get(symbolEnds.size() - 1),
879+
TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, symbolEnds.get(symbolEnds.size() - 1),
881880
value != null ? value.length() : 0);
882881
}
883882
}
@@ -892,7 +891,8 @@ private void parseString(COSString string, StringBuilder unicodeValue, TextPiece
892891
unicodeValue.append(result);
893892
} else {
894893
textPieces.add(new TextPieces.TextPiece(result, textPieces.getCurrentX(),
895-
textPieces.getCurrentX() + shift));
894+
textPieces.getCurrentX() + width));
895+
textPieces.shiftCurrentX(shift);
896896
}
897897
}
898898
} catch (IOException e) {

wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/TextPieces.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ public List<Double> getSymbolEnds() {
7777
return ends;
7878
}
7979

80+
public void addSpaces(double threshold) {
81+
List<TextPiece> spaces = new ArrayList<>();
82+
Iterator<TextPiece> validation = textPieces.iterator();
83+
if (!validation.hasNext()) {
84+
return;
85+
}
86+
TextPiece prev = validation.next();
87+
double previousEnd = prev.getEndX();
88+
89+
while (validation.hasNext()) {
90+
TextPiece piece = validation.next();
91+
double currentStart = piece.getStartX();
92+
if (currentStart - previousEnd > threshold) {
93+
spaces.add(new TextPieces.TextPiece(" ", previousEnd, currentStart));
94+
}
95+
previousEnd = piece.getEndX();
96+
}
97+
textPieces.addAll(spaces);
98+
}
99+
80100
public static class TextPiece {
81101
private final String value;
82102
private final double startX;
@@ -91,6 +111,10 @@ public TextPiece(String value, double startX, double endX) {
91111
public double getEndX() {
92112
return endX;
93113
}
114+
115+
public double getStartX() {
116+
return startX;
117+
}
94118
}
95119

96120
public static class TextPieceComparator implements Comparator<TextPiece> {

0 commit comments

Comments
 (0)