-
-
Notifications
You must be signed in to change notification settings - Fork 252
fix: improve badge text measurement accuracy #1486
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import * as v from 'valibot' | ||
| import { createCanvas, type SKRSContext2D } from '@napi-rs/canvas' | ||
| import { hash } from 'ohash' | ||
| import { createError, getRouterParam, getQuery, setHeader } from 'h3' | ||
| import { PackageRouteParamsSchema } from '#shared/schemas/package' | ||
|
|
@@ -34,15 +35,47 @@ const COLORS = { | |
| white: '#ffffff', | ||
| } | ||
|
|
||
| const DEFAULT_CHAR_WIDTH = 7 | ||
| const CHARS_WIDTH = { | ||
| engines: 5.5, | ||
| const CHAR_WIDTH = 7 | ||
|
|
||
| const BADGE_PADDING_X = 8 | ||
| const MIN_BADGE_TEXT_WIDTH = 40 | ||
|
|
||
| const BADGE_FONT_SHORTHAND = 'normal normal 400 11px Geist, system-ui, -apple-system, sans-serif' | ||
|
|
||
| let cachedCanvasContext: SKRSContext2D | null | undefined | ||
|
|
||
| function getCanvasContext(): SKRSContext2D | null { | ||
| if (cachedCanvasContext !== undefined) { | ||
| return cachedCanvasContext | ||
| } | ||
|
|
||
| try { | ||
| cachedCanvasContext = createCanvas(1, 1).getContext('2d') | ||
| } catch { | ||
| cachedCanvasContext = null | ||
| } | ||
|
|
||
| return cachedCanvasContext | ||
| } | ||
|
Comment on lines
+47
to
59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is reusing canvas context safe? like we're not going to run into issues with methods mutating the context and causing weird behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, were not drawing anything, we're just using a method you call "what IF you drew on that canvas"? |
||
|
|
||
| function measureTextWidth(text: string, charWidth?: number): number { | ||
| charWidth ??= DEFAULT_CHAR_WIDTH | ||
| const paddingX = 8 | ||
| return Math.max(40, Math.round(text.length * charWidth) + paddingX * 2) | ||
| function fallbackMeasureTextWidth(text: string): number { | ||
| return Math.max(MIN_BADGE_TEXT_WIDTH, Math.round(text.length * CHAR_WIDTH) + BADGE_PADDING_X * 2) | ||
| } | ||
|
|
||
| function measureTextWidth(text: string): number { | ||
| const context = getCanvasContext() | ||
|
|
||
| if (context) { | ||
| context.font = BADGE_FONT_SHORTHAND | ||
|
|
||
| const measuredWidth = context.measureText(text).width | ||
|
|
||
| if (!Number.isNaN(measuredWidth)) { | ||
| return Math.max(MIN_BADGE_TEXT_WIDTH, Math.ceil(measuredWidth) + BADGE_PADDING_X * 2) | ||
| } | ||
| } | ||
|
|
||
| return fallbackMeasureTextWidth(text) | ||
| } | ||
|
|
||
| function formatBytes(bytes: number): string { | ||
|
|
@@ -296,10 +329,7 @@ export default defineCachedEventHandler( | |
| const finalLabelColor = rawLabelColor?.startsWith('#') ? rawLabelColor : `#${rawLabelColor}` | ||
|
|
||
| const leftWidth = finalLabel.trim().length === 0 ? 0 : measureTextWidth(finalLabel) | ||
| const rightWidth = measureTextWidth( | ||
| finalValue, | ||
| CHARS_WIDTH[strategyKey as keyof typeof CHARS_WIDTH], | ||
| ) | ||
| const rightWidth = measureTextWidth(finalValue) | ||
| const totalWidth = leftWidth + rightWidth | ||
wojtekmaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const height = 20 | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.