MCPcopy Create free account
hub / github.com/creatale/node-dv / GetWordBaseline

Function GetWordBaseline

deps/tesseract/api/pdfrenderer.cpp:233–265  ·  view source on GitHub ↗

Viewers like evince can get really confused during copy-paste when the baseline wanders around. So I've decided to project every word onto the (straight) line baseline. All numbers are in the native PDF coordinate system, which has the origin in the bottom left and the unit is points, which is 1/72 inch. Tesseract reports baselines left-to-right no matter what the reading order is. We need the wor

Source from the content-addressed store, hash-verified

231// word baseline in reading order, so we do that conversion here. Returns
232// the word's baseline origin and length.
233void GetWordBaseline(int writing_direction, int ppi, int height,
234 int word_x1, int word_y1, int word_x2, int word_y2,
235 int line_x1, int line_y1, int line_x2, int line_y2,
236 double *x0, double *y0, double *length) {
237 if (writing_direction == WRITING_DIRECTION_RIGHT_TO_LEFT) {
238 Swap(&word_x1, &word_x2);
239 Swap(&word_y1, &word_y2);
240 }
241 double word_length;
242 double x, y;
243 {
244 int px = word_x1;
245 int py = word_y1;
246 double l2 = dist2(line_x1, line_y1, line_x2, line_y2);
247 if (l2 == 0) {
248 x = line_x1;
249 y = line_y1;
250 } else {
251 double t = ((px - line_x2) * (line_x2 - line_x1) +
252 (py - line_y2) * (line_y2 - line_y1)) / l2;
253 x = line_x2 + t * (line_x2 - line_x1);
254 y = line_y2 + t * (line_y2 - line_y1);
255 }
256 word_length = sqrt(static_cast<double>(dist2(word_x1, word_y1,
257 word_x2, word_y2)));
258 word_length = word_length * 72.0 / ppi;
259 x = x * 72 / ppi;
260 y = height - (y * 72.0 / ppi);
261 }
262 *x0 = x;
263 *y0 = y;
264 *length = word_length;
265}
266
267// Compute coefficients for an affine matrix describing the rotation
268// of the text. If the text is right-to-left such as Arabic or Hebrew,

Callers 1

GetPDFTextObjectsMethod · 0.85

Calls 3

SwapFunction · 0.85
dist2Function · 0.85
sqrtFunction · 0.85

Tested by

no test coverage detected