Simplify code for determining label font size (eliminate style inspection)

This commit is contained in:
Bryan Housel
2016-10-17 13:57:15 -04:00
parent ae96d3942c
commit 4381bd11ac
3 changed files with 31 additions and 67 deletions

View File

@@ -3,11 +3,8 @@ import _ from 'lodash';
import rbush from 'rbush';
import { geoPathLength } from '../geo/index';
import { osmEntity } from '../osm/index';
import {
utilDisplayName,
utilEntitySelector,
utilGetStyle
} from '../util/index';
import { utilDetect } from '../util/detect';
import { utilDisplayName, utilEntitySelector } from '../util/index';
export function svgLabels(projection, context) {
@@ -18,52 +15,34 @@ export function svgLabels(projection, context) {
// Replace with dict and iterate over entities tags instead?
var label_stack = [
['line', 'aeroway'],
['line', 'highway'],
['line', 'railway'],
['line', 'waterway'],
['area', 'aeroway'],
['area', 'amenity'],
['area', 'building'],
['area', 'historic'],
['area', 'leisure'],
['area', 'man_made'],
['area', 'natural'],
['area', 'shop'],
['area', 'tourism'],
['point', 'aeroway'],
['point', 'amenity'],
['point', 'building'],
['point', 'historic'],
['point', 'leisure'],
['point', 'man_made'],
['point', 'natural'],
['point', 'shop'],
['point', 'tourism'],
['line', 'name'],
['area', 'name'],
['point', 'name']
['line', 'aeroway', 12],
['line', 'highway', 12],
['line', 'railway', 12],
['line', 'waterway', 12],
['area', 'aeroway', 12],
['area', 'amenity', 12],
['area', 'building', 12],
['area', 'historic', 12],
['area', 'leisure', 12],
['area', 'man_made', 12],
['area', 'natural', 12],
['area', 'shop', 12],
['area', 'tourism', 12],
['point', 'aeroway', 10],
['point', 'amenity', 10],
['point', 'building', 10],
['point', 'historic', 10],
['point', 'leisure', 10],
['point', 'man_made', 10],
['point', 'natural', 10],
['point', 'shop', 10],
['point', 'tourism', 10],
['line', 'name', 12],
['area', 'name', 12],
['point', 'name', 10]
];
var font_sizes = label_stack.map(function(d) {
var default_size = 12;
var style = utilGetStyle('text.' + d[0] + '.tag-' + d[1]),
m = style && style.cssText.match('font-size: ([0-9]{1,2})px;');
if (m) {
return parseInt(m[1], 10);
}
style = utilGetStyle('text.' + d[0]);
m = style && style.cssText.match('font-size: ([0-9]{1,2})px;');
if (m) {
return parseInt(m[1], 10);
}
return default_size;
});
function blacklisted(preset) {
var noIcons = ['building', 'landuse', 'natural'];
@@ -308,7 +287,7 @@ export function svgLabels(projection, context) {
// Try and find a valid label for labellable entities
for (k = 0; k < labelable.length; k++) {
var font_size = font_sizes[k];
var font_size = label_stack[k][2];
for (i = 0; i < labelable[k].length; i++) {
entity = labelable[k][i];
var name = utilDisplayName(entity),
@@ -332,13 +311,13 @@ export function svgLabels(projection, context) {
function getPointLabel(entity, width, height) {
var pointOffsets = {
ltr: [15, -11, 'start'],
rtl: [-15, -11, 'end']
ltr: [15, -10, 'start'],
rtl: [-15, -10, 'end']
};
var coord = projection(entity.loc),
margin = 5,
textDirection = iD.Detect().textDirection,
textDirection = utilDetect().textDirection,
offset = pointOffsets[textDirection],
p = {
height: height,

View File

@@ -8,7 +8,6 @@ export { utilQsString } from './util';
export { utilPrefixDOMProperty } from './util';
export { utilPrefixCSSProperty } from './util';
export { utilSetTransform } from './util';
export { utilGetStyle } from './util';
export { utilEditDistance } from './util';
export { utilFastMouse } from './util';
export { utilGetPrototypeOf } from './util';

View File

@@ -1,5 +1,4 @@
import * as d3 from 'd3';
import _ from 'lodash';
import { t, textDirection } from './locale';
import { utilDetect } from './detect';
import { remove as removeDiacritics } from 'diacritics';
@@ -126,19 +125,6 @@ export function utilSetTransform(el, x, y, scale) {
}
export function utilGetStyle(selector) {
for (var i = 0; i < document.styleSheets.length; i++) {
var rules = document.styleSheets[i].rules || document.styleSheets[i].cssRules || [];
for (var k = 0; k < rules.length; k++) {
var selectorText = rules[k].selectorText && rules[k].selectorText.split(', ');
if (_.includes(selectorText, selector)) {
return rules[k];
}
}
}
}
// Calculates Levenshtein distance between two strings
// see: https://en.wikipedia.org/wiki/Levenshtein_distance
// first converts the strings to lowercase and replaces diacritic marks with ascii equilivants.