zcatpdf/examples/gradient_demo.zig
reugenio 3826cbaed4 Release v1.0 - Feature Complete PDF Generation Library
Major features added since v0.5:
- PNG support with alpha/transparency (soft masks)
- FlateDecode compression via libdeflate-zig
- Bookmarks/Outline for document navigation
- Bezier curves, circles, ellipses, arcs
- Transformations (rotate, scale, translate, skew)
- Transparency/opacity (fill and stroke alpha)
- Linear and radial gradients (Shading Patterns)
- Code128 (1D) and QR Code (2D) barcodes
- TrueType font parsing (metrics, glyph widths)
- RC4 encryption module (40/128-bit)
- AcroForms module (TextField, CheckBox)
- SVG import (basic shapes and paths)
- Template system (reusable layouts)
- Markdown styling (bold, italic, links, headings, lists)

Documentation:
- README.md: Complete API reference with code examples
- FUTURE_IMPROVEMENTS.md: Detailed roadmap for future development
- CLAUDE.md: Updated to v1.0 release status

Stats:
- 125+ unit tests passing
- 16 demo examples
- 46 source files

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 02:01:17 +01:00

120 lines
4.3 KiB
Zig

//! Gradient Demo - Linear and Radial Gradients
//!
//! Demonstrates gradient fills for rectangles, circles and ellipses.
const std = @import("std");
const zpdf = @import("zpdf");
const Pdf = zpdf.Pdf;
const Color = zpdf.Color;
const GradientDirection = zpdf.GradientDirection;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var pdf = Pdf.init(allocator, .{});
defer pdf.deinit();
pdf.setTitle("Gradient Demo");
pdf.setAuthor("zpdf");
var page = try pdf.addPage(.{});
// Title
try page.setFont(.helvetica_bold, 28);
page.setFillColor(Color.hex(0x333333));
try page.drawText(50, 780, "Gradient Demo");
try page.setFont(.helvetica, 12);
page.setFillColor(Color.hex(0x666666));
try page.drawText(50, 760, "Linear and radial gradients in zpdf");
// Section 1: Linear Gradients
try page.setFont(.helvetica_bold, 16);
page.setFillColor(Color.black);
try page.drawText(50, 720, "Linear Gradients");
// Horizontal gradient (left to right)
try page.setFont(.helvetica, 10);
try page.drawText(50, 700, "Horizontal (Red to Blue)");
try page.linearGradientRect(50, 620, 150, 70, Color.red, Color.blue, .horizontal);
// Vertical gradient (bottom to top)
try page.drawText(220, 700, "Vertical (Green to Yellow)");
try page.linearGradientRect(220, 620, 150, 70, Color.green, Color.yellow, .vertical);
// Diagonal gradient
try page.drawText(390, 700, "Diagonal (Purple to Cyan)");
try page.linearGradientRect(390, 620, 150, 70, Color.hex(0x800080), Color.cyan, .diagonal);
// Section 2: Radial Gradients
try page.setFont(.helvetica_bold, 16);
try page.drawText(50, 580, "Radial Gradients");
// Radial gradient circle
try page.setFont(.helvetica, 10);
try page.drawText(50, 555, "Circle (White to Blue)");
try page.radialGradientCircle(125, 490, 50, Color.white, Color.blue);
// Radial gradient with warm colors
try page.drawText(220, 555, "Circle (Yellow to Red)");
try page.radialGradientCircle(295, 490, 50, Color.yellow, Color.red);
// Dark theme circle
try page.drawText(390, 555, "Circle (Cyan to Dark)");
try page.radialGradientCircle(465, 490, 50, Color.cyan, Color.hex(0x001122));
// Section 3: Ellipse Gradients
try page.setFont(.helvetica_bold, 16);
try page.drawText(50, 410, "Ellipse Gradients");
// Horizontal ellipse
try page.setFont(.helvetica, 10);
try page.drawText(50, 385, "Horizontal Ellipse");
try page.radialGradientEllipse(125, 330, 70, 40, Color.white, Color.hex(0xFF6600));
// Vertical ellipse
try page.drawText(220, 385, "Vertical Ellipse");
try page.radialGradientEllipse(295, 330, 40, 55, Color.hex(0xFFFF99), Color.hex(0x006600));
// Wide ellipse
try page.drawText(390, 385, "Wide Ellipse");
try page.radialGradientEllipse(465, 330, 80, 35, Color.hex(0xFFCCFF), Color.hex(0x660066));
// Section 4: Real-world examples
try page.setFont(.helvetica_bold, 16);
try page.drawText(50, 250, "Real-world Examples");
// Button effect with gradient
try page.setFont(.helvetica, 10);
try page.drawText(50, 225, "Button Effect");
try page.linearGradientRect(50, 180, 120, 35, Color.hex(0x4A90D9), Color.hex(0x2E5C8A), .vertical);
// Button text
try page.setFont(.helvetica_bold, 12);
page.setFillColor(Color.white);
try page.drawText(78, 193, "Click Me");
// Metallic effect
try page.setFont(.helvetica, 10);
page.setFillColor(Color.black);
try page.drawText(200, 225, "Metallic Bar");
try page.linearGradientRect(200, 180, 120, 35, Color.hex(0xC0C0C0), Color.hex(0x606060), .vertical);
// Sunset gradient
try page.drawText(350, 225, "Sunset");
try page.linearGradientRect(350, 180, 120, 35, Color.hex(0xFF6600), Color.hex(0x330066), .horizontal);
// Orb/sphere effect
try page.drawText(500, 225, "Sphere");
try page.radialGradientCircle(535, 197, 25, Color.white, Color.hex(0x0066CC));
// Footer
try page.setFont(.helvetica_oblique, 10);
page.setFillColor(Color.hex(0x999999));
try page.drawText(50, 50, "Generated with zpdf - Linear and Radial Gradient Support");
try pdf.save("gradient_demo.pdf");
std.debug.print("Generated gradient_demo.pdf\n", .{});
}