Skip to content

Commit

Permalink
Merge branch 'icon-path'
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhenn committed Feb 18, 2020
2 parents 843d67b + 5a12ce4 commit 213fc46
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 71 deletions.
18 changes: 8 additions & 10 deletions src/core/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isString,
isArrayHasData,
isSVG,
isCssUrl,
extractCssUrl,
computeDegree
} from './util';
Expand Down Expand Up @@ -66,15 +65,16 @@ const Canvas = {
if (!isNil(strokeWidth) && ctx.lineWidth !== strokeWidth) {
ctx.lineWidth = strokeWidth;
}
const strokeColor = style['linePatternFile'] || style['lineColor'] || DEFAULT_STROKE_COLOR;
const strokePattern = style['linePatternFile'];
const strokeColor = style['lineColor'] || DEFAULT_STROKE_COLOR;
if (testing) {
ctx.strokeStyle = '#000';
} else if (isImageUrl(strokeColor) && resources) {
} else if (strokePattern && resources) {
let patternOffset;
if (style['linePatternDx'] || style['linePatternDy']) {
patternOffset = [style['linePatternDx'], style['linePatternDy']];
}
Canvas._setStrokePattern(ctx, strokeColor, strokeWidth, patternOffset, resources);
Canvas._setStrokePattern(ctx, strokePattern, strokeWidth, patternOffset, resources);
//line pattern will override stroke-dasharray
style['lineDasharray'] = [];
} else if (isGradient(strokeColor)) {
Expand All @@ -95,11 +95,12 @@ const Canvas = {
if (ctx.setLineDash && isArrayHasData(style['lineDasharray'])) {
ctx.setLineDash(style['lineDasharray']);
}
const fill = style['polygonPatternFile'] || style['polygonFill'] || DEFAULT_FILL_COLOR;
const polygonPattern = style['polygonPatternFile'];
const fill = style['polygonFill'] || DEFAULT_FILL_COLOR;
if (testing) {
ctx.fillStyle = '#000';
} else if (isImageUrl(fill) && resources) {
const fillImgUrl = extractImageUrl(fill);
} else if (polygonPattern && resources) {
const fillImgUrl = extractImageUrl(polygonPattern);
let fillTexture = resources.getImage([fillImgUrl, null, null]);
if (!fillTexture) {
//if the linestring has a arrow and a linePatternFile, polygonPatternFile will be set with the linePatternFile.
Expand Down Expand Up @@ -938,9 +939,6 @@ function drawDashLine(ctx, startPoint, endPoint, dashArray) {
}

const prefix = 'data:image/';
function isImageUrl(url) {
return url.length > prefix.length && url.substring(0, prefix.length) === prefix || isCssUrl(url);
}

function extractImageUrl(url) {
if (url.substring(0, prefix.length) === prefix) {
Expand Down
33 changes: 4 additions & 29 deletions src/core/util/resource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RESOURCE_PROPERTIES, RESOURCE_SIZE_PROPERTIES } from '../Constants';
import { IS_NODE } from './env';
import { extend, isNil, isNumber, isString } from './common';
import { isURL, extractCssUrl, btoa } from './util';
import { extractCssUrl, btoa } from './util';
import { isFunctionDefinition, getFunctionTypeResources } from '../mapbox';


Expand Down Expand Up @@ -202,46 +202,21 @@ export function convertResourceUrl(symbol) {
if (!res) {
continue;
}
s[props[ii]] = _convertUrlToAbsolute(res);
s[props[ii]] = _convertUrl(res);
}
return s;
}

function _convertUrlToAbsolute(res) {
function _convertUrl(res) {
if (isFunctionDefinition(res)) {
const stops = res.stops;
for (let i = 0; i < stops.length; i++) {
stops[i][1] = _convertUrlToAbsolute(stops[i][1]);
stops[i][1] = _convertUrl(stops[i][1]);
}
return res;
}
const embed = 'data:';
if (res.slice(0, 4) === 'url(') {
res = extractCssUrl(res);
}
if (!isURL(res) &&
(res.length <= embed.length || res.substring(0, embed.length) !== embed)) {
res = _absolute(location.href, res);
}
return res;
}

function _absolute(base, relative) {
const stack = base.split('/'),
parts = relative.split('/');
if (relative.slice(0, 1) === 0) {
return stack.slice(0, 3).join('/') + relative;
} else {
stack.pop(); // remove current file name (or empty string)
// (omit if "base" is the current folder without trailing slash)
for (let i = 0; i < parts.length; i++) {
if (parts[i] === '.')
continue;
if (parts[i] === '..')
stack.pop();
else
stack.push(parts[i]);
}
return stack.join('/');
}
}
25 changes: 10 additions & 15 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,22 @@ export function isArrayHasData(obj) {
return Array.isArray(obj) && obj.length > 0;
}


const urlPattern = /^([a-z][a-z\d+\-.]*:)?\/\//i;
/**
* Whether the input string is a valid url.
* form: https://github.com/axios/axios/blob/master/lib/helpers/isAbsoluteURL.js
* @param {String} url - url to check
* @return {Boolean}
* @memberOf Util
* @private
*/
export function isURL(url) {
if (!url) {
return false;
}
const head = url.slice(0, 6);
if (head === 'http:/' || head === 'https:' || head === 'file:/') {
return true;
}
return false;
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
// by any combination of letters, digits, plus, period, or hyphen.
// eslint-disable-next-line no-useless-escape
return urlPattern.test(url);
}

//改原先的regex名字为xWithQuote;不带引号的regex,/^url\(([^\'\"].*[^\'\"])\)$/i,为xWithoutQuote。然后在is函数里||测试,extract函数里if...else处理。没引号的匹配后,取matches[1]
Expand All @@ -264,26 +264,21 @@ export function isCssUrl(str) {
if (!isString(str)) {
return 0;
}
const head = str.slice(0, 6);
if (head === 'http:/' || head === 'https:') {
return 3;
}
if (cssUrlRe.test(str)) {
return 1;
}
if (cssUrlReWithQuote.test(str)) {
return 2;
}
return 0;
return 3;
}

export function extractCssUrl(str) {
const test = isCssUrl(str);
let matches;
if (test === 3) {
return str;
}
if (test === 1) {
} else if (test === 1) {
matches = cssUrlRe.exec(str);
return matches[1];
} else if (test === 2) {
Expand Down
2 changes: 1 addition & 1 deletion test/geometry/GeometryCollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('#GeometryCollection', function () {
'markerHeight' : 50
};
var expected = {
'markerFile' : 'http://localhost:9876/test',
'markerFile' : 'test',
'markerWidth' : 40,
'markerHeight' : 50
};
Expand Down
16 changes: 6 additions & 10 deletions test/geometry/symbol/SymbolSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ describe('SymbolSpec', function () {
});

it('marker file', function () {
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
var marker = new maptalks.Marker([100, 0], {
symbol:{
'markerFile' : 'resources/x.svg',
Expand All @@ -75,7 +74,7 @@ describe('SymbolSpec', function () {
});
var res = marker._getExternalResources();
expect(res).to.have.length(1);
expect(res[0][0]).to.be.eql(expected);
expect(res[0][0]).to.be.eql('resources/x.svg');
expect(res[0][1]).to.be.eql(20);
expect(res[0][2]).to.be.eql(30);
});
Expand Down Expand Up @@ -184,27 +183,25 @@ describe('SymbolSpec', function () {
});

it('line pattern file', function () {
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
var line = new maptalks.Polygon([[100, 0], [101, 1], [105, 10], [100, 0]], {
symbol:{
'linePatternFile' : 'resources/x.svg'
}
});
var res = line._getExternalResources();
expect(res).to.have.length(1);
expect(res[0][0]).to.be.eql(expected);
expect(res[0][0]).to.be.eql('resources/x.svg');
});

it('polygon pattern file', function () {
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/x.svg';
var polygon = new maptalks.Polygon([[100, 0], [101, 1], [105, 10], [100, 0]], {
symbol:{
'polygonPatternFile' : 'resources/x.svg'
}
});
var res = polygon._getExternalResources();
expect(res).to.have.length(1);
expect(res[0][0]).to.be.eql(expected);
expect(res[0][0]).to.be.eql('resources/x.svg');
});

it('with a non-exist svg icon', function (done) {
Expand Down Expand Up @@ -267,7 +264,6 @@ describe('SymbolSpec', function () {
});

it('function type markerFile', function () {
var expected = location.href.substring(0, location.href.lastIndexOf('/')) + '/resources/';
var marker = new maptalks.Marker([100, 0], {
symbol:{
'markerFile' : {
Expand All @@ -285,9 +281,9 @@ describe('SymbolSpec', function () {
});
var res = marker._getExternalResources();
expect(res).to.have.length(3);
expect(res[0]).to.be.eql([expected + 'x.svg', 20, 30]);
expect(res[1]).to.be.eql([expected + 'x1.svg', 20, 30]);
expect(res[2]).to.be.eql([expected + 'x2.svg', 20, 30]);
expect(res[0]).to.be.eql(['resources/x.svg', 20, 30]);
expect(res[1]).to.be.eql(['resources/x1.svg', 20, 30]);
expect(res[2]).to.be.eql(['resources/x2.svg', 20, 30]);
});

it('function type markerPath', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/layer/ImageLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ describe('Layer.ImageLayer', function () {
});
layer.on('layerload', function () {
if (maptalks.Browser.ie) {
expect(layer).to.be.painted(0, 1, [0, 0, 0, 58]);
expect(layer).to.be.painted(1, 1, [0, 0, 0, 128]);
} else {
expect(layer).to.be.painted(0, 1, [0, 0, 0, 104]);
expect(layer).to.be.painted(1, 1, [0, 0, 0, 56]);
}
done();
});
Expand Down
5 changes: 2 additions & 3 deletions test/layer/InfiniteHorizonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ describe('Infinite Horizon Specs', function () {
urlTemplate : '#'
}).addTo(map);
var tiles = tile.getTiles();
expect(tiles.tileGrids.length).to.be.eql(2);
expect(tiles.tileGrids.length).to.be.eql(1);
expect(tiles.tileGrids[0].tiles.length).to.be.eql(36);
expect(tiles.tileGrids[1].tiles.length).to.be.eql(16);
});

it('cascade level 2 tiles', function () {
Expand All @@ -91,7 +90,7 @@ describe('Infinite Horizon Specs', function () {
var tiles = tile.getTiles();
expect(tiles.tileGrids.length).to.be.eql(3);
expect(tiles.tileGrids[0].tiles.length).to.be.eql(30);
expect(tiles.tileGrids[1].tiles.length).to.be.eql(5);
expect(tiles.tileGrids[1].tiles.length).to.be.eql(2);
expect(tiles.tileGrids[2].tiles.length).to.be.eql(22);
});
});
2 changes: 1 addition & 1 deletion test/map/MapCameraSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ describe('Map.Camera', function () {
it('should generate dom css matrix', function () {
map.setPitch(75);
map.setBearing(45);
expect(maptalks.Util.join(map.domCssMatrix)).to.be.eql([31.819805153394643,-8.235571585149868,0.4403135308427733,0.6830127018922193,31.819805153394636,8.23557158514987,-0.4403135308427734,-0.6830127018922194,0,-43.466662183008076,-0.16685125662045044,-0.25881904510252074,0,0,28.501478623873567,45].join());
expect(maptalks.Util.join(map.domCssMatrix)).to.be.eql([31.819805153394643,-8.235571585149868,0.8554549536393147,0.6830127018922193,31.819805153394636,8.23557158514987,-0.8554549536393148,-0.6830127018922194,0,-43.466662183008076,-0.32416386051937174,-0.25881904510252074,0,0,55.66501093030995,45].join());
});
});

0 comments on commit 213fc46

Please sign in to comment.