annotation.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { v4 as uuidv4 } from 'uuid';
  2. import { ANNOTATION_TYPE } from '../constants';
  3. import {
  4. AnnotationType, PositionType, ViewportType,
  5. } from '../constants/type';
  6. import { getPdfPage, renderTextLayer } from './pdf';
  7. import { getPosition, parsePositionForBackend } from './position';
  8. import { normalizeRound, floatToHex } from './utility';
  9. import { xmlParser, getElementsByTagName } from './dom';
  10. type GetFontAttributeFunc = (type: string, element: Record<string, any>) => Record<string, any>;
  11. const getContent = (type: string, element: Record<string, any>): string => {
  12. if (type !== 'Text' && type !== 'FreeText') return '';
  13. let content = '';
  14. let nodes: any = element.childNodes;
  15. nodes = Array.prototype.slice.call(nodes);
  16. nodes.forEach((ele: HTMLElement) => {
  17. if (ele.tagName === 'contents') {
  18. content = ele.innerHTML || ele.textContent || '';
  19. }
  20. });
  21. return content;
  22. };
  23. const getFontAttribute: GetFontAttributeFunc = (type, element) => {
  24. if (type !== 'FreeText') return {};
  25. const appearanceString = element.childNodes[1].innerHTML || element.childNodes[1].textContent;
  26. const arr = appearanceString.split(' ');
  27. return {
  28. fontsize: parseInt(arr[5], 10),
  29. fontname: arr[4].substr(1),
  30. textcolor: floatToHex(parseFloat(arr[0]), parseFloat(arr[1]), parseFloat(arr[2])),
  31. };
  32. };
  33. export const parseAnnotationFromXml = (xmlString: string): AnnotationType[] => {
  34. if (!xmlString) return [];
  35. const xmlDoc = xmlParser(xmlString);
  36. const elements = xmlDoc.firstElementChild || xmlDoc.firstChild;
  37. const element = getElementsByTagName(elements as ChildNode, 'annots') || [];
  38. const annotations: Element[] = Array.prototype.slice.call(element);
  39. const filterAnnots = annotations.reduce((acc: any[], cur: any) => {
  40. const type = ANNOTATION_TYPE[cur.tagName];
  41. if (type) {
  42. const page = parseInt(cur.attributes.page.value, 10);
  43. acc.push({
  44. id: uuidv4(),
  45. obj_type: type,
  46. obj_attr: {
  47. page,
  48. position: getPosition(type, cur),
  49. bdcolor: cur.attributes.color ? cur.attributes.color.value : undefined,
  50. bdwidth: cur.attributes.width ? parseInt(cur.attributes.width.value, 10) : 0,
  51. transparency: cur.attributes.opacity ? parseFloat(cur.attributes.opacity.value) : 1,
  52. content: getContent(type, cur) || undefined,
  53. fcolor: cur.attributes['interior-color'] ? cur.attributes['interior-color'].value : undefined,
  54. ftransparency: cur.attributes['interior-opacity'] ? cur.attributes['interior-opacity'].value : undefined,
  55. is_arrow: cur.attributes.tail,
  56. ...getFontAttribute(type, cur),
  57. },
  58. });
  59. }
  60. return acc;
  61. }, []);
  62. return filterAnnots;
  63. };
  64. // eslint-disable-next-line consistent-return
  65. const getEleText = (coord: any, elements: any, viewport: any, scale: any): string => {
  66. const top = normalizeRound(viewport.height - coord.top * scale);
  67. const left = normalizeRound(coord.left * scale);
  68. const bottom = normalizeRound(viewport.height - coord.bottom * scale);
  69. const right = normalizeRound(coord.right * scale);
  70. for (let i = 0, len = elements.length; i <= len; i += 1) {
  71. const element = elements[i];
  72. if (element) {
  73. const eleTop = normalizeRound(element.offsetTop);
  74. const eleLeft = normalizeRound(element.offsetLeft);
  75. const eleRight = normalizeRound(element.offsetLeft + element.offsetWidth);
  76. if (eleTop >= top && eleTop <= bottom) {
  77. const textLength = element.innerText.length;
  78. const width = element.offsetWidth;
  79. if (eleLeft < left && eleRight > right) {
  80. const distanceL = left - eleLeft;
  81. const rateL = distanceL / width;
  82. const start = Math.floor(textLength * rateL);
  83. const distanceR = eleRight - right;
  84. const rateR = distanceR / width;
  85. const end = Math.floor(textLength - (textLength * rateR));
  86. return ` ${element.innerText.slice(start, end)}`;
  87. }
  88. if (eleLeft < left && eleRight > left) {
  89. const distance = left - eleLeft;
  90. const rate = distance / width;
  91. const start = Math.floor(textLength * rate);
  92. return ` ${element.innerText.slice(start)}`;
  93. }
  94. if (eleRight > right && eleLeft < right) {
  95. const distance = eleRight - right;
  96. const rate = distance / width;
  97. const end = Math.floor(textLength - (textLength * rate));
  98. return ` ${element.innerText.slice(0, end)}`;
  99. }
  100. if (eleLeft >= left && eleRight <= right) {
  101. return ` ${element.innerText}`;
  102. }
  103. }
  104. }
  105. }
  106. return '';
  107. };
  108. export const getAnnotationText = async ({
  109. viewport, scale, page, coords, pdf,
  110. }: {
  111. viewport: ViewportType;
  112. scale: number;
  113. page: number;
  114. coords: PositionType[];
  115. pdf: any;
  116. }): Promise<any> => {
  117. const pageContainer = document.getElementById(`page_${page}`) as HTMLElement;
  118. const textLayer = pageContainer.querySelector('[data-id="text-layer"]') as HTMLElement;
  119. const pdfPage = await getPdfPage(pdf, page);
  120. if (!textLayer.childNodes.length) {
  121. await renderTextLayer({
  122. textLayer,
  123. pdfPage,
  124. viewport,
  125. });
  126. }
  127. // @ts-ignore
  128. const textElements = [...textLayer.childNodes];
  129. let text = '';
  130. for (let i = 0, len = coords.length; i < len; i += 1) {
  131. const coord = coords[i];
  132. text += getEleText(coord, textElements, viewport, scale);
  133. }
  134. return text;
  135. };
  136. export const parseAnnotationObject = ({
  137. id,
  138. obj_type,
  139. obj_attr: {
  140. page,
  141. bdcolor,
  142. transparency,
  143. fcolor,
  144. ftransparency,
  145. position = '',
  146. content,
  147. style,
  148. bdwidth,
  149. fontname,
  150. fontsize,
  151. textcolor,
  152. is_arrow,
  153. },
  154. }: AnnotationType, pageHeight: number, scale: number): AnnotationType => ({
  155. id,
  156. obj_type,
  157. obj_attr: {
  158. page: page - 1,
  159. bdcolor,
  160. position: parsePositionForBackend(obj_type, position, pageHeight, scale),
  161. transparency: transparency ? transparency * 0.01 : 0,
  162. content: content || undefined,
  163. style,
  164. fcolor,
  165. ftransparency: ftransparency ? ftransparency * 0.01 : 0,
  166. bdwidth,
  167. fontname,
  168. fontsize,
  169. textcolor,
  170. is_arrow,
  171. },
  172. });