36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
||
* 低密度ディスプレイ(Samsung DeX等)でのテキスト自動拡大
|
||
*
|
||
* babel-plugin-disable-font-scaling がビルド時に全 Text/TextInput の
|
||
* style プロップを global.__scaleTextStyle() でラップする。
|
||
* このランタイム関数が render 毎に PixelRatio をチェックし、
|
||
* 低密度時のみ fontSize を拡大する。
|
||
*
|
||
* DeX実測: PR=0.756, Win=1133x690
|
||
* 補償倍率: 2.0/0.756 = 2.64 → cap 2.5x
|
||
*/
|
||
import { StyleSheet, PixelRatio } from "react-native";
|
||
|
||
const RN_DEFAULT_FONT_SIZE = 14;
|
||
|
||
function scaleTextStyle(style?: any): any {
|
||
const pr = PixelRatio.get();
|
||
if (pr >= 2.0) return style; // 通常密度 → 変更なし
|
||
|
||
// DeX(PR=0.756)向け: 大幅拡大テスト
|
||
// 3.5倍 → fontSize14→49dp, fontSize20→70dp
|
||
const compensation = Math.min(4.0, 2.625 / pr);
|
||
const flat = style != null ? StyleSheet.flatten(style) : null;
|
||
const baseSize =
|
||
flat && typeof flat === "object" && typeof flat.fontSize === "number"
|
||
? flat.fontSize
|
||
: RN_DEFAULT_FONT_SIZE;
|
||
const scaled = Math.round(baseSize * compensation);
|
||
|
||
if (style == null) return { fontSize: scaled };
|
||
return [style, { fontSize: scaled }];
|
||
}
|
||
|
||
(global as any).__scaleTextStyle = scaleTextStyle;
|
||
|