1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (c) 2018-2021 Thomas Kramer.
 *
 * This file is part of LibrEDA 
 * (see https://codeberg.org/libreda/libreda-oasis).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

//! Library for reading and writing OASIS files.
//!
//! OASIS is a binary format for storing two-dimensional geometrical data as it is commonly used
//! for silicon chip layouts. Its purpose is very similar to the older GDS2 format.
//!
//! # Examples
//!
//! ## Read a layout from OASIS
//!
//! ```
//! use std::fs::File;
//! use libreda_oasis::OASISStreamReader;
//! // Import the `LayoutStreamReader` trait.
//! use libreda_db::prelude::{Layout, LayoutStreamReader};
//!
//! let filename = "./tests/data/INVX1_no_compression.oas";
//! // Open the OASIS file for reading.
//! let mut f = File::open(filename).unwrap();
//!
//! // Create an empty layout that will be populated by the OASIS reader.
//! let mut layout = Layout::new();
//!
//! // Create a default OASIS reader and parse the data from the file.
//! let result = OASISStreamReader::default()
//!     .read_layout(&mut f, &mut layout);
//!
//! // Assert that there was no error.
//! assert!(result.is_ok());
//! ```
//!
//! ## Write a layout to OASIS
//!
//! ```
//! use std::fs::File;
//! use libreda_oasis::OASISStreamWriter;
//! // Import the `LayoutStreamReader` trait.
//! use libreda_db::prelude::{Layout, LayoutStreamWriter};
//!
//! // Create an empty layout.
//! let layout = Layout::new();
//!
//! let mut f = File::create("./tests/data/empty_layout_out.oas").unwrap();
//!
//! let writer = OASISStreamWriter::default();
//!
//! // Write the (empty) layout to the file.
//! let write_result = writer.write_layout(&mut f, &layout);
//!
//! // Assert that there was no error.
//! assert!(write_result.is_ok());
//! ```

#![deny(missing_docs)]

// As long as there are some unimplemented parts be less strict with warnings.
// TODO: Remove this.
#![allow(dead_code, unused_assignments)]

#[macro_use]
extern crate log;

pub use libreda_db;

use libreda_db as db;
use db::layout::prelude::*;

mod base_types;
mod reader;
mod writer;

use base_types::*;
use crate::base_types::Repetition;
use std::rc::Rc;
use std::io::{Read, Write};

pub use writer::OASISWriterConfig;
pub use libreda_db::prelude::{LayoutStreamReader, LayoutStreamWriter};

/// Reader for the OASIS file format.
#[derive(Clone, Default, Debug)]
pub struct OASISStreamReader {}

impl OASISStreamReader {
    /// Create a new OASIS reader.
    pub fn new() -> Self {
        OASISStreamReader {}
    }
}

/// Writer for the OASIS file format.
#[derive(Clone, Default, Debug)]
pub struct OASISStreamWriter {
    config: OASISWriterConfig
}

/// Reader implementation.
impl LayoutStreamReader for OASISStreamReader {
    type Error = OASISReadError;
    fn read_layout<R: Read>(&self, r: &mut R, layout: &mut Layout) -> Result<(), Self::Error> {
        reader::read_layout(r, layout)
    }
}

/// Writer implementation.
impl LayoutStreamWriter for OASISStreamWriter {
    type Error = OASISWriteError;
    fn write_layout<W: Write>(&self, w: &mut W, layout: &Layout) -> Result<(), Self::Error> {
        writer::write_layout(w, layout, &self.config)
    }
}

/// Type for the `xy-mode` modal variable.
#[derive(Copy, Clone, Debug)]
enum XYMode {
    Absolute,
    Relative,
}

impl Default for XYMode {
    fn default() -> Self {
        XYMode::Absolute
    }
}

/// Datatype for either a name string or a reference number to a name string.
/// In OASIS names can be given explicitly with a string or by reference number.
/// In case of a reference number, the name string might be specified later in the file.
#[derive(Debug, Clone)]
pub(crate) enum NameOrRef {
    /// A known name string.
    Name(String),
    /// A reference number to a name string.
    NameRef(UInt),
}

/// Modal variables.
/// Modal variables are used for implicit and context dependent values.
/// At the beginning at the file or of a CELL record all modal variables are set to `None` except
/// `placement_x`, `placement_y`, `geometry_x`, `geometry_y`, `text_x`, and `text_y`.
#[derive(Clone, Default, Debug)]
struct Modal {
    /// Related records: PLACEMENT, TEXT, POLYGON, PATH,
    /// RECTANGLE, TRAPEZOID, CTRAPEZOID,
    /// CIRCLE, XGEOMETRY
    repetition: Option<Repetition>,

    /// Related records: PLACEMENT
    placement_x: SInt,
    /// Related records: PLACEMENT
    placement_y: SInt,
    /// Reference to an implicit cell to be used in the PLACEMENT record.
    /// Related records: PLACEMENT
    placement_cell: Option<Rc<Cell<SInt>>>,

    /// Related records: POLYGON, PATH, RECTANGLE, TRAPEZOID,
    /// CTRAPEZOID, CIRCLE, XGEOMETRY
    layer: Option<UInt>,
    /// Related records: POLYGON, PATH, RECTANGLE, TRAPEZOID,
    /// CTRAPEZOID, CIRCLE, XGEOMETRY
    datatype: Option<UInt>,

    /// Related records: TEXT
    textlayer: Option<UInt>,
    /// Related records: TEXT
    texttype: Option<UInt>,
    /// Related records: TEXT
    text_x: SInt,
    /// Related records: TEXT
    text_y: SInt,
    /// Related records: TEXT
    text_string: Option<String>,

    /// Related records: POLYGON, PATH, RECTANGLE, TRAPEZOID
    /// CTRAPEZOID, CIRCLE, XGEOMETRY
    geometry_x: SInt,
    /// Related records: POLYGON, PATH, RECTANGLE, TRAPEZOID
    /// CTRAPEZOID, CIRCLE, XGEOMETRY
    geometry_y: SInt,

    /// Related records: PLACEMENT, TEXT, POLYGON, PATH,
    /// RECTANGLE, TRAPEZOID, CTRAPEZOID,
    /// CIRCLE, XGEOMETRY,
    /// XYABSOLUTE, XYRELATIVE
    /// Controls how `x` and `y` values are treated in the related records.
    xy_mode: XYMode,

    /// Related records: RECTANGLE, TRAPEZOID, CTRAPEZOID
    geometry_w: Option<UInt>,
    /// Related records: RECTANGLE, TRAPEZOID, CTRAPEZOID
    geometry_h: Option<UInt>,

    /// Related records: POLYGON
    polygon_point_list: Option<PointList>,

    /// Related records: PATH
    path_halfwidth: Option<UInt>,
    /// Related records: PATH
    path_point_list: Option<PointList>,
    /// Related records: PATH
    path_start_extension: Option<SInt>,
    /// Related records: PATH
    path_end_extension: Option<SInt>,

    /// Related records: CTRAPEZOID
    ctrapezoid_type: Option<()>,

    /// Related records: CIRCLE
    circle_radius: Option<()>,

    /// Related records: PROPERTY
    last_property_name: Option<NameOrRef>,
    /// Related records: PROPERTY
    last_value_list: Option<Vec<PropertyValue>>,
}

impl Modal {
    /// Set all modal variables to `None` except
    /// `placement_x`, `placement_y`, `geometry_x`, `geometry_y`, `text_x`, and `text_y` which are set to `0`.
    fn reset(&mut self) -> () {
        let reset = Self::default();
        *self = reset;
    }
}