Fix saving graph to image if text if long or font size is big.

Fix background during saving to image.
This commit is contained in:
Oleg Sh
2025-02-02 16:29:07 +01:00
parent 159c87c041
commit 0105f76838
7 changed files with 54 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
moduleLoader.beginCacheLoading(["/script/shared/utils.js?v=92","/script/shared/gzip.js?v=92","/script/entities/graph/api/index.js?v=92","/script/shared/point.js?v=92","/script/entities/edge/api/index.js?v=92","/script/entities/edge/model/BaseEdge.js?v=92","/script/entities/edge/model/EdgeModel.js?v=92","/script/entities/vertex/api/index.js?v=92","/script/entities/vertex/model/BaseVertex.js?v=92","/script/entities/vertex/model/VertexModel.js?v=92","/script/entities/graph/model/Graph.js?v=92","/script/features/draw_graph/api/index.js?v=92","/script/features/draw_graph/model/BaseBackgroundDrawer.js?v=92","/script/features/draw_graph/model/EdgeStyle.js?v=92","/script/features/draw_graph/model/BaseEdgeDrawer.js?v=92","/script/features/draw_graph/model/VertexShape.js?v=92","/script/features/draw_graph/model/VertexStyle.js?v=92","/script/features/draw_graph/model/BaseVertexDrawer.js?v=92","/script/features/draw_graph/model/GraphFullStyle.js?v=92","/script/features/algorithms/api/index.js?v=92","/script/features/algorithms/model/Algorithms.js?v=92","/script/features/algorithms/model/BaseTraversal.js?v=92","/script/features/base_handler/index.js?v=92","/script/features/default_handler/index.js?v=92","/script/features/add_vertices_handler/index.js?v=92","/script/features/connect_vertices_handler/index.js?v=92","/script/features/delete_objects_handler/index.js?v=92","/script/features/algorithm_handler/index.js?v=92","/script/features/select_auto_save_graph_or_not/index.js?v=92","/script/features/graph_preview/index.js?v=92","/script/features/serialization/api/index.js?v=92","/script/features/serialization/model/GraphMLCreator.js?v=92","/script/features/enum_vertices/EnumVertices.js?v=92","/script/pages/editor/model/texts.js?v=92","/script/pages/editor/model/UndoStack.js?v=92","/script/pages/editor/model/DiskSaveLoad.js?v=92","/script/pages/editor/model/Application.js?v=92","/script/pages/editor/ui/ya_metrika.js?v=92","/script/pages/editor/ui/editor.js?v=92","/script/pages/editor/ui/main.js?v=92",]);{function onloadEditor(){console.log("onload() call");doIncludeAsync([include("shared/canvas2svg.js"),include("features/group_rename_handler/index.js"),include("features/saved_graph_handler/index.js"),include("features/saved_graph_image_handler/index.js"),include("features/show_adjacency_matrix/index.js"),include("features/show_distance_matrix/index.js"),include("features/show_incidence_matrix/index.js"),include("features/setup_background_style/index.js"),include("features/setup_edge_style/index.js"),include("features/setup_vertex_style/index.js"),]);postLoadPage();}
moduleLoader.beginCacheLoading(["/script/shared/utils.js?v=93","/script/shared/gzip.js?v=93","/script/entities/graph/api/index.js?v=93","/script/shared/point.js?v=93","/script/entities/edge/api/index.js?v=93","/script/entities/edge/model/BaseEdge.js?v=93","/script/entities/edge/model/EdgeModel.js?v=93","/script/entities/vertex/api/index.js?v=93","/script/entities/vertex/model/BaseVertex.js?v=93","/script/entities/vertex/model/VertexModel.js?v=93","/script/entities/graph/model/Graph.js?v=93","/script/features/draw_graph/api/index.js?v=93","/script/features/draw_graph/model/BaseBackgroundDrawer.js?v=93","/script/features/draw_graph/model/EdgeStyle.js?v=93","/script/features/draw_graph/model/BaseEdgeDrawer.js?v=93","/script/features/draw_graph/model/VertexShape.js?v=93","/script/features/draw_graph/model/VertexStyle.js?v=93","/script/features/draw_graph/model/BaseVertexDrawer.js?v=93","/script/features/draw_graph/model/GraphFullStyle.js?v=93","/script/features/algorithms/api/index.js?v=93","/script/features/algorithms/model/Algorithms.js?v=93","/script/features/algorithms/model/BaseTraversal.js?v=93","/script/features/base_handler/index.js?v=93","/script/features/default_handler/index.js?v=93","/script/features/add_vertices_handler/index.js?v=93","/script/features/connect_vertices_handler/index.js?v=93","/script/features/delete_objects_handler/index.js?v=93","/script/features/algorithm_handler/index.js?v=93","/script/features/select_auto_save_graph_or_not/index.js?v=93","/script/features/graph_preview/index.js?v=93","/script/features/serialization/api/index.js?v=93","/script/features/serialization/model/GraphMLCreator.js?v=93","/script/features/enum_vertices/EnumVertices.js?v=93","/script/pages/editor/model/texts.js?v=93","/script/pages/editor/model/UndoStack.js?v=93","/script/pages/editor/model/DiskSaveLoad.js?v=93","/script/pages/editor/model/Application.js?v=93","/script/pages/editor/ui/ya_metrika.js?v=93","/script/pages/editor/ui/editor.js?v=93","/script/pages/editor/ui/main.js?v=93",]);{function onloadEditor(){console.log("onload() call");doIncludeAsync([include("shared/canvas2svg.js"),include("features/group_rename_handler/index.js"),include("features/saved_graph_handler/index.js"),include("features/saved_graph_image_handler/index.js"),include("features/show_adjacency_matrix/index.js"),include("features/show_distance_matrix/index.js"),include("features/show_incidence_matrix/index.js"),include("features/setup_background_style/index.js"),include("features/setup_edge_style/index.js"),include("features/setup_vertex_style/index.js"),]);postLoadPage();}
let modulDir="pages/editor/";doInclude([include("shared/utils.js"),include("shared/gzip.js"),include("entities/graph/api/index.js"),include("features/draw_graph/api/index.js"),include("features/algorithms/api/index.js"),include("features/base_handler/index.js"),include("features/default_handler/index.js"),include("features/add_vertices_handler/index.js"),include("features/connect_vertices_handler/index.js"),include("features/delete_objects_handler/index.js"),include("features/algorithm_handler/index.js"),include("features/select_auto_save_graph_or_not/index.js"),include("features/serialization/api/index.js"),include("features/enum_vertices/EnumVertices.js"),include("model/texts.js",modulDir),include("model/UndoStack.js",modulDir),include("model/DiskSaveLoad.js",modulDir),include("model/Application.js",modulDir),include("ui/ya_metrika.js",modulDir),include("ui/editor.js",modulDir),include("ui/main.js",modulDir)],onloadEditor);}
function gEncodeToHTML(str)
{if(typeof str!=='string')
@@ -198,6 +198,8 @@ EdgeModel.prototype.GetLoopShiftAngel=function()
else
{return this.loopShiftAngel+Math.PI;}}
{let modulDir="entities/vertex/";doInclude([include("model/BaseVertex.js",modulDir),include("model/VertexModel.js",modulDir)])}
function GetTextWidth(text,font)
{const canvas=GetTextWidth.canvas||(GetTextWidth.canvas=document.createElement("canvas"));const context=canvas.getContext("2d");context.font=font;const metrics=context.measureText(text);return metrics.width;}
function BaseVertex(x,y,vertexEnumType)
{this.position=new Point(x,y);this.id=0;this.mainText="";this.upText="";this.vertexEnumType=vertexEnumType;this.model=new VertexModel();this.hasUndefinedPosition=false;this.ownStyles={};};BaseVertex.prototype.position=new Point(0,0);BaseVertex.prototype.copyFrom=function(other)
{this.position=new Point(other.position.x,other.position.y);this.id=other.id;this.mainText=other.mainText;this.upText=other.upText;this.vertexEnumType=other.vertexEnumType;this.model=new VertexModel();this.hasUndefinedPosition=other.hasUndefinedPosition;this.ownStyles=FullObjectCopy(other.ownStyles);}
@@ -253,7 +255,7 @@ BaseVertex.prototype.getDefaultDiameterFactor=function(textSize)
BaseVertex.prototype.getBBox=function(style)
{var textSize=DefaultMainTextFontSize;if(style!==undefined&&style.mainTextFontSize!==undefined)
{textSize=style.mainTextFontSize;}
var defaultDiameter=(new VertexModel()).diameter;var vertexDiameter=this.model.diameter;var factor=this.getDefaultDiameterFactor(textSize);return new Point(Math.max(factor.x*defaultDiameter,vertexDiameter),Math.max(factor.y*defaultDiameter,vertexDiameter));}
var defaultDiameter=(new VertexModel()).diameter;var vertexDiameter=this.model.diameter;var factor=this.getDefaultDiameterFactor(textSize);let textWidth=(GetTextWidth(this.mainText,textSize+"px sans-serif")+8)/1.5;let textHeight=textSize+4;let isTextAbove=style.commonTextPosition==1;return new Point(Math.max(factor.x*defaultDiameter,vertexDiameter,textWidth),Math.max(factor.y*defaultDiameter,vertexDiameter+(isTextAbove?textHeight:0),textHeight));}
const defaultVertexDiameter=30;function VertexModel()
{this.diameter=globalApplication.GetDefaultVertexSize();}
function Graph()
@@ -1527,13 +1529,13 @@ var progress=(this.renderPathCounter-currentLength)/edge.GetPixelLength();this.R
if(this.renderPathLoops>=5)
{this.stopRenderTimer();}}
Application.prototype._redrawGraphInWindow=function()
{var context=this.canvas.getContext('2d');context.save();context.scale(this.canvasScale,this.canvasScale);context.translate(this.canvasPosition.x,this.canvasPosition.y);this._RedrawGraph(context,this.canvasPosition,this.style.backgroundCommonStyle,true);context.restore();return context;}
{var context=this.canvas.getContext('2d');context.save();context.scale(this.canvasScale,this.canvasScale);context.translate(this.canvasPosition.x,this.canvasPosition.y);this._RedrawGraph(context,{width:this.canvas.width,height:this.canvas.height,scale:this.canvasScale},this.canvasPosition,this.style.backgroundCommonStyle,true);context.restore();return context;}
Application.prototype._OffscreenRedrawGraph=function()
{var bbox=this.graph.getGraphBBox();var canvas=document.createElement('canvas');canvas.width=bbox.size().x;canvas.height=bbox.size().y;var context=canvas.getContext('2d');context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,bbox.minPoint.inverse(),this.style.backgroundCommonStyle,false);context.restore();return canvas;}
{var bbox=this.graph.getGraphBBox();var canvas=document.createElement('canvas');canvas.width=bbox.size().x;canvas.height=bbox.size().y;var context=canvas.getContext('2d');context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,{width:canvas.width,height:canvas.height,scale:1.0},bbox.minPoint.inverse(),this.style.backgroundCommonStyle,false);context.restore();return canvas;}
Application.prototype._PrintRedrawGraph=function()
{var bbox=this.graph.getGraphBBox();var canvas=document.createElement('canvas');canvas.width=bbox.size().x;canvas.height=bbox.size().y;var context=canvas.getContext('2d');context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,bbox.minPoint.inverse(),this.backgroundPrintStyle,false,this.vertexPrintCommonStyle,this.vertexPrintSelectedVertexStyles,this.edgePrintCommonStyle,this.edgePrintSelectedStyles);context.restore();return canvas;}
{var bbox=this.graph.getGraphBBox();var canvas=document.createElement('canvas');canvas.width=bbox.size().x;canvas.height=bbox.size().y;var context=canvas.getContext('2d');context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,{width:canvas.width,height:canvas.height,scale:1.0},bbox.minPoint.inverse(),this.backgroundPrintStyle,false,this.vertexPrintCommonStyle,this.vertexPrintSelectedVertexStyles,this.edgePrintCommonStyle,this.edgePrintSelectedStyles);context.restore();return canvas;}
Application.prototype._printToSVG=function()
{var bbox=this.graph.getGraphBBox();var context=new C2S(bbox.size().x,bbox.size().y);context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,bbox.minPoint.inverse(),this.style.backgroundCommonStyle,false);context.restore();return context.getSerializedSvg();}
{var bbox=this.graph.getGraphBBox();var context=new C2S(bbox.size().x,bbox.size().y);context.save();context.translate(bbox.minPoint.inverse().x,bbox.minPoint.inverse().y);this._RedrawGraph(context,{width:bbox.size().x,height:bbox.size().y,scale:1.0},bbox.minPoint.inverse(),this.style.backgroundCommonStyle,false);context.restore();return context.getSerializedSvg();}
Application.prototype.updateRenderPathLength=function()
{this.renderPathLength=0;this.renderPathCounter=0;if(this.renderPath.length>1)
{for(var i=0;i<this.renderPath.length-1;i++)
@@ -1903,8 +1905,8 @@ else if(styleName=="printedSelected")
{return this.edgePrintSelectedStyles[correctIndex];}
return null;}
return null;}
Application.prototype._RedrawGraph=function(context,backgroundPosition,backgroundStyle,bDrawSelectedRect,forceVertexCommon,forceVertexSelected,forceEdgeCommon,forceEdgeSelected)
{var backgroundDrawer=new BaseBackgroundDrawer(context);backgroundDrawer.Draw(backgroundStyle,Math.max(this.canvas.width,this.GetRealWidth()),Math.max(this.canvas.height,this.GetRealHeight()),backgroundPosition,this.canvasScale);this.UpdateEdgesCurrentStyle(forceEdgeCommon,forceEdgeSelected);this.UpdateNodesCurrentStyle(forceVertexCommon,forceVertexSelected);this.RedrawEdges(context);this.RedrawNodes(context);if(bDrawSelectedRect&&this.selectionRect!=null)
Application.prototype._RedrawGraph=function(context,canvasParams,backgroundPosition,backgroundStyle,bDrawSelectedRect,forceVertexCommon,forceVertexSelected,forceEdgeCommon,forceEdgeSelected)
{var backgroundDrawer=new BaseBackgroundDrawer(context);backgroundDrawer.Draw(backgroundStyle,Math.max(canvasParams.width,this.GetRealWidth()),Math.max(canvasParams.height,this.GetRealHeight()),backgroundPosition,canvasParams.scale);this.UpdateEdgesCurrentStyle(forceEdgeCommon,forceEdgeSelected);this.UpdateNodesCurrentStyle(forceVertexCommon,forceVertexSelected);this.RedrawEdges(context);this.RedrawNodes(context);if(bDrawSelectedRect&&this.selectionRect!=null)
this.RedrawSelectionRect(context);}
Application.prototype.GetSelectedVertices=function()
{var res=[];for(i=0;i<this.graph.vertices.length;i++)