SimpleLineSymbol.cpp 13.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

#include "SimpleLineSymbol.h"
#include "clsUtil.h"
#include"clsXML.h"
#include"clsCrSurf.h"
#include "clsJson.h"
#include "clsPtree.h"
#define _USE_MATH_DEFINES
#include<math.h>
namespace DmapCore_30
{

	SimpleLineSymbol::SimpleLineSymbol()
	{
		m_iAntialiasing =  CAIRO_ANTIALIAS_FAST;
		m_iColor = clsUtil::RandomColor();
		clsUtil::ToCairoColor(m_iColor, r, g, b, a);
		//m_pClsSurfBackup =	NULL;
		m_iType = 0;
		m_sPNG = "";
		m_dWidth = 1;
		m_iCapType = 0;
		m_iJionType = 0;
		m_sXML = "";
		//png = 0;
		m_mustReloadPng = true;
		m_simpleRenderer = true;
	}
	SimpleLineSymbol::~SimpleLineSymbol()
	{
		//if (png)
		//{
		//	delete png;
		//}
	}
	SimpleLineSymbol* SimpleLineSymbol::CreateNew()
	{
		SimpleLineSymbol * dest = new SimpleLineSymbol();
		return dest;
	}
	int SimpleLineSymbol::RendererType()
	{
		return dmapSimpleLineSymbol;
	}

	bool  SimpleLineSymbol::Parse(boost::property_tree::ptree &pt, AppendBuffer *f)
	{
		//if (!node&&!f)return false;
		clsXML clsX(f, "SIMPLELINESYMBOL");
		clsXML::ParseEnum("antialiasing", pt, f, &m_iAntialiasing, "antialias_default", "antialias_none", "antialias_gray", "antialias_subpixel", "antialias_fase", "antialias_good", "antialias_best", "true", "false");
		clsXML::XMLAttrParse(pt, f, &m_sPNG, "png");
		clsXML::ParseEnum("type", pt, f, &m_iType, "solid", "dash", "dot", "dash_dot", "dash_dot_dot");
		clsXML::XMLAttrParseColor(pt, f, &m_iColor, "color", "transparency");
		clsXML::XMLAttrParse(pt, f, &m_dWidth, "width");
		clsXML::ParseEnum("captype", pt, f, &m_iCapType, "cap_butt", "cap_round", "cap_square");
		clsXML::ParseEnum("jiontype", pt, f, &m_iJionType, "jion_miter", "jion_round", "jion_bevel");

			if (m_sPNG != "")
			{
				m_sPNG = GetFilePath(m_sPNG);
				if (FILE *file = fopen(m_sPNG.c_str(), "r")) {
					fclose(file);
				}
				else {
					m_sPNG = "";
				}
			}
		clsUtil::ToCairoColor(m_iColor, r, g, b, a);
		m_mustReloadPng = true;
		return true;
	}

	bool SimpleLineSymbol::ParsePtree( boost::property_tree::ptree &pt)
	{
		boost::property_tree::ptree ptRenderer;
		clsPtree::PtreeAttrParse("icon", ptRenderer, this->m_sPNG);
		//clsPtree::ParseEnum("type", resultbuff, buff, m_iAntialiasing, "antialias_default", "antialias_none", "antialias_gray", "antialias_subpixel", "antialias_fase", "antialias_good", "antialias_best", "true", "false");
		clsPtree::ParseEnum("type", ptRenderer, m_iType, "solid", "dash", "dot", "dash_dot", "dash_dot_dot");
		clsPtree::PtreeAttrParseColor("color", "transparency", ptRenderer, m_iColor);
		clsPtree::PtreeAttrParse("width", ptRenderer, m_dWidth);
		
		clsPtree::ParseEnum("captype", ptRenderer, m_iCapType, "cap_butt", "cap_round", "cap_square");
		clsPtree::ParseEnum("jiontype", ptRenderer, m_iJionType, "jion_miter", "jion_round", "jion_bevel");
		pt.add_child("SIMPLELINESYMBOL",ptRenderer);
		return true;
	}

	void  SimpleLineSymbol::ToJson(AppendBuffer *ab)
   {
	   char buff[300] = {0};
	   char resultbuff[5000] = {0};
		strcat(resultbuff, R"("SIMPLELINESYMBOL":{"antialiasing":"antialias_default",)");
		clsJson::JsonAttrParse("icon", resultbuff, buff, this->m_sPNG);
		//clsJson::ParseEnum("type", resultbuff, buff, m_iAntialiasing, "antialias_default", "antialias_none", "antialias_gray", "antialias_subpixel", "antialias_fase", "antialias_good", "antialias_best", "true", "false");
		clsJson::ParseEnum("type", resultbuff, buff, m_iType, "solid", "dash", "dot", "dash_dot", "dash_dot_dot");
		clsJson::JsonAttrParseColor("color", "transparency", resultbuff, buff, m_iColor);
		clsJson::JsonAttrParse("width", resultbuff, buff, m_dWidth);
		
		clsJson::ParseEnum("captype", resultbuff, buff, m_iCapType, "cap_butt", "cap_round", "cap_square");
		clsJson::ParseEnum("jiontype", resultbuff, buff, m_iJionType, "jion_miter", "jion_round", "jion_bevel");
		
		clsJson::JsonAttrEnd(resultbuff);
		ab->AppendString(resultbuff);
   }

	int                  SimpleLineSymbol::GetAntialiasing()
	{
		return  m_iAntialiasing;
	}
	bool                 SimpleLineSymbol::SetAntialiasing(int newVal)
	{
		m_iAntialiasing = newVal;
		return true;
	}
	unsigned int        SimpleLineSymbol::GetColor()
	{
		return  m_iColor;
	}
	bool                 SimpleLineSymbol::SetColor(unsigned int color)
	{
		m_iColor = color;
		clsUtil::ToCairoColor(color, r, g, b, a);
		return true;
	}
	int                   SimpleLineSymbol::GetType()
	{
		return  m_iType;
	}
	bool                 SimpleLineSymbol::SetType(int type)
	{
		m_iType = type;
		return true;
	}
	const char*               SimpleLineSymbol::GetPNG()
	{
		//m_sPNG = "中国人";
		//clsMalloc clsM2;
		//bool flag = clsUtil::ConvertEncGB18030_UTF8(m_sPNG.c_str(), clsM2);
		//m_sPNG = clsM2.mem;
		return  m_sPNG.c_str();
	}
	bool                 SimpleLineSymbol::SetPNG(const char* png)
	{
		m_sPNG = png;
		m_mustReloadPng = true;
		png = 0;
		return true;
	}
	double               SimpleLineSymbol::GetWidth()
	{
		return  m_dWidth;
	}
	bool                 SimpleLineSymbol::SetWidth(double width)
	{
		if (width < 0)return false;
		m_dWidth = width;
		return true;
	}
	int                      SimpleLineSymbol::GetCapType()
	{
		return m_iCapType;
	}
	bool                     SimpleLineSymbol::SetCapType(int iCapType)
	{
		m_iCapType = iCapType;
		return true;
	}
	int                      SimpleLineSymbol::GetJionType()
	{
		return m_iJionType;
	}
	bool                     SimpleLineSymbol::SetJionType(int iJionType)
	{
		m_iJionType = iJionType;
		return true;
	}

	/*
	*/
	int SimpleLineSymbol::BytesPerLine(int nWidth, int nBitsPerPixel)  //像素宽度、每个像素多少位
	{
		return ((nWidth * nBitsPerPixel + 31) & (~31)) / 8;
	}


	bool SimpleLineSymbol::DrawLINESimple(clsCrSurf* clsCS, clsStruct::LINE*pLine)
	{
		if(pLine == NULL || pLine->p == NULL)
			 return false;
		cairo_t* cr = clsCS->m_pCr;
		clsUtil::DoSetAntialias(m_iAntialiasing, cr);

		int nPoint = pLine->nPoint;
		clsStruct::POINT* pPoint = pLine->p;
		cairo_new_path(cr);
		for (int i = 0; i < nPoint; i++)
		{
			double x = pPoint[i].x;
			double y = pPoint[i].y;

			if (i == 0)
			{
				cairo_move_to(cr, x, y);
			}
			else
				cairo_line_to(cr, x, y);
		}
		clsUtil::DoSetLineType(m_iType, cr);
		cairo_set_source_rgba(cr, r, g, b, a);
		cairo_set_line_width(cr, m_dWidth);
		cairo_set_line_cap(cr, (cairo_line_cap_t)m_iCapType);
		cairo_set_line_join(cr, (cairo_line_join_t)m_iJionType);
		cairo_stroke(cr);
		return true;
	}

	bool SimpleLineSymbol::DrawMLINESimple(clsCrSurf* clsCS, clsStruct::MLINE*pMLine)
	{

		if(pMLine == NULL || pMLine->p == NULL) return false;
		cairo_t* cr = clsCS->m_pCr;
		clsUtil::DoSetAntialias(m_iAntialiasing, cr);

		int nLine = pMLine->nLine;
		clsStruct::LINE* pLine = pMLine->p;
		cairo_new_path(cr);
	
		for (int i = 0; i < nLine; i++)
		{
			int nPoint = (pLine + i)->nPoint;
			clsStruct::POINT* pPoint = (pLine + i)->p;

			if(pPoint == NULL) continue;
			for (int j = 0; j < nPoint; j++)
			{
				double x = pPoint[j].x;
				double y = pPoint[j].y;
				if (j == 0)
				{
					cairo_move_to(cr, x, y);
				}
				else
					cairo_line_to(cr, x, y);
			}
		}
		clsUtil::DoSetLineType(m_iType, cr);
		cairo_set_source_rgba(cr, r, g, b, a);
		cairo_set_line_width(cr, m_dWidth);
		cairo_set_line_cap(cr, (cairo_line_cap_t)m_iCapType);
		cairo_set_line_join(cr, (cairo_line_join_t)m_iJionType);
		cairo_stroke(cr);

		return true;
	}

	bool SimpleLineSymbol::DrawData(clsCrSurf* pClsCS, DataCollection* data, bool drawSimpleData, char* pFlag)
	{
		/***********************************************************************************************/
		/*
		patta
		*/
		/*
		if (m_sPNG != "")
		{
		if (m_pPatta != NULL)
		TryDelete(m_pPatta);
		this->TryCreatePatta(pClsCS);
		}
		*/
		/***********************************************************************************************/
		
		int structType = data->m_iStructType;
		int iPartCount = data->m_iPartCount;
	
       if (m_sPNG != "")
	   {
		cairo_t* cr = pClsCS->m_pCr;
		cairo_new_path(cr);
		cairo_arc(cr, 0, 0, 1, 0, 1 * M_PI);
		cairo_set_source_rgba(cr, 1, 1, 1, 0.01);
		cairo_fill(cr);  //不留边框填充
	   }

		switch (structType)
		{
		case clsStruct::Struct_Line:
		{
									   for (int i = 0; i < iPartCount; i++)
									   {
										   clsStruct::LINE* pLine = (clsStruct::LINE*)data->m_pData;
										   this->DrawLINE(pClsCS, pLine + i);
									   }
									   break;
		}
		case clsStruct::Struct_MLine:
		{
										for (int i = 0; i < iPartCount; i++)
										{
											clsStruct::MLINE* pMLine = (clsStruct::MLINE*)data->m_pData;
											this->DrawMLINE(pClsCS, pMLine + i);
										}
										break;
		}
		default:
			break;
		}
		return true;
	}

	bool SimpleLineSymbol::DrawData(clsCrSurf* pClsCS, DataCollection* data, int dataIndex, char* pFlag)
	{
		/***********************************************************************************************/
		/*
		patta
		*/
		/*
		if (m_sPNG != "")
		{
		if (m_pPatta != NULL)
		TryDelete(m_pPatta);
		this->TryCreatePatta(pClsCS);
		}
		*/
		/***********************************************************************************************/

		int structType = data->m_iStructType;
		int iPartCount = data->m_iPartCount;


		switch (structType)
		{
		case clsStruct::Struct_Line:
		{
									   clsStruct::LINE* pLine = (clsStruct::LINE*)data->m_pData;
									   this->DrawLINE(pClsCS, pLine + dataIndex);
									   break;
		}
		case clsStruct::Struct_MLine:
		{
										clsStruct::MLINE* pMLine = (clsStruct::MLINE*)data->m_pData;
										this->DrawMLINE(pClsCS, pMLine + dataIndex);
										break;
		}
		default:
			break;
		}
		return true;
	}

	bool SimpleLineSymbol::DrawLegend(clsCrSurf* pClsCS, shared_ptr<legendParamater> pLegendParamater,int layerType, char* pFlag)
	{
		if(layerType != 1)
			 return false;

		pLegendParamater->next(pFlag);

		cairo_t *cr = pClsCS->m_pCr;
		if (pFlag)
		{
			cairo_set_source_rgb(cr, 0, 0, 0); //设置画笔颜色,也就是红,绿,蓝,这里设置成绿色。
			cairo_select_font_face(cr, pLegendParamater->font.c_str(), 
				CAIRO_FONT_SLANT_NORMAL, 
				pLegendParamater->isbold? CAIRO_FONT_WEIGHT_BOLD:CAIRO_FONT_WEIGHT_NORMAL);

			cairo_set_font_size(cr, pLegendParamater->fontSize);
			cairo_move_to(cr, pLegendParamater->pixXIndex + pLegendParamater->size_x + 10, 
						pLegendParamater->pixYIndex + pLegendParamater->size_y - 3);
						
			cairo_show_text(cr, pFlag);
		}
		if (m_sPNG != "")
		{
			//cairo_t* cr = pClsCS->m_pCr;
			cairo_new_path(cr);
			cairo_arc(cr, 0, 0, 1, 0, 1 * M_PI);
			cairo_set_source_rgba(cr, 1, 1, 1, 0.01);
			cairo_fill(cr); //不留边框填充
		}

		clsUtil::DoSetAntialias(m_iAntialiasing, cr);

		//int nPoint = pLine->nPoint;
		//clsStruct::POINT* pPoint = pLine->p;
		cairo_new_path(cr);
		cairo_move_to(cr, pLegendParamater->pixXIndex, pLegendParamater->pixYIndex + pLegendParamater->size_y/2);
		cairo_line_to(cr, pLegendParamater->pixXIndex + pLegendParamater->size_x, pLegendParamater->pixYIndex + pLegendParamater->size_y/2);
		
		clsUtil::DoSetLineType(m_iType, cr);
		cairo_set_source_rgba(cr, r, g, b, a);
		cairo_set_line_width(cr, m_dWidth);
		cairo_set_line_cap(cr, (cairo_line_cap_t)m_iCapType);
		cairo_set_line_join(cr, (cairo_line_join_t)m_iJionType);
		cairo_stroke(cr);
		return true;
	}
	
	bool SimpleLineSymbol::DrawLINE(clsCrSurf* pClsCS, clsStruct::LINE * pLine)
	{
		if (m_sPNG == "")
			return this->DrawLINESimple(pClsCS, pLine);
		else
			return this->DrawLINEPNG(pClsCS, pLine);
	}


	bool SimpleLineSymbol::DrawMLINE(clsCrSurf* pClsCS, clsStruct::MLINE* pMLine)
	{
		if (m_sPNG == "")
			return this->DrawMLINESimple(pClsCS, pMLine);
		else
			return this->DrawMLINEPNG(pClsCS, pMLine);
	}

	bool SimpleLineSymbol::DrawLINEPNG(clsCrSurf* pClsCS, clsStruct::LINE* pLine)
	{
		return true;
	}
	bool SimpleLineSymbol::DrawMLINEPNG(clsCrSurf* pClsCS, clsStruct::MLINE* pMLine)
	{
	
		//	return true;
		/*if (m_mustReloadPng == true || m_LastBGColor != pClsCS->m_iBGColor)
		{
			TryReadPng(pClsCS->m_iBGColor);
		}
	
	  
		cairo_surface_t* surface = pClsCS->m_pSurf;
		int surf_w = cairo_image_surface_get_width(surface);
		int surf_h = cairo_image_surface_get_height(surface);

		unsigned char* buffer = cairo_image_surface_get_data(surface);
		
		//agg::rendering_buffer m_pRBuf(buffer, (unsigned int)surf_w, (unsigned int)surf_h, (int)(surf_w * 4));
		//pixfmt m_pf(m_pRBuf);
		//renderer_base m_pRen_base(m_pf);
		//renderer_type m_pRen_img(m_pRen_base, *(png->patta));
		//rasterizer_type m_pRas_img(m_pRen_img);
	
		/*int nLine = pMLine->nLine;
		clsStruct::LINE * pLine = pMLine->p;

		for (int j = 0; j < nLine; j++)
		{
			int nPoint = pLine[j].nPoint;
			clsStruct::POINT * pPoint = pLine[j].p;

			agg::path_storage path;
			for (int k = 0; k < nPoint; k++)
			{
				double x = pPoint[k].x;
				double y = pPoint[k].y;
				if (k == 0)
					path.move_to(x, y);
				else
					path.line_to(x, y);
			}
			m_pRas_img.add_path(path);
		}*/
		return true;


	}

	

	bool SimpleLineSymbol::TryReadPng(unsigned int colorThis)
	{
		//m_mustReloadPng = false;
	    //m_LastBGColor = colorThis;
		//if (png)delete png; png = new clsPng();
		//if (png->ReadPng(this->m_sPNG.c_str(), m_LastBGColor) == 1)
		//{
		//	delete png; png = 0;
		//	return false;
		//}

		return true;
	}

	bool SimpleLineSymbol::TryCreatePatta(clsCrSurf * pClsCS)
	{

		return true;
	}
	bool SimpleLineSymbol::TryCreateRendererBase()
	{

		return true;
	}
}