MCPcopy Create free account
hub / github.com/LibPDF-js/core / drawRectangle

Method drawRectangle

src/api/pdf-page.ts:853–898  ·  view source on GitHub ↗

* Draw a rectangle on the page. * * @example * ```typescript * // Filled rectangle * page.drawRectangle({ * x: 50, y: 500, width: 200, height: 100, * color: rgb(0.95, 0.95, 0.95), * }); * * // Stroked rectangle with rounded corners * page.drawRectangle({ * x

(options: DrawRectangleOptions)

Source from the content-addressed store, hash-verified

851 * ```
852 */
853 drawRectangle(options: DrawRectangleOptions): void {
854 // Register graphics state for opacity if needed
855 let gsName: string | undefined;
856
857 if (options.opacity !== undefined || options.borderOpacity !== undefined) {
858 gsName = this.registerGraphicsState({
859 fillOpacity: options.opacity,
860 strokeOpacity: options.borderOpacity,
861 });
862 }
863
864 // Register patterns if provided
865 const fillPatternName = options.pattern ? this.registerPattern(options.pattern) : undefined;
866 const strokePatternName = options.borderPattern
867 ? this.registerPattern(options.borderPattern)
868 : undefined;
869
870 // Calculate rotation center if rotating
871 let rotate: { angle: number; originX: number; originY: number } | undefined;
872
873 if (options.rotate) {
874 const bounds = { x: options.x, y: options.y, width: options.width, height: options.height };
875 const defaultOrigin = { x: options.x + options.width / 2, y: options.y + options.height / 2 };
876 const origin = resolveRotationOrigin(options.rotate.origin, bounds, defaultOrigin);
877 rotate = { angle: options.rotate.angle, originX: origin.x, originY: origin.y };
878 }
879
880 const ops = drawRectangleOps({
881 x: options.x,
882 y: options.y,
883 width: options.width,
884 height: options.height,
885 fillColor: options.color,
886 fillPatternName,
887 strokeColor: options.borderColor,
888 strokePatternName,
889 strokeWidth: options.borderWidth,
890 dashArray: options.borderDashArray,
891 dashPhase: options.borderDashPhase,
892 cornerRadius: options.cornerRadius,
893 graphicsStateName: gsName ? `/${gsName}` : undefined,
894 rotate,
895 });
896
897 this.appendOperators(ops);
898 }
899
900 /**
901 * Draw a line on the page.

Callers 15

svg.test.tsFile · 0.80
drawing.test.tsFile · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80

Calls 5

registerGraphicsStateMethod · 0.95
registerPatternMethod · 0.95
appendOperatorsMethod · 0.95
resolveRotationOriginFunction · 0.90
drawRectangleOpsFunction · 0.90

Tested by

no test coverage detected