annotation.ts 6.1 KB

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