(expected).unwrap()
323 | );
324 | }
325 |
326 | #[test]
327 | fn test_diff_json_3() {
328 | let mut stylesheet = css!("tests/fixtures/test_1.css");
329 |
330 | let mut tree_1 = rsx! {
331 |
332 | Hello world!
333 |
334 | };
335 |
336 | let mut tree_2 = rsx! {
337 |
338 | Goodbye cruel world!
339 |
340 | };
341 |
342 | let mut files = FileCache::new().unwrap();
343 |
344 | let image_path = "tests/fixtures/Quantum.png";
345 | assert!(files.add_file(image_path).is_ok());
346 |
347 | let image_keys = ImageKeysAPI::new(());
348 | let mut images = ImageCache::new(image_keys).unwrap();
349 |
350 | let image_id = ImageId::new(image_path);
351 | let image_bytes = files.get_file(image_path).unwrap();
352 | images.add_raw(image_id, image_bytes).unwrap();
353 |
354 | let font_path = "tests/fixtures/FreeSans.ttf";
355 | assert!(files.add_file(font_path).is_ok());
356 |
357 | let font_keys = FontKeysAPI::new(());
358 | let mut fonts = FontCache::new(font_keys).unwrap();
359 |
360 | let font_id = FontId::new("FreeSans");
361 | let font_bytes = files.get_file(font_path).unwrap();
362 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
363 |
364 | let resources = ResourceGroup::new(files, images, fonts);
365 | tree_1.generate_layout_tree(&resources);
366 | tree_2.generate_layout_tree(&resources);
367 |
368 | let width = 1024.0;
369 | let height = 768.0;
370 | tree_1.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
371 | tree_2.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
372 |
373 | let display_list_1 = DisplayList::from(&mut tree_1);
374 | let display_list_2 = DisplayList::from(&mut tree_2);
375 | let diff = display_list_1.diff(&display_list_2);
376 | let stringified: String = diff.into();
377 |
378 | let expected = r#"
379 | [
380 | {
381 | "M": [
382 | 0,
383 | [
384 | {
385 | "Z": {
386 | "W": 122
387 | }
388 | }
389 | ]
390 | ]
391 | },
392 | {
393 | "M": [
394 | 1,
395 | [
396 | {
397 | "Z": {
398 | "W": 122
399 | }
400 | }
401 | ]
402 | ]
403 | },
404 | {
405 | "M": [
406 | 2,
407 | [
408 | {
409 | "Z": {
410 | "W": 122
411 | }
412 | },
413 | {
414 | "T": {
415 | "T": "Goodbye\ncruel world !"
416 | }
417 | }
418 | ]
419 | ]
420 | }
421 | ]
422 | "#;
423 |
424 | assert_eq!(
425 | serde_json::from_str::(&stringified).unwrap(),
426 | serde_json::from_str::(expected).unwrap()
427 | );
428 | }
429 |
430 | #[test]
431 | fn test_diff_json_4() {
432 | let mut stylesheet = css!("tests/fixtures/test_2.css");
433 |
434 | let mut tree_1 = rsx! {
435 |
436 | Hello world!
437 |
438 | };
439 |
440 | let mut tree_2 = rsx! {
441 |
442 | Hello world!
443 |
444 | };
445 |
446 | let mut files = FileCache::new().unwrap();
447 |
448 | let image_path = "tests/fixtures/Quantum.png";
449 | assert!(files.add_file(image_path).is_ok());
450 |
451 | let image_keys = ImageKeysAPI::new(());
452 | let mut images = ImageCache::new(image_keys).unwrap();
453 |
454 | let image_id = ImageId::new(image_path);
455 | let image_bytes = files.get_file(image_path).unwrap();
456 | images.add_raw(image_id, image_bytes).unwrap();
457 |
458 | let font_path = "tests/fixtures/FreeSans.ttf";
459 | assert!(files.add_file(font_path).is_ok());
460 |
461 | let font_keys = FontKeysAPI::new(());
462 | let mut fonts = FontCache::new(font_keys).unwrap();
463 |
464 | let font_id = FontId::new("FreeSans");
465 | let font_bytes = files.get_file(font_path).unwrap();
466 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
467 |
468 | let resources = ResourceGroup::new(files, images, fonts);
469 | tree_1.generate_layout_tree(&resources);
470 | tree_2.generate_layout_tree(&resources);
471 |
472 | let width = 1024.0;
473 | let height = 768.0;
474 | tree_1.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
475 | tree_2.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
476 |
477 | let display_list_1 = DisplayList::from(&mut tree_1);
478 | let display_list_2 = DisplayList::from(&mut tree_2);
479 | let diff = display_list_1.diff(&display_list_2);
480 | let stringified: String = diff.into();
481 |
482 | let expected = r#"
483 | [
484 | {
485 | "M": [
486 | 2,
487 | [
488 | {
489 | "T": {
490 | "C": [
491 | 255,
492 | 0,
493 | 255,
494 | 255
495 | ]
496 | }
497 | }
498 | ]
499 | ]
500 | }
501 | ]
502 | "#;
503 |
504 | assert_eq!(
505 | serde_json::from_str::(&stringified).unwrap(),
506 | serde_json::from_str::(expected).unwrap()
507 | );
508 | }
509 |
510 | #[test]
511 | fn test_diff_json_5() {
512 | let mut stylesheet = css!("tests/fixtures/test_2.css");
513 |
514 | let mut tree_1 = rsx! {
515 |
516 | Hello world!
517 |
518 | };
519 |
520 | let mut tree_2 = rsx! {
521 |
522 |
523 |
524 | Hello world!
525 |
526 |
527 | };
528 |
529 | let mut files = FileCache::new().unwrap();
530 |
531 | let image_path = "tests/fixtures/Quantum.png";
532 | assert!(files.add_file(image_path).is_ok());
533 |
534 | let image_keys = ImageKeysAPI::new(());
535 | let mut images = ImageCache::new(image_keys).unwrap();
536 |
537 | let image_id = ImageId::new(image_path);
538 | let image_bytes = files.get_file(image_path).unwrap();
539 | images.add_raw(image_id, image_bytes).unwrap();
540 |
541 | let font_path = "tests/fixtures/FreeSans.ttf";
542 | assert!(files.add_file(font_path).is_ok());
543 |
544 | let font_keys = FontKeysAPI::new(());
545 | let mut fonts = FontCache::new(font_keys).unwrap();
546 |
547 | let font_id = FontId::new("FreeSans");
548 | let font_bytes = files.get_file(font_path).unwrap();
549 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
550 |
551 | let resources = ResourceGroup::new(files, images, fonts);
552 | tree_1.generate_layout_tree(&resources);
553 | tree_2.generate_layout_tree(&resources);
554 |
555 | let width = 1024.0;
556 | let height = 768.0;
557 | tree_1.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
558 | tree_2.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
559 |
560 | let display_list_1 = DisplayList::from(&mut tree_1);
561 | let display_list_2 = DisplayList::from(&mut tree_2);
562 | let diff = display_list_1.diff(&display_list_2);
563 | let stringified: String = diff.into();
564 |
565 | let expected = r#"
566 | [
567 | {
568 | "M": [
569 | 0,
570 | [
571 | {
572 | "Z": {
573 | "W": 500
574 | }
575 | },
576 | {
577 | "Z": {
578 | "H": 120
579 | }
580 | },
581 | {
582 | "R": {
583 | "C": [
584 | 255,
585 | 0,
586 | 0,
587 | 255
588 | ]
589 | }
590 | }
591 | ]
592 | ]
593 | },
594 | {
595 | "M": [
596 | 1,
597 | [
598 | {
599 | "Z": {
600 | "W": 500
601 | }
602 | },
603 | {
604 | "Z": {
605 | "H": 120
606 | }
607 | }
608 | ]
609 | ]
610 | },
611 | {
612 | "IntoRect": [
613 | 2,
614 | {
615 | "bounds": {
616 | "position": {
617 | "left": 20,
618 | "top": 20
619 | },
620 | "size": {
621 | "width": 80,
622 | "height": 83
623 | }
624 | },
625 | "display": {
626 | "color": {
627 | "red": 0,
628 | "green": 128,
629 | "blue": 0,
630 | "alpha": 255
631 | }
632 | }
633 | }
634 | ]
635 | },
636 | {
637 | "AddBorder": {
638 | "bounds": {
639 | "position": {
640 | "left": 20,
641 | "top": 20
642 | },
643 | "size": {
644 | "width": 80,
645 | "height": 83
646 | }
647 | },
648 | "display": {
649 | "widths": [
650 | 0,
651 | 0,
652 | 0,
653 | 0
654 | ],
655 | "colors": [
656 | {
657 | "red": 0,
658 | "green": 0,
659 | "blue": 0,
660 | "alpha": 0
661 | },
662 | {
663 | "red": 0,
664 | "green": 0,
665 | "blue": 0,
666 | "alpha": 0
667 | },
668 | {
669 | "red": 0,
670 | "green": 0,
671 | "blue": 0,
672 | "alpha": 0
673 | },
674 | {
675 | "red": 0,
676 | "green": 0,
677 | "blue": 0,
678 | "alpha": 0
679 | }
680 | ],
681 | "styles": [
682 | "Solid",
683 | "Solid",
684 | "Solid",
685 | "Solid"
686 | ]
687 | }
688 | }
689 | },
690 | {
691 | "AddImage": {
692 | "bounds": {
693 | "position": {
694 | "left": 20,
695 | "top": 20
696 | },
697 | "size": {
698 | "width": 80,
699 | "height": 83
700 | }
701 | },
702 | "display": {
703 | "image_src": {
704 | "Static": "tests/fixtures/Quantum.png"
705 | },
706 | "measured_image": {
707 | "image_key": 0,
708 | "size": [
709 | 512,
710 | 529
711 | ]
712 | }
713 | }
714 | }
715 | },
716 | {
717 | "AddRect": {
718 | "bounds": {
719 | "position": {
720 | "left": 120,
721 | "top": 48
722 | },
723 | "size": {
724 | "width": 360,
725 | "height": 25
726 | }
727 | },
728 | "display": {
729 | "color": {
730 | "red": 0,
731 | "green": 0,
732 | "blue": 255,
733 | "alpha": 255
734 | }
735 | }
736 | }
737 | },
738 | {
739 | "AddBorder": {
740 | "bounds": {
741 | "position": {
742 | "left": 120,
743 | "top": 48
744 | },
745 | "size": {
746 | "width": 360,
747 | "height": 25
748 | }
749 | },
750 | "display": {
751 | "widths": [
752 | 0,
753 | 0,
754 | 0,
755 | 0
756 | ],
757 | "colors": [
758 | {
759 | "red": 0,
760 | "green": 0,
761 | "blue": 0,
762 | "alpha": 0
763 | },
764 | {
765 | "red": 0,
766 | "green": 0,
767 | "blue": 0,
768 | "alpha": 0
769 | },
770 | {
771 | "red": 0,
772 | "green": 0,
773 | "blue": 0,
774 | "alpha": 0
775 | },
776 | {
777 | "red": 0,
778 | "green": 0,
779 | "blue": 0,
780 | "alpha": 0
781 | }
782 | ],
783 | "styles": [
784 | "Solid",
785 | "Solid",
786 | "Solid",
787 | "Solid"
788 | ]
789 | }
790 | }
791 | },
792 | {
793 | "AddText": {
794 | "bounds": {
795 | "position": {
796 | "left": 120,
797 | "top": 48
798 | },
799 | "size": {
800 | "width": 66,
801 | "height": 25
802 | }
803 | },
804 | "display": {
805 | "color": {
806 | "red": 255,
807 | "green": 255,
808 | "blue": 0,
809 | "alpha": 255
810 | },
811 | "source_text": [
812 | {
813 | "Static": "Hello world !"
814 | }
815 | ],
816 | "shaped_text": [
817 | {
818 | "font_key": 0,
819 | "font_instance_key": 1,
820 | "width_64": 4224,
821 | "height_64": 1088,
822 | "glyphs": [
823 | {
824 | "glyph_index": 43,
825 | "x_64": 0,
826 | "y_64": 768
827 | },
828 | {
829 | "glyph_index": 72,
830 | "x_64": 554,
831 | "y_64": 768
832 | },
833 | {
834 | "glyph_index": 79,
835 | "x_64": 981,
836 | "y_64": 768
837 | },
838 | {
839 | "glyph_index": 79,
840 | "x_64": 1151,
841 | "y_64": 768
842 | },
843 | {
844 | "glyph_index": 82,
845 | "x_64": 1321,
846 | "y_64": 768
847 | },
848 | {
849 | "glyph_index": 3,
850 | "x_64": 1748,
851 | "y_64": 768
852 | },
853 | {
854 | "glyph_index": 90,
855 | "x_64": 1962,
856 | "y_64": 768
857 | },
858 | {
859 | "glyph_index": 82,
860 | "x_64": 2516,
861 | "y_64": 768
862 | },
863 | {
864 | "glyph_index": 85,
865 | "x_64": 2943,
866 | "y_64": 768
867 | },
868 | {
869 | "glyph_index": 79,
870 | "x_64": 3199,
871 | "y_64": 768
872 | },
873 | {
874 | "glyph_index": 71,
875 | "x_64": 3369,
876 | "y_64": 768
877 | },
878 | {
879 | "glyph_index": 3,
880 | "x_64": 3796,
881 | "y_64": 768
882 | },
883 | {
884 | "glyph_index": 4,
885 | "x_64": 4010,
886 | "y_64": 768
887 | }
888 | ],
889 | "generation_id": 14732137242825294601
890 | }
891 | ]
892 | }
893 | }
894 | }
895 | ]
896 | "#;
897 |
898 | assert_eq!(
899 | serde_json::from_str::(&stringified).unwrap(),
900 | serde_json::from_str::(expected).unwrap()
901 | );
902 | }
903 |
--------------------------------------------------------------------------------
/tests/spec_json.rs:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2016 Mozilla
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 | this file except in compliance with the License. You may obtain a copy of the
5 | License at http://www.apache.org/licenses/LICENSE-2.0
6 | Unless required by applicable law or agreed to in writing, software distributed
7 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 | CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 | specific language governing permissions and limitations under the License.
10 | */
11 |
12 | #![cfg(feature = "json-display-list")]
13 | #![feature(proc_macro)]
14 |
15 | extern crate rsx;
16 | #[macro_use]
17 | extern crate rsx_primitives;
18 | extern crate serde_json;
19 |
20 | use rsx::{css, rsx};
21 | use rsx_primitives::build::types::*;
22 | use rsx_primitives::rsx_dom::types::*;
23 | use rsx_primitives::rsx_layout::types::*;
24 | use rsx_primitives::rsx_resources::files::types::*;
25 | use rsx_primitives::rsx_resources::fonts::types::*;
26 | use rsx_primitives::rsx_resources::images::types::*;
27 | use rsx_primitives::rsx_resources::types::ResourceGroup;
28 | use rsx_primitives::rsx_shared::traits::*;
29 | use rsx_primitives::rsx_stylesheet::types::*;
30 | use rsx_primitives::types::DisplayList;
31 |
32 | #[test]
33 | fn test_json_reflow_simple_resourceless() {
34 | let mut stylesheet = css!("tests/fixtures/test_1.css");
35 |
36 | let mut tree = rsx! {
37 |
38 | Hello world!
39 |
40 | };
41 |
42 | let files = FileCache::new().unwrap();
43 |
44 | let image_keys = ImageKeysAPI::new(());
45 | let images = ImageCache::new(image_keys).unwrap();
46 |
47 | let font_keys = FontKeysAPI::new(());
48 | let fonts = FontCache::new(font_keys).unwrap();
49 |
50 | let resources = ResourceGroup::new(files, images, fonts);
51 | tree.generate_layout_tree(&resources);
52 |
53 | let width = 100.0;
54 | let height = 100.0;
55 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
56 |
57 | let display_list = DisplayList::from(&mut tree).serialize();
58 | let stringified: String = display_list.into();
59 |
60 | let expected = r#"
61 | [
62 | {
63 | "Rect": {
64 | "bounds": {
65 | "position": {
66 | "left": 0,
67 | "top": 0
68 | },
69 | "size": {
70 | "width": 0,
71 | "height": 100
72 | }
73 | },
74 | "display": {
75 | "color": {
76 | "red": 0,
77 | "green": 0,
78 | "blue": 0,
79 | "alpha": 0
80 | }
81 | }
82 | }
83 | },
84 | {
85 | "Border": {
86 | "bounds": {
87 | "position": {
88 | "left": 0,
89 | "top": 0
90 | },
91 | "size": {
92 | "width": 0,
93 | "height": 100
94 | }
95 | },
96 | "display": {
97 | "widths": [
98 | 0,
99 | 0,
100 | 0,
101 | 0
102 | ],
103 | "colors": [
104 | {
105 | "red": 0,
106 | "green": 0,
107 | "blue": 0,
108 | "alpha": 0
109 | },
110 | {
111 | "red": 0,
112 | "green": 0,
113 | "blue": 0,
114 | "alpha": 0
115 | },
116 | {
117 | "red": 0,
118 | "green": 0,
119 | "blue": 0,
120 | "alpha": 0
121 | },
122 | {
123 | "red": 0,
124 | "green": 0,
125 | "blue": 0,
126 | "alpha": 0
127 | }
128 | ],
129 | "styles": [
130 | "Solid",
131 | "Solid",
132 | "Solid",
133 | "Solid"
134 | ]
135 | }
136 | }
137 | }
138 | ]
139 | "#;
140 |
141 | assert_eq!(
142 | serde_json::from_str::(&stringified).unwrap(),
143 | serde_json::from_str::(expected).unwrap()
144 | );
145 | }
146 |
147 | #[test]
148 | fn test_json_reflow_simple() {
149 | let mut stylesheet = css!("tests/fixtures/test_1.css");
150 |
151 | let mut tree = rsx! {
152 |
153 | Hello world!
154 |
155 | };
156 |
157 | let mut files = FileCache::new().unwrap();
158 |
159 | let image_path = "tests/fixtures/Quantum.png";
160 | assert!(files.add_file(image_path).is_ok());
161 |
162 | let image_keys = ImageKeysAPI::new(());
163 | let mut images = ImageCache::new(image_keys).unwrap();
164 |
165 | let image_id = ImageId::new(image_path);
166 | let image_bytes = files.get_file(image_path).unwrap();
167 | images.add_raw(image_id, image_bytes).unwrap();
168 |
169 | let font_path = "tests/fixtures/FreeSans.ttf";
170 | assert!(files.add_file(font_path).is_ok());
171 |
172 | let font_keys = FontKeysAPI::new(());
173 | let mut fonts = FontCache::new(font_keys).unwrap();
174 |
175 | let font_id = FontId::new("FreeSans");
176 | let font_bytes = files.get_file(font_path).unwrap();
177 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
178 |
179 | let resources = ResourceGroup::new(files, images, fonts);
180 | tree.generate_layout_tree(&resources);
181 |
182 | let width = 100.0;
183 | let height = 100.0;
184 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
185 |
186 | let display_list = DisplayList::from(&mut tree).serialize();
187 | let stringified: String = display_list.into();
188 |
189 | let expected = r#"
190 | [
191 | {
192 | "Rect": {
193 | "bounds": {
194 | "position": {
195 | "left": 0,
196 | "top": 0
197 | },
198 | "size": {
199 | "width": 66,
200 | "height": 100
201 | }
202 | },
203 | "display": {
204 | "color": {
205 | "red": 0,
206 | "green": 0,
207 | "blue": 0,
208 | "alpha": 0
209 | }
210 | }
211 | }
212 | },
213 | {
214 | "Border": {
215 | "bounds": {
216 | "position": {
217 | "left": 0,
218 | "top": 0
219 | },
220 | "size": {
221 | "width": 66,
222 | "height": 100
223 | }
224 | },
225 | "display": {
226 | "widths": [
227 | 0,
228 | 0,
229 | 0,
230 | 0
231 | ],
232 | "colors": [
233 | {
234 | "red": 0,
235 | "green": 0,
236 | "blue": 0,
237 | "alpha": 0
238 | },
239 | {
240 | "red": 0,
241 | "green": 0,
242 | "blue": 0,
243 | "alpha": 0
244 | },
245 | {
246 | "red": 0,
247 | "green": 0,
248 | "blue": 0,
249 | "alpha": 0
250 | },
251 | {
252 | "red": 0,
253 | "green": 0,
254 | "blue": 0,
255 | "alpha": 0
256 | }
257 | ],
258 | "styles": [
259 | "Solid",
260 | "Solid",
261 | "Solid",
262 | "Solid"
263 | ]
264 | }
265 | }
266 | },
267 | {
268 | "Text": {
269 | "bounds": {
270 | "position": {
271 | "left": 0,
272 | "top": 0
273 | },
274 | "size": {
275 | "width": 66,
276 | "height": 100
277 | }
278 | },
279 | "display": {
280 | "color": {
281 | "red": 0,
282 | "green": 0,
283 | "blue": 0,
284 | "alpha": 255
285 | },
286 | "source_text": [
287 | {
288 | "Static": "Hello world !"
289 | }
290 | ],
291 | "shaped_text": [
292 | {
293 | "font_key": 0,
294 | "font_instance_key": 1,
295 | "width_64": 4224,
296 | "height_64": 1088,
297 | "glyphs": [
298 | {
299 | "glyph_index": 43,
300 | "x_64": 0,
301 | "y_64": 768
302 | },
303 | {
304 | "glyph_index": 72,
305 | "x_64": 554,
306 | "y_64": 768
307 | },
308 | {
309 | "glyph_index": 79,
310 | "x_64": 981,
311 | "y_64": 768
312 | },
313 | {
314 | "glyph_index": 79,
315 | "x_64": 1151,
316 | "y_64": 768
317 | },
318 | {
319 | "glyph_index": 82,
320 | "x_64": 1321,
321 | "y_64": 768
322 | },
323 | {
324 | "glyph_index": 3,
325 | "x_64": 1748,
326 | "y_64": 768
327 | },
328 | {
329 | "glyph_index": 90,
330 | "x_64": 1962,
331 | "y_64": 768
332 | },
333 | {
334 | "glyph_index": 82,
335 | "x_64": 2516,
336 | "y_64": 768
337 | },
338 | {
339 | "glyph_index": 85,
340 | "x_64": 2943,
341 | "y_64": 768
342 | },
343 | {
344 | "glyph_index": 79,
345 | "x_64": 3199,
346 | "y_64": 768
347 | },
348 | {
349 | "glyph_index": 71,
350 | "x_64": 3369,
351 | "y_64": 768
352 | },
353 | {
354 | "glyph_index": 3,
355 | "x_64": 3796,
356 | "y_64": 768
357 | },
358 | {
359 | "glyph_index": 4,
360 | "x_64": 4010,
361 | "y_64": 768
362 | }
363 | ],
364 | "generation_id": 14732137242825294601
365 | }
366 | ]
367 | }
368 | }
369 | }
370 | ]
371 | "#;
372 |
373 | assert_eq!(
374 | serde_json::from_str::(&stringified).unwrap(),
375 | serde_json::from_str::(expected).unwrap()
376 | );
377 | }
378 |
379 | #[test]
380 | fn test_json_reflow_example() {
381 | let mut stylesheet = css!("tests/fixtures/test_1.css");
382 |
383 | let mut tree = rsx! {
384 |
385 |
386 |
387 | Hello world!
388 |
389 |
390 | };
391 |
392 | let mut files = FileCache::new().unwrap();
393 |
394 | let image_path = "tests/fixtures/Quantum.png";
395 | assert!(files.add_file(image_path).is_ok());
396 |
397 | let image_keys = ImageKeysAPI::new(());
398 | let mut images = ImageCache::new(image_keys).unwrap();
399 |
400 | let image_id = ImageId::new(image_path);
401 | let image_bytes = files.get_file(image_path).unwrap();
402 | images.add_raw(image_id, image_bytes).unwrap();
403 |
404 | let font_path = "tests/fixtures/FreeSans.ttf";
405 | assert!(files.add_file(font_path).is_ok());
406 |
407 | let font_keys = FontKeysAPI::new(());
408 | let mut fonts = FontCache::new(font_keys).unwrap();
409 |
410 | let font_id = FontId::new("FreeSans");
411 | let font_bytes = files.get_file(font_path).unwrap();
412 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
413 |
414 | let resources = ResourceGroup::new(files, images, fonts);
415 | tree.generate_layout_tree(&resources);
416 |
417 | let width = 1024.0;
418 | let height = 768.0;
419 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
420 |
421 | let display_list = DisplayList::from(&mut tree).serialize();
422 | let stringified: String = display_list.into();
423 |
424 | let expected = r#"
425 | [
426 | {
427 | "Rect": {
428 | "bounds": {
429 | "position": {
430 | "left": 0,
431 | "top": 0
432 | },
433 | "size": {
434 | "width": 500,
435 | "height": 120
436 | }
437 | },
438 | "display": {
439 | "color": {
440 | "red": 0,
441 | "green": 0,
442 | "blue": 0,
443 | "alpha": 0
444 | }
445 | }
446 | }
447 | },
448 | {
449 | "Border": {
450 | "bounds": {
451 | "position": {
452 | "left": 0,
453 | "top": 0
454 | },
455 | "size": {
456 | "width": 500,
457 | "height": 120
458 | }
459 | },
460 | "display": {
461 | "widths": [
462 | 0,
463 | 0,
464 | 0,
465 | 0
466 | ],
467 | "colors": [
468 | {
469 | "red": 0,
470 | "green": 0,
471 | "blue": 0,
472 | "alpha": 0
473 | },
474 | {
475 | "red": 0,
476 | "green": 0,
477 | "blue": 0,
478 | "alpha": 0
479 | },
480 | {
481 | "red": 0,
482 | "green": 0,
483 | "blue": 0,
484 | "alpha": 0
485 | },
486 | {
487 | "red": 0,
488 | "green": 0,
489 | "blue": 0,
490 | "alpha": 0
491 | }
492 | ],
493 | "styles": [
494 | "Solid",
495 | "Solid",
496 | "Solid",
497 | "Solid"
498 | ]
499 | }
500 | }
501 | },
502 | {
503 | "Rect": {
504 | "bounds": {
505 | "position": {
506 | "left": 20,
507 | "top": 20
508 | },
509 | "size": {
510 | "width": 80,
511 | "height": 83
512 | }
513 | },
514 | "display": {
515 | "color": {
516 | "red": 0,
517 | "green": 0,
518 | "blue": 0,
519 | "alpha": 0
520 | }
521 | }
522 | }
523 | },
524 | {
525 | "Border": {
526 | "bounds": {
527 | "position": {
528 | "left": 20,
529 | "top": 20
530 | },
531 | "size": {
532 | "width": 80,
533 | "height": 83
534 | }
535 | },
536 | "display": {
537 | "widths": [
538 | 0,
539 | 0,
540 | 0,
541 | 0
542 | ],
543 | "colors": [
544 | {
545 | "red": 0,
546 | "green": 0,
547 | "blue": 0,
548 | "alpha": 0
549 | },
550 | {
551 | "red": 0,
552 | "green": 0,
553 | "blue": 0,
554 | "alpha": 0
555 | },
556 | {
557 | "red": 0,
558 | "green": 0,
559 | "blue": 0,
560 | "alpha": 0
561 | },
562 | {
563 | "red": 0,
564 | "green": 0,
565 | "blue": 0,
566 | "alpha": 0
567 | }
568 | ],
569 | "styles": [
570 | "Solid",
571 | "Solid",
572 | "Solid",
573 | "Solid"
574 | ]
575 | }
576 | }
577 | },
578 | {
579 | "Image": {
580 | "bounds": {
581 | "position": {
582 | "left": 20,
583 | "top": 20
584 | },
585 | "size": {
586 | "width": 80,
587 | "height": 83
588 | }
589 | },
590 | "display": {
591 | "image_src": {
592 | "Static": "tests/fixtures/Quantum.png"
593 | },
594 | "measured_image": {
595 | "image_key": 0,
596 | "size": [
597 | 512,
598 | 529
599 | ]
600 | }
601 | }
602 | }
603 | },
604 | {
605 | "Rect": {
606 | "bounds": {
607 | "position": {
608 | "left": 120,
609 | "top": 48
610 | },
611 | "size": {
612 | "width": 360,
613 | "height": 25
614 | }
615 | },
616 | "display": {
617 | "color": {
618 | "red": 0,
619 | "green": 0,
620 | "blue": 0,
621 | "alpha": 0
622 | }
623 | }
624 | }
625 | },
626 | {
627 | "Border": {
628 | "bounds": {
629 | "position": {
630 | "left": 120,
631 | "top": 48
632 | },
633 | "size": {
634 | "width": 360,
635 | "height": 25
636 | }
637 | },
638 | "display": {
639 | "widths": [
640 | 0,
641 | 0,
642 | 0,
643 | 0
644 | ],
645 | "colors": [
646 | {
647 | "red": 0,
648 | "green": 0,
649 | "blue": 0,
650 | "alpha": 0
651 | },
652 | {
653 | "red": 0,
654 | "green": 0,
655 | "blue": 0,
656 | "alpha": 0
657 | },
658 | {
659 | "red": 0,
660 | "green": 0,
661 | "blue": 0,
662 | "alpha": 0
663 | },
664 | {
665 | "red": 0,
666 | "green": 0,
667 | "blue": 0,
668 | "alpha": 0
669 | }
670 | ],
671 | "styles": [
672 | "Solid",
673 | "Solid",
674 | "Solid",
675 | "Solid"
676 | ]
677 | }
678 | }
679 | },
680 | {
681 | "Text": {
682 | "bounds": {
683 | "position": {
684 | "left": 120,
685 | "top": 48
686 | },
687 | "size": {
688 | "width": 66,
689 | "height": 25
690 | }
691 | },
692 | "display": {
693 | "color": {
694 | "red": 0,
695 | "green": 0,
696 | "blue": 0,
697 | "alpha": 255
698 | },
699 | "source_text": [
700 | {
701 | "Static": "Hello world !"
702 | }
703 | ],
704 | "shaped_text": [
705 | {
706 | "font_key": 0,
707 | "font_instance_key": 1,
708 | "width_64": 4224,
709 | "height_64": 1088,
710 | "glyphs": [
711 | {
712 | "glyph_index": 43,
713 | "x_64": 0,
714 | "y_64": 768
715 | },
716 | {
717 | "glyph_index": 72,
718 | "x_64": 554,
719 | "y_64": 768
720 | },
721 | {
722 | "glyph_index": 79,
723 | "x_64": 981,
724 | "y_64": 768
725 | },
726 | {
727 | "glyph_index": 79,
728 | "x_64": 1151,
729 | "y_64": 768
730 | },
731 | {
732 | "glyph_index": 82,
733 | "x_64": 1321,
734 | "y_64": 768
735 | },
736 | {
737 | "glyph_index": 3,
738 | "x_64": 1748,
739 | "y_64": 768
740 | },
741 | {
742 | "glyph_index": 90,
743 | "x_64": 1962,
744 | "y_64": 768
745 | },
746 | {
747 | "glyph_index": 82,
748 | "x_64": 2516,
749 | "y_64": 768
750 | },
751 | {
752 | "glyph_index": 85,
753 | "x_64": 2943,
754 | "y_64": 768
755 | },
756 | {
757 | "glyph_index": 79,
758 | "x_64": 3199,
759 | "y_64": 768
760 | },
761 | {
762 | "glyph_index": 71,
763 | "x_64": 3369,
764 | "y_64": 768
765 | },
766 | {
767 | "glyph_index": 3,
768 | "x_64": 3796,
769 | "y_64": 768
770 | },
771 | {
772 | "glyph_index": 4,
773 | "x_64": 4010,
774 | "y_64": 768
775 | }
776 | ],
777 | "generation_id": 14732137242825294601
778 | }
779 | ]
780 | }
781 | }
782 | }
783 | ]
784 | "#;
785 |
786 | assert_eq!(
787 | serde_json::from_str::(&stringified).unwrap(),
788 | serde_json::from_str::(expected).unwrap()
789 | );
790 | }
791 |
792 | #[test]
793 | fn test_json_reflow_example_complex() {
794 | let mut stylesheet = css!("tests/fixtures/test_2.css");
795 |
796 | let mut tree = rsx! {
797 |
798 |
799 |
800 | Hello world!
801 |
802 |
803 | };
804 |
805 | let mut files = FileCache::new().unwrap();
806 |
807 | let image_path = "tests/fixtures/Quantum.png";
808 | assert!(files.add_file(image_path).is_ok());
809 |
810 | let image_keys = ImageKeysAPI::new(());
811 | let mut images = ImageCache::new(image_keys).unwrap();
812 |
813 | let image_id = ImageId::new(image_path);
814 | let image_bytes = files.get_file(image_path).unwrap();
815 | images.add_raw(image_id, image_bytes).unwrap();
816 |
817 | let font_path = "tests/fixtures/FreeSans.ttf";
818 | assert!(files.add_file(font_path).is_ok());
819 |
820 | let font_keys = FontKeysAPI::new(());
821 | let mut fonts = FontCache::new(font_keys).unwrap();
822 |
823 | let font_id = FontId::new("FreeSans");
824 | let font_bytes = files.get_file(font_path).unwrap();
825 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
826 |
827 | let resources = ResourceGroup::new(files, images, fonts);
828 | tree.generate_layout_tree(&resources);
829 |
830 | let width = 1024.0;
831 | let height = 768.0;
832 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
833 |
834 | let display_list = DisplayList::from(&mut tree).serialize();
835 | let stringified: String = display_list.into();
836 |
837 | let expected = r#"
838 | [
839 | {
840 | "Rect": {
841 | "bounds": {
842 | "position": {
843 | "left": 0,
844 | "top": 0
845 | },
846 | "size": {
847 | "width": 500,
848 | "height": 120
849 | }
850 | },
851 | "display": {
852 | "color": {
853 | "red": 255,
854 | "green": 0,
855 | "blue": 0,
856 | "alpha": 255
857 | }
858 | }
859 | }
860 | },
861 | {
862 | "Border": {
863 | "bounds": {
864 | "position": {
865 | "left": 0,
866 | "top": 0
867 | },
868 | "size": {
869 | "width": 500,
870 | "height": 120
871 | }
872 | },
873 | "display": {
874 | "widths": [
875 | 0,
876 | 0,
877 | 0,
878 | 0
879 | ],
880 | "colors": [
881 | {
882 | "red": 0,
883 | "green": 0,
884 | "blue": 0,
885 | "alpha": 0
886 | },
887 | {
888 | "red": 0,
889 | "green": 0,
890 | "blue": 0,
891 | "alpha": 0
892 | },
893 | {
894 | "red": 0,
895 | "green": 0,
896 | "blue": 0,
897 | "alpha": 0
898 | },
899 | {
900 | "red": 0,
901 | "green": 0,
902 | "blue": 0,
903 | "alpha": 0
904 | }
905 | ],
906 | "styles": [
907 | "Solid",
908 | "Solid",
909 | "Solid",
910 | "Solid"
911 | ]
912 | }
913 | }
914 | },
915 | {
916 | "Rect": {
917 | "bounds": {
918 | "position": {
919 | "left": 20,
920 | "top": 20
921 | },
922 | "size": {
923 | "width": 80,
924 | "height": 83
925 | }
926 | },
927 | "display": {
928 | "color": {
929 | "red": 0,
930 | "green": 128,
931 | "blue": 0,
932 | "alpha": 255
933 | }
934 | }
935 | }
936 | },
937 | {
938 | "Border": {
939 | "bounds": {
940 | "position": {
941 | "left": 20,
942 | "top": 20
943 | },
944 | "size": {
945 | "width": 80,
946 | "height": 83
947 | }
948 | },
949 | "display": {
950 | "widths": [
951 | 0,
952 | 0,
953 | 0,
954 | 0
955 | ],
956 | "colors": [
957 | {
958 | "red": 0,
959 | "green": 0,
960 | "blue": 0,
961 | "alpha": 0
962 | },
963 | {
964 | "red": 0,
965 | "green": 0,
966 | "blue": 0,
967 | "alpha": 0
968 | },
969 | {
970 | "red": 0,
971 | "green": 0,
972 | "blue": 0,
973 | "alpha": 0
974 | },
975 | {
976 | "red": 0,
977 | "green": 0,
978 | "blue": 0,
979 | "alpha": 0
980 | }
981 | ],
982 | "styles": [
983 | "Solid",
984 | "Solid",
985 | "Solid",
986 | "Solid"
987 | ]
988 | }
989 | }
990 | },
991 | {
992 | "Image": {
993 | "bounds": {
994 | "position": {
995 | "left": 20,
996 | "top": 20
997 | },
998 | "size": {
999 | "width": 80,
1000 | "height": 83
1001 | }
1002 | },
1003 | "display": {
1004 | "image_src": {
1005 | "Static": "tests/fixtures/Quantum.png"
1006 | },
1007 | "measured_image": {
1008 | "image_key": 0,
1009 | "size": [
1010 | 512,
1011 | 529
1012 | ]
1013 | }
1014 | }
1015 | }
1016 | },
1017 | {
1018 | "Rect": {
1019 | "bounds": {
1020 | "position": {
1021 | "left": 120,
1022 | "top": 48
1023 | },
1024 | "size": {
1025 | "width": 360,
1026 | "height": 25
1027 | }
1028 | },
1029 | "display": {
1030 | "color": {
1031 | "red": 0,
1032 | "green": 0,
1033 | "blue": 255,
1034 | "alpha": 255
1035 | }
1036 | }
1037 | }
1038 | },
1039 | {
1040 | "Border": {
1041 | "bounds": {
1042 | "position": {
1043 | "left": 120,
1044 | "top": 48
1045 | },
1046 | "size": {
1047 | "width": 360,
1048 | "height": 25
1049 | }
1050 | },
1051 | "display": {
1052 | "widths": [
1053 | 0,
1054 | 0,
1055 | 0,
1056 | 0
1057 | ],
1058 | "colors": [
1059 | {
1060 | "red": 0,
1061 | "green": 0,
1062 | "blue": 0,
1063 | "alpha": 0
1064 | },
1065 | {
1066 | "red": 0,
1067 | "green": 0,
1068 | "blue": 0,
1069 | "alpha": 0
1070 | },
1071 | {
1072 | "red": 0,
1073 | "green": 0,
1074 | "blue": 0,
1075 | "alpha": 0
1076 | },
1077 | {
1078 | "red": 0,
1079 | "green": 0,
1080 | "blue": 0,
1081 | "alpha": 0
1082 | }
1083 | ],
1084 | "styles": [
1085 | "Solid",
1086 | "Solid",
1087 | "Solid",
1088 | "Solid"
1089 | ]
1090 | }
1091 | }
1092 | },
1093 | {
1094 | "Text": {
1095 | "bounds": {
1096 | "position": {
1097 | "left": 120,
1098 | "top": 48
1099 | },
1100 | "size": {
1101 | "width": 66,
1102 | "height": 25
1103 | }
1104 | },
1105 | "display": {
1106 | "color": {
1107 | "red": 255,
1108 | "green": 255,
1109 | "blue": 0,
1110 | "alpha": 255
1111 | },
1112 | "source_text": [
1113 | {
1114 | "Static": "Hello world !"
1115 | }
1116 | ],
1117 | "shaped_text": [
1118 | {
1119 | "font_key": 0,
1120 | "font_instance_key": 1,
1121 | "width_64": 4224,
1122 | "height_64": 1088,
1123 | "glyphs": [
1124 | {
1125 | "glyph_index": 43,
1126 | "x_64": 0,
1127 | "y_64": 768
1128 | },
1129 | {
1130 | "glyph_index": 72,
1131 | "x_64": 554,
1132 | "y_64": 768
1133 | },
1134 | {
1135 | "glyph_index": 79,
1136 | "x_64": 981,
1137 | "y_64": 768
1138 | },
1139 | {
1140 | "glyph_index": 79,
1141 | "x_64": 1151,
1142 | "y_64": 768
1143 | },
1144 | {
1145 | "glyph_index": 82,
1146 | "x_64": 1321,
1147 | "y_64": 768
1148 | },
1149 | {
1150 | "glyph_index": 3,
1151 | "x_64": 1748,
1152 | "y_64": 768
1153 | },
1154 | {
1155 | "glyph_index": 90,
1156 | "x_64": 1962,
1157 | "y_64": 768
1158 | },
1159 | {
1160 | "glyph_index": 82,
1161 | "x_64": 2516,
1162 | "y_64": 768
1163 | },
1164 | {
1165 | "glyph_index": 85,
1166 | "x_64": 2943,
1167 | "y_64": 768
1168 | },
1169 | {
1170 | "glyph_index": 79,
1171 | "x_64": 3199,
1172 | "y_64": 768
1173 | },
1174 | {
1175 | "glyph_index": 71,
1176 | "x_64": 3369,
1177 | "y_64": 768
1178 | },
1179 | {
1180 | "glyph_index": 3,
1181 | "x_64": 3796,
1182 | "y_64": 768
1183 | },
1184 | {
1185 | "glyph_index": 4,
1186 | "x_64": 4010,
1187 | "y_64": 768
1188 | }
1189 | ],
1190 | "generation_id": 14732137242825294601
1191 | }
1192 | ]
1193 | }
1194 | }
1195 | }
1196 | ]
1197 | "#;
1198 |
1199 | assert_eq!(
1200 | serde_json::from_str::(&stringified).unwrap(),
1201 | serde_json::from_str::(expected).unwrap()
1202 | );
1203 | }
1204 |
1205 | #[test]
1206 | fn test_json_reflow_example_compound_1() {
1207 | let mut stylesheet = css!("tests/fixtures/test_2.css");
1208 |
1209 | let mut tree = rsx! {
1210 |
1211 |
1212 |
1213 | {"Hello "}{"world !"}
1214 |
1215 |
1216 | };
1217 |
1218 | let mut files = FileCache::new().unwrap();
1219 |
1220 | let image_path = "tests/fixtures/Quantum.png";
1221 | assert!(files.add_file(image_path).is_ok());
1222 |
1223 | let image_keys = ImageKeysAPI::new(());
1224 | let mut images = ImageCache::new(image_keys).unwrap();
1225 |
1226 | let image_id = ImageId::new(image_path);
1227 | let image_bytes = files.get_file(image_path).unwrap();
1228 | images.add_raw(image_id, image_bytes).unwrap();
1229 |
1230 | let font_path = "tests/fixtures/FreeSans.ttf";
1231 | assert!(files.add_file(font_path).is_ok());
1232 |
1233 | let font_keys = FontKeysAPI::new(());
1234 | let mut fonts = FontCache::new(font_keys).unwrap();
1235 |
1236 | let font_id = FontId::new("FreeSans");
1237 | let font_bytes = files.get_file(font_path).unwrap();
1238 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
1239 |
1240 | let resources = ResourceGroup::new(files, images, fonts);
1241 | tree.generate_layout_tree(&resources);
1242 |
1243 | let width = 1024.0;
1244 | let height = 768.0;
1245 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
1246 |
1247 | let display_list = DisplayList::from(&mut tree).serialize();
1248 | let stringified: String = display_list.into();
1249 |
1250 | let expected = r#"
1251 | [
1252 | {
1253 | "Rect": {
1254 | "bounds": {
1255 | "position": {
1256 | "left": 0,
1257 | "top": 0
1258 | },
1259 | "size": {
1260 | "width": 500,
1261 | "height": 120
1262 | }
1263 | },
1264 | "display": {
1265 | "color": {
1266 | "red": 255,
1267 | "green": 0,
1268 | "blue": 0,
1269 | "alpha": 255
1270 | }
1271 | }
1272 | }
1273 | },
1274 | {
1275 | "Border": {
1276 | "bounds": {
1277 | "position": {
1278 | "left": 0,
1279 | "top": 0
1280 | },
1281 | "size": {
1282 | "width": 500,
1283 | "height": 120
1284 | }
1285 | },
1286 | "display": {
1287 | "widths": [
1288 | 0,
1289 | 0,
1290 | 0,
1291 | 0
1292 | ],
1293 | "colors": [
1294 | {
1295 | "red": 0,
1296 | "green": 0,
1297 | "blue": 0,
1298 | "alpha": 0
1299 | },
1300 | {
1301 | "red": 0,
1302 | "green": 0,
1303 | "blue": 0,
1304 | "alpha": 0
1305 | },
1306 | {
1307 | "red": 0,
1308 | "green": 0,
1309 | "blue": 0,
1310 | "alpha": 0
1311 | },
1312 | {
1313 | "red": 0,
1314 | "green": 0,
1315 | "blue": 0,
1316 | "alpha": 0
1317 | }
1318 | ],
1319 | "styles": [
1320 | "Solid",
1321 | "Solid",
1322 | "Solid",
1323 | "Solid"
1324 | ]
1325 | }
1326 | }
1327 | },
1328 | {
1329 | "Rect": {
1330 | "bounds": {
1331 | "position": {
1332 | "left": 20,
1333 | "top": 20
1334 | },
1335 | "size": {
1336 | "width": 80,
1337 | "height": 83
1338 | }
1339 | },
1340 | "display": {
1341 | "color": {
1342 | "red": 0,
1343 | "green": 128,
1344 | "blue": 0,
1345 | "alpha": 255
1346 | }
1347 | }
1348 | }
1349 | },
1350 | {
1351 | "Border": {
1352 | "bounds": {
1353 | "position": {
1354 | "left": 20,
1355 | "top": 20
1356 | },
1357 | "size": {
1358 | "width": 80,
1359 | "height": 83
1360 | }
1361 | },
1362 | "display": {
1363 | "widths": [
1364 | 0,
1365 | 0,
1366 | 0,
1367 | 0
1368 | ],
1369 | "colors": [
1370 | {
1371 | "red": 0,
1372 | "green": 0,
1373 | "blue": 0,
1374 | "alpha": 0
1375 | },
1376 | {
1377 | "red": 0,
1378 | "green": 0,
1379 | "blue": 0,
1380 | "alpha": 0
1381 | },
1382 | {
1383 | "red": 0,
1384 | "green": 0,
1385 | "blue": 0,
1386 | "alpha": 0
1387 | },
1388 | {
1389 | "red": 0,
1390 | "green": 0,
1391 | "blue": 0,
1392 | "alpha": 0
1393 | }
1394 | ],
1395 | "styles": [
1396 | "Solid",
1397 | "Solid",
1398 | "Solid",
1399 | "Solid"
1400 | ]
1401 | }
1402 | }
1403 | },
1404 | {
1405 | "Image": {
1406 | "bounds": {
1407 | "position": {
1408 | "left": 20,
1409 | "top": 20
1410 | },
1411 | "size": {
1412 | "width": 80,
1413 | "height": 83
1414 | }
1415 | },
1416 | "display": {
1417 | "image_src": {
1418 | "Static": "tests/fixtures/Quantum.png"
1419 | },
1420 | "measured_image": {
1421 | "image_key": 0,
1422 | "size": [
1423 | 512,
1424 | 529
1425 | ]
1426 | }
1427 | }
1428 | }
1429 | },
1430 | {
1431 | "Rect": {
1432 | "bounds": {
1433 | "position": {
1434 | "left": 120,
1435 | "top": 48
1436 | },
1437 | "size": {
1438 | "width": 360,
1439 | "height": 25
1440 | }
1441 | },
1442 | "display": {
1443 | "color": {
1444 | "red": 0,
1445 | "green": 0,
1446 | "blue": 255,
1447 | "alpha": 255
1448 | }
1449 | }
1450 | }
1451 | },
1452 | {
1453 | "Border": {
1454 | "bounds": {
1455 | "position": {
1456 | "left": 120,
1457 | "top": 48
1458 | },
1459 | "size": {
1460 | "width": 360,
1461 | "height": 25
1462 | }
1463 | },
1464 | "display": {
1465 | "widths": [
1466 | 0,
1467 | 0,
1468 | 0,
1469 | 0
1470 | ],
1471 | "colors": [
1472 | {
1473 | "red": 0,
1474 | "green": 0,
1475 | "blue": 0,
1476 | "alpha": 0
1477 | },
1478 | {
1479 | "red": 0,
1480 | "green": 0,
1481 | "blue": 0,
1482 | "alpha": 0
1483 | },
1484 | {
1485 | "red": 0,
1486 | "green": 0,
1487 | "blue": 0,
1488 | "alpha": 0
1489 | },
1490 | {
1491 | "red": 0,
1492 | "green": 0,
1493 | "blue": 0,
1494 | "alpha": 0
1495 | }
1496 | ],
1497 | "styles": [
1498 | "Solid",
1499 | "Solid",
1500 | "Solid",
1501 | "Solid"
1502 | ]
1503 | }
1504 | }
1505 | },
1506 | {
1507 | "Text": {
1508 | "bounds": {
1509 | "position": {
1510 | "left": 120,
1511 | "top": 48
1512 | },
1513 | "size": {
1514 | "width": 66,
1515 | "height": 25
1516 | }
1517 | },
1518 | "display": {
1519 | "color": {
1520 | "red": 255,
1521 | "green": 255,
1522 | "blue": 0,
1523 | "alpha": 255
1524 | },
1525 | "source_text": [
1526 | {
1527 | "Static": "Hello "
1528 | },
1529 | {
1530 | "Static": "world !"
1531 | }
1532 | ],
1533 | "shaped_text": [
1534 | {
1535 | "font_key": 0,
1536 | "font_instance_key": 1,
1537 | "width_64": 1962,
1538 | "height_64": 1088,
1539 | "glyphs": [
1540 | {
1541 | "glyph_index": 43,
1542 | "x_64": 0,
1543 | "y_64": 768
1544 | },
1545 | {
1546 | "glyph_index": 72,
1547 | "x_64": 554,
1548 | "y_64": 768
1549 | },
1550 | {
1551 | "glyph_index": 79,
1552 | "x_64": 981,
1553 | "y_64": 768
1554 | },
1555 | {
1556 | "glyph_index": 79,
1557 | "x_64": 1151,
1558 | "y_64": 768
1559 | },
1560 | {
1561 | "glyph_index": 82,
1562 | "x_64": 1321,
1563 | "y_64": 768
1564 | },
1565 | {
1566 | "glyph_index": 3,
1567 | "x_64": 1748,
1568 | "y_64": 768
1569 | }
1570 | ],
1571 | "generation_id": 12598805059617695050
1572 | },
1573 | {
1574 | "font_key": 0,
1575 | "font_instance_key": 1,
1576 | "width_64": 2262,
1577 | "height_64": 1088,
1578 | "glyphs": [
1579 | {
1580 | "glyph_index": 90,
1581 | "x_64": 0,
1582 | "y_64": 768
1583 | },
1584 | {
1585 | "glyph_index": 82,
1586 | "x_64": 554,
1587 | "y_64": 768
1588 | },
1589 | {
1590 | "glyph_index": 85,
1591 | "x_64": 981,
1592 | "y_64": 768
1593 | },
1594 | {
1595 | "glyph_index": 79,
1596 | "x_64": 1237,
1597 | "y_64": 768
1598 | },
1599 | {
1600 | "glyph_index": 71,
1601 | "x_64": 1407,
1602 | "y_64": 768
1603 | },
1604 | {
1605 | "glyph_index": 3,
1606 | "x_64": 1834,
1607 | "y_64": 768
1608 | },
1609 | {
1610 | "glyph_index": 4,
1611 | "x_64": 2048,
1612 | "y_64": 768
1613 | }
1614 | ],
1615 | "generation_id": 8713082891591790693
1616 | }
1617 | ]
1618 | }
1619 | }
1620 | }
1621 | ]
1622 | "#;
1623 |
1624 | assert_eq!(
1625 | serde_json::from_str::(&stringified).unwrap(),
1626 | serde_json::from_str::(expected).unwrap()
1627 | );
1628 | }
1629 |
1630 | #[test]
1631 | fn test_json_reflow_example_compound_2() {
1632 | let mut stylesheet = css!("tests/fixtures/test_2.css");
1633 |
1634 | let mut tree = rsx! {
1635 |
1636 |
1637 |
1638 | {"H"}{"e"}{"l"}{"l"}{"o"}{" "}{"w"}{"o"}{"r"}{"l"}{"d"}{" "}{"!"}
1639 |
1640 |
1641 | };
1642 |
1643 | let mut files = FileCache::new().unwrap();
1644 |
1645 | let image_path = "tests/fixtures/Quantum.png";
1646 | assert!(files.add_file(image_path).is_ok());
1647 |
1648 | let image_keys = ImageKeysAPI::new(());
1649 | let mut images = ImageCache::new(image_keys).unwrap();
1650 |
1651 | let image_id = ImageId::new(image_path);
1652 | let image_bytes = files.get_file(image_path).unwrap();
1653 | images.add_raw(image_id, image_bytes).unwrap();
1654 |
1655 | let font_path = "tests/fixtures/FreeSans.ttf";
1656 | assert!(files.add_file(font_path).is_ok());
1657 |
1658 | let font_keys = FontKeysAPI::new(());
1659 | let mut fonts = FontCache::new(font_keys).unwrap();
1660 |
1661 | let font_id = FontId::new("FreeSans");
1662 | let font_bytes = files.get_file(font_path).unwrap();
1663 | fonts.add_raw(font_id, font_bytes, 0).unwrap();
1664 |
1665 | let resources = ResourceGroup::new(files, images, fonts);
1666 | tree.generate_layout_tree(&resources);
1667 |
1668 | let width = 1024.0;
1669 | let height = 768.0;
1670 | tree.reflow_subtree(width as u32, height as u32, LayoutReflowDirection::LTR);
1671 |
1672 | let display_list = DisplayList::from(&mut tree).serialize();
1673 | let stringified: String = display_list.into();
1674 |
1675 | let expected = r#"
1676 | [
1677 | {
1678 | "Rect": {
1679 | "bounds": {
1680 | "position": {
1681 | "left": 0,
1682 | "top": 0
1683 | },
1684 | "size": {
1685 | "width": 500,
1686 | "height": 120
1687 | }
1688 | },
1689 | "display": {
1690 | "color": {
1691 | "red": 255,
1692 | "green": 0,
1693 | "blue": 0,
1694 | "alpha": 255
1695 | }
1696 | }
1697 | }
1698 | },
1699 | {
1700 | "Border": {
1701 | "bounds": {
1702 | "position": {
1703 | "left": 0,
1704 | "top": 0
1705 | },
1706 | "size": {
1707 | "width": 500,
1708 | "height": 120
1709 | }
1710 | },
1711 | "display": {
1712 | "widths": [
1713 | 0,
1714 | 0,
1715 | 0,
1716 | 0
1717 | ],
1718 | "colors": [
1719 | {
1720 | "red": 0,
1721 | "green": 0,
1722 | "blue": 0,
1723 | "alpha": 0
1724 | },
1725 | {
1726 | "red": 0,
1727 | "green": 0,
1728 | "blue": 0,
1729 | "alpha": 0
1730 | },
1731 | {
1732 | "red": 0,
1733 | "green": 0,
1734 | "blue": 0,
1735 | "alpha": 0
1736 | },
1737 | {
1738 | "red": 0,
1739 | "green": 0,
1740 | "blue": 0,
1741 | "alpha": 0
1742 | }
1743 | ],
1744 | "styles": [
1745 | "Solid",
1746 | "Solid",
1747 | "Solid",
1748 | "Solid"
1749 | ]
1750 | }
1751 | }
1752 | },
1753 | {
1754 | "Rect": {
1755 | "bounds": {
1756 | "position": {
1757 | "left": 20,
1758 | "top": 20
1759 | },
1760 | "size": {
1761 | "width": 80,
1762 | "height": 83
1763 | }
1764 | },
1765 | "display": {
1766 | "color": {
1767 | "red": 0,
1768 | "green": 128,
1769 | "blue": 0,
1770 | "alpha": 255
1771 | }
1772 | }
1773 | }
1774 | },
1775 | {
1776 | "Border": {
1777 | "bounds": {
1778 | "position": {
1779 | "left": 20,
1780 | "top": 20
1781 | },
1782 | "size": {
1783 | "width": 80,
1784 | "height": 83
1785 | }
1786 | },
1787 | "display": {
1788 | "widths": [
1789 | 0,
1790 | 0,
1791 | 0,
1792 | 0
1793 | ],
1794 | "colors": [
1795 | {
1796 | "red": 0,
1797 | "green": 0,
1798 | "blue": 0,
1799 | "alpha": 0
1800 | },
1801 | {
1802 | "red": 0,
1803 | "green": 0,
1804 | "blue": 0,
1805 | "alpha": 0
1806 | },
1807 | {
1808 | "red": 0,
1809 | "green": 0,
1810 | "blue": 0,
1811 | "alpha": 0
1812 | },
1813 | {
1814 | "red": 0,
1815 | "green": 0,
1816 | "blue": 0,
1817 | "alpha": 0
1818 | }
1819 | ],
1820 | "styles": [
1821 | "Solid",
1822 | "Solid",
1823 | "Solid",
1824 | "Solid"
1825 | ]
1826 | }
1827 | }
1828 | },
1829 | {
1830 | "Image": {
1831 | "bounds": {
1832 | "position": {
1833 | "left": 20,
1834 | "top": 20
1835 | },
1836 | "size": {
1837 | "width": 80,
1838 | "height": 83
1839 | }
1840 | },
1841 | "display": {
1842 | "image_src": {
1843 | "Static": "tests/fixtures/Quantum.png"
1844 | },
1845 | "measured_image": {
1846 | "image_key": 0,
1847 | "size": [
1848 | 512,
1849 | 529
1850 | ]
1851 | }
1852 | }
1853 | }
1854 | },
1855 | {
1856 | "Rect": {
1857 | "bounds": {
1858 | "position": {
1859 | "left": 120,
1860 | "top": 48
1861 | },
1862 | "size": {
1863 | "width": 360,
1864 | "height": 25
1865 | }
1866 | },
1867 | "display": {
1868 | "color": {
1869 | "red": 0,
1870 | "green": 0,
1871 | "blue": 255,
1872 | "alpha": 255
1873 | }
1874 | }
1875 | }
1876 | },
1877 | {
1878 | "Border": {
1879 | "bounds": {
1880 | "position": {
1881 | "left": 120,
1882 | "top": 48
1883 | },
1884 | "size": {
1885 | "width": 360,
1886 | "height": 25
1887 | }
1888 | },
1889 | "display": {
1890 | "widths": [
1891 | 0,
1892 | 0,
1893 | 0,
1894 | 0
1895 | ],
1896 | "colors": [
1897 | {
1898 | "red": 0,
1899 | "green": 0,
1900 | "blue": 0,
1901 | "alpha": 0
1902 | },
1903 | {
1904 | "red": 0,
1905 | "green": 0,
1906 | "blue": 0,
1907 | "alpha": 0
1908 | },
1909 | {
1910 | "red": 0,
1911 | "green": 0,
1912 | "blue": 0,
1913 | "alpha": 0
1914 | },
1915 | {
1916 | "red": 0,
1917 | "green": 0,
1918 | "blue": 0,
1919 | "alpha": 0
1920 | }
1921 | ],
1922 | "styles": [
1923 | "Solid",
1924 | "Solid",
1925 | "Solid",
1926 | "Solid"
1927 | ]
1928 | }
1929 | }
1930 | },
1931 | {
1932 | "Text": {
1933 | "bounds": {
1934 | "position": {
1935 | "left": 120,
1936 | "top": 48
1937 | },
1938 | "size": {
1939 | "width": 65,
1940 | "height": 25
1941 | }
1942 | },
1943 | "display": {
1944 | "color": {
1945 | "red": 255,
1946 | "green": 255,
1947 | "blue": 0,
1948 | "alpha": 255
1949 | },
1950 | "source_text": [
1951 | {
1952 | "Static": "H"
1953 | },
1954 | {
1955 | "Static": "e"
1956 | },
1957 | {
1958 | "Static": "l"
1959 | },
1960 | {
1961 | "Static": "l"
1962 | },
1963 | {
1964 | "Static": "o"
1965 | },
1966 | {
1967 | "Static": " "
1968 | },
1969 | {
1970 | "Static": "w"
1971 | },
1972 | {
1973 | "Static": "o"
1974 | },
1975 | {
1976 | "Static": "r"
1977 | },
1978 | {
1979 | "Static": "l"
1980 | },
1981 | {
1982 | "Static": "d"
1983 | },
1984 | {
1985 | "Static": " "
1986 | },
1987 | {
1988 | "Static": "!"
1989 | }
1990 | ],
1991 | "shaped_text": [
1992 | {
1993 | "font_key": 0,
1994 | "font_instance_key": 1,
1995 | "width_64": 554,
1996 | "height_64": 1088,
1997 | "glyphs": [
1998 | {
1999 | "glyph_index": 43,
2000 | "x_64": 0,
2001 | "y_64": 768
2002 | }
2003 | ],
2004 | "generation_id": 659716905384036824
2005 | },
2006 | {
2007 | "font_key": 0,
2008 | "font_instance_key": 1,
2009 | "width_64": 427,
2010 | "height_64": 1088,
2011 | "glyphs": [
2012 | {
2013 | "glyph_index": 72,
2014 | "x_64": 0,
2015 | "y_64": 768
2016 | }
2017 | ],
2018 | "generation_id": 616369758961961485
2019 | },
2020 | {
2021 | "font_key": 0,
2022 | "font_instance_key": 1,
2023 | "width_64": 170,
2024 | "height_64": 1088,
2025 | "glyphs": [
2026 | {
2027 | "glyph_index": 79,
2028 | "x_64": 0,
2029 | "y_64": 768
2030 | }
2031 | ],
2032 | "generation_id": 625376958218562972
2033 | },
2034 | {
2035 | "font_key": 0,
2036 | "font_instance_key": 1,
2037 | "width_64": 170,
2038 | "height_64": 1088,
2039 | "glyphs": [
2040 | {
2041 | "glyph_index": 79,
2042 | "x_64": 0,
2043 | "y_64": 768
2044 | }
2045 | ],
2046 | "generation_id": 625376958218562972
2047 | },
2048 | {
2049 | "font_key": 0,
2050 | "font_instance_key": 1,
2051 | "width_64": 427,
2052 | "height_64": 1088,
2053 | "glyphs": [
2054 | {
2055 | "glyph_index": 82,
2056 | "x_64": 0,
2057 | "y_64": 768
2058 | }
2059 | ],
2060 | "generation_id": 625939908172017779
2061 | },
2062 | {
2063 | "font_key": 0,
2064 | "font_instance_key": 1,
2065 | "width_64": 214,
2066 | "height_64": 1088,
2067 | "glyphs": [
2068 | {
2069 | "glyph_index": 3,
2070 | "x_64": 0,
2071 | "y_64": 768
2072 | }
2073 | ],
2074 | "generation_id": 560074763608722560
2075 | },
2076 | {
2077 | "font_key": 0,
2078 | "font_instance_key": 1,
2079 | "width_64": 554,
2080 | "height_64": 1088,
2081 | "glyphs": [
2082 | {
2083 | "glyph_index": 90,
2084 | "x_64": 0,
2085 | "y_64": 768
2086 | }
2087 | ],
2088 | "generation_id": 633821207521520427
2089 | },
2090 | {
2091 | "font_key": 0,
2092 | "font_instance_key": 1,
2093 | "width_64": 427,
2094 | "height_64": 1088,
2095 | "glyphs": [
2096 | {
2097 | "glyph_index": 82,
2098 | "x_64": 0,
2099 | "y_64": 768
2100 | }
2101 | ],
2102 | "generation_id": 625939908172017779
2103 | },
2104 | {
2105 | "font_key": 0,
2106 | "font_instance_key": 1,
2107 | "width_64": 256,
2108 | "height_64": 1088,
2109 | "glyphs": [
2110 | {
2111 | "glyph_index": 85,
2112 | "x_64": 0,
2113 | "y_64": 768
2114 | }
2115 | ],
2116 | "generation_id": 638324807149726558
2117 | },
2118 | {
2119 | "font_key": 0,
2120 | "font_instance_key": 1,
2121 | "width_64": 170,
2122 | "height_64": 1088,
2123 | "glyphs": [
2124 | {
2125 | "glyph_index": 79,
2126 | "x_64": 0,
2127 | "y_64": 768
2128 | }
2129 | ],
2130 | "generation_id": 625376958218562972
2131 | },
2132 | {
2133 | "font_key": 0,
2134 | "font_instance_key": 1,
2135 | "width_64": 427,
2136 | "height_64": 1088,
2137 | "glyphs": [
2138 | {
2139 | "glyph_index": 71,
2140 | "x_64": 0,
2141 | "y_64": 768
2142 | }
2143 | ],
2144 | "generation_id": 617495658869060324
2145 | },
2146 | {
2147 | "font_key": 0,
2148 | "font_instance_key": 1,
2149 | "width_64": 214,
2150 | "height_64": 1088,
2151 | "glyphs": [
2152 | {
2153 | "glyph_index": 3,
2154 | "x_64": 0,
2155 | "y_64": 768
2156 | }
2157 | ],
2158 | "generation_id": 560074763608722560
2159 | },
2160 | {
2161 | "font_key": 0,
2162 | "font_instance_key": 1,
2163 | "width_64": 214,
2164 | "height_64": 1088,
2165 | "glyphs": [
2166 | {
2167 | "glyph_index": 4,
2168 | "x_64": 0,
2169 | "y_64": 768
2170 | }
2171 | ],
2172 | "generation_id": 558948863701623721
2173 | }
2174 | ]
2175 | }
2176 | }
2177 | }
2178 | ]
2179 | "#;
2180 |
2181 | assert_eq!(
2182 | serde_json::from_str::(&stringified).unwrap(),
2183 | serde_json::from_str::(expected).unwrap()
2184 | );
2185 | }
2186 |
--------------------------------------------------------------------------------