Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ composer require avvertix/html-shot

**Requirements**

- PHP 8.1 or higher
- PHP 8.2 or higher
- FFI extension enabled `ext-ffi` | enabled (`ffi.enable = true` in `php.ini`) |


Expand Down
5 changes: 4 additions & 1 deletion include/takumi_php.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ void takumi_output_free(struct OutputHandle *handle);
* layout still happens at the logical dimensions. Pass 0.0 to use 1.0.
* - `format`: output format string "png" | "webp" | "jpeg" (null = "png")
* - `quality`: JPEG/WebP quality 1–100 (0 = library default)
* - `base_font_size`: root font size in pixels used for `rem` and initial `em` resolution.
* Pass 0.0 to use the default (16 px, matching the browser default).
*
* Returns an `OutputHandle` on success, or null on error.
* The caller must free the handle with `takumi_output_free`.
Expand All @@ -124,6 +126,7 @@ struct OutputHandle *takumi_render_html(const struct ContextHandle *ctx,
uint32_t height,
float device_pixel_ratio,
const char *format,
uint8_t quality);
uint8_t quality,
float base_font_size);

#endif /* TAKUMI_PHP_H */
9 changes: 8 additions & 1 deletion rust/src/ffi/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::error::set_last_error;
/// layout still happens at the logical dimensions. Pass 0.0 to use 1.0.
/// - `format`: output format string "png" | "webp" | "jpeg" (null = "png")
/// - `quality`: JPEG/WebP quality 1–100 (0 = library default)
/// - `base_font_size`: root font size in pixels used for `rem` and initial `em` resolution.
/// Pass 0.0 to use the default (16 px, matching the browser default).
///
/// Returns an `OutputHandle` on success, or null on error.
/// The caller must free the handle with `takumi_output_free`.
Expand All @@ -39,6 +41,7 @@ pub unsafe extern "C" fn takumi_render_html(
device_pixel_ratio: f32,
format: *const c_char,
quality: u8,
base_font_size: f32,
) -> *mut OutputHandle {
if ctx.is_null() || html.is_null() {
set_last_error("null pointer: ctx or html");
Expand Down Expand Up @@ -93,8 +96,12 @@ pub unsafe extern "C" fn takumi_render_html(

// Render
let dpr = if device_pixel_ratio > 0.0 { device_pixel_ratio } else { 1.0 };
let mut viewport = Viewport::new((width, height)).with_device_pixel_ratio(dpr);
if base_font_size > 0.0 {
viewport = viewport.with_font_size(base_font_size);
}
let render_options = takumi::rendering::RenderOptions::builder()
.viewport(Viewport::new((width, height)).with_device_pixel_ratio(dpr))
.viewport(viewport)
.node(root_node)
.global(&ctx_data.global)
.stylesheet(stylesheet)
Expand Down
4 changes: 4 additions & 0 deletions src/HtmlShot/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(
* @param float $devicePixelRatio Output scale factor: 1.0 = normal, 2.0 = HiDPI/Retina.
* Layout stays at $width×$height logical px; the output bitmap
* is ($width * dpr) × ($height * dpr) physical pixels.
* @param float $baseFontSize Root font size in pixels for `rem` and initial `em` resolution.
* Matches the browser's `<html>` font-size. Default is 16 px.
* @return string Raw image bytes.
*
* @throws Exception\RuntimeException on render failure.
Expand All @@ -47,6 +49,7 @@ public function render(
int $quality = 0,
array $stylesheets = [],
float $devicePixelRatio = 1.0,
float $baseFontSize = 16.0,
): string {
$ffi = TakumiFfi::instance();

Expand All @@ -67,6 +70,7 @@ public function render(
(float) $devicePixelRatio,
TakumiFfi::cstring($format),
$quality,
$baseFontSize,
);

// $cssBufs must stay in scope until the FFI call returns
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlShot/TakumiFfi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @method int takumi_output_size(CData $handle)
* @method int takumi_output_save(CData $handle, CData $path)
* @method void takumi_output_free(CData $handle)
* @method ?CData takumi_render_html(CData $ctx, CData $html, ?CData $stylesheets, int $stylesheets_len, int $width, int $height, float $device_pixel_ratio, CData $format, int $quality)
* @method ?CData takumi_render_html(CData $ctx, CData $html, ?CData $stylesheets, int $stylesheets_len, int $width, int $height, float $device_pixel_ratio, CData $format, int $quality, float $base_font_size)
* @method ?CData new(string $type, bool $owned = true, bool $persistent = false)
*/
final class TakumiFfi
Expand Down