Esempio n. 1
0
void NodeRenderer::label(const Cairo::RefPtr<Cairo::Context>& cr,
		std::list<shared_ptr<Label> >& labels,
		AssetCache& cache)
{
	// nothing to print
	if (s->text.str().size() == 0 || s->font_size <= 0)
		return;

	cr->save();

	cr->set_font_size(s->font_size);

	cr->set_font_face(cache.getFont(
			s->font_family.str(),
			s->font_style == Style::STYLE_ITALIC ? Cairo::FONT_SLANT_ITALIC : Cairo::FONT_SLANT_NORMAL,
			s->font_weight == Style::WEIGHT_BOLD ? Cairo::FONT_WEIGHT_BOLD : Cairo::FONT_WEIGHT_NORMAL
		));

	Cairo::TextExtents textSize;
	cr->get_text_extents(s->text.str(), textSize);

	addLabel(labels, location + FloatPoint(0.0, s->text_offset), textSize);

	cr->restore();
}
Esempio n. 2
0
void WayRenderer::stroke(cairo_t* cr, AssetCache& cache)
{
	if (s->width <= 0.0)
		return;

	addWayPath(cr);

	cairo_save(cr);

	setLineCap(cr,  s->linecap);
	setLineJoin(cr, s->linejoin);
	cairo_set_source_rgba(cr, COLOR2RGBA(s->color));
	const string& image = s->image.str();
	cairo_pattern_t* pattern = NULL;
	if (!image.empty()) {
		pattern = cairo_pattern_create_for_surface(cache.getImage(image));
		cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
		cairo_set_source(cr, pattern);
	}

	if (s->dashes.size() > 0)
		cairo_set_dash(cr, s->dashes.data(), s->dashes.size(), 0.0);
	cairo_set_line_width(cr, s->width);

	cairo_stroke(cr);

	if (pattern != NULL)
		cairo_pattern_destroy(pattern);

	cairo_restore(cr);
}
Esempio n. 3
0
void NodeRenderer::icon(const Cairo::RefPtr<Cairo::Context>& cr, AssetCache& cache)
{
	// path to icon not set
	if (s->icon_image.str().size() == 0 || s->icon_width == 0.0 || s->icon_height == 0.0)
		return;

	cr->save();

	Cairo::RefPtr<Cairo::ImageSurface> image = cache.getImage(s->icon_image.str());
	double width = s->icon_width < 0 ? image->get_width() : s->icon_width;
	double height = s->icon_height < 0 ? image->get_height() : s->icon_height;
	double x0 = floor(location.x - width/2.0);
	double y0 = floor(location.y - height/2.0);
	cr->translate(x0, y0);
	cr->scale(width / image->get_width(),
			  height / image->get_height());
	cr->set_source(image, 0, 0);

	if (s->icon_opacity < 1.0)
		cr->paint_with_alpha(s->icon_opacity);
	else
		cr->paint();

	cr->restore();
}
Esempio n. 4
0
void Renderer::setupLayers(CairoLayer layers[], const shared_ptr<ImageWriter>& writer, AssetCache& cache) const
{
	for (int i = 0; i < LAYER_NUM; i++) {
		layers[i] = CairoLayer(writer);
		layers[i].clear();
	}

	// setup default font
	Cairo::FontOptions fontOpts;
	fontOpts.set_hint_style(Cairo::HINT_STYLE_NONE);
	fontOpts.set_hint_metrics(Cairo::HINT_METRICS_OFF);
	layers[LAYER_LABELS].cr->set_font_options(fontOpts);
	Cairo::RefPtr<Cairo::ToyFontFace> font = cache.getFont(DEFAULT_FONT, Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL);
	layers[LAYER_LABELS].cr->set_font_face(font);
}
Esempio n. 5
0
void Renderer::renderLabels(const Cairo::RefPtr<Cairo::Context>& cr,
							std::vector<shared_ptr<LabelType> >& labels,
							AssetCache& cache) const
{
	cr->save();

	cr->set_line_join(Cairo::LINE_JOIN_ROUND);

	for (auto it = labels.rbegin(); it != labels.rend(); it++)
	{
		const shared_ptr<LabelType>& label = *it;
		const Style* s = label->style;

		cr->set_font_size(s->font_size);

		cr->move_to(label->origin.x, label->origin.y);

		cr->set_font_face(cache.getFont(
					s->font_family.str(),
					s->font_style == Style::STYLE_ITALIC ? Cairo::FONT_SLANT_ITALIC : Cairo::FONT_SLANT_NORMAL,
					s->font_weight == Style::WEIGHT_BOLD ? Cairo::FONT_WEIGHT_BOLD : Cairo::FONT_WEIGHT_NORMAL
				));

		cr->text_path(label->text.str());

		if (s->text_halo_radius > 0.0)
		{
			cr->set_source_color(s->text_halo_color);
			cr->set_line_width(s->text_halo_radius*2.0);
			cr->stroke_preserve();
		}

		cr->set_source_color(s->text_color);
		cr->fill();
	}

	cr->restore();
}
Esempio n. 6
0
void WayRenderer::fill(cairo_t* cr, AssetCache& cache)
{
	if (!way->isClosed())
		return;

	addWayPath(cr);

	cairo_save(cr);

	const string& bg = s->fill_image.str();
	if (!bg.empty()) {
		cairo_pattern_t* pattern = cairo_pattern_create_for_surface(cache.getImage(bg));
		cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
		cairo_set_source(cr, pattern);
		cairo_fill(cr);
		cairo_pattern_destroy(pattern);
	} else {
		cairo_set_source_rgba(cr, COLOR2RGBA(s->fill_color));
		cairo_fill(cr);
	}

	cairo_restore(cr);
}
Esempio n. 7
0
void HttpAssetProvider::OnHttpTransferFinished(QNetworkReply *reply)
{
    // QNetworkAccessManager requires us to delete the QNetworkReply, or it will leak.
    reply->deleteLater();

    switch(reply->operation())
    {
    case QNetworkAccessManager::GetOperation:
    {
        QByteArray data = reply->readAll();
        TransferMap::iterator iter = transfers.find(reply);
        if (iter == transfers.end())
        {
            LogError("Received a finish signal of an unknown Http transfer!");
            return;
        }
        HttpAssetTransferPtr transfer = iter->second;
        assert(transfer);
        transfer->rawAssetData.clear();

        if (reply->error() == QNetworkReply::NoError)
        {
            // If asset request creator has not allowed caching, remove it now
            AssetCache *cache = framework->Asset()->GetAssetCache();
            if (!transfer->CachingAllowed())
                cache->remove(reply->url());

            // Setting cache allowed as false is very important! The items are already in our cache via the 
            // QAccessManagers QAbstractNetworkCache (same as our AssetAPI::AssetCache). Network replies will already call them
            // so the AssetAPI::AssetTransferCompletes doesn't have to.
            // \note GetDiskSource() will return empty string if above cache remove was performed, this is wanted behaviour.
            transfer->SetCachingBehavior(false, cache->GetDiskSource(reply->url()));

            // Copy raw data to transfer
            transfer->rawAssetData.insert(transfer->rawAssetData.end(), data.data(), data.data() + data.size());
            framework->Asset()->AssetTransferCompleted(transfer.get());
        }
        else
        {
            QString error = "Http GET for address \"" + reply->url().toString() + "\" returned an error: \"" + reply->errorString() + "\"";
            framework->Asset()->AssetTransferFailed(transfer.get(), error);
        }
        transfers.erase(iter);
        break;
    }
    case QNetworkAccessManager::PutOperation:
    case QNetworkAccessManager::PostOperation:
    {
        UploadTransferMap::iterator iter = uploadTransfers.find(reply);
        if (iter == uploadTransfers.end())
        {
            LogError("Received a finish signal of an unknown Http upload transfer!");
            return;
        }
        AssetUploadTransferPtr transfer = iter->second;

        if (reply->error() == QNetworkReply::NoError)
        {
            LogDebug("Http upload to address \"" + reply->url().toString().toStdString() + "\" returned successfully.");
            framework->Asset()->AssetUploadTransferCompleted(transfer.get());
        }
        else
        {
            LogError("Http upload to address \"" + reply->url().toString().toStdString() + "\" failed with an error: \"" + reply->errorString().toStdString() + "\"");
            ///\todo Call the following when implemented:
//            framework->Asset()->AssetUploadTransferFailed(transfer);
        }

        uploadTransfers.erase(iter);
        break;
    }
    case QNetworkAccessManager::DeleteOperation:
        if (reply->error() == QNetworkReply::NoError)
            LogInfo("Http DELETE to address \"" + reply->url().toString().toStdString() + "\" returned successfully.");
        else
            LogError("Http DELETE to address \"" + reply->url().toString().toStdString() + "\" failed with an error: \"" + reply->errorString().toStdString() + "\"");
        break;
    default:
        LogInfo("Unknown operation for address \"" + reply->url().toString().toStdString() + "\" finished with result: \"" + reply->errorString().toStdString() + "\"");
        break;
    }
}