Skip to content

Commit 405315c

Browse files
committed
Fix text chunk processing
1 parent fb38d76 commit 405315c

1 file changed

Lines changed: 21 additions & 47 deletions

File tree

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

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

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -810,24 +810,16 @@ private void putChunk(Long mcid, IChunk chunk) {
810810
}
811811
}
812812

813-
private List<Double> parseTextShowArgument(COSBase argument, StringBuilder unicodeValue, Matrix textRenderingMatrix) {
813+
private TextPieces parseTextShowArgument(COSBase argument) {
814+
TextPieces textPieces = new TextPieces();
814815
if (argument.getType() == COSObjType.COS_STRING) {
815-
List<Double> symbolEnds = new ArrayList<>();
816-
symbolEnds.add(0.0);
817-
textRenderingMatrix.concatenate(calculateTextRenderingMatrix());
818-
parseString((COSString) argument.getDirectBase(), unicodeValue, null, symbolEnds);
819-
if (!symbolEnds.isEmpty()) {
820-
textMatrix.concatenate(Matrix.getTranslateInstance(symbolEnds.get(symbolEnds.size() - 1), 0));
821-
}
822-
return symbolEnds;
823-
}
824-
if (argument.getType() == COSObjType.COS_ARRAY) {
816+
parseString((COSString) argument.getDirectBase(), textPieces);
817+
} else if (argument.getType() == COSObjType.COS_ARRAY) {
825818
COSArray array = (COSArray) argument;
826-
TextPieces textPieces = new TextPieces();
827819
for (COSObject obj : array) {
828820
if (obj != null) {
829821
if (obj.getType() == COSObjType.COS_STRING) {
830-
parseString((COSString) obj.getDirectBase(), unicodeValue, textPieces, null);
822+
parseString((COSString) obj.getDirectBase(), textPieces);
831823
} else if (obj.getType().isNumber()) {
832824
double shift = - obj.getReal() / 1000 *
833825
graphicsState.getTextState().getTextFontSize() *
@@ -840,23 +832,11 @@ private List<Double> parseTextShowArgument(COSBase argument, StringBuilder unico
840832
double threshold = graphicsState.getTextState().getTextFontSize() * TextChunkUtils.TEXT_LINE_SPACE_RATIO;
841833
textPieces.addSpaces(threshold);
842834
}
843-
844-
unicodeValue.append(textPieces.getValue());
845-
if (!textPieces.isEmpty()) {
846-
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getStartX(), 0));
847-
}
848-
textRenderingMatrix.concatenate(calculateTextRenderingMatrix());
849-
if (!textPieces.isEmpty()) {
850-
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getEndX() - textPieces.getStartX(), 0));
851-
} else {
852-
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getCurrentX(), 0));
853-
}
854-
return textPieces.getSymbolEnds();
855835
}
856-
return Collections.emptyList();
836+
return textPieces;
857837
}
858838

859-
private void parseString(COSString string, StringBuilder unicodeValue, TextPieces textPieces, List<Double> symbolEnds) {
839+
private void parseString(COSString string, TextPieces textPieces) {
860840
byte[] bytes = string.get();
861841
try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
862842
while (inputStream.available() > 0) {
@@ -874,28 +854,16 @@ private void parseString(COSString string, StringBuilder unicodeValue, TextPiece
874854
graphicsState.getTextState().getTextFontSize() / 1000 *
875855
graphicsState.getTextState().getHorizontalScaling();
876856
String value = graphicsState.getTextState().getTextFont().toUnicode(code);
877-
if (symbolEnds != null) {
878-
if (symbolEnds.isEmpty()) {
879-
TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, 0, value != null ? value.length() : 0);
880-
} else {
881-
TextChunksHelper.updateSymbolEnds(symbolEnds, shift + width, symbolEnds.get(symbolEnds.size() - 1),
882-
value != null ? value.length() : 0);
883-
}
884-
}
885857
String result = value;
886858
if (result == null) {
887859
result = StaticContainers.getIsIgnoreCharactersWithoutUnicode() ? "" : REPLACEMENT_CHARACTER_STRING;
888860
if (StaticContainers.isDataLoader()) {
889861
LOGGER.log(Level.WARNING, "The glyph can not be mapped to Unicode");
890862
}
891863
}
892-
if (textPieces == null) {
893-
unicodeValue.append(result);
894-
} else {
895-
textPieces.add(new TextPieces.TextPiece(result, textPieces.getCurrentX(),
896-
textPieces.getCurrentX() + width));
897-
textPieces.shiftCurrentX(shift);
898-
}
864+
textPieces.add(new TextPieces.TextPiece(result, textPieces.getCurrentX(),
865+
textPieces.getCurrentX() + width));
866+
textPieces.shiftCurrentX(shift);
899867
}
900868
} catch (IOException e) {
901869
LOGGER.log(Level.SEVERE, "Error processing text show operator's string argument : " + new String(bytes), e);
@@ -907,17 +875,23 @@ private TextChunk createTextChunk(List<COSBase> arguments, String operatorType)
907875
COSBase argument = TextChunksHelper.getArgument(arguments, operatorType);
908876
if (font != null && argument != null && (argument.getType() == COSObjType.COS_STRING ||
909877
argument.getType() == COSObjType.COS_ARRAY) && this.textMatrix != null) {
910-
StringBuilder unicodeValue = new StringBuilder();
911-
Matrix textRenderingMatrixBefore = new Matrix();
912-
List<Double> symbolEnds = parseTextShowArgument(argument, unicodeValue, textRenderingMatrixBefore);
878+
TextPieces textPieces = parseTextShowArgument(argument);
879+
if (textPieces.isEmpty()) {
880+
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getCurrentX(), 0));
881+
return null;
882+
}
883+
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getStartX(), 0));
884+
Matrix textRenderingMatrixBefore = calculateTextRenderingMatrix();
885+
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getEndX() - textPieces.getStartX(), 0));
913886
Matrix textRenderingMatrixAfter = calculateTextRenderingMatrix();
887+
textMatrix.concatenate(Matrix.getTranslateInstance(textPieces.getCurrentX() - textPieces.getEndX(), 0));
914888
TextChunk textChunk = new TextChunk(TextChunksHelper.calculateTextBoundingBox(textRenderingMatrixBefore,
915-
textRenderingMatrixAfter, font, pageNumber), unicodeValue.toString(),
889+
textRenderingMatrixAfter, font, pageNumber), textPieces.getValue(),
916890
font.getNameWithoutSubset(), TextChunksHelper.calculateTextSize(textRenderingMatrixAfter),
917891
TextChunksHelper.calculateFontWeight(graphicsState.getTextState().getRenderingMode(), font),
918892
font.getFontDescriptor().getItalicAngle(), TextChunksHelper.calculateTextBaseLine(textRenderingMatrixAfter),
919893
graphicsState.getFillColor(), textRenderingMatrixAfter.getRotationDegree());
920-
textChunk.adjustSymbolEndsToBoundingBox(symbolEnds);
894+
textChunk.adjustSymbolEndsToBoundingBox(textPieces.getSymbolEnds());
921895
return textChunk;
922896
}
923897
return null;

0 commit comments

Comments
 (0)