public static double toPixelValue(final String value, final double defaultValue) { if (isEmpty(value)) { return defaultValue; } return toPixelValue(value); }
private double getCornerRadius(final String rx, final String ry) { final double cx = SVGAttributeParser.toPixelValue(rx, 0d); final double cy = SVGAttributeParser.toPixelValue(ry, 0d); return cx > cy ? cx : cy; } }
public static String rgbToHexString(final int r, final int g, final int b, final int a) { final int rgb = toRGB(r, g, b, a); return rgbToHexString(rgb); }
@Test public void testParseElementSelectorsForElement() { Collection<String> selectors = SVGStyleTranslator.parseElementSelectors(element); assertNotNull(selectors); assertEquals(3, selectors.size()); assertTrue(selectors.contains("#" + ID_ELEMENT)); assertTrue(selectors.contains(".ce1")); assertTrue(selectors.contains(".ce2")); }
public static String rgbToHexString(int rgb) { String hex = Integer.toHexString(rgb & 0xffffff); if (hex.length() < 6) { if (hex.length() == 5) { hex = "0" + hex; } if (hex.length() == 4) { hex = "00" + hex; } if (hex.length() == 3) { hex = "000" + hex; } } hex = "#" + hex; return toHexColorString(hex); }
public static String rgbToHexString(final int r, final int g, final int b) { return rgbToHexString(r, g, b, 1); }
public static String[] getClassNames(final Element element) { final String raw = getStyleDeclaration(element); if (!isEmpty(raw)) { return raw.split(PATTERN_CLASSNAME_SEPARATOR); } return null; }
public static TransformDefinition parseTransformDefinition(final Element element) throws TranslatorException { final String transformRaw = element.getAttribute(TRANSFORM); if (!isEmpty(transformRaw)) { final double[] t = parseTransform(transformRaw); return new TransformDefinitionImpl(t[0], t[1], t[2], t[3]); } return new TransformDefinitionImpl(); }
@Test public void testGetParentsTree() { List<Element> tree = SVGStyleTranslator.getElementsTree(element); assertNotNull(tree); assertEquals(3, tree.size()); assertEquals(element, tree.get(0)); assertEquals(parent, tree.get(1)); assertEquals(root, tree.get(2)); }
public static double toPixelValue(final String value) { if (value.endsWith("px") || value.endsWith("PX")) { return parseDouble(value.substring(0, value.length() - 2)); } return parseDouble(value); }
protected TransformDefinition translateTransforms(final E element, final O def) throws TranslatorException { final TransformDefinition transformDefinition = SVGStyleTranslator.parseTransformDefinition(element); def.setTransformDefinition(transformDefinition); return transformDefinition; }
private static CSSStyleSheetImpl parseElementStyleSheet(final String style) throws TranslatorException { final String declaration = ".shape { " + style + "}"; return parseStyleSheet(new InputSource(new StringReader(declaration))); }
private static ViewDefinition.ViewBoxDefinition build(final String x, final String y, final String width, final String height) { return new ViewBoxDefinitionImpl(SVGAttributeParser.toPixelValue(x, 0d), SVGAttributeParser.toPixelValue(y, 0d), SVGAttributeParser.toPixelValue(width), SVGAttributeParser.toPixelValue(height)); } }
@Test public void testParseElementSelectorsForRoot() { Collection<String> selectors = SVGStyleTranslator.parseElementSelectors(root); assertNotNull(selectors); assertEquals(3, selectors.size()); assertTrue(selectors.contains("#" + ID_ROOT)); assertTrue(selectors.contains(".cr1")); assertTrue(selectors.contains(".cr2")); }
@Test public void testToHexColorString() { assertEquals("#000000", SVGAttributeParser.toHexColorString("#000000")); assertEquals("#123456", SVGAttributeParser.toHexColorString("#123456")); assertEquals("#0000ff", SVGAttributeParser.toHexColorString("#ff")); assertEquals("#0000ff", SVGAttributeParser.toHexColorString("blue")); assertEquals("#ff0000", SVGAttributeParser.toHexColorString("red")); assertEquals("#000000", SVGAttributeParser.toHexColorString("black")); assertEquals("#ff0000", SVGAttributeParser.toHexColorString("rgb(255,0,0)")); assertEquals("#0000ff", SVGAttributeParser.toHexColorString("rgb(0,0,255)")); }
@Override public RectDefinition doTranslate(final Element rectElement, final SVGTranslatorContext context) { final String rx = rectElement.getAttribute(RX); final String ry = rectElement.getAttribute(RY); final String width = rectElement.getAttribute(WIDTH); final String height = rectElement.getAttribute(HEIGHT); return new RectDefinition(getId(rectElement), SVGAttributeParser.toPixelValue(width), SVGAttributeParser.toPixelValue(height), getCornerRadius(rx, ry)); }
@Test public void testParseElementSelectorsForParent() { Collection<String> selectors = SVGStyleTranslator.parseElementSelectors(parent); assertNotNull(selectors); assertEquals(3, selectors.size()); assertTrue(selectors.contains("#" + ID_PARENT)); assertTrue(selectors.contains(".cp1")); assertTrue(selectors.contains(".cp2")); }
@Override public CircleDefinition doTranslate(final Element rectElement, final SVGTranslatorContext context) { final String radius = rectElement.getAttribute(RADIUS); return new CircleDefinition(getId(rectElement), SVGAttributeParser.toPixelValue(radius)); }
protected void translatePosition(final E element, final O def, final SVGTranslatorContext context) throws TranslatorException { final String xr = element.getAttribute(getXAttributeName()); final String yr = element.getAttribute(getYAttributeName()); final double x = SVGAttributeParser.toPixelValue(xr, 0d); final double y = SVGAttributeParser.toPixelValue(yr, 0d); def.setX(x); def.setY(y); }