Package Exports
- @log-er/han-span-calculator
- @log-er/han-span-calculator/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@log-er/han-span-calculator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@han-span/calculator
맑은 고딕 폰트 기준 텍스트 너비 계산 라이브러리입니다.
이 패키지는 @han-span/widths에 저장된 폰트 너비 테이블을 사용하여 텍스트의 실제 렌더링 너비를 계산합니다.
설치
npm install @han-span/calculator사용법
const HanSpanCalculator = require('@han-span/calculator');
// 12px 크기의 계산기 생성
const calculator = new HanSpanCalculator(12);
// 텍스트 너비 계산
const width = calculator.calculateTextWidth('한글 텍스트');
console.log(width); // 픽셀 너비 (예: 84.5)
// 다른 폰트 크기로 계산
const calculator14 = new HanSpanCalculator(14);
const width14 = calculator14.calculateTextWidth('한글 텍스트');
console.log(width14); // 14px 기준 너비API
new HanSpanCalculator(fontSize)
계산기 인스턴스를 생성합니다.
fontSize(number): 폰트 크기 (px). 예: 12, 14- throws: 해당 폰트 크기의 너비 테이블이 없을 경우 에러 발생
calculateTextWidth(text)
텍스트의 총 너비를 계산합니다.
text(string): 너비를 계산할 텍스트- returns: 텍스트의 총 너비 (px, number)
Fallback 처리: 폰트 테이블에 없는 문자가 발견될 경우, 공백 문자(' ')의 너비를 사용하여 합리적인 대체 처리를 합니다.
getFontSize()
현재 설정된 폰트 크기를 반환합니다.
- returns: 폰트 크기 (px, number)
예제
const calculator = new HanSpanCalculator(12);
// 한글 텍스트
calculator.calculateTextWidth('가나다라'); // 예: 72.3
// 영문 텍스트
calculator.calculateTextWidth('Hello'); // 예: 45.2
// 혼합 텍스트
calculator.calculateTextWidth('Hello 한글'); // 예: 89.5
// 특수문자 포함
calculator.calculateTextWidth('가나다!@#'); // 예: 78.4