Commits

Anonymous committed 444e4f4 Draft

fixed italic bold suffixes and font prefixes

  • Participants
  • Parent commits f690d9f

Comments (0)

Files changed (1)

src/main/java/org/xmlcml/graphics/pdf2svg/raw/PDFPage2SVGConverter.java

 package org.xmlcml.graphics.pdf2svg.raw;
 
 import java.awt.BasicStroke;
-
 import java.awt.Color;
 import java.awt.Composite;
 import java.awt.Dimension;
 import org.apache.pdfbox.pdmodel.text.PDTextState;
 import org.apache.pdfbox.util.Matrix;
 import org.apache.pdfbox.util.TextPosition;
+import org.xmlcml.euclid.Angle;
 import org.xmlcml.euclid.Real;
 import org.xmlcml.euclid.Real2;
+import org.xmlcml.euclid.RealArray;
 import org.xmlcml.euclid.Transform2;
 import org.xmlcml.graphics.svg.GraphicsElement.FontStyle;
 import org.xmlcml.graphics.svg.GraphicsElement.FontWeight;
 		currentFontStyle = FontStyle.NORMAL.toString().toLowerCase();
 		if (currentFontFamily != null) {
 			String ff = currentFontFamily.toLowerCase();
-			if (ff.contains("-oblique") || ff.contains("-italic")) {
+			int idx = currentFontFamily.indexOf("-");
+			String suffix = currentFontFamily.substring(idx+1).toLowerCase();
+			if (suffix.equals("oblique") || suffix.equals("italic")) {
 				currentFontStyle = FontStyle.ITALIC.toString().toLowerCase();
+				currentFontFamily = currentFontFamily.substring(0, idx);
 				PDFontDescriptor fd = font.getFontDescriptor();
-				System.out.println("Italic "+fd.getItalicAngle()+" "+currentFontStyle);
 			}
 		}
 	}
 			if (ff.contains("-bold")) {
 				currentFontWeight = FontWeight.BOLD.toString().toLowerCase();
 				PDFontDescriptor fd = font.getFontDescriptor();
-				System.out.println("Weight "+fd.getFontWeight());
 			}
 		}
 	}
 				fontFamily = fontName;
 			}
 		}
+		// strip leading characters (e.g. KAIKCD+Helvetica-Oblique);
+		int index = fontFamily.indexOf("+");
+		if (index != -1) {
+			fontFamily = fontFamily.substring(index+1);
+		}
 		return fontFamily;
 	}
 	
 		double scalex = at.getScaleX();
 		double scaley = at.getScaleY();
 		double scale = Math.sqrt(scalex * scaley);
+		Transform2 t2 = new Transform2(at);
+		Angle angle = t2.getAngleOfRotation();
+		int angleDeg = Math.round((float)angle.getDegrees());
+		if (angleDeg != 0) {
+			System.out.println("Transform "+t2+" "+svgText.getText()+" "+at+" "+getRealArray(fontMatrix));
+			// do this properly later
+			scale = Math.sqrt(Math.abs(t2.elementAt(0, 1)*t2.elementAt(1, 0)));
+			Transform2 t2a = Transform2.getRotationAboutPoint(angle, svgText.getXY());
+			svgText.setTransform(t2a);
+		}
 		if (currentFontSize == null) {
 			currentFontSize = scale;
 		} else if (hasChanged(scale, currentFontSize, eps)) {
 			createNewG = true;
 			currentFontSize = scale;
 		}
-		Transform2 t2 = new Transform2(at);
 		// svgText.setTransform(t2); seems to be already in x,y
 	}
 
+	private RealArray getRealArray(PDMatrix fontMatrix) {
+		double[] dd = new double[9];
+		int kk = 0;
+		int nrow = 2;
+		int ncol = 3;
+		for (int irow = 0; irow < nrow; irow++) {
+			for (int jcol = 0; jcol < ncol; jcol++) {
+				dd[kk++] = fontMatrix.getValue(irow, jcol);
+			}
+		}
+		RealArray ra = new RealArray(dd);
+		return ra;
+	}
+
 	/** changes coordinates because AWT and SVG use top-left origin while PDF uses bottom left
 	 * 
 	 * @param text
 	}
 
 	public void fillPath(int windingRule) throws IOException {
-		PDColorState colorState = getGraphicsState().getNonStrokingColor(); // something's muddled
-//		PDColorState colorState = getGraphicsState().getStrokingColor();
+		PDColorState colorState = getGraphicsState().getNonStrokingColor();
 		Paint currentPaint = getCurrentPaint(colorState, "non-stroking");
 		LOG.trace("non-stroke "+currentPaint);
 		createAndAddSVGPath(windingRule, currentPaint);
 	}
 
+	public void strokePath() throws IOException {
+		PDColorState colorState = getGraphicsState().getStrokingColor(); 
+		Paint currentPaint = getCurrentPaint(colorState, "stroking");
+		createAndAddSVGPath((Integer) null, currentPaint);
+
+	}
+
 	/** processes both stroke and fill for paths
 	 * 
 	 * @param windingRule
 		generalPath.reset();
 	}
 
-	public void strokePath() throws IOException {
-		PDColorState colorState = getGraphicsState().getStrokingColor(); // Something's muddled
-//		PDColorState colorState = getGraphicsState().getNonStrokingColor();
-		Paint currentPaint = getCurrentPaint(colorState, "stroking");
-		createAndAddSVGPath((Integer) null, currentPaint);
-
-	}
-
 	private Paint getCurrentPaint(PDColorState colorState, String type) throws IOException {
 		Paint currentPaint = colorState.getJavaColor();
 		if (currentPaint == null) {