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
[];
41 enum breaking_types
{ 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
, 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
, b_CJ
, b_RI
};
43 enum breaking_class
{b_r
=1, b_s
, b_x
};
45 static void debug_output_breaks(const short* breaks
, int count
)
47 if (TRACE_ON(uniscribe
))
51 for (i
= 0; i
< count
&& i
< 200; i
++)
55 case b_x
: TRACE("x"); break;
56 case b_r
: TRACE("!"); break;
57 case b_s
: TRACE("+"); break;
67 static inline void else_break(short* before
, short class)
69 if (*before
== 0) *before
= class;
72 void BREAK_line(const WCHAR
*chars
, int count
, const SCRIPT_ANALYSIS
*sa
, SCRIPT_LOGATTR
*la
)
78 TRACE("In %s\n",debugstr_wn(chars
,count
));
80 break_class
= HeapAlloc(GetProcessHeap(),0, count
* sizeof(short));
81 break_before
= HeapAlloc(GetProcessHeap(),0, count
* sizeof(short));
83 for (i
= 0; i
< count
; i
++)
85 break_class
[i
] = get_table_entry( wine_linebreak_table
, chars
[i
] );
88 memset(&la
[i
],0,sizeof(SCRIPT_LOGATTR
));
90 la
[i
].fCharStop
= TRUE
;
91 switch (break_class
[i
])
96 la
[i
].fWhiteSpace
= TRUE
;
99 la
[i
].fCharStop
= FALSE
;
105 /* TODO: Have outside algorithms for these scripts */
106 for (i
= 0; i
< count
; i
++)
108 switch(break_class
[i
])
114 break_class
[i
] = b_AL
;
117 break_class
[i
] = b_NS
;
123 break_before
[0] = b_x
;
124 for (i
= 0; i
< count
; i
++)
126 switch(break_class
[i
])
130 if (i
< count
-1 && break_class
[i
+1] == b_LF
)
132 else_break(&break_before
[i
],b_x
);
133 else_break(&break_before
[i
+1],b_x
);
139 if (i
< count
-1) else_break(&break_before
[i
+1],b_r
);
140 else_break(&break_before
[i
],b_x
);
144 else_break(&break_before
[i
],b_x
);
147 else_break(&break_before
[i
],b_x
);
149 while (i
< count
-1 && break_class
[i
+1] == b_SP
)
151 else_break(&break_before
[i
],b_s
);
156 debug_output_breaks(break_before
,count
);
159 for (i
= 0; i
< count
; i
++)
161 if (break_class
[i
] == b_CM
)
165 switch (break_class
[i
-1])
173 break_class
[i
] = b_AL
;
176 break_class
[i
] = break_class
[i
-1];
179 else break_class
[i
] = b_AL
;
183 for (i
= 0; i
< count
; i
++)
185 switch(break_class
[i
])
189 else_break(&break_before
[i
],b_x
);
191 else_break(&break_before
[i
+1],b_x
);
196 else_break(&break_before
[i
+1],b_x
);
200 if (break_class
[i
-1] != b_SP
&&
201 break_class
[i
-1] != b_BA
&&
202 break_class
[i
-1] != b_HY
)
203 else_break(&break_before
[i
],b_x
);
212 else_break(&break_before
[i
],b_x
);
216 while (i
< count
-1 && break_class
[i
+1] == b_SP
)
218 else_break(&break_before
[i
+1],b_x
);
221 else_break(&break_before
[i
+1],b_x
);
226 while (j
< count
-1 && break_class
[j
] == b_SP
)
228 if (break_class
[j
] == b_OP
)
231 else_break(&break_before
[j
],b_x
);
237 while(j
> 0 && break_class
[j
] == b_SP
)
239 if (break_class
[j
] == b_CL
|| break_class
[j
] == b_CP
)
241 for (j
++; j
<= i
; j
++)
242 else_break(&break_before
[j
],b_x
);
248 while (j
< count
&& break_class
[j
] == b_SP
)
250 if (break_class
[j
] == b_B2
)
253 else_break(&break_before
[j
],b_x
);
259 debug_output_breaks(break_before
,count
);
261 for (i
= 0; i
< count
; i
++)
263 switch(break_class
[i
])
268 else_break(&break_before
[i
+1],b_s
);
272 else_break(&break_before
[i
],b_x
);
274 else_break(&break_before
[i
+1],b_x
);
278 else_break(&break_before
[i
],b_s
);
280 else_break(&break_before
[i
+1],b_s
);
285 else_break(&break_before
[i
],b_x
);
289 else_break(&break_before
[i
+1],b_x
);
294 switch (break_class
[i
+1])
298 else_break(&break_before
[i
+2], b_x
);
305 switch (break_class
[i
-1])
312 else_break(&break_before
[i
], b_x
);
321 if ((break_class
[i
] == b_ID
&& break_class
[i
+1] == b_PO
) ||
322 (break_class
[i
] == b_AL
&& break_class
[i
+1] == b_NU
) ||
323 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_NU
) ||
324 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_AL
) ||
325 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_HL
))
326 else_break(&break_before
[i
+1],b_x
);
328 if ((break_class
[i
] == b_PR
&& break_class
[i
+1] == b_ID
) ||
329 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_AL
) ||
330 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_HL
) ||
331 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_AL
) ||
332 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_HL
))
333 else_break(&break_before
[i
+1],b_x
);
336 if ((break_class
[i
] == b_CL
&& break_class
[i
+1] == b_PO
) ||
337 (break_class
[i
] == b_CP
&& break_class
[i
+1] == b_PO
) ||
338 (break_class
[i
] == b_CL
&& break_class
[i
+1] == b_PR
) ||
339 (break_class
[i
] == b_CP
&& break_class
[i
+1] == b_PR
) ||
340 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_PO
) ||
341 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_PR
) ||
342 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_OP
) ||
343 (break_class
[i
] == b_PO
&& break_class
[i
+1] == b_NU
) ||
344 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_OP
) ||
345 (break_class
[i
] == b_PR
&& break_class
[i
+1] == b_NU
) ||
346 (break_class
[i
] == b_HY
&& break_class
[i
+1] == b_NU
) ||
347 (break_class
[i
] == b_IS
&& break_class
[i
+1] == b_NU
) ||
348 (break_class
[i
] == b_NU
&& break_class
[i
+1] == b_NU
) ||
349 (break_class
[i
] == b_SY
&& break_class
[i
+1] == b_NU
))
350 else_break(&break_before
[i
+1],b_x
);
353 if (break_class
[i
] == b_JL
)
355 switch (break_class
[i
+1])
361 else_break(&break_before
[i
+1],b_x
);
364 if ((break_class
[i
] == b_JV
|| break_class
[i
] == b_H2
) &&
365 (break_class
[i
+1] == b_JV
|| break_class
[i
+1] == b_JT
))
366 else_break(&break_before
[i
+1],b_x
);
367 if ((break_class
[i
] == b_JT
|| break_class
[i
] == b_H3
) &&
368 break_class
[i
+1] == b_JT
)
369 else_break(&break_before
[i
+1],b_x
);
372 switch (break_class
[i
])
379 if (break_class
[i
+1] == b_IN
|| break_class
[i
+1] == b_PO
)
380 else_break(&break_before
[i
+1],b_x
);
382 if (break_class
[i
] == b_PO
)
384 switch (break_class
[i
+1])
391 else_break(&break_before
[i
+1],b_x
);
396 if ((break_class
[i
] == b_AL
&& break_class
[i
+1] == b_AL
) ||
397 (break_class
[i
] == b_AL
&& break_class
[i
+1] == b_HL
) ||
398 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_AL
) ||
399 (break_class
[i
] == b_HL
&& break_class
[i
+1] == b_HL
))
400 else_break(&break_before
[i
+1],b_x
);
403 if ((break_class
[i
] == b_IS
&& break_class
[i
+1] == b_AL
) ||
404 (break_class
[i
] == b_IS
&& break_class
[i
+1] == b_HL
))
405 else_break(&break_before
[i
+1],b_x
);
408 if ((break_class
[i
] == b_AL
|| break_class
[i
] == b_HL
|| break_class
[i
] == b_NU
) &&
409 break_class
[i
+1] == b_OP
)
410 else_break(&break_before
[i
+1],b_x
);
411 if (break_class
[i
] == b_CP
&&
412 (break_class
[i
+1] == b_AL
|| break_class
[i
] == b_HL
|| break_class
[i
] == b_NU
))
413 else_break(&break_before
[i
+1],b_x
);
416 if (break_class
[i
] == b_RI
&& break_class
[i
+1] == b_RI
)
417 else_break(&break_before
[i
+1],b_x
);
420 debug_output_breaks(break_before
,count
);
423 for (i
= 0; i
< count
-1; i
++)
424 else_break(&break_before
[i
+1],b_s
);
426 debug_output_breaks(break_before
,count
);
427 for (i
= 0; i
< count
; i
++)
429 if (break_before
[i
] != b_x
)
431 la
[i
].fSoftBreak
= TRUE
;
432 la
[i
].fWordStop
= TRUE
;
436 HeapFree(GetProcessHeap(), 0, break_before
);
437 HeapFree(GetProcessHeap(), 0, break_class
);