Skip to content

Commit 463a724

Browse files
authored
Allow to specify base font size (#3)
1 parent eac9e5a commit 463a724

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ composer require avvertix/html-shot
3232

3333
**Requirements**
3434

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

3838

include/takumi_php.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ void takumi_output_free(struct OutputHandle *handle);
112112
* layout still happens at the logical dimensions. Pass 0.0 to use 1.0.
113113
* - `format`: output format string "png" | "webp" | "jpeg" (null = "png")
114114
* - `quality`: JPEG/WebP quality 1–100 (0 = library default)
115+
* - `base_font_size`: root font size in pixels used for `rem` and initial `em` resolution.
116+
* Pass 0.0 to use the default (16 px, matching the browser default).
115117
*
116118
* Returns an `OutputHandle` on success, or null on error.
117119
* The caller must free the handle with `takumi_output_free`.
@@ -124,6 +126,7 @@ struct OutputHandle *takumi_render_html(const struct ContextHandle *ctx,
124126
uint32_t height,
125127
float device_pixel_ratio,
126128
const char *format,
127-
uint8_t quality);
129+
uint8_t quality,
130+
float base_font_size);
128131

129132
#endif /* TAKUMI_PHP_H */

rust/src/ffi/render.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use crate::error::set_last_error;
2525
/// layout still happens at the logical dimensions. Pass 0.0 to use 1.0.
2626
/// - `format`: output format string "png" | "webp" | "jpeg" (null = "png")
2727
/// - `quality`: JPEG/WebP quality 1–100 (0 = library default)
28+
/// - `base_font_size`: root font size in pixels used for `rem` and initial `em` resolution.
29+
/// Pass 0.0 to use the default (16 px, matching the browser default).
2830
///
2931
/// Returns an `OutputHandle` on success, or null on error.
3032
/// The caller must free the handle with `takumi_output_free`.
@@ -39,6 +41,7 @@ pub unsafe extern "C" fn takumi_render_html(
3941
device_pixel_ratio: f32,
4042
format: *const c_char,
4143
quality: u8,
44+
base_font_size: f32,
4245
) -> *mut OutputHandle {
4346
if ctx.is_null() || html.is_null() {
4447
set_last_error("null pointer: ctx or html");
@@ -93,8 +96,12 @@ pub unsafe extern "C" fn takumi_render_html(
9396

9497
// Render
9598
let dpr = if device_pixel_ratio > 0.0 { device_pixel_ratio } else { 1.0 };
99+
let mut viewport = Viewport::new((width, height)).with_device_pixel_ratio(dpr);
100+
if base_font_size > 0.0 {
101+
viewport = viewport.with_font_size(base_font_size);
102+
}
96103
let render_options = takumi::rendering::RenderOptions::builder()
97-
.viewport(Viewport::new((width, height)).with_device_pixel_ratio(dpr))
104+
.viewport(viewport)
98105
.node(root_node)
99106
.global(&ctx_data.global)
100107
.stylesheet(stylesheet)

src/HtmlShot/Renderer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function __construct(
3535
* @param float $devicePixelRatio Output scale factor: 1.0 = normal, 2.0 = HiDPI/Retina.
3636
* Layout stays at $width×$height logical px; the output bitmap
3737
* is ($width * dpr) × ($height * dpr) physical pixels.
38+
* @param float $baseFontSize Root font size in pixels for `rem` and initial `em` resolution.
39+
* Matches the browser's `<html>` font-size. Default is 16 px.
3840
* @return string Raw image bytes.
3941
*
4042
* @throws Exception\RuntimeException on render failure.
@@ -47,6 +49,7 @@ public function render(
4749
int $quality = 0,
4850
array $stylesheets = [],
4951
float $devicePixelRatio = 1.0,
52+
float $baseFontSize = 16.0,
5053
): string {
5154
$ffi = TakumiFfi::instance();
5255

@@ -67,6 +70,7 @@ public function render(
6770
(float) $devicePixelRatio,
6871
TakumiFfi::cstring($format),
6972
$quality,
73+
$baseFontSize,
7074
);
7175

7276
// $cssBufs must stay in scope until the FFI call returns

src/HtmlShot/TakumiFfi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @method int takumi_output_size(CData $handle)
2323
* @method int takumi_output_save(CData $handle, CData $path)
2424
* @method void takumi_output_free(CData $handle)
25-
* @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)
25+
* @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)
2626
* @method ?CData new(string $type, bool $owned = true, bool $persistent = false)
2727
*/
2828
final class TakumiFfi

0 commit comments

Comments
 (0)