Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Add an option to pass custom browser arguments to Playwright when launching the browser #137

Closed
nielthiart opened this issue Dec 18, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@nielthiart
Copy link
Contributor

I had the need to pass command line arguments to Playwright when launching a browser.

Chromium has a long List of Chromium Command Line Switches that users might find useful.

Why I use browser arguments: font render hinting

Font rendering in headless chrome is inconsistent between different platforms. When testing on a mac, font rendering is good, but on GitHub's action runners (Linux) the kerning is off, which affects line lengths. Some fonts at smaller sizes also appear a few pixels too high or low.

One solution to fix this, is to pass the --font-render-hinting=none argument when calling chromium.launch().

This solution comes up when users ask for help with font rendering in headless chrome, puppeteer, etc.

Examples

Here's an example of a page captured in a GitHub action with and without font render hinting:

Default font render hinting

shot-scraper "https://www.bellingcat.com/resources/2016/12/06/vacuuming-image-metadata-wayback-machine/" \
  -w 1200 -h 800 \
  -o examples/1-bellingcat-default.png

1-bellingcat-default

The kerning is incorrect on small font sizes and lines wrap earlier.

With font-render-hinting=none

shot-scraper "https://www.bellingcat.com/resources/2016/12/06/vacuuming-image-metadata-wayback-machine/" \
  -w 1200 -h 800 \
  -o examples/2-bellingcat-font-render-hinting-none.png \
  --browser-args "--font-render-hinting=none"

2-bellingcat-font-render-hinting-none

The kerning looks much better here.

Alternative solution: Unrelated to browser arguments

As a workaround, we can achieve similar font rendering results by using CSS to add text-rendering: geometricPrecision to all elements:

shot-scraper "https://www.bellingcat.com/resources/2016/12/06/vacuuming-image-metadata-wayback-machine/" \
  -w 1200 -h 800 \
  -o examples/3-bellingcat-default-geomp.png \
  --javascript "(document.head || document.documentElement).appendChild(Object.assign(document.createElement('style'), {textContent: '*, ::before, ::after { text-rendering: geometricPrecision !important; }'}));"

3-bellingcat-default-geomp

I'd prefer not to add CSS if we can launch the browser with an argument that fixes font rendering instead.

nielthiart added a commit to nielthiart/shot-scraper that referenced this issue Dec 18, 2023
Ref simonw#137

This change adds `--browser-args` option to the CLI, then passes a list of arguments to the browser launch function.

The argument can be included multiple times to list multiple arguments.

This enables users to pass flags to the browser, such as `--font-render-hinting=none`, `--disable-gpu`, etc.

These flags can have an effect on how screenshots are captured across different platforms.

Detailed reason here: simonw#137
@simonw simonw added the enhancement New feature or request label Feb 5, 2024
simonw pushed a commit that referenced this issue Feb 5, 2024
Ref #137

This change adds `--browser-args` option to the CLI, then passes a list of arguments to the browser launch function.

The argument can be included multiple times to list multiple arguments.

This enables users to pass flags to the browser, such as `--font-render-hinting=none`, `--disable-gpu`, etc.

These flags can have an effect on how screenshots are captured across different platforms.

Detailed reason here: #137
@simonw
Copy link
Owner

simonw commented Feb 5, 2024

Thanks for this, it's a really neat (and cleanly implemented) new feature.

@simonw
Copy link
Owner

simonw commented Feb 5, 2024

@simonw
Copy link
Owner

simonw commented Feb 5, 2024

My bad, I messed up my change in cc199c4. Will fix that now.

simonw added a commit that referenced this issue Feb 5, 2024
@simonw simonw closed this as completed Feb 5, 2024
@simonw
Copy link
Owner

simonw commented Feb 5, 2024

Here are the examples files that were created by the last GitHub Action CI run:

font-hinting-default:

font-hinting-default

font-hinting-none:

font-hinting-none

simonw added a commit that referenced this issue Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants