annotation.ts 6.9 KB

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