com.caverock.androidsvg.SVGParseException Java Examples

The following examples show how to use com.caverock.androidsvg.SVGParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupConversation.java    From NaviBee with GNU General Public License v3.0 6 votes vote down vote up
public GroupConversation(String id, Date readTimestamp, Date createTimestamp, String name, String icon, Map<String, Boolean> users, String creator) {
    super(id, readTimestamp, createTimestamp);
    this.name = name;
    for (String user:users.keySet()){
        members.add(user);
    }
    this.creator = creator;
    this.createDate = createTimestamp;

    // generate icon using conversation id
    try {
        String hash = HashUtilitiesKt.sha256String(id);
        String svgString = Jdenticon.Companion.toSvg(hash, 256, 0.08f);
        iconBitmap = imageFromString(svgString);
    } catch (SVGParseException e) {
        iconBitmap = null;
    }

}
 
Example #2
Source File: GroupConversation.java    From NaviBee with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap imageFromString(String svgAsString) throws SVGParseException {

        SVG svg = SVG.getFromString(svgAsString);

        // Create a bitmap and canvas to draw onto
        float   svgWidth = (svg.getDocumentWidth() != -1) ? svg.getDocumentWidth() : 500f;
        float   svgHeight = (svg.getDocumentHeight() != -1) ? svg.getDocumentHeight() : 500f;

        Bitmap  newBM = Bitmap.createBitmap(Math.round(svgWidth),
                Math.round(svgHeight),
                Bitmap.Config.ARGB_8888);
        Canvas bmcanvas = new Canvas(newBM);

        // Clear background to white if you want
        bmcanvas.drawRGB(255, 255, 255);

        // Render our document onto our canvas
        svg.renderToCanvas(bmcanvas);

        return newBM;
    }
 
Example #3
Source File: VectorDrawable.java    From Carbon with Apache License 2.0 6 votes vote down vote up
public VectorDrawable(Resources res, int resId) {
    if (resId == 0)
        return;
    try {
        SVG svg = cache.get(resId);
        if (svg == null) {
            svg = SVG.getFromResource(res, resId);
            cache.put(resId, svg);
        }
        float density = res.getDisplayMetrics().density;

        float width = svg.getDocumentViewBox().width();
        float height = svg.getDocumentViewBox().height();

        int intWidth = (int) (width * density);
        int intHeight = (int) (height * density);
        state = new VectorDrawableState(svg, intWidth, intHeight);
        setBounds(0, 0, state.intWidth, state.intHeight);
    } catch (SVGParseException e) {

    }
}
 
Example #4
Source File: UIHelpers.java    From proofmode with GNU General Public License v3.0 6 votes vote down vote up
public static void populateContainerWithSVG(View rootView, int offsetX, int idSVG, int idContainer) {
	try {
		SVG svg = SVG.getFromResource(rootView.getContext(), idSVG);
		if (offsetX != 0) {
			RectF viewBox = svg.getDocumentViewBox();
			viewBox.offset(offsetX, 0);
			svg.setDocumentViewBox(viewBox.left, viewBox.top, viewBox.width(), viewBox.height());
		}
		SVGImageView svgImageView = new SVGImageView(rootView.getContext());
		svgImageView.setFocusable(false);
		svgImageView.setFocusableInTouchMode(false);
		svgImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		svgImageView.setSVG(svg);
		svgImageView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		ViewGroup layout = (ViewGroup) rootView.findViewById(idContainer);
		layout.addView(svgImageView);
	} catch (SVGParseException e) {
		e.printStackTrace();
	}
}
 
Example #5
Source File: SvgMediaDecoder.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final float w = svg.getDocumentWidth();
    final float h = svg.getDocumentHeight();
    final float density = resources.getDisplayMetrics().density;

    final int width = (int) (w * density + .5F);
    final int height = (int) (h * density + .5F);

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    final Canvas canvas = new Canvas(bitmap);
    canvas.scale(density, density);
    svg.renderToCanvas(canvas);

    return new BitmapDrawable(resources, bitmap);
}
 
Example #6
Source File: CustomImageView.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
private void setSvg(String svgData)
{
    svg = null;
    try
    {
        svg = SVG.getFromString(svgData);
        originalWidth = (int) svg.getDocumentWidth();
        originalHeight = (int) svg.getDocumentHeight();
        svg.setDocumentWidth("100%");
        svg.setDocumentHeight("100%");
        svg.setDocumentViewBox(0, 0, originalWidth, originalHeight);
    }
    catch (SVGParseException e)
    {
        // nothing to do
    }
    if (svg != null)
    {
        this.svgData = svgData;
    }
    imageType = this.svg == null ? ImageType.NONE : ImageType.SVG;
}
 
Example #7
Source File: SvgHelper.java    From road-trip with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #8
Source File: SvgDecoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example #9
Source File: SvgHelper.java    From google-io-2014 with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #10
Source File: SvgHelper.java    From Android-Anim-Playground with MIT License 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #11
Source File: SvgHelper.java    From google-io-2014-compat with Apache License 2.0 5 votes vote down vote up
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #12
Source File: SvgDecoderExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
    return new CloseableSvgImage(svg);
  } catch (SVGParseException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example #13
Source File: SvgUtils.java    From android-pathview with Apache License 2.0 5 votes vote down vote up
/**
 * Loading the svg from the resources.
 *
 * @param context     Context object to get the resources.
 * @param svgResource int resource id of the svg.
 */
public void load(Context context, int svgResource) {
    if (mSvg != null) 
        return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #14
Source File: SvgDecoder.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
    try {
        SVG svg = SVG.getFromInputStream(source);
        return new SimpleResource<SVG>(svg);
    } catch (SVGParseException ex) {
        throw new IOException("Cannot load SVG from stream", ex);
    }
}
 
Example #15
Source File: SvgUtils.java    From ACDD with MIT License 5 votes vote down vote up
/**
 * Loading the svg from the resources.
 *
 * @param context     Context object to get the resources.
 * @param svgResource int resource id of the svg.
 */
public void load(Context context, int svgResource) {
    if (mSvg != null) return;
    try {
        mSvg = SVG.getFromResource(context, svgResource);
        mSvg.setDocumentPreserveAspectRatio(PreserveAspectRatio.UNSCALED);
    } catch (SVGParseException e) {
        Log.e(LOG_TAG, "Could not load specified SVG resource", e);
    }
}
 
Example #16
Source File: SvgPictureMediaDecoder.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {

    final SVG svg;
    try {
        svg = SVG.getFromInputStream(inputStream);
    } catch (SVGParseException e) {
        throw new IllegalStateException("Exception decoding SVG", e);
    }

    final Picture picture = svg.renderToPicture();
    return new PictureDrawable(picture);
}
 
Example #17
Source File: SVGMediaMetadataRetriever.java    From MediaMetadataRetrieverCompat with MIT License 5 votes vote down vote up
@Override
public void setDataSource(@NonNull DataSource source) throws IOException {
    try {
        mSVG = SVG.getFromInputStream(source.toStream());
    } catch (SVGParseException e) {
        e.printStackTrace();
    }
}
 
Example #18
Source File: FileDescriptorSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(
        @NonNull FileDescriptor source,
        int width,
        int height,
        @NonNull Options options
) throws SvgParseException {
    try {
        return SvgUtils.getSvg(source);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #19
Source File: StringSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull String source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromString(source);
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #20
Source File: RawResourceSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull Uri source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromResource(mResources, ResourceUtils.getRawResourceId(mResources, source));
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #21
Source File: FileSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull File source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SvgUtils.getSvg(source);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #22
Source File: InputStreamSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull InputStream source, int width, int height, @NonNull Options options) throws SvgParseException {
    try {
        return SVG.getFromInputStream(source);
    } catch (SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #23
Source File: ByteBufferSvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@Override
SVG loadSvg(@NonNull ByteBuffer source, int width, int height, @NonNull Options options) throws SvgParseException {
    try (InputStream is = ByteBufferUtil.toStream(source)) {
        return SVG.getFromInputStream(is);
    } catch (IOException | SVGParseException e) {
        throw new SvgParseException(e);
    }
}
 
Example #24
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static SVG getSvg(@NonNull FileDescriptor descriptor)
        throws SVGParseException, IOException {

    try (InputStream is = new BufferedInputStream(new FileInputStream(descriptor))) {
        return SVG.getFromInputStream(is);
    }
}
 
Example #25
Source File: SvgUtils.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
public static SVG getSvg(@NonNull File file) throws SVGParseException, IOException {
    if (!file.exists()) {
        throw new FileNotFoundException("File: '" + file.getAbsolutePath() + "' not exists");
    }

    try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
        return SVG.getFromInputStream(is);
    }
}
 
Example #26
Source File: SvgDecoder.java    From GlideToVectorYou with Apache License 2.0 5 votes vote down vote up
public Resource<SVG> decode(@NonNull InputStream source, int width, int height,
                            @NonNull Options options)
    throws IOException {
  try {
    SVG svg = SVG.getFromInputStream(source);
    return new SimpleResource<>(svg);
  } catch (SVGParseException ex) {
    throw new IOException("Cannot load SVG from stream", ex);
  }
}
 
Example #27
Source File: XulSVGDrawable.java    From starcor.xul with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static XulDrawable buildSVGDrawable(InputStream stream, String url, String imageKey, int width, int height) {
	if (stream == null) {
		return null;
	}

	XulSVGDrawable drawable = new XulSVGDrawable();

	try {
		drawable._svg = SVG.getFromInputStream(stream);
	} catch (SVGParseException e) {
		e.printStackTrace();
		return null;
	}

	float offsetX = 0;
	float offsetY = 0;

	float xScalar = 1.0f;
	float yScalar = 1.0f;

	RectF documentViewBox = drawable._svg.getDocumentViewBox();
	if (documentViewBox == null) {
		float documentWidth = drawable._svg.getDocumentWidth();
		float documentHeight = drawable._svg.getDocumentHeight();
		if (documentWidth <= 0 && documentHeight <= 0) {
			documentWidth = 256;
			documentHeight = 256;
			drawable._svg.setDocumentWidth(documentWidth);
			drawable._svg.setDocumentHeight(documentHeight);
		}
		drawable._svg.setDocumentViewBox(0, 0, documentWidth, documentHeight);
		documentViewBox = drawable._svg.getDocumentViewBox();
	}

	int docWidth = XulUtils.roundToInt(documentViewBox.width());
	int docHeight = XulUtils.roundToInt(documentViewBox.height());
	if (width == 0 && height == 0) {
		width = docWidth;
		height = docHeight;
	} else if (width == 0) {
		xScalar = yScalar = (float)(height)/docHeight;
		width = XulUtils.roundToInt(docWidth*xScalar);
		docWidth = width;
		docHeight = height;
	} else if (height == 0) {
		xScalar = yScalar = (float)(width)/docWidth;
		height = XulUtils.roundToInt(docHeight*xScalar);
		docWidth = width;
		docHeight = height;
	} else {
		float wScalar = (float) width / docWidth;
		float hScalar = (float) height / docHeight;
		if (wScalar > hScalar) {
			docWidth *= wScalar;
			docHeight *= wScalar;
			xScalar = yScalar = wScalar;
		} else {
			docWidth *= hScalar;
			docHeight *= hScalar;
			xScalar = yScalar = hScalar;
		}
		offsetX = (docWidth - width) / 2.0f;
		offsetY = (docHeight - height) / 2.0f;
	}

	XulUtils.ticketMarker ticketMarker = new XulUtils.ticketMarker("BENCH!!!", true);
	ticketMarker.mark();

	drawable._cachedBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	Canvas c = new Canvas(drawable._cachedBmp);
	c.translate(-offsetX, -offsetY);
	c.scale(xScalar, yScalar);
	drawable._svg.renderToCanvas(c, documentViewBox);

	ticketMarker.mark("svg");
	Log.d("BENCH", ticketMarker.toString());

	drawable._url = url;
	drawable._key = imageKey;
	return drawable;
}