|
| 1 | +# Copyright (c) 2006, Mathieu Fenniak |
| 2 | +# Copyright (c) 2007, Ashish Kulkarni <kulkarni.ashish@gmail.com> |
| 3 | +# |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are |
| 8 | +# met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# * The name of the author may not be used to endorse or promote products |
| 16 | +# derived from this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +from typing import Any, Callable, Dict, List, Optional, Tuple, Union |
| 31 | + |
| 32 | +from .._cmap import build_font_width_map, compute_font_width, get_actual_str_key |
| 33 | +from ..generic import DictionaryObject, TextStringObject |
| 34 | +from . import get_display_str, get_text_operands |
| 35 | + |
| 36 | + |
| 37 | +class TextExtraction: |
| 38 | + """ |
| 39 | + A class to handle PDF text extraction operations. |
| 40 | +
|
| 41 | + This class encapsulates all the state and operations needed for extracting |
| 42 | + text from PDF content streams, replacing the nested functions and nonlocal |
| 43 | + variables in the original implementation. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self) -> None: |
| 47 | + self._font_width_maps: Dict[str, Tuple[Dict[Any, float], str, float]] = {} |
| 48 | + |
| 49 | + def _get_actual_font_widths( |
| 50 | + self, |
| 51 | + cmap: Tuple[ |
| 52 | + Union[str, Dict[int, str]], Dict[str, str], str, Optional[DictionaryObject] |
| 53 | + ], |
| 54 | + text_operands: str, |
| 55 | + font_size: float, |
| 56 | + space_width: float |
| 57 | + ) -> Tuple[float, float, float]: |
| 58 | + font_widths: float = 0 |
| 59 | + font_name: str = cmap[2] |
| 60 | + if font_name not in self._font_width_maps: |
| 61 | + if cmap[3] is None: |
| 62 | + font_width_map: Dict[Any, float] = {} |
| 63 | + space_char = " " |
| 64 | + actual_space_width: float = space_width |
| 65 | + font_width_map["default"] = actual_space_width * 2 |
| 66 | + else: |
| 67 | + space_char = get_actual_str_key(" ", cmap[0], cmap[1]) |
| 68 | + font_width_map = build_font_width_map(cmap[3], space_width * 2) |
| 69 | + actual_space_width = compute_font_width(font_width_map, space_char) |
| 70 | + if actual_space_width == 0: |
| 71 | + actual_space_width = space_width |
| 72 | + self._font_width_maps[font_name] = (font_width_map, space_char, actual_space_width) |
| 73 | + font_width_map = self._font_width_maps[font_name][0] |
| 74 | + space_char = self._font_width_maps[font_name][1] |
| 75 | + actual_space_width = self._font_width_maps[font_name][2] |
| 76 | + |
| 77 | + if text_operands: |
| 78 | + for char in text_operands: |
| 79 | + if char == space_char: |
| 80 | + font_widths += actual_space_width |
| 81 | + continue |
| 82 | + font_widths += compute_font_width(font_width_map, char) |
| 83 | + return (font_widths * font_size, space_width * font_size, font_size) |
| 84 | + |
| 85 | + def _handle_tj( |
| 86 | + self, |
| 87 | + text: str, |
| 88 | + operands: List[Union[str, TextStringObject]], |
| 89 | + cm_matrix: List[float], |
| 90 | + tm_matrix: List[float], |
| 91 | + cmap: Tuple[ |
| 92 | + Union[str, Dict[int, str]], Dict[str, str], str, Optional[DictionaryObject] |
| 93 | + ], |
| 94 | + orientations: Tuple[int, ...], |
| 95 | + font_size: float, |
| 96 | + rtl_dir: bool, |
| 97 | + visitor_text: Optional[Callable[[Any, Any, Any, Any, Any], None]], |
| 98 | + space_width: float, |
| 99 | + actual_str_size: Dict[str, float] |
| 100 | + ) -> Tuple[str, bool, Dict[str, float]]: |
| 101 | + text_operands, is_str_operands = get_text_operands( |
| 102 | + operands, cm_matrix, tm_matrix, cmap, orientations) |
| 103 | + if is_str_operands: |
| 104 | + text += text_operands |
| 105 | + else: |
| 106 | + text, rtl_dir = get_display_str( |
| 107 | + text, |
| 108 | + cm_matrix, |
| 109 | + tm_matrix, # text matrix |
| 110 | + cmap, |
| 111 | + text_operands, |
| 112 | + font_size, |
| 113 | + rtl_dir, |
| 114 | + visitor_text) |
| 115 | + font_widths, actual_str_size["space_width"], actual_str_size["str_height"] = ( |
| 116 | + self._get_actual_font_widths(cmap, text_operands, font_size, space_width)) |
| 117 | + actual_str_size["str_widths"] += font_widths |
| 118 | + |
| 119 | + return text, rtl_dir, actual_str_size |
0 commit comments