1use std::collections::BTreeSet;
4
5use fontoxide_misc::Tag;
6use fontoxide_misc::text_tools::{Literal, binary2num, num2binary};
7use fontoxide_misc::xml_reader::Element;
8use fontoxide_misc::xml_writer::XmlWriter;
9
10use crate::binary::{Reader, WriteBytes, fit};
11use crate::sstruct::{Bytes, I16, U8, U16, U32, eval};
12use crate::table::{Table, TableType, TableXml};
13use crate::{Error, Font, Result};
14
15crate::sstruct! {
16 pub struct Panose = "panoseFormat" {
18 pub b_family_type: u8 = "bFamilyType" as U8,
20 pub b_serif_style: u8 = "bSerifStyle" as U8,
22 pub b_weight: u8 = "bWeight" as U8,
24 pub b_proportion: u8 = "bProportion" as U8,
26 pub b_contrast: u8 = "bContrast" as U8,
28 pub b_stroke_variation: u8 = "bStrokeVariation" as U8,
30 pub b_arm_style: u8 = "bArmStyle" as U8,
32 pub b_letter_form: u8 = "bLetterForm" as U8,
34 pub b_midline: u8 = "bMidline" as U8,
36 pub b_x_height: u8 = "bXHeight" as U8,
38 }
39}
40
41crate::sstruct! {
42 pub struct Os2Format0 = "OS2_format_0" {
44 pub version: u16 = "version" as U16,
46 pub x_avg_char_width: i16 = "xAvgCharWidth" as I16,
48 pub us_weight_class: u16 = "usWeightClass" as U16,
50 pub us_width_class: u16 = "usWidthClass" as U16,
52 pub fs_type: u16 = "fsType" as U16,
54 pub y_subscript_x_size: i16 = "ySubscriptXSize" as I16,
56 pub y_subscript_y_size: i16 = "ySubscriptYSize" as I16,
58 pub y_subscript_x_offset: i16 = "ySubscriptXOffset" as I16,
60 pub y_subscript_y_offset: i16 = "ySubscriptYOffset" as I16,
62 pub y_superscript_x_size: i16 = "ySuperscriptXSize" as I16,
64 pub y_superscript_y_size: i16 = "ySuperscriptYSize" as I16,
66 pub y_superscript_x_offset: i16 = "ySuperscriptXOffset" as I16,
68 pub y_superscript_y_offset: i16 = "ySuperscriptYOffset" as I16,
70 pub y_strikeout_size: i16 = "yStrikeoutSize" as I16,
72 pub y_strikeout_position: i16 = "yStrikeoutPosition" as I16,
74 pub s_family_class: i16 = "sFamilyClass" as I16,
76 pub panose: [u8; 10] = "panose" as Bytes<10>,
78 pub ul_unicode_range1: u32 = "ulUnicodeRange1" as U32,
80 pub ul_unicode_range2: u32 = "ulUnicodeRange2" as U32,
82 pub ul_unicode_range3: u32 = "ulUnicodeRange3" as U32,
84 pub ul_unicode_range4: u32 = "ulUnicodeRange4" as U32,
86 pub ach_vend_id: [u8; 4] = "achVendID" as Bytes<4>,
88 pub fs_selection: u16 = "fsSelection" as U16,
90 pub us_first_char_index: u16 = "usFirstCharIndex" as U16,
92 pub us_last_char_index: u16 = "usLastCharIndex" as U16,
94 pub s_typo_ascender: i16 = "sTypoAscender" as I16,
96 pub s_typo_descender: i16 = "sTypoDescender" as I16,
98 pub s_typo_line_gap: i16 = "sTypoLineGap" as I16,
100 pub us_win_ascent: u16 = "usWinAscent" as U16,
102 pub us_win_descent: u16 = "usWinDescent" as U16,
104 }
105}
106
107crate::sstruct! {
108 pub struct Os2Format1Addition = "OS2_format_1_addition" {
110 pub ul_code_page_range1: u32 = "ulCodePageRange1" as U32,
112 pub ul_code_page_range2: u32 = "ulCodePageRange2" as U32,
114 }
115}
116
117crate::sstruct! {
118 pub struct Os2Format2Addition = "OS2_format_2_addition" {
120 pub ul_code_page_range1: u32 = "ulCodePageRange1" as U32,
122 pub ul_code_page_range2: u32 = "ulCodePageRange2" as U32,
124 pub sx_height: i16 = "sxHeight" as I16,
126 pub s_cap_height: i16 = "sCapHeight" as I16,
128 pub us_default_char: u16 = "usDefaultChar" as U16,
130 pub us_break_char: u16 = "usBreakChar" as U16,
132 pub us_max_context: u16 = "usMaxContext" as U16,
134 }
135}
136
137crate::sstruct! {
138 pub struct Os2Format5Addition = "OS2_format_5_addition" {
141 pub ul_code_page_range1: u32 = "ulCodePageRange1" as U32,
143 pub ul_code_page_range2: u32 = "ulCodePageRange2" as U32,
145 pub sx_height: i16 = "sxHeight" as I16,
147 pub s_cap_height: i16 = "sCapHeight" as I16,
149 pub us_default_char: u16 = "usDefaultChar" as U16,
151 pub us_break_char: u16 = "usBreakChar" as U16,
153 pub us_max_context: u16 = "usMaxContext" as U16,
155 pub us_lower_optical_point_size: u16 = "usLowerOpticalPointSize" as U16,
157 pub us_upper_optical_point_size: u16 = "usUpperOpticalPointSize" as U16,
159 }
160}
161
162#[derive(Debug, Clone, PartialEq, Default)]
169pub struct Os2 {
170 pub base: Os2Format0,
172 pub panose: Panose,
174 pub ul_code_page_range1: u32,
176 pub ul_code_page_range2: u32,
178 pub sx_height: i16,
180 pub s_cap_height: i16,
182 pub us_default_char: u16,
184 pub us_break_char: u16,
186 pub us_max_context: u16,
188 pub us_lower_optical_point_size: f64,
190 pub us_upper_optical_point_size: f64,
192}
193
194impl std::ops::Deref for Os2 {
195 type Target = Os2Format0;
196 fn deref(&self) -> &Os2Format0 {
197 &self.base
198 }
199}
200
201impl std::ops::DerefMut for Os2 {
202 fn deref_mut(&mut self) -> &mut Os2Format0 {
203 &mut self.base
204 }
205}
206
207const RANGE_FIELDS: [&str; 6] = [
208 "ulUnicodeRange1",
209 "ulUnicodeRange2",
210 "ulUnicodeRange3",
211 "ulUnicodeRange4",
212 "ulCodePageRange1",
213 "ulCodePageRange2",
214];
215
216enum Field {
218 Int(i64),
219 Float(f64),
220 Panose,
221 VendId,
222}
223
224impl Os2 {
225 fn field_names(&self) -> Vec<&'static str> {
227 let mut names: Vec<&'static str> = Os2Format0::FIELDS.iter().map(|f| f.name).collect();
228 let extra: &[crate::sstruct::FieldInfo] = match self.version {
229 1 => Os2Format1Addition::FIELDS,
230 2..=4 => Os2Format2Addition::FIELDS,
231 5 => Os2Format5Addition::FIELDS,
232 _ => &[],
233 };
234 names.extend(extra.iter().map(|f| f.name));
235 names
236 }
237
238 fn field(&self, name: &str) -> Field {
239 let b = &self.base;
240 Field::Int(match name {
241 "panose" => return Field::Panose,
242 "achVendID" => return Field::VendId,
243 "usLowerOpticalPointSize" => return Field::Float(self.us_lower_optical_point_size),
244 "usUpperOpticalPointSize" => return Field::Float(self.us_upper_optical_point_size),
245 "ulCodePageRange1" => self.ul_code_page_range1 as i64,
246 "ulCodePageRange2" => self.ul_code_page_range2 as i64,
247 "sxHeight" => self.sx_height as i64,
248 "sCapHeight" => self.s_cap_height as i64,
249 "usDefaultChar" => self.us_default_char as i64,
250 "usBreakChar" => self.us_break_char as i64,
251 "usMaxContext" => self.us_max_context as i64,
252 _ => match b.values().into_iter().find(|(n, _)| *n == name) {
253 Some((_, crate::sstruct::Value::Int(i))) => i,
254 _ => 0,
255 },
256 })
257 }
258
259 pub fn unicode_ranges(&self) -> std::collections::BTreeSet<u32> {
261 let ranges = [
262 self.ul_unicode_range1,
263 self.ul_unicode_range2,
264 self.ul_unicode_range3,
265 self.ul_unicode_range4,
266 ];
267 let mut bits = std::collections::BTreeSet::new();
268 for (k, r) in ranges.iter().enumerate() {
269 for i in 0..32 {
270 if r & (1 << i) != 0 {
271 bits.insert(i + 32 * k as u32);
272 }
273 }
274 }
275 bits
276 }
277
278 pub fn set_unicode_ranges(&mut self, bits: impl IntoIterator<Item = u32>) -> Result<()> {
280 let mut ul = [0u32; 4];
281 for bit in bits {
282 if bit >= 128 {
283 return Err(Error::value(format!("expected 0 <= int <= 127: {bit}")));
284 }
285 ul[(bit / 32) as usize] |= 1 << (bit % 32);
286 }
287 [
288 self.base.ul_unicode_range1,
289 self.base.ul_unicode_range2,
290 self.base.ul_unicode_range3,
291 self.base.ul_unicode_range4,
292 ] = ul;
293 Ok(())
294 }
295
296 pub fn recalc_unicode_ranges(
300 &mut self,
301 unicodes: &BTreeSet<u32>,
302 prune_only: bool,
303 ) -> Result<BTreeSet<u32>> {
304 let bits = if prune_only {
305 let empty = intersect_unicode_ranges(unicodes, true);
306 self.unicode_ranges().difference(&empty).copied().collect()
307 } else {
308 intersect_unicode_ranges(unicodes, false)
309 };
310 self.set_unicode_ranges(bits.iter().copied())?;
311 Ok(bits)
312 }
313
314 pub fn code_page_ranges(&self) -> BTreeSet<u32> {
316 let mut bits = BTreeSet::new();
317 if self.version < 1 {
318 return bits;
319 }
320 for (k, r) in [self.ul_code_page_range1, self.ul_code_page_range2]
321 .iter()
322 .enumerate()
323 {
324 for i in 0..32 {
325 if r & (1 << i) != 0 {
326 bits.insert(i + 32 * k as u32);
327 }
328 }
329 }
330 bits
331 }
332
333 pub fn set_code_page_ranges(&mut self, bits: impl IntoIterator<Item = u32>) -> Result<()> {
336 let mut ul = [0u32; 2];
337 for bit in bits {
338 if bit >= 64 {
339 return Err(Error::value(format!(
340 "expected 0 <= int <= 63, found: {bit}"
341 )));
342 }
343 ul[(bit / 32) as usize] |= 1 << (bit % 32);
344 }
345 if self.version < 1 {
346 self.base.version = 1;
347 }
348 [self.ul_code_page_range1, self.ul_code_page_range2] = ul;
349 Ok(())
350 }
351
352 pub fn recalc_code_page_ranges(
355 &mut self,
356 unicodes: &BTreeSet<u32>,
357 prune_only: bool,
358 ) -> Result<BTreeSet<u32>> {
359 let mut bits = calc_code_page_ranges(unicodes);
360 if prune_only {
361 let current = self.code_page_ranges();
362 bits.retain(|b| current.contains(b));
363 }
364 if bits.is_empty() {
365 bits.insert(0);
367 }
368 self.set_code_page_ranges(bits.iter().copied())?;
369 Ok(bits)
370 }
371
372 pub fn recalc_avg_char_width(&mut self, widths: impl IntoIterator<Item = u16>) -> i16 {
374 let widths: Vec<u16> = widths.into_iter().filter(|&w| w > 0).collect();
375 let avg = if widths.is_empty() {
376 0
377 } else {
378 let sum: u64 = widths.iter().map(|&w| u64::from(w)).sum();
379 fontoxide_misc::round_tools::ot_round(sum as f64 / widths.len() as f64) as i16
380 };
381 self.base.x_avg_char_width = avg;
382 avg
383 }
384
385 pub fn update_first_and_last_char_index(&mut self, font: &mut Font) -> Result<()> {
388 if let Some((min, max)) = crate::tables::cmap::unicode_code_range(font)? {
389 self.base.us_first_char_index = min.min(0xFFFF) as u16;
391 self.base.us_last_char_index = max.min(0xFFFF) as u16;
392 }
393 Ok(())
394 }
395
396 fn check_head_mac_style(&self, tag: &[u8; 4], font: &mut Font) -> Result<()> {
397 let tag = Tag::new(tag);
398 if !font.contains(tag) {
399 return Ok(());
400 }
401 let mac_style = if tag == *b"head" {
403 font.require::<crate::tables::Head>()?.mac_style
404 } else {
405 font.require::<crate::tables::Bhed>()?.mac_style
406 };
407 if self.fs_selection & 1 != 0 && mac_style & (1 << 1) == 0 {
408 log::warn!(
409 "fsSelection bit 0 (italic) and {tag} table macStyle bit 1 (italic) should match"
410 );
411 }
412 if self.fs_selection & (1 << 5) != 0 && mac_style & 1 == 0 {
413 log::warn!(
414 "fsSelection bit 5 (bold) and {tag} table macStyle bit 0 (bold) should match"
415 );
416 }
417 Ok(())
418 }
419}
420
421impl TableType for Os2 {
422 const TAG: Tag = Tag::new(b"OS/2");
423
424 fn decompile(data: &[u8], _font: &mut Font) -> Result<Self> {
425 let (base, rest) = Os2Format0::unpack2(data)?;
426 let mut os2 = Os2 {
427 panose: Panose::unpack(&mut Reader::new(&base.panose))?,
428 base,
429 ..Default::default()
430 };
431 let rest = match os2.version {
432 0 => rest,
433 1 => {
434 let (a, rest) = Os2Format1Addition::unpack2(rest)?;
435 os2.ul_code_page_range1 = a.ul_code_page_range1;
436 os2.ul_code_page_range2 = a.ul_code_page_range2;
437 rest
438 }
439 2..=4 => {
440 let (a, rest) = Os2Format2Addition::unpack2(rest)?;
441 os2.ul_code_page_range1 = a.ul_code_page_range1;
442 os2.ul_code_page_range2 = a.ul_code_page_range2;
443 os2.sx_height = a.sx_height;
444 os2.s_cap_height = a.s_cap_height;
445 os2.us_default_char = a.us_default_char;
446 os2.us_break_char = a.us_break_char;
447 os2.us_max_context = a.us_max_context;
448 rest
449 }
450 5 => {
451 let (a, rest) = Os2Format5Addition::unpack2(rest)?;
452 os2.ul_code_page_range1 = a.ul_code_page_range1;
453 os2.ul_code_page_range2 = a.ul_code_page_range2;
454 os2.sx_height = a.sx_height;
455 os2.s_cap_height = a.s_cap_height;
456 os2.us_default_char = a.us_default_char;
457 os2.us_break_char = a.us_break_char;
458 os2.us_max_context = a.us_max_context;
459 os2.us_lower_optical_point_size = a.us_lower_optical_point_size as f64 / 20.0;
460 os2.us_upper_optical_point_size = a.us_upper_optical_point_size as f64 / 20.0;
461 rest
462 }
463 v => {
464 return Err(Error::ttlib(format!(
465 "unknown format for OS/2 table: version {v}"
466 )));
467 }
468 };
469 if !rest.is_empty() {
470 log::warn!("too much 'OS/2' table data");
471 }
472 Ok(os2)
473 }
474}
475
476impl Table for Os2 {
477 fn remap_glyphs(&mut self, f: &mut dyn FnMut(u32) -> Result<u32>) -> Result<()> {
478 let _ = f;
479 Ok(())
480 }
481
482 fn tag(&self) -> Tag {
483 Self::TAG
484 }
485
486 fn compile(&mut self, font: &mut Font) -> Result<Vec<u8>> {
487 self.update_first_and_last_char_index(font)?;
488 self.check_head_mac_style(b"head", font)?;
489 self.check_head_mac_style(b"bhed", font)?;
490 if self.fs_selection & (1 << 6) != 0 && self.fs_selection & (1 + (1 << 5)) != 0 {
491 log::warn!(
492 "fsSelection bit 6 (regular) is set, bits 0 (italic) and 5 (bold) must be clear"
493 );
494 }
495 if self.version < 4 && self.fs_selection & 0b11_1000_0000 != 0 {
496 log::warn!(
497 "fsSelection bits 7, 8 and 9 are only defined in OS/2 table version 4 and up: version {}",
498 self.version
499 );
500 }
501 let mut base = self.base.clone();
502 let mut panose = Vec::with_capacity(10);
503 self.panose.pack(&mut panose)?;
504 base.panose.copy_from_slice(&panose);
505 let mut data = Vec::new();
506 match self.version {
507 0..=5 => base.pack(&mut data)?,
508 v => {
509 return Err(Error::ttlib(format!(
510 "unknown format for OS/2 table: version {v}"
511 )));
512 }
513 }
514 if self.version >= 1 {
515 data.put_u32(self.ul_code_page_range1);
516 data.put_u32(self.ul_code_page_range2);
517 }
518 if self.version >= 2 {
519 data.put_i16(self.sx_height);
520 data.put_i16(self.s_cap_height);
521 data.put_u16(self.us_default_char);
522 data.put_u16(self.us_break_char);
523 data.put_u16(self.us_max_context);
524 }
525 if self.version == 5 {
526 let twips = |v: f64, name: &str| -> Result<u16> {
527 fit(fontoxide_misc::py::round(v * 20.0) as i64, name)
528 };
529 data.put_u16(twips(
530 self.us_lower_optical_point_size,
531 "usLowerOpticalPointSize",
532 )?);
533 data.put_u16(twips(
534 self.us_upper_optical_point_size,
535 "usUpperOpticalPointSize",
536 )?);
537 }
538 Ok(data)
539 }
540
541 fn clone_table(&self) -> Box<dyn Table> {
542 Box::new(self.clone())
543 }
544}
545
546impl TableXml for Os2 {
547 fn to_xml(&self, writer: &mut XmlWriter, _font: &mut Font) -> Result<()> {
548 writer.comment(
549 "The fields 'usFirstCharIndex' and 'usLastCharIndex'\nwill be recalculated by the compiler",
550 );
551 writer.newline();
552 for name in self.field_names() {
553 match self.field(name) {
554 Field::Panose => {
555 writer.begintag("panose", &[]);
556 writer.newline();
557 for (n, v) in self.panose.values() {
558 writer.simpletag(n, &[("value", &v)]);
559 writer.newline();
560 }
561 writer.endtag("panose");
562 }
563 Field::VendId => {
564 let id = &self.ach_vend_id;
566 let r = if id.is_ascii() {
567 fontoxide_misc::py::str_repr(&fontoxide_misc::py::latin1_decode(id))
568 } else {
569 fontoxide_misc::py::bytes_repr(id)
570 };
571 let v = &r[1..r.len() - 1];
572 writer.simpletag(name, &[("value", &v)]);
573 }
574 Field::Int(i) if RANGE_FIELDS.contains(&name) => {
575 writer.simpletag(name, &[("value", &num2binary(i, 32))]);
576 }
577 Field::Int(i) if name == "fsType" || name == "fsSelection" => {
578 writer.simpletag(name, &[("value", &num2binary(i, 16))]);
579 }
580 Field::Int(i) => writer.simpletag(name, &[("value", &i)]),
581 Field::Float(f) => writer.simpletag(name, &[("value", &f)]),
582 }
583 writer.newline();
584 }
585 Ok(())
586 }
587
588 fn from_xml(&mut self, element: &Element, _font: &mut Font) -> Result<()> {
589 let name = element.name.as_str();
590 if name == "panose" {
591 let mut panose = Panose::default();
592 for e in element.elements() {
593 let value = e
594 .attr("value")
595 .ok_or_else(|| Error::ttx(format!("missing 'value' on <{}>", e.name)))?;
596 panose.set_literal(&e.name, &eval(value)?)?;
597 }
598 self.panose = panose;
599 return Ok(());
600 }
601 let value = element
602 .attr("value")
603 .ok_or_else(|| Error::ttx(format!("missing 'value' on <{name}>")))?;
604 if RANGE_FIELDS.contains(&name) || name == "fsType" || name == "fsSelection" {
605 let v = binary2num(value);
606 match name {
607 "ulCodePageRange1" => self.ul_code_page_range1 = fit(v, name)?,
608 "ulCodePageRange2" => self.ul_code_page_range2 = fit(v, name)?,
609 _ => {
610 self.base.set_literal(name, &Literal::Int(v as i128))?;
611 }
612 }
613 return Ok(());
614 }
615 if name == "achVendID" {
616 let s = fontoxide_misc::text_tools::parse_string_body(value)
618 .ok_or_else(|| Error::value(format!("invalid achVendID: {value:?}")))?;
619 if !s.is_ascii() {
620 return Err(Error::value("'ascii' codec can't encode achVendID"));
621 }
622 let mut id = [0u8; 4];
623 for (d, b) in id.iter_mut().zip(s.bytes()) {
624 *d = b;
625 }
626 self.base.ach_vend_id = id;
627 return Ok(());
628 }
629 let lit = eval(value)?;
630 let int = |lit: &Literal| {
631 lit.as_int()
632 .ok_or_else(|| Error::value(format!("invalid {name}")))
633 };
634 match name {
635 "sxHeight" => self.sx_height = fit(int(&lit)?, name)?,
636 "sCapHeight" => self.s_cap_height = fit(int(&lit)?, name)?,
637 "usDefaultChar" => self.us_default_char = fit(int(&lit)?, name)?,
638 "usBreakChar" => self.us_break_char = fit(int(&lit)?, name)?,
639 "usMaxContext" | "usMaxContex" => self.us_max_context = fit(int(&lit)?, name)?,
640 "usLowerOpticalPointSize" => {
641 self.us_lower_optical_point_size = lit
642 .as_float()
643 .ok_or_else(|| Error::value(format!("invalid {name}")))?;
644 }
645 "usUpperOpticalPointSize" => {
646 self.us_upper_optical_point_size = lit
647 .as_float()
648 .ok_or_else(|| Error::value(format!("invalid {name}")))?;
649 }
650 _ => {
651 let name = match name {
652 "fsFirstCharIndex" => "usFirstCharIndex",
653 "fsLastCharIndex" => "usLastCharIndex",
654 n => n,
655 };
656 if !self.base.set_literal(name, &lit)? {
657 return Err(Error::ttx(format!("unknown 'OS/2' field: {name}")));
658 }
659 }
660 }
661 Ok(())
662 }
663}
664
665#[cfg(test)]
666mod tests {
667 use super::*;
668
669 #[test]
670 fn formats_match_upstream() {
671 use crate::sstruct::tests::check;
672 check(Panose::FORMAT_NAME, Panose::FIELDS);
673 check(Os2Format0::FORMAT_NAME, Os2Format0::FIELDS);
674 check(Os2Format1Addition::FORMAT_NAME, Os2Format1Addition::FIELDS);
675 check(Os2Format2Addition::FORMAT_NAME, Os2Format2Addition::FIELDS);
676 check(Os2Format5Addition::FORMAT_NAME, Os2Format5Addition::FIELDS);
677 }
678}
679
680pub fn intersect_unicode_ranges(unicodes: &BTreeSet<u32>, inverse: bool) -> BTreeSet<u32> {
683 use super::os2_ranges::OS2_UNICODE_RANGES;
684 let mut ranges: Vec<(u32, u32, u32)> = OS2_UNICODE_RANGES
686 .iter()
687 .enumerate()
688 .flat_map(|(bit, blocks)| blocks.iter().map(move |&(_, a, b)| (a, b, bit as u32)))
689 .collect();
690 ranges.sort_unstable();
691 let mut bits = BTreeSet::new();
692 for &code in unicodes {
693 let i = ranges.partition_point(|r| r.0 <= code);
695 if i > 0 && code <= ranges[i - 1].1 {
696 bits.insert(ranges[i - 1].2);
697 }
698 }
699 if unicodes.iter().any(|&c| (0x10000..0x110000).contains(&c)) {
701 bits.insert(57);
702 }
703 if inverse {
704 (0..OS2_UNICODE_RANGES.len() as u32)
705 .filter(|b| !bits.contains(b))
706 .collect()
707 } else {
708 bits
709 }
710}
711
712pub fn calc_code_page_ranges(unicodes: &BTreeSet<u32>) -> BTreeSet<u32> {
715 let has = |c: char| unicodes.contains(&(c as u32));
716 let mut bits = BTreeSet::new();
717 let has_ascii = (0x20..0x7E).all(|c| unicodes.contains(&c));
718 let has_lineart = has('┤');
719 for &uni in unicodes {
720 let Some(c) = char::from_u32(uni) else {
721 continue;
722 };
723 match c {
724 'Þ' if has_ascii => {
725 bits.insert(0);
726 }
727 'Ľ' if has_ascii => {
728 bits.insert(1);
729 if has_lineart {
730 bits.insert(58);
731 }
732 }
733 'Б' => {
734 bits.insert(2);
735 if has('Ѕ') && has_lineart {
736 bits.insert(57);
737 }
738 if has('╜') && has_lineart {
739 bits.insert(49);
740 }
741 }
742 'Ά' => {
743 bits.insert(3);
744 if has_lineart && has('½') {
745 bits.insert(48);
746 }
747 if has_lineart && has('√') {
748 bits.insert(60);
749 }
750 }
751 'İ' if has_ascii => {
752 bits.insert(4);
753 if has_lineart {
754 bits.insert(56);
755 }
756 }
757 'א' => {
758 bits.insert(5);
759 if has_lineart && has('√') {
760 bits.insert(53);
761 }
762 }
763 'ر' => {
764 bits.insert(6);
765 if has('√') {
766 bits.insert(51);
767 }
768 if has_lineart {
769 bits.insert(61);
770 }
771 }
772 'ŗ' if has_ascii => {
773 bits.insert(7);
774 if has_lineart {
775 bits.insert(59);
776 }
777 }
778 '₫' if has_ascii => {
779 bits.insert(8);
780 }
781 'ๅ' => {
782 bits.insert(16);
783 }
784 'エ' => {
785 bits.insert(17);
786 }
787 'ㄅ' => {
788 bits.insert(18);
789 }
790 'ㄱ' => {
791 bits.insert(19);
792 }
793 '央' => {
794 bits.insert(20);
795 }
796 '곴' => {
797 bits.insert(21);
798 }
799 '♥' if has_ascii => {
800 bits.insert(30);
801 }
802 'þ' if has_ascii && has_lineart => {
803 bits.insert(54);
804 }
805 '╚' if has_ascii => {
806 bits.insert(62);
807 bits.insert(63);
808 }
809 _ if has_ascii && has_lineart && has('√') => match c {
810 'Å' => {
811 bits.insert(50);
812 }
813 'é' => {
814 bits.insert(52);
815 }
816 'õ' => {
817 bits.insert(55);
818 }
819 _ => {}
820 },
821 _ => {}
822 }
823 }
824 if has_ascii && has('‰') && has('∑') {
825 bits.insert(29);
826 }
827 bits
828}
829
830#[cfg(test)]
831mod range_tests {
832 use super::*;
833
834 #[test]
835 fn doctests() {
836 assert_eq!(
837 intersect_unicode_ranges(&[0x0410].into(), false),
838 [9].into()
839 );
840 assert_eq!(
841 intersect_unicode_ranges(&[0x0410, 0x1F000].into(), false),
842 [9, 57, 122].into()
843 );
844 let inverse = intersect_unicode_ranges(&[0x0410, 0x1F000].into(), true);
845 assert_eq!(inverse.len(), 123 - 3);
846 }
847}