diff --git a/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/ChunkParser.java b/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/ChunkParser.java index 53c6d4a9b..1ab97758c 100644 --- a/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/ChunkParser.java +++ b/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/ChunkParser.java @@ -832,15 +832,13 @@ private List parseTextShowArgument(COSBase argument, StringBuilder unico double shift = - obj.getReal() / 1000 * graphicsState.getTextState().getTextFontSize() * graphicsState.getTextState().getHorizontalScaling(); - if (-obj.getReal() >= TextChunkUtils.TEXT_CHUNK_SPACE_RATIO && StaticStorages.getIsAddSpacesBetweenTextPieces()) { - textPieces.add(new TextPieces.TextPiece(" ", textPieces.getCurrentX(), - textPieces.getCurrentX() + shift)); - } else { - textPieces.shiftCurrentX(shift); - } + textPieces.shiftCurrentX(shift); } } } + double threshold = graphicsState.getTextState().getTextFontSize() * TextChunkUtils.TEXT_LINE_SPACE_RATIO; + textPieces.addSpaces(threshold); + unicodeValue.append(textPieces.getValue()); if (!textPieces.isEmpty()) { textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getStartX(), 0)); @@ -867,17 +865,18 @@ private void parseString(COSString string, StringBuilder unicodeValue, TextPiece " in font" + graphicsState.getTextState().getTextFont().getName()); width = 0.0; } - double shift = (width * - graphicsState.getTextState().getTextFontSize() / 1000 + - graphicsState.getTextState().getCharacterSpacing() + (code == 32 ? + double shift = (graphicsState.getTextState().getCharacterSpacing() + (code == 32 ? graphicsState.getTextState().getWordSpacing() : 0)) * graphicsState.getTextState().getHorizontalScaling(); - String value = graphicsState.getTextState().getTextFont().toUnicode(code); + width = width * + graphicsState.getTextState().getTextFontSize() / 1000 * + graphicsState.getTextState().getHorizontalScaling(); + String value = graphicsState.getTextState().getTextFont().toUnicode(code); if (symbolEnds != null) { if (symbolEnds.isEmpty()) { - TextChunksHelper.updateSymbolEnds(symbolEnds, shift, 0, value != null ? value.length() : 0); + TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, 0, value != null ? value.length() : 0); } else { - TextChunksHelper.updateSymbolEnds(symbolEnds, shift, symbolEnds.get(symbolEnds.size() - 1), + TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, symbolEnds.get(symbolEnds.size() - 1), value != null ? value.length() : 0); } } @@ -892,7 +891,8 @@ private void parseString(COSString string, StringBuilder unicodeValue, TextPiece unicodeValue.append(result); } else { textPieces.add(new TextPieces.TextPiece(result, textPieces.getCurrentX(), - textPieces.getCurrentX() + shift)); + textPieces.getCurrentX() + width)); + textPieces.shiftCurrentX(shift); } } } catch (IOException e) { diff --git a/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/TextPieces.java b/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/TextPieces.java index fcd945bfd..73a8ed36b 100644 --- a/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/TextPieces.java +++ b/wcag-validation/src/main/java/org/verapdf/gf/model/factory/chunks/TextPieces.java @@ -77,6 +77,26 @@ public List getSymbolEnds() { return ends; } + public void addSpaces(double threshold) { + List spaces = new ArrayList<>(); + Iterator validation = textPieces.iterator(); + if (!validation.hasNext()) { + return; + } + TextPiece prev = validation.next(); + double previousEnd = prev.getEndX(); + + while (validation.hasNext()) { + TextPiece piece = validation.next(); + double currentStart = piece.getStartX(); + if (currentStart - previousEnd > threshold) { + spaces.add(new TextPieces.TextPiece(" ", previousEnd, currentStart)); + } + previousEnd = piece.getEndX(); + } + textPieces.addAll(spaces); + } + public static class TextPiece { private final String value; private final double startX; @@ -91,6 +111,10 @@ public TextPiece(String value, double startX, double endX) { public double getEndX() { return endX; } + + public double getStartX() { + return startX; + } } public static class TextPieceComparator implements Comparator {