• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adnsistemas / pdf-lib / #18

24 Mar 2026 08:15PM UTC coverage: 74.286% (+0.3%) from 74.001%
#18

push

David N. Abdala
Documentation change

2569 of 3981 branches covered (64.53%)

Branch coverage included in aggregate %.

7372 of 9401 relevant lines covered (78.42%)

297170.51 hits per line

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

86.3
/src/core/objects/PDFArray.ts
1
import PDFBool from './PDFBool';
2
import PDFDict from './PDFDict';
3
import PDFHexString from './PDFHexString';
4
import PDFName from './PDFName';
5
import PDFNull from './PDFNull';
6
import PDFNumber from './PDFNumber';
54✔
7
import PDFObject from './PDFObject';
54✔
8
import PDFRef from './PDFRef';
9
import PDFStream from './PDFStream';
10
import PDFString from './PDFString';
11
import PDFContext from '../PDFContext';
12
import CharCodes from '../syntax/CharCodes';
54✔
13
import { PDFArrayIsNotRectangleError } from '../errors';
54✔
14
import PDFRawStream from './PDFRawStream';
15
import { isPDFInstance, PDFClasses } from '../../api/objects';
54✔
16

17
class PDFArray extends PDFObject {
18
  static className = () => PDFClasses.PDFArray;
394,844✔
19
  myClass(): PDFClasses {
20
    return PDFClasses.PDFArray;
776,917✔
21
  }
22
  static withContext = (context: PDFContext) => new PDFArray(context);
106,711✔
23

24
  private readonly array: PDFObject[];
25
  private readonly context: PDFContext;
26

27
  private constructor(context: PDFContext) {
28
    super();
106,711✔
29
    this.array = [];
106,711✔
30
    this.context = context;
106,711✔
31
  }
32

33
  size(): number {
34
    return this.array.length;
390,065✔
35
  }
36

37
  push(object: PDFObject): void {
38
    this.registerChange();
5,590,095✔
39
    this.array.push(object);
5,590,095✔
40
  }
41

42
  insert(index: number, object: PDFObject): void {
43
    this.registerChange();
241✔
44
    this.array.splice(index, 0, object);
241✔
45
  }
46

47
  indexOf(object: PDFObject): number | undefined {
48
    const index = this.array.indexOf(object);
23✔
49
    return index === -1 ? undefined : index;
23✔
50
  }
51

52
  remove(index: number): void {
53
    this.registerChange();
40✔
54
    this.array.splice(index, 1);
40✔
55
  }
56

57
  set(idx: number, object: PDFObject): void {
58
    this.registerChange();
822✔
59
    this.array[idx] = object;
822✔
60
  }
61

62
  get(index: number): PDFObject {
63
    return this.array[index];
25,386,459✔
64
  }
65

66
  lookupMaybe(index: number, type: typeof PDFArray): PDFArray | undefined;
67
  lookupMaybe(index: number, type: typeof PDFBool): PDFBool | undefined;
68
  lookupMaybe(index: number, type: typeof PDFDict): PDFDict | undefined;
69
  lookupMaybe(
70
    index: number,
71
    type: typeof PDFHexString,
72
  ): PDFHexString | undefined;
73
  lookupMaybe(index: number, type: typeof PDFName): PDFName | undefined;
74
  lookupMaybe(index: number, type: typeof PDFNull): typeof PDFNull | undefined;
75
  lookupMaybe(index: number, type: typeof PDFNumber): PDFNumber | undefined;
76
  lookupMaybe(index: number, type: typeof PDFStream): PDFStream | undefined;
77
  lookupMaybe(
78
    index: number,
79
    type: typeof PDFRawStream,
80
  ): PDFRawStream | undefined;
81
  lookupMaybe(index: number, type: typeof PDFRef): PDFRef | undefined;
82
  lookupMaybe(index: number, type: typeof PDFString): PDFString | undefined;
83
  lookupMaybe(
84
    index: number,
85
    type1: typeof PDFString,
86
    type2: typeof PDFHexString,
87
  ): PDFString | PDFHexString | undefined;
88

89
  lookupMaybe(index: number, ...types: any[]) {
90
    return this.context.lookupMaybe(
×
91
      this.get(index),
92
      // @ts-ignore
93
      ...types,
94
    ) as any;
95
  }
96

97
  lookup(index: number): PDFObject | undefined;
98
  lookup(index: number, type: typeof PDFArray): PDFArray;
99
  lookup(index: number, type: typeof PDFBool): PDFBool;
100
  lookup(index: number, type: typeof PDFDict): PDFDict;
101
  lookup(index: number, type: typeof PDFHexString): PDFHexString;
102
  lookup(index: number, type: typeof PDFName): PDFName;
103
  lookup(index: number, type: typeof PDFNull): typeof PDFNull;
104
  lookup(index: number, type: typeof PDFNumber): PDFNumber;
105
  lookup(index: number, type: typeof PDFStream): PDFStream;
106
  lookup(index: number, type: typeof PDFRawStream): PDFRawStream;
107
  lookup(index: number, type: typeof PDFRef): PDFRef;
108
  lookup(index: number, type: typeof PDFString): PDFString;
109
  lookup(
110
    index: number,
111
    type1: typeof PDFString,
112
    type2: typeof PDFHexString,
113
  ): PDFString | PDFHexString;
114

115
  lookup(index: number, ...types: any[]) {
116
    return this.context.lookup(
12,195✔
117
      this.get(index),
118
      // @ts-ignore
119
      ...types,
120
    ) as any;
121
  }
122

123
  asRectangle(): { x: number; y: number; width: number; height: number } {
124
    if (this.size() !== 4) throw new PDFArrayIsNotRectangleError(this.size());
460!
125

126
    const x1 = this.lookup(0, PDFNumber).asNumber();
460✔
127
    const y1 = this.lookup(1, PDFNumber).asNumber();
460✔
128
    const x2 = this.lookup(2, PDFNumber).asNumber();
460✔
129
    const y2 = this.lookup(3, PDFNumber).asNumber();
460✔
130

131
    const x = Math.min(x1, x2);
460✔
132
    const y = Math.min(y1, y2);
460✔
133
    const width = Math.abs(x1 - x2);
460✔
134
    const height = Math.abs(y1 - y2);
460✔
135

136
    return { x, y, width, height };
460✔
137
  }
138

139
  asArray(): PDFObject[] {
140
    return this.array.slice();
12✔
141
  }
142

143
  clone(context?: PDFContext): PDFArray {
144
    const clone = PDFArray.withContext(context || this.context);
58✔
145
    for (let idx = 0, len = this.size(); idx < len; idx++) {
58✔
146
      clone.push(this.array[idx]);
827✔
147
    }
148
    return clone;
58✔
149
  }
150

151
  toString(): string {
152
    let arrayString = '[ ';
34✔
153
    for (let idx = 0, len = this.size(); idx < len; idx++) {
34✔
154
      arrayString += this.get(idx).toString();
97✔
155
      arrayString += ' ';
97✔
156
    }
157
    arrayString += ']';
34✔
158
    return arrayString;
34✔
159
  }
160

161
  sizeInBytes(): number {
162
    let size = 3;
3,065✔
163
    for (let idx = 0, len = this.size(); idx < len; idx++) {
3,065✔
164
      size += this.get(idx).sizeInBytes() + 1;
34,138✔
165
    }
166
    return size;
3,065✔
167
  }
168

169
  copyBytesInto(buffer: Uint8Array, offset: number): number {
170
    const initialOffset = offset;
3,009✔
171

172
    buffer[offset++] = CharCodes.LeftSquareBracket;
3,009✔
173
    buffer[offset++] = CharCodes.Space;
3,009✔
174
    for (let idx = 0, len = this.size(); idx < len; idx++) {
3,009✔
175
      offset += this.get(idx).copyBytesInto(buffer, offset);
30,429✔
176
      buffer[offset++] = CharCodes.Space;
30,429✔
177
    }
178
    buffer[offset++] = CharCodes.RightSquareBracket;
3,009✔
179

180
    return offset - initialOffset;
3,009✔
181
  }
182

183
  scalePDFNumbers(x: number, y: number): void {
184
    for (let idx = 0, len = this.size(); idx < len; idx++) {
×
185
      const el = this.lookup(idx);
×
186
      if (isPDFInstance(el, PDFClasses.PDFNumber)) {
×
187
        const factor = idx % 2 === 0 ? x : y;
×
188
        this.set(idx, PDFNumber.of((el as PDFNumber).asNumber() * factor));
×
189
      }
190
    }
191
  }
192

193
  registerChange(): void {
194
    this.context.registerObjectChange(this);
5,591,198✔
195
  }
196
}
197

198
export default PDFArray;
54✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc