@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 9.72 kB
JavaScript
import{getAssetUrl as e}from"../../assets.js";import{toHSV as t,toRGB as s}from"../../core/colorUtils.js";import"../../core/has.js";import r from"../../core/Error.js";import a from"../../core/Logger.js";import{isNone as o,unwrapOrThrow as n,isSome as l}from"../../core/maybe.js";import{eachAlways as i}from"../../core/promiseUtils.js";import{pt2px as c}from"../../core/screenUtils.js";import{defaultThematicColor as p,getPatternUrlWithColor as u,dekebabifyLineStyle as h}from"./gfxUtils.js";import{defaultPrimitive as m}from"./IconSymbol3DLayerResource.js";import{defaultPrimitive as f}from"./ObjectSymbol3DLayerResource.js";import{SymbolSizeDefaults as y,adjustColorComponentBrightness as d,getPathSymbolShapes as b,shapes as v,getExtrudeSymbolShapes as g,getTetrahedronShapes as k,getDiamondShapes as w,getCylinderShapes as x,getCubeShapes as M,getInvertedConeShapes as j,getConeShapes as L,getWaterSymbolShapes as z}from"./previewUtils.js";import{tintImageWithColor as S,renderSymbol as P}from"./renderUtils.js";import{isVolumetricSymbol as U,getIconHref as D}from"./utils.js";import{resolveWebStyleSymbol as O}from"./webStyleSymbolUtils.js";const C=y.size,I=y.maxSize,R=y.maxOutlineSize,E=y.lineWidth,Z=y.tallSymbolWidth;function q(e){const t=e.outline,s=l(e.material)?e.material.color:null,r=l(s)?s.toHex():null;if(o(t)||"pattern"in t&&l(t.pattern)&&"style"===t.pattern.type&&"none"===t.pattern.style)return"fill"===e.type&&"#ffffff"===r?{color:"#bdc3c7",width:.75}:null;const a=c(t.size)||0;return{color:"rgba("+(l(t.color)?t.color.toRgba():"255,255,255,1")+")",width:Math.min(a,R),style:"pattern"in t&&l(t.pattern)&&"style"===t.pattern.type?h(t.pattern.style):null,join:"butt",cap:"patternCap"in t?t.patternCap:"butt"}}function A(t,s){const r=s&&s.resource,a=r&&r.href;if(t.thumbnail&&t.thumbnail.url)return Promise.resolve(t.thumbnail.url);if(a&&"object"!==s.type)return Promise.resolve(D(s));const o=e("esri/images/Legend/legend3dsymboldefault.png");return t.styleOrigin&&(t.styleOrigin.styleName||t.styleOrigin.styleUrl)?O(t.styleOrigin,{portal:t.styleOrigin.portal},"webRef").catch((e=>e)).then((e=>e?.thumbnail?.url||o)):Promise.resolve(o)}function H(e,r=1){const a=e.a,o=t(e),n=o.h,l=o.s/r,i=100-(100-o.v)/r,{r:c,g:p,b:u}=s({h:n,s:l,v:i});return[c,p,u,a]}function N(e){return"water"===e.type?o(e.color)?null:e.color:o(e.material)||o(e.material.color)?null:e.material.color}function T(e,t=0){const s=N(e);if(!s){if("fill"===e.type)return null;const s=p.r,r=d(s,t);return[r,r,r,100]}const r=s.toRgba();for(let a=0;a<3;a++)r[a]=d(r[a],t);return r}async function W(t,s){const r=t.style;if("none"===r)return null;return{type:"pattern",x:0,y:0,src:await u(e(`esri/symbols/patterns/${r}.png`),s.toCss(!0)),width:5,height:5}}function $(e){return e.outline?q(e):{color:"rgba(0, 0, 0, 1)",width:1.5}}function B(e,t){const s=N(e);if(!s)return null;let r="rgba(";return r+=d(s.r,t)+",",r+=d(s.g,t)+",",r+=d(s.b,t)+",",r+s.a+");"}function F(e,t){const s=B(e,t);if(!s)return{};if("pattern"in e&&l(e.pattern)&&"style"===e.pattern.type&&"none"===e.pattern.style)return null;return{color:s,width:Math.min(e.size?c(e.size):.75,R),style:"pattern"in e&&l(e.pattern)&&"style"===e.pattern.type?h(e.pattern.style):null,cap:"cap"in e?e.cap:null,join:"join"in e?"miter"===e.join?c(2):e.join:null}}function G(e,t,s){const r=null!=s?.75*s:0;return{type:"linear",x1:r?.25*r:0,y1:r?.5*r:0,x2:r||4,y2:r?.5*r:4,colors:[{color:e,offset:0},{color:t,offset:1}]}}function J(e){const t=e.depth,s=e.height,r=e.width;return 0!==r&&0!==t&&0!==s&&r===t&&null!=r&&null!=s&&r<s}function K(e,t,s){const r=[];if(!e)return r;switch(e.type){case"icon":{const s=0,a=0,o=t,n=t;switch(e.resource&&e.resource.primitive||m){case"circle":r.push({shape:{type:"circle",cx:0,cy:0,r:.5*t},fill:T(e,0),stroke:q(e)});break;case"square":r.push({shape:{type:"path",path:[{command:"M",values:[s,n]},{command:"L",values:[s,a]},{command:"L",values:[o,a]},{command:"L",values:[o,n]},{command:"Z",values:[]}]},fill:T(e,0),stroke:q(e)});break;case"triangle":r.push({shape:{type:"path",path:[{command:"M",values:[s,n]},{command:"L",values:[.5*o,a]},{command:"L",values:[o,n]},{command:"Z",values:[]}]},fill:T(e,0),stroke:q(e)});break;case"cross":r.push({shape:{type:"path",path:[{command:"M",values:[.5*o,a]},{command:"L",values:[.5*o,n]},{command:"M",values:[s,.5*n]},{command:"L",values:[o,.5*n]}]},stroke:$(e)});break;case"x":r.push({shape:{type:"path",path:[{command:"M",values:[s,a]},{command:"L",values:[o,n]},{command:"M",values:[o,a]},{command:"L",values:[s,n]}]},stroke:$(e)});break;case"kite":r.push({shape:{type:"path",path:[{command:"M",values:[s,.5*n]},{command:"L",values:[.5*o,a]},{command:"L",values:[o,.5*n]},{command:"L",values:[.5*o,n]},{command:"Z",values:[]}]},fill:T(e,0),stroke:q(e)})}break}case"object":switch(e.resource&&e.resource.primitive||f){case"cone":{const a=G(T(e,0),T(e,-.6),s?Z:t),o=L(t,s);r.push({shape:o[0],fill:a}),r.push({shape:o[1],fill:a});break}case"inverted-cone":{const s=T(e,0),a=G(s,T(e,-.6),t),o=j(t);r.push({shape:o[0],fill:a}),r.push({shape:o[1],fill:s});break}case"cube":{const a=M(t,s);r.push({shape:a[0],fill:T(e,0)}),r.push({shape:a[1],fill:T(e,-.3)}),r.push({shape:a[2],fill:T(e,-.5)});break}case"cylinder":{const a=G(T(e,0),T(e,-.6),s?Z:t),o=x(t,s);r.push({shape:o[0],fill:a}),r.push({shape:o[1],fill:a}),r.push({shape:o[2],fill:T(e,0)});break}case"diamond":{const s=w(t);r.push({shape:s[0],fill:T(e,-.3)}),r.push({shape:s[1],fill:T(e,0)}),r.push({shape:s[2],fill:T(e,-.3)}),r.push({shape:s[3],fill:T(e,-.7)});break}case"sphere":{const s=G(T(e,0),T(e,-.6));s.x1=0,s.y1=0,s.x2=.25*t,s.y2=.25*t,r.push({shape:{type:"circle",cx:0,cy:0,r:.5*t},fill:s});break}case"tetrahedron":{const s=k(t);r.push({shape:s[0],fill:T(e,-.3)}),r.push({shape:s[1],fill:T(e,0)}),r.push({shape:s[2],fill:T(e,-.6)});break}}break}return r}function Q(e){const t="number"==typeof e?.size?e?.size:null;return t?c(t):null}function V(e){return"icon"===e.type?"multiply":"tint"}function X(e,t){const s=Q(t),r=t?.maxSize?c(t.maxSize):null,o=t?.disableUpsampling??!1,n=e.symbolLayers,l=[];let p=0,u=0;const h=n.getItemAt(n.length-1);let m;return h&&"icon"===h.type&&(m=h.size&&c(h.size)),n.forEach((a=>{if("icon"!==a.type&&"object"!==a.type)return;const n="icon"===a.type?a.size&&c(a.size):0,i=s||n?Math.ceil(Math.min(s||n,r||I)):C;if(a&&a.resource&&a.resource.href){const t=A(e,a).then((e=>{const t=a.get("material.color"),s=V(a);return S(e,i,t,s,o)})).then((e=>{const t=e.width,s=e.height;return p=Math.max(p,t),u=Math.max(u,s),[{shape:{type:"image",x:0,y:0,width:t,height:s,src:e.url},fill:null,stroke:null}]}));l.push(t)}else{let e=i;"icon"===a.type&&m&&s&&(e=i*(n/m));const r="tall"===t?.symbolConfig||t?.symbolConfig?.isTall||"object"===a.type&&J(a);p=Math.max(p,r?Z:e),u=Math.max(u,e),l.push(Promise.resolve(K(a,e,r)))}})),i(l).then((e=>{const s=[];return e.forEach((e=>{e.value?s.push(e.value):e.error&&a.getLogger("esri.symbols.support.previewSymbol3D").warn("error while building swatchInfo!",e.error)})),P(s,[p,u],{node:t&&t.node,scale:!1,opacity:t&&t.opacity})}))}function Y(e,t){const s=e.symbolLayers,r=[],a=U(e),n=Q(t),l=(t&&t.maxSize?c(t.maxSize):null)||R;let i,p=0,u=0;return s.forEach(((e,t)=>{if(!e)return;if("line"!==e.type&&"path"!==e.type)return;const s=[];switch(e.type){case"line":{const r=F(e,0);if(o(r))break;const a=r&&r.width||0;0===t&&(i=a);const c=Math.min(n||a,l),h=0===t?c:n?c*(a/i):c,m=h>E/2?2*h:E;u=Math.max(u,h),p=Math.max(p,m),r.width=h,s.push({shape:{type:"path",path:[{command:"M",values:[0,.5*u]},{command:"L",values:[p,.5*u]}]},stroke:r});break}case"path":{const t=Math.min(n||C,l),r=T(e,0),a=T(e,-.2),o=B(e,-.4),i=o?{color:o,width:1}:{};if("quad"===e.profile){const t=e.width,o=e.height,n=b(t&&o?t/o:1,0===o,0===t),l={...i,join:"bevel"};s.push({shape:n[0],fill:a,stroke:l}),s.push({shape:n[1],fill:a,stroke:l}),s.push({shape:n[2],fill:r,stroke:l})}else s.push({shape:v.pathSymbol3DLayer[0],fill:a,stroke:i}),s.push({shape:v.pathSymbol3DLayer[1],fill:r,stroke:i});u=Math.max(u,t),p=u}}r.push(s)})),Promise.resolve(P(r,[p,u],{node:t&&t.node,scale:a,opacity:t&&t.opacity}))}async function _(e,t){const s="mesh-3d"===e.type,r=e.symbolLayers,a=Q(t),i=t&&t.maxSize?c(t.maxSize):null,p=a||C,u=[];let h=0,m=0,f=!1;for(let c=0;c<r.length;c++){const e=r.getItemAt(c),t=[];if(s&&"fill"!==e.type)continue;const a=v.fill[0];switch(e.type){case"fill":{const r=q(e),o=Math.min(p,i||I);h=Math.max(h,o),m=Math.max(m,o),f=!0;let n=T(e,0);const c="pattern"in e?e.pattern:null,u=N(e);!s&&l(c)&&"style"===c.type&&"solid"!==c.style&&u&&(n=await W(c,u)),t.push({shape:a,fill:n,stroke:r});break}case"line":{const s=F(e,0);if(o(s))break;const r={stroke:s,shape:a};h=Math.max(h,C),m=Math.max(m,C),t.push(r);break}case"extrude":{const s={join:"round",width:1,...F(e,-.4)},r=T(e,0),a=T(e,-.2),o=Math.min(p,i||I),n=g(o);s.width=1,t.push({shape:n[0],fill:a,stroke:s}),t.push({shape:n[1],fill:a,stroke:s}),t.push({shape:n[2],fill:r,stroke:s});const l=C,c=.7*C+.5*o;h=Math.max(h,l),m=Math.max(m,c);break}case"water":{const s=n(N(e)),r=H(s),a=H(s,2),o=H(s,3),l=z();f=!0,t.push({shape:l[0],fill:r}),t.push({shape:l[1],fill:a}),t.push({shape:l[2],fill:o});const c=Math.min(p,i||I);h=Math.max(h,c),m=Math.max(m,c);break}}u.push(t)}return P(u,[h,m],{node:t&&t.node,scale:f,opacity:t&&t.opacity})}function ee(e,t){if(0===e.symbolLayers.length)return Promise.reject(new r("symbolPreview: renderPreviewHTML3D","No symbolLayers in the symbol."));switch(e.type){case"point-3d":return X(e,t);case"line-3d":return Y(e,t);case"polygon-3d":case"mesh-3d":return _(e,t)}return Promise.reject(new r("symbolPreview: swatchInfo3D","symbol not supported."))}export{W as getPatternDescriptor,Q as getSizeFromOptions,T as getSymbolLayerFill,ee as previewSymbol3D};