Exemple #1
0
WRasterImage::WRasterImage(const std::string& type,
			   const WLength& width, const WLength& height,
			   WObject *parent)
  : WResource(parent),
    width_(width),
    height_(height),
    painter_(0),
    impl_(new Impl)
{
  impl_->type_ = type;
  impl_->w_ = static_cast<unsigned long>(width.toPixels());
  impl_->h_ = static_cast<unsigned long>(height.toPixels());
  
  if (!impl_->w_ || !impl_->h_) {
    impl_->bitmap_ = 0;
    return;
  }
  
  impl_->bitmap_ = new SkBitmap();
  impl_->bitmap_->setConfig(SkBitmap::kARGB_8888_Config, impl_->w_, impl_->h_);
  impl_->bitmap_->allocPixels();
  impl_->bitmap_->eraseARGB(0, 0, 0, 0);
  impl_->device_ = new SkBitmapDevice(*impl_->bitmap_);
  impl_->canvas_ = new SkCanvas(impl_->device_);
  
  impl_->textPaint_.setStyle(SkPaint::kStrokeAndFill_Style);
  impl_->textPaint_.setTextEncoding(SkPaint::kUTF8_TextEncoding);
  impl_->strokePaint_.setStyle(SkPaint::kStroke_Style);
  impl_->fillPaint_.setStyle(SkPaint::kFill_Style);
  
}
Exemple #2
0
void WPaintedWidget::resize(const WLength& width, const WLength& height)
{
  if (!width.isAuto() && !height.isAuto()) {
    setLayoutSizeAware(false);
    resizeCanvas(static_cast<int>(width.toPixels()),
		 static_cast<int>(height.toPixels()));
  }

  WInteractWidget::resize(width, height);
}
Exemple #3
0
int StdGridLayoutImpl2::pixelSize(const WLength& size)
{
  if (size.unit() == WLength::Percentage)
    return 0;
  else
    return (int)size.toPixels();
}
Exemple #4
0
void PaintedSlider::sliderResized(const WLength& width, const WLength& height)
{
  if (slider_->orientation() == Orientation::Horizontal) {
    WLength w = width;
    if (!w.isAuto())
      w = WLength(w.toPixels() - 10);

    resize(w, height);
  } else {
    WLength h = height;
    if (!h.isAuto())
      h = WLength(h.toPixels() - 10);

    resize(width, h);    
  }

  updateState();
}
Exemple #5
0
void WPdfImage::setChanged(WFlags<PainterChangeFlag> flags)
{
  if (!flags.empty()) {
    HPDF_Page_GRestore(page_);
    HPDF_Page_GSave(page_);

    HPDF_ExtGState gstate;
    gstate = HPDF_CreateExtGState (pdf_);

    currentFont_ = WFont();

    if (painter()->hasClipping()) {
      const WTransform& t = painter()->clipPathTransform();

      if (!painter()->clipPath().isEmpty()) {
        applyTransform(t);

        drawPlainPath(painter()->clipPath());
        HPDF_Page_Clip(page_);
        HPDF_Page_EndPath(page_);

        applyTransform(t.inverted());
      }
    }

    applyTransform(painter()->combinedTransform());

    const WPen& pen = painter()->pen();

    if (pen.style() != PenStyle::None) {
      const WColor& color = pen.color();

      HPDF_Page_SetRGBStroke(page_,
                             color.red() / 255.,
                             color.green() / 255.,
                             color.blue() / 255.);

      HPDF_ExtGState_SetAlphaStroke (gstate, color.alpha()/255.);

      WLength w = painter()->normalizedPenWidth(pen.width(), false);
      HPDF_Page_SetLineWidth(page_, w.toPixels());

      switch (pen.capStyle()) {
      case PenCapStyle::Flat:
	HPDF_Page_SetLineCap(page_, HPDF_BUTT_END);
	break;
      case PenCapStyle::Square:
	HPDF_Page_SetLineCap(page_, HPDF_PROJECTING_SCUARE_END); // scuary !
	break;
      case PenCapStyle::Round:
	HPDF_Page_SetLineCap(page_, HPDF_ROUND_END);
	break;
      }

      switch (pen.joinStyle()) {
      case PenJoinStyle::Miter:
	HPDF_Page_SetLineJoin(page_, HPDF_MITER_JOIN);
	break;
      case PenJoinStyle::Bevel:
	HPDF_Page_SetLineJoin(page_, HPDF_BEVEL_JOIN);
	break;
      case PenJoinStyle::Round:
	HPDF_Page_SetLineJoin(page_, HPDF_ROUND_JOIN);
	break;
      }

      switch (pen.style()) {
      case PenStyle::None:
	break;
      case PenStyle::SolidLine:
	HPDF_Page_SetDash(page_, nullptr, 0, 0);
	break;
      case PenStyle::DashLine: {
	const HPDF_UINT16 dash_ptn[] = { 4, 2 };
	HPDF_Page_SetDash(page_, dash_ptn, 2, 0);
	break;
      }
      case PenStyle::DotLine: {
	const HPDF_UINT16 dash_ptn[] = { 1, 2 };
	HPDF_Page_SetDash(page_, dash_ptn, 2, 0);
	break;
      }
      case PenStyle::DashDotLine: {
	const HPDF_UINT16 dash_ptn[] = { 4, 2, 1, 2 };
	HPDF_Page_SetDash(page_, dash_ptn, 4, 0);
	break;
      }
      case PenStyle::DashDotDotLine: {
	const HPDF_UINT16 dash_ptn[] = { 4, 2, 1, 2, 1, 2 };
	HPDF_Page_SetDash(page_, dash_ptn, 6, 0);
	break;
      }
      }
    }

    const WBrush& brush = painter()->brush();

    if (brush.style() != BrushStyle::None) {
      const WColor& color = painter()->brush().color();

      HPDF_Page_SetRGBFill(page_,
                           color.red() / 255.,
                           color.green() / 255.,
                           color.blue() / 255.);

      HPDF_ExtGState_SetAlphaFill (gstate, color.alpha()/255.);
    }

    HPDF_Page_SetExtGState (page_, gstate);

    

    const WFont& font = painter()->font();

    if (font == currentFont_ && !trueTypeFonts_->busy())
      return;

    /*
     * First, try a true type font.
     */
    std::string ttfFont;
    if (trueTypeFonts_->busy()) {
      /*
       * We have a resolved true type font.
       */
      ttfFont = trueTypeFonts_->drawingFontPath();
    } else {
      FontSupport::FontMatch match = trueTypeFonts_->matchFont(font);

      if (match.matched())
	ttfFont = match.fileName();
    }

    LOG_DEBUG("font: " << ttfFont);

    if (font == currentFont_ &&
        !ttfFont.empty() &&
        currentTtfFont_ == ttfFont)
      return;

    currentFont_ = font;

    const char *font_name = nullptr;
    font_ = nullptr;

    if (!ttfFont.empty()) {

      bool fontOk = false;

      std::map<std::string, const char *>::const_iterator i
	= ttfFonts_.find(ttfFont);

      if (i != ttfFonts_.end()) {
	font_name = i->second;
	fontOk = true;
      } else if (ttfFont.length() > 4) {
	std::string suffix
	  = Utils::lowerCase(ttfFont.substr(ttfFont.length() - 4));

	if (suffix == ".ttf") {
	  font_name = HPDF_LoadTTFontFromFile (pdf_, ttfFont.c_str(),
					       HPDF_TRUE);
	} else if (suffix == ".ttc") {
	  /* Oops, pango didn't tell us which font to load ... */
	  font_name = HPDF_LoadTTFontFromFile2(pdf_, ttfFont.c_str(),
					       0, HPDF_TRUE);
	}

	if (!font_name)
	  HPDF_ResetError (pdf_);
	else {
	  ttfFonts_[ttfFont] = font_name;
	  fontOk = true;
	}
      }

      if (!fontOk)
	LOG_ERROR("cannot read font: '" << ttfFont << "': "
		  "expecting a true type font (.ttf, .ttc)");
    }

    if (!font_ && font_name) {
      font_ = HPDF_GetFont (pdf_, font_name, "UTF-8");

      if (!font_)
	HPDF_ResetError (pdf_);
      else {
	trueTypeFont_ = true;
        currentTtfFont_ = ttfFont;
      }
    }

    if (!font_) {
      trueTypeFont_ = false;
      currentTtfFont_.clear();

      std::string name = Pdf::toBase14Font(font);
      font_ = HPDF_GetFont(pdf_, name.c_str(), nullptr);
    }

    fontSize_ = font.sizeLength(12).toPixels();

    HPDF_Page_SetFontAndSize (page_, font_, fontSize_);
  }
}
Exemple #6
0
void WRasterImage::setChanged(WFlags<ChangeFlag> flags)
{
  if (flags & Clipping) {
    
    if (painter()->hasClipping()) {
      impl_->setTransform(painter()->clipPathTransform());
      SkPath clipPath;
      impl_->drawPlainPath(clipPath, painter()->clipPath());
      impl_->canvas_->clipPath(clipPath, SkRegion::kReplace_Op);
    } else {
      impl_->canvas_->restore();
      impl_->canvas_->save();
    }
    impl_->setTransform(painter()->combinedTransform());
  }

  if (flags & Transform) {
    impl_->setTransform(painter()->combinedTransform());
    flags = Pen | Brush | Font | Hints;
  }

  if (flags & Hints) {
    if (!(painter()->renderHints() & WPainter::Antialiasing)) {
      impl_->strokePaint_.setAntiAlias(false);
      impl_->fillPaint_.setAntiAlias(false);
      impl_->textPaint_.setAntiAlias(false);
    } else {
      impl_->strokePaint_.setAntiAlias(true);
      impl_->fillPaint_.setAntiAlias(true);
      impl_->textPaint_.setAntiAlias(true);
    }
  }

  if (flags & Pen) {
    const WPen& pen = painter()->pen();

    if (pen.style() != NoPen) {
      const WColor& color = pen.color();

      impl_->strokePaint_.setColor(fromWColor(color));

      WLength w = pen.width();
      impl_->strokePaint_.setStrokeWidth(SkIntToScalar(w.toPixels()));

      switch (pen.capStyle()) {
      case FlatCap:
	impl_->strokePaint_.setStrokeCap(SkPaint::kButt_Cap);
	break;
      case SquareCap:
	impl_->strokePaint_.setStrokeCap(SkPaint::kSquare_Cap);
	break;
      case RoundCap:
	impl_->strokePaint_.setStrokeCap(SkPaint::kRound_Cap);
	break;
      }

      switch (pen.joinStyle()) {
      case MiterJoin:
	impl_->strokePaint_.setStrokeJoin(SkPaint::kMiter_Join);
	break;
      case BevelJoin:
	impl_->strokePaint_.setStrokeJoin(SkPaint::kBevel_Join);
	break;
      case RoundJoin:
	impl_->strokePaint_.setStrokeJoin(SkPaint::kRound_Join);
	break;
      }

      SkPathEffect *pe = impl_->strokePaint_.setPathEffect(0);
      if (pe)
	pe->unref();
      switch (pen.style()) {
      case NoPen:
	break;
      case SolidLine:
	break;
      case DashLine: {
	const SkScalar dasharray[] = { SkIntToScalar(4), SkIntToScalar(2) };
	impl_->strokePaint_.setPathEffect(new SkDashPathEffect(dasharray, 2,
							false))->unref();
	break;
      }
      case DotLine: {
	const SkScalar dasharray[] = { SkIntToScalar(1), SkIntToScalar(2) };
	impl_->strokePaint_.setPathEffect(new SkDashPathEffect(dasharray, 2,
							false))->unref();
	break;
      }
      case DashDotLine: {
	const SkScalar dasharray[] = {
	  SkIntToScalar(4),
	  SkIntToScalar(2),
	  SkIntToScalar(1),
	  SkIntToScalar(2)
	};
	impl_->strokePaint_.setPathEffect(new SkDashPathEffect(dasharray, 4,
							false))->unref();
	break;
      }
      case DashDotDotLine: {
	const SkScalar dasharray[] = {
	  SkIntToScalar(4),
	  SkIntToScalar(2),
	  SkIntToScalar(1),
	  SkIntToScalar(2),
	  SkIntToScalar(1),
	  SkIntToScalar(2)
	};
	impl_->strokePaint_.setPathEffect(new SkDashPathEffect(dasharray, 6,
							false))->unref();
	break;
      }
      }

    }
  }

  if (flags & Brush) {
    const WBrush& brush = painter()->brush();
    if (brush.style() != NoBrush) {
      const WColor& color = painter()->brush().color();
      impl_->fillPaint_.setColor(fromWColor(color));
    }
  }

  if (flags & Font) {
    const WFont& font = painter()->font();

    const char *base = 0;
    switch (font.genericFamily()) {
    case WFont::Default:
    case WFont::Serif:
      base = "Times";
      break;
    case WFont::SansSerif:
      base = "Helvetica";
      break;
    case WFont::Monospace:
      base = "Courier";
      break;
    case WFont::Fantasy: // Not really !
      base = "Symbol";
      break;
    case WFont::Cursive: // Not really !
      base = "ZapfDingbats";
    }

    int style = SkTypeface::kNormal;
    if (font.style() != WFont::NormalStyle)
      style |= SkTypeface::kItalic;
    if (font.weight() == WFont::Bold ||
	font.weight() == WFont::Bolder)
      style |= SkTypeface::kBold;

    impl_->textPaint_.setTypeface(SkTypeface::CreateFromName(base,
				    (SkTypeface::Style)style))->unref();
    impl_->textPaint_.setTextSize(SkIntToScalar(font.sizeLength(12).toPixels()));
  }
}