7#include "public/fpdf_text.h"
14#include "build/build_config.h"
15#include "core/fpdfapi/font/cpdf_font.h"
16#include "core/fpdfapi/page/cpdf_page.h"
17#include "core/fpdfapi/page/cpdf_textobject.h"
18#include "core/fpdfdoc/cpdf_viewerpreferences.h"
19#include "core/fpdftext/cpdf_linkextract.h"
20#include "core/fpdftext/cpdf_textpage.h"
21#include "core/fpdftext/cpdf_textpagefind.h"
22#include "core/fxcrt/check_op.h"
23#include "core/fxcrt/compiler_specific.h"
24#include "core/fxcrt/fx_memcpy_wrappers.h"
25#include "core/fxcrt/numerics/safe_conversions.h"
26#include "core/fxcrt/span.h"
27#include "core/fxcrt/span_util.h"
28#include "core/fxcrt/stl_util.h"
29#include "fpdfsdk/cpdfsdk_helpers.h"
33CPDF_TextPage* GetTextPageForValidIndex(FPDF_TEXTPAGE text_page,
int index) {
34 if (!text_page || index < 0)
38 return static_cast<size_t>(index) < textpage->size() ? textpage :
nullptr;
53 return FPDFTextPageFromCPDFTextPage(textpage.release());
58 std::unique_ptr<CPDF_TextPage> textpage_deleter(
69 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
79 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
84 return FPDFPageObjectFromCPDFPageObject(
85 textpage->GetCharInfo(index).m_pTextObj);
90 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
100 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
111 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
121 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
125 return textpage->GetCharFontSize(index);
132 unsigned long buflen,
134 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
139 if (!charinfo.m_pTextObj)
144 *flags = font->GetFontFlags();
147 auto result_span =
UNSAFE_BUFFERS(SpanFromFPDFApiArgs(buffer, buflen));
148 ByteString basefont = font->GetBaseFontName();
149 auto basefont_span = basefont.span_with_terminator();
150 fxcrt::try_spancpy(result_span, basefont_span);
151 return pdfium::checked_cast<
unsigned long>(basefont_span.size());
156 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
161 if (!charinfo.m_pTextObj)
164 return charinfo.m_pTextObj->GetFont()->GetFontWeight();
174 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
175 if (!textpage || !R || !G || !B || !A)
179 if (!charinfo.m_pTextObj)
182 FX_COLORREF fill_color = charinfo.m_pTextObj->color_state().GetFillColorRef();
186 *A = FXSYS_GetUnsignedAlpha(
187 charinfo.m_pTextObj->general_state().GetFillAlpha());
198 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
199 if (!textpage || !R || !G || !B || !A)
203 if (!charinfo.m_pTextObj)
207 charinfo.m_pTextObj->color_state().GetStrokeColorRef();
211 *A = FXSYS_GetUnsignedAlpha(
212 charinfo.m_pTextObj->general_state().GetStrokeAlpha());
218 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
242 if (!left || !right || !bottom || !top)
245 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
262 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
266 *rect = FSRectFFromCFXFloatRect(textpage->GetCharLooseBounds(index));
276 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
290 CPDF_TextPage* textpage = GetTextPageForValidIndex(text_page, index);
295 *x = charinfo.m_Origin.x;
296 *y = charinfo.m_Origin.y;
311 CFX_PointF(
static_cast<
float>(x),
static_cast<
float>(y))
,
312 CFX_SizeF(
static_cast<
float>(xTolerance),
313 static_cast<
float>(yTolerance))
);
319 unsigned short* result) {
321 if (!textpage || start_index < 0 || char_count < 0 || !result) {
325 if (char_available <= 0) {
328 char_count =
std::min(char_count, char_available);
329 if (char_count == 0) {
337 CHECK_LT(char_count,
std::numeric_limits<
int>::max());
338 pdfium::span<
unsigned short> result_span =
343 auto str_span =
fxcrt::reinterpret_span<
const unsigned short>(str.span());
346 fxcrt::Copy(str_span, result_span);
347 return pdfium::checked_cast<
int>(str_span.size());
382 unsigned short* buffer,
390 if (buflen <= 0 || !buffer) {
391 return pdfium::checked_cast<
int>(wstr.GetLength());
399 pdfium::span<
const char> str_span = str.span();
400 auto copy_span =
fxcrt::reinterpret_span<
const unsigned short>(str_span);
401 if (copy_span.size() > buffer_span.size()) {
402 copy_span = copy_span.first(buffer_span.size());
404 fxcrt::Copy(copy_span, buffer_span);
405 return pdfium::checked_cast<
int>(copy_span.size());
410 FPDF_WIDESTRING findwhat,
423 auto find = CPDF_TextPageFind::Create(
424 textpage,
UNSAFE_BUFFERS(WideStringFromFPDFWideString(findwhat)), options,
425 start_index >= 0 ? std::optional<size_t>(start_index) : std::nullopt);
428 return FPDFSchHandleFromCPDFTextPageFind(find.release());
469 std::unique_ptr<CPDF_TextPageFind> textpageFind(
481 pagelink->ExtractLinks();
484 return FPDFPageLinkFromCPDFLinkExtract(pagelink.release());
492 return pdfium::checked_cast<
int>(pageLink->CountLinks());
497 unsigned short* buffer,
500 if (link_page && link_index >= 0) {
502 wsUrl = pageLink->GetURL(link_index);
506 fxcrt::reinterpret_span<
const unsigned short>(cbUTF16URL.span());
507 if (!buffer || buflen <= 0) {
508 return pdfium::checked_cast<
int>(url_span.size());
512 pdfium::span<
unsigned short> result_span =
515 size_t size =
std::min(url_span.size(), result_span.size());
516 fxcrt::Copy(url_span.first(size), result_span);
517 return pdfium::checked_cast<
int>(size);
522 if (!link_page || link_index < 0)
526 return fxcrt::CollectionSize<
int>(pageLink->GetRects(link_index));
536 if (!link_page || link_index < 0 || rect_index < 0)
540 std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index);
541 if (rect_index >=
fxcrt::CollectionSize<
int>(rectArray))
544 *left = rectArray[rect_index].left;
545 *right = rectArray[rect_index].right;
546 *top = rectArray[rect_index].top;
547 *bottom = rectArray[rect_index].bottom;
554 int* start_char_index,
556 if (!link_page || link_index < 0)
560 auto maybe_range = page_link->GetTextRange(link_index);
561 if (!maybe_range.has_value())
564 *start_char_index =
pdfium::checked_cast<
int>(maybe_range.value().m_Start);
565 *char_count =
pdfium::checked_cast<
int>(maybe_range.value().m_Count);
fxcrt::ByteString ByteString
constexpr CFX_FloatRect(float l, float b, float r, float t)
CPDF_Document * GetDocument() const override
int GetMatchedCount() const
bool GetRect(int rectIndex, CFX_FloatRect *pRect) const
WideString GetTextByRect(const CFX_FloatRect &rect) const
int GetIndexAtPos(const CFX_PointF &point, const CFX_SizeF &tolerance) const
int CountRects(int start, int nCount)
WideString GetPageText(int start, int count) const
bool IsDirectionR2L() const
CPDF_ViewerPreferences(const CPDF_Document *pDoc)
WideString(const wchar_t *ptr)
ByteString ToUCS2LE() const
ByteString ToUTF16LE() const
#define UNSAFE_BUFFERS(...)
CPDF_TextPageFind * CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle)
CPDF_TextPage * CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE page)
FS_MATRIX FSMatrixFromCFXMatrix(const CFX_Matrix &matrix)
CPDF_Page * CPDFPageFromFPDFPage(FPDF_PAGE page)
CPDF_LinkExtract * CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetStrokeColor(FPDF_TEXTPAGE text_page, int index, unsigned int *R, unsigned int *G, unsigned int *B, unsigned int *A)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle)
FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetURL(FPDF_PAGELINK link_page, int link_index, unsigned short *buffer, int buflen)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetText(FPDF_TEXTPAGE page, int start_index, int char_count, unsigned short *result)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetTextRange(FPDF_PAGELINK link_page, int link_index, int *start_char_index, int *char_count)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_IsGenerated(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetCharOrigin(FPDF_TEXTPAGE text_page, int index, double *x, double *y)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetFillColor(FPDF_TEXTPAGE text_page, int index, unsigned int *R, unsigned int *G, unsigned int *B, unsigned int *A)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_IsHyphen(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page, double x, double y, double xTolerance, double yTolerance)
FPDF_EXPORT void FPDF_CALLCONV FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page)
FPDF_EXPORT float FPDF_CALLCONV FPDFText_GetCharAngle(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_HasUnicodeMapError(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page, double left, double top, double right, double bottom, unsigned short *buffer, int buflen)
FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountRects(FPDF_PAGELINK link_page, int link_index)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetFontWeight(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetLooseCharBox(FPDF_TEXTPAGE text_page, int index, FS_RECTF *rect)
FPDF_EXPORT double FPDF_CALLCONV FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFText_GetTextObject(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, int index, double *left, double *right, double *bottom, double *top)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetRect(FPDF_PAGELINK link_page, int link_index, int rect_index, double *left, double *top, double *right, double *bottom)
FPDF_EXPORT void FPDF_CALLCONV FPDFText_FindClose(FPDF_SCHHANDLE handle)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountRects(FPDF_TEXTPAGE text_page, int start, int count)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountChars(FPDF_TEXTPAGE text_page)
FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindPrev(FPDF_SCHHANDLE handle)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetMatrix(FPDF_TEXTPAGE text_page, int index, FS_MATRIX *matrix)
FPDF_EXPORT void FPDF_CALLCONV FPDFText_ClosePage(FPDF_TEXTPAGE text_page)
FPDF_EXPORT FPDF_PAGELINK FPDF_CALLCONV FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page)
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFText_GetFontInfo(FPDF_TEXTPAGE text_page, int index, void *buffer, unsigned long buflen, int *flags)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_GetRect(FPDF_TEXTPAGE text_page, int rect_index, double *left, double *top, double *right, double *bottom)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindNext(FPDF_SCHHANDLE handle)
FPDF_EXPORT FPDF_SCHHANDLE FPDF_CALLCONV FPDFText_FindStart(FPDF_TEXTPAGE text_page, FPDF_WIDESTRING findwhat, unsigned long flags, int start_index)
FPDF_EXPORT unsigned int FPDF_CALLCONV FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index)
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetSchCount(FPDF_SCHHANDLE handle)
FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountWebLinks(FPDF_PAGELINK link_page)
#define FPDF_MATCHWHOLEWORD
constexpr uint8_t FXSYS_GetRValue(uint32_t bgr)
constexpr uint8_t FXSYS_GetGValue(uint32_t bgr)
constexpr uint8_t FXSYS_GetBValue(uint32_t bgr)
fxcrt::WideString WideString