2 * Implementation of line breaking algorithm for the Uniscribe Script Processor
4 * Copyright 2011 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/debug.h"
35 #include "usp10_internal.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(uniscribe
);
39 extern const unsigned short wine_linebreak_table
[] DECLSPEC_HIDDEN
;
42 b_BK
=1, b_CR
, b_LF
, b_CM
, b_SG
, b_GL
, b_CB
, b_SP
, b_ZW
, b_NL
, b_WJ
, b_JL
, b_JV
, b_JT
, b_H2
, b_H3
, b_XX
, b_OP
, b_CL
,
43 b_CP
, b_QU
, b_NS
, b_EX
, b_SY
, b_IS
, b_PR
, b_PO
, b_NU
, b_AL
, b_ID
, b_IN
, b_HY
, b_BB
, b_BA
, b_SA
, b_AI
, b_B2
, b_HL
,
44 b_CJ
, b_RI
, b_EB
, b_EM
, b_ZWJ
47 enum breaking_class
{b_r
=1, b_s
, b_x
};
49 static void debug_output_breaks(const short* breaks
, int count
)
51 if (TRACE_ON(uniscribe
))
55 for (i
= 0; i
< count
&& i
< 200; i
++)
59 case b_x
: TRACE("x"); break;
60 case b_r
: TRACE("!"); break;
61 case b_s
: TRACE("+"); break;
71 static inline void else_break(short* before
, short class)
73 if (*before
== 0) *before
= class;
76 void BREAK_line(const WCHAR
*chars
, int count
, const SCRIPT_ANALYSIS
*sa
, SCRIPT_LOGATTR
*la
)
82 TRACE("In %s\n",debugstr_wn(chars
,count
));
84 break_class
= heap_alloc(count
* sizeof(*break_class
));
85 break_before
= heap_alloc(count
* sizeof(*break_before
));
87 for (i
= 0; i
< count
; i
++)
89 break_class
[i
] = get_table_entry( wine_linebreak_table
, chars
[i
] );
92 memset(&la
[i
],0,sizeof(SCRIPT_LOGATTR
));
94 la
[i
].fCharStop
= TRUE
;
95 switch (break_class
[i
])
100 la
[i
].fWhiteSpace
= TRUE
;
103 la
[i
].fCharStop
= FALSE
;
109 /* TODO: Have outside algorithms for these scripts */
110 for (i
= 0; i
< count
; i
++)
112 switch(break_class
[i
])
118 break_class
[i
] = b_AL
;
121 break_class
[i
] = b_NS
;
127 break_before
[0] = b_x
;
128 for (i
= 0; i
< count
; i
++)
130 switch(break_class
[i
])
134 if (i
< count
-1 && break_class
[i
+1] == b_LF
)
136 else_break(&break_before
[i
],b_x
);
137 else_break(&break_before
[i
+1],b_x
);
143 if (i
< count
-1) else_break(&break_before
[i
+1],b_r
);
144 else_break(&break_before
[i
],b_x
);
148 else_break(&break_before
[i
],b_x
);
151 else_break(&break_before
[i
],b_x
);
153 while (i
< count
-1 && break_class
[i
+1] == b_SP
)
155 else_break(&break_before
[i
],b_s
);
160 debug_output_breaks(break_before
,count
);
163 for (i
= 0; i
< count
; i
++)
165 if (break_class
[i
] == b_CM
)
169 switch (break_class
[i
-1])
177 break_class
[i
] = b_AL
;
180 break_class
[i
] = break_class
[i
-1];
183 else break_class
[i
] = b_AL
;
187 for (i
= 0; i
< count
; i
++)
189 switch(break_class
[i
])
193 else_break(&break_before
[i
],b_x
);
195 else_break(&break_before
[i
+1],b_x
);
200 else_break(&break_before
[i
+1],b_x
);
204 if (break_class
[i
-1] != b_SP
&&
205 break_class
[i
-1] != b_BA
&&
206 break_class
[i
-1] != b_HY
)
207 else_break(&break_before
[i
],b_x
);
216 else_break(&break_before
[i
],b_x
);
220 while (i
< count
-1 && break_class
[i
+1] == b_SP
)
222 else_break(&break_before
[i
+1],b_x
);
225 else_break(&break_before
[i
+1],b_x
);
230 while (j
< count
-1 && break_class
[j
] == b_SP
)
232 if (break_class
[j
] == b_OP
)
235 else_break(&break_before
[j
],b_x
);
241 while(j
> 0 && break_class
[j
] == b_SP
)
243 if (break_class
[j
] == b_CL
|| break_class
[j
] == b_CP
)
245 for (j
++; j
<= i
; j
++)
246 else_break(&break_before
[j
],b_x
);
252 while (j
< count
&& break_class
[j
] == b_SP
)
254 if (break_class
[j
] == b_B2
)
257 else_break(&break_before
[j
],b_x
);
263 debug_output_breaks(break_before
,count
);
265 for (i
= 0; i
< count
; i
++)
267 switch(break_class
[i
])
272 else_break(&break_before
[i
+1],b_s
);
276 else_break(&break_before
[i
],b_x
);
278 else_break(&break_before
[i
+1],b_x
);
282 else_break(&break_before
[i
],b_s
);
284 else_break(&break_before
[i
+1],b_s
);
290 else_break(&break_before
[i
],b_x
);
294 else_break(&break_before
[i
+1],b_x
);
299 switch (break_class
[i
+1])
303 else_break(&break_before
[i
+2], b_x
);
310 switch (break_class
[i
-1])
317 else_break(&break_before
[i
], b_x
);
326 if ((break_class
[i
] == b_ID
&& break_class
[i
+1] == b_PO
) ||
327 (break_class
[i
] == b_AL
&& break_class
[i
+1] == b_NU
) ||
328 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_NU
) ||
329 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_AL
) ||
330 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_HL
))
331 else_break(&break_before
[i
+1],b_x
);
333 if ((break_class
[i
] == b_PR
&& break_class
[i
+1] == b_ID
) ||
334 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_AL
) ||
335 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_HL
) ||
336 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_AL
) ||
337 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_HL
))
338 else_break(&break_before
[i
+1],b_x
);
341 if ((break_class
[i
] == b_CL
&& break_class
[i
+1] == b_PO
) ||
342 (break_class
[i
] == b_CP
&& break_class
[i
+1] == b_PO
) ||
343 (break_class
[i
] == b_CL
&& break_class
[i
+1] == b_PR
) ||
344 (break_class
[i
] == b_CP
&& break_class
[i
+1] == b_PR
) ||
345 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_PO
) ||
346 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_PR
) ||
347 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_OP
) ||
348 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_NU
) ||
349 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_OP
) ||
350 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_NU
) ||
351 (break_class
[i
] == b_HY
&& break_class
[i
+1] == b_NU
) ||
352 (break_class
[i
] == b_IS
&& break_class
[i
+1] == b_NU
) ||
353 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_NU
) ||
354 (break_class
[i
] == b_SY
&& break_class
[i
+1] == b_NU
))
355 else_break(&break_before
[i
+1],b_x
);
358 if (break_class
[i
] == b_JL
)
360 switch (break_class
[i
+1])
366 else_break(&break_before
[i
+1],b_x
);
369 if ((break_class
[i
] == b_JV
|| break_class
[i
] == b_H2
) &&
370 (break_class
[i
+1] == b_JV
|| break_class
[i
+1] == b_JT
))
371 else_break(&break_before
[i
+1],b_x
);
372 if ((break_class
[i
] == b_JT
|| break_class
[i
] == b_H3
) &&
373 break_class
[i
+1] == b_JT
)
374 else_break(&break_before
[i
+1],b_x
);
377 switch (break_class
[i
])
384 if (break_class
[i
+1] == b_IN
|| break_class
[i
+1] == b_PO
)
385 else_break(&break_before
[i
+1],b_x
);
387 if (break_class
[i
] == b_PR
)
389 switch (break_class
[i
+1])
396 else_break(&break_before
[i
+1],b_x
);
401 if ((break_class
[i
] == b_AL
&& break_class
[i
+1] == b_AL
) ||
402 (break_class
[i
] == b_AL
&& break_class
[i
+1] == b_HL
) ||
403 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_AL
) ||
404 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_HL
))
405 else_break(&break_before
[i
+1],b_x
);
408 if ((break_class
[i
] == b_IS
&& break_class
[i
+1] == b_AL
) ||
409 (break_class
[i
] == b_IS
&& break_class
[i
+1] == b_HL
))
410 else_break(&break_before
[i
+1],b_x
);
413 if ((break_class
[i
] == b_AL
|| break_class
[i
] == b_HL
|| break_class
[i
] == b_NU
) &&
414 break_class
[i
+1] == b_OP
)
415 else_break(&break_before
[i
+1],b_x
);
416 if (break_class
[i
] == b_CP
&&
417 (break_class
[i
+1] == b_AL
|| break_class
[i
+1] == b_HL
|| break_class
[i
+1] == b_NU
))
418 else_break(&break_before
[i
+1],b_x
);
421 if (break_class
[i
] == b_RI
&& break_class
[i
+1] == b_RI
)
422 else_break(&break_before
[i
+1],b_x
);
425 debug_output_breaks(break_before
,count
);
428 for (i
= 0; i
< count
-1; i
++)
429 else_break(&break_before
[i
+1],b_s
);
431 debug_output_breaks(break_before
,count
);
432 for (i
= 0; i
< count
; i
++)
434 if (break_before
[i
] != b_x
)
436 la
[i
].fSoftBreak
= TRUE
;
437 la
[i
].fWordStop
= TRUE
;
441 heap_free(break_before
);
442 heap_free(break_class
);