Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Jan 7, 2025
1 parent e753163 commit 3c54bd3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 90 deletions.
94 changes: 4 additions & 90 deletions packages/qrx/src/qrcode/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { QRCodeOptions, RSBlock } from './types'
import { QRErrorCorrectLevel, QRMaskPattern, QRMode } from './types'
import { getAndroid, getUTF8Length, isSupportCanvas } from './utils'

/**
* A TypeScript library for working with QR codes.
*
Expand All @@ -12,88 +16,6 @@
* - https://github.com/stacksjs/qrx
*/

/**
* Enumeration of QR modes.
*/
enum QRMode {
MODE_NUMBER = 1, // 1 << 0 is equivalent to 1
MODE_ALPHA_NUM = 2, // 1 << 1 is equivalent to 2
MODE_8BIT_BYTE = 4, // 1 << 2 is equivalent to 4
MODE_KANJI = 8, // 1 << 3 is equivalent to 8
}

/**
* Enumeration of QR error-correction levels.
*/
enum QRErrorCorrectLevel {
L = 1,
M = 0,
Q = 3,
H = 2,
}

/**
* Enumeration of QR masking patterns.
*/
enum QRMaskPattern {
PATTERN000 = 0,
PATTERN001 = 1,
PATTERN010 = 2,
PATTERN011 = 3,
PATTERN100 = 4,
PATTERN101 = 5,
PATTERN110 = 6,
PATTERN111 = 7,
}

/**
* Helper interface defining the structure of RS Blocks.
*/
interface RSBlock {
totalCount: number
dataCount: number
errorCount: number
}

/**
* Default options for drawing/rendering the QRCode.
*/
interface QRCodeOptions {
text?: string
width?: number
height?: number
colorDark?: string
colorLight?: string
correctLevel?: QRErrorCorrectLevel
useSVG?: boolean // If true, will prefer SVG drawing
}

/**
* Helper function to determine if the current browser supports the Canvas API.
*/
function isSupportCanvas(): boolean {
return typeof CanvasRenderingContext2D !== 'undefined'
}

/**
* Helper function to detect Android versions (workarounds for older Android).
*/
function getAndroid(): number | false {
const sAgent = navigator.userAgent.toLowerCase()

if (sAgent.includes('android')) {
// Try to grab the version number
const match = sAgent.match(/android (\d\.\d)/i)
if (match && match[1]) {
return Number.parseFloat(match[1])
}

return 1 // No version number found
}

return false
}

/**
* A utility class for typical QR math functions, used by polynomial operations.
*/
Expand Down Expand Up @@ -1324,14 +1246,6 @@ function getTypeNumber(sText: string, nCorrectLevel: QRErrorCorrectLevel): numbe
return nType
}

/**
* Returns the length of text when encoded in UTF-8, plus BOM if needed.
*/
function getUTF8Length(sText: string): number {
const replacedText = encodeURI(sText).toString().replace(/%[0-9a-f]{2}/gi, 'a')
return replacedText.length + (replacedText.length !== sText.length ? 3 : 0)
}

/**
* Drawing interface: each method of drawing (Canvas, SVG, Table) implements `draw` and `clear`.
*/
Expand Down
57 changes: 57 additions & 0 deletions packages/qrx/src/qrcode/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Enumeration of QR modes.
*/
export enum QRMode {
MODE_NUMBER = 1, // 1 << 0 is equivalent to 1
MODE_ALPHA_NUM = 2, // 1 << 1 is equivalent to 2
MODE_8BIT_BYTE = 4, // 1 << 2 is equivalent to 4
MODE_KANJI = 8, // 1 << 3 is equivalent to 8
}

/**
* Enumeration of QR error-correction levels.
*/
export enum QRErrorCorrectLevel {
L = 1,
M = 0,
Q = 3,
H = 2,
}

/**
* Enumeration of QR masking patterns.
*/
export enum QRMaskPattern {
PATTERN000 = 0,
PATTERN001 = 1,
PATTERN010 = 2,
PATTERN011 = 3,
PATTERN100 = 4,
PATTERN101 = 5,
PATTERN110 = 6,
PATTERN111 = 7,
}

/**
* Helper interface defining the structure of RS Blocks.
*/
export interface RSBlock {
totalCount: number
dataCount: number
errorCount: number
}

/**
* Config for drawing/rendering the QRCode.
*/
export interface QRCodeConfig {
text: string
width: number
height: number
colorDark: string
colorLight: string
correctLevel: QRErrorCorrectLevel
useSVG: boolean
}

export type QRCodeOptions = Partial<QRCodeConfig>
33 changes: 33 additions & 0 deletions packages/qrx/src/qrcode/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Helper function to determine if the current browser supports the Canvas API.
*/
export function isSupportCanvas(): boolean {
return typeof CanvasRenderingContext2D !== 'undefined'
}

/**
* Helper function to detect Android versions (workarounds for older Android).
*/
export function getAndroid(): number | false {
const sAgent = navigator.userAgent.toLowerCase()

if (sAgent.includes('android')) {
// Try to grab the version number
const match = sAgent.match(/android (\d\.\d)/i)
if (match && match[1]) {
return Number.parseFloat(match[1])
}

return 1 // No version number found
}

return false
}

/**
* Returns the length of text when encoded in UTF-8, plus BOM if needed.
*/
export function getUTF8Length(sText: string): number {
const replacedText = encodeURI(sText).toString().replace(/%[0-9a-f]{2}/gi, 'a')
return replacedText.length + (replacedText.length !== sText.length ? 3 : 0)
}

0 comments on commit 3c54bd3

Please sign in to comment.