Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

uber / deck.gl / 13779

18 Sep 2019 - 0:00 coverage decreased (-2.9%) to 79.902%
13779

Pull #3623

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
beta.2
Pull Request #3623: Bump dependency versions

3405 of 4619 branches covered (73.72%)

Branch coverage included in aggregate %.

7031 of 8442 relevant lines covered (83.29%)

5687.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/modules/layers/src/text-layer/utils.js
1
// TODO merge with icon-layer/icon-manager
7×
2
import {log} from '@deck.gl/core';
3

4
const MISSING_CHAR_WIDTH = 32;
1×
5

6
export function nextPowOfTwo(number) {
7
  return Math.pow(2, Math.ceil(Math.log2(number)));
7×
8
}
9

10
/**
11
 * Generate character mapping table or update from an existing mapping table
12
 * @param characterSet {Array|Set} new characters
13
 * @param getFontWidth {Function} function to get width of each character
14
 * @param fontHeight {Number} height of font
15
 * @param buffer {Number} buffer surround each character
16
 * @param maxCanvasWidth {Number} max width of font atlas
17
 * @param mapping {Object} old mapping table
18
 * @param xOffset {Number} x position of last character in old mapping table
19
 * @param yOffset {Number} y position of last character in old mapping table
20
 * @returns {{
21
 *   mapping: Object,
22
 *   xOffset: Number, x position of last character
23
 *   yOffset: Number, y position of last character in old mapping table
24
 *   canvasHeight: Number, height of the font atlas canvas, power of 2
25
 *  }}
26
 */
27
export function buildMapping({
28
  characterSet,
29
  getFontWidth,
30
  fontHeight,
31
  buffer,
32
  maxCanvasWidth,
33
  mapping = {},
34
  xOffset = 0,
35
  yOffset = 0
36
}) {
37
  let row = 0;
3×
38
  // continue from x position of last character in the old mapping
39
  let x = xOffset;
3×
40
  Array.from(characterSet).forEach((char, i) => {
3×
41
    if (!mapping[char]) {
Branches [[3, 1]] missed. 102×
42
      // measure texts
43
      // TODO - use Advanced text metrics when they are adopted:
44
      // https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics
45
      const width = getFontWidth(char, i);
102×
46

47
      if (x + width + buffer * 2 > maxCanvasWidth) {
102×
48
        x = 0;
6×
49
        row++;
6×
50
      }
51
      mapping[char] = {
102×
52
        x: x + buffer,
53
        y: yOffset + row * (fontHeight + buffer * 2) + buffer,
54
        width,
55
        height: fontHeight,
56
        mask: true
57
      };
58
      x += width + buffer * 2;
102×
59
    }
60
  });
61

62
  const rowHeight = fontHeight + buffer * 2;
3×
63

64
  return {
3×
65
    mapping,
66
    xOffset: x,
67
    yOffset: yOffset + row * rowHeight,
68
    canvasHeight: nextPowOfTwo(yOffset + (row + 1) * rowHeight)
69
  };
70
}
71

72
export function transformRow(row, iconMapping, lineHeight) {
73
  let offsetLeft = 0;
15,129×
74
  let rowHeight = 0;
15,129×
75

76
  let characters = Array.from(row);
15,129×
77
  characters = characters.map((character, i) => {
15,129×
78
    const datum = {
208,759×
79
      text: character,
80
      offsetLeft
81
    };
82

83
    const frame = iconMapping[character];
208,759×
84

85
    if (frame) {
Branches [[5, 1]] missed. 208,759×
86
      offsetLeft += frame.width;
208,759×
87
      if (!rowHeight) {
208,759×
88
        // frame.height should be a constant
89
        rowHeight = frame.height * lineHeight;
15,129×
90
      }
91
    } else {
UNCOV
92
      log.warn(`Missing character: ${character}`)();
!
UNCOV
93
      offsetLeft += MISSING_CHAR_WIDTH;
!
94
    }
95

96
    return datum;
208,759×
97
  });
98

99
  return {characters, rowWidth: offsetLeft, rowHeight};
15,129×
100
}
101

102
/**
103
 * Transform a text paragraph to an array of characters, each character contains
104
 * @param paragraph {String}
105
 * @param lineHeight {Number} css line-height
106
 * @param iconMapping {Object} character mapping table for retrieving a character from font atlas
107
 * @param transformCharacter {Function} callback to transform a single character
108
 * @param transformedData {Array} output transformed data array, each datum contains
109
 *   - text: character
110
 *   - index: character index in the paragraph
111
 *   - offsetLeft: x offset in the row,
112
 *   - offsetTop: y offset in the paragraph
113
 *   - size: [width, height] size of the paragraph
114
 *   - rowSize: [rowWidth, rowHeight] size of the row
115
 *   - len: length of the paragraph
116
 */
117
export function transformParagraph(
118
  paragraph,
119
  lineHeight,
120
  iconMapping,
121
  transformCharacter,
122
  transformedData
123
) {
124
  const rows = paragraph.split('\n');
15,126×
125

126
  // width and height of the paragraph
127
  const size = [0, 0];
15,126×
128
  let offsetTop = 0;
15,126×
129

130
  rows.forEach(row => {
15,126×
131
    const {characters, rowWidth, rowHeight} = transformRow(row, iconMapping, lineHeight);
15,129×
132
    const rowSize = [rowWidth, rowHeight];
15,129×
133

134
    characters.forEach(datum => {
15,129×
135
      datum.offsetTop = offsetTop;
208,759×
136
      datum.size = size;
208,759×
137
      datum.rowSize = rowSize;
208,759×
138

139
      transformedData.push(transformCharacter(datum));
208,759×
140
    });
141

142
    offsetTop = offsetTop + rowHeight;
15,129×
143
    size[0] = Math.max(size[0], rowWidth);
15,129×
144
  });
145

146
  // last row
147
  size[1] = offsetTop;
15,126×
148
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC