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

uber / deck.gl / 13720

17 Sep 2019 - 4:53 coverage increased (+2.8%) to 82.714%
13720

Pull #3588

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Try to fix examples
Pull Request #3588: Add JSON Tile3D examples to Playground

3390 of 4602 branches covered (73.66%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

479 existing lines in 86 files now uncovered.

7161 of 8154 relevant lines covered (87.82%)

4294.85 hits per line

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

92.45
/modules/layers/src/text-layer/utils.js
1
// TODO merge with icon-layer/icon-manager
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)));
6×
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;
6×
38
  // continue from x position of last character in the old mapping
39
  let x = xOffset;
5×
40
  Array.from(characterSet).forEach((char, i) => {
7×
41
    if (!mapping[char]) {
Branches [[3, 1]] missed. 1×
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);
1×
46

47
      if (x + width + buffer * 2 > maxCanvasWidth) {
1×
48
        x = 0;
7×
49
        row++;
3×
50
      }
51
      mapping[char] = {
3×
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;
3×
59
    }
60
  });
61

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

64
  return {
102×
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;
102×
74
  let rowHeight = 0;
6×
75

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

83
    const frame = iconMapping[character];
3×
84

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

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

99
  return {characters, rowWidth: offsetLeft, rowHeight};
208,759×
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');
208,759×
125

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

UNCOV
130
  rows.forEach(row => {
!
UNCOV
131
    const {characters, rowWidth, rowHeight} = transformRow(row, iconMapping, lineHeight);
!
132
    const rowSize = [rowWidth, rowHeight];
208,759×
133

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

139
      transformedData.push(transformCharacter(datum));
15,126×
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,129×
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