Skip to content

Commit

Permalink
feat: interpret \u+xxxx and \m+1xxxx
Browse files Browse the repository at this point in the history
  • Loading branch information
luncheon committed Feb 1, 2021
1 parent a3edb71 commit 3116d66
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ export interface DxfTextContentElement {
/** underscore */
u?: 1;
}
export declare const parseDxfTextContent: (text: string) => DxfTextContentElement[];
export declare const decodeDxfTextCharacterCodes: (text: string, mbcsEncoding?: string | TextDecoder | undefined) => string;
export declare const decodeDxfTextUnicodeCodePoints: (text: string) => string;
export declare const decodeDxfTextMbcsCharacterCodes: (text: string, encoding: string | TextDecoder) => string;
export declare const parseDxfTextContent: (text: string, options?: {
readonly encoding?: string | TextDecoder | undefined;
} | undefined) => DxfTextContentElement[];
18 changes: 11 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const t={d:"°",c:"⌀",p:"±"}
export const parseDxfTextContent=e=>{e=e.replace(/\\[uU]\+([0-9a-fA-F]{4})/g,(t,e)=>String.fromCodePoint(parseInt(e,16)))
let o=0,n={text:""}
const r=[n]
for(const x of e.matchAll(/%%(\d\d\d|.)/g)){n.text+=e.slice(o,x.index)
const s=x[1].toLowerCase(),c=t[s]
c?n.text+=c:3===s.length?n.text+=String.fromCodePoint(+s):"k"===s||"o"===s||"u"===s?(n={...n,text:""},n[s]?delete n[s]:n[s]=1,r.push(n)):n.text+=s,o=x.index+x[0].length}return n.text+=e.slice(o),r.filter(t=>t.text)}
const e={d:"°",c:"⌀",p:"±"}
export const decodeDxfTextCharacterCodes=(e,t)=>(e=decodeDxfTextUnicodeCodePoints(e),t?decodeDxfTextMbcsCharacterCodes(e,t):e)
export const decodeDxfTextUnicodeCodePoints=e=>e.replace(/\\[uU]\+([0-9a-fA-F]{4})/g,((e,t)=>String.fromCodePoint(parseInt(t,16))))
export const decodeDxfTextMbcsCharacterCodes=(e,t)=>{let o=t instanceof TextDecoder?t:void 0
return e.replace(/\\[mM]\+1([0-9a-fA-F]{2})([0-9a-fA-F]{2})/g,((e,d,n)=>(o=o||new TextDecoder(t)).decode(new Uint8Array([parseInt(d,16),parseInt(n,16)]))))}
export const parseDxfTextContent=(t,o)=>{t=decodeDxfTextCharacterCodes(t,o?.encoding)
let d=0,n={text:""}
const c=[n]
for(const o of t.matchAll(/%%(\d\d\d|.)/g)){n.text+=t.slice(d,o.index)
const r=o[1].toLowerCase(),x=e[r]
x?n.text+=x:3===r.length?n.text+=String.fromCodePoint(+r):"k"===r||"o"===r||"u"===r?(n={...n,text:""},n[r]?delete n[r]:n[r]=1,c.push(n)):n.text+=r,d=o.index+o[0].length}return n.text+=t.slice(d),c.filter((e=>e.text))}
20 changes: 18 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ const dxfTextControlCodeSymbolMap: { [c in string]?: string } = {
p: '±',
}

export const parseDxfTextContent = (text: string): DxfTextContentElement[] => {
text = text.replace(/\\[uU]\+([0-9a-fA-F]{4})/g, (_, codePoint) => String.fromCodePoint(parseInt(codePoint, 16)))
export const decodeDxfTextCharacterCodes = (text: string, mbcsEncoding?: string | TextDecoder) => {
text = decodeDxfTextUnicodeCodePoints(text)
return mbcsEncoding ? decodeDxfTextMbcsCharacterCodes(text, mbcsEncoding) : text
}

export const decodeDxfTextUnicodeCodePoints = (text: string) =>
text.replace(/\\[uU]\+([0-9a-fA-F]{4})/g, (_, codePoint) => String.fromCodePoint(parseInt(codePoint, 16)))

export const decodeDxfTextMbcsCharacterCodes = (text: string, encoding: string | TextDecoder) => {
let decoder: TextDecoder | undefined = encoding instanceof TextDecoder ? encoding : undefined
return text.replace(
/\\[mM]\+1([0-9a-fA-F]{2})([0-9a-fA-F]{2})/g,
(_, a, b) => (decoder = decoder || new TextDecoder(encoding as string)).decode(new Uint8Array([parseInt(a, 16), parseInt(b, 16)]))
)
}

export const parseDxfTextContent = (text: string, options?: { readonly encoding?: string | TextDecoder }): DxfTextContentElement[] => {
text = decodeDxfTextCharacterCodes(text, options?.encoding)
let previousIndex = 0
let currentContent: DxfTextContentElement = { text: '' }
const contents = [currentContent]
Expand Down

0 comments on commit 3116d66

Please sign in to comment.