private final String getTextBestFit(final Context2D context, final String text, final int wide) { double pt = LienzoCore.get().getDefaultFontSize(); String st = LienzoCore.get().getDefaultFontStyle(); String fm = LienzoCore.get().getDefaultFontFamily(); String tf = Text.getFontString(pt, TextUnit.PT, st, fm); context.save(); context.setToIdentityTransform(); while (true) { context.setTextFont(tf); final TextMetrics tm = context.measureText(text); if (tm.getWidth() < wide) { break; } pt = pt - 2; if (pt < 6) { break; } tf = Text.getFontString(pt, TextUnit.PT, st, fm); } context.restore(); return tf; }
/** * Draws the frames of the video. If looping has been set, frames are drawn continuously in a * loop. * * @param context */ @Override protected boolean prepare(final Context2D context, final Attributes attr, final double alpha) { if (m_inits) { init(); if ((null == m_error) && (isAutoPlay())) { play(); } } int wide = getWidth(); int high = getHeight(); if (null != m_error) { if (false == context.isSelection()) { if (wide < 1) { wide = MOVIE_ERROR_WIDE; } if (high < 1) { high = MOVIE_ERROR_HIGH; } context.save(); context.setFillColor(ColorName.BLACK); context.rect(0, 0, wide, high); context.fill(); context.setTextAlign(TextAlign.CENTER); context.setTextBaseline(TextBaseLine.MIDDLE); context.setTextFont(getTextBestFit(context, m_error, wide)); context.setFillColor(ColorName.WHITE); context.rect(0, 0, wide, high); context.clip(); context.fillText(m_error, wide / 2, high / 2); context.restore(); } } else { if ((wide < 1) || (high < 1)) { return false; } if (context.isSelection()) { final String color = getColorKey(); if (null != color) { context.save(); context.setFillColor(color); context.fillRect(0, 0, wide, high); context.restore(); } return false; } if (isEnded()) { if (null != m_postr) { context.save(); context.setGlobalAlpha(alpha); context.drawImage(m_postr, 0, 0, wide, high); context.restore(); } else { String fill = getFillColor(); if (null != fill) { context.save(); context.setGlobalAlpha(alpha); context.setFillColor(fill); context.fillRect(0, 0, wide, high); context.restore(); } } return false; } context.save(); context.setGlobalAlpha(alpha); if ((false == m_xorig) && (m_filters.isActive())) { try { m_canvas.getContext().drawImage(m_video.getElement(), 0, 0, wide, high); m_canvas .getContext() .putImageData( m_filters.filter(m_canvas.getContext().getImageData(0, 0, wide, high), false), 0, 0); context.drawImage(m_canvas.getElement(), 0, 0, wide, high); } catch (Exception e) { // We should only get an exception here if the URL is cross-origin, and getImageData() is // basically a security exception. // ...or other unknown bad things, either way, turn off filtering. DSJ 7/18/2014 context.drawImage(m_video.getElement(), 0, 0, wide, high); m_xorig = true; LienzoCore.get() .error("ERROR: In Movie filtering " + m_video.getSrc() + " " + e.getMessage()); } } else { context.drawImage(m_video.getElement(), 0, 0, wide, high); } context.restore(); } return false; }